• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef COMPONENTS_POWER_ORIGIN_POWER_MAP_H_
6 #define COMPONENTS_POWER_ORIGIN_POWER_MAP_H_
7 
8 #include <map>
9 
10 #include "base/callback_list.h"
11 #include "components/keyed_service/core/keyed_service.h"
12 #include "url/gurl.h"
13 
14 namespace power {
15 
16 // Tracks app and website origins and how much power they are consuming while
17 // running.
18 class OriginPowerMap : public KeyedService {
19  public:
20   typedef std::map<GURL, int> PercentOriginMap;
21   typedef base::CallbackList<void(void)>::Subscription Subscription;
22 
23   OriginPowerMap();
24   virtual ~OriginPowerMap();
25 
26   // Returns the integer percentage usage of the total power consumed by a
27   // given URL's origin.
28   int GetPowerForOrigin(const GURL& url);
29 
30   // Adds a certain amount of power consumption to a given URL's origin.
31   // |power| is a platform-specific heuristic estimating power consumption.
32   void AddPowerForOrigin(const GURL& url, double power);
33 
34   // Returns a map of all origins to the integer percentage usage of power
35   // consumed.
36   PercentOriginMap GetPercentOriginMap();
37 
38   // Adds a callback for the completion of a round of updates to |origin_map_|.
39   scoped_ptr<Subscription> AddPowerConsumptionUpdatedCallback(
40       const base::Closure& callback);
41 
42   // Notifies observers to let them know that the origin power map has finished
43   // updating for all origins this cycle.
44   void OnAllOriginsUpdated();
45 
46   // Clears all URLs out of the map.
47   void ClearOriginMap();
48 
49  private:
50   // OriginMap maps a URL to the amount of power consumed by the URL using the
51   // same units as |total_consumed_|.
52   typedef std::map<GURL, double> OriginMap;
53   OriginMap origin_map_;
54 
55   // Total amount of power consumed using units determined by
56   // the power heuristics available to the platform.
57   double total_consumed_;
58 
59   base::CallbackList<void(void)> callback_list_;
60 
61   DISALLOW_COPY_AND_ASSIGN(OriginPowerMap);
62 };
63 
64 }  // namespace power
65 
66 #endif  // COMPONENTS_POWER_ORIGIN_POWER_MAP_H_
67