blob: 422e0a5381ee2ded6c85c566f8ac7d6b9e6da486 [file] [log] [blame]
Alexey Baskakovac8c4b02018-11-07 06:10:021// Copyright 2018 The Chromium Authors. All rights reserved.
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>
9
10#include "base/callback_forward.h"
Keishi Hattori0e45c022021-11-27 09:25:5211#include "base/memory/raw_ptr.h"
Alexey Baskakovac8c4b02018-11-07 06:10:0212#include "base/memory/weak_ptr.h"
Alexey Baskakovac8c4b02018-11-07 06:10:0213#include "base/sequence_checker.h"
Eric Willigers14c5e572019-10-29 11:39:4914#include "chrome/browser/web_applications/proto/web_app.pb.h"
Song Fangzhencda4af62021-09-09 05:24:0215#include "chrome/browser/web_applications/web_app_constants.h"
16#include "chrome/browser/web_applications/web_app_id.h"
Alexey Baskakov27f14d42019-09-20 07:09:4817#include "chrome/browser/web_applications/web_app_registrar.h"
Alexey Baskakovac8c4b02018-11-07 06:10:0218#include "components/sync/model/model_type_store.h"
Eric Willigers18544282019-10-09 05:59:5819#include "components/sync/protocol/web_app_specifics.pb.h"
Anton Bikineev46bbb972021-05-15 17:53:5320#include "third_party/abseil-cpp/absl/types/optional.h"
Alexey Baskakovac8c4b02018-11-07 06:10:0221
22namespace syncer {
23class ModelError;
Alexey Baskakov0b50ec62019-10-01 03:29:2324class MetadataBatch;
Alexey Baskakovf8fb8c62019-10-10 04:55:4925class MetadataChangeList;
Alexey Baskakovac8c4b02018-11-07 06:10:0226} // namespace syncer
27
28namespace web_app {
29
30class AbstractWebAppDatabaseFactory;
31class WebApp;
32class WebAppProto;
Alexey Baskakov6856e5e42019-09-26 04:31:2633struct RegistryUpdateData;
Alexey Baskakovac8c4b02018-11-07 06:10:0234
35// Exclusively used from the UI thread.
Alexey Baskakov4702d6632019-09-17 06:58:5136class WebAppDatabase {
Alexey Baskakovac8c4b02018-11-07 06:10:0237 public:
Alexey Baskakov0b50ec62019-10-01 03:29:2338 using ReportErrorCallback =
39 base::RepeatingCallback<void(const syncer::ModelError&)>;
40
41 WebAppDatabase(AbstractWebAppDatabaseFactory* database_factory,
42 ReportErrorCallback error_callback);
Haben Fotoe3d073b2020-10-06 01:22:5843 WebAppDatabase(const WebAppDatabase&) = delete;
44 WebAppDatabase& operator=(const WebAppDatabase&) = delete;
Alexey Baskakov4702d6632019-09-17 06:58:5145 ~WebAppDatabase();
Alexey Baskakovac8c4b02018-11-07 06:10:0246
Alexey Baskakov0b50ec62019-10-01 03:29:2347 using RegistryOpenedCallback = base::OnceCallback<void(
48 Registry registry,
49 std::unique_ptr<syncer::MetadataBatch> metadata_batch)>;
Alexey Baskakov27f14d42019-09-20 07:09:4850 // Open existing or create new DB. Read all data and return it via callback.
Alexey Baskakov4702d6632019-09-17 06:58:5151 void OpenDatabase(RegistryOpenedCallback callback);
Alexey Baskakov6856e5e42019-09-26 04:31:2652
53 using CompletionCallback = base::OnceCallback<void(bool success)>;
Alexey Baskakovd6e93822019-10-11 02:39:4454 void Write(const RegistryUpdateData& update_data,
55 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
56 CompletionCallback callback);
Alexey Baskakovac8c4b02018-11-07 06:10:0257
58 // Exposed for testing.
59 static std::unique_ptr<WebAppProto> CreateWebAppProto(const WebApp& web_app);
60 // Exposed for testing.
Alexey Baskakov27f14d42019-09-20 07:09:4861 static std::unique_ptr<WebApp> ParseWebApp(const AppId& app_id,
62 const std::string& value);
Alan Cutter87b14852022-04-08 06:34:1563 // Exposed for testing.
64 static std::unique_ptr<WebApp> CreateWebApp(const WebAppProto& local_data);
Alexey Baskakovac8c4b02018-11-07 06:10:0265
Alexey Baskakov583f6edf2020-05-21 03:56:1166 bool is_opened() const { return opened_; }
67
Alexey Baskakovac8c4b02018-11-07 06:10:0268 private:
Alexey Baskakoveecc87f12019-09-24 08:03:0169 void OnDatabaseOpened(RegistryOpenedCallback callback,
Anton Bikineev46bbb972021-05-15 17:53:5370 const absl::optional<syncer::ModelError>& error,
Alexey Baskakoveecc87f12019-09-24 08:03:0171 std::unique_ptr<syncer::ModelTypeStore> store);
Alexey Baskakovac8c4b02018-11-07 06:10:0272
73 void OnAllDataRead(
Alexey Baskakov4702d6632019-09-17 06:58:5174 RegistryOpenedCallback callback,
Anton Bikineev46bbb972021-05-15 17:53:5375 const absl::optional<syncer::ModelError>& error,
Alexey Baskakovac8c4b02018-11-07 06:10:0276 std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records);
Alexey Baskakov0b50ec62019-10-01 03:29:2377 void OnAllMetadataRead(
78 std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records,
79 RegistryOpenedCallback callback,
Anton Bikineev46bbb972021-05-15 17:53:5380 const absl::optional<syncer::ModelError>& error,
Alexey Baskakov0b50ec62019-10-01 03:29:2381 std::unique_ptr<syncer::MetadataBatch> metadata_batch);
Alexey Baskakovac8c4b02018-11-07 06:10:0282
Alexey Baskakov00fb85eae2019-09-03 07:29:1283 void OnDataWritten(CompletionCallback callback,
Anton Bikineev46bbb972021-05-15 17:53:5384 const absl::optional<syncer::ModelError>& error);
Alexey Baskakovac8c4b02018-11-07 06:10:0285
Alexey Baskakovac8c4b02018-11-07 06:10:0286 std::unique_ptr<syncer::ModelTypeStore> store_;
Keishi Hattori0e45c022021-11-27 09:25:5287 const raw_ptr<AbstractWebAppDatabaseFactory> database_factory_;
Alexey Baskakov0b50ec62019-10-01 03:29:2388 ReportErrorCallback error_callback_;
Alexey Baskakovac8c4b02018-11-07 06:10:0289
90 // Database is opened if store is created and all data read.
91 bool opened_ = false;
92
93 SEQUENCE_CHECKER(sequence_checker_);
94
95 base::WeakPtrFactory<WebAppDatabase> weak_ptr_factory_{this};
96
Alexey Baskakovac8c4b02018-11-07 06:10:0297};
98
Eric Willigersd32986ad2019-11-04 00:40:0499DisplayMode ToMojomDisplayMode(WebAppProto::DisplayMode display_mode);
Eric Willigers18544282019-10-09 05:59:58100
Eric Willigersd32986ad2019-11-04 00:40:04101DisplayMode ToMojomDisplayMode(
Eric Willigers14c5e572019-10-29 11:39:49102 ::sync_pb::WebAppSpecifics::UserDisplayMode user_display_mode);
103
Eric Willigersd32986ad2019-11-04 00:40:04104WebAppProto::DisplayMode ToWebAppProtoDisplayMode(DisplayMode display_mode);
Eric Willigers18544282019-10-09 05:59:58105
Alexey Baskakovac8c4b02018-11-07 06:10:02106} // namespace web_app
107
108#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_DATABASE_H_