OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "content/browser/power_save_blocker.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "content/browser/browser_thread.h" | 8 #include "content/browser/browser_thread.h" |
8 | 9 |
9 // Accessed only from the UI thread. | 10 // Accessed only from the UI thread. |
10 int PowerSaveBlocker::blocker_count_ = 0; | 11 int PowerSaveBlocker::blocker_count_[kPowerSaveBlockPreventStateCount]; |
11 | 12 |
12 PowerSaveBlocker::PowerSaveBlocker(bool enable) : enabled_(false) { | 13 PowerSaveBlocker::PowerSaveBlocker(PowerSaveBlockerType type) : type_(type) { |
13 if (enable) | 14 DCHECK_LT(kPowerSaveBlockPreventNone, type); |
14 Enable(); | 15 DCHECK_GT(kPowerSaveBlockPreventStateCount, type); |
| 16 std::vector<int> change(kPowerSaveBlockPreventStateCount); |
| 17 ++change[type_]; |
| 18 PostAdjustBlockCount(change); |
15 } | 19 } |
16 | 20 |
17 PowerSaveBlocker::~PowerSaveBlocker(void) { | 21 PowerSaveBlocker::~PowerSaveBlocker(void) { |
18 Disable(); | 22 std::vector<int> change(kPowerSaveBlockPreventStateCount); |
| 23 --change[type_]; |
| 24 PostAdjustBlockCount(change); |
19 } | 25 } |
20 | 26 |
21 void PowerSaveBlocker::Enable() { | 27 // static |
22 if (enabled_) | 28 void PowerSaveBlocker::PostAdjustBlockCount(const std::vector<int>& deltas) { |
23 return; | |
24 | |
25 enabled_ = true; | |
26 PostAdjustBlockCount(1); | |
27 } | |
28 | |
29 void PowerSaveBlocker::Disable() { | |
30 if (!enabled_) | |
31 return; | |
32 | |
33 enabled_ = false; | |
34 PostAdjustBlockCount(-1); | |
35 } | |
36 | |
37 | |
38 void PowerSaveBlocker::PostAdjustBlockCount(int delta) { | |
39 BrowserThread::PostTask( | 29 BrowserThread::PostTask( |
40 BrowserThread::UI, FROM_HERE, | 30 BrowserThread::UI, FROM_HERE, |
41 NewRunnableFunction(&PowerSaveBlocker::AdjustBlockCount, delta)); | 31 base::Bind(&PowerSaveBlocker::AdjustBlockCount, deltas)); |
42 } | 32 } |
43 | 33 |
44 // Called only from UI thread. | 34 // Called only from UI thread. |
45 void PowerSaveBlocker::AdjustBlockCount(int delta) { | 35 // static |
| 36 void PowerSaveBlocker::AdjustBlockCount(const std::vector<int>& deltas) { |
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
47 | 38 |
48 bool was_blocking = (blocker_count_ != 0); | 39 PowerSaveBlockerType old_block = HighestBlockType(); |
49 | 40 |
50 blocker_count_ += delta; | 41 for (size_t i = 0; i < deltas.size(); ++i) |
| 42 blocker_count_[i] += deltas[i]; |
51 | 43 |
52 bool is_blocking = (blocker_count_ != 0); | 44 PowerSaveBlockerType new_block = HighestBlockType(); |
53 | 45 |
54 DCHECK_GE(blocker_count_, 0); | 46 if (new_block != old_block) |
| 47 ApplyBlock(new_block); |
| 48 } |
55 | 49 |
56 if (is_blocking != was_blocking) | 50 // Called only from UI thread. |
57 ApplyBlock(is_blocking); | 51 PowerSaveBlocker::PowerSaveBlockerType PowerSaveBlocker::HighestBlockType() { |
| 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 53 |
| 54 for (PowerSaveBlockerType t = kPowerSaveBlockPreventDisplaySleep; |
| 55 t >= kPowerSaveBlockPreventSystemSleep; |
| 56 t = static_cast<PowerSaveBlockerType>(t - 1)) { |
| 57 if (blocker_count_[t]) |
| 58 return t; |
| 59 } |
| 60 |
| 61 return kPowerSaveBlockPreventNone; |
58 } | 62 } |
OLD | NEW |