blob: b093cf766834154f625878c50f360c4065e9df32 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2018 The Chromium Authors
Alexey Baskakovac8c4b02018-11-07 06:10:022// 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 Sonzognife132ee2024-01-15 11:01:049#include <optional>
Alexey Baskakovac8c4b02018-11-07 06:10:0210
Avi Drissman9269d4ed2023-01-07 01:38:0611#include "base/functional/callback_forward.h"
Keishi Hattori0e45c022021-11-27 09:25:5212#include "base/memory/raw_ptr.h"
Alexey Baskakovac8c4b02018-11-07 06:10:0213#include "base/memory/weak_ptr.h"
Alexey Baskakovac8c4b02018-11-07 06:10:0214#include "base/sequence_checker.h"
Eric Willigers14c5e572019-10-29 11:39:4915#include "chrome/browser/web_applications/proto/web_app.pb.h"
Marijn Kruisselbrinkda029512024-10-07 20:40:1716#include "chrome/browser/web_applications/proto/web_app_database_metadata.pb.h"
Song Fangzhencda4af62021-09-09 05:24:0217#include "chrome/browser/web_applications/web_app_constants.h"
Alexey Baskakov27f14d42019-09-20 07:09:4818#include "chrome/browser/web_applications/web_app_registrar.h"
Mikel Astiz9048b122024-08-05 12:55:4119#include "components/sync/model/data_type_store.h"
Eric Willigers18544282019-10-09 05:59:5820#include "components/sync/protocol/web_app_specifics.pb.h"
Glenn Hartmann5f992ed2023-09-25 18:05:3621#include "components/webapps/common/web_app_id.h"
Alexey Baskakovac8c4b02018-11-07 06:10:0222
23namespace syncer {
24class ModelError;
Alexey Baskakov0b50ec62019-10-01 03:29:2325class MetadataBatch;
Alexey Baskakovf8fb8c62019-10-10 04:55:4926class MetadataChangeList;
Alexey Baskakovac8c4b02018-11-07 06:10:0227} // namespace syncer
28
29namespace web_app {
30
31class AbstractWebAppDatabaseFactory;
32class WebApp;
Daniel Murphyadf553062025-03-10 16:46:3533namespace proto {
34class WebApp;
35} // namespace proto
Alexey Baskakov6856e5e42019-09-26 04:31:2636struct RegistryUpdateData;
Alexey Baskakovac8c4b02018-11-07 06:10:0237
38// Exclusively used from the UI thread.
Alexey Baskakov4702d6632019-09-17 06:58:5139class WebAppDatabase {
Alexey Baskakovac8c4b02018-11-07 06:10:0240 public:
Alexey Baskakov0b50ec62019-10-01 03:29:2341 using ReportErrorCallback =
42 base::RepeatingCallback<void(const syncer::ModelError&)>;
43
Marijn Kruisselbrinkda029512024-10-07 20:40:1744 static constexpr std::string_view kDatabaseMetadataKey = "DATABASE_METADATA";
45
Alexey Baskakov0b50ec62019-10-01 03:29:2346 WebAppDatabase(AbstractWebAppDatabaseFactory* database_factory,
47 ReportErrorCallback error_callback);
Haben Fotoe3d073b2020-10-06 01:22:5848 WebAppDatabase(const WebAppDatabase&) = delete;
49 WebAppDatabase& operator=(const WebAppDatabase&) = delete;
Alexey Baskakov4702d6632019-09-17 06:58:5150 ~WebAppDatabase();
Alexey Baskakovac8c4b02018-11-07 06:10:0251
Alexey Baskakov0b50ec62019-10-01 03:29:2352 using RegistryOpenedCallback = base::OnceCallback<void(
53 Registry registry,
54 std::unique_ptr<syncer::MetadataBatch> metadata_batch)>;
Alexey Baskakov27f14d42019-09-20 07:09:4855 // Open existing or create new DB. Read all data and return it via callback.
Alexey Baskakov4702d6632019-09-17 06:58:5156 void OpenDatabase(RegistryOpenedCallback callback);
Alexey Baskakov6856e5e42019-09-26 04:31:2657
58 using CompletionCallback = base::OnceCallback<void(bool success)>;
Alexey Baskakovd6e93822019-10-11 02:39:4459 void Write(const RegistryUpdateData& update_data,
60 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
61 CompletionCallback callback);
Alexey Baskakovac8c4b02018-11-07 06:10:0262
Alexey Baskakov583f6edf2020-05-21 03:56:1163 bool is_opened() const { return opened_; }
64
Marijn Kruisselbrinkda029512024-10-07 20:40:1765 // 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 Murphy21c96982025-04-18 19:52:2669 // - Version 2 migrates shortcut apps to DIY apps, ensures platform user
70 // display mode is set, and fixes partial install state inconsistencies.
Marijn Kruisselbrinkda029512024-10-07 20:40:1771 static int GetCurrentDatabaseVersion();
72
Alexey Baskakovac8c4b02018-11-07 06:10:0273 private:
Marijn Kruisselbrinkda029512024-10-07 20:40:1774 struct ProtobufState {
75 ProtobufState();
76 ~ProtobufState();
77 ProtobufState(ProtobufState&&);
78 ProtobufState& operator=(ProtobufState&&);
79
80 proto::DatabaseMetadata metadata;
Daniel Murphyadf553062025-03-10 16:46:3581 base::flat_map<webapps::AppId, proto::WebApp> apps;
Marijn Kruisselbrinkda029512024-10-07 20:40:1782 };
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 Murphy21c96982025-04-18 19:52:2691 // 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 Kruisselbrinkda029512024-10-07 20:40:17107
Alexey Baskakoveecc87f12019-09-24 08:03:01108 void OnDatabaseOpened(RegistryOpenedCallback callback,
Arthur Sonzognife132ee2024-01-15 11:01:04109 const std::optional<syncer::ModelError>& error,
Mikel Astiz31a57c5e2024-08-05 15:45:52110 std::unique_ptr<syncer::DataTypeStore> store);
Alexey Baskakovac8c4b02018-11-07 06:10:02111
Florian Leimgruberd5731262024-06-06 08:41:20112 void OnAllDataAndMetadataRead(
Alexey Baskakov4702d6632019-09-17 06:58:51113 RegistryOpenedCallback callback,
Arthur Sonzognife132ee2024-01-15 11:01:04114 const std::optional<syncer::ModelError>& error,
Mikel Astiz31a57c5e2024-08-05 15:45:52115 std::unique_ptr<syncer::DataTypeStore::RecordList> data_records,
Alexey Baskakov0b50ec62019-10-01 03:29:23116 std::unique_ptr<syncer::MetadataBatch> metadata_batch);
Alexey Baskakovac8c4b02018-11-07 06:10:02117
Alexey Baskakov00fb85eae2019-09-03 07:29:12118 void OnDataWritten(CompletionCallback callback,
Arthur Sonzognife132ee2024-01-15 11:01:04119 const std::optional<syncer::ModelError>& error);
Alexey Baskakovac8c4b02018-11-07 06:10:02120
Mikel Astiz31a57c5e2024-08-05 15:45:52121 std::unique_ptr<syncer::DataTypeStore> store_;
Arthur Sonzognie98d2142023-06-01 15:02:25122 const raw_ptr<AbstractWebAppDatabaseFactory, DanglingUntriaged>
123 database_factory_;
Alexey Baskakov0b50ec62019-10-01 03:29:23124 ReportErrorCallback error_callback_;
Alexey Baskakovac8c4b02018-11-07 06:10:02125
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 Baskakovac8c4b02018-11-07 06:10:02132};
133
Alexey Baskakovac8c4b02018-11-07 06:10:02134} // namespace web_app
135
136#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_DATABASE_H_