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