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"
13 OriginPowerMap::OriginPowerMap() : total_consumed_(0.0) {
16 OriginPowerMap::~OriginPowerMap() {
19 int OriginPowerMap::GetPowerForOrigin(const GURL
& url
) {
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
) {
30 GURL origin
= url
.GetOrigin();
31 if (!origin
.is_valid() || origin
.SchemeIs(content::kChromeUIScheme
))
34 origin_map_
[origin
] += power
;
35 total_consumed_
+= power
;
38 OriginPowerMap::PercentOriginMap
OriginPowerMap::GetPercentOriginMap() {
39 OriginPowerMap::PercentOriginMap percent_map
;
44 for (OriginMap::iterator it
= origin_map_
.begin(); it
!= origin_map_
.end();
46 percent_map
[it
->first
] =
47 static_cast<int>(it
->second
* 100 / total_consumed_
+ 0.5);
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() {