Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 1 | // 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" |
| 15 | #include "chrome/browser/web_applications/abstract_web_app_database.h" |
| 16 | #include "chrome/browser/web_applications/components/web_app_helpers.h" |
| 17 | #include "components/sync/model/model_type_store.h" |
| 18 | |
| 19 | namespace syncer { |
| 20 | class ModelError; |
| 21 | } // namespace syncer |
| 22 | |
| 23 | namespace web_app { |
| 24 | |
| 25 | class AbstractWebAppDatabaseFactory; |
| 26 | class WebApp; |
| 27 | class WebAppProto; |
| 28 | |
| 29 | // Exclusively used from the UI thread. |
| 30 | class WebAppDatabase : public AbstractWebAppDatabase { |
| 31 | public: |
| 32 | explicit WebAppDatabase(AbstractWebAppDatabaseFactory* database_factory); |
| 33 | ~WebAppDatabase() override; |
| 34 | |
| 35 | // AbstractWebAppDatabase: |
| 36 | void OpenDatabase(OnceRegistryOpenedCallback callback) override; |
Alexey Baskakov | 00fb85eae | 2019-09-03 07:29:12 | [diff] [blame^] | 37 | void WriteWebApps(AppsToWrite apps, CompletionCallback callback) override; |
| 38 | void DeleteWebApps(std::vector<AppId> app_ids, |
| 39 | CompletionCallback callback) override; |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 40 | |
| 41 | // Exposed for testing. |
| 42 | static std::unique_ptr<WebAppProto> CreateWebAppProto(const WebApp& web_app); |
| 43 | // Exposed for testing. |
| 44 | static std::unique_ptr<WebApp> CreateWebApp(const WebAppProto& proto); |
| 45 | // Exposed for testing. |
| 46 | void ReadRegistry(OnceRegistryOpenedCallback callback); |
| 47 | |
| 48 | private: |
| 49 | void CreateStore(syncer::OnceModelTypeStoreFactory store_factory, |
| 50 | base::OnceClosure closure); |
| 51 | void OnStoreCreated(base::OnceClosure closure, |
| 52 | const base::Optional<syncer::ModelError>& error, |
| 53 | std::unique_ptr<syncer::ModelTypeStore> store); |
| 54 | |
| 55 | void OnAllDataRead( |
| 56 | OnceRegistryOpenedCallback callback, |
| 57 | const base::Optional<syncer::ModelError>& error, |
| 58 | std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records); |
| 59 | |
Alexey Baskakov | 00fb85eae | 2019-09-03 07:29:12 | [diff] [blame^] | 60 | void OnDataWritten(CompletionCallback callback, |
| 61 | const base::Optional<syncer::ModelError>& error); |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 62 | |
| 63 | static std::unique_ptr<WebApp> ParseWebApp(const AppId& app_id, |
| 64 | const std::string& value); |
| 65 | |
| 66 | void BeginTransaction(); |
Alexey Baskakov | 00fb85eae | 2019-09-03 07:29:12 | [diff] [blame^] | 67 | void CommitTransaction(CompletionCallback callback); |
Alexey Baskakov | ac8c4b0 | 2018-11-07 06:10:02 | [diff] [blame] | 68 | |
| 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_ |