Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / power_save_blocker_chromeos.cc
blobae99975b4d8ea04e5f248254c071ae45becfa1e3
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"
7 #include <string>
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"
17 namespace content {
19 class PowerSaveBlockerImpl::Delegate
20 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> {
21 public:
22 Delegate(PowerSaveBlockerType type, const std::string& reason)
23 : type_(type),
24 reason_(reason),
25 block_id_(0) {}
27 void ApplyBlock() {
28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
29 if (!chromeos::PowerPolicyController::IsInitialized())
30 return;
32 auto* controller = chromeos::PowerPolicyController::Get();
33 switch (type_) {
34 case kPowerSaveBlockPreventAppSuspension:
35 block_id_ = controller->AddSystemWakeLock(reason_);
36 break;
37 case kPowerSaveBlockPreventDisplaySleep:
38 block_id_ = controller->AddScreenWakeLock(reason_);
39 break;
40 default:
41 NOTREACHED() << "Unhandled block type " << type_;
45 void RemoveBlock() {
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
47 if (!chromeos::PowerPolicyController::IsInitialized())
48 return;
50 chromeos::PowerPolicyController::Get()->RemoveWakeLock(block_id_);
53 private:
54 friend class base::RefCountedThreadSafe<Delegate>;
55 virtual ~Delegate() {}
57 PowerSaveBlockerType type_;
58 std::string reason_;
60 // ID corresponding to the block request in PowerPolicyController.
61 int block_id_;
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