Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_DATABASE_H_ |
| 6 | #define CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_DATABASE_H_ |
| 7 | |
| 8 | #include <memory> |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 9 | #include <optional> |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 10 | |
Avi Drissman | 9269d4ed | 2023-01-07 01:38:06 | [diff] [blame] | 11 | #include "base/functional/callback_forward.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 12 | #include "base/memory/raw_ptr.h" |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 14 | #include "base/sequence_checker.h" |
Eric Willigers | 14c5e57 | 2019-10-29 11:39:49 | [diff] [blame] | 15 | #include "chrome/browser/web_applications/proto/web_app.pb.h" |
Marijn Kruisselbrink | da02951 | 2024-10-07 20:40:17 | [diff] [blame] | 16 | #include "chrome/browser/web_applications/proto/web_app_database_metadata.pb.h" |
Song Fangzhen | cda4af6 | 2021-09-09 05:24:02 | [diff] [blame] | 17 | #include "chrome/browser/web_applications/web_app_constants.h" |
Alexey Baskakov | 27f14d4 | 2019-09-20 07:09:48 | [diff] [blame] | 18 | #include "chrome/browser/web_applications/web_app_registrar.h" |
Mikel Astiz | 9048b12 | 2024-08-05 12:55:41 | [diff] [blame] | 19 | #include "components/sync/model/data_type_store.h" |
Eric Willigers | 1854428 | 2019-10-09 05:59:58 | [diff] [blame] | 20 | #include "components/sync/protocol/web_app_specifics.pb.h" |
Glenn Hartmann | 5f992ed | 2023-09-25 18:05:36 | [diff] [blame] | 21 | #include "components/webapps/common/web_app_id.h" |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 22 | |
| 23 | namespace syncer { |
| 24 | class ModelError; |
Alexey Baskakov | 0b50ec6 | 2019-10-01 03:29:23 | [diff] [blame] | 25 | class MetadataBatch; |
Alexey Baskakov | f8fb8c6 | 2019-10-10 04:55:49 | [diff] [blame] | 26 | class MetadataChangeList; |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 27 | } // namespace syncer |
| 28 | |
| 29 | namespace web_app { |
| 30 | |
| 31 | class AbstractWebAppDatabaseFactory; |
| 32 | class WebApp; |
Daniel Murphy | adf55306 | 2025-03-10 16:46:35 | [diff] [blame] | 33 | namespace proto { |
| 34 | class WebApp; |
| 35 | } // namespace proto |
Alexey Baskakov | 6856e5e4 | 2019-09-26 04:31:26 | [diff] [blame] | 36 | struct RegistryUpdateData; |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 37 | |
| 38 | // Exclusively used from the UI thread. |
Alexey Baskakov | 4702d663 | 2019-09-17 06:58:51 | [diff] [blame] | 39 | class WebAppDatabase { |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 40 | public: |
Alexey Baskakov | 0b50ec6 | 2019-10-01 03:29:23 | [diff] [blame] | 41 | using ReportErrorCallback = |
| 42 | base::RepeatingCallback<void(const syncer::ModelError&)>; |
| 43 | |
Marijn Kruisselbrink | da02951 | 2024-10-07 20:40:17 | [diff] [blame] | 44 | static constexpr std::string_view kDatabaseMetadataKey = "DATABASE_METADATA"; |
| 45 | |
Alexey Baskakov | 0b50ec6 | 2019-10-01 03:29:23 | [diff] [blame] | 46 | WebAppDatabase(AbstractWebAppDatabaseFactory* database_factory, |
| 47 | ReportErrorCallback error_callback); |
Haben Foto | e3d073b | 2020-10-06 01:22:58 | [diff] [blame] | 48 | WebAppDatabase(const WebAppDatabase&) = delete; |
| 49 | WebAppDatabase& operator=(const WebAppDatabase&) = delete; |
Alexey Baskakov | 4702d663 | 2019-09-17 06:58:51 | [diff] [blame] | 50 | ~WebAppDatabase(); |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 51 | |
Alexey Baskakov | 0b50ec6 | 2019-10-01 03:29:23 | [diff] [blame] | 52 | using RegistryOpenedCallback = base::OnceCallback<void( |
| 53 | Registry registry, |
| 54 | std::unique_ptr<syncer::MetadataBatch> metadata_batch)>; |
Alexey Baskakov | 27f14d4 | 2019-09-20 07:09:48 | [diff] [blame] | 55 | // Open existing or create new DB. Read all data and return it via callback. |
Alexey Baskakov | 4702d663 | 2019-09-17 06:58:51 | [diff] [blame] | 56 | void OpenDatabase(RegistryOpenedCallback callback); |
Alexey Baskakov | 6856e5e4 | 2019-09-26 04:31:26 | [diff] [blame] | 57 | |
| 58 | using CompletionCallback = base::OnceCallback<void(bool success)>; |
Alexey Baskakov | d6e9382 | 2019-10-11 02:39:44 | [diff] [blame] | 59 | void Write(const RegistryUpdateData& update_data, |
| 60 | std::unique_ptr<syncer::MetadataChangeList> metadata_change_list, |
| 61 | CompletionCallback callback); |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 62 | |
Alexey Baskakov | 583f6edf | 2020-05-21 03:56:11 | [diff] [blame] | 63 | bool is_opened() const { return opened_; } |
| 64 | |
Marijn Kruisselbrink | da02951 | 2024-10-07 20:40:17 | [diff] [blame] | 65 | // Returns the version that the database will be migrated to when opened. |
| 66 | // - No version/version 0 is the original version. |
| 67 | // - Version 1 introduces the UserInstalled install source, migration between |
| 68 | // 0 and 1 add or remove this source. |
Daniel Murphy | 21c9698 | 2025-04-18 19:52:26 | [diff] [blame] | 69 | // - Version 2 migrates shortcut apps to DIY apps, ensures platform user |
| 70 | // display mode is set, and fixes partial install state inconsistencies. |
Daniel Murphy | 202da28 | 2025-05-15 23:08:35 | [diff] [blame] | 71 | // - Version 3 migrates deprecated launch handler fields to client_mode, |
| 72 | // removes query/ref from scope, and ensures relative_manifest_id exists |
| 73 | // without fragments. |
Marijn Kruisselbrink | da02951 | 2024-10-07 20:40:17 | [diff] [blame] | 74 | static int GetCurrentDatabaseVersion(); |
| 75 | |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 76 | private: |
Marijn Kruisselbrink | da02951 | 2024-10-07 20:40:17 | [diff] [blame] | 77 | struct ProtobufState { |
| 78 | ProtobufState(); |
| 79 | ~ProtobufState(); |
| 80 | ProtobufState(ProtobufState&&); |
| 81 | ProtobufState& operator=(ProtobufState&&); |
| 82 | |
| 83 | proto::DatabaseMetadata metadata; |
Daniel Murphy | adf55306 | 2025-03-10 16:46:35 | [diff] [blame] | 84 | base::flat_map<webapps::AppId, proto::WebApp> apps; |
Marijn Kruisselbrink | da02951 | 2024-10-07 20:40:17 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | ProtobufState ParseProtobufs( |
| 88 | const syncer::DataTypeStore::RecordList& data_records) const; |
| 89 | |
| 90 | void MigrateDatabase(ProtobufState& state); |
| 91 | void MigrateInstallSourceAddUserInstalled( |
| 92 | ProtobufState& state, |
| 93 | std::set<webapps::AppId>& changed_apps); |
Daniel Murphy | 21c9698 | 2025-04-18 19:52:26 | [diff] [blame] | 94 | // Migrates apps that were created as shortcuts (empty scope or installed via |
| 95 | // "Create shortcut") to be DIY apps with a valid scope derived from the start |
| 96 | // URL. |
| 97 | void MigrateShortcutAppsToDiyApps(ProtobufState& state, |
| 98 | std::set<webapps::AppId>& changed_apps); |
| 99 | // Ensures that the user display mode is set for the current platform in the |
| 100 | // sync proto. If it's missing, it derives it from the other platform's |
| 101 | // setting or defaults to STANDALONE. |
| 102 | void MigrateDefaultDisplayModeToPlatformDisplayMode( |
| 103 | ProtobufState& state, |
| 104 | std::set<webapps::AppId>& changed_apps); |
| 105 | // Corrects the install_state for apps that claim OS integration but lack the |
| 106 | // necessary OS integration state data. |
| 107 | void MigratePartiallyInstalledAppsToCorrectState( |
| 108 | ProtobufState& state, |
| 109 | std::set<webapps::AppId>& changed_apps); |
Daniel Murphy | 202da28 | 2025-05-15 23:08:35 | [diff] [blame] | 110 | // Migrates deprecated launch handler fields (`route_to`, |
| 111 | // `navigate_existing_client`) to the `client_mode` field. Also handles the |
| 112 | // `client_mode_valid_and_specified` field correctly. |
| 113 | void MigrateDeprecatedLaunchHandlerToClientMode( |
| 114 | ProtobufState& state, |
| 115 | std::set<webapps::AppId>& changed_apps); |
| 116 | // Migrates the `scope` field by removing any query or ref components. |
| 117 | void MigrateScopeToRemoveRefAndQuery(ProtobufState& state, |
| 118 | std::set<webapps::AppId>& changed_apps); |
| 119 | // Ensures the `relative_manifest_id` field exists and does not contain URL |
| 120 | // fragments. Populates it from the start_url if missing. Records a histogram |
| 121 | // if an existing fragment needed removal. |
| 122 | void MigrateToRelativeManifestIdNoFragment( |
| 123 | ProtobufState& state, |
| 124 | std::set<webapps::AppId>& changed_apps); |
Marijn Kruisselbrink | da02951 | 2024-10-07 20:40:17 | [diff] [blame] | 125 | |
Alexey Baskakov | eecc87f1 | 2019-09-24 08:03:01 | [diff] [blame] | 126 | void OnDatabaseOpened(RegistryOpenedCallback callback, |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 127 | const std::optional<syncer::ModelError>& error, |
Mikel Astiz | 31a57c5e | 2024-08-05 15:45:52 | [diff] [blame] | 128 | std::unique_ptr<syncer::DataTypeStore> store); |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 129 | |
Florian Leimgruber | d573126 | 2024-06-06 08:41:20 | [diff] [blame] | 130 | void OnAllDataAndMetadataRead( |
Alexey Baskakov | 4702d663 | 2019-09-17 06:58:51 | [diff] [blame] | 131 | RegistryOpenedCallback callback, |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 132 | const std::optional<syncer::ModelError>& error, |
Mikel Astiz | 31a57c5e | 2024-08-05 15:45:52 | [diff] [blame] | 133 | std::unique_ptr<syncer::DataTypeStore::RecordList> data_records, |
Alexey Baskakov | 0b50ec6 | 2019-10-01 03:29:23 | [diff] [blame] | 134 | std::unique_ptr<syncer::MetadataBatch> metadata_batch); |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 135 | |
Alexey Baskakov | 00fb85eae | 2019-09-03 07:29:12 | [diff] [blame] | 136 | void OnDataWritten(CompletionCallback callback, |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 137 | const std::optional<syncer::ModelError>& error); |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 138 | |
Mikel Astiz | 31a57c5e | 2024-08-05 15:45:52 | [diff] [blame] | 139 | std::unique_ptr<syncer::DataTypeStore> store_; |
Arthur Sonzogni | e98d214 | 2023-06-01 15:02:25 | [diff] [blame] | 140 | const raw_ptr<AbstractWebAppDatabaseFactory, DanglingUntriaged> |
| 141 | database_factory_; |
Alexey Baskakov | 0b50ec6 | 2019-10-01 03:29:23 | [diff] [blame] | 142 | ReportErrorCallback error_callback_; |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 143 | |
| 144 | // Database is opened if store is created and all data read. |
| 145 | bool opened_ = false; |
| 146 | |
| 147 | SEQUENCE_CHECKER(sequence_checker_); |
| 148 | |
| 149 | base::WeakPtrFactory<WebAppDatabase> weak_ptr_factory_{this}; |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 150 | }; |
| 151 | |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 152 | } // namespace web_app |
| 153 | |
| 154 | #endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_DATABASE_H_ |