// 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 mojom;

import "mojo/public/mojom/base/file_path.mojom";
import "url/mojom/origin.mojom";
import "url/mojom/url.mojom";

struct InstallIsolatedWebAppSuccess {
  string web_bundle_id;
};

union InstallIsolatedWebAppResult {
  InstallIsolatedWebAppSuccess success;
  string error;
};

// Describes how a manifest-installed app should perform update checks.
struct UpdateInfo {
  url.mojom.Url update_manifest_url;
  string update_channel;
  string? pinned_version;
  bool allow_downgrades = false;
};

// Dev mode IWAs are either installed from a proxy origin, or from a Web Bundle
// file, or via an update manifest url (the latter case is indicated by the
// presence of `update_info` in IwaDevModeInfo).
union IwaDevModeLocation {
  url.mojom.Origin proxy_origin;
  mojo_base.mojom.FilePath bundle_path;
};

struct IwaDevModeAppInfo {
  string app_id;
  string web_bundle_id;
  string name;
  IwaDevModeLocation location;
  string installed_version;

  UpdateInfo? update_info;
};

// Mimics web_app::UpdateManifest::VersionEntry.
struct VersionEntry {
  string version;
  url.mojom.Url web_bundle_url;
};

// Mimics web_app::UpdateManifest.
struct UpdateManifest {
  array<VersionEntry> versions;
};

union ParseUpdateManifestFromUrlResult {
  UpdateManifest update_manifest;
  string error;
};

// Parameters necessary for InstallIsolatedWebAppFromBundleUrl().
struct InstallFromBundleUrlParams {
  url.mojom.Url web_bundle_url;
  UpdateInfo update_info;
};

// Factory ensures that the handler interface is created with the WebUI page
// without requiring an initialization call from the WebUI to the handler.
interface PageHandlerFactory {
  // Creates the `handler` to be used for communication between the Browser and
  // the Renderer.
  CreateWebAppInternalsHandler(
      pending_receiver<WebAppInternalsHandler> handler);
};

// Handles requests from chrome://web-app-internals.
// This is expected to be hosted in the browser process.
interface WebAppInternalsHandler {
  // Returns Web App related debug information as a JSON string.
  GetDebugInfoAsJsonString() => (string result);

  // Returns whether the installation succeeded.
  InstallIsolatedWebAppFromDevProxy(url.mojom.Url url)
      => (InstallIsolatedWebAppResult result);

  // Returns whether the installation succeeded.
  SelectFileAndInstallIsolatedWebAppFromDevBundle()
      => (InstallIsolatedWebAppResult result);

  // Attempts to fetch & parse an IWA update manifest from the provided url.
  ParseUpdateManifestFromUrl(url.mojom.Url update_manifest_url)
      => (ParseUpdateManifestFromUrlResult result);

  // Attempts to download a web bundle & install an IWA in dev mode with the
  // provided `params`.
  InstallIsolatedWebAppFromBundleUrl(InstallFromBundleUrlParams params)
      => (InstallIsolatedWebAppResult result);

  // Triggers an update for a dev mode proxy app. Returns a string containing a
  // success or error message.
  UpdateDevProxyIsolatedWebApp(string app_id) => (string result);

  // Triggers an update for a dev mode bundle app by opening a file picker to
  // let the user choose a Signed Web Bundle. Returns a string containing a
  // success or error message.
  SelectFileAndUpdateIsolatedWebAppFromDevBundle(string app_id)
      => (string result);

  // Triggers an update for a manifest-installed dev mode app.
  // Returns a string containing a success or error message.
  UpdateManifestInstalledIsolatedWebApp(string app_id) => (string result);

  // Deletes the IWA.
  // Returns a boolean indicating success or failure of delete operation.
  DeleteIsolatedWebApp(string app_id) => (bool success);

  // Sets `web_app.isolation_data.update_channel` to `update_channel` for this
  // `app_id`.
  SetUpdateChannelForIsolatedWebApp(string app_id, string update_channel)
      => (bool success);

  // Sets `pinned_version` value for given `app_id`.
  SetPinnedVersionForIsolatedWebApp(string app_id, string pinned_version)
      => (bool success);

  // Unpins the IWA by setting `pinned_version` to null.
  ResetPinnedVersionForIsolatedWebApp(string app_id);

  // Sets the `allow_downgrades` value for a given `app_id`.
  SetAllowDowngradesForIsolatedWebApp(bool allow_downgrades, string app_id);

  // Triggers update discovery for installed non-dev-mode Isolated Web Apps.
  // Returns a string containing a success or error message.
  SearchForIsolatedWebAppUpdates() => (string result);

  // Returns information about installed dev mode IWAs.
  GetIsolatedWebAppDevModeAppInfo() => (array<IwaDevModeAppInfo> apps);
};
