Chromium Code Reviews| Index: remoting/host/local_input_monitor_mac.mm |
| diff --git a/remoting/host/local_input_monitor_mac.mm b/remoting/host/local_input_monitor_mac.mm |
| index 99a56be9b6f1fff53324a02694cd518957b7978d..8f5227ca91ebbf2cbe41604a9809d00008927c23 100644 |
| --- a/remoting/host/local_input_monitor_mac.mm |
| +++ b/remoting/host/local_input_monitor_mac.mm |
| @@ -14,24 +14,48 @@ |
| #include "base/mac/scoped_cftyperef.h" |
| #include "base/memory/ref_counted.h" |
| #include "base/synchronization/lock.h" |
| -#include "remoting/host/chromoting_host.h" |
| +#include "remoting/host/mouse_move_observer.h" |
| +#include "third_party/skia/include/core/SkPoint.h" |
| #import "third_party/GTM/AppKit/GTMCarbonEvent.h" |
| // Esc Key Code is 53. |
| // http://boredzo.org/blog/wp-content/uploads/2007/05/IMTx-virtual-keycodes.pdf |
| static const NSUInteger kEscKeyCode = 53; |
| +namespace remoting { |
| + |
| namespace { |
| -typedef std::set<scoped_refptr<remoting::ChromotingHost> > Hosts; |
| -} |
| -@interface LocalInputMonitorImpl : NSObject { |
| +class LocalInputMonitorMac : public LocalInputMonitor { |
|
Wez
2012/05/25 23:32:18
Why have you moved this definition into an anonymo
Sergey Ulanov
2012/05/26 01:06:15
It was that way before my change. I just moved dec
|
| + public: |
| + LocalInputMonitorMac() : mouse_move_observer_(NULL) {} |
| + virtual ~LocalInputMonitorMac(); |
| + virtual void Start(MouseMoveObserver* mouse_move_observer, |
| + const base::Closure& disconnect_callback) OVERRIDE; |
| + virtual void Stop() OVERRIDE; |
| + |
| + void OnLocalMouseMoved(const SkIPoint& new_pos); |
| + void OnDisconnectShortcut(); |
| + |
| + private: |
| + MouseMoveObserver* mouse_move_observer_; |
| + base::Closure disconnect_callback_; |
| + DISALLOW_COPY_AND_ASSIGN(LocalInputMonitorMac); |
| +}; |
| + |
| +typedef std::set<remoting::LocalInputMonitorMac*> LocalInputMonitors; |
| + |
| +} // namespace |
| + |
| +} // namespace remoting |
| + |
| +@interface LocalInputMonitorManager : NSObject { |
| @private |
| GTMCarbonHotKey* hotKey_; |
| CFRunLoopSourceRef mouseRunLoopSource_; |
| base::mac::ScopedCFTypeRef<CFMachPortRef> mouseMachPort_; |
| - base::Lock hostsLock_; |
| - Hosts hosts_; |
| + base::Lock lock_; |
| + remoting::LocalInputMonitors monitors_; |
| } |
| // Called when the hotKey is hit. |
| @@ -40,20 +64,16 @@ typedef std::set<scoped_refptr<remoting::ChromotingHost> > Hosts; |
| // Called when the local mouse moves |
| - (void)localMouseMoved:(const SkIPoint&)mousePos; |
| -// Must be called when the LocalInputMonitorImpl is no longer to be used. |
| +// Must be called when the LocalInputMonitorManager is no longer to be used. |
| // Similar to NSTimer in that more than a simple release is required. |
| - (void)invalidate; |
| -// Called to add a host to the list of those to be Shutdown() when the hotkey |
| -// is pressed. |
| -- (void)addHost:(remoting::ChromotingHost*)host; |
| - |
| -// Called to remove a host. Returns true if it was the last host being |
| -// monitored, in which case the object should be destroyed. |
| -- (bool)removeHost:(remoting::ChromotingHost*)host; |
| +// Called to add a monitor. |
| +- (void)addMonitor:(remoting::LocalInputMonitorMac*)monitor; |
| -// Disabled disconnection keyboard shortcut. |
| -- (void)disableShortcut; |
| +// Called to remove a monitor. Returns true if it was the last |
| +// monitor, in which case the object should be destroyed. |
| +- (bool)removeMonitor:(remoting::LocalInputMonitorMac*)monitor; |
| @end |
| @@ -63,12 +83,12 @@ static CGEventRef LocalMouseMoved(CGEventTapProxy proxy, CGEventType type, |
| if (pid == 0) { |
| CGPoint cgMousePos = CGEventGetLocation(event); |
| SkIPoint mousePos = SkIPoint::Make(cgMousePos.x, cgMousePos.y); |
| - [static_cast<LocalInputMonitorImpl*>(context) localMouseMoved:mousePos]; |
| + [static_cast<LocalInputMonitorManager*>(context) localMouseMoved:mousePos]; |
| } |
| return NULL; |
| } |
| -@implementation LocalInputMonitorImpl |
| +@implementation LocalInputMonitorManager |
| - (id)init { |
| if ((self = [super init])) { |
| @@ -103,16 +123,18 @@ static CGEventRef LocalMouseMoved(CGEventTapProxy proxy, CGEventType type, |
| } |
| - (void)hotKeyHit:(GTMCarbonHotKey*)hotKey { |
| - base::AutoLock lock(hostsLock_); |
| - for (Hosts::const_iterator i = hosts_.begin(); i != hosts_.end(); ++i) { |
| - (*i)->Shutdown(base::Closure()); |
| + base::AutoLock lock(lock_); |
| + for (remoting::LocalInputMonitors::const_iterator i = monitors_.begin(); |
| + i != monitors_.end(); ++i) { |
| + (*i)->OnDisconnectShortcut(); |
| } |
| } |
| - (void)localMouseMoved:(const SkIPoint&)mousePos { |
| - base::AutoLock lock(hostsLock_); |
| - for (Hosts::const_iterator i = hosts_.begin(); i != hosts_.end(); ++i) { |
| - (*i)->LocalMouseMoved(mousePos); |
| + base::AutoLock lock(lock_); |
| + for (remoting::LocalInputMonitors::const_iterator i = monitors_.begin(); |
| + i != monitors_.end(); ++i) { |
| + (*i)->OnLocalMouseMoved(mousePos); |
| } |
| } |
| @@ -133,24 +155,15 @@ static CGEventRef LocalMouseMoved(CGEventTapProxy proxy, CGEventType type, |
| } |
| } |
| -- (void)addHost:(remoting::ChromotingHost*)host { |
| - base::AutoLock lock(hostsLock_); |
| - hosts_.insert(host); |
| +- (void)addMonitor:(remoting::LocalInputMonitorMac*)monitor { |
| + base::AutoLock lock(lock_); |
| + monitors_.insert(monitor); |
| } |
| -- (bool)removeHost:(remoting::ChromotingHost*)host { |
| - base::AutoLock lock(hostsLock_); |
| - hosts_.erase(host); |
| - return hosts_.empty(); |
| -} |
| - |
| -- (void)disableShortcut { |
| - if (hotKey_) { |
| - GTMCarbonEventDispatcherHandler* handler = |
| - [GTMCarbonEventDispatcherHandler sharedEventDispatcherHandler]; |
| - [handler unregisterHotKey:hotKey_]; |
| - hotKey_ = NULL; |
| - } |
| +- (bool)removeMonitor:(remoting::LocalInputMonitorMac*)monitor { |
| + base::AutoLock lock(lock_); |
| + monitors_.erase(monitor); |
| + return monitors_.empty(); |
| } |
| @end |
| @@ -159,21 +172,9 @@ namespace remoting { |
| namespace { |
| -class LocalInputMonitorMac : public LocalInputMonitor { |
| - public: |
| - LocalInputMonitorMac() : host_(NULL) {} |
| - virtual ~LocalInputMonitorMac(); |
| - virtual void Start(ChromotingHost* host) OVERRIDE; |
| - virtual void Stop() OVERRIDE; |
| - virtual void DisableShortcutOnMac() OVERRIDE; |
| - |
| - private: |
| - ChromotingHost* host_; |
| - DISALLOW_COPY_AND_ASSIGN(LocalInputMonitorMac); |
| -}; |
| - |
| -base::LazyInstance<base::Lock>::Leaky monitor_lock = LAZY_INSTANCE_INITIALIZER; |
| -LocalInputMonitorImpl* local_input_monitor = NULL; |
| +base::LazyInstance<base::Lock>::Leaky g_monitor_lock = |
| + LAZY_INSTANCE_INITIALIZER; |
| +LocalInputMonitorManager* g_local_input_monitor_manager = NULL; |
| } // namespace |
| @@ -181,26 +182,32 @@ LocalInputMonitorMac::~LocalInputMonitorMac() { |
| Stop(); |
| } |
| -void LocalInputMonitorMac::Start(ChromotingHost* host) { |
| - base::AutoLock lock(monitor_lock.Get()); |
| - if (!local_input_monitor) |
| - local_input_monitor = [[LocalInputMonitorImpl alloc] init]; |
| - CHECK(local_input_monitor); |
| - [local_input_monitor addHost:host]; |
| - host_ = host; |
| +void LocalInputMonitorMac::Start(MouseMoveObserver* mouse_move_observer, |
| + const base::Closure& disconnect_callback) { |
| + base::AutoLock lock(g_monitor_lock.Get()); |
| + if (!g_local_input_monitor_manager) |
| + g_local_input_monitor_manager = [[LocalInputMonitorManager alloc] init]; |
| + CHECK(g_local_input_monitor_manager); |
| + mouse_move_observer_ = mouse_move_observer; |
| + disconnect_callback_ = disconnect_callback; |
| + [g_local_input_monitor_manager addMonitor:this]; |
| } |
| void LocalInputMonitorMac::Stop() { |
| - base::AutoLock lock(monitor_lock.Get()); |
| - if ([local_input_monitor removeHost:host_]) { |
| - [local_input_monitor invalidate]; |
| - [local_input_monitor release]; |
| - local_input_monitor = nil; |
| + base::AutoLock lock(g_monitor_lock.Get()); |
| + if ([g_local_input_monitor_manager removeMonitor:this]) { |
| + [g_local_input_monitor_manager invalidate]; |
| + [g_local_input_monitor_manager release]; |
| + g_local_input_monitor_manager = nil; |
| } |
| } |
| -void LocalInputMonitorMac::DisableShortcutOnMac() { |
| - [local_input_monitor disableShortcut]; |
| +void LocalInputMonitorMac::OnLocalMouseMoved(const SkIPoint& new_pos) { |
| + mouse_move_observer_->OnLocalMouseMoved(new_pos); |
| +} |
| + |
| +void LocalInputMonitorMac::OnDisconnectShortcut() { |
| + disconnect_callback_.Run(); |
| } |
| scoped_ptr<LocalInputMonitor> LocalInputMonitor::Create() { |