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..089f2bab3b8705bc77e426a03acd2ead5fe178c2 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; |
| -} |
| + |
| +class LocalInputMonitorMac : public LocalInputMonitor { |
|
Jamie
2012/05/25 21:19:24
I realize that you inherited this from the older c
Sergey Ulanov
2012/05/25 21:59:30
Yes, I agree that the current terminology is confu
|
| + 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 OnMouseMoved(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 LocalInputMonitorImpl : 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. |
| @@ -44,16 +68,12 @@ typedef std::set<scoped_refptr<remoting::ChromotingHost> > Hosts; |
| // 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 mouse move observers. Returns true if it was the |
|
Jamie
2012/05/25 21:19:24
s/observers/observer/
Sergey Ulanov
2012/05/25 21:59:30
It actually removes monitor.
|
| +// last observer, in which case the object should be destroyed. |
| +- (bool)removeMonitor:(remoting::LocalInputMonitorMac*)monitor; |
| @end |
| @@ -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)->OnMouseMoved(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; |
| +LocalInputMonitorImpl* g_local_input_monitor = 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) |
| + g_local_input_monitor = [[LocalInputMonitorImpl alloc] init]; |
| + CHECK(g_local_input_monitor); |
| + mouse_move_observer_ = mouse_move_observer; |
| + disconnect_callback_ = disconnect_callback; |
| + [g_local_input_monitor 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 removeMonitor:this]) { |
| + [g_local_input_monitor invalidate]; |
| + [g_local_input_monitor release]; |
| + g_local_input_monitor = nil; |
| } |
| } |
| -void LocalInputMonitorMac::DisableShortcutOnMac() { |
| - [local_input_monitor disableShortcut]; |
| +void LocalInputMonitorMac::OnMouseMoved(const SkIPoint& new_pos) { |
| + mouse_move_observer_->OnMouseMoved(new_pos); |
| +} |
| + |
| +void LocalInputMonitorMac::OnDisconnectShortcut() { |
| + disconnect_callback_.Run(); |
| } |
| scoped_ptr<LocalInputMonitor> LocalInputMonitor::Create() { |