OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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_FEATURE_ENGAGEMENT_TRACKER_INTERNAL_PERSISTENT_STORE_H_ |
| 6 #define COMPONENTS_FEATURE_ENGAGEMENT_TRACKER_INTERNAL_PERSISTENT_STORE_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "components/feature_engagement_tracker/internal/proto/event.pb.h" |
| 15 #include "components/feature_engagement_tracker/internal/store.h" |
| 16 #include "components/leveldb_proto/proto_database.h" |
| 17 |
| 18 namespace feature_engagement_tracker { |
| 19 |
| 20 // A PersistentStore provides a DB layer that persists the data to disk. The |
| 21 // data is retrieved once during the load process and after that this store is |
| 22 // write only. Data will be persisted asynchronously so it is not guaranteed to |
| 23 // always save every write during shutdown. |
| 24 class PersistentStore : public Store { |
| 25 public: |
| 26 // Builds a PersistentStore backed by the ProtoDatabase |db|. The database |
| 27 // will be loaded and/or created at |storage_dir|. |
| 28 PersistentStore(const base::FilePath& storage_dir, |
| 29 std::unique_ptr<leveldb_proto::ProtoDatabase<Event>> db); |
| 30 ~PersistentStore() override; |
| 31 |
| 32 // Store implementation. |
| 33 void Load(const OnLoadedCallback& callback) override; |
| 34 bool IsReady() const override; |
| 35 void WriteEvent(const Event& event) override; |
| 36 |
| 37 private: |
| 38 void OnInitComplete(const OnLoadedCallback& callback, bool success); |
| 39 void OnLoadComplete(const OnLoadedCallback& callback, |
| 40 bool success, |
| 41 std::unique_ptr<std::vector<Event>> entries); |
| 42 |
| 43 const base::FilePath storage_dir_; |
| 44 std::unique_ptr<leveldb_proto::ProtoDatabase<Event>> db_; |
| 45 |
| 46 // Whether or not the underlying ProtoDatabase is ready. This will be false |
| 47 // until the OnLoadedCallback is broadcast. It will also be false if loading |
| 48 // fails. |
| 49 bool ready_; |
| 50 |
| 51 base::WeakPtrFactory<PersistentStore> weak_ptr_factory_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(PersistentStore); |
| 54 }; |
| 55 |
| 56 } // namespace feature_engagement_tracker |
| 57 |
| 58 #endif // COMPONENTS_FEATURE_ENGAGEMENT_TRACKER_INTERNAL_PERSISTENT_STORE_H_ |
OLD | NEW |