blob: bdc6fe584b50980db70613eb7766a1a4df28a34b [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"
11#include "base/macros.h"
12#include "base/memory/weak_ptr.h"
13#include "base/optional.h"
14#include "base/sequence_checker.h"
Eric Willigersd32986ad2019-11-04 00:40:0415#include "chrome/browser/web_applications/components/web_app_constants.h"
Glen Robertson271d1332020-01-28 00:16:0216#include "chrome/browser/web_applications/components/web_app_id.h"
Eric Willigers14c5e572019-10-29 11:39:4917#include "chrome/browser/web_applications/proto/web_app.pb.h"
Alexey Baskakov27f14d42019-09-20 07:09:4818#include "chrome/browser/web_applications/web_app_registrar.h"
Alexey Baskakovac8c4b02018-11-07 06:10:0219#include "components/sync/model/model_type_store.h"
Eric Willigers18544282019-10-09 05:59:5820#include "components/sync/protocol/web_app_specifics.pb.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);
Alexey Baskakov4702d6632019-09-17 06:58:5143 ~WebAppDatabase();
Alexey Baskakovac8c4b02018-11-07 06:10:0244
Alexey Baskakov0b50ec62019-10-01 03:29:2345 using RegistryOpenedCallback = base::OnceCallback<void(
46 Registry registry,
47 std::unique_ptr<syncer::MetadataBatch> metadata_batch)>;
Alexey Baskakov27f14d42019-09-20 07:09:4848 // Open existing or create new DB. Read all data and return it via callback.
Alexey Baskakov4702d6632019-09-17 06:58:5149 void OpenDatabase(RegistryOpenedCallback callback);
Alexey Baskakov6856e5e42019-09-26 04:31:2650
51 using CompletionCallback = base::OnceCallback<void(bool success)>;
Alexey Baskakovd6e93822019-10-11 02:39:4452 void Write(const RegistryUpdateData& update_data,
53 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
54 CompletionCallback callback);
Alexey Baskakovac8c4b02018-11-07 06:10:0255
56 // Exposed for testing.
57 static std::unique_ptr<WebAppProto> CreateWebAppProto(const WebApp& web_app);
58 // Exposed for testing.
Alexey Baskakov27f14d42019-09-20 07:09:4859 static std::unique_ptr<WebApp> ParseWebApp(const AppId& app_id,
60 const std::string& value);
Alexey Baskakovac8c4b02018-11-07 06:10:0261
Alexey Baskakov583f6edf2020-05-21 03:56:1162 bool is_opened() const { return opened_; }
63
Alexey Baskakovac8c4b02018-11-07 06:10:0264 private:
Alexey Baskakov8c864f182019-10-04 03:39:5665 static std::unique_ptr<WebApp> CreateWebApp(const WebAppProto& local_data);
Alexey Baskakoveecc87f12019-09-24 08:03:0166
67 void OnDatabaseOpened(RegistryOpenedCallback callback,
68 const base::Optional<syncer::ModelError>& error,
69 std::unique_ptr<syncer::ModelTypeStore> store);
Alexey Baskakovac8c4b02018-11-07 06:10:0270
71 void OnAllDataRead(
Alexey Baskakov4702d6632019-09-17 06:58:5172 RegistryOpenedCallback callback,
Alexey Baskakovac8c4b02018-11-07 06:10:0273 const base::Optional<syncer::ModelError>& error,
74 std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records);
Alexey Baskakov0b50ec62019-10-01 03:29:2375 void OnAllMetadataRead(
76 std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records,
77 RegistryOpenedCallback callback,
78 const base::Optional<syncer::ModelError>& error,
79 std::unique_ptr<syncer::MetadataBatch> metadata_batch);
Alexey Baskakovac8c4b02018-11-07 06:10:0280
Alexey Baskakov00fb85eae2019-09-03 07:29:1281 void OnDataWritten(CompletionCallback callback,
82 const base::Optional<syncer::ModelError>& error);
Alexey Baskakovac8c4b02018-11-07 06:10:0283
Alexey Baskakovac8c4b02018-11-07 06:10:0284 std::unique_ptr<syncer::ModelTypeStore> store_;
Eric Willigers5a71d192019-10-08 02:36:4085 AbstractWebAppDatabaseFactory* const database_factory_;
Alexey Baskakov0b50ec62019-10-01 03:29:2386 ReportErrorCallback error_callback_;
Alexey Baskakovac8c4b02018-11-07 06:10:0287
88 // Database is opened if store is created and all data read.
89 bool opened_ = false;
90
91 SEQUENCE_CHECKER(sequence_checker_);
92
93 base::WeakPtrFactory<WebAppDatabase> weak_ptr_factory_{this};
94
95 DISALLOW_COPY_AND_ASSIGN(WebAppDatabase);
96};
97
Eric Willigersd32986ad2019-11-04 00:40:0498DisplayMode ToMojomDisplayMode(WebAppProto::DisplayMode display_mode);
Eric Willigers18544282019-10-09 05:59:5899
Eric Willigersd32986ad2019-11-04 00:40:04100DisplayMode ToMojomDisplayMode(
Eric Willigers14c5e572019-10-29 11:39:49101 ::sync_pb::WebAppSpecifics::UserDisplayMode user_display_mode);
102
Eric Willigersd32986ad2019-11-04 00:40:04103WebAppProto::DisplayMode ToWebAppProtoDisplayMode(DisplayMode display_mode);
Eric Willigers18544282019-10-09 05:59:58104
Eric Willigers14c5e572019-10-29 11:39:49105::sync_pb::WebAppSpecifics::UserDisplayMode ToWebAppSpecificsUserDisplayMode(
Eric Willigersd32986ad2019-11-04 00:40:04106 DisplayMode user_display_mode);
Eric Willigers14c5e572019-10-29 11:39:49107
Alexey Baskakovac8c4b02018-11-07 06:10:02108} // namespace web_app
109
110#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_DATABASE_H_