Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / power_save_blocker_chromeos.cc
blob83c6e8aab2312b9617888c42e54c0e804df665b7
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 namespace {
21 // Converts a PowerSaveBlocker::Reason to a
22 // chromeos::PowerPolicyController::WakeLockReason.
23 chromeos::PowerPolicyController::WakeLockReason GetWakeLockReason(
24 PowerSaveBlocker::Reason reason) {
25 switch (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;
36 } // namespace
38 class PowerSaveBlockerImpl::Delegate
39 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> {
40 public:
41 Delegate(PowerSaveBlockerType type,
42 Reason reason,
43 const std::string& description)
44 : type_(type), reason_(reason), description_(description), block_id_(0) {}
46 void ApplyBlock() {
47 DCHECK_CURRENTLY_ON(BrowserThread::UI);
48 if (!chromeos::PowerPolicyController::IsInitialized())
49 return;
51 auto* controller = chromeos::PowerPolicyController::Get();
52 switch (type_) {
53 case kPowerSaveBlockPreventAppSuspension:
54 block_id_ = controller->AddSystemWakeLock(GetWakeLockReason(reason_),
55 description_);
56 break;
57 case kPowerSaveBlockPreventDisplaySleep:
58 block_id_ = controller->AddScreenWakeLock(GetWakeLockReason(reason_),
59 description_);
60 break;
61 default:
62 NOTREACHED() << "Unhandled block type " << type_;
66 void RemoveBlock() {
67 DCHECK_CURRENTLY_ON(BrowserThread::UI);
68 if (!chromeos::PowerPolicyController::IsInitialized())
69 return;
71 chromeos::PowerPolicyController::Get()->RemoveWakeLock(block_id_);
74 private:
75 friend class base::RefCountedThreadSafe<Delegate>;
76 virtual ~Delegate() {}
78 PowerSaveBlockerType type_;
79 Reason reason_;
80 std::string description_;
82 // ID corresponding to the block request in PowerPolicyController.
83 int block_id_;
85 DISALLOW_COPY_AND_ASSIGN(Delegate);
88 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type,
89 Reason reason,
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