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 #include "components/feature_engagement_tracker/internal/persistent_store.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 |
| 12 namespace feature_engagement_tracker { |
| 13 namespace { |
| 14 // Corresponds to a UMA suffix "LevelDBOpenResults" in histograms.xml. |
| 15 // Please do not change. |
| 16 const char kDatabaseUMAName[] = "FeatureEngagementTrackerStore"; |
| 17 |
| 18 using KeyEventPair = std::pair<std::string, Event>; |
| 19 using KeyEventList = std::vector<KeyEventPair>; |
| 20 |
| 21 void NoopUpdateCallback(bool success) {} |
| 22 } // namespace |
| 23 |
| 24 PersistentStore::PersistentStore( |
| 25 const base::FilePath& storage_dir, |
| 26 std::unique_ptr<leveldb_proto::ProtoDatabase<Event>> db) |
| 27 : storage_dir_(storage_dir), |
| 28 db_(std::move(db)), |
| 29 ready_(false), |
| 30 weak_ptr_factory_(this) {} |
| 31 |
| 32 PersistentStore::~PersistentStore() = default; |
| 33 |
| 34 void PersistentStore::Load(const OnLoadedCallback& callback) { |
| 35 DCHECK(!ready_); |
| 36 |
| 37 db_->Init(kDatabaseUMAName, storage_dir_, |
| 38 base::Bind(&PersistentStore::OnInitComplete, |
| 39 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 40 } |
| 41 |
| 42 bool PersistentStore::IsReady() const { |
| 43 return ready_; |
| 44 } |
| 45 |
| 46 void PersistentStore::WriteEvent(const Event& event) { |
| 47 DCHECK(IsReady()); |
| 48 std::unique_ptr<KeyEventList> entries = base::MakeUnique<KeyEventList>(); |
| 49 entries->push_back(KeyEventPair(event.name(), event)); |
| 50 |
| 51 // TODO(dtrainor, nyquist): Consider tracking failures here and storing UMA. |
| 52 db_->UpdateEntries(std::move(entries), |
| 53 base::MakeUnique<std::vector<std::string>>(), |
| 54 base::Bind(&NoopUpdateCallback)); |
| 55 } |
| 56 |
| 57 void PersistentStore::OnInitComplete(const OnLoadedCallback& callback, |
| 58 bool success) { |
| 59 if (!success) { |
| 60 callback.Run(false, base::MakeUnique<std::vector<Event>>()); |
| 61 return; |
| 62 } |
| 63 |
| 64 db_->LoadEntries(base::Bind(&PersistentStore::OnLoadComplete, |
| 65 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 66 } |
| 67 |
| 68 void PersistentStore::OnLoadComplete( |
| 69 const OnLoadedCallback& callback, |
| 70 bool success, |
| 71 std::unique_ptr<std::vector<Event>> entries) { |
| 72 ready_ = success; |
| 73 callback.Run(success, std::move(entries)); |
| 74 } |
| 75 |
| 76 } // namespace feature_engagement_tracker |
OLD | NEW |