Roll src/third_party/WebKit c63b89c:29324ab (svn 202546:202547)
[chromium-blink-merge.git] / components / offline_pages / offline_page_metadata_store_impl.h
blob828f9350edd9bf711baf95660284ef91745ebea1
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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_IMPL_H_
6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_IMPL_H_
8 #include <vector>
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "components/leveldb_proto/proto_database.h"
13 #include "components/offline_pages/offline_page_metadata_store.h"
15 namespace base {
16 class FilePath;
19 namespace offline_pages {
21 class OfflinePageEntry;
23 // Implements OfflinePageMetadataStore using leveldb_proto::ProtoDatabase
24 // component. Stores metadata of offline pages as serialized protobufs in a
25 // LevelDB key/value pairs.
26 // Underlying implementation guarantees that all of the method calls will be
27 // executed sequentially, and started operations will finish even after the
28 // store is already destroyed (callbacks will be called).
29 class OfflinePageMetadataStoreImpl : public OfflinePageMetadataStore {
30 public:
31 OfflinePageMetadataStoreImpl(
32 scoped_ptr<leveldb_proto::ProtoDatabase<OfflinePageEntry>> database,
33 const base::FilePath& database_dir);
34 ~OfflinePageMetadataStoreImpl() override;
36 // OfflinePageMetadataStore implementation:
37 void Load(const LoadCallback& callback) override;
38 void AddOfflinePage(const OfflinePageItem& offline_page_record,
39 const UpdateCallback& callback) override;
40 void RemoveOfflinePages(const std::vector<int64>& bookmark_ids,
41 const UpdateCallback& callback) override;
43 private:
44 // Callback for when initialization of the |database_| is done.
45 void OnInitDone(bool success);
47 // Implements the update.
48 void UpdateEntries(
49 scoped_ptr<leveldb_proto::ProtoDatabase<OfflinePageEntry>::KeyEntryVector>
50 entries_to_save,
51 scoped_ptr<std::vector<std::string>> keys_to_remove,
52 const UpdateCallback& callback);
54 // Resets the database. This is to be used when one of the operations fails
55 // with no good explanation.
56 void ResetDB();
58 scoped_ptr<leveldb_proto::ProtoDatabase<OfflinePageEntry>> database_;
60 base::WeakPtrFactory<OfflinePageMetadataStoreImpl> weak_ptr_factory_;
62 DISALLOW_COPY_AND_ASSIGN(OfflinePageMetadataStoreImpl);
65 } // namespace offline_pages
67 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_IMPL_H_