Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: chromeos/dbus/power_manager_client.cc

Issue 181413006: Replace misc. network stub flags with more flexible ones (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Elim. shill_stub_helper and move functions to clients. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/command_line.h" 11 #include "base/command_line.h"
12 #include "base/format_macros.h" 12 #include "base/format_macros.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
16 #include "base/observer_list.h" 16 #include "base/observer_list.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_split.h"
17 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
18 #include "base/threading/platform_thread.h" 20 #include "base/threading/platform_thread.h"
19 #include "base/time/time.h" 21 #include "base/time/time.h"
20 #include "base/timer/timer.h" 22 #include "base/timer/timer.h"
21 #include "chromeos/chromeos_switches.h" 23 #include "chromeos/chromeos_switches.h"
24 #include "chromeos/dbus/dbus_command_line_helper.h"
25 #include "chromeos/dbus/dbus_thread_manager.h"
22 #include "chromeos/dbus/power_manager/input_event.pb.h" 26 #include "chromeos/dbus/power_manager/input_event.pb.h"
23 #include "chromeos/dbus/power_manager/peripheral_battery_status.pb.h" 27 #include "chromeos/dbus/power_manager/peripheral_battery_status.pb.h"
24 #include "chromeos/dbus/power_manager/policy.pb.h" 28 #include "chromeos/dbus/power_manager/policy.pb.h"
25 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" 29 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
26 #include "chromeos/dbus/power_manager/suspend.pb.h" 30 #include "chromeos/dbus/power_manager/suspend.pb.h"
27 #include "dbus/bus.h" 31 #include "dbus/bus.h"
28 #include "dbus/message.h" 32 #include "dbus/message.h"
29 #include "dbus/object_path.h" 33 #include "dbus/object_path.h"
30 #include "dbus/object_proxy.h" 34 #include "dbus/object_proxy.h"
31 35
(...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after
647 // Last state passed to SetIsProjecting(). 651 // Last state passed to SetIsProjecting().
648 bool last_is_projecting_; 652 bool last_is_projecting_;
649 653
650 // Note: This should remain the last member so it'll be destroyed and 654 // Note: This should remain the last member so it'll be destroyed and
651 // invalidate its weak pointers before any other members are destroyed. 655 // invalidate its weak pointers before any other members are destroyed.
652 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_; 656 base::WeakPtrFactory<PowerManagerClientImpl> weak_ptr_factory_;
653 657
654 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl); 658 DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl);
655 }; 659 };
656 660
657 // The PowerManagerClient implementation used on Linux desktop, 661 // The fake PowerManagerClient implementation used on Linux desktop. This
658 // which does nothing. 662 // can simulate a battery draining/charging, etc, for testing UI.
659 class PowerManagerClientStubImpl : public PowerManagerClient { 663 class FakePowerManagerClient : public PowerManagerClient {
660 public: 664 public:
661 PowerManagerClientStubImpl() 665 FakePowerManagerClient()
662 : discharging_(true), 666 : power_cycle_delay_(false),
667 discharging_(true),
663 battery_percentage_(40), 668 battery_percentage_(40),
664 brightness_(50.0), 669 brightness_(50.0),
665 pause_count_(2), 670 pause_count_(2),
666 cycle_count_(0), 671 cycle_count_(0),
667 num_pending_suspend_readiness_callbacks_(0), 672 num_pending_suspend_readiness_callbacks_(0),
668 weak_ptr_factory_(this) {} 673 weak_ptr_factory_(this) {
674 ParseCommandLineSwitch();
675 }
669 676
670 virtual ~PowerManagerClientStubImpl() {} 677 virtual ~FakePowerManagerClient() {}
671 678
672 int num_pending_suspend_readiness_callbacks() const { 679 int num_pending_suspend_readiness_callbacks() const {
673 return num_pending_suspend_readiness_callbacks_; 680 return num_pending_suspend_readiness_callbacks_;
674 } 681 }
675 682
676 // PowerManagerClient overrides: 683 // PowerManagerClient overrides:
677 virtual void Init(dbus::Bus* bus) OVERRIDE { 684 virtual void Init(dbus::Bus* bus) OVERRIDE {
678 if (CommandLine::ForCurrentProcess()->HasSwitch( 685 if (power_cycle_delay_) {
679 chromeos::switches::kEnableStubInteractive)) {
680 const int kStatusUpdateMs = 1000;
681 update_timer_.Start(FROM_HERE, 686 update_timer_.Start(FROM_HERE,
682 base::TimeDelta::FromMilliseconds(kStatusUpdateMs), this, 687 base::TimeDelta::FromSeconds(power_cycle_delay_),
683 &PowerManagerClientStubImpl::UpdateStatus); 688 this,
689 &FakePowerManagerClient::UpdateStatus);
684 } 690 }
685 } 691 }
686 692
687 virtual void AddObserver(Observer* observer) OVERRIDE { 693 virtual void AddObserver(Observer* observer) OVERRIDE {
688 observers_.AddObserver(observer); 694 observers_.AddObserver(observer);
689 } 695 }
690 696
691 virtual void RemoveObserver(Observer* observer) OVERRIDE { 697 virtual void RemoveObserver(Observer* observer) OVERRIDE {
692 observers_.RemoveObserver(observer); 698 observers_.RemoveObserver(observer);
693 } 699 }
(...skipping 27 matching lines...) Expand all
721 virtual void DecreaseKeyboardBrightness() OVERRIDE { 727 virtual void DecreaseKeyboardBrightness() OVERRIDE {
722 VLOG(1) << "Requested to descrease keyboard brightness"; 728 VLOG(1) << "Requested to descrease keyboard brightness";
723 } 729 }
724 730
725 virtual void IncreaseKeyboardBrightness() OVERRIDE { 731 virtual void IncreaseKeyboardBrightness() OVERRIDE {
726 VLOG(1) << "Requested to increase keyboard brightness"; 732 VLOG(1) << "Requested to increase keyboard brightness";
727 } 733 }
728 734
729 virtual void RequestStatusUpdate() OVERRIDE { 735 virtual void RequestStatusUpdate() OVERRIDE {
730 base::MessageLoop::current()->PostTask(FROM_HERE, 736 base::MessageLoop::current()->PostTask(FROM_HERE,
731 base::Bind(&PowerManagerClientStubImpl::UpdateStatus, 737 base::Bind(&FakePowerManagerClient::UpdateStatus,
732 weak_ptr_factory_.GetWeakPtr())); 738 weak_ptr_factory_.GetWeakPtr()));
733 } 739 }
734 740
735 virtual void RequestRestart() OVERRIDE {} 741 virtual void RequestRestart() OVERRIDE {}
736 virtual void RequestShutdown() OVERRIDE {} 742 virtual void RequestShutdown() OVERRIDE {}
737 743
738 virtual void NotifyUserActivity( 744 virtual void NotifyUserActivity(
739 power_manager::UserActivityType type) OVERRIDE {} 745 power_manager::UserActivityType type) OVERRIDE {}
740 virtual void NotifyVideoActivity(bool is_fullscreen) OVERRIDE {} 746 virtual void NotifyVideoActivity(bool is_fullscreen) OVERRIDE {}
741 virtual void SetPolicy( 747 virtual void SetPolicy(
742 const power_manager::PowerManagementPolicy& policy) OVERRIDE {} 748 const power_manager::PowerManagementPolicy& policy) OVERRIDE {}
743 virtual void SetIsProjecting(bool is_projecting) OVERRIDE {} 749 virtual void SetIsProjecting(bool is_projecting) OVERRIDE {}
744 virtual base::Closure GetSuspendReadinessCallback() OVERRIDE { 750 virtual base::Closure GetSuspendReadinessCallback() OVERRIDE {
745 num_pending_suspend_readiness_callbacks_++; 751 num_pending_suspend_readiness_callbacks_++;
746 return base::Bind(&PowerManagerClientStubImpl::HandleSuspendReadiness, 752 return base::Bind(&FakePowerManagerClient::HandleSuspendReadiness,
747 weak_ptr_factory_.GetWeakPtr()); 753 weak_ptr_factory_.GetWeakPtr());
748 } 754 }
749 virtual int GetNumPendingSuspendReadinessCallbacks() OVERRIDE { 755 virtual int GetNumPendingSuspendReadinessCallbacks() OVERRIDE {
750 return num_pending_suspend_readiness_callbacks_; 756 return num_pending_suspend_readiness_callbacks_;
751 } 757 }
752 758
753 private: 759 private:
754 void HandleSuspendReadiness() { 760 void HandleSuspendReadiness() {
755 num_pending_suspend_readiness_callbacks_--; 761 num_pending_suspend_readiness_callbacks_--;
756 } 762 }
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
823 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(props_)); 829 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(props_));
824 } 830 }
825 831
826 void SetBrightness(double percent, bool user_initiated) { 832 void SetBrightness(double percent, bool user_initiated) {
827 brightness_ = std::min(std::max(0.0, percent), 100.0); 833 brightness_ = std::min(std::max(0.0, percent), 100.0);
828 int brightness_level = static_cast<int>(brightness_); 834 int brightness_level = static_cast<int>(brightness_);
829 FOR_EACH_OBSERVER(Observer, observers_, 835 FOR_EACH_OBSERVER(Observer, observers_,
830 BrightnessChanged(brightness_level, user_initiated)); 836 BrightnessChanged(brightness_level, user_initiated));
831 } 837 }
832 838
839 void ParseCommandLineSwitch() {
840 dbus_command_line_helper::ParseOptions(
841 switches::kPowerStub,
842 base::Bind(&FakePowerManagerClient::ParseOption,
843 base::Unretained(this)));
844 }
845
846 bool ParseOption(const std::string& arg0, const std::string& arg1) {
847 if (arg0 == "cycle" || arg0 == "interactive") {
848 int seconds = 1;
849 if (!arg1.empty())
850 base::StringToInt(arg1, &seconds);
851 power_cycle_delay_ = seconds;
852 return true;
853 }
854 return false;
855 }
856
857 int power_cycle_delay_; // Seconds over which to cycle power state
833 bool discharging_; 858 bool discharging_;
834 int battery_percentage_; 859 int battery_percentage_;
835 double brightness_; 860 double brightness_;
836 int pause_count_; 861 int pause_count_;
837 int cycle_count_; 862 int cycle_count_;
838 ObserverList<Observer> observers_; 863 ObserverList<Observer> observers_;
839 base::RepeatingTimer<PowerManagerClientStubImpl> update_timer_; 864 base::RepeatingTimer<FakePowerManagerClient> update_timer_;
840 power_manager::PowerSupplyProperties props_; 865 power_manager::PowerSupplyProperties props_;
841 866
842 // Number of callbacks returned by GetSuspendReadinessCallback() but not yet 867 // Number of callbacks returned by GetSuspendReadinessCallback() but not yet
843 // invoked. 868 // invoked.
844 int num_pending_suspend_readiness_callbacks_; 869 int num_pending_suspend_readiness_callbacks_;
845 870
846 // Note: This should remain the last member so it'll be destroyed and 871 // Note: This should remain the last member so it'll be destroyed and
847 // invalidate its weak pointers before any other members are destroyed. 872 // invalidate its weak pointers before any other members are destroyed.
848 base::WeakPtrFactory<PowerManagerClientStubImpl> weak_ptr_factory_; 873 base::WeakPtrFactory<FakePowerManagerClient> weak_ptr_factory_;
849 }; 874 };
850 875
851 PowerManagerClient::PowerManagerClient() { 876 PowerManagerClient::PowerManagerClient() {
852 } 877 }
853 878
854 PowerManagerClient::~PowerManagerClient() { 879 PowerManagerClient::~PowerManagerClient() {
855 } 880 }
856 881
857 // static 882 // static
858 PowerManagerClient* PowerManagerClient::Create( 883 PowerManagerClient* PowerManagerClient::Create(
859 DBusClientImplementationType type) { 884 DBusClientImplementationType type) {
860 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) 885 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
861 return new PowerManagerClientImpl(); 886 return new PowerManagerClientImpl();
862 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); 887 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
863 return new PowerManagerClientStubImpl(); 888 return new FakePowerManagerClient();
864 } 889 }
865 890
866 } // namespace chromeos 891 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698