// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Message format for the MojoLPM fuzzer for the DomStorage interface.

syntax = "proto2";

package content.fuzzing.dom_storage.proto;

import "third_party/blink/public/mojom/dom_storage/session_storage_namespace.mojom.mojolpm.proto";
import "third_party/blink/public/mojom/dom_storage/storage_area.mojom.mojolpm.proto";

enum OriginId {
  ORIGIN_A = 0;       // https://example.com
  ORIGIN_B = 1;       // https://other.com
  ORIGIN_OPAQUE = 2;  // opaque origin
}

enum RendererProcessId {
  RENDERER_A = 67;
  RENDERER_B = 68;
}

// Bind a new StorageArea remote for local storage.
message NewLocalStorageAreaAction {
  required uint32 id = 1;
  optional OriginId origin_id = 2;
  optional RendererProcessId renderer_id = 3 [default = RENDERER_A];
}

// Bind a new StorageArea remote for session storage.
message NewSessionStorageAreaAction {
  required uint32 id = 1;
  required OriginId origin_id = 2;
  optional RendererProcessId renderer_id = 3 [default = RENDERER_A];
}

// Bind a SessionStorageNamespace remote for the primary namespace.
message NewSessionStorageNamespaceAction {
  required uint32 id = 1;
}

message NewPendingCloneAction {
  required uint32 id = 1;
}

message CloneNamespaceImmediateAction {}

// Bind a SessionStorageNamespace remote for a pending clone target namespace.
message NewTargetSessionStorageNamespaceAction {
  required uint32 id = 1;
  required uint32 remote_id = 2;
}

// Construct a metadata-backed source namespace and clone from it.
message OnDiskCloneAction {
  required uint32 id = 1;
}

// Request session storage memory purge.
message PurgeMemoryAction {}

// Delete session storage data for a storage key in the primary namespace.
message DeleteStorageKeyAction {
  required OriginId origin_id = 1;
}

// Posts a no-op to the named thread (UI or IO) and waits for the reply,
// draining work already queued on that thread before the fuzzer continues.
// This is not intended to create a specific ordering, but to allow the fuzzer
// to delay a later task until previous tasks have completed.
message RunThreadAction {
  enum ThreadId {
    UI = 0;
    IO = 1;
  }
  required ThreadId id = 1;
}

// Ask the DOMStorageContextWrapper to commit any pending writes to the backing
// store. This drives StorageAreaImpl::ScheduleImmediateCommit through the
// storage service and exercises the post-commit callback paths. Pair with
// RunThreadAction { id: IO } to drain the resulting replies.
message FlushStorageAction {}

// Actions that can be performed by the fuzzer.
message Action {
  oneof action {
    NewLocalStorageAreaAction new_local_storage_area = 1;
    mojolpm.blink.mojom.StorageArea.RemoteAction storage_area_remote_action = 2;
    NewSessionStorageAreaAction new_session_storage_area = 3;
    RunThreadAction run_thread = 4;
    FlushStorageAction flush_storage = 5;
    NewSessionStorageNamespaceAction new_session_storage_namespace = 6;
    mojolpm.blink.mojom.SessionStorageNamespace.RemoteAction
        session_storage_namespace_remote_action = 7;
    NewPendingCloneAction new_pending_clone = 8;
    NewTargetSessionStorageNamespaceAction
        new_target_session_storage_namespace = 9;
    CloneNamespaceImmediateAction clone_namespace_immediate = 10;
    OnDiskCloneAction on_disk_clone = 11;
    PurgeMemoryAction purge_memory = 12;
    DeleteStorageKeyAction delete_storage_key = 13;
  }
}

// Sequence provides a level of indirection which allows Testcase to compactly
// express repeated sequences of actions.
message Sequence {
  repeated uint32 action_indexes = 1 [packed = true];
}

// Testcase is the top-level message type interpreted by the fuzzer.
message Testcase {
  repeated Action actions = 1;
  repeated Sequence sequences = 2;
  repeated uint32 sequence_indexes = 3 [packed = true];
}
