Make |context| passed to queryRequiredElement optional
[chromium-blink-merge.git] / components / power / origin_power_map.cc
blob69d9fbd391f8b8fb34274ead848a571a9b1458b2
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.
5 #include "components/power/origin_power_map.h"
7 #include "base/logging.h"
8 #include "content/public/common/url_constants.h"
9 #include "url/gurl.h"
11 namespace power {
13 OriginPowerMap::OriginPowerMap() : total_consumed_(0.0) {
16 OriginPowerMap::~OriginPowerMap() {
19 int OriginPowerMap::GetPowerForOrigin(const GURL& url) {
20 if (!total_consumed_)
21 return 0;
23 OriginMap::const_iterator it = origin_map_.find(url.GetOrigin());
24 return it == origin_map_.end() ? 0 :
25 static_cast<int>(it->second * 100 / total_consumed_ + 0.5);
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(content::kChromeUIScheme))
32 return;
34 origin_map_[origin] += power;
35 total_consumed_ += power;
38 OriginPowerMap::PercentOriginMap OriginPowerMap::GetPercentOriginMap() {
39 OriginPowerMap::PercentOriginMap percent_map;
41 if (!total_consumed_)
42 return percent_map;
44 for (OriginMap::iterator it = origin_map_.begin(); it != origin_map_.end();
45 ++it) {
46 percent_map[it->first] =
47 static_cast<int>(it->second * 100 / total_consumed_ + 0.5);
49 return percent_map;
52 scoped_ptr<OriginPowerMap::Subscription>
53 OriginPowerMap::AddPowerConsumptionUpdatedCallback(
54 const base::Closure& callback) {
55 return callback_list_.Add(callback);
58 void OriginPowerMap::OnAllOriginsUpdated() {
59 callback_list_.Notify();
62 void OriginPowerMap::ClearOriginMap() {
63 origin_map_.clear();
64 total_consumed_ = 0;
67 } // namespace power