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