Chromium Code Reviews| 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 "content/browser/power_save_blocker_impl.h" | 5 #include "content/browser/power_save_blocker_impl.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/chromeos/chromeos_version.h" | |
| 10 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
|
bartfab (slow)
2013/03/21 14:22:26
Not used.
Daniel Erat
2013/03/21 14:48:46
Done.
| |
| 12 #include "chromeos/power/power_state_override.h" | 11 #include "chromeos/dbus/dbus_thread_manager.h" |
| 12 #include "chromeos/dbus/power_policy_controller.h" | |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 | 14 |
| 15 namespace content { | 15 namespace content { |
| 16 | 16 |
| 17 class PowerSaveBlockerImpl::Delegate | 17 class PowerSaveBlockerImpl::Delegate |
| 18 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { | 18 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { |
| 19 public: | 19 public: |
| 20 Delegate(PowerSaveBlockerType type) : type_(type) {} | 20 Delegate(PowerSaveBlockerType type, const std::string& reason) |
|
bartfab (slow)
2013/03/21 14:22:26
#include <string>
Daniel Erat
2013/03/21 14:48:46
Done (but it's already included by content/public/
bartfab (slow)
2013/03/21 15:34:38
Borderline case for IWYU :).
| |
| 21 : type_(type), | |
| 22 reason_(reason), | |
| 23 block_id_(0) {} | |
|
bartfab (slow)
2013/03/21 14:22:26
Clever idea to default block_id_ to 0 and have the
| |
| 21 | 24 |
| 22 // Creates a PowerStateOverride object to override the default power | |
| 23 // management behavior. | |
| 24 void ApplyBlock() { | 25 void ApplyBlock() { |
| 25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
|
bartfab (slow)
2013/03/21 14:22:26
#include "base/logging.h"
Daniel Erat
2013/03/21 14:48:46
Done.
| |
| 27 if (!chromeos::DBusThreadManager::IsInitialized()) { | |
| 28 LOG(WARNING) << "DBusThreadManager not initialized"; | |
| 29 return; | |
| 30 } | |
| 26 | 31 |
| 27 // Do nothing when not running on real CrOS devices. | 32 chromeos::PowerPolicyController* controller = |
| 28 if (!base::chromeos::IsRunningOnChromeOS()) | 33 chromeos::DBusThreadManager::Get()->GetPowerPolicyController(); |
| 29 return; | |
| 30 | |
| 31 chromeos::PowerStateOverride::Mode mode = | |
| 32 chromeos::PowerStateOverride::BLOCK_SYSTEM_SUSPEND; | |
| 33 switch (type_) { | 34 switch (type_) { |
| 34 case kPowerSaveBlockPreventAppSuspension: | 35 case kPowerSaveBlockPreventAppSuspension: |
| 35 mode = chromeos::PowerStateOverride::BLOCK_SYSTEM_SUSPEND; | 36 block_id_ = controller->AddSuspendBlock(reason_); |
| 36 break; | 37 break; |
| 37 case kPowerSaveBlockPreventDisplaySleep: | 38 case kPowerSaveBlockPreventDisplaySleep: |
| 38 mode = chromeos::PowerStateOverride::BLOCK_DISPLAY_SLEEP; | 39 block_id_ = controller->AddScreenBlock(reason_); |
| 39 break; | 40 break; |
| 40 default: | 41 default: |
| 41 NOTREACHED() << "Unhandled block type " << type_; | 42 NOTREACHED() << "Unhandled block type " << type_; |
| 42 } | 43 } |
| 43 override_ = new chromeos::PowerStateOverride(mode); | |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Resets the previously-created PowerStateOverride object. | |
| 47 void RemoveBlock() { | 46 void RemoveBlock() { |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 49 override_ = NULL; | 48 if (!chromeos::DBusThreadManager::IsInitialized()) { |
| 49 LOG(WARNING) << "DBusThreadManager not initialized"; | |
| 50 return; | |
| 51 } | |
| 52 chromeos::DBusThreadManager::Get()->GetPowerPolicyController()-> | |
| 53 RemoveBlock(block_id_); | |
| 50 } | 54 } |
| 51 | 55 |
| 52 private: | 56 private: |
| 53 friend class base::RefCountedThreadSafe<Delegate>; | 57 friend class base::RefCountedThreadSafe<Delegate>; |
| 54 virtual ~Delegate() {} | 58 virtual ~Delegate() {} |
| 55 | 59 |
| 56 PowerSaveBlockerType type_; | 60 PowerSaveBlockerType type_; |
| 61 std::string reason_; | |
| 57 | 62 |
| 58 scoped_refptr<chromeos::PowerStateOverride> override_; | 63 // ID corresponding to the block request in PowerPolicyController. |
| 64 int block_id_; | |
| 59 | 65 |
| 60 DISALLOW_COPY_AND_ASSIGN(Delegate); | 66 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 61 }; | 67 }; |
| 62 | 68 |
| 63 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, | 69 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, |
| 64 const std::string& reason) | 70 const std::string& reason) |
| 65 : delegate_(new Delegate(type)) { | 71 : delegate_(new Delegate(type, reason)) { |
| 66 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 72 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
|
bartfab (slow)
2013/03/21 14:22:26
#include "base/location.h"
Daniel Erat
2013/03/21 14:48:46
Done.
| |
| 67 base::Bind(&Delegate::ApplyBlock, delegate_)); | 73 base::Bind(&Delegate::ApplyBlock, delegate_)); |
| 68 } | 74 } |
| 69 | 75 |
| 70 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { | 76 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { |
| 71 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 77 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 72 base::Bind(&Delegate::RemoveBlock, delegate_)); | 78 base::Bind(&Delegate::RemoveBlock, delegate_)); |
| 73 } | 79 } |
| 74 | 80 |
| 75 } // namespace content | 81 } // namespace content |
| OLD | NEW |