Fix build break
[chromium-blink-merge.git] / content / plugin / plugin_interpose_util_mac.mm
blobf58d1d33fa70204628d02fb7f99b8cfc33e3a957
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.
5 #include "content/plugin/plugin_interpose_util_mac.h"
7 #import <AppKit/AppKit.h>
8 #import <objc/runtime.h>
10 #include "content/common/plugin_messages.h"
11 #include "content/plugin/plugin_thread.h"
12 #include "webkit/plugins/npapi/webplugin_delegate_impl.h"
14 using content::PluginThread;
16 namespace mac_plugin_interposing {
18 // TODO(stuartmorgan): Make this an IPC to order the plugin process above the
19 // browser process only if the browser is current frontmost.
20 __attribute__((visibility("default")))
21 void SwitchToPluginProcess() {
22   ProcessSerialNumber this_process, front_process;
23   if ((GetCurrentProcess(&this_process) != noErr) ||
24       (GetFrontProcess(&front_process) != noErr)) {
25     return;
26   }
28   Boolean matched = false;
29   if ((SameProcess(&this_process, &front_process, &matched) == noErr) &&
30       !matched) {
31     SetFrontProcess(&this_process);
32   }
35 __attribute__((visibility("default")))
36 OpaquePluginRef GetActiveDelegate() {
37   return webkit::npapi::WebPluginDelegateImpl::GetActiveDelegate();
40 __attribute__((visibility("default")))
41 void NotifyBrowserOfPluginSelectWindow(uint32 window_id, CGRect bounds,
42                                        bool modal) {
43   PluginThread* plugin_thread = PluginThread::current();
44   if (plugin_thread) {
45     gfx::Rect window_bounds(bounds);
46     plugin_thread->Send(
47         new PluginProcessHostMsg_PluginSelectWindow(window_id, window_bounds,
48                                                     modal));
49   }
52 __attribute__((visibility("default")))
53 void NotifyBrowserOfPluginShowWindow(uint32 window_id, CGRect bounds,
54                                      bool modal) {
55   PluginThread* plugin_thread = PluginThread::current();
56   if (plugin_thread) {
57     gfx::Rect window_bounds(bounds);
58     plugin_thread->Send(
59         new PluginProcessHostMsg_PluginShowWindow(window_id, window_bounds,
60                                                   modal));
61   }
64 __attribute__((visibility("default")))
65 void NotifyBrowserOfPluginHideWindow(uint32 window_id, CGRect bounds) {
66   PluginThread* plugin_thread = PluginThread::current();
67   if (plugin_thread) {
68     gfx::Rect window_bounds(bounds);
69     plugin_thread->Send(
70         new PluginProcessHostMsg_PluginHideWindow(window_id, window_bounds));
71   }
74 void NotifyPluginOfSetCursorVisibility(bool visibility) {
75   PluginThread* plugin_thread = PluginThread::current();
76   if (plugin_thread) {
77     plugin_thread->Send(
78         new PluginProcessHostMsg_PluginSetCursorVisibility(visibility));
79   }
82 }  // namespace mac_plugin_interposing
84 #pragma mark -
86 struct WindowInfo {
87   uint32 window_id;
88   CGRect bounds;
89   WindowInfo(NSWindow* window) {
90     NSInteger window_num = [window windowNumber];
91     window_id = window_num > 0 ? window_num : 0;
92     bounds = NSRectToCGRect([window frame]);
93   }
96 static void OnPluginWindowClosed(const WindowInfo& window_info) {
97   if (window_info.window_id == 0)
98     return;
99   mac_plugin_interposing::NotifyBrowserOfPluginHideWindow(window_info.window_id,
100                                                           window_info.bounds);
103 static BOOL g_waiting_for_window_number = NO;
105 static void OnPluginWindowShown(const WindowInfo& window_info, BOOL is_modal) {
106   // The window id is 0 if it has never been shown (including while it is the
107   // process of being shown for the first time); when that happens, we'll catch
108   // it in _setWindowNumber instead.
109   static BOOL s_pending_display_is_modal = NO;
110   if (window_info.window_id == 0) {
111     g_waiting_for_window_number = YES;
112     if (is_modal)
113       s_pending_display_is_modal = YES;
114     return;
115   }
116   g_waiting_for_window_number = NO;
117   if (s_pending_display_is_modal) {
118     is_modal = YES;
119     s_pending_display_is_modal = NO;
120   }
121   mac_plugin_interposing::NotifyBrowserOfPluginShowWindow(
122     window_info.window_id, window_info.bounds, is_modal);
125 @interface NSWindow (ChromePluginUtilities)
126 // Returns YES if the window is visible and actually on the screen.
127 - (BOOL)chromePlugin_isWindowOnScreen;
128 @end
130 @implementation NSWindow (ChromePluginUtilities)
132 - (BOOL)chromePlugin_isWindowOnScreen {
133   if (![self isVisible])
134     return NO;
135   NSRect window_frame = [self frame];
136   for (NSScreen* screen in [NSScreen screens]) {
137     if (NSIntersectsRect(window_frame, [screen frame]))
138       return YES;
139   }
140   return NO;
143 @end
145 @interface NSWindow (ChromePluginInterposing)
146 - (void)chromePlugin_orderOut:(id)sender;
147 - (void)chromePlugin_orderFront:(id)sender;
148 - (void)chromePlugin_makeKeyAndOrderFront:(id)sender;
149 - (void)chromePlugin_setWindowNumber:(NSInteger)num;
150 @end
152 @implementation NSWindow (ChromePluginInterposing)
154 - (void)chromePlugin_orderOut:(id)sender {
155   WindowInfo window_info(self);
156   [self chromePlugin_orderOut:sender];
157   OnPluginWindowClosed(window_info);
160 - (void)chromePlugin_orderFront:(id)sender {
161   [self chromePlugin_orderFront:sender];
162   if ([self chromePlugin_isWindowOnScreen])
163     mac_plugin_interposing::SwitchToPluginProcess();
164   OnPluginWindowShown(WindowInfo(self), NO);
167 - (void)chromePlugin_makeKeyAndOrderFront:(id)sender {
168   [self chromePlugin_makeKeyAndOrderFront:sender];
169   if ([self chromePlugin_isWindowOnScreen])
170     mac_plugin_interposing::SwitchToPluginProcess();
171   OnPluginWindowShown(WindowInfo(self), NO);
174 - (void)chromePlugin_setWindowNumber:(NSInteger)num {
175   if (!g_waiting_for_window_number || num <= 0) {
176     [self chromePlugin_setWindowNumber:num];
177     return;
178   }
179   mac_plugin_interposing::SwitchToPluginProcess();
180   [self chromePlugin_setWindowNumber:num];
181   OnPluginWindowShown(WindowInfo(self), NO);
184 @end
186 @interface NSApplication (ChromePluginInterposing)
187 - (NSInteger)chromePlugin_runModalForWindow:(NSWindow*)window;
188 @end
190 @implementation NSApplication (ChromePluginInterposing)
192 - (NSInteger)chromePlugin_runModalForWindow:(NSWindow*)window {
193   mac_plugin_interposing::SwitchToPluginProcess();
194   // This is out-of-order relative to the other calls, but runModalForWindow:
195   // won't return until the window closes, and the order only matters for
196   // full-screen windows.
197   OnPluginWindowShown(WindowInfo(window), YES);
198   return [self chromePlugin_runModalForWindow:window];
201 @end
203 @interface NSCursor (ChromePluginInterposing)
204 - (void)chromePlugin_set;
205 + (void)chromePlugin_hide;
206 + (void)chromePlugin_unhide;
207 @end
209 @implementation NSCursor (ChromePluginInterposing)
211 - (void)chromePlugin_set {
212   OpaquePluginRef delegate = mac_plugin_interposing::GetActiveDelegate();
213   if (delegate) {
214     static_cast<webkit::npapi::WebPluginDelegateImpl*>(delegate)->SetNSCursor(
215         self);
216     return;
217   }
218   [self chromePlugin_set];
221 + (void)chromePlugin_hide {
222   mac_plugin_interposing::NotifyPluginOfSetCursorVisibility(false);
225 + (void)chromePlugin_unhide {
226   mac_plugin_interposing::NotifyPluginOfSetCursorVisibility(true);
229 @end
231 #pragma mark -
233 static void ExchangeMethods(Class target_class,
234                             BOOL class_method,
235                             SEL original,
236                             SEL replacement) {
237   Method m1;
238   Method m2;
239   if (class_method) {
240     m1 = class_getClassMethod(target_class, original);
241     m2 = class_getClassMethod(target_class, replacement);
242   } else {
243     m1 = class_getInstanceMethod(target_class, original);
244     m2 = class_getInstanceMethod(target_class, replacement);
245   }
246   if (m1 && m2)
247     method_exchangeImplementations(m1, m2);
248   else
249     NOTREACHED() << "Cocoa swizzling failed";
252 namespace mac_plugin_interposing {
254 void SetUpCocoaInterposing() {
255   Class nswindow_class = [NSWindow class];
256   ExchangeMethods(nswindow_class, NO, @selector(orderOut:),
257                   @selector(chromePlugin_orderOut:));
258   ExchangeMethods(nswindow_class, NO, @selector(orderFront:),
259                   @selector(chromePlugin_orderFront:));
260   ExchangeMethods(nswindow_class, NO, @selector(makeKeyAndOrderFront:),
261                   @selector(chromePlugin_makeKeyAndOrderFront:));
262   ExchangeMethods(nswindow_class, NO, @selector(_setWindowNumber:),
263                   @selector(chromePlugin_setWindowNumber:));
265   ExchangeMethods([NSApplication class], NO, @selector(runModalForWindow:),
266                   @selector(chromePlugin_runModalForWindow:));
268   Class nscursor_class = [NSCursor class];
269   ExchangeMethods(nscursor_class, NO, @selector(set),
270                   @selector(chromePlugin_set));
271   ExchangeMethods(nscursor_class, YES, @selector(hide),
272                   @selector(chromePlugin_hide));
273   ExchangeMethods(nscursor_class, YES, @selector(unhide),
274                   @selector(chromePlugin_unhide));
277 }  // namespace mac_plugin_interposing