blob: 4efe69ec6e99f6099bba44e099981fd9d172b4fe [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"
Alexey Baskakov27f14d42019-09-20 07:09:4816#include "chrome/browser/web_applications/web_app_registrar.h"
Alexey Baskakovac8c4b02018-11-07 06:10:0217#include "components/sync/model/model_type_store.h"
Eric Willigers18544282019-10-09 05:59:5818#include "components/sync/protocol/web_app_specifics.pb.h"
19#include "third_party/blink/public/mojom/manifest/display_mode.mojom-forward.h"
Alexey Baskakovac8c4b02018-11-07 06:10:0220
21namespace syncer {
22class ModelError;
Alexey Baskakov0b50ec62019-10-01 03:29:2323class MetadataBatch;
Alexey Baskakovac8c4b02018-11-07 06:10:0224} // namespace syncer
25
26namespace web_app {
27
28class AbstractWebAppDatabaseFactory;
29class WebApp;
30class WebAppProto;
Alexey Baskakov6856e5e42019-09-26 04:31:2631struct RegistryUpdateData;
Alexey Baskakovac8c4b02018-11-07 06:10:0232
33// Exclusively used from the UI thread.
Alexey Baskakov4702d6632019-09-17 06:58:5134class WebAppDatabase {
Alexey Baskakovac8c4b02018-11-07 06:10:0235 public:
Alexey Baskakov0b50ec62019-10-01 03:29:2336 using ReportErrorCallback =
37 base::RepeatingCallback<void(const syncer::ModelError&)>;
38
39 WebAppDatabase(AbstractWebAppDatabaseFactory* database_factory,
40 ReportErrorCallback error_callback);
Alexey Baskakov4702d6632019-09-17 06:58:5141 ~WebAppDatabase();
Alexey Baskakovac8c4b02018-11-07 06:10:0242
Alexey Baskakov0b50ec62019-10-01 03:29:2343 using RegistryOpenedCallback = base::OnceCallback<void(
44 Registry registry,
45 std::unique_ptr<syncer::MetadataBatch> metadata_batch)>;
Alexey Baskakov27f14d42019-09-20 07:09:4846 // Open existing or create new DB. Read all data and return it via callback.
Alexey Baskakov4702d6632019-09-17 06:58:5147 void OpenDatabase(RegistryOpenedCallback callback);
Alexey Baskakov6856e5e42019-09-26 04:31:2648
49 using CompletionCallback = base::OnceCallback<void(bool success)>;
50 // There can be only 1 transaction at a time.
51 void BeginTransaction();
52 void CommitTransaction(const RegistryUpdateData& update_data,
53 CompletionCallback callback);
54 void CancelTransaction();
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_;
83 std::unique_ptr<syncer::ModelTypeStore::WriteBatch> write_batch_;
Eric Willigers5a71d192019-10-08 02:36:4084 AbstractWebAppDatabaseFactory* const 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
94 DISALLOW_COPY_AND_ASSIGN(WebAppDatabase);
95};
96
Eric Willigers18544282019-10-09 05:59:5897blink::mojom::DisplayMode ToMojomDisplayMode(
98 ::sync_pb::WebAppSpecifics::DisplayMode display_mode);
99
100::sync_pb::WebAppSpecifics::DisplayMode ToWebAppSpecificsDisplayMode(
101 blink::mojom::DisplayMode display_mode);
102
Alexey Baskakovac8c4b02018-11-07 06:10:02103} // namespace web_app
104
105#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_DATABASE_H_