OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "chromeos/dbus/dbus_thread_manager.h" | |
15 #include "chromeos/dbus/power_policy_controller.h" | |
16 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
17 | 15 |
18 namespace content { | 16 namespace content { |
19 | 17 |
20 class PowerSaveBlockerImpl::Delegate | 18 class PowerSaveBlockerImpl::Delegate |
21 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { | 19 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { |
22 public: | 20 public: |
23 Delegate(PowerSaveBlockerType type, const std::string& reason) | 21 Delegate(PowerSaveBlockerType type, const std::string& reason) |
24 : type_(type), | 22 : type_(type), |
25 reason_(reason), | 23 reason_(reason), |
26 block_id_(0) {} | 24 block_id_(0) {} |
27 | 25 |
28 void ApplyBlock() { | 26 void ApplyBlock() { |
29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
30 if (!chromeos::DBusThreadManager::IsInitialized()) { | |
31 LOG(WARNING) << "DBusThreadManager not initialized"; | |
32 return; | |
33 } | |
34 | |
35 chromeos::PowerPolicyController* controller = | |
36 chromeos::DBusThreadManager::Get()->GetPowerPolicyController(); | |
37 switch (type_) { | |
38 case kPowerSaveBlockPreventAppSuspension: | |
39 block_id_ = controller->AddSystemWakeLock(reason_); | |
40 break; | |
41 case kPowerSaveBlockPreventDisplaySleep: | |
42 block_id_ = controller->AddScreenWakeLock(reason_); | |
43 break; | |
44 default: | |
45 NOTREACHED() << "Unhandled block type " << type_; | |
46 } | |
47 } | 27 } |
48 | 28 |
49 void RemoveBlock() { | 29 void RemoveBlock() { |
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
51 if (!chromeos::DBusThreadManager::IsInitialized()) { | |
52 LOG(WARNING) << "DBusThreadManager not initialized"; | |
53 return; | |
54 } | |
55 chromeos::DBusThreadManager::Get()->GetPowerPolicyController()-> | |
56 RemoveWakeLock(block_id_); | |
57 } | 30 } |
58 | 31 |
59 private: | 32 private: |
60 friend class base::RefCountedThreadSafe<Delegate>; | 33 friend class base::RefCountedThreadSafe<Delegate>; |
61 virtual ~Delegate() {} | 34 virtual ~Delegate() {} |
62 | 35 |
63 PowerSaveBlockerType type_; | 36 PowerSaveBlockerType type_; |
64 std::string reason_; | 37 std::string reason_; |
65 | 38 |
66 // ID corresponding to the block request in PowerPolicyController. | 39 // ID corresponding to the block request in PowerPolicyController. |
67 int block_id_; | 40 int block_id_; |
68 | 41 |
69 DISALLOW_COPY_AND_ASSIGN(Delegate); | 42 DISALLOW_COPY_AND_ASSIGN(Delegate); |
70 }; | 43 }; |
71 | 44 |
72 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, | 45 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, |
73 const std::string& reason) | 46 const std::string& reason) |
74 : delegate_(new Delegate(type, reason)) { | 47 : delegate_(new Delegate(type, reason)) { |
75 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 48 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
76 base::Bind(&Delegate::ApplyBlock, delegate_)); | 49 base::Bind(&Delegate::ApplyBlock, delegate_)); |
77 } | 50 } |
78 | 51 |
79 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { | 52 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { |
80 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 53 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
81 base::Bind(&Delegate::RemoveBlock, delegate_)); | 54 base::Bind(&Delegate::RemoveBlock, delegate_)); |
82 } | 55 } |
83 | 56 |
84 } // namespace content | 57 } // namespace content |
OLD | NEW |