OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/dbus/power_manager_client.h" | 5 #include "chromeos/dbus/power_manager_client.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
16 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
17 #include "base/threading/platform_thread.h" | 17 #include "base/threading/platform_thread.h" |
18 #include "base/time.h" | 18 #include "base/time.h" |
19 #include "base/timer.h" | 19 #include "base/timer.h" |
20 #include "chromeos/dbus/power_manager/input_event.pb.h" | 20 #include "chromeos/dbus/power_manager/input_event.pb.h" |
21 #include "chromeos/dbus/power_manager/policy.pb.h" | 21 #include "chromeos/dbus/power_manager/policy.pb.h" |
22 #include "chromeos/dbus/power_manager/suspend.pb.h" | 22 #include "chromeos/dbus/power_manager/suspend.pb.h" |
23 #include "chromeos/dbus/power_state_control.pb.h" | |
24 #include "chromeos/dbus/power_supply_properties.pb.h" | 23 #include "chromeos/dbus/power_supply_properties.pb.h" |
25 #include "chromeos/dbus/video_activity_update.pb.h" | 24 #include "chromeos/dbus/video_activity_update.pb.h" |
26 #include "dbus/bus.h" | 25 #include "dbus/bus.h" |
27 #include "dbus/message.h" | 26 #include "dbus/message.h" |
28 #include "dbus/object_path.h" | 27 #include "dbus/object_path.h" |
29 #include "dbus/object_proxy.h" | 28 #include "dbus/object_proxy.h" |
30 #include "third_party/cros_system_api/dbus/service_constants.h" | 29 #include "third_party/cros_system_api/dbus/service_constants.h" |
31 | 30 |
32 namespace chromeos { | 31 namespace chromeos { |
33 | 32 |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
301 if (!writer.AppendProtoAsArrayOfBytes(policy)) { | 300 if (!writer.AppendProtoAsArrayOfBytes(policy)) { |
302 LOG(ERROR) << "Error calling " << power_manager::kSetPolicyMethod; | 301 LOG(ERROR) << "Error calling " << power_manager::kSetPolicyMethod; |
303 return; | 302 return; |
304 } | 303 } |
305 power_manager_proxy_->CallMethod( | 304 power_manager_proxy_->CallMethod( |
306 &method_call, | 305 &method_call, |
307 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 306 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
308 dbus::ObjectProxy::EmptyResponseCallback()); | 307 dbus::ObjectProxy::EmptyResponseCallback()); |
309 } | 308 } |
310 | 309 |
311 virtual void RequestPowerStateOverrides( | |
312 uint32 request_id, | |
313 base::TimeDelta duration, | |
314 int overrides, | |
315 const PowerStateRequestIdCallback& callback) OVERRIDE { | |
316 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, | |
317 power_manager::kStateOverrideRequest); | |
318 dbus::MessageWriter writer(&method_call); | |
319 | |
320 PowerStateControl protobuf; | |
321 protobuf.set_request_id(request_id); | |
322 protobuf.set_duration(duration.InSeconds()); | |
323 protobuf.set_disable_idle_dim(overrides & DISABLE_IDLE_DIM); | |
324 protobuf.set_disable_idle_blank(overrides & DISABLE_IDLE_BLANK); | |
325 protobuf.set_disable_idle_suspend(overrides & DISABLE_IDLE_SUSPEND); | |
326 protobuf.set_disable_lid_suspend(overrides & DISABLE_LID_SUSPEND); | |
327 | |
328 if (!writer.AppendProtoAsArrayOfBytes(protobuf)) { | |
329 LOG(ERROR) << "Error calling " | |
330 << power_manager::kStateOverrideRequest; | |
331 return; | |
332 } | |
333 power_manager_proxy_->CallMethod( | |
334 &method_call, | |
335 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
336 base::Bind(&PowerManagerClientImpl::OnPowerStateOverride, | |
337 weak_ptr_factory_.GetWeakPtr(), callback)); | |
338 } | |
339 | |
340 virtual void CancelPowerStateOverrides(uint32 request_id) OVERRIDE { | |
341 dbus::MethodCall method_call(power_manager::kPowerManagerInterface, | |
342 power_manager::kStateOverrideCancel); | |
343 dbus::MessageWriter writer(&method_call); | |
344 writer.AppendInt32(request_id); | |
345 power_manager_proxy_->CallMethod( | |
346 &method_call, | |
347 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
348 dbus::ObjectProxy::EmptyResponseCallback()); | |
349 } | |
350 | |
351 virtual void SetIsProjecting(bool is_projecting) OVERRIDE { | 310 virtual void SetIsProjecting(bool is_projecting) OVERRIDE { |
352 dbus::MethodCall method_call( | 311 dbus::MethodCall method_call( |
353 power_manager::kPowerManagerInterface, | 312 power_manager::kPowerManagerInterface, |
354 power_manager::kSetIsProjectingMethod); | 313 power_manager::kSetIsProjectingMethod); |
355 dbus::MessageWriter writer(&method_call); | 314 dbus::MessageWriter writer(&method_call); |
356 writer.AppendBool(is_projecting); | 315 writer.AppendBool(is_projecting); |
357 power_manager_proxy_->CallMethod( | 316 power_manager_proxy_->CallMethod( |
358 &method_call, | 317 &method_call, |
359 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 318 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
360 dbus::ObjectProxy::EmptyResponseCallback()); | 319 dbus::ObjectProxy::EmptyResponseCallback()); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
462 status.battery_percentage = protobuf.battery_percentage(); | 421 status.battery_percentage = protobuf.battery_percentage(); |
463 status.battery_is_present = protobuf.battery_is_present(); | 422 status.battery_is_present = protobuf.battery_is_present(); |
464 status.battery_is_full = protobuf.battery_is_charged(); | 423 status.battery_is_full = protobuf.battery_is_charged(); |
465 status.is_calculating_battery_time = protobuf.is_calculating_battery_time(); | 424 status.is_calculating_battery_time = protobuf.is_calculating_battery_time(); |
466 status.battery_energy_rate = protobuf.battery_energy_rate(); | 425 status.battery_energy_rate = protobuf.battery_energy_rate(); |
467 | 426 |
468 VLOG(1) << "Power status: " << status.ToString(); | 427 VLOG(1) << "Power status: " << status.ToString(); |
469 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); | 428 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(status)); |
470 } | 429 } |
471 | 430 |
472 void OnPowerStateOverride(const PowerStateRequestIdCallback& callback, | |
473 dbus::Response* response) { | |
474 if (!response) { | |
475 LOG(ERROR) << "Error calling " << power_manager::kStateOverrideRequest; | |
476 return; | |
477 } | |
478 | |
479 dbus::MessageReader reader(response); | |
480 int32 request_id = 0; | |
481 if (!reader.PopInt32(&request_id)) { | |
482 LOG(ERROR) << "Error reading response from powerd: " | |
483 << response->ToString(); | |
484 callback.Run(0); | |
485 return; | |
486 } | |
487 | |
488 callback.Run(request_id); | |
489 } | |
490 | |
491 void OnGetScreenBrightnessPercent( | 431 void OnGetScreenBrightnessPercent( |
492 const GetScreenBrightnessPercentCallback& callback, | 432 const GetScreenBrightnessPercentCallback& callback, |
493 dbus::Response* response) { | 433 dbus::Response* response) { |
494 if (!response) { | 434 if (!response) { |
495 LOG(ERROR) << "Error calling " | 435 LOG(ERROR) << "Error calling " |
496 << power_manager::kGetScreenBrightnessPercent; | 436 << power_manager::kGetScreenBrightnessPercent; |
497 return; | 437 return; |
498 } | 438 } |
499 dbus::MessageReader reader(response); | 439 dbus::MessageReader reader(response); |
500 double percent = 0.0; | 440 double percent = 0.0; |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
847 base::TimeDelta::FromMilliseconds(threshold)); | 787 base::TimeDelta::FromMilliseconds(threshold)); |
848 } | 788 } |
849 | 789 |
850 virtual void NotifyUserActivity( | 790 virtual void NotifyUserActivity( |
851 const base::TimeTicks& last_activity_time) OVERRIDE {} | 791 const base::TimeTicks& last_activity_time) OVERRIDE {} |
852 virtual void NotifyVideoActivity( | 792 virtual void NotifyVideoActivity( |
853 const base::TimeTicks& last_activity_time, | 793 const base::TimeTicks& last_activity_time, |
854 bool is_fullscreen) OVERRIDE {} | 794 bool is_fullscreen) OVERRIDE {} |
855 virtual void SetPolicy( | 795 virtual void SetPolicy( |
856 const power_manager::PowerManagementPolicy& policy) OVERRIDE {} | 796 const power_manager::PowerManagementPolicy& policy) OVERRIDE {} |
857 virtual void RequestPowerStateOverrides( | |
858 uint32 request_id, | |
859 base::TimeDelta duration, | |
860 int overrides, | |
861 const PowerStateRequestIdCallback& callback) OVERRIDE { | |
862 // Mimic the behavior of power manager w.r.t. the request_id. | |
863 if (request_id == 0) { | |
864 callback.Run(next_request_id_++); | |
865 } else { | |
866 callback.Run(request_id); | |
867 } | |
868 } | |
869 virtual void CancelPowerStateOverrides(uint32 request_id) OVERRIDE {} | |
870 virtual void SetIsProjecting(bool is_projecting) OVERRIDE {} | 797 virtual void SetIsProjecting(bool is_projecting) OVERRIDE {} |
871 virtual base::Closure GetSuspendReadinessCallback() OVERRIDE { | 798 virtual base::Closure GetSuspendReadinessCallback() OVERRIDE { |
872 return base::Closure(); | 799 return base::Closure(); |
873 } | 800 } |
874 | 801 |
875 private: | 802 private: |
876 void Update() { | 803 void Update() { |
877 if (pause_count_ > 0) { | 804 if (pause_count_ > 0) { |
878 pause_count_--; | 805 pause_count_--; |
879 } else { | 806 } else { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
915 FOR_EACH_OBSERVER(Observer, observers_, IdleNotify(threshold)); | 842 FOR_EACH_OBSERVER(Observer, observers_, IdleNotify(threshold)); |
916 } | 843 } |
917 | 844 |
918 bool discharging_; | 845 bool discharging_; |
919 int battery_percentage_; | 846 int battery_percentage_; |
920 double brightness_; | 847 double brightness_; |
921 int pause_count_; | 848 int pause_count_; |
922 ObserverList<Observer> observers_; | 849 ObserverList<Observer> observers_; |
923 base::RepeatingTimer<PowerManagerClientStubImpl> timer_; | 850 base::RepeatingTimer<PowerManagerClientStubImpl> timer_; |
924 PowerSupplyStatus status_; | 851 PowerSupplyStatus status_; |
925 uint32 next_request_id_; | 852 uint32 next_request_id_; |
bartfab (slow)
2013/03/21 15:34:38
No longer needed.
Daniel Erat
2013/03/21 15:42:31
Thanks!
| |
926 }; | 853 }; |
927 | 854 |
928 PowerManagerClient::PowerManagerClient() { | 855 PowerManagerClient::PowerManagerClient() { |
929 } | 856 } |
930 | 857 |
931 PowerManagerClient::~PowerManagerClient() { | 858 PowerManagerClient::~PowerManagerClient() { |
932 } | 859 } |
933 | 860 |
861 // static | |
934 PowerManagerClient* PowerManagerClient::Create( | 862 PowerManagerClient* PowerManagerClient::Create( |
935 DBusClientImplementationType type, | 863 DBusClientImplementationType type, |
936 dbus::Bus* bus) { | 864 dbus::Bus* bus) { |
937 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 865 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
938 return new PowerManagerClientImpl(bus); | 866 return new PowerManagerClientImpl(bus); |
939 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 867 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
940 return new PowerManagerClientStubImpl(); | 868 return new PowerManagerClientStubImpl(); |
941 } | 869 } |
942 | 870 |
943 } // namespace chromeos | 871 } // namespace chromeos |
OLD | NEW |