Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1401)

Unified Diff: base/system_monitor/system_monitor_win.cc

Issue 11821050: Move power event handling logic from ui/ to base/ on Windows (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: add message only window into base/ Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/system_monitor/system_monitor_win.cc
diff --git a/base/system_monitor/system_monitor_win.cc b/base/system_monitor/system_monitor_win.cc
index 69ab85e84f6852c34095a143342a3a0417e230bc..80af41ac9a235bd0634c00bdc01d7d2f4dd9da4c 100644
--- a/base/system_monitor/system_monitor_win.cc
+++ b/base/system_monitor/system_monitor_win.cc
@@ -4,23 +4,78 @@
#include "base/system_monitor/system_monitor.h"
+#include "base/win/wrapped_window_proc.h"
+
namespace base {
-void SystemMonitor::ProcessWmPowerBroadcastMessage(int event_id) {
+namespace {
+
+const wchar_t kWindowClassName[] = L"Base_PowerMessageWindow";
+
+} // namespace
+
+void SystemMonitor::PlatformInit() {
+ power_message_window_.reset(new PowerMessageWindow(this));
+}
+
+void SystemMonitor::PlatformDestroy() {
+}
+
+// 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)) {
+ DLOG_GETLASTERROR(ERROR) << "GetSystemPowerStatus failed";
+ return false;
+ }
+ return (status.ACLineStatus == 0);
+}
+
+SystemMonitor::PowerMessageWindow::PowerMessageWindow(SystemMonitor* monitor)
+ : monitor_(monitor), instance_(NULL), message_hwnd_(NULL) {
+ InitMessageHWND();
+}
+
+SystemMonitor::PowerMessageWindow::~PowerMessageWindow() {
+ if (message_hwnd_)
+ DestroyWindow(message_hwnd_);
+}
+
+void SystemMonitor::PowerMessageWindow::InitMessageHWND() {
vandebo (ex-Chrome) 2013/02/20 22:30:40 I think this can all go in the constructor. It is
Hongbo Min 2013/02/21 07:39:08 Make more sense. Done!
+ WNDCLASSEX window_class;
+ base::win::InitializeWindowClass(
+ kWindowClassName,
+ &base::win::WrappedWindowProc<
+ SystemMonitor::PowerMessageWindow::WndProcThunk>,
+ 0, 0, 0, NULL, NULL, NULL, NULL, NULL,
+ &window_class);
+ instance_ = window_class.hInstance;
+ ATOM clazz = RegisterClassEx(&window_class);
+ DCHECK(clazz);
+
+ message_hwnd_ = CreateWindow(kWindowClassName,
+ 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, instance_, 0);
vandebo (ex-Chrome) 2013/02/20 22:30:40 Are you sure you want HWND_MESSAGE as the parent?
Hongbo Min 2013/02/21 07:39:08 I just saw MessagePumpForUI in message_pump_win.cc
+ SetWindowLongPtr(message_hwnd_, GWLP_USERDATA,
+ reinterpret_cast<LONG_PTR>(this));
+}
+
+void SystemMonitor::PowerMessageWindow::ProcessWmPowerBroadcastMessage(
+ int event_id) {
PowerEvent power_event;
switch (event_id) {
case PBT_APMPOWERSTATUSCHANGE: // The power status changed.
- power_event = POWER_STATE_EVENT;
+ power_event = SystemMonitor::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;
+ power_event = SystemMonitor::RESUME_EVENT;
break;
case PBT_APMSUSPEND: // System has been suspended.
- power_event = SUSPEND_EVENT;
+ power_event = SystemMonitor::SUSPEND_EVENT;
break;
default:
return;
@@ -33,18 +88,32 @@ void SystemMonitor::ProcessWmPowerBroadcastMessage(int event_id) {
// PBT_APMRESUMECRITICAL - removed in Vista.
// PBT_POWERSETTINGCHANGE - user changed the power settings.
}
- ProcessPowerMessage(power_event);
+ monitor_->ProcessPowerMessage(power_event);
vandebo (ex-Chrome) 2013/02/20 22:30:40 Can we just call SystemMonitor::Get() here, that w
Hongbo Min 2013/02/21 07:39:08 Done!
}
-// 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)) {
- DLOG_GETLASTERROR(ERROR) << "GetSystemPowerStatus failed";
- return false;
+LRESULT CALLBACK SystemMonitor::PowerMessageWindow::WndProc(
+ HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
+ switch (message) {
+ case WM_POWERBROADCAST: {
+ DWORD power_event = static_cast<DWORD>(message);
+ ProcessWmPowerBroadcastMessage(power_event);
+ return TRUE;
+ }
+ default:
+ break;
}
- return (status.ACLineStatus == 0);
+ return TRUE;
vandebo (ex-Chrome) 2013/02/20 22:30:40 Should this also return ::DefWindowProc ?
Hongbo Min 2013/02/21 07:39:08 Good catch! Done!
+}
+
+// static
+LRESULT CALLBACK SystemMonitor::PowerMessageWindow::WndProcThunk(
+ HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
+ SystemMonitor::PowerMessageWindow* message_hwnd =
+ reinterpret_cast<SystemMonitor::PowerMessageWindow*>(
+ GetWindowLongPtr(hwnd, GWLP_USERDATA));
+ if (message_hwnd)
+ return message_hwnd->WndProc(hwnd, message, wparam, lparam);
+ return ::DefWindowProc(hwnd, message, wparam, lparam);
}
} // namespace base

Powered by Google App Engine
This is Rietveld 408576698