OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_IMPL_H_ | 5 #ifndef COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_IMPL_H_ |
6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_IMPL_H_ | 6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_IMPL_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
| 10 #include "base/files/file_path.h" |
10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
12 #include "components/leveldb_proto/proto_database.h" | 13 #include "components/leveldb_proto/proto_database.h" |
13 #include "components/offline_pages/offline_page_metadata_store.h" | 14 #include "components/offline_pages/offline_page_metadata_store.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 class FilePath; | 17 class SequencedTaskRunner; |
17 } | 18 } |
18 | 19 |
19 namespace offline_pages { | 20 namespace offline_pages { |
20 | 21 |
21 class OfflinePageEntry; | 22 class OfflinePageEntry; |
22 | 23 |
23 // Implements OfflinePageMetadataStore using leveldb_proto::ProtoDatabase | 24 // Implements OfflinePageMetadataStore using leveldb_proto::ProtoDatabase |
24 // component. Stores metadata of offline pages as serialized protobufs in a | 25 // component. Stores metadata of offline pages as serialized protobufs in a |
25 // LevelDB key/value pairs. | 26 // LevelDB key/value pairs. |
26 // Underlying implementation guarantees that all of the method calls will be | 27 // Underlying implementation guarantees that all of the method calls will be |
27 // executed sequentially, and started operations will finish even after the | 28 // executed sequentially, and started operations will finish even after the |
28 // store is already destroyed (callbacks will be called). | 29 // store is already destroyed (callbacks will be called). |
29 class OfflinePageMetadataStoreImpl : public OfflinePageMetadataStore { | 30 class OfflinePageMetadataStoreImpl : public OfflinePageMetadataStore { |
30 public: | 31 public: |
31 OfflinePageMetadataStoreImpl( | 32 OfflinePageMetadataStoreImpl( |
32 scoped_ptr<leveldb_proto::ProtoDatabase<OfflinePageEntry>> database, | 33 scoped_refptr<base::SequencedTaskRunner> background_task_runner, |
33 const base::FilePath& database_dir); | 34 const base::FilePath& database_dir); |
34 ~OfflinePageMetadataStoreImpl() override; | 35 ~OfflinePageMetadataStoreImpl() override; |
35 | 36 |
36 // OfflinePageMetadataStore implementation: | 37 // OfflinePageMetadataStore implementation: |
37 void Load(const LoadCallback& callback) override; | 38 void Load(const LoadCallback& callback) override; |
38 void AddOrUpdateOfflinePage(const OfflinePageItem& offline_page_record, | 39 void AddOrUpdateOfflinePage(const OfflinePageItem& offline_page_record, |
39 const UpdateCallback& callback) override; | 40 const UpdateCallback& callback) override; |
40 void RemoveOfflinePages(const std::vector<int64>& bookmark_ids, | 41 void RemoveOfflinePages(const std::vector<int64>& bookmark_ids, |
41 const UpdateCallback& callback) override; | 42 const UpdateCallback& callback) override; |
| 43 void Reset(const ResetCallback& callback) override; |
42 | 44 |
43 private: | 45 private: |
44 // Callback for when initialization of the |database_| is done. | 46 void LoadContinuation(const LoadCallback& callback, bool success); |
45 void OnInitDone(bool success); | 47 void LoadCompleted(const LoadCallback& callback, |
| 48 bool success, |
| 49 scoped_ptr<std::vector<OfflinePageEntry>> entries); |
46 | 50 |
47 // Implements the update. | 51 // Implements the update. |
48 void UpdateEntries( | 52 void UpdateEntries( |
49 scoped_ptr<leveldb_proto::ProtoDatabase<OfflinePageEntry>::KeyEntryVector> | 53 scoped_ptr<leveldb_proto::ProtoDatabase<OfflinePageEntry>::KeyEntryVector> |
50 entries_to_save, | 54 entries_to_save, |
51 scoped_ptr<std::vector<std::string>> keys_to_remove, | 55 scoped_ptr<std::vector<std::string>> keys_to_remove, |
52 const UpdateCallback& callback); | 56 const UpdateCallback& callback); |
| 57 void UpdateCompleted( |
| 58 const OfflinePageMetadataStore::UpdateCallback& callback, |
| 59 bool success); |
53 | 60 |
54 // Resets the database. This is to be used when one of the operations fails | 61 void ResetCompleted(const ResetCallback& callback, bool success); |
55 // with no good explanation. | |
56 void ResetDB(); | |
57 | 62 |
| 63 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; |
| 64 base::FilePath database_dir_; |
58 scoped_ptr<leveldb_proto::ProtoDatabase<OfflinePageEntry>> database_; | 65 scoped_ptr<leveldb_proto::ProtoDatabase<OfflinePageEntry>> database_; |
59 | 66 |
60 base::WeakPtrFactory<OfflinePageMetadataStoreImpl> weak_ptr_factory_; | 67 base::WeakPtrFactory<OfflinePageMetadataStoreImpl> weak_ptr_factory_; |
61 | 68 |
62 DISALLOW_COPY_AND_ASSIGN(OfflinePageMetadataStoreImpl); | 69 DISALLOW_COPY_AND_ASSIGN(OfflinePageMetadataStoreImpl); |
63 }; | 70 }; |
64 | 71 |
65 } // namespace offline_pages | 72 } // namespace offline_pages |
66 | 73 |
67 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_IMPL_H_ | 74 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_IMPL_H_ |
OLD | NEW |