| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/system_monitor/system_monitor.h" | 5 #include "base/system_monitor/system_monitor.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/time.h" | 11 #include "base/time.h" |
| 12 | 12 |
| 13 #if defined(OS_WIN) |
| 14 #include "base/win/wrapped_window_proc.h" |
| 15 #endif |
| 16 |
| 13 namespace base { | 17 namespace base { |
| 14 | 18 |
| 15 static SystemMonitor* g_system_monitor = NULL; | 19 static SystemMonitor* g_system_monitor = NULL; |
| 16 | 20 |
| 17 #if defined(ENABLE_BATTERY_MONITORING) | 21 #if defined(ENABLE_BATTERY_MONITORING) |
| 18 // The amount of time (in ms) to wait before running the initial | 22 // The amount of time (in ms) to wait before running the initial |
| 19 // battery check. | 23 // battery check. |
| 20 static int kDelayedBatteryCheckMs = 10 * 1000; | 24 static int kDelayedBatteryCheckMs = 10 * 1000; |
| 21 #endif // defined(ENABLE_BATTERY_MONITORING) | 25 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 22 | 26 |
| 27 #if defined(OS_WIN) |
| 28 namespace { |
| 29 |
| 30 const wchar_t kWindowClassName[] = L"Base_PowerMessageWindow"; |
| 31 |
| 32 } // namespace |
| 33 |
| 34 class SystemMonitor::PowerMessageWindow { |
| 35 public: |
| 36 PowerMessageWindow(); |
| 37 ~PowerMessageWindow(); |
| 38 |
| 39 private: |
| 40 void ProcessWmPowerBroadcastMessage(int event_id); |
| 41 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, |
| 42 WPARAM wparam, LPARAM lparam); |
| 43 static LRESULT CALLBACK WndProcThunk(HWND hwnd, |
| 44 UINT message, |
| 45 WPARAM wparam, |
| 46 LPARAM lparam); |
| 47 // Instance of the module containing the window procedure. |
| 48 HMODULE instance_; |
| 49 // A hidden window handle. |
| 50 HWND message_hwnd_; |
| 51 }; |
| 52 |
| 53 SystemMonitor::PowerMessageWindow::PowerMessageWindow() |
| 54 : instance_(NULL), message_hwnd_(NULL) { |
| 55 WNDCLASSEX window_class; |
| 56 base::win::InitializeWindowClass( |
| 57 kWindowClassName, |
| 58 &base::win::WrappedWindowProc< |
| 59 SystemMonitor::PowerMessageWindow::WndProcThunk>, |
| 60 0, 0, 0, NULL, NULL, NULL, NULL, NULL, |
| 61 &window_class); |
| 62 instance_ = window_class.hInstance; |
| 63 ATOM clazz = RegisterClassEx(&window_class); |
| 64 DCHECK(clazz); |
| 65 |
| 66 message_hwnd_ = CreateWindow(kWindowClassName, |
| 67 NULL, 0, 0, 0, 0, 0, 0, 0, instance_, 0); |
| 68 SetWindowLongPtr(message_hwnd_, GWLP_USERDATA, |
| 69 reinterpret_cast<LONG_PTR>(this)); |
| 70 } |
| 71 |
| 72 SystemMonitor::PowerMessageWindow::~PowerMessageWindow() { |
| 73 if (message_hwnd_) { |
| 74 DestroyWindow(message_hwnd_); |
| 75 UnregisterClass(kWindowClassName, instance_); |
| 76 } |
| 77 } |
| 78 |
| 79 void SystemMonitor::PowerMessageWindow::ProcessWmPowerBroadcastMessage( |
| 80 int event_id) { |
| 81 SystemMonitor::PowerEvent power_event; |
| 82 switch (event_id) { |
| 83 case PBT_APMPOWERSTATUSCHANGE: // The power status changed. |
| 84 power_event = SystemMonitor::POWER_STATE_EVENT; |
| 85 break; |
| 86 case PBT_APMRESUMEAUTOMATIC: // Resume from suspend. |
| 87 //case PBT_APMRESUMESUSPEND: // User-initiated resume from suspend. |
| 88 // We don't notify for this latter event |
| 89 // because if it occurs it is always sent as a |
| 90 // second event after PBT_APMRESUMEAUTOMATIC. |
| 91 power_event = SystemMonitor::RESUME_EVENT; |
| 92 break; |
| 93 case PBT_APMSUSPEND: // System has been suspended. |
| 94 power_event = SystemMonitor::SUSPEND_EVENT; |
| 95 break; |
| 96 default: |
| 97 return; |
| 98 |
| 99 // Other Power Events: |
| 100 // PBT_APMBATTERYLOW - removed in Vista. |
| 101 // PBT_APMOEMEVENT - removed in Vista. |
| 102 // PBT_APMQUERYSUSPEND - removed in Vista. |
| 103 // PBT_APMQUERYSUSPENDFAILED - removed in Vista. |
| 104 // PBT_APMRESUMECRITICAL - removed in Vista. |
| 105 // PBT_POWERSETTINGCHANGE - user changed the power settings. |
| 106 } |
| 107 SystemMonitor::Get()->ProcessPowerMessage(power_event); |
| 108 } |
| 109 |
| 110 LRESULT CALLBACK SystemMonitor::PowerMessageWindow::WndProc( |
| 111 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { |
| 112 switch (message) { |
| 113 case WM_POWERBROADCAST: { |
| 114 DWORD power_event = static_cast<DWORD>(message); |
| 115 ProcessWmPowerBroadcastMessage(power_event); |
| 116 return TRUE; |
| 117 } |
| 118 default: |
| 119 break; |
| 120 } |
| 121 return ::DefWindowProc(hwnd, message, wparam, lparam); |
| 122 } |
| 123 |
| 124 // static |
| 125 LRESULT CALLBACK SystemMonitor::PowerMessageWindow::WndProcThunk( |
| 126 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { |
| 127 PowerMessageWindow* message_hwnd = |
| 128 reinterpret_cast<PowerMessageWindow*>( |
| 129 GetWindowLongPtr(hwnd, GWLP_USERDATA)); |
| 130 if (message_hwnd) |
| 131 return message_hwnd->WndProc(hwnd, message, wparam, lparam); |
| 132 return ::DefWindowProc(hwnd, message, wparam, lparam); |
| 133 } |
| 134 #endif // OS_WIN |
| 135 |
| 136 |
| 23 SystemMonitor::SystemMonitor() | 137 SystemMonitor::SystemMonitor() |
| 24 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), | 138 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), |
| 25 devices_changed_observer_list_( | 139 devices_changed_observer_list_( |
| 26 new ObserverListThreadSafe<DevicesChangedObserver>()), | 140 new ObserverListThreadSafe<DevicesChangedObserver>()), |
| 27 battery_in_use_(false), | 141 battery_in_use_(false), |
| 28 suspended_(false) { | 142 suspended_(false) { |
| 29 DCHECK(!g_system_monitor); | 143 DCHECK(!g_system_monitor); |
| 30 g_system_monitor = this; | 144 g_system_monitor = this; |
| 31 | 145 |
| 32 DCHECK(MessageLoop::current()); | 146 DCHECK(MessageLoop::current()); |
| 33 #if defined(ENABLE_BATTERY_MONITORING) | 147 #if defined(ENABLE_BATTERY_MONITORING) |
| 34 delayed_battery_check_.Start(FROM_HERE, | 148 delayed_battery_check_.Start(FROM_HERE, |
| 35 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, | 149 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, |
| 36 &SystemMonitor::BatteryCheck); | 150 &SystemMonitor::BatteryCheck); |
| 37 #endif // defined(ENABLE_BATTERY_MONITORING) | 151 #endif // defined(ENABLE_BATTERY_MONITORING) |
| 38 #if defined(OS_MACOSX) | 152 #if defined(OS_MACOSX) |
| 39 PlatformInit(); | 153 PlatformInit(); |
| 40 #endif | 154 #endif |
| 155 #if defined(OS_WIN) |
| 156 power_message_window_.reset(new PowerMessageWindow); |
| 157 #endif |
| 41 } | 158 } |
| 42 | 159 |
| 43 SystemMonitor::~SystemMonitor() { | 160 SystemMonitor::~SystemMonitor() { |
| 44 #if defined(OS_MACOSX) | 161 #if defined(OS_MACOSX) |
| 45 PlatformDestroy(); | 162 PlatformDestroy(); |
| 46 #endif | 163 #endif |
| 47 DCHECK_EQ(this, g_system_monitor); | 164 DCHECK_EQ(this, g_system_monitor); |
| 48 g_system_monitor = NULL; | 165 g_system_monitor = NULL; |
| 49 } | 166 } |
| 50 | 167 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 void SystemMonitor::NotifyResume() { | 239 void SystemMonitor::NotifyResume() { |
| 123 DVLOG(1) << "Power Resuming"; | 240 DVLOG(1) << "Power Resuming"; |
| 124 power_observer_list_->Notify(&PowerObserver::OnResume); | 241 power_observer_list_->Notify(&PowerObserver::OnResume); |
| 125 } | 242 } |
| 126 | 243 |
| 127 void SystemMonitor::BatteryCheck() { | 244 void SystemMonitor::BatteryCheck() { |
| 128 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | 245 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); |
| 129 } | 246 } |
| 130 | 247 |
| 131 } // namespace base | 248 } // namespace base |
| OLD | NEW |