chromium/extensions/common/mojom/service_worker_host.mojom

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

module extensions.mojom;

import "mojo/public/mojom/base/unguessable_token.mojom";
import "mojo/public/mojom/base/uuid.mojom";
import "extensions/common/mojom/event_dispatcher.mojom";
import "extensions/common/mojom/extra_response_data.mojom";
import "extensions/common/mojom/frame.mojom";
import "mojo/public/mojom/base/values.mojom";
import "extensions/common/mojom/message_port.mojom";
import "third_party/blink/public/mojom/tokens/tokens.mojom";
import "url/mojom/url.mojom";

// An interface for an extension service worker context. Implemented in the
// browser process.
interface ServiceWorkerHost {
  // Tells the browser that an extension service worker context was initialized,
  // but possibly didn't start executing its top-level JavaScript.
  DidInitializeServiceWorkerContext(
      string extension_id,
      int64 service_worker_version_id,
      int32 worker_thread_id,
      blink.mojom.ServiceWorkerToken service_worker_token,
      pending_associated_remote<EventDispatcher> event_dispatcher);

  // Tells the browser that an extension service worker context has started and
  // finished executing its top-level JavaScript.
  // Start corresponds to EmbeddedWorkerInstance::OnStarted notification.
  //
  // TODO(crbug.com/40259636): This is a workaround: ideally this IPC should be
  // redundant because it directly corresponds to
  // EmbeddedWorkerInstance::OnStarted message. However, because OnStarted
  // message is on different mojo IPC pipe, and most extension IPCs are on
  // legacy IPC pipe, this IPC is necessary to ensure FIFO ordering of this
  // message with rest of the extension IPCs.
  // Two possible solutions to this:
  //   - Associate extension IPCs with Service Worker IPCs. This can be done
  //     (and will be a requirement) when extension IPCs are moved to mojo, but
  //     requires resolving or defining ordering dependencies amongst the
  //     extension messages, and any additional messages in Chrome.
  //   - Make Service Worker IPCs channel-associated so that there's FIFO
  //     guarantee between extension IPCs and Service Worker IPCs. This isn't
  //     straightforward as it changes SW IPC ordering with respect of rest of
  //     Chrome.
  // See https://crbug.com/879015#c4 for details.
  DidStartServiceWorkerContext(
      string extension_id,
      mojo_base.mojom.UnguessableToken activation_token,
      url.mojom.Url service_worker_scope,
      int64 service_worker_version_id,
      int32 worker_thread_id);

  // Tells the browser that an extension service worker context has been
  // destroyed.
  DidStopServiceWorkerContext(
      string extension_id,
      mojo_base.mojom.UnguessableToken activation_token,
      url.mojom.Url service_worker_scope,
      int64 service_worker_version_id,
      int32 worker_thread_id);

  // A service worker thread sends this message when an extension service worker
  // starts an API request. We use [UnlimitedSize] here because `params` may be
  // large with some extension function (ex. Storage API).
  [UnlimitedSize]
  RequestWorker(RequestParams params)
       => (bool success,
          mojo_base.mojom.ListValue response_wrapper,
          string error,
          ExtraResponseData? extra_data);

  // Optional Ack message sent to the browser to notify that the response to a
  // function has been processed.
  // The `request_uuid` is the UUID of the extension function.
  WorkerResponseAck(mojo_base.mojom.Uuid request_uuid);

  // Open a channel to all listening contexts owned by the extension with
  // the given ID.
  OpenChannelToExtension(
    extensions.mojom.ExternalConnectionInfo info,
    extensions.mojom.ChannelType channel_type,
    string channel_name, extensions.mojom.PortId port_id,
    pending_associated_remote<extensions.mojom.MessagePort> port,
    pending_associated_receiver<extensions.mojom.MessagePortHost> port_host);

  // Get a port handle to the native application.  The handle can be used for
  // sending messages to the extension.
  OpenChannelToNativeApp(
    string native_app_name, extensions.mojom.PortId port_id,
    pending_associated_remote<extensions.mojom.MessagePort> port,
    pending_associated_receiver<extensions.mojom.MessagePortHost> port_host);

  // Get a port handle to the given tab.  The handle can be used for sending
  // messages to the extension.
  OpenChannelToTab(
    int32 tab_id, int32 frame_id, string? document_id,
    extensions.mojom.ChannelType channel_type,
    string channel_name, extensions.mojom.PortId port_id,
    pending_associated_remote<extensions.mojom.MessagePort> port,
    pending_associated_receiver<extensions.mojom.MessagePortHost> port_host);
};