blob: 9b387a91862aceee4534acaebf807e04b1290af2 [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"
Song Fangzhencda4af62021-09-09 05:24:0216#include "chrome/browser/web_applications/web_app_constants.h"
Alexey Baskakov27f14d42019-09-20 07:09:4817#include "chrome/browser/web_applications/web_app_registrar.h"
Mikel Astiz9048b122024-08-05 12:55:4118#include "components/sync/model/data_type_store.h"
Eric Willigers18544282019-10-09 05:59:5819#include "components/sync/protocol/web_app_specifics.pb.h"
Glenn Hartmann5f992ed2023-09-25 18:05:3620#include "components/webapps/common/web_app_id.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.
Glenn Hartmann5f992ed2023-09-25 18:05:3661 static std::unique_ptr<WebApp> ParseWebApp(const webapps::AppId& app_id,
Alexey Baskakov27f14d42019-09-20 07:09:4862 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,
Arthur Sonzognife132ee2024-01-15 11:01:0470 const std::optional<syncer::ModelError>& error,
Alexey Baskakoveecc87f12019-09-24 08:03:0171 std::unique_ptr<syncer::ModelTypeStore> store);
Alexey Baskakovac8c4b02018-11-07 06:10:0272
Florian Leimgruberd5731262024-06-06 08:41:2073 void OnAllDataAndMetadataRead(
Alexey Baskakov4702d6632019-09-17 06:58:5174 RegistryOpenedCallback callback,
Arthur Sonzognife132ee2024-01-15 11:01:0475 const std::optional<syncer::ModelError>& error,
Alexey Baskakov0b50ec62019-10-01 03:29:2376 std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records,
Alexey Baskakov0b50ec62019-10-01 03:29:2377 std::unique_ptr<syncer::MetadataBatch> metadata_batch);
Alexey Baskakovac8c4b02018-11-07 06:10:0278
Alexey Baskakov00fb85eae2019-09-03 07:29:1279 void OnDataWritten(CompletionCallback callback,
Arthur Sonzognife132ee2024-01-15 11:01:0480 const std::optional<syncer::ModelError>& error);
Alexey Baskakovac8c4b02018-11-07 06:10:0281
Alexey Baskakovac8c4b02018-11-07 06:10:0282 std::unique_ptr<syncer::ModelTypeStore> store_;
Arthur Sonzognie98d2142023-06-01 15:02:2583 const raw_ptr<AbstractWebAppDatabaseFactory, DanglingUntriaged>
84 database_factory_;
Alexey Baskakov0b50ec62019-10-01 03:29:2385 ReportErrorCallback error_callback_;
Alexey Baskakovac8c4b02018-11-07 06:10:0286
87 // Database is opened if store is created and all data read.
88 bool opened_ = false;
89
90 SEQUENCE_CHECKER(sequence_checker_);
91
92 base::WeakPtrFactory<WebAppDatabase> weak_ptr_factory_{this};
93
Alexey Baskakovac8c4b02018-11-07 06:10:0294};
95
Eric Willigersd32986ad2019-11-04 00:40:0496DisplayMode ToMojomDisplayMode(WebAppProto::DisplayMode display_mode);
Eric Willigers18544282019-10-09 05:59:5897
Eric Willigersd32986ad2019-11-04 00:40:0498WebAppProto::DisplayMode ToWebAppProtoDisplayMode(DisplayMode display_mode);
Eric Willigers18544282019-10-09 05:59:5899
Alexey Baskakovac8c4b02018-11-07 06:10:02100} // namespace web_app
101
102#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_DATABASE_H_