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 "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 | |
OLD | NEW |