blob: ef5ea55e583bc788f306a606ed925b020769bdce [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"
Alexey Baskakovac8c4b02018-11-07 06:10:0215#include "chrome/browser/web_applications/components/web_app_helpers.h"
Eric Willigers14c5e572019-10-29 11:39:4916#include "chrome/browser/web_applications/proto/web_app.pb.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"
20#include "third_party/blink/public/mojom/manifest/display_mode.mojom-forward.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
62 private:
Alexey Baskakov8c864f182019-10-04 03:39:5663 static std::unique_ptr<WebApp> CreateWebApp(const WebAppProto& local_data);
Alexey Baskakoveecc87f12019-09-24 08:03:0164
65 void OnDatabaseOpened(RegistryOpenedCallback callback,
66 const base::Optional<syncer::ModelError>& error,
67 std::unique_ptr<syncer::ModelTypeStore> store);
Alexey Baskakovac8c4b02018-11-07 06:10:0268
69 void OnAllDataRead(
Alexey Baskakov4702d6632019-09-17 06:58:5170 RegistryOpenedCallback callback,
Alexey Baskakovac8c4b02018-11-07 06:10:0271 const base::Optional<syncer::ModelError>& error,
72 std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records);
Alexey Baskakov0b50ec62019-10-01 03:29:2373 void OnAllMetadataRead(
74 std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records,
75 RegistryOpenedCallback callback,
76 const base::Optional<syncer::ModelError>& error,
77 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,
80 const base::Optional<syncer::ModelError>& error);
Alexey Baskakovac8c4b02018-11-07 06:10:0281
Alexey Baskakovac8c4b02018-11-07 06:10:0282 std::unique_ptr<syncer::ModelTypeStore> store_;
Eric Willigers5a71d192019-10-08 02:36:4083 AbstractWebAppDatabaseFactory* const database_factory_;
Alexey Baskakov0b50ec62019-10-01 03:29:2384 ReportErrorCallback error_callback_;
Alexey Baskakovac8c4b02018-11-07 06:10:0285
86 // Database is opened if store is created and all data read.
87 bool opened_ = false;
88
89 SEQUENCE_CHECKER(sequence_checker_);
90
91 base::WeakPtrFactory<WebAppDatabase> weak_ptr_factory_{this};
92
93 DISALLOW_COPY_AND_ASSIGN(WebAppDatabase);
94};
95
Eric Willigers18544282019-10-09 05:59:5896blink::mojom::DisplayMode ToMojomDisplayMode(
Eric Willigers14c5e572019-10-29 11:39:4997 WebAppProto::DisplayMode display_mode);
Eric Willigers18544282019-10-09 05:59:5898
Eric Willigers14c5e572019-10-29 11:39:4999blink::mojom::DisplayMode ToMojomDisplayMode(
100 ::sync_pb::WebAppSpecifics::UserDisplayMode user_display_mode);
101
102WebAppProto::DisplayMode ToWebAppProtoDisplayMode(
Eric Willigers18544282019-10-09 05:59:58103 blink::mojom::DisplayMode display_mode);
104
Eric Willigers14c5e572019-10-29 11:39:49105::sync_pb::WebAppSpecifics::UserDisplayMode ToWebAppSpecificsUserDisplayMode(
106 blink::mojom::DisplayMode user_display_mode);
107
Alexey Baskakovac8c4b02018-11-07 06:10:02108} // namespace web_app
109
110#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_DATABASE_H_