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/basictypes.h" 10 #include "base/bind.h" 11 #include "base/location.h" 12 #include "base/logging.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" 17 18 namespace content { 19 20 class PowerSaveBlockerImpl::Delegate 21 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { 22 public: 23 Delegate(PowerSaveBlockerType type, const std::string& reason) 24 : type_(type), 25 reason_(reason), 26 block_id_(0) {} 27 28 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 } 48 49 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 } 58 59 private: 60 friend class base::RefCountedThreadSafe<Delegate>; 61 virtual ~Delegate() {} 62 63 PowerSaveBlockerType type_; 64 std::string reason_; 65 66 // ID corresponding to the block request in PowerPolicyController. 67 int block_id_; 68 69 DISALLOW_COPY_AND_ASSIGN(Delegate); 70 }; 71 72 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, 73 const std::string& reason) 74 : delegate_(new Delegate(type, reason)) { 75 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 76 base::Bind(&Delegate::ApplyBlock, delegate_)); 77 } 78 79 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { 80 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 81 base::Bind(&Delegate::RemoveBlock, delegate_)); 82 } 83 84 } // namespace content 85