Index: content/browser/power_save_blocker_common.cc |
diff --git a/content/browser/power_save_blocker_common.cc b/content/browser/power_save_blocker_common.cc |
index 53aecd5d80955d8ef1601f9eb2ccb68fb223a59f..68c69f26190227a3767e047a0e0f374a4827f7b8 100644 |
--- a/content/browser/power_save_blocker_common.cc |
+++ b/content/browser/power_save_blocker_common.cc |
@@ -1,58 +1,62 @@ |
-// Copyright (c) 2009 The Chromium Authors. All rights reserved. |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
#include "content/browser/power_save_blocker.h" |
+#include "base/bind.h" |
#include "content/browser/browser_thread.h" |
// Accessed only from the UI thread. |
-int PowerSaveBlocker::blocker_count_ = 0; |
- |
-PowerSaveBlocker::PowerSaveBlocker(bool enable) : enabled_(false) { |
- if (enable) |
- Enable(); |
+int PowerSaveBlocker::blocker_count_[kPowerSaveBlockPreventStateCount]; |
+ |
+PowerSaveBlocker::PowerSaveBlocker(PowerSaveBlockerType type) : type_(type) { |
+ DCHECK_LT(kPowerSaveBlockPreventNone, type); |
+ DCHECK_GT(kPowerSaveBlockPreventStateCount, type); |
+ std::vector<int> change(kPowerSaveBlockPreventStateCount); |
+ ++change[type_]; |
+ PostAdjustBlockCount(change); |
} |
PowerSaveBlocker::~PowerSaveBlocker(void) { |
- Disable(); |
-} |
- |
-void PowerSaveBlocker::Enable() { |
- if (enabled_) |
- return; |
- |
- enabled_ = true; |
- PostAdjustBlockCount(1); |
-} |
- |
-void PowerSaveBlocker::Disable() { |
- if (!enabled_) |
- return; |
- |
- enabled_ = false; |
- PostAdjustBlockCount(-1); |
+ std::vector<int> change(kPowerSaveBlockPreventStateCount); |
+ --change[type_]; |
+ PostAdjustBlockCount(change); |
} |
- |
-void PowerSaveBlocker::PostAdjustBlockCount(int delta) { |
+// static |
+void PowerSaveBlocker::PostAdjustBlockCount(const std::vector<int>& deltas) { |
BrowserThread::PostTask( |
BrowserThread::UI, FROM_HERE, |
- NewRunnableFunction(&PowerSaveBlocker::AdjustBlockCount, delta)); |
+ base::Bind(&PowerSaveBlocker::AdjustBlockCount, deltas)); |
} |
// Called only from UI thread. |
-void PowerSaveBlocker::AdjustBlockCount(int delta) { |
+// static |
+void PowerSaveBlocker::AdjustBlockCount(const std::vector<int>& deltas) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
- bool was_blocking = (blocker_count_ != 0); |
+ PowerSaveBlockerType old_block = HighestBlockType(); |
+ |
+ for (size_t i = 0; i < deltas.size(); ++i) |
+ blocker_count_[i] += deltas[i]; |
+ |
+ PowerSaveBlockerType new_block = HighestBlockType(); |
- blocker_count_ += delta; |
+ if (new_block != old_block) |
+ ApplyBlock(new_block); |
+} |
- bool is_blocking = (blocker_count_ != 0); |
+// Called only from UI thread. |
+PowerSaveBlocker::PowerSaveBlockerType PowerSaveBlocker::HighestBlockType() { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
- DCHECK_GE(blocker_count_, 0); |
+ for (PowerSaveBlockerType t = kPowerSaveBlockPreventDisplaySleep; |
+ t >= kPowerSaveBlockPreventSystemSleep; |
+ t = static_cast<PowerSaveBlockerType>(t - 1)) { |
+ if (blocker_count_[t]) |
+ return t; |
+ } |
- if (is_blocking != was_blocking) |
- ApplyBlock(is_blocking); |
+ return kPowerSaveBlockPreventNone; |
} |