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 "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "chromeos/power/power_state_override.h"
12 #include "content/public/browser/browser_thread.h"
16 class PowerSaveBlockerImpl::Delegate
17 : public base::RefCountedThreadSafe
<PowerSaveBlockerImpl::Delegate
> {
19 Delegate(PowerSaveBlockerType type
) : type_(type
) {}
21 // Creates a PowerStateOverride object to override the default power
22 // management behavior.
24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
25 chromeos::PowerStateOverride::Mode mode
=
26 chromeos::PowerStateOverride::BLOCK_SYSTEM_SUSPEND
;
28 case kPowerSaveBlockPreventAppSuspension
:
29 mode
= chromeos::PowerStateOverride::BLOCK_SYSTEM_SUSPEND
;
31 case kPowerSaveBlockPreventDisplaySleep
:
32 mode
= chromeos::PowerStateOverride::BLOCK_DISPLAY_SLEEP
;
35 NOTREACHED() << "Unhandled block type " << type_
;
37 override_
= new chromeos::PowerStateOverride(mode
);
40 // Resets the previously-created PowerStateOverride object.
42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
47 friend class base::RefCountedThreadSafe
<Delegate
>;
48 virtual ~Delegate() {}
50 PowerSaveBlockerType type_
;
52 scoped_refptr
<chromeos::PowerStateOverride
> override_
;
54 DISALLOW_COPY_AND_ASSIGN(Delegate
);
57 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type
,
58 const std::string
& reason
)
59 : delegate_(new Delegate(type
)) {
60 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
61 base::Bind(&Delegate::ApplyBlock
, delegate_
));
64 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() {
65 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
66 base::Bind(&Delegate::RemoveBlock
, delegate_
));
69 } // namespace content