summaryrefslogtreecommitdiffstats
path: root/chromeos
diff options
context:
space:
mode:
authorderat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-02-06 22:33:08 +0000
committerderat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-02-06 22:33:08 +0000
commitf53977445e51f7c2e62539891fbf55d9b6989fa7 (patch)
treef811a63c27b3b2afff50701fdbf3a0d897fc4a09 /chromeos
parent0f03b3f0436d91cd1343dd31abea739e45da0bcd (diff)
downloadchromium_src-f53977445e51f7c2e62539891fbf55d9b6989fa7.zip
chromium_src-f53977445e51f7c2e62539891fbf55d9b6989fa7.tar.gz
chromium_src-f53977445e51f7c2e62539891fbf55d9b6989fa7.tar.bz2
chromeos: Add power management policy prefs.
This adds various prefs related to power management. A new PowerPolicyController class is responsible for sending them to powerd. BUG=173849,chromium-os:38281 Review URL: https://codereview.chromium.org/12186010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@181070 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chromeos')
-rw-r--r--chromeos/chromeos.gyp4
-rw-r--r--chromeos/dbus/dbus_thread_manager.cc17
-rw-r--r--chromeos/dbus/dbus_thread_manager.h2
-rw-r--r--chromeos/dbus/mock_dbus_thread_manager.cc7
-rw-r--r--chromeos/dbus/mock_dbus_thread_manager.h9
-rw-r--r--chromeos/dbus/mock_dbus_thread_manager_without_gmock.cc6
-rw-r--r--chromeos/dbus/mock_dbus_thread_manager_without_gmock.h1
-rw-r--r--chromeos/dbus/mock_power_manager_client.cc1
-rw-r--r--chromeos/dbus/mock_power_manager_client.h1
-rw-r--r--chromeos/dbus/power_manager_client.cc20
-rw-r--r--chromeos/dbus/power_manager_client.h8
-rw-r--r--chromeos/dbus/power_policy_controller.cc178
-rw-r--r--chromeos/dbus/power_policy_controller.h79
13 files changed, 328 insertions, 5 deletions
diff --git a/chromeos/chromeos.gyp b/chromeos/chromeos.gyp
index ded0c5d..9d35c08 100644
--- a/chromeos/chromeos.gyp
+++ b/chromeos/chromeos.gyp
@@ -111,6 +111,8 @@
'dbus/permission_broker_client.h',
'dbus/power_manager_client.cc',
'dbus/power_manager_client.h',
+ 'dbus/power_policy_controller.cc',
+ 'dbus/power_policy_controller.h',
'dbus/power_supply_status.cc',
'dbus/power_supply_status.h',
'dbus/session_manager_client.cc',
@@ -202,6 +204,7 @@
'../build/linux/system.gyp:dbus',
'../testing/gmock.gyp:gmock',
'chromeos',
+ 'power_manager_proto',
],
'sources': [
'attestation/mock_attestation_flow.cc',
@@ -373,6 +376,7 @@
'type': 'static_library',
'sources': [
'../third_party/cros_system_api/dbus/power_manager/input_event.proto',
+ '../third_party/cros_system_api/dbus/power_manager/policy.proto',
'../third_party/cros_system_api/dbus/power_manager/suspend.proto',
],
'variables': {
diff --git a/chromeos/dbus/dbus_thread_manager.cc b/chromeos/dbus/dbus_thread_manager.cc
index 0b0c5d1..25e1fef 100644
--- a/chromeos/dbus/dbus_thread_manager.cc
+++ b/chromeos/dbus/dbus_thread_manager.cc
@@ -39,6 +39,7 @@
#include "chromeos/dbus/modem_messaging_client.h"
#include "chromeos/dbus/permission_broker_client.h"
#include "chromeos/dbus/power_manager_client.h"
+#include "chromeos/dbus/power_policy_controller.h"
#include "chromeos/dbus/session_manager_client.h"
#include "chromeos/dbus/sms_client.h"
#include "chromeos/dbus/speech_synthesizer_client.h"
@@ -124,6 +125,11 @@ class DBusThreadManagerImpl : public DBusThreadManager {
SpeechSynthesizerClient::Create(client_type, system_bus_.get()));
update_engine_client_.reset(
UpdateEngineClient::Create(client_type, system_bus_.get()));
+
+ // PowerPolicyController is dependent on PowerManagerClient, so
+ // initialize it after the main list of clients.
+ power_policy_controller_.reset(
+ new PowerPolicyController(this, power_manager_client_.get()));
}
virtual ~DBusThreadManagerImpl() {
@@ -281,6 +287,10 @@ class DBusThreadManagerImpl : public DBusThreadManager {
return power_manager_client_.get();
}
+ virtual PowerPolicyController* GetPowerPolicyController() OVERRIDE {
+ return power_policy_controller_.get();
+ }
+
virtual SessionManagerClient* GetSessionManagerClient() OVERRIDE {
return session_manager_client_.get();
}
@@ -342,6 +352,10 @@ class DBusThreadManagerImpl : public DBusThreadManager {
return ibus_panel_service_.get();
}
+ // Note: Keep this before other members so they can call AddObserver() in
+ // their c'tors.
+ ObserverList<DBusThreadManagerObserver> observers_;
+
scoped_ptr<base::Thread> dbus_thread_;
scoped_refptr<dbus::Bus> system_bus_;
scoped_refptr<dbus::Bus> ibus_bus_;
@@ -375,10 +389,9 @@ class DBusThreadManagerImpl : public DBusThreadManager {
scoped_ptr<IBusEngineFactoryService> ibus_engine_factory_service_;
std::map<dbus::ObjectPath, IBusEngineService*> ibus_engine_services_;
scoped_ptr<IBusPanelService> ibus_panel_service_;
+ scoped_ptr<PowerPolicyController> power_policy_controller_;
std::string ibus_address_;
-
- ObserverList<DBusThreadManagerObserver> observers_;
};
// static
diff --git a/chromeos/dbus/dbus_thread_manager.h b/chromeos/dbus/dbus_thread_manager.h
index f163123..cbdd610 100644
--- a/chromeos/dbus/dbus_thread_manager.h
+++ b/chromeos/dbus/dbus_thread_manager.h
@@ -46,6 +46,7 @@ class IntrospectableClient;
class ModemMessagingClient;
class PermissionBrokerClient;
class PowerManagerClient;
+class PowerPolicyController;
class SMSClient;
class SessionManagerClient;
class ShillDeviceClient;
@@ -137,6 +138,7 @@ class CHROMEOS_EXPORT DBusThreadManager {
virtual ModemMessagingClient* GetModemMessagingClient() = 0;
virtual PermissionBrokerClient* GetPermissionBrokerClient() = 0;
virtual PowerManagerClient* GetPowerManagerClient() = 0;
+ virtual PowerPolicyController* GetPowerPolicyController() = 0;
virtual SessionManagerClient* GetSessionManagerClient() = 0;
virtual ShillDeviceClient* GetShillDeviceClient() = 0;
virtual ShillIPConfigClient* GetShillIPConfigClient() = 0;
diff --git a/chromeos/dbus/mock_dbus_thread_manager.cc b/chromeos/dbus/mock_dbus_thread_manager.cc
index 627f6e7..2d5b774 100644
--- a/chromeos/dbus/mock_dbus_thread_manager.cc
+++ b/chromeos/dbus/mock_dbus_thread_manager.cc
@@ -29,6 +29,7 @@
#include "chromeos/dbus/mock_sms_client.h"
#include "chromeos/dbus/mock_speech_synthesizer_client.h"
#include "chromeos/dbus/mock_update_engine_client.h"
+#include "chromeos/dbus/power_policy_controller.h"
using ::testing::AnyNumber;
using ::testing::Return;
@@ -61,7 +62,9 @@ MockDBusThreadManager::MockDBusThreadManager()
mock_session_manager_client_(new MockSessionManagerClient),
mock_sms_client_(new MockSMSClient),
mock_speech_synthesizer_client_(new MockSpeechSynthesizerClient),
- mock_update_engine_client_(new MockUpdateEngineClient) {
+ mock_update_engine_client_(new MockUpdateEngineClient),
+ power_policy_controller_(ALLOW_THIS_IN_INITIALIZER_LIST(
+ new PowerPolicyController(this, mock_power_manager_client_.get()))) {
EXPECT_CALL(*this, GetBluetoothAdapterClient())
.WillRepeatedly(Return(mock_bluetooth_adapter_client_.get()));
EXPECT_CALL(*this, GetBluetoothDeviceClient())
@@ -100,6 +103,8 @@ MockDBusThreadManager::MockDBusThreadManager()
.WillRepeatedly(Return(mock_modem_messaging_client()));
EXPECT_CALL(*this, GetPowerManagerClient())
.WillRepeatedly(Return(mock_power_manager_client_.get()));
+ EXPECT_CALL(*this, GetPowerPolicyController())
+ .WillRepeatedly(Return(power_policy_controller_.get()));
EXPECT_CALL(*this, GetSessionManagerClient())
.WillRepeatedly(Return(mock_session_manager_client_.get()));
EXPECT_CALL(*this, GetSMSClient())
diff --git a/chromeos/dbus/mock_dbus_thread_manager.h b/chromeos/dbus/mock_dbus_thread_manager.h
index 6102cbd..55d853b 100644
--- a/chromeos/dbus/mock_dbus_thread_manager.h
+++ b/chromeos/dbus/mock_dbus_thread_manager.h
@@ -44,6 +44,7 @@ class MockSessionManagerClient;
class MockSMSClient;
class MockSpeechSynthesizerClient;
class MockUpdateEngineClient;
+class PowerPolicyController;
// This class provides a mock DBusThreadManager with mock clients
// installed. You can customize the behaviors of mock clients with
@@ -78,6 +79,7 @@ class MockDBusThreadManager : public DBusThreadManager {
MOCK_METHOD0(GetModemMessagingClient, ModemMessagingClient*(void));
MOCK_METHOD0(GetPermissionBrokerClient, PermissionBrokerClient*(void));
MOCK_METHOD0(GetPowerManagerClient, PowerManagerClient*(void));
+ MOCK_METHOD0(GetPowerPolicyController, PowerPolicyController*(void));
MOCK_METHOD0(GetSessionManagerClient, SessionManagerClient*(void));
MOCK_METHOD0(GetSMSClient, SMSClient*(void));
MOCK_METHOD0(GetSpeechSynthesizerClient, SpeechSynthesizerClient*(void));
@@ -166,6 +168,10 @@ class MockDBusThreadManager : public DBusThreadManager {
}
private:
+ // Note: Keep this before other members so they can call AddObserver() in
+ // their c'tors.
+ ObserverList<DBusThreadManagerObserver> observers_;
+
scoped_ptr<MockBluetoothAdapterClient> mock_bluetooth_adapter_client_;
scoped_ptr<MockBluetoothDeviceClient> mock_bluetooth_device_client_;
scoped_ptr<MockBluetoothInputClient> mock_bluetooth_input_client_;
@@ -190,8 +196,7 @@ class MockDBusThreadManager : public DBusThreadManager {
scoped_ptr<MockSMSClient> mock_sms_client_;
scoped_ptr<MockSpeechSynthesizerClient> mock_speech_synthesizer_client_;
scoped_ptr<MockUpdateEngineClient> mock_update_engine_client_;
-
- ObserverList<DBusThreadManagerObserver> observers_;
+ scoped_ptr<PowerPolicyController> power_policy_controller_;
DISALLOW_COPY_AND_ASSIGN(MockDBusThreadManager);
};
diff --git a/chromeos/dbus/mock_dbus_thread_manager_without_gmock.cc b/chromeos/dbus/mock_dbus_thread_manager_without_gmock.cc
index f79ffae..0635d89 100644
--- a/chromeos/dbus/mock_dbus_thread_manager_without_gmock.cc
+++ b/chromeos/dbus/mock_dbus_thread_manager_without_gmock.cc
@@ -159,6 +159,12 @@ PowerManagerClient* MockDBusThreadManagerWithoutGMock::GetPowerManagerClient() {
return NULL;
}
+PowerPolicyController*
+MockDBusThreadManagerWithoutGMock::GetPowerPolicyController() {
+ NOTIMPLEMENTED();
+ return NULL;
+}
+
SessionManagerClient*
MockDBusThreadManagerWithoutGMock::GetSessionManagerClient() {
NOTIMPLEMENTED();
diff --git a/chromeos/dbus/mock_dbus_thread_manager_without_gmock.h b/chromeos/dbus/mock_dbus_thread_manager_without_gmock.h
index b40b184..0f431f6 100644
--- a/chromeos/dbus/mock_dbus_thread_manager_without_gmock.h
+++ b/chromeos/dbus/mock_dbus_thread_manager_without_gmock.h
@@ -59,6 +59,7 @@ class MockDBusThreadManagerWithoutGMock : public DBusThreadManager {
virtual ModemMessagingClient* GetModemMessagingClient() OVERRIDE;
virtual PermissionBrokerClient* GetPermissionBrokerClient() OVERRIDE;
virtual PowerManagerClient* GetPowerManagerClient() OVERRIDE;
+ virtual PowerPolicyController* GetPowerPolicyController() OVERRIDE;
virtual SessionManagerClient* GetSessionManagerClient() OVERRIDE;
virtual SMSClient* GetSMSClient() OVERRIDE;
virtual SpeechSynthesizerClient* GetSpeechSynthesizerClient() OVERRIDE;
diff --git a/chromeos/dbus/mock_power_manager_client.cc b/chromeos/dbus/mock_power_manager_client.cc
index 34c5170..7e1f3ca 100644
--- a/chromeos/dbus/mock_power_manager_client.cc
+++ b/chromeos/dbus/mock_power_manager_client.cc
@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "chromeos/dbus/mock_power_manager_client.h"
+#include "chromeos/dbus/power_manager/policy.pb.h"
namespace chromeos {
diff --git a/chromeos/dbus/mock_power_manager_client.h b/chromeos/dbus/mock_power_manager_client.h
index 48c7c26..a5abdea 100644
--- a/chromeos/dbus/mock_power_manager_client.h
+++ b/chromeos/dbus/mock_power_manager_client.h
@@ -35,6 +35,7 @@ class MockPowerManagerClient : public PowerManagerClient {
MOCK_METHOD0(RequestActiveNotification, void(void));
MOCK_METHOD1(NotifyUserActivity, void(const base::TimeTicks&));
MOCK_METHOD2(NotifyVideoActivity, void(const base::TimeTicks&, bool));
+ MOCK_METHOD1(SetPolicy, void(const power_manager::PowerManagementPolicy&));
MOCK_METHOD4(RequestPowerStateOverrides,
void(uint32,
base::TimeDelta,
diff --git a/chromeos/dbus/power_manager_client.cc b/chromeos/dbus/power_manager_client.cc
index 56a36a5..54b46ae 100644
--- a/chromeos/dbus/power_manager_client.cc
+++ b/chromeos/dbus/power_manager_client.cc
@@ -9,6 +9,7 @@
#include "base/bind.h"
#include "base/callback.h"
#include "base/format_macros.h"
+#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/observer_list.h"
@@ -17,6 +18,7 @@
#include "base/time.h"
#include "base/timer.h"
#include "chromeos/dbus/power_manager/input_event.pb.h"
+#include "chromeos/dbus/power_manager/policy.pb.h"
#include "chromeos/dbus/power_manager/suspend.pb.h"
#include "chromeos/dbus/power_state_control.pb.h"
#include "chromeos/dbus/power_supply_properties.pb.h"
@@ -282,6 +284,22 @@ class PowerManagerClientImpl : public PowerManagerClient {
dbus::ObjectProxy::EmptyResponseCallback());
}
+ virtual void SetPolicy(
+ const power_manager::PowerManagementPolicy& policy) OVERRIDE {
+ dbus::MethodCall method_call(
+ power_manager::kPowerManagerInterface,
+ power_manager::kSetPolicyMethod);
+ dbus::MessageWriter writer(&method_call);
+ if (!writer.AppendProtoAsArrayOfBytes(policy)) {
+ LOG(ERROR) << "Error calling " << power_manager::kSetPolicyMethod;
+ return;
+ }
+ power_manager_proxy_->CallMethod(
+ &method_call,
+ dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
+ dbus::ObjectProxy::EmptyResponseCallback());
+ }
+
virtual void RequestPowerStateOverrides(
uint32 request_id,
base::TimeDelta duration,
@@ -844,6 +862,8 @@ class PowerManagerClientStubImpl : public PowerManagerClient {
virtual void NotifyVideoActivity(
const base::TimeTicks& last_activity_time,
bool is_fullscreen) OVERRIDE {}
+ virtual void SetPolicy(
+ const power_manager::PowerManagementPolicy& policy) OVERRIDE {}
virtual void RequestPowerStateOverrides(
uint32 request_id,
base::TimeDelta duration,
diff --git a/chromeos/dbus/power_manager_client.h b/chromeos/dbus/power_manager_client.h
index 265001f..3accbadb 100644
--- a/chromeos/dbus/power_manager_client.h
+++ b/chromeos/dbus/power_manager_client.h
@@ -19,6 +19,10 @@ namespace dbus {
class Bus;
}
+namespace power_manager {
+class PowerManagementPolicy;
+}
+
namespace chromeos {
// Callback used for processing the idle time. The int64 param is the number of
@@ -165,6 +169,10 @@ class CHROMEOS_EXPORT PowerManagerClient {
const base::TimeTicks& last_activity_time,
bool is_fullscreen) = 0;
+ // Tells the power manager to begin using |policy|.
+ virtual void SetPolicy(
+ const power_manager::PowerManagementPolicy& policy) = 0;
+
// Override the current power state on the machine. The overrides will be
// applied to the request ID specified. To specify a new request; use 0 as the
// request id and the method will call the provided callback with the new
diff --git a/chromeos/dbus/power_policy_controller.cc b/chromeos/dbus/power_policy_controller.cc
new file mode 100644
index 0000000..b7683e4
--- /dev/null
+++ b/chromeos/dbus/power_policy_controller.cc
@@ -0,0 +1,178 @@
+// 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.
+
+#include "chromeos/dbus/power_policy_controller.h"
+
+#include "base/logging.h"
+#include "base/values.h"
+#include "chromeos/dbus/dbus_thread_manager.h"
+
+namespace chromeos {
+
+namespace {
+
+// If |pref|, a PrefServiceBase::Preference containing an integer, has been
+// explicitly set to 0 or a positive value, assigns it to |proto_field|, a
+// int32 field in |proto|, a google::protobuf::MessageLite*.
+#define SET_DELAY_FROM_PREF(pref, proto_field, proto) \
+ { \
+ int value = GetIntPrefValue(pref); \
+ if (value >= 0) \
+ (proto)->set_##proto_field(value); \
+ }
+
+// Similar to SET_DELAY_FROM_PREF() but sets a
+// power_manager::PowerManagementPolicy_Action field instead.
+#define SET_ACTION_FROM_PREF(pref, proto_field, proto) \
+ { \
+ int value = GetIntPrefValue(pref); \
+ if (value >= 0) { \
+ (proto)->set_##proto_field( \
+ static_cast<power_manager::PowerManagementPolicy_Action>(value)); \
+ } \
+ }
+
+// If |pref|, a PrefServiceBase::Preference containing a bool, has been
+// set, assigns it to |proto_field|, a bool field in |proto|, a
+// google::protobuf::MessageLite*.
+#define SET_BOOL_FROM_PREF(pref, proto_field, proto) \
+ if (!pref.IsDefaultValue()) { \
+ bool value = false; \
+ if (pref.GetValue()->GetAsBoolean(&value)) \
+ (proto)->set_##proto_field(value); \
+ else \
+ LOG(DFATAL) << pref.name() << " pref has non-boolean value"; \
+ }
+
+// Returns a zero or positive integer value from |pref|. Returns -1 if the
+// pref is unset and logs an error if it's set to a negative value.
+int GetIntPrefValue(const PrefServiceBase::Preference& pref) {
+ if (pref.IsDefaultValue())
+ return -1;
+
+ int value = -1;
+ if (!pref.GetValue()->GetAsInteger(&value)) {
+ LOG(DFATAL) << pref.name() << " pref has non-integer value";
+ return -1;
+ }
+
+ if (value < 0)
+ LOG(WARNING) << pref.name() << " pref has negative value";
+ return value;
+}
+
+// Returns the power_manager::PowerManagementPolicy_Action value
+// corresponding to |action|.
+power_manager::PowerManagementPolicy_Action GetProtoAction(
+ PowerPolicyController::Action action) {
+ switch (action) {
+ case PowerPolicyController::ACTION_SUSPEND:
+ return power_manager::PowerManagementPolicy_Action_SUSPEND;
+ case PowerPolicyController::ACTION_STOP_SESSION:
+ return power_manager::PowerManagementPolicy_Action_STOP_SESSION;
+ case PowerPolicyController::ACTION_SHUT_DOWN:
+ return power_manager::PowerManagementPolicy_Action_SHUT_DOWN;
+ case PowerPolicyController::ACTION_DO_NOTHING:
+ return power_manager::PowerManagementPolicy_Action_DO_NOTHING;
+ default:
+ NOTREACHED() << "Unhandled action " << action;
+ return power_manager::PowerManagementPolicy_Action_DO_NOTHING;
+ }
+}
+
+} // namespace
+
+PowerPolicyController::PowerPolicyController(DBusThreadManager* manager,
+ PowerManagerClient* client)
+ : manager_(manager),
+ client_(client) {
+ manager_->AddObserver(this);
+ client_->AddObserver(this);
+ SendEmptyPolicy();
+}
+
+PowerPolicyController::~PowerPolicyController() {
+ // The power manager's policy is reset before this point, in
+ // OnDBusThreadManagerDestroying(). At the time that
+ // PowerPolicyController is destroyed, PowerManagerClient's D-Bus proxy
+ // to the power manager is already gone.
+ client_->RemoveObserver(this);
+ client_ = NULL;
+ manager_->RemoveObserver(this);
+ manager_ = NULL;
+}
+
+void PowerPolicyController::UpdatePolicyFromPrefs(
+ const PrefServiceBase::Preference& ac_screen_dim_delay_ms_pref,
+ const PrefServiceBase::Preference& ac_screen_off_delay_ms_pref,
+ const PrefServiceBase::Preference& ac_screen_lock_delay_ms_pref,
+ const PrefServiceBase::Preference& ac_idle_delay_ms_pref,
+ const PrefServiceBase::Preference& battery_screen_dim_delay_ms_pref,
+ const PrefServiceBase::Preference& battery_screen_off_delay_ms_pref,
+ const PrefServiceBase::Preference& battery_screen_lock_delay_ms_pref,
+ const PrefServiceBase::Preference& battery_idle_delay_ms_pref,
+ const PrefServiceBase::Preference& idle_action_pref,
+ const PrefServiceBase::Preference& lid_closed_action_pref,
+ const PrefServiceBase::Preference& use_audio_activity_pref,
+ const PrefServiceBase::Preference& use_video_activity_pref,
+ const PrefServiceBase::Preference& presentation_idle_delay_factor_pref) {
+ prefs_policy_.Clear();
+
+ power_manager::PowerManagementPolicy::Delays* delays =
+ prefs_policy_.mutable_ac_delays();
+ SET_DELAY_FROM_PREF(ac_screen_dim_delay_ms_pref, screen_dim_ms, delays);
+ SET_DELAY_FROM_PREF(ac_screen_off_delay_ms_pref, screen_off_ms, delays);
+ SET_DELAY_FROM_PREF(ac_screen_lock_delay_ms_pref, screen_lock_ms, delays);
+ SET_DELAY_FROM_PREF(ac_idle_delay_ms_pref, idle_ms, delays);
+
+ delays = prefs_policy_.mutable_battery_delays();
+ SET_DELAY_FROM_PREF(battery_screen_dim_delay_ms_pref, screen_dim_ms, delays);
+ SET_DELAY_FROM_PREF(battery_screen_off_delay_ms_pref, screen_off_ms, delays);
+ SET_DELAY_FROM_PREF(
+ battery_screen_lock_delay_ms_pref, screen_lock_ms, delays);
+ SET_DELAY_FROM_PREF(battery_idle_delay_ms_pref, idle_ms, delays);
+
+ SET_ACTION_FROM_PREF(idle_action_pref, idle_action, &prefs_policy_);
+ SET_ACTION_FROM_PREF(
+ lid_closed_action_pref, lid_closed_action, &prefs_policy_);
+
+ SET_BOOL_FROM_PREF(
+ use_audio_activity_pref, use_audio_activity, &prefs_policy_);
+ SET_BOOL_FROM_PREF(
+ use_video_activity_pref, use_video_activity, &prefs_policy_);
+
+ if (!presentation_idle_delay_factor_pref.IsDefaultValue()) {
+ double value = 0.0;
+ if (presentation_idle_delay_factor_pref.GetValue()->GetAsDouble(&value)) {
+ prefs_policy_.set_presentation_idle_delay_factor(value);
+ } else {
+ LOG(DFATAL) << presentation_idle_delay_factor_pref.name()
+ << " pref has non-double value";
+ }
+ }
+
+ SendCurrentPolicy();
+}
+
+void PowerPolicyController::OnDBusThreadManagerDestroying(
+ DBusThreadManager* manager) {
+ DCHECK_EQ(manager, manager_);
+ SendEmptyPolicy();
+}
+
+void PowerPolicyController::PowerManagerRestarted() {
+ SendCurrentPolicy();
+}
+
+void PowerPolicyController::SendCurrentPolicy() {
+ // TODO(derat): Incorporate other information into the policy that is
+ // sent, e.g. from content::PowerSaveBlocker.
+ client_->SetPolicy(prefs_policy_);
+}
+
+void PowerPolicyController::SendEmptyPolicy() {
+ client_->SetPolicy(power_manager::PowerManagementPolicy());
+}
+
+} // namespace chromeos
diff --git a/chromeos/dbus/power_policy_controller.h b/chromeos/dbus/power_policy_controller.h
new file mode 100644
index 0000000..5bdc413
--- /dev/null
+++ b/chromeos/dbus/power_policy_controller.h
@@ -0,0 +1,79 @@
+// 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 "base/basictypes.h"
+#include "base/compiler_specific.h"
+#include "base/prefs/public/pref_service_base.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,
+ };
+
+ PowerPolicyController(DBusThreadManager* manager, PowerManagerClient* client);
+ ~PowerPolicyController();
+
+ // Sends an updated policy to the power manager based on the current
+ // values of the passed-in prefs.
+ void UpdatePolicyFromPrefs(
+ const PrefServiceBase::Preference& ac_screen_dim_delay_ms_pref,
+ const PrefServiceBase::Preference& ac_screen_off_delay_ms_pref,
+ const PrefServiceBase::Preference& ac_screen_lock_delay_ms_pref,
+ const PrefServiceBase::Preference& ac_idle_delay_ms_pref,
+ const PrefServiceBase::Preference& battery_screen_dim_delay_ms_pref,
+ const PrefServiceBase::Preference& battery_screen_off_delay_ms_pref,
+ const PrefServiceBase::Preference& battery_screen_lock_delay_ms_pref,
+ const PrefServiceBase::Preference& battery_idle_delay_ms_pref,
+ const PrefServiceBase::Preference& idle_action_pref,
+ const PrefServiceBase::Preference& lid_closed_action_pref,
+ const PrefServiceBase::Preference& use_audio_activity_pref,
+ const PrefServiceBase::Preference& use_video_activity_pref,
+ const PrefServiceBase::Preference& presentation_idle_delay_factor_pref);
+
+ // DBusThreadManagerObserver implementation:
+ virtual void OnDBusThreadManagerDestroying(DBusThreadManager* manager);
+
+ // PowerManagerClient::Observer implementation:
+ virtual void PowerManagerRestarted() OVERRIDE;
+
+ private:
+ // 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 specified by the prefs that were last passed to
+ // UpdatePolicyFromPrefs().
+ power_manager::PowerManagementPolicy prefs_policy_;
+
+ DISALLOW_COPY_AND_ASSIGN(PowerPolicyController);
+};
+
+} // namespace chromeos
+
+#endif // CHROMEOS_DBUS_POWER_POLICY_CONTROLLER_H_