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"
21 // Converts a PowerSaveBlocker::Reason to a
22 // chromeos::PowerPolicyController::WakeLockReason.
23 chromeos::PowerPolicyController::WakeLockReason
GetWakeLockReason(
24 PowerSaveBlocker::Reason reason
) {
26 case PowerSaveBlocker::kReasonAudioPlayback
:
27 return chromeos::PowerPolicyController::REASON_AUDIO_PLAYBACK
;
28 case PowerSaveBlocker::kReasonVideoPlayback
:
29 return chromeos::PowerPolicyController::REASON_VIDEO_PLAYBACK
;
30 case PowerSaveBlocker::kReasonOther
:
31 return chromeos::PowerPolicyController::REASON_OTHER
;
33 return chromeos::PowerPolicyController::REASON_OTHER
;
38 class PowerSaveBlockerImpl::Delegate
39 : public base::RefCountedThreadSafe
<PowerSaveBlockerImpl::Delegate
> {
41 Delegate(PowerSaveBlockerType type
,
43 const std::string
& description
)
44 : type_(type
), reason_(reason
), description_(description
), block_id_(0) {}
47 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
48 if (!chromeos::PowerPolicyController::IsInitialized())
51 auto* controller
= chromeos::PowerPolicyController::Get();
53 case kPowerSaveBlockPreventAppSuspension
:
54 block_id_
= controller
->AddSystemWakeLock(GetWakeLockReason(reason_
),
57 case kPowerSaveBlockPreventDisplaySleep
:
58 block_id_
= controller
->AddScreenWakeLock(GetWakeLockReason(reason_
),
62 NOTREACHED() << "Unhandled block type " << type_
;
67 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
68 if (!chromeos::PowerPolicyController::IsInitialized())
71 chromeos::PowerPolicyController::Get()->RemoveWakeLock(block_id_
);
75 friend class base::RefCountedThreadSafe
<Delegate
>;
76 virtual ~Delegate() {}
78 PowerSaveBlockerType type_
;
80 std::string description_
;
82 // ID corresponding to the block request in PowerPolicyController.
85 DISALLOW_COPY_AND_ASSIGN(Delegate
);
88 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type
,
90 const std::string
& description
)
91 : delegate_(new Delegate(type
, reason
, description
)) {
92 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
93 base::Bind(&Delegate::ApplyBlock
, delegate_
));
96 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() {
97 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
98 base::Bind(&Delegate::RemoveBlock
, delegate_
));
101 } // namespace content