blob: 9ef4edf467e9a760ce1f61e7a9d1d540b253536f [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"
Alexey Baskakov27f14d42019-09-20 07:09:4811#include "base/containers/flat_set.h"
Alexey Baskakovac8c4b02018-11-07 06:10:0212#include "base/macros.h"
13#include "base/memory/weak_ptr.h"
14#include "base/optional.h"
15#include "base/sequence_checker.h"
Alexey Baskakovac8c4b02018-11-07 06:10:0216#include "chrome/browser/web_applications/components/web_app_helpers.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"
19
20namespace syncer {
21class ModelError;
22} // namespace syncer
23
24namespace web_app {
25
26class AbstractWebAppDatabaseFactory;
27class WebApp;
28class WebAppProto;
29
30// Exclusively used from the UI thread.
Alexey Baskakov4702d6632019-09-17 06:58:5131class WebAppDatabase {
Alexey Baskakovac8c4b02018-11-07 06:10:0232 public:
33 explicit WebAppDatabase(AbstractWebAppDatabaseFactory* database_factory);
Alexey Baskakov4702d6632019-09-17 06:58:5134 ~WebAppDatabase();
Alexey Baskakovac8c4b02018-11-07 06:10:0235
Alexey Baskakov27f14d42019-09-20 07:09:4836 using RegistryOpenedCallback = base::OnceCallback<void(Registry registry)>;
37 using CompletionCallback = base::OnceCallback<void(bool success)>;
38 using AppsToWrite = base::flat_set<const WebApp*>;
Alexey Baskakov4702d6632019-09-17 06:58:5139
Alexey Baskakov27f14d42019-09-20 07:09:4840 // Open existing or create new DB. Read all data and return it via callback.
Alexey Baskakov4702d6632019-09-17 06:58:5141 void OpenDatabase(RegistryOpenedCallback callback);
42 void WriteWebApps(AppsToWrite apps, CompletionCallback callback);
43 void DeleteWebApps(std::vector<AppId> app_ids, CompletionCallback callback);
Alexey Baskakovac8c4b02018-11-07 06:10:0244
45 // Exposed for testing.
46 static std::unique_ptr<WebAppProto> CreateWebAppProto(const WebApp& web_app);
47 // Exposed for testing.
Alexey Baskakov27f14d42019-09-20 07:09:4848 static std::unique_ptr<WebApp> ParseWebApp(const AppId& app_id,
49 const std::string& value);
Alexey Baskakovac8c4b02018-11-07 06:10:0250
51 private:
Alexey Baskakoveecc87f12019-09-24 08:03:0152 static std::unique_ptr<WebApp> CreateWebApp(const WebAppProto& proto);
53
54 void OnDatabaseOpened(RegistryOpenedCallback callback,
55 const base::Optional<syncer::ModelError>& error,
56 std::unique_ptr<syncer::ModelTypeStore> store);
Alexey Baskakovac8c4b02018-11-07 06:10:0257
58 void OnAllDataRead(
Alexey Baskakov4702d6632019-09-17 06:58:5159 RegistryOpenedCallback callback,
Alexey Baskakovac8c4b02018-11-07 06:10:0260 const base::Optional<syncer::ModelError>& error,
61 std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records);
62
Alexey Baskakov00fb85eae2019-09-03 07:29:1263 void OnDataWritten(CompletionCallback callback,
64 const base::Optional<syncer::ModelError>& error);
Alexey Baskakovac8c4b02018-11-07 06:10:0265
Alexey Baskakovac8c4b02018-11-07 06:10:0266 void BeginTransaction();
Alexey Baskakov00fb85eae2019-09-03 07:29:1267 void CommitTransaction(CompletionCallback callback);
Alexey Baskakovac8c4b02018-11-07 06:10:0268
69 std::unique_ptr<syncer::ModelTypeStore> store_;
70 std::unique_ptr<syncer::ModelTypeStore::WriteBatch> write_batch_;
71 AbstractWebAppDatabaseFactory* database_factory_;
72
73 // Database is opened if store is created and all data read.
74 bool opened_ = false;
75
76 SEQUENCE_CHECKER(sequence_checker_);
77
78 base::WeakPtrFactory<WebAppDatabase> weak_ptr_factory_{this};
79
80 DISALLOW_COPY_AND_ASSIGN(WebAppDatabase);
81};
82
83} // namespace web_app
84
85#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_DATABASE_H_