Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / content / browser / power_save_blocker_chromeos.cc
blobf9a6722f8d284e4d57863f60f6c4162feff705a4
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/dbus_thread_manager.h"
15 #include "chromeos/dbus/power_policy_controller.h"
16 #include "content/public/browser/browser_thread.h"
18 namespace content {
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) {}
28 void ApplyBlock() {
29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
30 if (!chromeos::DBusThreadManager::IsInitialized())
31 return;
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_;
47 void RemoveBlock() {
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
49 if (!chromeos::DBusThreadManager::IsInitialized())
50 return;
52 chromeos::DBusThreadManager::Get()->GetPowerPolicyController()->
53 RemoveWakeLock(block_id_);
56 private:
57 friend class base::RefCountedThreadSafe<Delegate>;
58 virtual ~Delegate() {}
60 PowerSaveBlockerType type_;
61 std::string reason_;
63 // ID corresponding to the block request in PowerPolicyController.
64 int block_id_;
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