| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 #include "components/feature_engagement_tracker/internal/availability_store.h" | 5 #include "components/feature_engagement_tracker/internal/availability_store.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/feature_list.h" | 14 #include "base/feature_list.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "components/feature_engagement_tracker/internal/proto/availability.pb.h
" | 16 #include "components/feature_engagement_tracker/internal/proto/availability.pb.h
" |
| 17 #include "components/feature_engagement_tracker/internal/stats.h" |
| 17 #include "components/feature_engagement_tracker/public/feature_list.h" | 18 #include "components/feature_engagement_tracker/public/feature_list.h" |
| 18 #include "components/leveldb_proto/proto_database.h" | 19 #include "components/leveldb_proto/proto_database.h" |
| 19 | 20 |
| 20 namespace feature_engagement_tracker { | 21 namespace feature_engagement_tracker { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 using KeyAvailabilityPair = std::pair<std::string, Availability>; | 25 using KeyAvailabilityPair = std::pair<std::string, Availability>; |
| 25 using KeyAvailabilityList = std::vector<KeyAvailabilityPair>; | 26 using KeyAvailabilityList = std::vector<KeyAvailabilityPair>; |
| 26 | 27 |
| 27 // Corresponds to a UMA suffix "LevelDBOpenResults" in histograms.xml. | 28 // Corresponds to a UMA suffix "LevelDBOpenResults" in histograms.xml. |
| 28 // Please do not change. | 29 // Please do not change. |
| 29 const char kDatabaseUMAName[] = "FeatureEngagementTrackerAvailabilityStore"; | 30 const char kDatabaseUMAName[] = "FeatureEngagementTrackerAvailabilityStore"; |
| 30 | 31 |
| 31 void OnDBUpdateComplete( | 32 void OnDBUpdateComplete( |
| 32 std::unique_ptr<leveldb_proto::ProtoDatabase<Availability>> db, | 33 std::unique_ptr<leveldb_proto::ProtoDatabase<Availability>> db, |
| 33 AvailabilityStore::OnLoadedCallback on_loaded_callback, | 34 AvailabilityStore::OnLoadedCallback on_loaded_callback, |
| 34 std::unique_ptr<std::map<const base::Feature*, uint32_t>> | 35 std::unique_ptr<std::map<const base::Feature*, uint32_t>> |
| 35 feature_availabilities, | 36 feature_availabilities, |
| 36 bool success) { | 37 bool success) { |
| 38 stats::RecordDbUpdate(success, stats::StoreType::AVAILABILITY_STORE); |
| 37 std::move(on_loaded_callback).Run(success, std::move(feature_availabilities)); | 39 std::move(on_loaded_callback).Run(success, std::move(feature_availabilities)); |
| 38 } | 40 } |
| 39 | 41 |
| 40 void OnDBLoadComplete( | 42 void OnDBLoadComplete( |
| 41 std::unique_ptr<leveldb_proto::ProtoDatabase<Availability>> db, | 43 std::unique_ptr<leveldb_proto::ProtoDatabase<Availability>> db, |
| 42 FeatureVector feature_filter, | 44 FeatureVector feature_filter, |
| 43 AvailabilityStore::OnLoadedCallback on_loaded_callback, | 45 AvailabilityStore::OnLoadedCallback on_loaded_callback, |
| 44 uint32_t current_day, | 46 uint32_t current_day, |
| 45 bool success, | 47 bool success, |
| 46 std::unique_ptr<std::vector<Availability>> availabilities) { | 48 std::unique_ptr<std::vector<Availability>> availabilities) { |
| 49 stats::RecordAvailabilityDbLoadEvent(success); |
| 47 if (!success) { | 50 if (!success) { |
| 48 std::move(on_loaded_callback) | 51 std::move(on_loaded_callback) |
| 49 .Run(false, | 52 .Run(false, |
| 50 base::MakeUnique<std::map<const base::Feature*, uint32_t>>()); | 53 base::MakeUnique<std::map<const base::Feature*, uint32_t>>()); |
| 51 return; | 54 return; |
| 52 } | 55 } |
| 53 | 56 |
| 54 // Create map from feature name to Feature. | 57 // Create map from feature name to Feature. |
| 55 std::map<std::string, const base::Feature*> feature_mapping; | 58 std::map<std::string, const base::Feature*> feature_mapping; |
| 56 for (const base::Feature* feature : feature_filter) { | 59 for (const base::Feature* feature : feature_filter) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 std::move(on_loaded_callback), | 112 std::move(on_loaded_callback), |
| 110 std::move(feature_availabilities))); | 113 std::move(feature_availabilities))); |
| 111 } | 114 } |
| 112 | 115 |
| 113 void OnDBInitComplete( | 116 void OnDBInitComplete( |
| 114 std::unique_ptr<leveldb_proto::ProtoDatabase<Availability>> db, | 117 std::unique_ptr<leveldb_proto::ProtoDatabase<Availability>> db, |
| 115 FeatureVector feature_filter, | 118 FeatureVector feature_filter, |
| 116 AvailabilityStore::OnLoadedCallback on_loaded_callback, | 119 AvailabilityStore::OnLoadedCallback on_loaded_callback, |
| 117 uint32_t current_day, | 120 uint32_t current_day, |
| 118 bool success) { | 121 bool success) { |
| 122 stats::RecordDbInitEvent(success, stats::StoreType::AVAILABILITY_STORE); |
| 123 |
| 119 if (!success) { | 124 if (!success) { |
| 120 std::move(on_loaded_callback) | 125 std::move(on_loaded_callback) |
| 121 .Run(false, | 126 .Run(false, |
| 122 base::MakeUnique<std::map<const base::Feature*, uint32_t>>()); | 127 base::MakeUnique<std::map<const base::Feature*, uint32_t>>()); |
| 123 return; | 128 return; |
| 124 } | 129 } |
| 125 | 130 |
| 126 db->LoadEntries(base::BindOnce(&OnDBLoadComplete, std::move(db), | 131 db->LoadEntries(base::BindOnce(&OnDBLoadComplete, std::move(db), |
| 127 std::move(feature_filter), | 132 std::move(feature_filter), |
| 128 std::move(on_loaded_callback), current_day)); | 133 std::move(on_loaded_callback), current_day)); |
| 129 } | 134 } |
| 130 | 135 |
| 131 } // namespace | 136 } // namespace |
| 132 | 137 |
| 133 // static | 138 // static |
| 134 void AvailabilityStore::LoadAndUpdateStore( | 139 void AvailabilityStore::LoadAndUpdateStore( |
| 135 const base::FilePath& storage_dir, | 140 const base::FilePath& storage_dir, |
| 136 std::unique_ptr<leveldb_proto::ProtoDatabase<Availability>> db, | 141 std::unique_ptr<leveldb_proto::ProtoDatabase<Availability>> db, |
| 137 FeatureVector feature_filter, | 142 FeatureVector feature_filter, |
| 138 AvailabilityStore::OnLoadedCallback on_loaded_callback, | 143 AvailabilityStore::OnLoadedCallback on_loaded_callback, |
| 139 uint32_t current_day) { | 144 uint32_t current_day) { |
| 140 db->Init(kDatabaseUMAName, storage_dir, | 145 db->Init(kDatabaseUMAName, storage_dir, |
| 141 base::BindOnce(&OnDBInitComplete, std::move(db), | 146 base::BindOnce(&OnDBInitComplete, std::move(db), |
| 142 std::move(feature_filter), | 147 std::move(feature_filter), |
| 143 std::move(on_loaded_callback), current_day)); | 148 std::move(on_loaded_callback), current_day)); |
| 144 } | 149 } |
| 145 | 150 |
| 146 } // namespace feature_engagement_tracker | 151 } // namespace feature_engagement_tracker |
| OLD | NEW |