- add sources.
[platform/framework/web/crosswalk.git] / src / chromeos / dbus / power_policy_controller.h
1 // Copyright (c) 2013 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 CHROMEOS_DBUS_POWER_POLICY_CONTROLLER_H_
6 #define CHROMEOS_DBUS_POWER_POLICY_CONTROLLER_H_
7
8 #include <map>
9 #include <string>
10
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "chromeos/chromeos_export.h"
14 #include "chromeos/dbus/dbus_thread_manager_observer.h"
15 #include "chromeos/dbus/power_manager/policy.pb.h"
16 #include "chromeos/dbus/power_manager_client.h"
17
18 namespace chromeos {
19
20 class DBusThreadManager;
21
22 // PowerPolicyController is responsible for sending Chrome's assorted power
23 // management preferences to the Chrome OS power manager.
24 class CHROMEOS_EXPORT PowerPolicyController
25     : public DBusThreadManagerObserver,
26       public PowerManagerClient::Observer {
27  public:
28   // Note: Do not change these values; they are used by preferences.
29   enum Action {
30     ACTION_SUSPEND      = 0,
31     ACTION_STOP_SESSION = 1,
32     ACTION_SHUT_DOWN    = 2,
33     ACTION_DO_NOTHING   = 3,
34   };
35
36   // Values of various power-management-related preferences.
37   struct PrefValues {
38     PrefValues();
39
40     int ac_screen_dim_delay_ms;
41     int ac_screen_off_delay_ms;
42     int ac_screen_lock_delay_ms;
43     int ac_idle_warning_delay_ms;
44     int ac_idle_delay_ms;
45     int battery_screen_dim_delay_ms;
46     int battery_screen_off_delay_ms;
47     int battery_screen_lock_delay_ms;
48     int battery_idle_warning_delay_ms;
49     int battery_idle_delay_ms;
50     Action ac_idle_action;
51     Action battery_idle_action;
52     Action lid_closed_action;
53     bool use_audio_activity;
54     bool use_video_activity;
55     bool allow_screen_wake_locks;
56     bool enable_screen_lock;
57     double presentation_screen_dim_delay_factor;
58     double user_activity_screen_dim_delay_factor;
59     bool wait_for_initial_user_activity;
60   };
61
62   // Returns a string describing |policy|.  Useful for tests.
63   static std::string GetPolicyDebugString(
64       const power_manager::PowerManagementPolicy& policy);
65
66   // Delay in milliseconds between the screen being turned off and the
67   // screen being locked. Used if the |enable_screen_lock| pref is set but
68   // |*_screen_lock_delay_ms| are unset or set to higher values than what
69   // this constant would imply.
70   static const int kScreenLockAfterOffDelayMs;
71
72   PowerPolicyController(DBusThreadManager* manager, PowerManagerClient* client);
73   virtual ~PowerPolicyController();
74
75   // Updates |prefs_policy_| with |values| and sends an updated policy.
76   void ApplyPrefs(const PrefValues& values);
77
78   // Resets |prefs_policy_| to its defaults and sends an updated policy.
79   void ClearPrefs();
80
81   // Registers a request to temporarily prevent the screen from getting
82   // dimmed or turned off or the system from suspending in response to user
83   // inactivity and sends an updated policy.  |reason| is a human-readable
84   // description of the reason the lock was created.  Returns a unique ID
85   // that can be passed to RemoveWakeLock() later.
86   int AddScreenWakeLock(const std::string& reason);
87   int AddSystemWakeLock(const std::string& reason);
88
89   // Unregisters a request previously created via AddScreenWakeLock() or
90   // AddSystemWakeLock() and sends an updated policy.
91   void RemoveWakeLock(int id);
92
93   // DBusThreadManagerObserver implementation:
94   virtual void OnDBusThreadManagerDestroying(DBusThreadManager* manager)
95       OVERRIDE;
96
97   // PowerManagerClient::Observer implementation:
98   virtual void PowerManagerRestarted() OVERRIDE;
99
100  private:
101   friend class PowerPrefsTest;
102
103   typedef std::map<int, std::string> WakeLockMap;
104
105   // Sends a policy based on |prefs_policy_| to the power manager.
106   void SendCurrentPolicy();
107
108   // Sends an empty policy to the power manager to reset its configuration.
109   void SendEmptyPolicy();
110
111   DBusThreadManager* manager_;  // not owned
112   PowerManagerClient* client_;  // not owned
113
114   // Policy derived from values passed to ApplyPrefs().
115   power_manager::PowerManagementPolicy prefs_policy_;
116
117   // Was ApplyPrefs() called?
118   bool prefs_were_set_;
119
120   // Maps from an ID representing a request to prevent the screen from
121   // getting dimmed or turned off or to prevent the system from suspending
122   // to the reason for the request.
123   WakeLockMap screen_wake_locks_;
124   WakeLockMap system_wake_locks_;
125
126   // Should entries in |screen_wake_locks_| be honored?  If false, screen
127   // wake locks are just treated as system wake locks instead.
128   bool honor_screen_wake_locks_;
129
130   // Next ID to be used by AddScreenWakeLock() or AddSystemWakeLock().
131   int next_wake_lock_id_;
132
133   DISALLOW_COPY_AND_ASSIGN(PowerPolicyController);
134 };
135
136 }  // namespace chromeos
137
138 #endif  // CHROMEOS_DBUS_POWER_POLICY_CONTROLLER_H_