Update GlobalShortcut API design.

* Rename Shortcut API to GlobalShortcut for better suite.
* Implement the new design interfaces.
* Put the chromium related source code to the same directory as in chrome.
This commit is contained in:
Haojian Wu 2014-07-31 12:22:29 +08:00
parent b2217474c1
commit 035679057e
16 changed files with 288 additions and 302 deletions

View file

@ -1,18 +1,16 @@
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/global_shortcut_listener.h"
#include "base/logging.h"
#include "chrome/browser/ui/shortcut/global_shortcut_listener.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/accelerators/accelerator.h"
using content::BrowserThread;
namespace atom {
namespace api {
namespace extensions {
GlobalShortcutListener::GlobalShortcutListener()
: shortcut_handling_suspended_(false) {
@ -21,6 +19,7 @@ GlobalShortcutListener::GlobalShortcutListener()
GlobalShortcutListener::~GlobalShortcutListener() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(accelerator_map_.empty()); // Make sure we've cleaned up.
}
bool GlobalShortcutListener::RegisterAccelerator(
@ -55,8 +54,8 @@ void GlobalShortcutListener::UnregisterAccelerator(
return;
AcceleratorMap::iterator it = accelerator_map_.find(accelerator);
if (it == accelerator_map_.end())
return;
// We should never get asked to unregister something that we didn't register.
DCHECK(it != accelerator_map_.end());
// The caller should call this function with the right observer.
DCHECK(it->second == observer);
@ -120,6 +119,4 @@ void GlobalShortcutListener::NotifyKeyPressed(
iter->second->OnKeyPressed(accelerator);
}
} // namespace api
} // namespace atom
} // namespace extensions

View file

@ -1,9 +1,9 @@
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_SHORTCUT_GLOBAL_SHORTCUT_LISTENER_H_
#define CHROME_BROWSER_UI_SHORTCUT_GLOBAL_SHORTCUT_LISTENER_H_
#ifndef CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_H_
#define CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_H_
#include <map>
@ -14,9 +14,7 @@ namespace ui {
class Accelerator;
}
namespace atom {
namespace api {
namespace extensions {
// Platform-neutral implementation of a class that keeps track of observers and
// monitors keystrokes. It relays messages to the appropriate observer when a
@ -61,9 +59,6 @@ class GlobalShortcutListener {
// Returns whether shortcut handling is currently suspended.
bool IsShortcutHandlingSuspended() const;
// Returen whether accelerator is registered.
virtual bool IsAcceleratorRegistered(const ui::Accelerator& accelerator) = 0;
protected:
GlobalShortcutListener();
@ -99,7 +94,6 @@ class GlobalShortcutListener {
DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListener);
};
} // namespace api
} // namespace extensions
} // namespace atom
#endif // CHROME_BROWSER_UI_SHORTCUT_GLOBAL_SHORTCUT_LISTENER_H_
#endif // CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_H_

View file

@ -1,11 +1,11 @@
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_SHORTCUT_GLOBAL_SHORTCUT_LISTENER_MAC_H_
#define CHROME_BROWSER_UI_SHORTCUT_GLOBAL_SHORTCUT_LISTENER_MAC_H_
#ifndef CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_
#define CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_
#include "chrome/browser/ui/shortcut/global_shortcut_listener.h"
#include "chrome/browser/extensions/global_shortcut_listener.h"
#include <Carbon/Carbon.h>
#include <CoreFoundation/CoreFoundation.h>
@ -14,9 +14,7 @@
#include "base/mac/scoped_nsobject.h"
namespace atom {
namespace api {
namespace extensions {
// Mac-specific implementation of the GlobalShortcutListener class that
// listens for global shortcuts. Handles basic keyboard intercepting and
@ -32,7 +30,6 @@ class GlobalShortcutListenerMac : public GlobalShortcutListener {
GlobalShortcutListenerMac();
virtual ~GlobalShortcutListenerMac();
virtual bool IsAcceleratorRegistered(const ui::Accelerator& accelerator) OVERRIDE;
private:
typedef int KeyId;
typedef std::map<ui::Accelerator, KeyId> AcceleratorIdMap;
@ -104,8 +101,6 @@ class GlobalShortcutListenerMac : public GlobalShortcutListener {
DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerMac);
};
} // namespace api
} // namespace extensions
} // namespace atom
#endif // CHROME_BROWSER_UI_SHORTCUT_GLOBAL_SHORTCUT_LISTENER_MAC_H_
#endif // CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_MAC_H_

View file

@ -1,8 +1,8 @@
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/shortcut/global_shortcut_listener_mac.h"
#include "chrome/browser/extensions/global_shortcut_listener_mac.h"
#include <ApplicationServices/ApplicationServices.h>
#import <Cocoa/Cocoa.h>
@ -15,7 +15,7 @@
#import "ui/events/keycodes/keyboard_code_conversion_mac.h"
using content::BrowserThread;
using atom::api::GlobalShortcutListenerMac;
using extensions::GlobalShortcutListenerMac;
namespace {
@ -48,9 +48,7 @@ bool IsMediaKey(const ui::Accelerator& accelerator) {
} // namespace
namespace atom {
namespace api {
namespace extensions {
// static
GlobalShortcutListener* GlobalShortcutListener::GetInstance() {
@ -86,11 +84,6 @@ GlobalShortcutListenerMac::~GlobalShortcutListenerMac() {
StopWatchingHotKeys();
}
bool GlobalShortcutListenerMac::IsAcceleratorRegistered(
const ui::Accelerator& accelerator) {
return accelerator_ids_.find(accelerator) != accelerator_ids_.end();
}
void GlobalShortcutListenerMac::StartListening() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
@ -387,6 +380,4 @@ OSStatus GlobalShortcutListenerMac::HotKeyHandler(
return noErr;
}
} // namespace api
} // namespace atom
} // namespace extensions

View file

@ -1,8 +1,8 @@
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/shortcut/global_shortcut_listener_win.h"
#include "chrome/browser/extensions/global_shortcut_listener_win.h"
#include "base/win/win_util.h"
#include "content/public/browser/browser_thread.h"
@ -12,9 +12,7 @@
using content::BrowserThread;
namespace atom {
namespace api {
namespace extensions {
// static
GlobalShortcutListener* GlobalShortcutListener::GetInstance() {
@ -34,11 +32,6 @@ GlobalShortcutListenerWin::~GlobalShortcutListenerWin() {
StopListening();
}
bool GlobalShortcutListenerWin::IsAcceleratorRegistered(
const ui::Accelerator& accelerator) {
return hotkey_ids_.find(accelerator) != hotkey_ids_.end();
}
void GlobalShortcutListenerWin::StartListening() {
DCHECK(!is_listening_); // Don't start twice.
DCHECK(!hotkey_ids_.empty()); // Also don't start if no hotkey is registered.
@ -80,7 +73,6 @@ bool GlobalShortcutListenerWin::RegisterAcceleratorImpl(
modifiers |= accelerator.IsCtrlDown() ? MOD_CONTROL : 0;
modifiers |= accelerator.IsAltDown() ? MOD_ALT : 0;
static int hotkey_id = 0;
//bool success = false;
bool success = !!RegisterHotKey(
gfx::SingletonHwnd::GetInstance()->hwnd(),
hotkey_id,
@ -101,8 +93,6 @@ void GlobalShortcutListenerWin::UnregisterAcceleratorImpl(
HotkeyIdMap::iterator it = hotkey_ids_.find(accelerator);
DCHECK(it != hotkey_ids_.end());
//bool success = false;
gfx::SingletonHwnd::GetInstance();
bool success = !!UnregisterHotKey(
gfx::SingletonHwnd::GetInstance()->hwnd(), it->second);
// This call should always succeed, as long as we pass in the right HWND and
@ -112,6 +102,4 @@ void GlobalShortcutListenerWin::UnregisterAcceleratorImpl(
hotkey_ids_.erase(it);
}
} // namespace api
} // namespace atom
} // namespace extensions

View file

@ -1,18 +1,16 @@
// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_SHORTCUT_GLOBAL_SHORTCUT_LISTENER_WIN_H_
#define CHROME_BROWSER_UI_SHORTCUT_GLOBAL_SHORTCUT_LISTENER_WIN_H_
#ifndef CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_WIN_H_
#define CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_WIN_H_
#include <windows.h>
#include "chrome/browser/ui/shortcut/global_shortcut_listener.h"
#include "chrome/browser/extensions/global_shortcut_listener.h"
#include "ui/gfx/win/singleton_hwnd.h"
namespace atom {
namespace api {
namespace extensions {
// Windows-specific implementation of the GlobalShortcutListener class that
// listens for global shortcuts. Handles setting up a keyboard hook and
@ -23,9 +21,6 @@ class GlobalShortcutListenerWin : public GlobalShortcutListener,
GlobalShortcutListenerWin();
virtual ~GlobalShortcutListenerWin();
virtual bool IsAcceleratorRegistered(
const ui::Accelerator& accelerator) OVERRIDE;
private:
// The implementation of our Window Proc, called by SingletonHwnd.
virtual void OnWndProc(HWND hwnd,
@ -51,8 +46,6 @@ class GlobalShortcutListenerWin : public GlobalShortcutListener,
DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerWin);
};
} // namespace api
} // namespace extensions
} // namespace atom
#endif // CHROME_BROWSER_UI_SHORTCUT_GLOBAL_SHORTCUT_LISTENER_WIN_H_
#endif // CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_WIN_H_

View file

@ -1,16 +1,21 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/shortcut/global_shortcut_listener_x11.h"
#include "chrome/browser/extensions/global_shortcut_listener_x11.h"
#include "base/message_loop/message_pump_x11.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/events/keycodes/keyboard_code_conversion_x.h"
#include "ui/gfx/x/x11_error_tracker.h"
#include "ui/gfx/x/x11_types.h"
#if defined(TOOLKIT_GTK)
#include <gdk/gdkx.h>
#else
#include "base/message_loop/message_pump_x11.h"
#endif
using content::BrowserThread;
namespace {
@ -41,9 +46,7 @@ int GetNativeModifiers(const ui::Accelerator& accelerator) {
} // namespace
namespace atom {
namespace api {
namespace extensions {
// static
GlobalShortcutListener* GlobalShortcutListener::GetInstance() {
@ -65,16 +68,17 @@ GlobalShortcutListenerX11::~GlobalShortcutListenerX11() {
StopListening();
}
bool GlobalShortcutListenerX11::IsAcceleratorRegistered(
const ui::Accelerator& accelerator) {
return registered_hot_keys_.find(accelerator) != registered_hot_keys_.end();
}
void GlobalShortcutListenerX11::StartListening() {
DCHECK(!is_listening_); // Don't start twice.
DCHECK(!registered_hot_keys_.empty()); // Also don't start if no hotkey is
// registered.
#if defined(TOOLKIT_GTK)
gdk_window_add_filter(gdk_get_default_root_window(),
&GlobalShortcutListenerX11::OnXEventThunk,
this);
#else
base::MessagePumpX11::Current()->AddDispatcherForRootWindow(this);
#endif
is_listening_ = true;
}
@ -84,17 +88,25 @@ void GlobalShortcutListenerX11::StopListening() {
DCHECK(registered_hot_keys_.empty()); // Make sure the set is clean before
// ending.
#if defined(TOOLKIT_GTK)
gdk_window_remove_filter(NULL,
&GlobalShortcutListenerX11::OnXEventThunk,
this);
#else
base::MessagePumpX11::Current()->RemoveDispatcherForRootWindow(this);
#endif
is_listening_ = false;
}
#if !defined(TOOLKIT_GTK)
uint32_t GlobalShortcutListenerX11::Dispatch(const base::NativeEvent& event) {
if (event->type == KeyPress)
OnXKeyPressEvent(event);
return POST_DISPATCH_NONE;
}
#endif
bool GlobalShortcutListenerX11::RegisterAcceleratorImpl(
const ui::Accelerator& accelerator) {
@ -142,6 +154,17 @@ void GlobalShortcutListenerX11::UnregisterAcceleratorImpl(
registered_hot_keys_.erase(accelerator);
}
#if defined(TOOLKIT_GTK)
GdkFilterReturn GlobalShortcutListenerX11::OnXEvent(GdkXEvent* gdk_x_event,
GdkEvent* gdk_event) {
XEvent* x_event = static_cast<XEvent*>(gdk_x_event);
if (x_event->type == KeyPress)
OnXKeyPressEvent(x_event);
return GDK_FILTER_CONTINUE;
}
#endif
void GlobalShortcutListenerX11::OnXKeyPressEvent(::XEvent* x_event) {
DCHECK(x_event->type == KeyPress);
int modifiers = 0;
@ -155,6 +178,4 @@ void GlobalShortcutListenerX11::OnXKeyPressEvent(::XEvent* x_event) {
NotifyKeyPressed(accelerator);
}
} // namespace api
} // namespace atom
} // namespace extensions

View file

@ -1,35 +1,40 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_SHORTCUT_GLOBAL_SHORTCUT_LISTENER_X11_H_
#define CHROME_BROWSER_UI_SHORTCUT_GLOBAL_SHORTCUT_LISTENER_X11_H_
#ifndef CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_X11_H_
#define CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_X11_H_
#include <X11/Xlib.h>
#include <set>
#include "base/message_loop/message_pump_dispatcher.h"
#include "chrome/browser/ui/shortcut/global_shortcut_listener.h"
#include "chrome/browser/extensions/global_shortcut_listener.h"
namespace atom {
#if defined(TOOLKIT_GTK)
#include <gtk/gtk.h>
#include "ui/base/gtk/gtk_signal.h"
#endif // defined(TOOLKIT_GTK)
namespace api {
namespace extensions {
// X11-specific implementation of the GlobalShortcutListener class that
// listens for global shortcuts. Handles basic keyboard intercepting and
// forwards its output to the base class for processing.
class GlobalShortcutListenerX11
: public base::MessagePumpDispatcher,
:
#if !defined(TOOLKIT_GTK)
public base::MessagePumpDispatcher,
#endif
public GlobalShortcutListener {
public:
GlobalShortcutListenerX11();
virtual ~GlobalShortcutListenerX11();
#if !defined(TOOLKIT_GTK)
// base::MessagePumpDispatcher implementation.
virtual uint32_t Dispatch(const base::NativeEvent& event) OVERRIDE;
virtual bool IsAcceleratorRegistered(
const ui::Accelerator& accelerator) OVERRIDE;
#endif
private:
// GlobalShortcutListener implementation.
@ -40,6 +45,12 @@ class GlobalShortcutListenerX11
virtual void UnregisterAcceleratorImpl(
const ui::Accelerator& accelerator) OVERRIDE;
#if defined(TOOLKIT_GTK)
// Callback for XEvents of the default root window.
CHROMEG_CALLBACK_1(GlobalShortcutListenerX11, GdkFilterReturn,
OnXEvent, GdkXEvent*, GdkEvent*);
#endif
// Invoked when a global shortcut is pressed.
void OnXKeyPressEvent(::XEvent* x_event);
@ -57,8 +68,6 @@ class GlobalShortcutListenerX11
DISALLOW_COPY_AND_ASSIGN(GlobalShortcutListenerX11);
};
} // namespace api
} // namespace extensions
} // namespace atom
#endif // CHROME_BROWSER_UI_SHORTCUT_GLOBAL_SHORTCUT_LISTENER_X11_H_
#endif // CHROME_BROWSER_EXTENSIONS_GLOBAL_SHORTCUT_LISTENER_X11_H_