Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: components/power/origin_power_map.cc

Issue 447053002: Add Origin Power Map to Store Battery Auditing Data. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Componentialize and remove dependency in //content. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "components/power/origin_power_map.h"
6
7 #include "base/logging.h"
8 #include "url/gurl.h"
9
10 // Hardcode to avoid dependency on //content.
11 const char kChromeUIScheme[] = "chrome";
Daniel Erat 2014/08/12 15:58:52 duplicating a constant to avoid a dependency feels
Daniel Nishi 2014/08/12 17:33:46 There is precedent to avoid dependencies on //cont
12
13 namespace power {
14
15 OriginPowerMap::OriginPowerMap() : total_consumed_() {
Daniel Erat 2014/08/12 15:58:52 explicitly initialize |total_consumed_| to 0.0
Daniel Nishi 2014/08/12 17:33:46 Done.
16 }
17
18 OriginPowerMap::~OriginPowerMap() {
19 }
20
21 int OriginPowerMap::GetPowerForOrigin(const GURL& url) {
22 GURL origin = url.GetOrigin();
23 if (origin_map_.find(origin) == origin_map_.end() || !total_consumed_)
24 return 0;
25 return origin_map_[origin] * 100 / total_consumed_;
Daniel Erat 2014/08/12 15:58:52 use an iterator so you don't do the lookup twice.
Daniel Nishi 2014/08/12 17:33:46 Done.
26 }
27
28 void OriginPowerMap::AddPowerForOrigin(const GURL& url, double power) {
29 DCHECK_GE(power, 0);
30 GURL origin = url.GetOrigin();
31 if (!origin.is_valid() || origin.SchemeIs(kChromeUIScheme))
32 return;
33
34 origin_map_[origin] += power;
35 total_consumed_ += power;
36 }
37
38 scoped_ptr<OriginPowerMap::PercentOriginMap>
39 OriginPowerMap::GetPercentOriginMap() {
40 scoped_ptr<OriginPowerMap::PercentOriginMap> percent_map(
41 new OriginPowerMap::PercentOriginMap);
42 for (OriginMap::iterator it = origin_map_.begin(); it != origin_map_.end();
43 ++it) {
Daniel Erat 2014/08/12 15:58:52 don't you need to return here if total_consumed_ i
Daniel Nishi 2014/08/12 17:33:46 This is true. If all sources in the map consumed
44 (*percent_map)[it->first] = (it->second / total_consumed_ * 100);
Daniel Erat 2014/08/12 15:58:52 round here too
Daniel Nishi 2014/08/12 17:33:46 Done.
45 }
46 return percent_map.Pass();
47 }
48
49 } // namespace power
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698