Home | History | Annotate | Download | only in browser
      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       return;
     32 
     33     chromeos::PowerPolicyController* controller =
     34         chromeos::DBusThreadManager::Get()->GetPowerPolicyController();
     35     switch (type_) {
     36       case kPowerSaveBlockPreventAppSuspension:
     37         block_id_ = controller->AddSystemWakeLock(reason_);
     38         break;
     39       case kPowerSaveBlockPreventDisplaySleep:
     40         block_id_ = controller->AddScreenWakeLock(reason_);
     41         break;
     42       default:
     43         NOTREACHED() << "Unhandled block type " << type_;
     44     }
     45   }
     46 
     47   void RemoveBlock() {
     48     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     49     if (!chromeos::DBusThreadManager::IsInitialized())
     50       return;
     51 
     52     chromeos::DBusThreadManager::Get()->GetPowerPolicyController()->
     53         RemoveWakeLock(block_id_);
     54   }
     55 
     56  private:
     57   friend class base::RefCountedThreadSafe<Delegate>;
     58   virtual ~Delegate() {}
     59 
     60   PowerSaveBlockerType type_;
     61   std::string reason_;
     62 
     63   // ID corresponding to the block request in PowerPolicyController.
     64   int block_id_;
     65 
     66   DISALLOW_COPY_AND_ASSIGN(Delegate);
     67 };
     68 
     69 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type,
     70                                            const std::string& reason)
     71     : delegate_(new Delegate(type, reason)) {
     72   BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
     73                           base::Bind(&Delegate::ApplyBlock, delegate_));
     74 }
     75 
     76 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() {
     77   BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
     78                           base::Bind(&Delegate::RemoveBlock, delegate_));
     79 }
     80 
     81 }  // namespace content
     82