summaryrefslogtreecommitdiffstats
path: root/components/power
diff options
context:
space:
mode:
authordhnishi@chromium.org <dhnishi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-08-14 20:25:33 +0000
committerdhnishi@chromium.org <dhnishi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-08-14 20:27:09 +0000
commit3e30590352226fc82855ea6464a4f45f65e2dc64 (patch)
treecbf2e05c81e8e36352c0467aae14399a98449309 /components/power
parentc8fa7cab711ff346fae094131a31209729795726 (diff)
downloadchromium_src-3e30590352226fc82855ea6464a4f45f65e2dc64.zip
chromium_src-3e30590352226fc82855ea6464a4f45f65e2dc64.tar.gz
chromium_src-3e30590352226fc82855ea6464a4f45f65e2dc64.tar.bz2
Initial commit for battery auditing by website origin by profile.
This is part of Website Settings resource/permissions monitoring. Design Doc: https://docs.google.com/document/d/1oQwmj3AU4QYhTyGrYEGr6zaZhHUfx-wqUgEcQGbUU-U/edit?usp=sharing BUG=372607 Review URL: https://codereview.chromium.org/447053002 Cr-Commit-Position: refs/heads/master@{#289689} git-svn-id: svn://svn.chromium.org/chrome/trunk/src@289689 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components/power')
-rw-r--r--components/power/BUILD.gn18
-rw-r--r--components/power/DEPS4
-rw-r--r--components/power/OWNERS3
-rw-r--r--components/power/origin_power_map.cc52
-rw-r--r--components/power/origin_power_map.h51
-rw-r--r--components/power/origin_power_map_factory.cc38
-rw-r--r--components/power/origin_power_map_factory.h35
-rw-r--r--components/power/origin_power_map_unittest.cc68
8 files changed, 269 insertions, 0 deletions
diff --git a/components/power/BUILD.gn b/components/power/BUILD.gn
new file mode 100644
index 0000000..c77545c
--- /dev/null
+++ b/components/power/BUILD.gn
@@ -0,0 +1,18 @@
+# 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.
+
+static_library("power") {
+ sources = [
+ "origin_power_map.cc",
+ "origin_power_map.h",
+ "origin_power_map_factory.cc",
+ "origin_power_map_factory.h",
+ ]
+
+ deps = [
+ "//base",
+ "//components/keyed_service/core:core",
+ "//content/public/common",
+ ]
+}
diff --git a/components/power/DEPS b/components/power/DEPS
new file mode 100644
index 0000000..df2301d5
--- /dev/null
+++ b/components/power/DEPS
@@ -0,0 +1,4 @@
+include_rules = [
+ "+components/keyed_service",
+ "+content/public/common",
+]
diff --git a/components/power/OWNERS b/components/power/OWNERS
new file mode 100644
index 0000000..76a50a4
--- /dev/null
+++ b/components/power/OWNERS
@@ -0,0 +1,3 @@
+derat@chromium.org
+dhnishi@chromium.org
+sivachandra@chromium.org
diff --git a/components/power/origin_power_map.cc b/components/power/origin_power_map.cc
new file mode 100644
index 0000000..dfb0f17
--- /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
diff --git a/components/power/origin_power_map.h b/components/power/origin_power_map.h
new file mode 100644
index 0000000..0936be4
--- /dev/null
+++ b/components/power/origin_power_map.h
@@ -0,0 +1,51 @@
+// 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.
+
+#ifndef COMPONENTS_POWER_ORIGIN_POWER_MAP_H_
+#define COMPONENTS_POWER_ORIGIN_POWER_MAP_H_
+
+#include <map>
+
+#include "components/keyed_service/core/keyed_service.h"
+#include "url/gurl.h"
+
+namespace power {
+
+// Tracks app and website origins and how much power they are consuming while
+// running.
+class OriginPowerMap : public KeyedService {
+ public:
+ typedef std::map<GURL, int> PercentOriginMap;
+
+ OriginPowerMap();
+ virtual ~OriginPowerMap();
+
+ // Returns the integer percentage usage of the total power consumed by a
+ // given URL's origin.
+ int GetPowerForOrigin(const GURL& url);
+
+ // Adds a certain amount of power consumption to a given URL's origin.
+ // |power| is a platform-specific heuristic estimating power consumption.
+ void AddPowerForOrigin(const GURL& url, double power);
+
+ // Returns a map of all origins to the integer percentage usage of power
+ // consumed.
+ PercentOriginMap GetPercentOriginMap();
+
+ private:
+ // OriginMap maps a URL to the amount of power consumed by the URL using the
+ // same units as |total_consumed_|.
+ typedef std::map<GURL, double> OriginMap;
+ OriginMap origin_map_;
+
+ // Total amount of power consumed using units determined by
+ // the power heuristics available to the platform.
+ double total_consumed_;
+
+ DISALLOW_COPY_AND_ASSIGN(OriginPowerMap);
+};
+
+} // namespace power
+
+#endif // COMPONENTS_POWER_ORIGIN_POWER_MAP_H_
diff --git a/components/power/origin_power_map_factory.cc b/components/power/origin_power_map_factory.cc
new file mode 100644
index 0000000..8553aec
--- /dev/null
+++ b/components/power/origin_power_map_factory.cc
@@ -0,0 +1,38 @@
+// 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_factory.h"
+
+#include "base/memory/singleton.h"
+#include "components/keyed_service/content/browser_context_dependency_manager.h"
+#include "components/power/origin_power_map.h"
+
+namespace power {
+// static
+OriginPowerMap* OriginPowerMapFactory::GetForBrowserContext(
+ content::BrowserContext* context) {
+ return static_cast<OriginPowerMap*>(
+ GetInstance()->GetServiceForBrowserContext(context, true));
+}
+
+// static
+OriginPowerMapFactory* OriginPowerMapFactory::GetInstance() {
+ return Singleton<OriginPowerMapFactory>::get();
+}
+
+OriginPowerMapFactory::OriginPowerMapFactory()
+ : BrowserContextKeyedServiceFactory(
+ "OriginPowerMap",
+ BrowserContextDependencyManager::GetInstance()) {
+}
+
+OriginPowerMapFactory::~OriginPowerMapFactory() {
+}
+
+KeyedService* OriginPowerMapFactory::BuildServiceInstanceFor(
+ content::BrowserContext* context) const {
+ return new OriginPowerMap();
+}
+
+} // namespace power
diff --git a/components/power/origin_power_map_factory.h b/components/power/origin_power_map_factory.h
new file mode 100644
index 0000000..c772a2b
--- /dev/null
+++ b/components/power/origin_power_map_factory.h
@@ -0,0 +1,35 @@
+// 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.
+
+#ifndef COMPONENTS_POWER_ORIGIN_POWER_MAP_FACTORY_H_
+#define COMPONENTS_POWER_ORIGIN_POWER_MAP_FACTORY_H_
+
+#include "base/memory/singleton.h"
+#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
+
+namespace power {
+
+class OriginPowerMap;
+
+class OriginPowerMapFactory : public BrowserContextKeyedServiceFactory {
+ public:
+ static OriginPowerMap* GetForBrowserContext(content::BrowserContext* context);
+ static OriginPowerMapFactory* GetInstance();
+
+ private:
+ friend struct DefaultSingletonTraits<OriginPowerMapFactory>;
+
+ OriginPowerMapFactory();
+ virtual ~OriginPowerMapFactory();
+
+ // BrowserContextKeyedServiceFactory:
+ virtual KeyedService* BuildServiceInstanceFor(
+ content::BrowserContext* context) const OVERRIDE;
+
+ DISALLOW_COPY_AND_ASSIGN(OriginPowerMapFactory);
+};
+
+} // namespace power
+
+#endif // COMPONENTS_POWER_ORIGIN_POWER_MAP_FACTORY_H_
diff --git a/components/power/origin_power_map_unittest.cc b/components/power/origin_power_map_unittest.cc
new file mode 100644
index 0000000..0cf2981
--- /dev/null
+++ b/components/power/origin_power_map_unittest.cc
@@ -0,0 +1,68 @@
+// 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 "testing/gtest/include/gtest/gtest.h"
+
+namespace power {
+
+TEST(OriginPowerMapTest, StartEmpty) {
+ OriginPowerMap origin_power_map;
+ EXPECT_EQ(size_t(0), origin_power_map.GetPercentOriginMap().size());
+}
+
+TEST(OriginPowerMapTest, AddOneOriginNotInMap) {
+ OriginPowerMap origin_power_map;
+ GURL url("http://www.google.com");
+ EXPECT_EQ(0, origin_power_map.GetPowerForOrigin(url));
+ origin_power_map.AddPowerForOrigin(url, 10);
+ EXPECT_EQ(size_t(1), origin_power_map.GetPercentOriginMap().size());
+ EXPECT_EQ(100, origin_power_map.GetPowerForOrigin(url));
+}
+
+TEST(OriginPowerMapTest, AddMultiplesOrigins) {
+ OriginPowerMap origin_power_map;
+ GURL url1("http://www.google.com");
+ EXPECT_EQ(0, origin_power_map.GetPowerForOrigin(url1));
+ origin_power_map.AddPowerForOrigin(url1, 10);
+ EXPECT_EQ(size_t(1), origin_power_map.GetPercentOriginMap().size());
+ EXPECT_EQ(100, origin_power_map.GetPowerForOrigin(url1));
+
+ GURL url2("http://www.example.com");
+ origin_power_map.AddPowerForOrigin(url2, 30);
+ EXPECT_EQ(25, origin_power_map.GetPowerForOrigin(url1));
+ EXPECT_EQ(75, origin_power_map.GetPowerForOrigin(url2));
+ origin_power_map.AddPowerForOrigin(url2, 10);
+ EXPECT_EQ(20, origin_power_map.GetPowerForOrigin(url1));
+ EXPECT_EQ(80, origin_power_map.GetPowerForOrigin(url2));
+
+ GURL url3("https://www.google.com");
+ origin_power_map.AddPowerForOrigin(url3, 50);
+ EXPECT_EQ(10, origin_power_map.GetPowerForOrigin(url1));
+ EXPECT_EQ(40, origin_power_map.GetPowerForOrigin(url2));
+ EXPECT_EQ(50, origin_power_map.GetPowerForOrigin(url3));
+}
+
+TEST(OriginPowerMapTest, PercentOriginMap) {
+ OriginPowerMap origin_power_map;
+ GURL url1("http://www.google.com");
+ GURL url2("http://www.example.com");
+ origin_power_map.AddPowerForOrigin(url1, 10);
+ origin_power_map.AddPowerForOrigin(url2, 40);
+ OriginPowerMap::PercentOriginMap origin_map =
+ origin_power_map.GetPercentOriginMap();
+ EXPECT_EQ(20, origin_map.find(url1)->second);
+ EXPECT_EQ(80, origin_map.find(url2)->second);
+}
+
+TEST(OriginPowerMapTest, EmptyPercentOriginMapWhenZeroConsumed) {
+ OriginPowerMap origin_power_map;
+ EXPECT_EQ(size_t(0), origin_power_map.GetPercentOriginMap().size());
+ GURL url("http://www.google.com");
+ origin_power_map.AddPowerForOrigin(url, 0);
+ EXPECT_EQ(size_t(0), origin_power_map.GetPercentOriginMap().size());
+}
+
+} // namespace power