No gfx::SingletonHwnd::Observer any more.

Using gfx::SingletonHwndObserver instead.
This commit is contained in:
Haojian Wu 2015-08-04 16:23:54 +08:00 committed by Cheng Zhao
parent b4e836bf2e
commit 48ccb0f2ab
2 changed files with 15 additions and 9 deletions

View file

@ -4,11 +4,14 @@
#include "chrome/browser/extensions/global_shortcut_listener_win.h" #include "chrome/browser/extensions/global_shortcut_listener_win.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/win/win_util.h" #include "base/win/win_util.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "ui/base/accelerators/accelerator.h" #include "ui/base/accelerators/accelerator.h"
#include "ui/events/event_constants.h" #include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_code_conversion_win.h" #include "ui/events/keycodes/keyboard_code_conversion_win.h"
#include "ui/gfx/win/singleton_hwnd.h"
using content::BrowserThread; using content::BrowserThread;
@ -35,14 +38,17 @@ GlobalShortcutListenerWin::~GlobalShortcutListenerWin() {
void GlobalShortcutListenerWin::StartListening() { void GlobalShortcutListenerWin::StartListening() {
DCHECK(!is_listening_); // Don't start twice. DCHECK(!is_listening_); // Don't start twice.
DCHECK(!hotkey_ids_.empty()); // Also don't start if no hotkey is registered. DCHECK(!hotkey_ids_.empty()); // Also don't start if no hotkey is registered.
gfx::SingletonHwnd::GetInstance()->AddObserver(this); singleton_hwnd_observer_.reset(new gfx::SingletonHwndObserver(
base::Bind(
&GlobalShortcutListenerWin::OnWndProc, base::Unretained(this))));
is_listening_ = true; is_listening_ = true;
} }
void GlobalShortcutListenerWin::StopListening() { void GlobalShortcutListenerWin::StopListening() {
DCHECK(is_listening_); // No point if we are not already listening. DCHECK(is_listening_); // No point if we are not already listening.
DCHECK(hotkey_ids_.empty()); // Make sure the map is clean before ending. DCHECK(hotkey_ids_.empty()); // Make sure the map is clean before ending.
gfx::SingletonHwnd::GetInstance()->RemoveObserver(this); singleton_hwnd_observer_.reset(nullptr);
is_listening_ = false; is_listening_ = false;
} }

View file

@ -7,26 +7,24 @@
#include <windows.h> #include <windows.h>
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/extensions/global_shortcut_listener.h" #include "chrome/browser/extensions/global_shortcut_listener.h"
#include "ui/gfx/win/singleton_hwnd.h" #include "ui/gfx/win/singleton_hwnd.h"
#include "ui/gfx/win/singleton_hwnd_observer.h"
namespace extensions { namespace extensions {
// Windows-specific implementation of the GlobalShortcutListener class that // Windows-specific implementation of the GlobalShortcutListener class that
// listens for global shortcuts. Handles setting up a keyboard hook and // listens for global shortcuts. Handles setting up a keyboard hook and
// forwarding its output to the base class for processing. // forwarding its output to the base class for processing.
class GlobalShortcutListenerWin : public GlobalShortcutListener, class GlobalShortcutListenerWin : public GlobalShortcutListener {
public gfx::SingletonHwnd::Observer {
public: public:
GlobalShortcutListenerWin(); GlobalShortcutListenerWin();
virtual ~GlobalShortcutListenerWin(); virtual ~GlobalShortcutListenerWin();
private: private:
// The implementation of our Window Proc, called by SingletonHwnd. // The implementation of our Window Proc, called by SingletonHwndObserver.
virtual void OnWndProc(HWND hwnd, void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
UINT message,
WPARAM wparam,
LPARAM lparam) override;
// GlobalShortcutListener implementation. // GlobalShortcutListener implementation.
virtual void StartListening() override; virtual void StartListening() override;
@ -43,6 +41,8 @@ class GlobalShortcutListenerWin : public GlobalShortcutListener,
typedef std::map<ui::Accelerator, int> HotkeyIdMap; typedef std::map<ui::Accelerator, int> HotkeyIdMap;
HotkeyIdMap hotkey_ids_; HotkeyIdMap hotkey_ids_;
scoped_ptr<gfx::SingletonHwndObserver> singleton_hwnd_observer_;
DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerWin); DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerWin);
}; };