OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/win/wrapped_window_proc.h" | |
8 | |
7 namespace base { | 9 namespace base { |
8 | 10 |
9 void SystemMonitor::ProcessWmPowerBroadcastMessage(int event_id) { | 11 namespace { |
12 | |
13 const wchar_t kWindowClassName[] = L"Base_PowerMessageWindow"; | |
14 | |
15 } // namespace | |
16 | |
17 void SystemMonitor::PlatformInit() { | |
18 power_message_window_.reset(new PowerMessageWindow(this)); | |
19 } | |
20 | |
21 void SystemMonitor::PlatformDestroy() { | |
22 } | |
23 | |
24 // Function to query the system to see if it is currently running on | |
25 // battery power. Returns true if running on battery. | |
26 bool SystemMonitor::IsBatteryPower() { | |
27 SYSTEM_POWER_STATUS status; | |
28 if (!GetSystemPowerStatus(&status)) { | |
29 DLOG_GETLASTERROR(ERROR) << "GetSystemPowerStatus failed"; | |
30 return false; | |
31 } | |
32 return (status.ACLineStatus == 0); | |
33 } | |
34 | |
35 SystemMonitor::PowerMessageWindow::PowerMessageWindow(SystemMonitor* monitor) | |
36 : monitor_(monitor), instance_(NULL), message_hwnd_(NULL) { | |
37 InitMessageHWND(); | |
38 } | |
39 | |
40 SystemMonitor::PowerMessageWindow::~PowerMessageWindow() { | |
41 if (message_hwnd_) | |
42 DestroyWindow(message_hwnd_); | |
43 } | |
44 | |
45 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!
| |
46 WNDCLASSEX window_class; | |
47 base::win::InitializeWindowClass( | |
48 kWindowClassName, | |
49 &base::win::WrappedWindowProc< | |
50 SystemMonitor::PowerMessageWindow::WndProcThunk>, | |
51 0, 0, 0, NULL, NULL, NULL, NULL, NULL, | |
52 &window_class); | |
53 instance_ = window_class.hInstance; | |
54 ATOM clazz = RegisterClassEx(&window_class); | |
55 DCHECK(clazz); | |
56 | |
57 message_hwnd_ = CreateWindow(kWindowClassName, | |
58 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
| |
59 SetWindowLongPtr(message_hwnd_, GWLP_USERDATA, | |
60 reinterpret_cast<LONG_PTR>(this)); | |
61 } | |
62 | |
63 void SystemMonitor::PowerMessageWindow::ProcessWmPowerBroadcastMessage( | |
64 int event_id) { | |
10 PowerEvent power_event; | 65 PowerEvent power_event; |
11 switch (event_id) { | 66 switch (event_id) { |
12 case PBT_APMPOWERSTATUSCHANGE: // The power status changed. | 67 case PBT_APMPOWERSTATUSCHANGE: // The power status changed. |
13 power_event = POWER_STATE_EVENT; | 68 power_event = SystemMonitor::POWER_STATE_EVENT; |
14 break; | 69 break; |
15 case PBT_APMRESUMEAUTOMATIC: // Resume from suspend. | 70 case PBT_APMRESUMEAUTOMATIC: // Resume from suspend. |
16 //case PBT_APMRESUMESUSPEND: // User-initiated resume from suspend. | 71 //case PBT_APMRESUMESUSPEND: // User-initiated resume from suspend. |
17 // We don't notify for this latter event | 72 // We don't notify for this latter event |
18 // because if it occurs it is always sent as a | 73 // because if it occurs it is always sent as a |
19 // second event after PBT_APMRESUMEAUTOMATIC. | 74 // second event after PBT_APMRESUMEAUTOMATIC. |
20 power_event = RESUME_EVENT; | 75 power_event = SystemMonitor::RESUME_EVENT; |
21 break; | 76 break; |
22 case PBT_APMSUSPEND: // System has been suspended. | 77 case PBT_APMSUSPEND: // System has been suspended. |
23 power_event = SUSPEND_EVENT; | 78 power_event = SystemMonitor::SUSPEND_EVENT; |
24 break; | 79 break; |
25 default: | 80 default: |
26 return; | 81 return; |
27 | 82 |
28 // Other Power Events: | 83 // Other Power Events: |
29 // PBT_APMBATTERYLOW - removed in Vista. | 84 // PBT_APMBATTERYLOW - removed in Vista. |
30 // PBT_APMOEMEVENT - removed in Vista. | 85 // PBT_APMOEMEVENT - removed in Vista. |
31 // PBT_APMQUERYSUSPEND - removed in Vista. | 86 // PBT_APMQUERYSUSPEND - removed in Vista. |
32 // PBT_APMQUERYSUSPENDFAILED - removed in Vista. | 87 // PBT_APMQUERYSUSPENDFAILED - removed in Vista. |
33 // PBT_APMRESUMECRITICAL - removed in Vista. | 88 // PBT_APMRESUMECRITICAL - removed in Vista. |
34 // PBT_POWERSETTINGCHANGE - user changed the power settings. | 89 // PBT_POWERSETTINGCHANGE - user changed the power settings. |
35 } | 90 } |
36 ProcessPowerMessage(power_event); | 91 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!
| |
37 } | 92 } |
38 | 93 |
39 // Function to query the system to see if it is currently running on | 94 LRESULT CALLBACK SystemMonitor::PowerMessageWindow::WndProc( |
40 // battery power. Returns true if running on battery. | 95 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { |
41 bool SystemMonitor::IsBatteryPower() { | 96 switch (message) { |
42 SYSTEM_POWER_STATUS status; | 97 case WM_POWERBROADCAST: { |
43 if (!GetSystemPowerStatus(&status)) { | 98 DWORD power_event = static_cast<DWORD>(message); |
44 DLOG_GETLASTERROR(ERROR) << "GetSystemPowerStatus failed"; | 99 ProcessWmPowerBroadcastMessage(power_event); |
45 return false; | 100 return TRUE; |
101 } | |
102 default: | |
103 break; | |
46 } | 104 } |
47 return (status.ACLineStatus == 0); | 105 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!
| |
106 } | |
107 | |
108 // static | |
109 LRESULT CALLBACK SystemMonitor::PowerMessageWindow::WndProcThunk( | |
110 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { | |
111 SystemMonitor::PowerMessageWindow* message_hwnd = | |
112 reinterpret_cast<SystemMonitor::PowerMessageWindow*>( | |
113 GetWindowLongPtr(hwnd, GWLP_USERDATA)); | |
114 if (message_hwnd) | |
115 return message_hwnd->WndProc(hwnd, message, wparam, lparam); | |
116 return ::DefWindowProc(hwnd, message, wparam, lparam); | |
48 } | 117 } |
49 | 118 |
50 } // namespace base | 119 } // namespace base |
OLD | NEW |