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

Unified 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: rebase. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/power/origin_power_map.h ('k') | components/power/origin_power_map_factory.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..dfb0f172a93d510d5d013c9ae39b47717f731f71
--- /dev/null
+++ b/components/power/origin_power_map.cc
@@ -0,0 +1,52 @@
+// 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 "content/public/common/url_constants.h"
+#include "url/gurl.h"
+
+namespace power {
+
+OriginPowerMap::OriginPowerMap() : total_consumed_(0.0) {
+}
+
+OriginPowerMap::~OriginPowerMap() {
+}
+
+int OriginPowerMap::GetPowerForOrigin(const GURL& url) {
+ if (!total_consumed_)
+ return 0;
+
+ OriginMap::const_iterator it = origin_map_.find(url.GetOrigin());
+ return it == origin_map_.end() ? 0 :
+ static_cast<int>(it->second * 100 / total_consumed_ + 0.5);
+}
+
+void OriginPowerMap::AddPowerForOrigin(const GURL& url, double power) {
+ DCHECK_GE(power, 0);
+ GURL origin = url.GetOrigin();
+ if (!origin.is_valid() || origin.SchemeIs(content::kChromeUIScheme))
+ return;
+
+ origin_map_[origin] += power;
+ total_consumed_ += power;
+}
+
+OriginPowerMap::PercentOriginMap OriginPowerMap::GetPercentOriginMap() {
+ OriginPowerMap::PercentOriginMap percent_map;
+
+ if (!total_consumed_)
+ return percent_map;
+
+ for (OriginMap::iterator it = origin_map_.begin(); it != origin_map_.end();
+ ++it) {
+ percent_map[it->first] =
+ static_cast<int>(it->second * 100 / total_consumed_ + 0.5);
+ }
+ return percent_map;
+}
+
+} // namespace power
« no previous file with comments | « components/power/origin_power_map.h ('k') | components/power/origin_power_map_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698