blob: 99b12f6115cc58dca9921cb01b5588efa6218745 [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 Baskakovac8c4b02018-11-07 06:10:0211#include "base/memory/weak_ptr.h"
12#include "base/optional.h"
13#include "base/sequence_checker.h"
Eric Willigersd32986ad2019-11-04 00:40:0414#include "chrome/browser/web_applications/components/web_app_constants.h"
Glen Robertson271d1332020-01-28 00:16:0215#include "chrome/browser/web_applications/components/web_app_id.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"
Alexey Baskakovac8c4b02018-11-07 06:10:0220
21namespace syncer {
22class ModelError;
Alexey Baskakov0b50ec62019-10-01 03:29:2323class MetadataBatch;
Alexey Baskakovf8fb8c62019-10-10 04:55:4924class MetadataChangeList;
Alexey Baskakovac8c4b02018-11-07 06:10:0225} // namespace syncer
26
27namespace web_app {
28
29class AbstractWebAppDatabaseFactory;
30class WebApp;
31class WebAppProto;
Alexey Baskakov6856e5e42019-09-26 04:31:2632struct RegistryUpdateData;
Alexey Baskakovac8c4b02018-11-07 06:10:0233
34// Exclusively used from the UI thread.
Alexey Baskakov4702d6632019-09-17 06:58:5135class WebAppDatabase {
Alexey Baskakovac8c4b02018-11-07 06:10:0236 public:
Alexey Baskakov0b50ec62019-10-01 03:29:2337 using ReportErrorCallback =
38 base::RepeatingCallback<void(const syncer::ModelError&)>;
39
40 WebAppDatabase(AbstractWebAppDatabaseFactory* database_factory,
41 ReportErrorCallback error_callback);
Haben Fotoe3d073b2020-10-06 01:22:5842 WebAppDatabase(const WebAppDatabase&) = delete;
43 WebAppDatabase& operator=(const WebAppDatabase&) = delete;
Alexey Baskakov4702d6632019-09-17 06:58:5144 ~WebAppDatabase();
Alexey Baskakovac8c4b02018-11-07 06:10:0245
Alexey Baskakov0b50ec62019-10-01 03:29:2346 using RegistryOpenedCallback = base::OnceCallback<void(
47 Registry registry,
48 std::unique_ptr<syncer::MetadataBatch> metadata_batch)>;
Alexey Baskakov27f14d42019-09-20 07:09:4849 // Open existing or create new DB. Read all data and return it via callback.
Alexey Baskakov4702d6632019-09-17 06:58:5150 void OpenDatabase(RegistryOpenedCallback callback);
Alexey Baskakov6856e5e42019-09-26 04:31:2651
52 using CompletionCallback = base::OnceCallback<void(bool success)>;
Alexey Baskakovd6e93822019-10-11 02:39:4453 void Write(const RegistryUpdateData& update_data,
54 std::unique_ptr<syncer::MetadataChangeList> metadata_change_list,
55 CompletionCallback callback);
Alexey Baskakovac8c4b02018-11-07 06:10:0256
57 // Exposed for testing.
58 static std::unique_ptr<WebAppProto> CreateWebAppProto(const WebApp& web_app);
59 // Exposed for testing.
Alexey Baskakov27f14d42019-09-20 07:09:4860 static std::unique_ptr<WebApp> ParseWebApp(const AppId& app_id,
61 const std::string& value);
Alexey Baskakovac8c4b02018-11-07 06:10:0262
Alexey Baskakov583f6edf2020-05-21 03:56:1163 bool is_opened() const { return opened_; }
64
Alexey Baskakovac8c4b02018-11-07 06:10:0265 private:
Alexey Baskakov8c864f182019-10-04 03:39:5666 static std::unique_ptr<WebApp> CreateWebApp(const WebAppProto& local_data);
Alexey Baskakoveecc87f12019-09-24 08:03:0167
68 void OnDatabaseOpened(RegistryOpenedCallback callback,
69 const base::Optional<syncer::ModelError>& error,
70 std::unique_ptr<syncer::ModelTypeStore> store);
Alexey Baskakovac8c4b02018-11-07 06:10:0271
72 void OnAllDataRead(
Alexey Baskakov4702d6632019-09-17 06:58:5173 RegistryOpenedCallback callback,
Alexey Baskakovac8c4b02018-11-07 06:10:0274 const base::Optional<syncer::ModelError>& error,
75 std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records);
Alexey Baskakov0b50ec62019-10-01 03:29:2376 void OnAllMetadataRead(
77 std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records,
78 RegistryOpenedCallback callback,
79 const base::Optional<syncer::ModelError>& error,
80 std::unique_ptr<syncer::MetadataBatch> metadata_batch);
Alexey Baskakovac8c4b02018-11-07 06:10:0281
Alexey Baskakov00fb85eae2019-09-03 07:29:1282 void OnDataWritten(CompletionCallback callback,
83 const base::Optional<syncer::ModelError>& error);
Alexey Baskakovac8c4b02018-11-07 06:10:0284
Alexey Baskakovac8c4b02018-11-07 06:10:0285 std::unique_ptr<syncer::ModelTypeStore> store_;
Eric Willigers5a71d192019-10-08 02:36:4086 AbstractWebAppDatabaseFactory* const database_factory_;
Alexey Baskakov0b50ec62019-10-01 03:29:2387 ReportErrorCallback error_callback_;
Alexey Baskakovac8c4b02018-11-07 06:10:0288
89 // Database is opened if store is created and all data read.
90 bool opened_ = false;
91
92 SEQUENCE_CHECKER(sequence_checker_);
93
94 base::WeakPtrFactory<WebAppDatabase> weak_ptr_factory_{this};
95
Alexey Baskakovac8c4b02018-11-07 06:10:0296};
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
Alexey Baskakovac8c4b02018-11-07 06:10:02105} // namespace web_app
106
107#endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_DATABASE_H_