OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/power_save_blocker_impl.h" | 5 #include "content/browser/power_save_blocker_impl.h" |
6 | 6 |
7 #include "base/atomicops.h" | |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "content/browser/android/content_video_view.h" | |
10 #include "content/public/browser/browser_thread.h" | |
8 | 11 |
9 namespace content { | 12 namespace content { |
13 namespace { | |
14 base::subtle::Atomic32 g_blocker_count = 0; | |
15 } // namespace | |
10 | 16 |
11 class PowerSaveBlockerImpl::Delegate | 17 class PowerSaveBlockerImpl::Delegate |
12 : public base::RefCounted<PowerSaveBlockerImpl::Delegate> { | 18 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { |
19 public: | |
20 Delegate(PowerSaveBlockerType type, const std::string& reason) | |
21 : type_(type), reason_(reason) {} | |
22 | |
23 // Does the actual work to apply or remove the desired power save block. | |
24 void ApplyBlock(); | |
25 void RemoveBlock(); | |
26 | |
27 PowerSaveBlockerType type() { return type_; } | |
28 | |
13 private: | 29 private: |
14 friend class base::RefCounted<Delegate>; | 30 friend class base::RefCountedThreadSafe<Delegate>; |
15 ~Delegate() {} | 31 ~Delegate() {} |
32 | |
33 PowerSaveBlockerType type_; | |
34 const std::string reason_; | |
qinmin
2013/05/21 17:18:40
nit: you can remove this, not being used
| |
35 | |
36 DISALLOW_COPY_AND_ASSIGN(Delegate); | |
16 }; | 37 }; |
17 | 38 |
39 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { | |
40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
41 ContentVideoView::KeepScreenOn(true); | |
42 } | |
43 | |
44 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { | |
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
46 ContentVideoView::KeepScreenOn(false); | |
47 } | |
48 | |
18 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, | 49 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, |
19 const std::string& reason) { | 50 const std::string& reason) |
20 // TODO(wangxianzhu): Implement it. | 51 : delegate_(new Delegate(type, reason)) { |
21 // This may be called on any thread. | 52 // This may be called on any thread. |
22 NOTIMPLEMENTED(); | 53 if (type == kPowerSaveBlockPreventDisplaySleep) { |
54 int32 count = base::subtle::NoBarrier_AtomicIncrement(&g_blocker_count, 1); | |
55 DCHECK_GT(count, 0); | |
56 if (count == 1) { | |
qinmin
2013/05/21 14:49:56
hmm...I think you don't need the global here. You
| |
57 BrowserThread::PostTask( | |
58 BrowserThread::UI, FROM_HERE, | |
59 base::Bind(&Delegate::ApplyBlock, delegate_)); | |
60 } | |
61 } | |
23 } | 62 } |
24 | 63 |
25 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { | 64 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { |
65 if (delegate_->type() == kPowerSaveBlockPreventDisplaySleep) { | |
66 int32 count = base::subtle::NoBarrier_AtomicIncrement(&g_blocker_count, -1); | |
67 DCHECK_GE(count, 0); | |
68 if (count == 0) { | |
69 BrowserThread::PostTask( | |
70 BrowserThread::UI, FROM_HERE, | |
71 base::Bind(&Delegate::RemoveBlock, delegate_)); | |
72 } | |
73 } | |
26 } | 74 } |
27 | 75 |
28 } // namespace content | 76 } // namespace content |
OLD | NEW |