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.
5 #include "content/browser/power_save_blocker_impl.h"
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"
20 class PowerSaveBlockerImpl::Delegate
21 : public base::RefCountedThreadSafe
<PowerSaveBlockerImpl::Delegate
> {
23 Delegate(PowerSaveBlockerType type
, const std::string
& reason
)
29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
30 if (!chromeos::DBusThreadManager::IsInitialized())
33 chromeos::PowerPolicyController
* controller
=
34 chromeos::DBusThreadManager::Get()->GetPowerPolicyController();
36 case kPowerSaveBlockPreventAppSuspension
:
37 block_id_
= controller
->AddSystemWakeLock(reason_
);
39 case kPowerSaveBlockPreventDisplaySleep
:
40 block_id_
= controller
->AddScreenWakeLock(reason_
);
43 NOTREACHED() << "Unhandled block type " << type_
;
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
49 if (!chromeos::DBusThreadManager::IsInitialized())
52 chromeos::DBusThreadManager::Get()->GetPowerPolicyController()->
53 RemoveWakeLock(block_id_
);
57 friend class base::RefCountedThreadSafe
<Delegate
>;
58 virtual ~Delegate() {}
60 PowerSaveBlockerType type_
;
63 // ID corresponding to the block request in PowerPolicyController.
66 DISALLOW_COPY_AND_ASSIGN(Delegate
);
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_
));
76 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() {
77 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
78 base::Bind(&Delegate::RemoveBlock
, delegate_
));
81 } // namespace content