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/logging.h" | 7 #include "base/logging.h" |
| 8 #include "content/browser/android/content_video_view.h" |
| 9 #include "content/public/browser/browser_thread.h" |
8 | 10 |
9 namespace content { | 11 namespace content { |
10 | 12 |
11 class PowerSaveBlockerImpl::Delegate | 13 class PowerSaveBlockerImpl::Delegate |
12 : public base::RefCounted<PowerSaveBlockerImpl::Delegate> { | 14 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { |
| 15 public: |
| 16 explicit Delegate(PowerSaveBlockerType type) : type_(type) {} |
| 17 |
| 18 // Does the actual work to apply or remove the desired power save block. |
| 19 void ApplyBlock(); |
| 20 void RemoveBlock(); |
| 21 |
13 private: | 22 private: |
14 friend class base::RefCounted<Delegate>; | 23 friend class base::RefCountedThreadSafe<Delegate>; |
15 ~Delegate() {} | 24 ~Delegate() {} |
| 25 |
| 26 // The counter of requests from clients for type |
| 27 // kPowerSaveBlockPreventDisplaySleep. |
| 28 static int blocker_count_; |
| 29 const PowerSaveBlockerType type_; |
| 30 |
| 31 DISALLOW_COPY_AND_ASSIGN(Delegate); |
16 }; | 32 }; |
17 | 33 |
| 34 int PowerSaveBlockerImpl::Delegate::blocker_count_ = 0; |
| 35 |
| 36 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { |
| 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 38 if (type_ != kPowerSaveBlockPreventDisplaySleep) |
| 39 return; |
| 40 |
| 41 if (blocker_count_ == 0) |
| 42 ContentVideoView::KeepScreenOn(true); |
| 43 ++blocker_count_; |
| 44 } |
| 45 |
| 46 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { |
| 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 48 if (type_ != kPowerSaveBlockPreventDisplaySleep) |
| 49 return; |
| 50 |
| 51 --blocker_count_; |
| 52 if (blocker_count_ == 0) |
| 53 ContentVideoView::KeepScreenOn(false); |
| 54 } |
| 55 |
18 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, | 56 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, |
19 const std::string& reason) { | 57 const std::string& reason) |
20 // TODO(wangxianzhu): Implement it. | 58 : delegate_(new Delegate(type)) { |
21 // This may be called on any thread. | 59 // This may be called on any thread. |
22 NOTIMPLEMENTED(); | 60 BrowserThread::PostTask( |
| 61 BrowserThread::UI, FROM_HERE, |
| 62 base::Bind(&Delegate::ApplyBlock, delegate_)); |
23 } | 63 } |
24 | 64 |
25 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { | 65 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { |
| 66 BrowserThread::PostTask( |
| 67 BrowserThread::UI, FROM_HERE, |
| 68 base::Bind(&Delegate::RemoveBlock, delegate_)); |
26 } | 69 } |
27 | 70 |
28 } // namespace content | 71 } // namespace content |
OLD | NEW |