summaryrefslogtreecommitdiffstats
path: root/chromeos/dbus/power_policy_controller.h
blob: 41a58e90e9fd231c66bc22b98cc7fd5532da7a49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2013 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 CHROMEOS_DBUS_POWER_POLICY_CONTROLLER_H_
#define CHROMEOS_DBUS_POWER_POLICY_CONTROLLER_H_

#include <map>
#include <string>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/dbus/dbus_thread_manager_observer.h"
#include "chromeos/dbus/power_manager/policy.pb.h"
#include "chromeos/dbus/power_manager_client.h"

namespace chromeos {

class DBusThreadManager;

// PowerPolicyController is responsible for sending Chrome's assorted power
// management preferences to the Chrome OS power manager.
class CHROMEOS_EXPORT PowerPolicyController
    : public DBusThreadManagerObserver,
      public PowerManagerClient::Observer {
 public:
  // Note: Do not change these values; they are used by preferences.
  enum Action {
    ACTION_SUSPEND      = 0,
    ACTION_STOP_SESSION = 1,
    ACTION_SHUT_DOWN    = 2,
    ACTION_DO_NOTHING   = 3,
  };

  // Values of various power-management-related preferences.
  struct PrefValues {
    PrefValues();

    int ac_screen_dim_delay_ms;
    int ac_screen_off_delay_ms;
    int ac_screen_lock_delay_ms;
    int ac_idle_warning_delay_ms;
    int ac_idle_delay_ms;
    int battery_screen_dim_delay_ms;
    int battery_screen_off_delay_ms;
    int battery_screen_lock_delay_ms;
    int battery_idle_warning_delay_ms;
    int battery_idle_delay_ms;
    Action idle_action;
    Action lid_closed_action;
    bool use_audio_activity;
    bool use_video_activity;
    bool enable_screen_lock;
    double presentation_idle_delay_factor;
    double user_activity_screen_dim_delay_factor;
  };

  // Returns a string describing |policy|.  Useful for tests.
  static std::string GetPolicyDebugString(
      const power_manager::PowerManagementPolicy& policy);

  PowerPolicyController(DBusThreadManager* manager, PowerManagerClient* client);
  virtual ~PowerPolicyController();

  // Updates |prefs_policy_| with |values| and sends an updated policy.
  void ApplyPrefs(const PrefValues& values);

  // Registers a request to temporarily prevent the screen from getting
  // dimmed or turned off or the system from suspending in response to user
  // inactivity and sends an updated policy.  Returns a unique ID that can
  // be passed to RemoveBlock() later.
  int AddScreenBlock(const std::string& reason);
  int AddSuspendBlock(const std::string& reason);

  // Unregisters a request previously created via AddScreenBlock() or
  // AddSuspendBlock() and sends an updated policy.
  void RemoveBlock(int id);

  // DBusThreadManagerObserver implementation:
  virtual void OnDBusThreadManagerDestroying(DBusThreadManager* manager)
      OVERRIDE;

  // PowerManagerClient::Observer implementation:
  virtual void PowerManagerRestarted() OVERRIDE;

 private:
  typedef std::map<int, std::string> BlockMap;

  // Sends a policy based on |prefs_policy_| to the power manager.
  void SendCurrentPolicy();

  // Sends an empty policy to the power manager to reset its configuration.
  void SendEmptyPolicy();

  DBusThreadManager* manager_;  // not owned
  PowerManagerClient* client_;  // not owned

  // Policy derived from values passed to ApplyPrefs().
  power_manager::PowerManagementPolicy prefs_policy_;

  // Was ApplyPrefs() called?
  bool prefs_were_set_;

  // Maps from an ID representing a request to prevent the screen from
  // getting dimmed or turned off or to prevent the system from suspending
  // to the reason for the request.
  BlockMap screen_blocks_;
  BlockMap suspend_blocks_;

  // Next ID to be used by AddScreenBlock() or AddSuspendBlock().
  int next_block_id_;

  DISALLOW_COPY_AND_ASSIGN(PowerPolicyController);
};

}  // namespace chromeos

#endif  // CHROMEOS_DBUS_POWER_POLICY_CONTROLLER_H_