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

syntax = "proto2";

import "components/sync/protocol/web_app_specifics.proto";
import "chrome/browser/web_applications/proto/web_app_launch_handler.proto";
import "chrome/browser/web_applications/proto/web_app_os_integration_state.proto";
import "chrome/browser/web_applications/proto/web_app_install_state.proto";
import "chrome/browser/web_applications/proto/web_app_isolation_data.proto";
import "chrome/browser/web_applications/proto/web_app_related_applications.proto";
import "chrome/browser/web_applications/proto/web_app_share_target.proto";
import "chrome/browser/web_applications/proto/web_app_tab_strip.proto";
import "chrome/browser/web_applications/proto/web_app_url_pattern.proto";
import "chromeos/ash/experiences/system_web_apps/types/proto/system_web_app_data.proto";

option optimize_for = LITE_RUNTIME;

package web_app.proto;

// A mapping between a MIME type and a set of file extensions for a file handler
// "accept" entry. See chrome/browser/web_applications/web_app.h for details.
message WebAppFileHandlerAccept {
  optional string mimetype = 1;  // Required.
  repeated string file_extensions = 2;
}

// A file handler "action" associated with a set of "accept" entries, each of
// which specifies a MIME type and corresponding set of file extensions that the
// handler can handle. See chrome/browser/web_applications/web_app.h for
// details.
message WebAppFileHandler {
  optional string action = 1;  // Required.
  repeated WebAppFileHandlerAccept accept = 2;
  reserved 3;
  optional string display_name = 4;

  // This enum should be synced with `LaunchType` in `apps::FileHandler`.
  enum LaunchType {
    LAUNCH_TYPE_UNSPECIFIED = 0;
    LAUNCH_TYPE_SINGLE_CLIENT = 1;
    LAUNCH_TYPE_MULTIPLE_CLIENTS = 2;
  }
  optional LaunchType launch_type = 5;  // Required.
}

enum LinkCapturingUserPreference {
  NAVIGATION_CAPTURING_PREFERENCE_DEFAULT = 0;
  NAVIGATION_CAPTURING_PREFERENCE_CAPTURE = 1;
  NAVIGATION_CAPTURING_PREFERENCE_DO_NOT_CAPTURE = 2;
}

message WebAppProtocolHandler {
  optional string protocol = 1;  // Required.
  optional string url = 2;       // Required.
}

// Represents a parsed and validated scope extension defined in the web app's
// manifest. The `scope` field is a full URL, not only the path.
message WebAppScopeExtension {
  optional string origin = 1;             // Required.
  optional bool has_origin_wildcard = 2;  // Required.
  optional string scope = 3;              // Required.
}

// A set to track simultaneous installs and uninstalls from multiple install
// sources. This should stay in sync with |WebAppManagement| in
// chrome/browser/web_applications/web_app_constants.h
// and the WebAppManagement enum below.
message Sources {
  optional bool system = 1;
  optional bool policy = 2;
  optional bool web_app_store = 3;
  optional bool sync = 4;
  optional bool default = 5;
  optional bool sub_app = 6;
  optional bool kiosk = 7;
  reserved 8;
  optional bool oem = 9;
  optional bool one_drive_integration = 10;
  optional bool aps_default = 11;
  optional bool iwa_shimless_rma = 12;
  optional bool iwa_policy = 13;
  optional bool iwa_user_installed = 14;
  optional bool user_installed = 15;
}

// Properties for integrating with Chrome OS.
message ChromeOSData {
  optional bool show_in_launcher = 1;
  optional bool show_in_search_and_shelf = 2;
  optional bool show_in_management = 3;
  optional bool is_disabled = 4;
  optional bool oem_installed = 5;
  optional bool handles_file_open_intents = 6;
  reserved 7;
}

message ClientData {
  optional ash.SystemWebAppDataProto system_web_app_data = 1;
}

// Properties for a WebApp's shortcuts menu item.
message WebAppShortcutsMenuItemInfo {
  optional string name = 1;  // Required.
  optional string url = 2;   // Required.
  repeated sync_pb.WebAppIconInfo shortcut_manifest_icons = 3;
  repeated sync_pb.WebAppIconInfo shortcut_manifest_icons_maskable = 4;
  repeated sync_pb.WebAppIconInfo shortcut_manifest_icons_monochrome = 5;
}

// List of icon sizes downloaded to disk for a shortcuts menu item.
message DownloadedShortcutsMenuIconSizes {
  repeated int32 icon_sizes = 1;
  repeated int32 icon_sizes_maskable = 2;
  repeated int32 icon_sizes_monochrome = 3;
}

// This enum should be synced with |WebAppManagement| in
// chrome/browser/web_applications/web_app_constants.h as well as
// with the Sources message defined above.
enum WebAppManagementType {
  WEB_APP_MANAGEMENT_TYPE_UNSPECIFIED = 0;
  WEB_APP_MANAGEMENT_TYPE_SYSTEM = 1;
  WEB_APP_MANAGEMENT_TYPE_POLICY = 2;
  WEB_APP_MANAGEMENT_TYPE_SUB_APP = 3;
  WEB_APP_MANAGEMENT_TYPE_WEB_APP_STORE = 4;
  WEB_APP_MANAGEMENT_TYPE_SYNC = 5;
  WEB_APP_MANAGEMENT_TYPE_DEFAULT = 6;
  WEB_APP_MANAGEMENT_TYPE_KIOSK = 7;
  reserved 8;
  WEB_APP_MANAGEMENT_TYPE_OEM = 9;
  WEB_APP_MANAGEMENT_TYPE_ONE_DRIVE_INTEGRATION = 10;
  WEB_APP_MANAGEMENT_TYPE_APS_DEFAULT = 11;
  WEB_APP_MANAGEMENT_TYPE_IWA_SHIMLESS_RMA = 12;
  WEB_APP_MANAGEMENT_TYPE_IWA_POLICY = 13;
  WEB_APP_MANAGEMENT_TYPE_IWA_USER_INSTALLED = 14;
  WEB_APP_MANAGEMENT_TYPE_USER_INSTALLED = 15;
}

// This stores data per-external-WebAppManagement::Type, as each installation
// manager can have different installation configurations for the same web app.
message ManagementToExternalConfigInfo {
  optional WebAppManagementType management = 1;
  // The collection of install_urls per web_app per WebAppManagement::Type.
  repeated string install_urls = 2;
  // Stores whether the app is a placeholder app or not.
  optional bool is_placeholder = 3;
  // A list of additional terms to use when matching this app against
  // identifiers in admin policies (for shelf pinning, default file handlers,
  // etc) per web_app per WebAppManagement::Type.
  // Note that list is not meant to be an exhaustive enumeration of all possible
  // policy_ids but rather just a supplement for tricky cases.
  repeated string additional_policy_ids = 4;
}

// The initiator of a sync generated icon fix time window.
enum GeneratedIconFixSource {
  GENERATED_ICON_FIX_SOURCE_UNKNOWN = 0;
  GENERATED_ICON_FIX_SOURCE_SYNC_INSTALL = 1;
  GENERATED_ICON_FIX_SOURCE_RETROACTIVE = 2;
  GENERATED_ICON_FIX_SOURCE_MANIFEST_UPDATE = 3;
};

// Represents the current state of attempts to fix broken icons from sync
// installed web apps.
message GeneratedIconFix {
  optional GeneratedIconFixSource source = 1;  // Required.

  optional int64 window_start_time = 2;  // Required.

  optional int64 last_attempt_time = 3;

  optional uint32 attempt_count = 4;  // Required.
}

// Stores the mapping of an icon purpose to icon sizes for downloaded icons.
// This is saved in PendingUpdateInfo if there are pending icons to be updated
// from the ManifestSilentUpdateCommand. It is written to the web app in the
// ApplyManifestPendingUpdateCommand if the user confirms the updates.
message DownloadedIconSizeInfo {
  optional sync_pb.WebAppIconInfo.Purpose purpose = 1;
  repeated int32 icon_sizes = 2;
}

// Stores the pending updates for security sensitive members. Saved when an
// update is registered in the ManifestSilentUpdateCommand, and used to show
// update UX. At least one field should be populated. If the icon is populated,
// the url, size, and purpose must all be populated. The downloaded manifest
// and downloaded trusted icons map the stored manifest and trusted icon
// purposes with all their downloaded sizes. If manifest icons and trusted
// icons exist, the downloaded manifest and downloaded trusted icons must also
// exist.
message PendingUpdateInfo {
  optional string name = 1;
  reserved 2;
  repeated sync_pb.WebAppIconInfo manifest_icons = 3;
  repeated sync_pb.WebAppIconInfo trusted_icons = 4;
  repeated DownloadedIconSizeInfo downloaded_manifest_icons = 5;
  repeated DownloadedIconSizeInfo downloaded_trusted_icons = 6;
  optional bool was_ignored = 7;
}

// This stores the time and requesting page URL of an attempted install of the
// app via the Web Install API.
message InstalledBy {
  optional int64 install_api_call_time = 1;  // Required.
  optional string requesting_url = 2;        // Required.
}

// Used to specify the behavior of a migration.
enum WebAppMigrationBehavior {
  WEB_APP_MIGRATION_BEHAVIOR_UNSPECIFIED = 0;
  // The migration should be performed via a blocking dialog that the user must
  // accept (or uninstall the app).
  WEB_APP_MIGRATION_BEHAVIOR_FORCE = 1;
  // The migration should be suggested to the user, who can choose to ignore it.
  WEB_APP_MIGRATION_BEHAVIOR_SUGGEST = 2;
}

// Represents a possible source for migration, extracted from the manifest of
// the new version of the app.
message WebAppMigrationSource {
  optional string manifest_id = 1;                // Required.
  optional WebAppMigrationBehavior behavior = 2;  // Required.
  optional string install_url = 3;
}

// Represents a migration that has been identified for this app, where this app
// is the source of the migration.
message PendingMigrationInfo {
  // The manifest_id of the app that this app should migrate to.
  optional string manifest_id = 1;                // Required.
  optional WebAppMigrationBehavior behavior = 2;  // Required.

  // The time when the user last ignored the migration prompt for this app.
  // ms since the Unix epoch. See sync/base/time.h.
  optional int64 last_ignored_time = 3;
}

// Full WebApp object data. See detailed comments in
// chrome/browser/web_applications/web_app.h. Note on database identities:
// app_id is a hash of launch_url. app_id is the client tag for sync system.
// app_id is the storage key in DataTypeStore.
message WebApp {
  // LINT.IfChange(MemberVariables)
  reserved "handle_links", "is_in_sync_install", "is_placeholder",
      "file_handler_os_integration_state",
      "run_on_os_login_os_integration_state", "url_handlers", "update_token",
      "display_mode_override", "borderless_url_patterns", "permissions_policy";
  // Synced data. It is replicated across , all devices with WEB_APPS.
  //
  // |sync_data.name| and |sync_data.theme_color| are read by a device to
  // generate a placeholder icon. Any device may write new values to synced
  // |name| and |theme_color|. A random last update wins.
  optional sync_pb.WebAppSpecifics sync_data = 1;  // Required.

  // This enum should be synced with
  // third_party/blink/public/mojom/manifest/display_mode.mojom
  enum DisplayMode {
    DISPLAY_MODE_UNSPECIFIED = 0;
    DISPLAY_MODE_BROWSER = 1;
    DISPLAY_MODE_MINIMAL_UI = 2;
    DISPLAY_MODE_STANDALONE = 3;
    DISPLAY_MODE_FULLSCREEN = 4;
    DISPLAY_MODE_WINDOW_CONTROLS_OVERLAY = 5;
    DISPLAY_MODE_TABBED = 6;
    DISPLAY_MODE_UNFRAMED = 7;
    DISPLAY_MODE_PICTURE_IN_PICTURE = 8;
  }

  // This message should be synced with
  // third_party/blink/public/mojom/manifest/manifest.mojom
  message DisplayOverrideItem {
    // The display mode of this override.
    optional DisplayMode display_mode = 1;
    // The URL patterns where this override should apply. Empty means it applies
    // in all URLs.
    //
    // Currently only parsed when `display_mode` is DISPLAY_MODE_UNFRAMED.
    repeated UrlPattern url_patterns = 2;
  }

  // Local data. May vary across devices. Not to be synced.
  //
  optional string name = 2;  // Required.
  optional uint32 theme_color = 3;
  optional string description = 4;
  optional DisplayMode display_mode = 5;
  optional string scope = 6;
  optional Sources sources = 7;  // Required.
  // This used to be `bool is_locally_installed`.
  required proto.InstallState install_state = 8;  // Required.

  // Installation state:
  // Specifies if this web app is from sync and has not completed installation.
  // If true, this entity will be re-installed on startup.
  optional bool is_from_sync_and_pending_installation = 9;

  repeated sync_pb.WebAppIconInfo manifest_icons = 10;

  // A list of icon sizes we successfully downloaded to store on disk, for icons
  // that are suitable for any purpose (ie. IconPurpose::ANY). See also:
  // |downloaded_icon_sizes_purpose_monochrome|.
  repeated int32 downloaded_icon_sizes_purpose_any = 11;

  // A list of file handlers.
  repeated WebAppFileHandler file_handlers = 12;

  // A list of additional search terms to use when searching for the app.
  repeated string additional_search_terms = 13;

  // Even though |chromeos_data| is optional, it should always exist on
  // ChromeOS.
  optional ChromeOSData chromeos_data = 14;

  // Last time the app is launched.
  // ms since the Unix epoch. See sync/base/time.h.
  optional int64 last_launch_time = 15;

  // Time the app is installed.
  // ms since the Unix epoch. See sync/base/time.h.
  optional int64 first_install_time = 16;

  // A list of protocol handlers.
  repeated WebAppProtocolHandler protocol_handlers = 17;

  // Represents whether the icons for the web app are generated by Chrome due to
  // no suitable icons being available.
  optional bool is_generated_icon = 18;

  // A list of Shortcuts Menu items specified in the Web App Manifest.
  repeated WebAppShortcutsMenuItemInfo shortcuts_menu_item_infos = 19;
  // A list of icon sizes we successfully downloaded to store on disk.
  repeated DownloadedShortcutsMenuIconSizes
      downloaded_shortcuts_menu_icons_sizes = 20;

  optional uint32 background_color = 21;

  // DEPRECATED: Use `display_overrides` instead.
  // A list of display modes specified in app manifest.
  repeated DisplayMode display_mode_override_deprecated = 22
      [deprecated = true];

  // A list of icon sizes we successfully downloaded to store on disk, for icons
  // that are designed for masking (ie. IconPurpose::MASKABLE). See also:
  // |downloaded_icon_sizes_purpose_any|.
  repeated int32 downloaded_icon_sizes_purpose_maskable = 23;

  // Run on OS Login modes for web app.
  // See chrome/browser/web_applications/web_app_constants.h
  // for web_app::RunOnOsLoginMode definition, which this enum should be in sync
  // with.
  enum RunOnOsLoginMode {
    // NOT_RUN if optional |user_run_on_os_login_mode| is absent.
    RUN_ON_OS_LOGIN_MODE_NOT_RUN = 0;
    RUN_ON_OS_LOGIN_MODE_WINDOWED = 1;
    RUN_ON_OS_LOGIN_MODE_MINIMIZED = 2;
  }
  // User preference: Run on OS Login mode for web app.
  // If present, the web app is configured to run on OS login.
  optional RunOnOsLoginMode user_run_on_os_login_mode = 24;

  optional ShareTarget share_target = 25;

  optional string launch_query_params = 26;

  reserved 27;

  optional ClientData client_data = 28;

  // This enum should be synced with |CaptureLinks| in
  // third_party/blink/public/mojom/manifest/capture_links.mojom
  enum CaptureLinks {
    // UNDEFINED if optional |capture_links| is absent.
    NONE = 1;
    NEW_CLIENT = 2;
    EXISTING_CLIENT_NAVIGATE = 3;
  }

  optional CaptureLinks capture_links = 29;

  optional string manifest_url = 30;

  // Last time the Badging API was used.
  // ms since the Unix epoch. See sync/base/time.h.
  optional int64 last_badging_time = 31;

  optional bool file_handler_permission_blocked = 32;

  // A list of icon sizes we successfully downloaded to store on disk for
  // monochrome icons. (IconPurpose::MONOCHROME). See also:
  // |downloaded_icon_sizes_purpose_any|,
  // |downloaded_icon_sizes_purpose_maskable|.
  repeated int32 downloaded_icon_sizes_purpose_monochrome = 33;

  optional bool window_controls_overlay_enabled = 34;

  // If present, the URL to use to create a new note in the app.
  optional string note_taking_new_note_url = 35;

  // A list of allowed launch protocols when launching the app with a protocol
  // url. This list is checked to see if we can bypass the permission dialog
  // when launching the web app.
  repeated string allowed_launch_protocols = 37;

  optional LaunchHandler launch_handler = 38;

  // Time the app is updated.
  // ms since the Unix epoch. See sync/base/time.h.
  optional int64 manifest_update_time = 39;

  // A list of disallowed launch protocols. The user has elected to never
  // allow these protocols be launched in the web app.
  repeated string disallowed_launch_protocols = 40;

  // A theme color to be used when the app is opened in dark mode.
  optional uint32 dark_mode_theme_color = 41;

  // Should be kept in sync with `ApiApprovalState` in web_app_constants.h.
  enum ApiApprovalState {
    REQUIRES_PROMPT = 0;
    ALLOWED = 1;
    DISALLOWED = 2;
  }

  optional ApiApprovalState file_handler_approval_state = 42;

  // For apps installed via the SubApp API, ID of the parent app that installed
  // the Sub-app.
  optional string parent_app_id = 43;

  // A background color to be used when the app is opened in dark mode.
  optional uint32 dark_mode_background_color = 44;

  // Specifies if this web app is currently being uninstalled. If true on
  // startup, it's assumed there was a crash or other error during
  // uninstallation, and the uninstallation will be tried again.
  optional bool is_uninstalling = 45;

  reserved 46;

  // Whether the app should show up in file-open intent and picking surfaces.
  optional bool handles_file_open_intents = 47;

  reserved 48, 49;

  // The source that triggered the latest install. This is a
  // `webapps::WebappInstallSource` encoded as an integer.
  optional uint32 latest_install_source = 50;

  reserved 51;

  // Map of WebAppManagement enum to placeholder info and install_urls.
  // There should be only one entry per WebAppManagement source.
  repeated ManagementToExternalConfigInfo management_to_external_config_info =
      52;

  reserved 53;

  // The amount of storage (in bytes) used by the app and its related data.
  optional uint64 app_size_in_bytes = 54;
  optional uint64 data_size_in_bytes = 55;

  // If present, the URL to use to launch the app on the lock screen.
  optional string lock_screen_start_url = 56;

  // Contains customisations to the web app tab strip. Only present when the
  // display_mode is tabbed.
  optional TabStrip tab_strip = 57;

  // Only used on Mac OS, stores whether the app should always show the
  // toolbar when in fullscreen mode.
  optional bool always_show_toolbar_in_fullscreen = 58;

  // Contains the os_integration_state currently deployed for a web_app.
  // This will be populated from the fields in this proto when ran for the first
  // time after OS hooks have been registered.
  optional os_state.WebAppOsIntegration current_os_integration_states = 59;

  // If present, signals that this app is an Isolated Web App, and contains
  // IWA-specific information like bundle location.
  optional IsolationData isolation_data = 60;

  // Contains web origins that are part of the app's extended scope.
  repeated WebAppScopeExtension scope_extensions = 61;

  // The same as scope_extensions but filtered to origins that have validated
  // associations with this web app. See
  // https://github.com/WICG/manifest-incubations/blob/gh-pages/scope_extensions-explainer.md
  // for association requirements.
  repeated WebAppScopeExtension scope_extensions_validated = 62;

  // Stores whether the current web app is set as the default app to capture
  // links.
  optional LinkCapturingUserPreference user_link_capturing_preference = 63;

  // Time the app was updated as part of a full install. For example when a
  // policy installs an app that is already in the DB installed as a
  // default/sync installed app and the DB data is overwritten. ms since the
  // Unix epoch. See sync/base/time.h.
  optional int64 latest_install_time = 64;

  // Contains bookkeeping details about attempts to fix broken icons from sync
  // installed web apps.
  optional GeneratedIconFix generated_icon_fix = 65;

  // Records the number of times the user has ignored/dismissed the offer to
  // turn on user link capturing after launching the app via the intent picker,
  // specifically from the `EnableLinkCapturingInfoBarDelegate`.
  optional int32 supported_links_offer_ignore_count = 66;
  optional int32 supported_links_offer_dismiss_count = 67;

  // Set to true if the app was installed via the DIY app install path by the
  // user (3-dot menu, Install page as App...). DIY apps aren't promotable or
  // installable, and the user can customize its name.
  optional bool is_diy_app = 68;

  // Set to true if the app was installed via the shortcut app install path by
  // the user and was migrated to being a DIY app.
  optional bool was_shortcut_app = 69;

  // The related applications field of the manifest for installed web apps.
  repeated proto.RelatedApplications related_applications = 70;

  // Only used on Mac OS, Set to true if the app was installed via the DIY app
  // install path by the user and had new icons.
  optional bool diy_app_icons_masked_on_mac = 71;

  reserved 72;

  // Populated when there is a pending update to security sensitive members. If
  // not populated, then no update is registered.
  optional PendingUpdateInfo pending_update_info = 73;

  // Contains the metadata for the trusted icons to be shown on security
  // sensitive surfaces by the web app.
  repeated sync_pb.WebAppIconInfo trusted_icons = 74;

  // A list of icon sizes for trusted icons stored on disk that are suitable for
  // any purpose (ie. IconPurpose::ANY). See also:
  // |stored_trusted_icon_sizes_maskable|.
  repeated int32 stored_trusted_icon_sizes_any = 75;

  // A list of icon sizes for trusted icons stored on disk that are designed for
  // masking (ie. IconPurpose::MASKABLE). See also:
  // |stored_trusted_icon_sizes_any|.
  repeated int32 stored_trusted_icon_sizes_maskable = 76;

  reserved 77;

  // A list of up to ten unique URLs of the most recent pages that attempt to
  // install the app via the Web Install API. The order is that the front is
  // the oldest entry and the back is the newest.
  repeated InstalledBy installed_by = 78;

  // Migration sources as specified in the manifest 'migrate_from' field. These
  // are the apps that this app claims to be a migration from.
  repeated WebAppMigrationSource unvalidated_migration_sources = 79;

  // Subset of unvalidated_migration_sources that have been validated against
  // the old origin (via .well-known).
  repeated WebAppMigrationSource validated_migration_sources = 80;

  // List of apps that this app can migrate to. This is populated by checking
  // the validated_migration_sources of other installed apps.
  optional PendingMigrationInfo pending_migration_info = 81;

  // A list of display mode overrides specified in app manifest.
  repeated DisplayOverrideItem display_overrides = 82;

  // The last time origin association was checked for validity.
  // ms since the Unix epoch. See sync/base/time.h.
  optional int64 origin_association_last_validation_check_time = 83;
  // LINT.ThenChange(//chrome/browser/web_applications/web_app.h)
}
