| 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> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "chromeos/dbus/power_policy_controller.h" | 14 #include "chromeos/dbus/power_policy_controller.h" |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 | 15 |
| 17 namespace content { | 16 namespace content { |
| 18 | 17 |
| 19 namespace { | 18 namespace { |
| 20 | 19 |
| 21 // Converts a PowerSaveBlocker::Reason to a | 20 // Converts a PowerSaveBlocker::Reason to a |
| 22 // chromeos::PowerPolicyController::WakeLockReason. | 21 // chromeos::PowerPolicyController::WakeLockReason. |
| 23 chromeos::PowerPolicyController::WakeLockReason GetWakeLockReason( | 22 chromeos::PowerPolicyController::WakeLockReason GetWakeLockReason( |
| 24 PowerSaveBlocker::Reason reason) { | 23 PowerSaveBlocker::Reason reason) { |
| 25 switch (reason) { | 24 switch (reason) { |
| 26 case PowerSaveBlocker::kReasonAudioPlayback: | 25 case PowerSaveBlocker::kReasonAudioPlayback: |
| 27 return chromeos::PowerPolicyController::REASON_AUDIO_PLAYBACK; | 26 return chromeos::PowerPolicyController::REASON_AUDIO_PLAYBACK; |
| 28 case PowerSaveBlocker::kReasonVideoPlayback: | 27 case PowerSaveBlocker::kReasonVideoPlayback: |
| 29 return chromeos::PowerPolicyController::REASON_VIDEO_PLAYBACK; | 28 return chromeos::PowerPolicyController::REASON_VIDEO_PLAYBACK; |
| 30 case PowerSaveBlocker::kReasonOther: | 29 case PowerSaveBlocker::kReasonOther: |
| 31 return chromeos::PowerPolicyController::REASON_OTHER; | 30 return chromeos::PowerPolicyController::REASON_OTHER; |
| 32 } | 31 } |
| 33 return chromeos::PowerPolicyController::REASON_OTHER; | 32 return chromeos::PowerPolicyController::REASON_OTHER; |
| 34 } | 33 } |
| 35 | 34 |
| 36 } // namespace | 35 } // namespace |
| 37 | 36 |
| 38 class PowerSaveBlockerImpl::Delegate | 37 class PowerSaveBlockerImpl::Delegate |
| 39 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { | 38 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { |
| 40 public: | 39 public: |
| 41 Delegate(PowerSaveBlockerType type, | 40 Delegate(PowerSaveBlockerType type, |
| 42 Reason reason, | 41 Reason reason, |
| 43 const std::string& description) | 42 const std::string& description, |
| 44 : type_(type), reason_(reason), description_(description), block_id_(0) {} | 43 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) |
| 44 : type_(type), |
| 45 reason_(reason), |
| 46 description_(description), |
| 47 block_id_(0), |
| 48 ui_task_runner_(ui_task_runner) {} |
| 45 | 49 |
| 46 void ApplyBlock() { | 50 void ApplyBlock() { |
| 47 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 51 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 48 if (!chromeos::PowerPolicyController::IsInitialized()) | 52 if (!chromeos::PowerPolicyController::IsInitialized()) |
| 49 return; | 53 return; |
| 50 | 54 |
| 51 auto* controller = chromeos::PowerPolicyController::Get(); | 55 auto* controller = chromeos::PowerPolicyController::Get(); |
| 52 switch (type_) { | 56 switch (type_) { |
| 53 case kPowerSaveBlockPreventAppSuspension: | 57 case kPowerSaveBlockPreventAppSuspension: |
| 54 block_id_ = controller->AddSystemWakeLock(GetWakeLockReason(reason_), | 58 block_id_ = controller->AddSystemWakeLock(GetWakeLockReason(reason_), |
| 55 description_); | 59 description_); |
| 56 break; | 60 break; |
| 57 case kPowerSaveBlockPreventDisplaySleep: | 61 case kPowerSaveBlockPreventDisplaySleep: |
| 58 block_id_ = controller->AddScreenWakeLock(GetWakeLockReason(reason_), | 62 block_id_ = controller->AddScreenWakeLock(GetWakeLockReason(reason_), |
| 59 description_); | 63 description_); |
| 60 break; | 64 break; |
| 61 default: | 65 default: |
| 62 NOTREACHED() << "Unhandled block type " << type_; | 66 NOTREACHED() << "Unhandled block type " << type_; |
| 63 } | 67 } |
| 64 } | 68 } |
| 65 | 69 |
| 66 void RemoveBlock() { | 70 void RemoveBlock() { |
| 67 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 71 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 68 if (!chromeos::PowerPolicyController::IsInitialized()) | 72 if (!chromeos::PowerPolicyController::IsInitialized()) |
| 69 return; | 73 return; |
| 70 | 74 |
| 71 chromeos::PowerPolicyController::Get()->RemoveWakeLock(block_id_); | 75 chromeos::PowerPolicyController::Get()->RemoveWakeLock(block_id_); |
| 72 } | 76 } |
| 73 | 77 |
| 74 private: | 78 private: |
| 75 friend class base::RefCountedThreadSafe<Delegate>; | 79 friend class base::RefCountedThreadSafe<Delegate>; |
| 76 virtual ~Delegate() {} | 80 virtual ~Delegate() {} |
| 77 | 81 |
| 78 PowerSaveBlockerType type_; | 82 PowerSaveBlockerType type_; |
| 79 Reason reason_; | 83 Reason reason_; |
| 80 std::string description_; | 84 std::string description_; |
| 81 | 85 |
| 82 // ID corresponding to the block request in PowerPolicyController. | 86 // ID corresponding to the block request in PowerPolicyController. |
| 83 int block_id_; | 87 int block_id_; |
| 84 | 88 |
| 89 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; |
| 90 |
| 85 DISALLOW_COPY_AND_ASSIGN(Delegate); | 91 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 86 }; | 92 }; |
| 87 | 93 |
| 88 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, | 94 PowerSaveBlockerImpl::PowerSaveBlockerImpl( |
| 89 Reason reason, | 95 PowerSaveBlockerType type, |
| 90 const std::string& description) | 96 Reason reason, |
| 91 : delegate_(new Delegate(type, reason, description)) { | 97 const std::string& description, |
| 92 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 98 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 93 base::Bind(&Delegate::ApplyBlock, delegate_)); | 99 scoped_refptr<base::SingleThreadTaskRunner> blocking_task_runner) |
| 100 : delegate_(new Delegate(type, reason, description, ui_task_runner)), |
| 101 ui_task_runner_(ui_task_runner), |
| 102 blocking_task_runner_(blocking_task_runner) { |
| 103 ui_task_runner_->PostTask(FROM_HERE, |
| 104 base::Bind(&Delegate::ApplyBlock, delegate_)); |
| 94 } | 105 } |
| 95 | 106 |
| 96 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { | 107 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { |
| 97 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 108 ui_task_runner_->PostTask(FROM_HERE, |
| 98 base::Bind(&Delegate::RemoveBlock, delegate_)); | 109 base::Bind(&Delegate::RemoveBlock, delegate_)); |
| 99 } | 110 } |
| 100 | 111 |
| 101 } // namespace content | 112 } // namespace content |
| OLD | NEW |