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