Chromium Code Reviews| Index: components/power/origin_power_map.cc |
| diff --git a/components/power/origin_power_map.cc b/components/power/origin_power_map.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e356173373a284a7dbf7159e6533fa48d58c4bb3 |
| --- /dev/null |
| +++ b/components/power/origin_power_map.cc |
| @@ -0,0 +1,49 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/power/origin_power_map.h" |
| + |
| +#include "base/logging.h" |
| +#include "url/gurl.h" |
| + |
| +// Hardcode to avoid dependency on //content. |
| +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
|
| + |
| +namespace power { |
| + |
| +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.
|
| +} |
| + |
| +OriginPowerMap::~OriginPowerMap() { |
| +} |
| + |
| +int OriginPowerMap::GetPowerForOrigin(const GURL& url) { |
| + GURL origin = url.GetOrigin(); |
| + if (origin_map_.find(origin) == origin_map_.end() || !total_consumed_) |
| + return 0; |
| + 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.
|
| +} |
| + |
| +void OriginPowerMap::AddPowerForOrigin(const GURL& url, double power) { |
| + DCHECK_GE(power, 0); |
| + GURL origin = url.GetOrigin(); |
| + if (!origin.is_valid() || origin.SchemeIs(kChromeUIScheme)) |
| + return; |
| + |
| + origin_map_[origin] += power; |
| + total_consumed_ += power; |
| +} |
| + |
| +scoped_ptr<OriginPowerMap::PercentOriginMap> |
| +OriginPowerMap::GetPercentOriginMap() { |
| + scoped_ptr<OriginPowerMap::PercentOriginMap> percent_map( |
| + new OriginPowerMap::PercentOriginMap); |
| + for (OriginMap::iterator it = origin_map_.begin(); it != origin_map_.end(); |
| + ++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
|
| + (*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.
|
| + } |
| + return percent_map.Pass(); |
| +} |
| + |
| +} // namespace power |