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; |
| 37 | void WriteWebApp(const WebApp& web_app) override; |
| 38 | void DeleteWebApps(std::vector<AppId> app_ids) override; |
| 39 | |
| 40 | // Exposed for testing. |
| 41 | static std::unique_ptr<WebAppProto> CreateWebAppProto(const WebApp& web_app); |
| 42 | // Exposed for testing. |
| 43 | static std::unique_ptr<WebApp> CreateWebApp(const WebAppProto& proto); |
| 44 | // Exposed for testing. |
| 45 | void ReadRegistry(OnceRegistryOpenedCallback callback); |
| 46 | |
| 47 | private: |
| 48 | void CreateStore(syncer::OnceModelTypeStoreFactory store_factory, |
| 49 | base::OnceClosure closure); |
| 50 | void OnStoreCreated(base::OnceClosure closure, |
| 51 | const base::Optional<syncer::ModelError>& error, |
| 52 | std::unique_ptr<syncer::ModelTypeStore> store); |
| 53 | |
| 54 | void OnAllDataRead( |
| 55 | OnceRegistryOpenedCallback callback, |
| 56 | const base::Optional<syncer::ModelError>& error, |
| 57 | std::unique_ptr<syncer::ModelTypeStore::RecordList> data_records); |
| 58 | |
| 59 | void OnDataWritten(const base::Optional<syncer::ModelError>& error); |
| 60 | |
| 61 | static std::unique_ptr<WebApp> ParseWebApp(const AppId& app_id, |
| 62 | const std::string& value); |
| 63 | |
| 64 | void BeginTransaction(); |
| 65 | void CommitTransaction(); |
| 66 | |
| 67 | std::unique_ptr<syncer::ModelTypeStore> store_; |
| 68 | std::unique_ptr<syncer::ModelTypeStore::WriteBatch> write_batch_; |
| 69 | AbstractWebAppDatabaseFactory* database_factory_; |
| 70 | |
| 71 | // Database is opened if store is created and all data read. |
| 72 | bool opened_ = false; |
| 73 | |
| 74 | SEQUENCE_CHECKER(sequence_checker_); |
| 75 | |
| 76 | base::WeakPtrFactory<WebAppDatabase> weak_ptr_factory_{this}; |
| 77 | |
| 78 | DISALLOW_COPY_AND_ASSIGN(WebAppDatabase); |
| 79 | }; |
| 80 | |
| 81 | } // namespace web_app |
| 82 | |
| 83 | #endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_DATABASE_H_ |