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

Unified Diff: content/browser/battery_status/battery_status_manager_win.cc

Issue 480503002: [Win] Battery Status API: use SingletonHwnd for notifications. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: added thread checker Created 6 years, 4 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
« no previous file with comments | « no previous file | ui/gfx/win/singleton_hwnd.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/battery_status/battery_status_manager_win.cc
diff --git a/content/browser/battery_status/battery_status_manager_win.cc b/content/browser/battery_status/battery_status_manager_win.cc
index d998d5aca0e2ab6405e323d659aa894cf179ef34..273c429e1bdd2c5722ed58a79cc1a5b6a1e320a8 100644
--- a/content/browser/battery_status/battery_status_manager_win.cc
+++ b/content/browser/battery_status/battery_status_manager_win.cc
@@ -5,11 +5,10 @@
#include "content/browser/battery_status/battery_status_manager_win.h"
#include "base/memory/ref_counted.h"
-#include "base/strings/string16.h"
-#include "base/win/message_window.h"
#include "base/win/windows_version.h"
#include "content/browser/battery_status/battery_status_manager.h"
#include "content/public/browser/browser_thread.h"
+#include "ui/gfx/win/singleton_hwnd.h"
namespace content {
@@ -17,19 +16,19 @@ namespace {
typedef BatteryStatusService::BatteryUpdateCallback BatteryCallback;
-const wchar_t kWindowClassName[] = L"BatteryStatusMessageWindow";
-
-// Message-only window for handling battery changes on Windows.
+// Listens to battery changes using a singleton message window.
class BatteryStatusObserver
- : public base::RefCountedThreadSafe<BatteryStatusObserver> {
+ : public gfx::SingletonHwnd::Observer,
+ public base::RefCountedThreadSafe<BatteryStatusObserver> {
public:
explicit BatteryStatusObserver(const BatteryCallback& callback)
: power_handle_(NULL),
battery_change_handle_(NULL),
- callback_(callback) {
+ callback_(callback),
+ started_(false) {
}
- virtual ~BatteryStatusObserver() { DCHECK(!window_); }
+ virtual ~BatteryStatusObserver() { DCHECK(!started_); }
void Start() {
// Need to start on the UI thread to receive battery status notifications.
@@ -49,37 +48,38 @@ class BatteryStatusObserver
private:
void StartOnUI() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- if (window_)
+ if (started_)
return;
- if (CreateMessageWindow()) {
- BatteryChanged();
- // RegisterPowerSettingNotification function work from Windows Vista
- // onwards. However even without them we will receive notifications,
- // e.g. when a power source is connected.
- // TODO(timvolodine) : consider polling for battery changes on windows
- // versions prior to Vista, see crbug.com/402466.
- power_handle_ =
- RegisterNotification(&GUID_ACDC_POWER_SOURCE);
- battery_change_handle_ =
- RegisterNotification(&GUID_BATTERY_PERCENTAGE_REMAINING);
- } else {
- // Could not create a message window, execute callback with the default
- // values.
- callback_.Run(blink::WebBatteryStatus());
- }
+ gfx::SingletonHwnd* message_window = gfx::SingletonHwnd::GetInstance();
+ message_window->AddObserver(this);
+
+ // RegisterPowerSettingNotification function work from Windows Vista
+ // onwards. However even without them we will receive notifications,
+ // e.g. when a power source is connected.
+ // TODO(timvolodine) : consider polling for battery changes on windows
+ // versions prior to Vista, see crbug.com/402466.
+ power_handle_ = RegisterNotification(&GUID_ACDC_POWER_SOURCE,
+ message_window->hwnd());
+ battery_change_handle_ =
+ RegisterNotification(&GUID_BATTERY_PERCENTAGE_REMAINING,
+ message_window->hwnd());
+ BatteryChanged();
+ started_ = true;
}
void StopOnUI() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- if (!window_)
+ if (!started_)
return;
if (power_handle_)
UnregisterNotification(power_handle_);
if (battery_change_handle_)
UnregisterNotification(battery_change_handle_);
- window_.reset();
+
+ gfx::SingletonHwnd::GetInstance()->RemoveObserver(this);
+ started_ = false;
}
void BatteryChanged() {
@@ -90,29 +90,27 @@ class BatteryStatusObserver
callback_.Run(blink::WebBatteryStatus());
}
- bool HandleMessage(UINT message,
- WPARAM wparam,
- LPARAM lparam,
- LRESULT* result) {
- switch(message) {
- case WM_POWERBROADCAST:
- if (wparam == PBT_APMPOWERSTATUSCHANGE ||
- wparam == PBT_POWERSETTINGCHANGE) {
- BatteryChanged();
- }
- *result = NULL;
- return true;
- default:
- return false;
+ // SingletonHwnd::Observer
+ void OnWndProc(HWND hwnd,
+ UINT message,
+ WPARAM wparam,
+ LPARAM lparam) OVERRIDE {
+ if (message != WM_POWERBROADCAST)
+ return;
+
+ if (wparam == PBT_APMPOWERSTATUSCHANGE ||
+ wparam == PBT_POWERSETTINGCHANGE) {
+ BatteryChanged();
}
}
- HPOWERNOTIFY RegisterNotification(LPCGUID power_setting) {
+ HPOWERNOTIFY RegisterNotification(LPCGUID power_setting, HWND hwnd) {
if (base::win::GetVersion() < base::win::VERSION_VISTA)
return NULL;
- return RegisterPowerSettingNotification(window_->hwnd(), power_setting,
- DEVICE_NOTIFY_WINDOW_HANDLE);
+ return RegisterPowerSettingNotification(hwnd,
+ power_setting,
+ DEVICE_NOTIFY_WINDOW_HANDLE);
}
BOOL UnregisterNotification(HPOWERNOTIFY handle) {
@@ -122,23 +120,10 @@ class BatteryStatusObserver
return UnregisterPowerSettingNotification(handle);
}
- bool CreateMessageWindow() {
- // TODO(timvolodine): consider reusing the message window of PowerMonitor.
- window_.reset(new base::win::MessageWindow());
- if (!window_->CreateNamed(base::Bind(&BatteryStatusObserver::HandleMessage,
- base::Unretained(this)),
- base::string16(kWindowClassName))) {
- LOG(ERROR) << "Failed to create message window: " << kWindowClassName;
- window_.reset();
- return false;
- }
- return true;
- }
-
HPOWERNOTIFY power_handle_;
HPOWERNOTIFY battery_change_handle_;
BatteryCallback callback_;
- scoped_ptr<base::win::MessageWindow> window_;
+ bool started_;
DISALLOW_COPY_AND_ASSIGN(BatteryStatusObserver);
};
« no previous file with comments | « no previous file | ui/gfx/win/singleton_hwnd.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698