Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 "chrome/browser/power/origin_power_map.h" | |
| 6 | |
| 7 #include "base/memory/ref_counted.h" | |
| 8 #include "base/prefs/pref_service.h" | |
| 9 #include "base/prefs/scoped_user_pref_update.h" | |
| 10 #include "chrome/common/pref_names.h" | |
| 11 #include "components/pref_registry/pref_registry_syncable.h" | |
| 12 #include "content/public/common/url_constants.h" | |
| 13 #include "url/gurl.h" | |
| 14 | |
| 15 OriginPowerMap::OriginPowerMap(PrefService* prefs) | |
| 16 : total_consumed_(0), prefs_(prefs) { | |
| 17 DictionaryPrefUpdate update(prefs_, prefs::kBatteryOriginMap); | |
|
sky
2014/08/06 23:59:22
Is kBatteryOriginMap effectively going to become e
Daniel Nishi
2014/08/07 21:15:08
Would replacing this with a SQLite Database be an
sky
2014/08/07 21:47:07
Why do you need to make this data persistent at al
Daniel Nishi
2014/08/07 22:43:11
Ah, I should have specified -- it is intended to b
| |
| 18 base::DictionaryValue* old_values = update.Get(); | |
| 19 for (base::DictionaryValue::Iterator i(*old_values); !i.IsAtEnd(); | |
| 20 i.Advance()) { | |
| 21 double value = 0.0; | |
| 22 bool rv = i.value().GetAsDouble(&value); | |
| 23 | |
| 24 if (!rv) | |
| 25 continue; | |
| 26 | |
| 27 origin_map_[GURL(i.key())] = value; | |
| 28 total_consumed_ += value; | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 OriginPowerMap::~OriginPowerMap() { | |
| 33 } | |
| 34 | |
| 35 int OriginPowerMap::GetPowerForOrigin(const GURL& origin) { | |
| 36 if (origin_map_.find(origin) == origin_map_.end()) | |
| 37 return -1; | |
| 38 else if (!total_consumed_) | |
| 39 return 0; | |
| 40 return origin_map_[origin] * 100 / total_consumed_; | |
| 41 } | |
| 42 | |
| 43 void OriginPowerMap::AddPowerForOrigin(const GURL& origin, double power) { | |
| 44 if (!origin.is_valid() || origin.SchemeIs(content::kChromeUIScheme)) | |
| 45 return; | |
| 46 | |
| 47 if (origin_map_.find(origin) == origin_map_.end()) { | |
| 48 origin_map_[origin] = 0; | |
| 49 } | |
| 50 origin_map_[origin] += power; | |
| 51 total_consumed_ += power; | |
| 52 } | |
| 53 | |
| 54 scoped_ptr<OriginPowerMap::ScaledOriginMap> | |
| 55 OriginPowerMap::GetScaledOriginMap() { | |
| 56 scoped_ptr<OriginPowerMap::ScaledOriginMap> percent_map( | |
| 57 new OriginPowerMap::ScaledOriginMap); | |
| 58 for (OriginMap::iterator it = origin_map_.begin(); it != origin_map_.end(); | |
| 59 ++it) { | |
| 60 (*percent_map)[it->first] = (it->second / total_consumed_ * 100); | |
| 61 } | |
| 62 return percent_map.Pass(); | |
| 63 } | |
| 64 | |
| 65 void OriginPowerMap::SavePrefs() { | |
| 66 DictionaryPrefUpdate update(prefs_, prefs::kBatteryOriginMap); | |
| 67 base::DictionaryValue* origin_entries = update.Get(); | |
| 68 DCHECK(origin_entries); | |
| 69 | |
| 70 for (OriginMap::iterator it = origin_map_.begin(); it != origin_map_.end(); | |
| 71 ++it) { | |
| 72 GURL origin = it->first; | |
| 73 origin_entries->SetDoubleWithoutPathExpansion(origin.spec(), | |
| 74 origin_map_[origin]); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 void OriginPowerMap::RegisterProfilePrefs( | |
| 79 user_prefs::PrefRegistrySyncable* registry) { | |
| 80 registry->RegisterDictionaryPref( | |
| 81 prefs::kBatteryOriginMap, | |
| 82 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
| 83 } | |
| OLD | NEW |