- add sources.
[platform/framework/web/crosswalk.git] / src / base / power_monitor / power_monitor_device_source_win.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/power_monitor/power_monitor.h"
6 #include "base/power_monitor/power_monitor_device_source.h"
7 #include "base/power_monitor/power_monitor_source.h"
8 #include "base/win/wrapped_window_proc.h"
9
10 namespace base {
11
12 void ProcessPowerEventHelper(PowerMonitorSource::PowerEvent event) {
13   PowerMonitorSource::ProcessPowerEvent(event);
14 }
15
16 namespace {
17
18 const wchar_t kWindowClassName[] = L"Base_PowerMessageWindow";
19
20 }  // namespace
21
22 // Function to query the system to see if it is currently running on
23 // battery power.  Returns true if running on battery.
24 bool PowerMonitorDeviceSource::IsOnBatteryPowerImpl() {
25   SYSTEM_POWER_STATUS status;
26   if (!GetSystemPowerStatus(&status)) {
27     DLOG_GETLASTERROR(ERROR) << "GetSystemPowerStatus failed";
28     return false;
29   }
30   return (status.ACLineStatus == 0);
31 }
32
33 PowerMonitorDeviceSource::PowerMessageWindow::PowerMessageWindow()
34     : instance_(NULL), message_hwnd_(NULL) {
35   if (MessageLoop::current()->type() != MessageLoop::TYPE_UI) {
36     // Creating this window in (e.g.) a renderer inhibits shutdown on Windows.
37     // See http://crbug.com/230122. TODO(vandebo): http://crbug.com/236031
38     DLOG(ERROR)
39         << "Cannot create windows on non-UI thread, power monitor disabled!";
40     return;
41   }
42   WNDCLASSEX window_class;
43   base::win::InitializeWindowClass(
44       kWindowClassName,
45       &base::win::WrappedWindowProc<
46           PowerMonitorDeviceSource::PowerMessageWindow::WndProcThunk>,
47       0, 0, 0, NULL, NULL, NULL, NULL, NULL,
48       &window_class);
49   instance_ = window_class.hInstance;
50   ATOM clazz = RegisterClassEx(&window_class);
51   DCHECK(clazz);
52
53   message_hwnd_ = CreateWindowEx(WS_EX_NOACTIVATE, kWindowClassName,
54       NULL, WS_POPUP, 0, 0, 0, 0, NULL, NULL, instance_, NULL);
55   SetWindowLongPtr(message_hwnd_, GWLP_USERDATA,
56                    reinterpret_cast<LONG_PTR>(this));
57 }
58
59 PowerMonitorDeviceSource::PowerMessageWindow::~PowerMessageWindow() {
60   if (message_hwnd_) {
61     DestroyWindow(message_hwnd_);
62     UnregisterClass(kWindowClassName, instance_);
63   }
64 }
65
66 void
67 PowerMonitorDeviceSource::PowerMessageWindow::ProcessWmPowerBroadcastMessage(
68     int event_id) {
69   PowerMonitorSource::PowerEvent power_event;
70   switch (event_id) {
71     case PBT_APMPOWERSTATUSCHANGE:  // The power status changed.
72       power_event = PowerMonitorSource::POWER_STATE_EVENT;
73       break;
74     case PBT_APMRESUMEAUTOMATIC:  // Resume from suspend.
75     //case PBT_APMRESUMESUSPEND:  // User-initiated resume from suspend.
76                                   // We don't notify for this latter event
77                                   // because if it occurs it is always sent as a
78                                   // second event after PBT_APMRESUMEAUTOMATIC.
79       power_event = PowerMonitorSource::RESUME_EVENT;
80       break;
81     case PBT_APMSUSPEND:  // System has been suspended.
82       power_event = PowerMonitorSource::SUSPEND_EVENT;
83       break;
84     default:
85       return;
86
87     // Other Power Events:
88     // PBT_APMBATTERYLOW - removed in Vista.
89     // PBT_APMOEMEVENT - removed in Vista.
90     // PBT_APMQUERYSUSPEND - removed in Vista.
91     // PBT_APMQUERYSUSPENDFAILED - removed in Vista.
92     // PBT_APMRESUMECRITICAL - removed in Vista.
93     // PBT_POWERSETTINGCHANGE - user changed the power settings.
94   }
95
96   ProcessPowerEventHelper(power_event);
97 }
98
99 LRESULT CALLBACK PowerMonitorDeviceSource::PowerMessageWindow::WndProc(
100     HWND hwnd,
101     UINT message,
102     WPARAM wparam,
103     LPARAM lparam) {
104   switch (message) {
105     case WM_POWERBROADCAST: {
106       DWORD power_event = static_cast<DWORD>(message);
107       ProcessWmPowerBroadcastMessage(power_event);
108       return TRUE;
109     }
110     default:
111       break;
112   }
113   return ::DefWindowProc(hwnd, message, wparam, lparam);
114 }
115
116 // static
117 LRESULT CALLBACK PowerMonitorDeviceSource::PowerMessageWindow::WndProcThunk(
118     HWND hwnd,
119     UINT message,
120     WPARAM wparam,
121     LPARAM lparam) {
122   PowerMonitorDeviceSource::PowerMessageWindow* message_hwnd =
123       reinterpret_cast<PowerMonitorDeviceSource::PowerMessageWindow*>(
124           GetWindowLongPtr(hwnd, GWLP_USERDATA));
125   if (message_hwnd)
126     return message_hwnd->WndProc(hwnd, message, wparam, lparam);
127   return ::DefWindowProc(hwnd, message, wparam, lparam);
128 }
129
130 }  // namespace base