Home | History | Annotate | Download | only in dbus
      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/power_manager/policy.pb.h"
     15 #include "chromeos/dbus/power_manager_client.h"
     16 
     17 namespace chromeos {
     18 
     19 class DBusThreadManager;
     20 
     21 // PowerPolicyController is responsible for sending Chrome's assorted power
     22 // management preferences to the Chrome OS power manager.
     23 class CHROMEOS_EXPORT PowerPolicyController
     24     : public PowerManagerClient::Observer {
     25  public:
     26   // Note: Do not change these values; they are used by preferences.
     27   enum Action {
     28     ACTION_SUSPEND      = 0,
     29     ACTION_STOP_SESSION = 1,
     30     ACTION_SHUT_DOWN    = 2,
     31     ACTION_DO_NOTHING   = 3,
     32   };
     33 
     34   // Values of various power-management-related preferences.
     35   struct PrefValues {
     36     PrefValues();
     37 
     38     int ac_screen_dim_delay_ms;
     39     int ac_screen_off_delay_ms;
     40     int ac_screen_lock_delay_ms;
     41     int ac_idle_warning_delay_ms;
     42     int ac_idle_delay_ms;
     43     int battery_screen_dim_delay_ms;
     44     int battery_screen_off_delay_ms;
     45     int battery_screen_lock_delay_ms;
     46     int battery_idle_warning_delay_ms;
     47     int battery_idle_delay_ms;
     48     Action ac_idle_action;
     49     Action battery_idle_action;
     50     Action lid_closed_action;
     51     bool use_audio_activity;
     52     bool use_video_activity;
     53     double ac_brightness_percent;
     54     double battery_brightness_percent;
     55     bool allow_screen_wake_locks;
     56     bool enable_auto_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 screen
     67   // being locked. Used if the |enable_auto_screen_lock| pref is set but
     68   // |*_screen_lock_delay_ms| are unset or set to higher values than what this
     69   // constant would imply.
     70   static const int kScreenLockAfterOffDelayMs;
     71 
     72   PowerPolicyController();
     73   virtual ~PowerPolicyController();
     74 
     75   void Init(DBusThreadManager* manager);
     76 
     77   // Updates |prefs_policy_| with |values| and sends an updated policy.
     78   void ApplyPrefs(const PrefValues& values);
     79 
     80   // Registers a request to temporarily prevent the screen from getting
     81   // dimmed or turned off or the system from suspending in response to user
     82   // inactivity and sends an updated policy.  |reason| is a human-readable
     83   // description of the reason the lock was created.  Returns a unique ID
     84   // that can be passed to RemoveWakeLock() later.
     85   int AddScreenWakeLock(const std::string& reason);
     86   int AddSystemWakeLock(const std::string& reason);
     87 
     88   // Unregisters a request previously created via AddScreenWakeLock() or
     89   // AddSystemWakeLock() and sends an updated policy.
     90   void RemoveWakeLock(int id);
     91 
     92   // PowerManagerClient::Observer implementation:
     93   virtual void PowerManagerRestarted() OVERRIDE;
     94 
     95  private:
     96   friend class PowerPrefsTest;
     97 
     98   typedef std::map<int, std::string> WakeLockMap;
     99 
    100   // Sends a policy based on |prefs_policy_| to the power manager.
    101   void SendCurrentPolicy();
    102 
    103   PowerManagerClient* client_;  // weak
    104 
    105   // Policy derived from values passed to ApplyPrefs().
    106   power_manager::PowerManagementPolicy prefs_policy_;
    107 
    108   // Was ApplyPrefs() called?
    109   bool prefs_were_set_;
    110 
    111   // Maps from an ID representing a request to prevent the screen from
    112   // getting dimmed or turned off or to prevent the system from suspending
    113   // to the reason for the request.
    114   WakeLockMap screen_wake_locks_;
    115   WakeLockMap system_wake_locks_;
    116 
    117   // Should entries in |screen_wake_locks_| be honored?  If false, screen
    118   // wake locks are just treated as system wake locks instead.
    119   bool honor_screen_wake_locks_;
    120 
    121   // Next ID to be used by AddScreenWakeLock() or AddSystemWakeLock().
    122   int next_wake_lock_id_;
    123 
    124   DISALLOW_COPY_AND_ASSIGN(PowerPolicyController);
    125 };
    126 
    127 }  // namespace chromeos
    128 
    129 #endif  // CHROMEOS_DBUS_POWER_POLICY_CONTROLLER_H_
    130