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