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