blob: 44cd43ef48d38d7834b07ce7a992fdd616aafed5 [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;
33class WebAppProto;
Alexey Baskakov6856e5e42019-09-26 04:31:2634struct RegistryUpdateData;
Alexey Baskakovac8c4b02018-11-07 06:10:0235
36// Exclusively used from the UI thread.
Alexey Baskakov4702d6632019-09-17 06:58:5137class WebAppDatabase {
Alexey Baskakovac8c4b02018-11-07 06:10:0238 public:
Alexey Baskakov0b50ec62019-10-01 03:29:2339 using ReportErrorCallback =
40 base::RepeatingCallback<void(const syncer::ModelError&)>;
41
Marijn Kruisselbrinkda029512024-10-07 20:40:1742 static constexpr std::string_view kDatabaseMetadataKey = "DATABASE_METADATA";
43
Alexey Baskakov0b50ec62019-10-01 03:29:2344 WebAppDatabase(AbstractWebAppDatabaseFactory* database_factory,
45 ReportErrorCallback error_callback);
Haben Fotoe3d073b2020-10-06 01:22:5846 WebAppDatabase(const WebAppDatabase&) = delete;
47 WebAppDatabase& operator=(const WebAppDatabase&) = delete;
Alexey Baskakov4702d6632019-09-17 06:58:5148 ~WebAppDatabase();
Alexey Baskakovac8c4b02018-11-07 06:10:0249
Alexey Baskakov0b50ec62019-10-01 03:29:2350 using RegistryOpenedCallback = base::OnceCallback<void(
51 Registry registry,
52 std::unique_ptr<syncer::MetadataBatch> metadata_batch)>;
Alexey Baskakov27f14d42019-09-20 07:09:4853 // Open existing or create new DB. Read all data and return it via callback.
Alexey Baskakov4702d6632019-09-17 06:58:5154 void OpenDatabase(RegistryOpenedCallback callback);
Alexey Baskakov6856e5e42019-09-26 04:31:2655
56 using CompletionCallback = base::OnceCallback<void(bool success)>;
Alexey Baskakovd6e93822019-10-11 02:39:4457 void Write(const RegistryUpdateData& update_data,
58 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
59 CompletionCallback callback);
Alexey Baskakovac8c4b02018-11-07 06:10:0260
61 // Exposed for testing.
62 static std::unique_ptr<WebAppProto> CreateWebAppProto(const WebApp& web_app);
63 // Exposed for testing.
Glenn Hartmann5f992ed2023-09-25 18:05:3664 static std::unique_ptr<WebApp> ParseWebApp(const webapps::AppId& app_id,
Alexey Baskakov27f14d42019-09-20 07:09:4865 const std::string& value);
Alan Cutter87b14852022-04-08 06:34:1566 // Exposed for testing.
67 static std::unique_ptr<WebApp> CreateWebApp(const WebAppProto& local_data);
Alexey Baskakovac8c4b02018-11-07 06:10:0268
Alexey Baskakov583f6edf2020-05-21 03:56:1169 bool is_opened() const { return opened_; }
70
Marijn Kruisselbrinkda029512024-10-07 20:40:1771 // Returns the version that the database will be migrated to when opened.
72 // - No version/version 0 is the original version.
73 // - Version 1 introduces the UserInstalled install source, migration between
74 // 0 and 1 add or remove this source.
75 static int GetCurrentDatabaseVersion();
76
Alexey Baskakovac8c4b02018-11-07 06:10:0277 private:
Marijn Kruisselbrinkda029512024-10-07 20:40:1778 struct ProtobufState {
79 ProtobufState();
80 ~ProtobufState();
81 ProtobufState(ProtobufState&&);
82 ProtobufState& operator=(ProtobufState&&);
83
84 proto::DatabaseMetadata metadata;
85 base::flat_map<webapps::AppId, WebAppProto> apps;
86 };
87
88 ProtobufState ParseProtobufs(
89 const syncer::DataTypeStore::RecordList& data_records) const;
90
91 void MigrateDatabase(ProtobufState& state);
92 void MigrateInstallSourceAddUserInstalled(
93 ProtobufState& state,
94 std::set<webapps::AppId>& changed_apps);
95 void MigrateInstallSourceRemoveUserInstalled(
96 ProtobufState& state,
97 std::set<webapps::AppId>& changed_apps);
98
Alexey Baskakoveecc87f12019-09-24 08:03:0199 void OnDatabaseOpened(RegistryOpenedCallback callback,
Arthur Sonzognife132ee2024-01-15 11:01:04100 const std::optional<syncer::ModelError>& error,
Mikel Astiz31a57c5e2024-08-05 15:45:52101 std::unique_ptr<syncer::DataTypeStore> store);
Alexey Baskakovac8c4b02018-11-07 06:10:02102
Florian Leimgruberd5731262024-06-06 08:41:20103 void OnAllDataAndMetadataRead(
Alexey Baskakov4702d6632019-09-17 06:58:51104 RegistryOpenedCallback callback,
Arthur Sonzognife132ee2024-01-15 11:01:04105 const std::optional<syncer::ModelError>& error,
Mikel Astiz31a57c5e2024-08-05 15:45:52106 std::unique_ptr<syncer::DataTypeStore::RecordList> data_records,
Alexey Baskakov0b50ec62019-10-01 03:29:23107 std::unique_ptr<syncer::MetadataBatch> metadata_batch);
Alexey Baskakovac8c4b02018-11-07 06:10:02108
Alexey Baskakov00fb85eae2019-09-03 07:29:12109 void OnDataWritten(CompletionCallback callback,
Arthur Sonzognife132ee2024-01-15 11:01:04110 const std::optional<syncer::ModelError>& error);
Alexey Baskakovac8c4b02018-11-07 06:10:02111
Mikel Astiz31a57c5e2024-08-05 15:45:52112 std::unique_ptr<syncer::DataTypeStore> store_;
Arthur Sonzognie98d2142023-06-01 15:02:25113 const raw_ptr<AbstractWebAppDatabaseFactory, DanglingUntriaged>
114 database_factory_;
Alexey Baskakov0b50ec62019-10-01 03:29:23115 ReportErrorCallback error_callback_;
Alexey Baskakovac8c4b02018-11-07 06:10:02116
117 // Database is opened if store is created and all data read.
118 bool opened_ = false;
119
120 SEQUENCE_CHECKER(sequence_checker_);
121
122 base::WeakPtrFactory<WebAppDatabase> weak_ptr_factory_{this};
Alexey Baskakovac8c4b02018-11-07 06:10:02123};
124
Eric Willigersd32986ad2019-11-04 00:40:04125DisplayMode ToMojomDisplayMode(WebAppProto::DisplayMode display_mode);
Eric Willigers18544282019-10-09 05:59:58126
Eric Willigersd32986ad2019-11-04 00:40:04127WebAppProto::DisplayMode ToWebAppProtoDisplayMode(DisplayMode display_mode);
Eric Willigers18544282019-10-09 05:59:58128
Alexey Baskakovac8c4b02018-11-07 06:10:02129} // namespace web_app
130
131#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_DATABASE_H_