| 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 <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "components/leveldb_proto/proto_database.h" | 17 #include "components/leveldb_proto/proto_database.h" |
| 18 #include "components/offline_pages/offline_page_metadata_store.h" | 18 #include "components/offline_pages/offline_page_metadata_store.h" |
| 19 | 19 |
| 20 namespace base { | 20 namespace base { |
| 21 class SequencedTaskRunner; | 21 class SequencedTaskRunner; |
| 22 } | 22 } |
| 23 | 23 |
| 24 namespace offline_pages { | 24 namespace offline_pages { |
| 25 | 25 |
| 26 class OfflinePageEntry; | 26 class OfflinePageEntry; |
| 27 | 27 |
| 28 // Implements OfflinePageMetadataStore using leveldb_proto::Protoatabase | 28 // Implements OfflinePageMetadataStore using leveldb_proto::ProtoDatabase |
| 29 // component. Stores metadata of offline pages as serialized protobufs in a | 29 // component. Stores metadata of offline pages as serialized protobufs in a |
| 30 // LevelDB key/value pairs. | 30 // LevelDB key/value pairs. |
| 31 // Underlying implementation guarantees that all of the method calls will be | 31 // Underlying implementation guarantees that all of the method calls will be |
| 32 // executed sequentially, and started operations will finish even after the | 32 // executed sequentially, and started operations will finish even after the |
| 33 // store is already destroyed (callbacks will be called). | 33 // store is already destroyed (callbacks will be called). |
| 34 class OfflinePageMetadataStoreImpl : public OfflinePageMetadataStore { | 34 class OfflinePageMetadataStoreImpl : public OfflinePageMetadataStore { |
| 35 public: | 35 public: |
| 36 OfflinePageMetadataStoreImpl( | 36 OfflinePageMetadataStoreImpl( |
| 37 scoped_refptr<base::SequencedTaskRunner> background_task_runner, | 37 scoped_refptr<base::SequencedTaskRunner> background_task_runner, |
| 38 const base::FilePath& database_dir); | 38 const base::FilePath& database_dir); |
| 39 ~OfflinePageMetadataStoreImpl() override; | 39 ~OfflinePageMetadataStoreImpl() override; |
| 40 | 40 |
| 41 // OfflinePageMetadataStore implementation: | 41 // OfflinePageMetadataStore implementation: |
| 42 void Load(const LoadCallback& callback) override; | 42 void Load(const LoadCallback& callback) override; |
| 43 void AddOrUpdateOfflinePage(const OfflinePageItem& offline_page_record, | 43 void AddOrUpdateOfflinePage(const OfflinePageItem& offline_page_record, |
| 44 const UpdateCallback& callback) override; | 44 const UpdateCallback& callback) override; |
| 45 void RemoveOfflinePages(const std::vector<int64_t>& bookmark_ids, | 45 void RemoveOfflinePages(const std::vector<int64_t>& offline_ids, |
| 46 const UpdateCallback& callback) override; | 46 const UpdateCallback& callback) override; |
| 47 void Reset(const ResetCallback& callback) override; | 47 void Reset(const ResetCallback& callback) override; |
| 48 | 48 |
| 49 private: | 49 private: |
| 50 void LoadContinuation(const LoadCallback& callback, bool success); | 50 void LoadContinuation(const LoadCallback& callback, bool success); |
| 51 void LoadDone(const LoadCallback& callback, | 51 void LoadDone(const LoadCallback& callback, |
| 52 bool success, | 52 bool success, |
| 53 scoped_ptr<std::vector<OfflinePageEntry>> entries); | 53 scoped_ptr<std::vector<OfflinePageEntry>> entries); |
| 54 void NotifyLoadResult(const LoadCallback& callback, | 54 void NotifyLoadResult(const LoadCallback& callback, |
| 55 LoadStatus status, | 55 LoadStatus status, |
| 56 const std::vector<OfflinePageItem>& result); | 56 const std::vector<OfflinePageItem>& result); |
| 57 | 57 |
| 58 // Implements the update. | 58 // Implements the update. |
| 59 void UpdateEntries( | 59 void UpdateEntries( |
| 60 scoped_ptr<leveldb_proto::ProtoDatabase<OfflinePageEntry>::KeyEntryVector> | 60 scoped_ptr<leveldb_proto::ProtoDatabase<OfflinePageEntry>::KeyEntryVector> |
| 61 entries_to_save, | 61 entries_to_save, |
| 62 scoped_ptr<std::vector<std::string>> keys_to_remove, | 62 scoped_ptr<std::vector<std::string>> keys_to_remove, |
| 63 const UpdateCallback& callback); | 63 const UpdateCallback& callback); |
| 64 | 64 |
| 65 void UpdateDone(const OfflinePageMetadataStore::UpdateCallback& callback, | 65 void UpdateDone(const OfflinePageMetadataStore::UpdateCallback& callback, |
| 66 bool success); | 66 bool success); |
| 67 | 67 |
| 68 void ResetDone(const ResetCallback& callback, bool success); | 68 void ResetDone(const ResetCallback& callback, bool success); |
| 69 | 69 |
| 70 // Called when a database has to be migrated from old bookmark ids |
| 71 // to new offline ids. |
| 72 // TODO(bburns): Perhaps remove this eventually? |
| 73 void DatabaseUpdateDone(const OfflinePageMetadataStore::LoadCallback& cb, |
| 74 LoadStatus status, |
| 75 const std::vector<OfflinePageItem>& result, |
| 76 bool success); |
| 77 |
| 70 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; | 78 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; |
| 71 base::FilePath database_dir_; | 79 base::FilePath database_dir_; |
| 72 scoped_ptr<leveldb_proto::ProtoDatabase<OfflinePageEntry>> database_; | 80 scoped_ptr<leveldb_proto::ProtoDatabase<OfflinePageEntry>> database_; |
| 73 | 81 |
| 74 base::WeakPtrFactory<OfflinePageMetadataStoreImpl> weak_ptr_factory_; | 82 base::WeakPtrFactory<OfflinePageMetadataStoreImpl> weak_ptr_factory_; |
| 75 | 83 |
| 76 FRIEND_TEST_ALL_PREFIXES(OfflinePageMetadataStoreImplTest, | 84 FRIEND_TEST_ALL_PREFIXES(OfflinePageMetadataStoreImplTest, |
| 77 LoadCorruptedStore); | 85 LoadCorruptedStore); |
| 78 FRIEND_TEST_ALL_PREFIXES(OfflinePageMetadataStoreImplTest, | 86 FRIEND_TEST_ALL_PREFIXES(OfflinePageMetadataStoreImplTest, |
| 79 LoadTotallyCorruptedStore); | 87 LoadTotallyCorruptedStore); |
| 80 | 88 |
| 81 DISALLOW_COPY_AND_ASSIGN(OfflinePageMetadataStoreImpl); | 89 DISALLOW_COPY_AND_ASSIGN(OfflinePageMetadataStoreImpl); |
| 82 }; | 90 }; |
| 83 | 91 |
| 84 } // namespace offline_pages | 92 } // namespace offline_pages |
| 85 | 93 |
| 86 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_IMPL_H_ | 94 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_IMPL_H_ |
| OLD | NEW |