blob: bca34a554be55df98fa34f8aa7c459f369268e78 [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"
18
19namespace syncer {
20class ModelError;
Alexey Baskakov0b50ec62019-10-01 03:29:2321class MetadataBatch;
Alexey Baskakovac8c4b02018-11-07 06:10:0222} // namespace syncer
23
24namespace web_app {
25
26class AbstractWebAppDatabaseFactory;
27class WebApp;
28class WebAppProto;
Alexey Baskakov6856e5e42019-09-26 04:31:2629struct RegistryUpdateData;
Alexey Baskakovac8c4b02018-11-07 06:10:0230
31// Exclusively used from the UI thread.
Alexey Baskakov4702d6632019-09-17 06:58:5132class WebAppDatabase {
Alexey Baskakovac8c4b02018-11-07 06:10:0233 public:
Alexey Baskakov0b50ec62019-10-01 03:29:2334 using ReportErrorCallback =
35 base::RepeatingCallback<void(const syncer::ModelError&)>;
36
37 WebAppDatabase(AbstractWebAppDatabaseFactory* database_factory,
38 ReportErrorCallback error_callback);
Alexey Baskakov4702d6632019-09-17 06:58:5139 ~WebAppDatabase();
Alexey Baskakovac8c4b02018-11-07 06:10:0240
Alexey Baskakov0b50ec62019-10-01 03:29:2341 using RegistryOpenedCallback = base::OnceCallback<void(
42 Registry registry,
43 std::unique_ptr<syncer::MetadataBatch> metadata_batch)>;
Alexey Baskakov27f14d42019-09-20 07:09:4844 // Open existing or create new DB. Read all data and return it via callback.
Alexey Baskakov4702d6632019-09-17 06:58:5145 void OpenDatabase(RegistryOpenedCallback callback);
Alexey Baskakov6856e5e42019-09-26 04:31:2646
47 using CompletionCallback = base::OnceCallback<void(bool success)>;
48 // There can be only 1 transaction at a time.
49 void BeginTransaction();
50 void CommitTransaction(const RegistryUpdateData& update_data,
51 CompletionCallback callback);
52 void CancelTransaction();
Alexey Baskakovac8c4b02018-11-07 06:10:0253
54 // Exposed for testing.
55 static std::unique_ptr<WebAppProto> CreateWebAppProto(const WebApp& web_app);
56 // Exposed for testing.
Alexey Baskakov27f14d42019-09-20 07:09:4857 static std::unique_ptr<WebApp> ParseWebApp(const AppId& app_id,
58 const std::string& value);
Alexey Baskakovac8c4b02018-11-07 06:10:0259
60 private:
Alexey Baskakoveecc87f12019-09-24 08:03:0161 static std::unique_ptr<WebApp> CreateWebApp(const WebAppProto& proto);
62
63 void OnDatabaseOpened(RegistryOpenedCallback callback,
64 const base::Optional<syncer::ModelError>& error,
65 std::unique_ptr<syncer::ModelTypeStore> store);
Alexey Baskakovac8c4b02018-11-07 06:10:0266
67 void OnAllDataRead(
Alexey Baskakov4702d6632019-09-17 06:58:5168 RegistryOpenedCallback callback,
Alexey Baskakovac8c4b02018-11-07 06:10:0269 const base::Optional<syncer::ModelError>& error,
70 std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records);
Alexey Baskakov0b50ec62019-10-01 03:29:2371 void OnAllMetadataRead(
72 std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records,
73 RegistryOpenedCallback callback,
74 const base::Optional<syncer::ModelError>& error,
75 std::unique_ptr<syncer::MetadataBatch> metadata_batch);
Alexey Baskakovac8c4b02018-11-07 06:10:0276
Alexey Baskakov00fb85eae2019-09-03 07:29:1277 void OnDataWritten(CompletionCallback callback,
78 const base::Optional<syncer::ModelError>& error);
Alexey Baskakovac8c4b02018-11-07 06:10:0279
Alexey Baskakovac8c4b02018-11-07 06:10:0280 std::unique_ptr<syncer::ModelTypeStore> store_;
81 std::unique_ptr<syncer::ModelTypeStore::WriteBatch> write_batch_;
82 AbstractWebAppDatabaseFactory* database_factory_;
Alexey Baskakov0b50ec62019-10-01 03:29:2383 ReportErrorCallback error_callback_;
Alexey Baskakovac8c4b02018-11-07 06:10:0284
85 // Database is opened if store is created and all data read.
86 bool opened_ = false;
87
88 SEQUENCE_CHECKER(sequence_checker_);
89
90 base::WeakPtrFactory<WebAppDatabase> weak_ptr_factory_{this};
91
92 DISALLOW_COPY_AND_ASSIGN(WebAppDatabase);
93};
94
95} // namespace web_app
96
97#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_DATABASE_H_