diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-14 21:22:31 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-14 21:22:31 +0000 |
commit | 08110ceae09a5e148e67bebd712e7ba1840ccf42 (patch) | |
tree | 94bdbbc82317bc6f7579d552aace6becd1beaab2 /app | |
parent | a61c5c9bb6524d77c30b6a18f542c0e4616ee7bc (diff) | |
download | chromium_src-08110ceae09a5e148e67bebd712e7ba1840ccf42.zip chromium_src-08110ceae09a5e148e67bebd712e7ba1840ccf42.tar.gz chromium_src-08110ceae09a5e148e67bebd712e7ba1840ccf42.tar.bz2 |
Move SystemMonitor to src/ui/base/system_monitor.
Move HiResTimerManager to src/chrome/common.
BUG=none
TEST=none
TBR=brettw
Review URL: http://codereview.chromium.org/6361002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@71487 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app')
-rw-r--r-- | app/app.gyp | 2 | ||||
-rw-r--r-- | app/app_base.gypi | 13 | ||||
-rw-r--r-- | app/hi_res_timer_manager.h | 30 | ||||
-rw-r--r-- | app/hi_res_timer_manager_posix.cc | 20 | ||||
-rw-r--r-- | app/hi_res_timer_manager_win.cc | 29 | ||||
-rw-r--r-- | app/system_monitor.cc | 104 | ||||
-rw-r--r-- | app/system_monitor.h | 139 | ||||
-rw-r--r-- | app/system_monitor_mac.mm | 67 | ||||
-rw-r--r-- | app/system_monitor_posix.cc | 10 | ||||
-rw-r--r-- | app/system_monitor_unittest.cc | 87 | ||||
-rw-r--r-- | app/system_monitor_win.cc | 46 |
11 files changed, 6 insertions, 541 deletions
diff --git a/app/app.gyp b/app/app.gyp index 09231358..de566ee 100644 --- a/app/app.gyp +++ b/app/app.gyp @@ -47,6 +47,7 @@ '../ui/base/dragdrop/os_exchange_data_win_unittest.cc', '../ui/base/models/tree_node_iterator_unittest.cc', '../ui/base/models/tree_node_model_unittest.cc', + '../ui/base/system_monitor/system_monitor_unittest.cc', 'data_pack_unittest.cc', 'l10n_util_mac_unittest.mm', 'l10n_util_unittest.cc', @@ -54,7 +55,6 @@ 'sql/connection_unittest.cc', 'sql/statement_unittest.cc', 'sql/transaction_unittest.cc', - 'system_monitor_unittest.cc', 'test_suite.h', 'test/data/resource.h', 'text_elider_unittest.cc', diff --git a/app/app_base.gypi b/app/app_base.gypi index 126d04d..c35847f 100644 --- a/app/app_base.gypi +++ b/app/app_base.gypi @@ -22,14 +22,11 @@ 'app_paths.cc', 'app_switches.h', 'app_switches.cc', - 'hi_res_timer_manager_posix.cc', - 'hi_res_timer_manager_win.cc', - 'hi_res_timer_manager.h', - 'system_monitor.cc', - 'system_monitor.h', - 'system_monitor_mac.mm', - 'system_monitor_posix.cc', - 'system_monitor_win.cc', + '../ui/base/system_monitor/system_monitor.cc', + '../ui/base/system_monitor/system_monitor.h', + '../ui/base/system_monitor/system_monitor_mac.mm', + '../ui/base/system_monitor/system_monitor_posix.cc', + '../ui/base/system_monitor/system_monitor_win.cc', ], 'conditions': [ ['OS!="linux" and OS!="freebsd" and OS!="openbsd"', { diff --git a/app/hi_res_timer_manager.h b/app/hi_res_timer_manager.h deleted file mode 100644 index 7f3b87b..0000000 --- a/app/hi_res_timer_manager.h +++ /dev/null @@ -1,30 +0,0 @@ -// 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 APP_HI_RES_TIMER_MANAGER_H_ -#define APP_HI_RES_TIMER_MANAGER_H_ -#pragma once - -#include "app/system_monitor.h" - -// Ensures that the Windows high resolution timer is only used -// when not running on battery power. -class HighResolutionTimerManager : public SystemMonitor::PowerObserver { - public: - HighResolutionTimerManager(); - virtual ~HighResolutionTimerManager(); - - // SystemMonitor::PowerObserver: - virtual void OnPowerStateChange(bool on_battery_power); - - private: - // Enable or disable the faster multimedia timer. - void UseHiResClock(bool use); - - bool hi_res_clock_used_; - - DISALLOW_COPY_AND_ASSIGN(HighResolutionTimerManager); -}; - -#endif // APP_HI_RES_TIMER_MANAGER_H_ diff --git a/app/hi_res_timer_manager_posix.cc b/app/hi_res_timer_manager_posix.cc deleted file mode 100644 index 1398449..0000000 --- a/app/hi_res_timer_manager_posix.cc +++ /dev/null @@ -1,20 +0,0 @@ -// 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 "app/hi_res_timer_manager.h" - -// On POSIX we don't need to do anything special with the system timer. - -HighResolutionTimerManager::HighResolutionTimerManager() - : hi_res_clock_used_(false) { -} - -HighResolutionTimerManager::~HighResolutionTimerManager() { -} - -void HighResolutionTimerManager::OnPowerStateChange(bool on_battery_power) { -} - -void HighResolutionTimerManager::UseHiResClock(bool use) { -} diff --git a/app/hi_res_timer_manager_win.cc b/app/hi_res_timer_manager_win.cc deleted file mode 100644 index 6fbffca..0000000 --- a/app/hi_res_timer_manager_win.cc +++ /dev/null @@ -1,29 +0,0 @@ -// 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 "app/hi_res_timer_manager.h" - -#include "base/time.h" - -HighResolutionTimerManager::HighResolutionTimerManager() - : hi_res_clock_used_(false) { - SystemMonitor* system_monitor = SystemMonitor::Get(); - system_monitor->AddObserver(this); - UseHiResClock(!system_monitor->BatteryPower()); -} - -HighResolutionTimerManager::~HighResolutionTimerManager() { - SystemMonitor::Get()->RemoveObserver(this); - UseHiResClock(false); -} - -void HighResolutionTimerManager::OnPowerStateChange(bool on_battery_power) { - UseHiResClock(!on_battery_power); -} - -void HighResolutionTimerManager::UseHiResClock(bool use) { - if (use == hi_res_clock_used_) - return; - base::Time::EnableHighResolutionTimer(use); -} diff --git a/app/system_monitor.cc b/app/system_monitor.cc deleted file mode 100644 index a30dac1..0000000 --- a/app/system_monitor.cc +++ /dev/null @@ -1,104 +0,0 @@ -// Copyright (c) 2010 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 "app/system_monitor.h" - -#include "base/logging.h" -#include "base/message_loop.h" -#include "base/time.h" - -static SystemMonitor* g_system_monitor = NULL; - -#if defined(ENABLE_BATTERY_MONITORING) -// The amount of time (in ms) to wait before running the initial -// battery check. -static int kDelayedBatteryCheckMs = 10 * 1000; -#endif // defined(ENABLE_BATTERY_MONITORING) - -SystemMonitor::SystemMonitor() - : observer_list_(new ObserverListThreadSafe<PowerObserver>()), - battery_in_use_(false), - suspended_(false) { - DCHECK(!g_system_monitor); - g_system_monitor = this; - - DCHECK(MessageLoop::current()); -#if defined(ENABLE_BATTERY_MONITORING) - delayed_battery_check_.Start( - base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, - &SystemMonitor::BatteryCheck); -#endif // defined(ENABLE_BATTERY_MONITORING) -#if defined(OS_MACOSX) - PlatformInit(); -#endif -} - -SystemMonitor::~SystemMonitor() { -#if defined(OS_MACOSX) - PlatformDestroy(); -#endif - DCHECK_EQ(this, g_system_monitor); - g_system_monitor = NULL; -} - -// static -SystemMonitor* SystemMonitor::Get() { - return g_system_monitor; -} - -void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { - // Suppress duplicate notifications. Some platforms may - // send multiple notifications of the same event. - switch (event_id) { - case POWER_STATE_EVENT: - { - bool on_battery = IsBatteryPower(); - if (on_battery != battery_in_use_) { - battery_in_use_ = on_battery; - NotifyPowerStateChange(); - } - } - break; - case RESUME_EVENT: - if (suspended_) { - suspended_ = false; - NotifyResume(); - } - break; - case SUSPEND_EVENT: - if (!suspended_) { - suspended_ = true; - NotifySuspend(); - } - break; - } -} - -void SystemMonitor::AddObserver(PowerObserver* obs) { - observer_list_->AddObserver(obs); -} - -void SystemMonitor::RemoveObserver(PowerObserver* obs) { - observer_list_->RemoveObserver(obs); -} - -void SystemMonitor::NotifyPowerStateChange() { - VLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") - << " battery"; - observer_list_->Notify(&PowerObserver::OnPowerStateChange, BatteryPower()); -} - -void SystemMonitor::NotifySuspend() { - VLOG(1) << "Power Suspending"; - observer_list_->Notify(&PowerObserver::OnSuspend); -} - -void SystemMonitor::NotifyResume() { - VLOG(1) << "Power Resuming"; - observer_list_->Notify(&PowerObserver::OnResume); -} - -void SystemMonitor::BatteryCheck() { - ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); -} diff --git a/app/system_monitor.h b/app/system_monitor.h deleted file mode 100644 index 39711a7..0000000 --- a/app/system_monitor.h +++ /dev/null @@ -1,139 +0,0 @@ -// Copyright (c) 2010 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 APP_SYSTEM_MONITOR_H_ -#define APP_SYSTEM_MONITOR_H_ -#pragma once - -#include "build/build_config.h" - -// Windows HiRes timers drain the battery faster so we need to know the battery -// status. This isn't true for other platforms. -#if defined(OS_WIN) -#define ENABLE_BATTERY_MONITORING 1 -#else -#undef ENABLE_BATTERY_MONITORING -#endif // !OS_WIN - -#include "base/observer_list_threadsafe.h" -#if defined(ENABLE_BATTERY_MONITORING) -#include "base/timer.h" -#endif // defined(ENABLE_BATTERY_MONITORING) - -#if defined(OS_MACOSX) -#ifdef __OBJC__ -@class SystemMonitorBridge; -#else -class SystemMonitorBridge; -#endif -#endif - -// Class for monitoring various system-related subsystems -// such as power management, network status, etc. -// TODO(mbelshe): Add support beyond just power management. -class SystemMonitor { - public: - // Normalized list of power events. - enum PowerEvent { - POWER_STATE_EVENT, // The Power status of the system has changed. - SUSPEND_EVENT, // The system is being suspended. - RESUME_EVENT // The system is being resumed. - }; - - // Create SystemMonitor. Only one SystemMonitor instance per application - // is allowed. - SystemMonitor(); - ~SystemMonitor(); - - // Get the application-wide SystemMonitor (if not present, returns NULL). - static SystemMonitor* Get(); - - // - // Power-related APIs - // - - // Is the computer currently on battery power. - // Can be called on any thread. - bool BatteryPower() const { - // Using a lock here is not necessary for just a bool. - return battery_in_use_; - } - - // Callbacks will be called on the thread which creates the SystemMonitor. - // During the callback, Add/RemoveObserver will block until the callbacks - // are finished. Observers should implement quick callback functions; if - // lengthy operations are needed, the observer should take care to invoke - // the operation on an appropriate thread. - class PowerObserver { - public: - // Notification of a change in power status of the computer, such - // as from switching between battery and A/C power. - virtual void OnPowerStateChange(bool on_battery_power) {} - - // Notification that the system is suspending. - virtual void OnSuspend() {} - - // Notification that the system is resuming. - virtual void OnResume() {} - - protected: - virtual ~PowerObserver() {} - }; - - // Add a new observer. - // Can be called from any thread. - // Must not be called from within a notification callback. - void AddObserver(PowerObserver* obs); - - // Remove an existing observer. - // Can be called from any thread. - // Must not be called from within a notification callback. - void RemoveObserver(PowerObserver* obs); - -#if defined(OS_WIN) - // Windows-specific handling of a WM_POWERBROADCAST message. - // Embedders of this API should hook their top-level window - // message loop and forward WM_POWERBROADCAST through this call. - void ProcessWmPowerBroadcastMessage(int event_id); -#endif - - // Cross-platform handling of a power event. - void ProcessPowerMessage(PowerEvent event_id); - - private: -#if defined(OS_MACOSX) - void PlatformInit(); - void PlatformDestroy(); -#endif - - // Platform-specific method to check whether the system is currently - // running on battery power. Returns true if running on batteries, - // false otherwise. - bool IsBatteryPower(); - - // Checks the battery status and notifies observers if the battery - // status has changed. - void BatteryCheck(); - - // Functions to trigger notifications. - void NotifyPowerStateChange(); - void NotifySuspend(); - void NotifyResume(); - - scoped_refptr<ObserverListThreadSafe<PowerObserver> > observer_list_; - bool battery_in_use_; - bool suspended_; - -#if defined(ENABLE_BATTERY_MONITORING) - base::OneShotTimer<SystemMonitor> delayed_battery_check_; -#endif - -#if defined(OS_MACOSX) - SystemMonitorBridge* system_monitor_bridge_; -#endif - - DISALLOW_COPY_AND_ASSIGN(SystemMonitor); -}; - -#endif // APP_SYSTEM_MONITOR_H_ diff --git a/app/system_monitor_mac.mm b/app/system_monitor_mac.mm deleted file mode 100644 index d40970b..0000000 --- a/app/system_monitor_mac.mm +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright (c) 2010 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 "app/system_monitor.h" - -#import <AppKit/AppKit.h> - -@interface SystemMonitorBridge : NSObject { - @private - SystemMonitor* systemMonitor_; // weak -} - -- (id)initWithSystemMonitor:(SystemMonitor*)monitor; -- (void)computerDidSleep:(NSNotification*)notification; -- (void)computerDidWake:(NSNotification*)notification; - -@end - -@implementation SystemMonitorBridge - -- (id)initWithSystemMonitor:(SystemMonitor*)monitor { - self = [super init]; - if (self) { - systemMonitor_ = monitor; - - // See QA1340 - // <http://developer.apple.com/library/mac/#qa/qa2004/qa1340.html> for more - // details. - [[[NSWorkspace sharedWorkspace] notificationCenter] - addObserver:self - selector:@selector(computerDidSleep:) - name:NSWorkspaceWillSleepNotification - object:nil]; - [[[NSWorkspace sharedWorkspace] notificationCenter] - addObserver:self - selector:@selector(computerDidWake:) - name:NSWorkspaceDidWakeNotification - object:nil]; - } - return self; -} - -- (void)dealloc { - [[[NSWorkspace sharedWorkspace] notificationCenter] - removeObserver:self]; - [super dealloc]; -} - -- (void)computerDidSleep:(NSNotification*)notification { - systemMonitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); -} - -- (void)computerDidWake:(NSNotification*)notification { - systemMonitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); -} - -@end - -void SystemMonitor::PlatformInit() { - system_monitor_bridge_ = - [[SystemMonitorBridge alloc] initWithSystemMonitor:this]; -} - -void SystemMonitor::PlatformDestroy() { - [system_monitor_bridge_ release]; -} diff --git a/app/system_monitor_posix.cc b/app/system_monitor_posix.cc deleted file mode 100644 index 5ac7a12..0000000 --- a/app/system_monitor_posix.cc +++ /dev/null @@ -1,10 +0,0 @@ -// 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 "app/system_monitor.h" - -bool SystemMonitor::IsBatteryPower() { - NOTIMPLEMENTED(); - return false; -} diff --git a/app/system_monitor_unittest.cc b/app/system_monitor_unittest.cc deleted file mode 100644 index 5b107aa..0000000 --- a/app/system_monitor_unittest.cc +++ /dev/null @@ -1,87 +0,0 @@ -// 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 "app/system_monitor.h" -#include "testing/gtest/include/gtest/gtest.h" - -class PowerTest : public SystemMonitor::PowerObserver { - public: - PowerTest() - : battery_(false), - power_state_changes_(0), - suspends_(0), - resumes_(0) { - } - - // PowerObserver callbacks. - void OnPowerStateChange(bool on_battery_power) { - power_state_changes_++; - } - - void OnSuspend() { - suspends_++; - } - - void OnResume() { - resumes_++; - } - - // Test status counts. - bool battery() { return battery_; } - int power_state_changes() { return power_state_changes_; } - int suspends() { return suspends_; } - int resumes() { return resumes_; } - - private: - bool battery_; // Do we currently think we're on battery power. - int power_state_changes_; // Count of OnPowerStateChange notifications. - int suspends_; // Count of OnSuspend notifications. - int resumes_; // Count of OnResume notifications. -}; - -TEST(SystemMonitor, PowerNotifications) { - const int kObservers = 5; - - // Initialize a message loop for this to run on. - MessageLoop loop; - // Initialize time() since it registers as a SystemMonitor observer. - base::Time now = base::Time::Now(); - - SystemMonitor system_monitor; - PowerTest test[kObservers]; - for (int index = 0; index < kObservers; ++index) - system_monitor.AddObserver(&test[index]); - - // Send a bunch of power changes. Since the battery power hasn't - // actually changed, we shouldn't get notifications. - for (int index = 0; index < 5; index++) { - system_monitor.ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); - EXPECT_EQ(test[0].power_state_changes(), 0); - } - - // Sending resume when not suspended should have no effect. - system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); - loop.RunAllPending(); - EXPECT_EQ(test[0].resumes(), 0); - - // Pretend we suspended. - system_monitor.ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); - loop.RunAllPending(); - EXPECT_EQ(test[0].suspends(), 1); - - // Send a second suspend notification. This should be suppressed. - system_monitor.ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); - loop.RunAllPending(); - EXPECT_EQ(test[0].suspends(), 1); - - // Pretend we were awakened. - system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); - loop.RunAllPending(); - EXPECT_EQ(test[0].resumes(), 1); - - // Send a duplicate resume notification. This should be suppressed. - system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); - loop.RunAllPending(); - EXPECT_EQ(test[0].resumes(), 1); -} diff --git a/app/system_monitor_win.cc b/app/system_monitor_win.cc deleted file mode 100644 index c9347dc..0000000 --- a/app/system_monitor_win.cc +++ /dev/null @@ -1,46 +0,0 @@ -// 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 "app/system_monitor.h" - -void SystemMonitor::ProcessWmPowerBroadcastMessage(int event_id) { - PowerEvent power_event; - switch (event_id) { - case PBT_APMPOWERSTATUSCHANGE: // The power status changed. - power_event = POWER_STATE_EVENT; - break; - case PBT_APMRESUMEAUTOMATIC: // Resume from suspend. - //case PBT_APMRESUMESUSPEND: // User-initiated resume from suspend. - // We don't notify for this latter event - // because if it occurs it is always sent as a - // second event after PBT_APMRESUMEAUTOMATIC. - power_event = RESUME_EVENT; - break; - case PBT_APMSUSPEND: // System has been suspended. - power_event = SUSPEND_EVENT; - break; - default: - return; - - // Other Power Events: - // PBT_APMBATTERYLOW - removed in Vista. - // PBT_APMOEMEVENT - removed in Vista. - // PBT_APMQUERYSUSPEND - removed in Vista. - // PBT_APMQUERYSUSPENDFAILED - removed in Vista. - // PBT_APMRESUMECRITICAL - removed in Vista. - // PBT_POWERSETTINGCHANGE - user changed the power settings. - } - ProcessPowerMessage(power_event); -} - -// Function to query the system to see if it is currently running on -// battery power. Returns true if running on battery. -bool SystemMonitor::IsBatteryPower() { - SYSTEM_POWER_STATUS status; - if (!GetSystemPowerStatus(&status)) { - LOG(ERROR) << "GetSystemPowerStatus failed: " << GetLastError(); - return false; - } - return (status.ACLineStatus == 0); -} |