| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_SQL_H_ |
| 6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_SQL_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <memory> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/files/file_path.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "components/offline_pages/offline_page_metadata_store.h" |
| 16 |
| 17 namespace sql { |
| 18 class Connection; |
| 19 class MetaTable; |
| 20 class Statement; |
| 21 class StatementID; |
| 22 } |
| 23 |
| 24 namespace base { |
| 25 class SequencedTaskRunner; |
| 26 } |
| 27 |
| 28 namespace offline_pages { |
| 29 |
| 30 // OfflinePageMetadataStoreSQL is an instance of OfflinePageMetadataStore |
| 31 // which is implemented using a SQLite database. |
| 32 class OfflinePageMetadataStoreSQL : public OfflinePageMetadataStore { |
| 33 public: |
| 34 OfflinePageMetadataStoreSQL( |
| 35 scoped_refptr<base::SequencedTaskRunner> background_task_runner, |
| 36 const base::FilePath& database_dir); |
| 37 ~OfflinePageMetadataStoreSQL() override; |
| 38 |
| 39 // Implementation methods. |
| 40 void Load(const LoadCallback& callback) override; |
| 41 void AddOrUpdateOfflinePage(const OfflinePageItem& offline_page, |
| 42 const UpdateCallback& callback) override; |
| 43 void RemoveOfflinePages(const std::vector<int64_t>& offline_ids, |
| 44 const UpdateCallback& callback) override; |
| 45 void Reset(const ResetCallback& callback) override; |
| 46 |
| 47 // If true, use an in-memory database to make testing easier. |
| 48 // Only has an effect if you call it before a call to Load(...). |
| 49 // Should only be used for testing. |
| 50 void SetInMemoryDatabaseForTesting(bool in_memory); |
| 51 |
| 52 private: |
| 53 bool InitDatabase(); |
| 54 |
| 55 // Synchronous implementations, these are run on the background thread |
| 56 // and actually do the work to access SQL. The implementations above |
| 57 // simply dispatch to the corresponding *Sync method on the background thread. |
| 58 // 'runner' is where to run the callback. |
| 59 void LoadSync(scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 60 const LoadCallback& callback); |
| 61 void AddOrUpdateOfflinePageSync( |
| 62 const OfflinePageItem& offline_page, |
| 63 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 64 const UpdateCallback& callback); |
| 65 void RemoveOfflinePagesSync( |
| 66 const std::vector<int64_t>& offline_ids, |
| 67 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 68 const UpdateCallback& callback); |
| 69 void ResetSync(scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 70 const ResetCallback& callback); |
| 71 |
| 72 void NotifyLoadResult(scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 73 const LoadCallback& callback, |
| 74 LoadStatus status, |
| 75 const std::vector<OfflinePageItem>& result); |
| 76 |
| 77 // Background thread where all SQL access should be run. |
| 78 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; |
| 79 // Path to the database on disk. |
| 80 base::FilePath db_file_path_; |
| 81 |
| 82 std::unique_ptr<sql::Connection> db_; |
| 83 |
| 84 // only useful for testing. |
| 85 bool use_in_memory_; |
| 86 |
| 87 base::WeakPtrFactory<OfflinePageMetadataStoreSQL> weak_ptr_factory_; |
| 88 |
| 89 DISALLOW_COPY_AND_ASSIGN(OfflinePageMetadataStoreSQL); |
| 90 }; |
| 91 |
| 92 } // namespace offline_pages |
| 93 |
| 94 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_SQL_H_ |
| OLD | NEW |