| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/plugin/plugin_interpose_util_mac.h" | |
| 6 | |
| 7 #import <AppKit/AppKit.h> | |
| 8 #import <objc/runtime.h> | |
| 9 #include <stdint.h> | |
| 10 | |
| 11 #include "content/child/npapi/webplugin_delegate_impl.h" | |
| 12 #include "content/common/plugin_process_messages.h" | |
| 13 #include "content/plugin/plugin_thread.h" | |
| 14 | |
| 15 using content::PluginThread; | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // Brings the plugin process to the front so that the user can see its windows. | |
| 20 void SwitchToPluginProcess() { | |
| 21 ProcessSerialNumber this_process, front_process; | |
| 22 if ((GetCurrentProcess(&this_process) != noErr) || | |
| 23 (GetFrontProcess(&front_process) != noErr)) { | |
| 24 return; | |
| 25 } | |
| 26 | |
| 27 Boolean matched = false; | |
| 28 if ((SameProcess(&this_process, &front_process, &matched) == noErr) && | |
| 29 !matched) { | |
| 30 SetFrontProcess(&this_process); | |
| 31 } | |
| 32 } | |
| 33 | |
| 34 // Sends a message to the browser process to inform it that the given window | |
| 35 // has been shown. | |
| 36 void NotifyBrowserOfPluginShowWindow(uint32_t window_id, | |
| 37 CGRect bounds, | |
| 38 bool modal) { | |
| 39 PluginThread* plugin_thread = PluginThread::current(); | |
| 40 if (plugin_thread) { | |
| 41 gfx::Rect window_bounds(bounds); | |
| 42 plugin_thread->Send( | |
| 43 new PluginProcessHostMsg_PluginShowWindow(window_id, window_bounds, | |
| 44 modal)); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 // Sends a message to the browser process to inform it that the given window | |
| 49 // has been hidden, and switches focus back to the browser process if there are | |
| 50 // no remaining plugin windows. | |
| 51 void NotifyBrowserOfPluginHideWindow(uint32_t window_id, CGRect bounds) { | |
| 52 PluginThread* plugin_thread = PluginThread::current(); | |
| 53 if (plugin_thread) { | |
| 54 gfx::Rect window_bounds(bounds); | |
| 55 plugin_thread->Send( | |
| 56 new PluginProcessHostMsg_PluginHideWindow(window_id, window_bounds)); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 // Informs the host that the plugin has changed the cursor visibility. | |
| 61 void NotifyPluginOfSetCursorVisibility(bool visibility) { | |
| 62 PluginThread* plugin_thread = PluginThread::current(); | |
| 63 if (plugin_thread) { | |
| 64 plugin_thread->Send( | |
| 65 new PluginProcessHostMsg_PluginSetCursorVisibility(visibility)); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 struct WindowInfo { | |
| 70 uint32_t window_id; | |
| 71 CGRect bounds; | |
| 72 WindowInfo(NSWindow* window) { | |
| 73 NSInteger window_num = [window windowNumber]; | |
| 74 window_id = window_num > 0 ? window_num : 0; | |
| 75 bounds = NSRectToCGRect([window frame]); | |
| 76 } | |
| 77 }; | |
| 78 | |
| 79 void OnPluginWindowClosed(const WindowInfo& window_info) { | |
| 80 if (window_info.window_id == 0) | |
| 81 return; | |
| 82 NotifyBrowserOfPluginHideWindow(window_info.window_id, window_info.bounds); | |
| 83 } | |
| 84 | |
| 85 BOOL g_waiting_for_window_number = NO; | |
| 86 | |
| 87 void OnPluginWindowShown(const WindowInfo& window_info, BOOL is_modal) { | |
| 88 // The window id is 0 if it has never been shown (including while it is the | |
| 89 // process of being shown for the first time); when that happens, we'll catch | |
| 90 // it in _setWindowNumber instead. | |
| 91 static BOOL s_pending_display_is_modal = NO; | |
| 92 if (window_info.window_id == 0) { | |
| 93 g_waiting_for_window_number = YES; | |
| 94 if (is_modal) | |
| 95 s_pending_display_is_modal = YES; | |
| 96 return; | |
| 97 } | |
| 98 g_waiting_for_window_number = NO; | |
| 99 if (s_pending_display_is_modal) { | |
| 100 is_modal = YES; | |
| 101 s_pending_display_is_modal = NO; | |
| 102 } | |
| 103 NotifyBrowserOfPluginShowWindow(window_info.window_id, window_info.bounds, | |
| 104 is_modal); | |
| 105 } | |
| 106 | |
| 107 } // namespace | |
| 108 | |
| 109 @interface NSWindow (ChromePluginUtilities) | |
| 110 // Returns YES if the window is visible and actually on the screen. | |
| 111 - (BOOL)chromePlugin_isWindowOnScreen; | |
| 112 @end | |
| 113 | |
| 114 @implementation NSWindow (ChromePluginUtilities) | |
| 115 | |
| 116 - (BOOL)chromePlugin_isWindowOnScreen { | |
| 117 if (![self isVisible]) | |
| 118 return NO; | |
| 119 NSRect window_frame = [self frame]; | |
| 120 for (NSScreen* screen in [NSScreen screens]) { | |
| 121 if (NSIntersectsRect(window_frame, [screen frame])) | |
| 122 return YES; | |
| 123 } | |
| 124 return NO; | |
| 125 } | |
| 126 | |
| 127 @end | |
| 128 | |
| 129 @interface NSWindow (ChromePluginInterposing) | |
| 130 - (void)chromePlugin_orderOut:(id)sender; | |
| 131 - (void)chromePlugin_orderFront:(id)sender; | |
| 132 - (void)chromePlugin_makeKeyAndOrderFront:(id)sender; | |
| 133 - (void)chromePlugin_setWindowNumber:(NSInteger)num; | |
| 134 @end | |
| 135 | |
| 136 @implementation NSWindow (ChromePluginInterposing) | |
| 137 | |
| 138 - (void)chromePlugin_orderOut:(id)sender { | |
| 139 WindowInfo window_info(self); | |
| 140 [self chromePlugin_orderOut:sender]; | |
| 141 OnPluginWindowClosed(window_info); | |
| 142 } | |
| 143 | |
| 144 - (void)chromePlugin_orderFront:(id)sender { | |
| 145 [self chromePlugin_orderFront:sender]; | |
| 146 if ([self chromePlugin_isWindowOnScreen]) | |
| 147 SwitchToPluginProcess(); | |
| 148 OnPluginWindowShown(WindowInfo(self), NO); | |
| 149 } | |
| 150 | |
| 151 - (void)chromePlugin_makeKeyAndOrderFront:(id)sender { | |
| 152 [self chromePlugin_makeKeyAndOrderFront:sender]; | |
| 153 if ([self chromePlugin_isWindowOnScreen]) | |
| 154 SwitchToPluginProcess(); | |
| 155 OnPluginWindowShown(WindowInfo(self), NO); | |
| 156 } | |
| 157 | |
| 158 - (void)chromePlugin_setWindowNumber:(NSInteger)num { | |
| 159 if (!g_waiting_for_window_number || num <= 0) { | |
| 160 [self chromePlugin_setWindowNumber:num]; | |
| 161 return; | |
| 162 } | |
| 163 SwitchToPluginProcess(); | |
| 164 [self chromePlugin_setWindowNumber:num]; | |
| 165 OnPluginWindowShown(WindowInfo(self), NO); | |
| 166 } | |
| 167 | |
| 168 @end | |
| 169 | |
| 170 @interface NSApplication (ChromePluginInterposing) | |
| 171 - (NSInteger)chromePlugin_runModalForWindow:(NSWindow*)window; | |
| 172 @end | |
| 173 | |
| 174 @implementation NSApplication (ChromePluginInterposing) | |
| 175 | |
| 176 - (NSInteger)chromePlugin_runModalForWindow:(NSWindow*)window { | |
| 177 SwitchToPluginProcess(); | |
| 178 // This is out-of-order relative to the other calls, but runModalForWindow: | |
| 179 // won't return until the window closes, and the order only matters for | |
| 180 // full-screen windows. | |
| 181 OnPluginWindowShown(WindowInfo(window), YES); | |
| 182 return [self chromePlugin_runModalForWindow:window]; | |
| 183 } | |
| 184 | |
| 185 @end | |
| 186 | |
| 187 @interface NSCursor (ChromePluginInterposing) | |
| 188 - (void)chromePlugin_set; | |
| 189 + (void)chromePlugin_hide; | |
| 190 + (void)chromePlugin_unhide; | |
| 191 @end | |
| 192 | |
| 193 @implementation NSCursor (ChromePluginInterposing) | |
| 194 | |
| 195 - (void)chromePlugin_set { | |
| 196 content::WebPluginDelegateImpl* delegate = | |
| 197 content::WebPluginDelegateImpl::GetActiveDelegate(); | |
| 198 if (delegate) { | |
| 199 delegate->SetNSCursor(self); | |
| 200 return; | |
| 201 } | |
| 202 [self chromePlugin_set]; | |
| 203 } | |
| 204 | |
| 205 + (void)chromePlugin_hide { | |
| 206 NotifyPluginOfSetCursorVisibility(false); | |
| 207 } | |
| 208 | |
| 209 + (void)chromePlugin_unhide { | |
| 210 NotifyPluginOfSetCursorVisibility(true); | |
| 211 } | |
| 212 | |
| 213 @end | |
| 214 | |
| 215 #pragma mark - | |
| 216 | |
| 217 static void ExchangeMethods(Class target_class, | |
| 218 BOOL class_method, | |
| 219 SEL original, | |
| 220 SEL replacement) { | |
| 221 Method m1; | |
| 222 Method m2; | |
| 223 if (class_method) { | |
| 224 m1 = class_getClassMethod(target_class, original); | |
| 225 m2 = class_getClassMethod(target_class, replacement); | |
| 226 } else { | |
| 227 m1 = class_getInstanceMethod(target_class, original); | |
| 228 m2 = class_getInstanceMethod(target_class, replacement); | |
| 229 } | |
| 230 if (m1 && m2) | |
| 231 method_exchangeImplementations(m1, m2); | |
| 232 else | |
| 233 NOTREACHED() << "Cocoa swizzling failed"; | |
| 234 } | |
| 235 | |
| 236 namespace mac_plugin_interposing { | |
| 237 | |
| 238 void SetUpCocoaInterposing() { | |
| 239 Class nswindow_class = [NSWindow class]; | |
| 240 ExchangeMethods(nswindow_class, NO, @selector(orderOut:), | |
| 241 @selector(chromePlugin_orderOut:)); | |
| 242 ExchangeMethods(nswindow_class, NO, @selector(orderFront:), | |
| 243 @selector(chromePlugin_orderFront:)); | |
| 244 ExchangeMethods(nswindow_class, NO, @selector(makeKeyAndOrderFront:), | |
| 245 @selector(chromePlugin_makeKeyAndOrderFront:)); | |
| 246 ExchangeMethods(nswindow_class, NO, @selector(_setWindowNumber:), | |
| 247 @selector(chromePlugin_setWindowNumber:)); | |
| 248 | |
| 249 ExchangeMethods([NSApplication class], NO, @selector(runModalForWindow:), | |
| 250 @selector(chromePlugin_runModalForWindow:)); | |
| 251 | |
| 252 Class nscursor_class = [NSCursor class]; | |
| 253 ExchangeMethods(nscursor_class, NO, @selector(set), | |
| 254 @selector(chromePlugin_set)); | |
| 255 ExchangeMethods(nscursor_class, YES, @selector(hide), | |
| 256 @selector(chromePlugin_hide)); | |
| 257 ExchangeMethods(nscursor_class, YES, @selector(unhide), | |
| 258 @selector(chromePlugin_unhide)); | |
| 259 } | |
| 260 | |
| 261 } // namespace mac_plugin_interposing | |
| OLD | NEW |