0
Review URL: http://codereview.chromium.org/13009

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6127 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
mbelshe@google.com
2008-12-01 00:52:40 +00:00
parent 4f470ec4e0
commit 3abd6bbdfb
15 changed files with 154 additions and 104 deletions

@ -20,10 +20,8 @@ class SystemMonitor {
return Singleton<SystemMonitor>::get();
}
// Start the System Monitor within a process. This method
// is provided so that the battery check can be deferred.
// The MessageLoop must be started before calling this
// method.
// To start the System Monitor within an application
// use this call.
static void Start();
//

@ -2262,6 +2262,14 @@
RelativePath=".\ssl_blocking_page.h"
>
</File>
<File
RelativePath=".\suspend_controller.cc"
>
</File>
<File
RelativePath=".\suspend_controller.h"
>
</File>
<File
RelativePath=".\toolbar_model.cc"
>

@ -16,7 +16,6 @@
#include "base/registry.h"
#include "base/string_piece.h"
#include "base/string_util.h"
#include "base/system_monitor.h"
#include "base/tracked_objects.h"
#include "base/win_util.h"
#include "chrome/app/result_codes.h"
@ -289,9 +288,6 @@ int BrowserMain(CommandLine &parsed_command_line,
MessageLoop main_message_loop(MessageLoop::TYPE_UI);
// Initialize the SystemMonitor
base::SystemMonitor::Start();
// Initialize statistical testing infrastructure.
FieldTrialList field_trial;

@ -122,6 +122,8 @@ class BrowserProcess {
virtual MemoryModel memory_model() = 0;
virtual SuspendController* suspend_controller() = 0;
#if defined(OS_WIN)
DownloadRequestManager* download_request_manager() {
ResourceDispatcherHost* rdh = resource_dispatcher_host();

@ -22,6 +22,7 @@
#include "chrome/browser/resource_dispatcher_host.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "chrome/browser/debugger/debugger_wrapper.h"
#include "chrome/browser/suspend_controller.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/clipboard_service.h"
@ -127,6 +128,8 @@ BrowserProcessImpl::BrowserProcessImpl(CommandLine& command_line)
memory_model_ = MEDIUM_MEMORY_MODEL;
}
suspend_controller_ = new SuspendController();
shutdown_event_ = ::CreateEvent(NULL, TRUE, FALSE, NULL);
}

@ -167,6 +167,11 @@ class BrowserProcessImpl : public BrowserProcess, public NonThreadSafe {
return memory_model_;
}
virtual SuspendController* suspend_controller() {
DCHECK(CalledOnValidThread());
return suspend_controller_.get();
}
virtual HANDLE shutdown_event() { return shutdown_event_; }
private:
@ -238,6 +243,8 @@ class BrowserProcessImpl : public BrowserProcess, public NonThreadSafe {
MemoryModel memory_model_;
scoped_refptr<SuspendController> suspend_controller_;
bool checked_for_new_frames_;
bool using_new_frames_;

@ -21,8 +21,6 @@
#include "chrome/common/logging_chrome.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
#include "net/url_request/url_request_job.h"
#include "net/url_request/url_request_job_tracker.h"
#include "generated_resources.h"
@ -41,16 +39,9 @@ void ProfileManager::ShutdownSessionServices() {
}
ProfileManager::ProfileManager() {
base::SystemMonitor* monitor = base::SystemMonitor::Get();
if (monitor)
monitor->AddObserver(this);
}
ProfileManager::~ProfileManager() {
base::SystemMonitor* monitor = base::SystemMonitor::Get();
if (monitor)
monitor->RemoveObserver(this);
// Destroy all profiles that we're keeping track of.
for (ProfileVector::const_iterator iter = profiles_.begin();
iter != profiles_.end(); ++iter) {
@ -224,47 +215,6 @@ Profile* ProfileManager::GetProfileByID(const std::wstring& id) const {
return NULL;
}
void ProfileManager::OnSuspend(base::SystemMonitor* monitor) {
DCHECK(CalledOnValidThread());
ProfileManager::const_iterator it = begin();
while(it != end()) {
g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
NewRunnableFunction(&ProfileManager::SuspendProfile, *it));
it++;
}
}
void ProfileManager::OnResume(base::SystemMonitor* monitor) {
DCHECK(CalledOnValidThread());
ProfileManager::const_iterator it = begin();
while (it != end()) {
g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
NewRunnableFunction(&ProfileManager::ResumeProfile, *it));
it++;
}
}
void ProfileManager::SuspendProfile(Profile* profile) {
DCHECK(profile);
DCHECK(MessageLoop::current() ==
ChromeThread::GetMessageLoop(ChromeThread::IO));
URLRequestJobTracker::JobIterator it = g_url_request_job_tracker.begin();
for (;it != g_url_request_job_tracker.end(); ++it)
(*it)->Kill();
profile->GetRequestContext()->http_transaction_factory()->Suspend(true);
}
void ProfileManager::ResumeProfile(Profile* profile) {
DCHECK(profile);
DCHECK(MessageLoop::current() ==
ChromeThread::GetMessageLoop(ChromeThread::IO));
profile->GetRequestContext()->http_transaction_factory()->Suspend(false);
}
// static
bool ProfileManager::IsProfile(const std::wstring& path) {

@ -12,9 +12,6 @@
#include <vector>
#include "base/basictypes.h"
#include "base/message_loop.h"
#include "base/non_thread_safe.h"
#include "base/system_monitor.h"
#include "base/values.h"
#include "chrome/browser/profile.h"
@ -60,8 +57,7 @@ class AvailableProfile {
DISALLOW_EVIL_CONSTRUCTORS(AvailableProfile);
};
class ProfileManager : public NonThreadSafe,
public base::SystemMonitor::PowerObserver {
class ProfileManager {
public:
ProfileManager();
virtual ~ProfileManager();
@ -126,11 +122,6 @@ class ProfileManager : public NonThreadSafe,
// Creates a new window with the given profile.
void NewWindowWithProfile(Profile* profile);
// PowerObserver notifications
void OnPowerStateChange(base::SystemMonitor*) {}
void OnSuspend(base::SystemMonitor*);
void OnResume(base::SystemMonitor*);
// ------------------ static utility functions -------------------
// Returns the path to the profile directory based on the user data directory.
@ -166,11 +157,6 @@ class ProfileManager : public NonThreadSafe,
// or NULL if no match is found.
AvailableProfile* GetAvailableProfileByID(const std::wstring& id);
// Hooks to suspend/resume per-profile network traffic.
// These must be called on the IO thread.
static void SuspendProfile(Profile*);
static void ResumeProfile(Profile*);
// We keep a simple vector of profiles rather than something fancier
// because we expect there to be a small number of profiles active.
ProfileVector profiles_;

@ -12,7 +12,6 @@
#include "base/path_service.h"
#include "base/string_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/profile_manager.h"
#include "chrome/browser/safe_browsing/protocol_manager.h"
#include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h"
@ -35,16 +34,9 @@ SafeBrowsingService::SafeBrowsingService()
resetting_(false),
database_loaded_(false) {
new_safe_browsing_ = !CommandLine().HasSwitch(switches::kUseOldSafeBrowsing);
base::SystemMonitor* monitor = base::SystemMonitor::Get();
DCHECK(monitor);
if (monitor)
monitor->AddObserver(this);
}
SafeBrowsingService::~SafeBrowsingService() {
base::SystemMonitor* monitor = base::SystemMonitor::Get();
if (monitor)
monitor->RemoveObserver(this);
}
// Only called on the UI thread.
@ -670,17 +662,18 @@ void SafeBrowsingService::CacheHashResults(
GetDatabase()->CacheHashResults(prefixes, full_hashes);
}
void SafeBrowsingService::OnSuspend(base::SystemMonitor*) {
void SafeBrowsingService::OnSuspend() {
}
// Tell the SafeBrowsing database not to do expensive disk operations for a few
// minutes after waking up. It's quite likely that the act of resuming from a
// low power state will involve much disk activity, which we don't want to
// exacerbate.
void SafeBrowsingService::OnResume(base::SystemMonitor*) {
void SafeBrowsingService::OnResume() {
DCHECK(MessageLoop::current() == io_loop_);
if (enabled_) {
ChromeThread::GetMessageLoop(ChromeThread::DB)->PostTask(FROM_HERE,
NewRunnableMethod(this, &SafeBrowsingService::HandleResume));
db_thread_->message_loop()->PostTask(FROM_HERE,
NewRunnableMethod(this, &SafeBrowsingService::HandleResume));
}
}

@ -16,7 +16,6 @@
#include "base/hash_tables.h"
#include "base/ref_counted.h"
#include "base/scoped_ptr.h"
#include "base/system_monitor.h"
#include "base/thread.h"
#include "base/time.h"
#include "chrome/browser/safe_browsing/safe_browsing_util.h"
@ -32,8 +31,7 @@ class SafeBrowsingProtocolManager;
// Construction needs to happen on the main thread.
class SafeBrowsingService
: public base::RefCountedThreadSafe<SafeBrowsingService>,
public base::SystemMonitor::PowerObserver {
: public base::RefCountedThreadSafe<SafeBrowsingService> {
public:
// Users of this service implement this interface to be notified
// asynchronously of the result.
@ -171,12 +169,10 @@ class SafeBrowsingService
// the current page is 'safe'.
void LogPauseDelay(base::TimeDelta time);
// PowerObserver notifications
// We defer SafeBrowsing work for a short duration when the computer comes
// out of a suspend state to avoid thrashing the disk.
void OnPowerStateChange(base::SystemMonitor*) {};
void OnSuspend(base::SystemMonitor*);
void OnResume(base::SystemMonitor*);
void OnSuspend();
void OnResume();
bool new_safe_browsing() const { return new_safe_browsing_; }

@ -0,0 +1,83 @@
// Copyright (c) 2006-2008 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/suspend_controller.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/task.h"
#include "base/thread.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/resource_dispatcher_host.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "net/url_request/url_request_job.h"
#include "net/url_request/url_request_job_tracker.h"
// Static member.
bool SuspendController::is_suspended_ = false;
// Static method.
void SuspendController::OnSuspend(Profile* profile) {
if (is_suspended_)
return;
is_suspended_ = true;
LOG(INFO) << "Received APM suspend message";
SuspendController* controller = g_browser_process->suspend_controller();
MessageLoop* io_loop = g_browser_process->io_thread()->message_loop();
io_loop->PostTask(FROM_HERE,
NewRunnableMethod(controller,
&SuspendController::StopRequests,
profile));
ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host();
SafeBrowsingService* safe_browsing_service = rdh->safe_browsing_service();
io_loop->PostTask(FROM_HERE,
NewRunnableMethod(safe_browsing_service,
&SafeBrowsingService::OnSuspend));
}
// Static method.
void SuspendController::OnResume(Profile* profile) {
if (!is_suspended_)
return;
is_suspended_ = false;
LOG(INFO) << "Received APM resume message";
SuspendController* controller = g_browser_process->suspend_controller();
MessageLoop* io_loop = g_browser_process->io_thread()->message_loop();
io_loop->PostTask(FROM_HERE,
NewRunnableMethod(controller,
&SuspendController::AllowNewRequests,
profile));
ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host();
SafeBrowsingService* safe_browsing_service = rdh->safe_browsing_service();
io_loop->PostTask(FROM_HERE,
NewRunnableMethod(safe_browsing_service,
&SafeBrowsingService::OnResume));
}
void SuspendController::StopRequests(Profile* profile) {
// Cancel all requests and stop creating new ones.
URLRequestJobTracker::JobIterator it = g_url_request_job_tracker.begin();
for (;it != g_url_request_job_tracker.end(); ++it) {
URLRequestJob* job = const_cast<URLRequestJob*>(*it);
job->Kill();
}
// Close the network session.
profile->GetRequestContext()->http_transaction_factory()->Suspend(true);
}
void SuspendController::AllowNewRequests(Profile* profile) {
// Re-enable the network session.
profile->GetRequestContext()->http_transaction_factory()->Suspend(false);
}

@ -0,0 +1,40 @@
// Copyright (c) 2006-2008 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.
// The SuspendController keeps track of actions related to the computer going
// into power savings mode. Its purpose right now is to close all network
// requests and prevent creation of new requests until the computer resumes.
#ifndef CHROME_BROWSER_SUSPEND_CONTROLLER_H__
#define CHROME_BROWSER_SUSPEND_CONTROLLER_H__
#include "base/basictypes.h"
#include "chrome/browser/profile.h"
class Profile;
// The browser process owns the only instance of this class.
class SuspendController : public base::RefCountedThreadSafe<SuspendController> {
public:
SuspendController() {}
~SuspendController() {}
// Called when the system is going to be suspended.
static void OnSuspend(Profile* profile);
// Called when the system has been resumed.
static void OnResume(Profile* profile);
private:
// Run on the io_thread.
void StopRequests(Profile* profile);
void AllowNewRequests(Profile* profile);
static bool is_suspended_;
DISALLOW_EVIL_CONSTRUCTORS(SuspendController);
};
#endif // CHROME_BROWSER_SUSPEND_CONTROLLER_H__

@ -4,7 +4,6 @@
#include "base/command_line.h"
#include "base/message_loop.h"
#include "base/system_monitor.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/logging_chrome.h"
@ -21,9 +20,6 @@ int PluginMain(CommandLine &parsed_command_line,
std::wstring app_name = chrome::kBrowserAppName;
PlatformThread::SetName(WideToASCII(app_name + L"_PluginMain").c_str());
// Initialize the SystemMonitor
base::SystemMonitor::Start();
CoInitialize(NULL);
DLOG(INFO) << "Started plugin with " <<
parsed_command_line.command_line_string();

@ -7,7 +7,6 @@
#include "base/path_service.h"
#include "base/platform_thread.h"
#include "base/string_util.h"
#include "base/system_monitor.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_counters.h"
#include "chrome/common/chrome_switches.h"
@ -54,9 +53,6 @@ int RendererMain(CommandLine &parsed_command_line,
std::wstring app_name = chrome::kBrowserAppName;
PlatformThread::SetName(WideToASCII(app_name + L"_RendererMain").c_str());
// Initialize the SystemMonitor
base::SystemMonitor::Start();
CoInitialize(NULL);
DLOG(INFO) << "Started renderer with " <<

@ -9,7 +9,6 @@
#include <atlcrack.h>
#include "base/message_loop.h"
#include "base/system_monitor.h"
#include "chrome/views/focus_manager.h"
#include "chrome/views/layout_manager.h"
#include "chrome/views/widget.h"
@ -411,9 +410,6 @@ class WidgetWin : public Widget,
virtual LRESULT OnNotify(int w_param, NMHDR* l_param);
virtual void OnPaint(HDC dc);
virtual LRESULT OnPowerBroadcast(DWORD power_event, DWORD data) {
base::SystemMonitor* monitor = base::SystemMonitor::Get();
if (monitor)
monitor->ProcessWmPowerBroadcastMessage(power_event);
SetMsgHandled(FALSE);
return 0;
}