blob: 81dacf591603dd9c9fd0ffb4a9ffc7badf8ffc5d [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
63 // Exposed for testing.
Daniel Murphyadf553062025-03-10 16:46:3564 static std::unique_ptr<proto::WebApp> CreateWebAppProto(
65 const WebApp& web_app);
Alexey Baskakovac8c4b02018-11-07 06:10:0266 // Exposed for testing.
Glenn Hartmann5f992ed2023-09-25 18:05:3667 static std::unique_ptr<WebApp> ParseWebApp(const webapps::AppId& app_id,
Alexey Baskakov27f14d42019-09-20 07:09:4868 const std::string& value);
Alan Cutter87b14852022-04-08 06:34:1569 // Exposed for testing.
Daniel Murphyadf553062025-03-10 16:46:3570 static std::unique_ptr<WebApp> CreateWebApp(const proto::WebApp& local_data);
Alexey Baskakovac8c4b02018-11-07 06:10:0271
Alexey Baskakov583f6edf2020-05-21 03:56:1172 bool is_opened() const { return opened_; }
73
Marijn Kruisselbrinkda029512024-10-07 20:40:1774 // Returns the version that the database will be migrated to when opened.
75 // - No version/version 0 is the original version.
76 // - Version 1 introduces the UserInstalled install source, migration between
77 // 0 and 1 add or remove this source.
78 static int GetCurrentDatabaseVersion();
79
Alexey Baskakovac8c4b02018-11-07 06:10:0280 private:
Marijn Kruisselbrinkda029512024-10-07 20:40:1781 struct ProtobufState {
82 ProtobufState();
83 ~ProtobufState();
84 ProtobufState(ProtobufState&&);
85 ProtobufState& operator=(ProtobufState&&);
86
87 proto::DatabaseMetadata metadata;
Daniel Murphyadf553062025-03-10 16:46:3588 base::flat_map<webapps::AppId, proto::WebApp> apps;
Marijn Kruisselbrinkda029512024-10-07 20:40:1789 };
90
91 ProtobufState ParseProtobufs(
92 const syncer::DataTypeStore::RecordList& data_records) const;
93
94 void MigrateDatabase(ProtobufState& state);
95 void MigrateInstallSourceAddUserInstalled(
96 ProtobufState& state,
97 std::set<webapps::AppId>& changed_apps);
98 void MigrateInstallSourceRemoveUserInstalled(
99 ProtobufState& state,
100 std::set<webapps::AppId>& changed_apps);
101
Alexey Baskakoveecc87f12019-09-24 08:03:01102 void OnDatabaseOpened(RegistryOpenedCallback callback,
Arthur Sonzognife132ee2024-01-15 11:01:04103 const std::optional<syncer::ModelError>& error,
Mikel Astiz31a57c5e2024-08-05 15:45:52104 std::unique_ptr<syncer::DataTypeStore> store);
Alexey Baskakovac8c4b02018-11-07 06:10:02105
Florian Leimgruberd5731262024-06-06 08:41:20106 void OnAllDataAndMetadataRead(
Alexey Baskakov4702d6632019-09-17 06:58:51107 RegistryOpenedCallback callback,
Arthur Sonzognife132ee2024-01-15 11:01:04108 const std::optional<syncer::ModelError>& error,
Mikel Astiz31a57c5e2024-08-05 15:45:52109 std::unique_ptr<syncer::DataTypeStore::RecordList> data_records,
Alexey Baskakov0b50ec62019-10-01 03:29:23110 std::unique_ptr<syncer::MetadataBatch> metadata_batch);
Alexey Baskakovac8c4b02018-11-07 06:10:02111
Alexey Baskakov00fb85eae2019-09-03 07:29:12112 void OnDataWritten(CompletionCallback callback,
Arthur Sonzognife132ee2024-01-15 11:01:04113 const std::optional<syncer::ModelError>& error);
Alexey Baskakovac8c4b02018-11-07 06:10:02114
Mikel Astiz31a57c5e2024-08-05 15:45:52115 std::unique_ptr<syncer::DataTypeStore> store_;
Arthur Sonzognie98d2142023-06-01 15:02:25116 const raw_ptr<AbstractWebAppDatabaseFactory, DanglingUntriaged>
117 database_factory_;
Alexey Baskakov0b50ec62019-10-01 03:29:23118 ReportErrorCallback error_callback_;
Alexey Baskakovac8c4b02018-11-07 06:10:02119
120 // Database is opened if store is created and all data read.
121 bool opened_ = false;
122
123 SEQUENCE_CHECKER(sequence_checker_);
124
125 base::WeakPtrFactory<WebAppDatabase> weak_ptr_factory_{this};
Alexey Baskakovac8c4b02018-11-07 06:10:02126};
127
Daniel Murphyadf553062025-03-10 16:46:35128DisplayMode ToMojomDisplayMode(proto::WebApp::DisplayMode display_mode);
Eric Willigers18544282019-10-09 05:59:58129
Daniel Murphyadf553062025-03-10 16:46:35130proto::WebApp::DisplayMode ToWebAppProtoDisplayMode(DisplayMode display_mode);
Eric Willigers18544282019-10-09 05:59:58131
Alexey Baskakovac8c4b02018-11-07 06:10:02132} // namespace web_app
133
134#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_DATABASE_H_