chromium/chrome/common/compose/compose.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 compose.mojom;

import "components/compose/core/browser/compose_enums.mojom";

// Interface for setting up connection to allow calls between the untrusted
// compose dialog web page and native code.
interface ComposeSessionUntrustedPageHandlerFactory {
  // Called from untrusted dialog compose JS to set up native handler.
  CreateComposeSessionUntrustedPageHandler(
      pending_receiver<ComposeClientUntrustedPageHandler> client_handler,
      pending_receiver<ComposeSessionUntrustedPageHandler> handler,
      pending_remote<ComposeUntrustedDialog> dialog);
};

enum StyleModifier {
  kUnset,
  kFormal,
  kCasual,
  kLonger,
  kShorter,
  kRetry,
};

// The response to compose requests. `result` is only valid if `status`
// is kOk.
struct ComposeResponse {
  ComposeStatus status;
  string result;
  // Whether undo is possible to a prior state before this response.
  bool undo_available;
  // Whether redo is possible to a later state before this response.
  bool redo_available;
  // True if the result stored in this response came from a user edit and false
  // if it came from the server.
  bool provided_by_user;
  // Whether the response was generated on-device.
  bool on_device_evaluation_used;
  // Whether the request producing this response came from a modifier action.
  bool triggered_from_modifier;
};

// An intermediate compose response, which is available before the final
// response is complete.
struct PartialComposeResponse {
  string result;
};

// Snapshot of state that can describe current state or be used to
// fully restore a previous state.
struct ComposeState {
  // TODO(crbug.com/40285814): Change this field to a type that's opaque and
  // non-trivial to use from untrustworthy processes.
  // State for the WebUI session. This should be considered opaque and is
  // unsafe to parse in the browser process.
  string webui_state;
  // Response from compose service. This is not present if the request
  // has not been completed. The response is present if the request has
  // completed, even if the response status is an error.
  ComposeResponse? response;
  // Whether a request is pending. False can also mean that the
  // request was not made.
  bool has_pending_request;
  // Whether the user has pressed the thumbs up/down feedback buttons.
  UserFeedback feedback;
};

// Parameters used to configure the WebUI. These params are passed from the
// browser to the WebUI as part of OpenMetadata when the dialog opens. The
// values may be configured through Finch.
struct ConfigurableParams {
  // Word and character limits on the text input for the request.
  int32 min_word_limit;
  int32 max_word_limit;
  int32 max_character_limit;
};

// Information needed by the WebUI to show a new or restored
// compose dialog.
struct OpenMetadata {
  bool fre_complete;
  bool msbb_state;
  // Renderer provided text selection.
  string initial_input;
  // Whether or not the user selected text when opening the dialog.
  bool text_selected;
  ComposeState compose_state;
  ConfigurableParams configurable_params;
};

// The trigger for dismissing the compose dialog.
enum CloseReason {
  // The X button on the upper-right of the FRE dialog.
  kFirstRunCloseButton,
  // The X button on the upper-right of the main app dialog.
  kCloseButton,
  // The Insert button of the dialog.
  kInsertButton,
  // The MSBB dialog was shown and dismissed.
  kMSBBCloseButton,
};

//TODO(b/311697865) move this to a common location.
enum UserFeedback {
  // Unspecified.
  kUserFeedbackUnspecified,
  // A thumbs down.
  kUserFeedbackNegative,
  // A thumbs up.
  kUserFeedbackPositive,
};

// Interface for calls from the untrusted Compose dialog JS into the Browser
// process.
interface ComposeSessionUntrustedPageHandler {
  // Asks the browser to track a cancel edit action for logging purposes.
  LogCancelEdit();

  // Asks the Browser to compose `input`, informing the Browser if the compose
  // call was based on the user input being 'edited' in the dialog after the
  // initial compose response.
  Compose(string input, bool edited);

  // Asks the Browser to rewrite the last response. `style` specifies how the
  // response should be changed.
  // changes to the tone or length.
  Rewrite(StyleModifier style);

  // Asks the browser to track an edit input action for logging purposes.
  LogEditInput();

  // Asks the native handler to store `webui_state` for the last field the user
  // selected compose on.
  // TODO(crbug.com/40285814): Change this field to a type that's opaque and
  // non-trivial to use from untrustworthy processes.
  // State for the WebUI session. This should be considered opaque and is
  // unsafe to parse in the browser process.
  SaveWebUIState(string webui_state);

  // Inform the Browser that the response has been accepted by the user.
  // Should only be called if the ComposeResponse received is valid.
  // Boolean success indicates if the result was accepted successfully.
  AcceptComposeResult() => (bool success);

  // Asks the native handler for state information needed for opening the
  // compose dialog for the last field the user selected compose on.
  RequestInitialState() => (OpenMetadata initial_state);

  // Asks the native handler to return the `last_state`. A null return means
  // that there are no undoable states remaining. Calling `Undo()` and
  // simultaneously closing the dialog (i.e., disconnecting the IPC pipe) may
  // result in the state being undone, or remaining the same. This depends on
  // which action executes first. If the IPC pipe disconnects, the Promise will
  // reject/fail, which should not be confused with a null return. In that case,
  // an undoable state may exist, but the call failed due to the broken pipe.
  Undo() => (ComposeState? last_state);

  // Asks the native handler to revert to and return the saved state directly
  // preceding a filtered error state. A null return means that there is no such
  // valid state. No-op on rejected promise.
  RecoverFromErrorState() => (ComposeState? state_before_error);

  // Asks the native handler to move to and return the `next_state` in the saved
  // state history. This represents the state that the user saw previous to the
  // last Undo action. A null return means there is no redoable state available.
  // No-op on rejected promise.
  Redo() => (ComposeState? next_state);

  // Asks the Browser to open a new tab navigated to the Compose bug report
  // Buganizer.
  OpenBugReportingLink();

  // Asks the Browser to open a new tab navigated to the Compose Learn More
  // page.
  OpenComposeLearnMorePage();

  // Asks the Browser to open a new tab navigated to the Dogfood feedback
  // survey.
  OpenFeedbackSurveyLink();

  // Asks the Browser to open a new tab navigated to the sign in page.
  OpenSignInPage();

  // Asks the native handler to store the user feedback supplied by the user.
  SetUserFeedback(UserFeedback feedback);

  // Tells the native handler that the user has updated the compose results
  // to `new_result`. `is_edited` indicates whether the result text was
  // changed by the edit.
  EditResult(string new_result) => (bool is_edited);
};

// Interface for calls from the untrusted Compose dialog JS into the Browser
// process.
interface ComposeClientUntrustedPageHandler {
  // Asks the receiver to show the compose dialog.
  ShowUI();
  // Asks the receiver to close the compose dialog. `reason` describes
  // the user action that triggered the close.
  CloseUI(CloseReason reason);
  // Notifies the browser that the FRE was completed.
  CompleteFirstRun();
  // Asks the Browser to open a new tab navigated to the Compose-related Chrome
  // settings page.
  OpenComposeSettings();
};

// Interface for calls from the Browser process into the untrusted Compose
// dialog JS.
interface ComposeUntrustedDialog {
  // Called when a compose request is fulfilled, either by the model execution
  // service or due to an error. Because compose requests can outlive this
  // WebUI, this is an event rather than a callback on the original `Compose`
  // call above.
  ResponseReceived(ComposeResponse response);

  // Called when a compose request is not yet fulfilled, but a partial response
  // is available.
  PartialResponseReceived(PartialComposeResponse partial_response);
};