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/power_policy_controller.h"
15 #include "content/public/browser/browser_thread.h"
19 class PowerSaveBlockerImpl::Delegate
20 : public base::RefCountedThreadSafe
<PowerSaveBlockerImpl::Delegate
> {
22 Delegate(PowerSaveBlockerType type
, const std::string
& reason
)
28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
29 if (!chromeos::PowerPolicyController::IsInitialized())
32 auto* controller
= chromeos::PowerPolicyController::Get();
34 case kPowerSaveBlockPreventAppSuspension
:
35 block_id_
= controller
->AddSystemWakeLock(reason_
);
37 case kPowerSaveBlockPreventDisplaySleep
:
38 block_id_
= controller
->AddScreenWakeLock(reason_
);
41 NOTREACHED() << "Unhandled block type " << type_
;
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
47 if (!chromeos::PowerPolicyController::IsInitialized())
50 chromeos::PowerPolicyController::Get()->RemoveWakeLock(block_id_
);
54 friend class base::RefCountedThreadSafe
<Delegate
>;
55 virtual ~Delegate() {}
57 PowerSaveBlockerType type_
;
60 // ID corresponding to the block request in PowerPolicyController.
63 DISALLOW_COPY_AND_ASSIGN(Delegate
);
66 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type
,
67 const std::string
& reason
)
68 : delegate_(new Delegate(type
, reason
)) {
69 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
70 base::Bind(&Delegate::ApplyBlock
, delegate_
));
73 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() {
74 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
75 base::Bind(&Delegate::RemoveBlock
, delegate_
));
78 } // namespace content