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

Side by Side 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: update per vandebo's comments 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 unified diff | Download patch
OLDNEW
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 // Function to query the system to see if it is currently running on
18 // battery power. Returns true if running on battery.
19 bool SystemMonitor::IsBatteryPower() {
20 SYSTEM_POWER_STATUS status;
21 if (!GetSystemPowerStatus(&status)) {
22 DLOG_GETLASTERROR(ERROR) << "GetSystemPowerStatus failed";
23 return false;
24 }
25 return (status.ACLineStatus == 0);
26 }
27
28 SystemMonitor::PowerMessageWindow::PowerMessageWindow()
29 : instance_(NULL), message_hwnd_(NULL) {
30 WNDCLASSEX window_class;
31 base::win::InitializeWindowClass(
32 kWindowClassName,
33 &base::win::WrappedWindowProc<
34 SystemMonitor::PowerMessageWindow::WndProcThunk>,
35 0, 0, 0, NULL, NULL, NULL, NULL, NULL,
36 &window_class);
37 instance_ = window_class.hInstance;
38 ATOM clazz = RegisterClassEx(&window_class);
39 DCHECK(clazz);
40
41 message_hwnd_ = CreateWindow(kWindowClassName,
42 0, 0, 0, 0, 0, 0, 0, 0, instance_, 0);
cpu_(ooo_6.6-7.5) 2013/02/21 20:44:38 the createwindow call is unnecessarily terse, use
Hongbo Min 2013/02/22 06:55:43 I am a bit confused by your concerns about the tit
cpu_(ooo_6.6-7.5) 2013/02/23 18:00:55 Because the right flags signal your intention. And
Hongbo Min 2013/02/24 07:27:22 cpu@, I did an experiment to verify the concerns y
vandebo (ex-Chrome) 2013/02/25 17:46:54 I think Carlos would like you to do the clearer th
43 SetWindowLongPtr(message_hwnd_, GWLP_USERDATA,
44 reinterpret_cast<LONG_PTR>(this));
45 }
46
47 SystemMonitor::PowerMessageWindow::~PowerMessageWindow() {
48 if (message_hwnd_)
49 DestroyWindow(message_hwnd_);
50 }
51
52 void SystemMonitor::PowerMessageWindow::ProcessWmPowerBroadcastMessage(
53 int event_id) {
10 PowerEvent power_event; 54 PowerEvent power_event;
11 switch (event_id) { 55 switch (event_id) {
12 case PBT_APMPOWERSTATUSCHANGE: // The power status changed. 56 case PBT_APMPOWERSTATUSCHANGE: // The power status changed.
13 power_event = POWER_STATE_EVENT; 57 power_event = SystemMonitor::POWER_STATE_EVENT;
14 break; 58 break;
15 case PBT_APMRESUMEAUTOMATIC: // Resume from suspend. 59 case PBT_APMRESUMEAUTOMATIC: // Resume from suspend.
16 //case PBT_APMRESUMESUSPEND: // User-initiated resume from suspend. 60 //case PBT_APMRESUMESUSPEND: // User-initiated resume from suspend.
17 // We don't notify for this latter event 61 // We don't notify for this latter event
18 // because if it occurs it is always sent as a 62 // because if it occurs it is always sent as a
19 // second event after PBT_APMRESUMEAUTOMATIC. 63 // second event after PBT_APMRESUMEAUTOMATIC.
20 power_event = RESUME_EVENT; 64 power_event = SystemMonitor::RESUME_EVENT;
21 break; 65 break;
22 case PBT_APMSUSPEND: // System has been suspended. 66 case PBT_APMSUSPEND: // System has been suspended.
23 power_event = SUSPEND_EVENT; 67 power_event = SystemMonitor::SUSPEND_EVENT;
24 break; 68 break;
25 default: 69 default:
26 return; 70 return;
27 71
28 // Other Power Events: 72 // Other Power Events:
29 // PBT_APMBATTERYLOW - removed in Vista. 73 // PBT_APMBATTERYLOW - removed in Vista.
30 // PBT_APMOEMEVENT - removed in Vista. 74 // PBT_APMOEMEVENT - removed in Vista.
31 // PBT_APMQUERYSUSPEND - removed in Vista. 75 // PBT_APMQUERYSUSPEND - removed in Vista.
32 // PBT_APMQUERYSUSPENDFAILED - removed in Vista. 76 // PBT_APMQUERYSUSPENDFAILED - removed in Vista.
33 // PBT_APMRESUMECRITICAL - removed in Vista. 77 // PBT_APMRESUMECRITICAL - removed in Vista.
34 // PBT_POWERSETTINGCHANGE - user changed the power settings. 78 // PBT_POWERSETTINGCHANGE - user changed the power settings.
35 } 79 }
36 ProcessPowerMessage(power_event); 80 SystemMonitor::Get()->ProcessPowerMessage(power_event);
37 } 81 }
38 82
39 // Function to query the system to see if it is currently running on 83 LRESULT CALLBACK SystemMonitor::PowerMessageWindow::WndProc(
40 // battery power. Returns true if running on battery. 84 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
41 bool SystemMonitor::IsBatteryPower() { 85 switch (message) {
42 SYSTEM_POWER_STATUS status; 86 case WM_POWERBROADCAST: {
43 if (!GetSystemPowerStatus(&status)) { 87 DWORD power_event = static_cast<DWORD>(message);
44 DLOG_GETLASTERROR(ERROR) << "GetSystemPowerStatus failed"; 88 ProcessWmPowerBroadcastMessage(power_event);
45 return false; 89 return TRUE;
90 }
91 default:
92 break;
46 } 93 }
47 return (status.ACLineStatus == 0); 94 return ::DefWindowProc(hwnd, message, wparam, lparam);
95 }
96
97 // static
98 LRESULT CALLBACK SystemMonitor::PowerMessageWindow::WndProcThunk(
99 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
100 SystemMonitor::PowerMessageWindow* message_hwnd =
101 reinterpret_cast<SystemMonitor::PowerMessageWindow*>(
102 GetWindowLongPtr(hwnd, GWLP_USERDATA));
cpu_(ooo_6.6-7.5) 2013/02/21 20:44:38 hmmm danger danger You can (and will) receive mes
vandebo (ex-Chrome) 2013/02/21 20:54:45 Will message_hwnd be Null in that case? If so, li
Hongbo Min 2013/02/22 06:55:43 I agree with Steve.
cpu_(ooo_6.6-7.5) 2013/02/23 18:00:55 I agree too. I must have misread it.
103 if (message_hwnd)
104 return message_hwnd->WndProc(hwnd, message, wparam, lparam);
105 return ::DefWindowProc(hwnd, message, wparam, lparam);
48 } 106 }
49 107
50 } // namespace base 108 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698