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. |
Marijn Kruisselbrink | da02951 | 2024-10-07 20:40:17 | [diff] [blame] | 71 | static int GetCurrentDatabaseVersion(); |
| 72 | |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 73 | private: |
Marijn Kruisselbrink | da02951 | 2024-10-07 20:40:17 | [diff] [blame] | 74 | struct ProtobufState { |
| 75 | ProtobufState(); |
| 76 | ~ProtobufState(); |
| 77 | ProtobufState(ProtobufState&&); |
| 78 | ProtobufState& operator=(ProtobufState&&); |
| 79 | |
| 80 | proto::DatabaseMetadata metadata; |
Daniel Murphy | adf55306 | 2025-03-10 16:46:35 | [diff] [blame] | 81 | base::flat_map<webapps::AppId, proto::WebApp> apps; |
Marijn Kruisselbrink | da02951 | 2024-10-07 20:40:17 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | ProtobufState ParseProtobufs( |
| 85 | const syncer::DataTypeStore::RecordList& data_records) const; |
| 86 | |
| 87 | void MigrateDatabase(ProtobufState& state); |
| 88 | void MigrateInstallSourceAddUserInstalled( |
| 89 | ProtobufState& state, |
| 90 | std::set<webapps::AppId>& changed_apps); |
Daniel Murphy | 21c9698 | 2025-04-18 19:52:26 | [diff] [blame] | 91 | // Migrates apps that were created as shortcuts (empty scope or installed via |
| 92 | // "Create shortcut") to be DIY apps with a valid scope derived from the start |
| 93 | // URL. |
| 94 | void MigrateShortcutAppsToDiyApps(ProtobufState& state, |
| 95 | std::set<webapps::AppId>& changed_apps); |
| 96 | // Ensures that the user display mode is set for the current platform in the |
| 97 | // sync proto. If it's missing, it derives it from the other platform's |
| 98 | // setting or defaults to STANDALONE. |
| 99 | void MigrateDefaultDisplayModeToPlatformDisplayMode( |
| 100 | ProtobufState& state, |
| 101 | std::set<webapps::AppId>& changed_apps); |
| 102 | // Corrects the install_state for apps that claim OS integration but lack the |
| 103 | // necessary OS integration state data. |
| 104 | void MigratePartiallyInstalledAppsToCorrectState( |
| 105 | ProtobufState& state, |
| 106 | std::set<webapps::AppId>& changed_apps); |
Marijn Kruisselbrink | da02951 | 2024-10-07 20:40:17 | [diff] [blame] | 107 | |
Alexey Baskakov | eecc87f1 | 2019-09-24 08:03:01 | [diff] [blame] | 108 | void OnDatabaseOpened(RegistryOpenedCallback callback, |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 109 | const std::optional<syncer::ModelError>& error, |
Mikel Astiz | 31a57c5e | 2024-08-05 15:45:52 | [diff] [blame] | 110 | std::unique_ptr<syncer::DataTypeStore> store); |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 111 | |
Florian Leimgruber | d573126 | 2024-06-06 08:41:20 | [diff] [blame] | 112 | void OnAllDataAndMetadataRead( |
Alexey Baskakov | 4702d663 | 2019-09-17 06:58:51 | [diff] [blame] | 113 | RegistryOpenedCallback callback, |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 114 | const std::optional<syncer::ModelError>& error, |
Mikel Astiz | 31a57c5e | 2024-08-05 15:45:52 | [diff] [blame] | 115 | std::unique_ptr<syncer::DataTypeStore::RecordList> data_records, |
Alexey Baskakov | 0b50ec6 | 2019-10-01 03:29:23 | [diff] [blame] | 116 | std::unique_ptr<syncer::MetadataBatch> metadata_batch); |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 117 | |
Alexey Baskakov | 00fb85eae | 2019-09-03 07:29:12 | [diff] [blame] | 118 | void OnDataWritten(CompletionCallback callback, |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 119 | const std::optional<syncer::ModelError>& error); |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 120 | |
Mikel Astiz | 31a57c5e | 2024-08-05 15:45:52 | [diff] [blame] | 121 | std::unique_ptr<syncer::DataTypeStore> store_; |
Arthur Sonzogni | e98d214 | 2023-06-01 15:02:25 | [diff] [blame] | 122 | const raw_ptr<AbstractWebAppDatabaseFactory, DanglingUntriaged> |
| 123 | database_factory_; |
Alexey Baskakov | 0b50ec6 | 2019-10-01 03:29:23 | [diff] [blame] | 124 | ReportErrorCallback error_callback_; |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 125 | |
| 126 | // Database is opened if store is created and all data read. |
| 127 | bool opened_ = false; |
| 128 | |
| 129 | SEQUENCE_CHECKER(sequence_checker_); |
| 130 | |
| 131 | base::WeakPtrFactory<WebAppDatabase> weak_ptr_factory_{this}; |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 132 | }; |
| 133 | |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 134 | } // namespace web_app |
| 135 | |
| 136 | #endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_DATABASE_H_ |