summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authormark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-23 04:18:05 +0000
committermark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-23 04:18:05 +0000
commit36e93a7796e1551fafeaaed832f166a2a6d9bb2b (patch)
tree6653c08fae8302920cc2bae5c5a67a0b1f586bcc /chrome/browser
parentb384d90d7713768c2eb9191ea0d2410f9c9ac0fc (diff)
downloadchromium_src-36e93a7796e1551fafeaaed832f166a2a6d9bb2b.zip
chromium_src-36e93a7796e1551fafeaaed832f166a2a6d9bb2b.tar.gz
chromium_src-36e93a7796e1551fafeaaed832f166a2a6d9bb2b.tar.bz2
Disable system suspend while downloading files on win32.
If the system goes into power-save sleep mode while downloading files, the download fails. So, prevent sleep mode until the download finishes. This patch introduces a new RAII class to request that the system's power-save mode be disabled - PowerSaveBlocker. This is only implemented for win32; other platforms are stubbed out. This only partially implements bug 25420 it only attempts to handle the downloading case. Patch by Bryan Donlan <bdonlan@gmail.com> BUG=25420 TEST=Download a large file with the system sleep timeout set to a short interval. Review URL: http://codereview.chromium.org/287017 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29873 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/download/download_file.cc3
-rw-r--r--chrome/browser/download/download_file.h4
-rw-r--r--chrome/browser/power_save_blocker.h42
-rw-r--r--chrome/browser/power_save_blocker_common.cc58
-rw-r--r--chrome/browser/power_save_blocker_stub.cc13
-rw-r--r--chrome/browser/power_save_blocker_win.cc21
6 files changed, 140 insertions, 1 deletions
diff --git a/chrome/browser/download/download_file.cc b/chrome/browser/download/download_file.cc
index 7066b5b..b340d00 100644
--- a/chrome/browser/download/download_file.cc
+++ b/chrome/browser/download/download_file.cc
@@ -65,7 +65,8 @@ DownloadFile::DownloadFile(const DownloadCreateInfo* info)
request_id_(info->request_id),
bytes_so_far_(0),
path_renamed_(false),
- in_progress_(true) {
+ in_progress_(true),
+ dont_sleep_(true) {
}
DownloadFile::~DownloadFile() {
diff --git a/chrome/browser/download/download_file.h b/chrome/browser/download/download_file.h
index 79195ec..66d3e7c 100644
--- a/chrome/browser/download/download_file.h
+++ b/chrome/browser/download/download_file.h
@@ -51,6 +51,7 @@
#include "base/lock.h"
#include "base/ref_counted.h"
#include "base/timer.h"
+#include "chrome/browser/power_save_blocker.h"
#include "googleurl/src/gurl.h"
namespace net {
@@ -153,6 +154,9 @@ class DownloadFile {
// Whether the download is still receiving data.
bool in_progress_;
+ // RAII handle to keep the system from sleeping while we're downloading.
+ PowerSaveBlocker dont_sleep_;
+
DISALLOW_COPY_AND_ASSIGN(DownloadFile);
};
diff --git a/chrome/browser/power_save_blocker.h b/chrome/browser/power_save_blocker.h
new file mode 100644
index 0000000..f362915
--- /dev/null
+++ b/chrome/browser/power_save_blocker.h
@@ -0,0 +1,42 @@
+// Copyright (c) 2009 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.
+
+#ifndef CHROME_BROWSER_POWER_SAVE_BLOCKER_H_
+#define CHROME_BROWSER_POWER_SAVE_BLOCKER_H_
+
+#include "base/basictypes.h"
+
+// A RAII-style class to block the system from entering low-power
+// (sleep) mode.
+class PowerSaveBlocker {
+ public:
+ explicit PowerSaveBlocker(bool enabled);
+ ~PowerSaveBlocker();
+
+ bool enabled() const { return enabled_; }
+
+ // Puts the sleep mode block into effect.
+ void Enable();
+ // Disables the sleep block.
+ void Disable();
+
+ private:
+ // Platform-specific function called when enable state is changed.
+ // Guarenteed to be called only from the UI thread.
+ static void ApplyBlock(bool blocked);
+
+ // Called only from UI thread.
+ static void AdjustBlockCount(int delta);
+
+ // Invokes AdjustBlockCount on the UI thread.
+ static void PostAdjustBlockCount(int delta);
+
+ bool enabled_;
+
+ static int blocker_count_;
+
+ DISALLOW_COPY_AND_ASSIGN(PowerSaveBlocker);
+};
+
+#endif // CHROME_BROWSER_POWER_SAVE_BLOCKER_H_
diff --git a/chrome/browser/power_save_blocker_common.cc b/chrome/browser/power_save_blocker_common.cc
new file mode 100644
index 0000000..b6ff2ac
--- /dev/null
+++ b/chrome/browser/power_save_blocker_common.cc
@@ -0,0 +1,58 @@
+// Copyright (c) 2009 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 "chrome/browser/power_save_blocker.h"
+#include "chrome/browser/chrome_thread.h"
+
+// Accessed only from the UI thread.
+int PowerSaveBlocker::blocker_count_ = 0;
+
+PowerSaveBlocker::PowerSaveBlocker(bool enable) : enabled_(false) {
+ if (enable)
+ Enable();
+}
+
+PowerSaveBlocker::~PowerSaveBlocker(void) {
+ Disable();
+}
+
+void PowerSaveBlocker::Enable() {
+ if (enabled_)
+ return;
+
+ enabled_ = true;
+ PostAdjustBlockCount(1);
+}
+
+void PowerSaveBlocker::Disable() {
+ if (!enabled_)
+ return;
+
+ enabled_ = false;
+ PostAdjustBlockCount(-1);
+}
+
+
+void PowerSaveBlocker::PostAdjustBlockCount(int delta) {
+ MessageLoop *ml = ChromeThread::GetMessageLoop(ChromeThread::UI);
+
+ ml->PostTask(FROM_HERE,
+ NewRunnableFunction(&PowerSaveBlocker::AdjustBlockCount, delta));
+}
+
+// Called only from UI thread.
+void PowerSaveBlocker::AdjustBlockCount(int delta) {
+ DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
+
+ bool was_blocking = (blocker_count_ != 0);
+
+ blocker_count_ += delta;
+
+ bool is_blocking = (blocker_count_ != 0);
+
+ DCHECK_GE(blocker_count_, 0);
+
+ if (is_blocking != was_blocking)
+ ApplyBlock(is_blocking);
+}
diff --git a/chrome/browser/power_save_blocker_stub.cc b/chrome/browser/power_save_blocker_stub.cc
new file mode 100644
index 0000000..3c31f14
--- /dev/null
+++ b/chrome/browser/power_save_blocker_stub.cc
@@ -0,0 +1,13 @@
+// Copyright (c) 2009 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 "chrome/browser/power_save_blocker.h"
+
+#include "base/logging.h"
+
+// Default, stub implementation, for platforms that don't have their own yet.
+
+void PowerSaveBlocker::ApplyBlock(bool blocking) {
+ NOTIMPLEMENTED();
+}
diff --git a/chrome/browser/power_save_blocker_win.cc b/chrome/browser/power_save_blocker_win.cc
new file mode 100644
index 0000000..f7d3d8c
--- /dev/null
+++ b/chrome/browser/power_save_blocker_win.cc
@@ -0,0 +1,21 @@
+// Copyright (c) 2009 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 "chrome/browser/power_save_blocker.h"
+
+#include <windows.h>
+
+#include "chrome/browser/chrome_thread.h"
+
+// Runs on UI thread only.
+void PowerSaveBlocker::ApplyBlock(bool blocking) {
+ DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
+
+ DWORD flags = ES_CONTINUOUS;
+
+ if (blocking)
+ flags |= ES_SYSTEM_REQUIRED;
+
+ SetThreadExecutionState(flags);
+}