2 * Copyright (C) 2005, 2006 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #import "WebPluginController.h"
32 #import "DOMNodeInternal.h"
33 #import "WebDataSourceInternal.h"
34 #import "WebFrameInternal.h"
35 #import "WebFrameView.h"
36 #import "WebHTMLViewPrivate.h"
37 #import "WebKitErrorsPrivate.h"
38 #import "WebKitLogging.h"
39 #import "WebNSObjectExtras.h"
40 #import "WebNSURLExtras.h"
41 #import "WebNSViewExtras.h"
43 #import "WebPluginContainer.h"
44 #import "WebPluginContainerCheck.h"
45 #import "WebPluginPackage.h"
46 #import "WebPluginPrivate.h"
47 #import "WebPluginViewFactory.h"
48 #import "WebUIDelegate.h"
49 #import "WebViewInternal.h"
50 #import <Foundation/NSURLRequest.h>
51 #import <WebCore/DocumentLoader.h>
52 #import <WebCore/Frame.h>
53 #import <WebCore/FrameLoader.h>
54 #import <WebCore/HTMLMediaElement.h>
55 #import <WebCore/HTMLNames.h>
56 #import <WebCore/MediaPlayerProxy.h>
57 #import <WebCore/PlatformString.h>
58 #import <WebCore/ResourceRequest.h>
59 #import <WebCore/ScriptController.h>
60 #import <WebCore/WebCoreURLResponse.h>
61 #import <objc/objc-runtime.h>
62 #import <runtime/JSLock.h>
64 using namespace WebCore;
65 using namespace HTMLNames;
67 @interface NSView (PluginSecrets)
68 - (void)setContainingWindow:(NSWindow *)w;
71 // For compatibility only.
72 @interface NSObject (OldPluginAPI)
73 + (NSView *)pluginViewWithArguments:(NSDictionary *)arguments;
76 @interface NSView (OldPluginAPI)
77 - (void)pluginInitialize;
80 - (void)pluginDestroy;
83 static bool isKindOfClass(id, NSString* className);
84 static void installFlip4MacPlugInWorkaroundIfNecessary();
87 static NSMutableSet *pluginViews = nil;
89 @implementation WebPluginController
91 + (NSView *)plugInViewWithArguments:(NSDictionary *)arguments fromPluginPackage:(WebPluginPackage *)pluginPackage
94 Class viewFactory = [pluginPackage viewFactory];
98 if ([viewFactory respondsToSelector:@selector(plugInViewWithArguments:)]) {
99 JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
100 view = [viewFactory plugInViewWithArguments:arguments];
101 } else if ([viewFactory respondsToSelector:@selector(pluginViewWithArguments:)]) {
102 JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
103 view = [viewFactory pluginViewWithArguments:arguments];
110 if (pluginViews == nil) {
111 pluginViews = [[NSMutableSet alloc] init];
113 [pluginViews addObject:view];
118 + (BOOL)isPlugInView:(NSView *)view
120 return [pluginViews containsObject:view];
123 - (id)initWithDocumentView:(NSView *)view
128 _documentView = view;
129 _views = [[NSMutableArray alloc] init];
130 _checksInProgress = (NSMutableSet *)CFMakeCollectable(CFSetCreateMutable(NULL, 0, NULL));
134 - (void)setDataSource:(WebDataSource *)dataSource
136 _dataSource = dataSource;
142 [_checksInProgress release];
143 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
144 [_viewsNotInDocument release];
149 - (void)stopOnePlugin:(NSView *)view
151 if ([view respondsToSelector:@selector(webPlugInStop)]) {
152 JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
153 [view webPlugInStop];
154 } else if ([view respondsToSelector:@selector(pluginStop)]) {
155 JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
160 - (void)destroyOnePlugin:(NSView *)view
162 if ([view respondsToSelector:@selector(webPlugInDestroy)]) {
163 JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
164 [view webPlugInDestroy];
165 } else if ([view respondsToSelector:@selector(pluginDestroy)]) {
166 JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
167 [view pluginDestroy];
171 - (void)startAllPlugins
176 if ([_views count] > 0)
177 LOG(Plugins, "starting WebKit plugins : %@", [_views description]);
179 int count = [_views count];
180 for (int i = 0; i < count; i++) {
181 id aView = [_views objectAtIndex:i];
182 if ([aView respondsToSelector:@selector(webPlugInStart)]) {
183 JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
184 [aView webPlugInStart];
185 } else if ([aView respondsToSelector:@selector(pluginStart)]) {
186 JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
193 - (void)stopAllPlugins
198 if ([_views count] > 0) {
199 LOG(Plugins, "stopping WebKit plugins: %@", [_views description]);
202 int viewsCount = [_views count];
203 for (int i = 0; i < viewsCount; i++)
204 [self stopOnePlugin:[_views objectAtIndex:i]];
206 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
207 int viewsNotInDocumentCount = [_viewsNotInDocument count];
208 for (int i = 0; i < viewsNotInDocumentCount; i++)
209 [self stopOnePlugin:[_viewsNotInDocument objectAtIndex:i]];
215 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
216 - (void)pluginViewCreated:(NSView *)view
218 if (!_viewsNotInDocument)
219 _viewsNotInDocument= [[NSMutableArray alloc] init];
220 if (![_viewsNotInDocument containsObject:view])
221 [_viewsNotInDocument addObject:view];
224 + (void)pluginViewHidden:(NSView *)view
226 [pluginViews removeObject:view];
230 - (void)addPlugin:(NSView *)view
232 if (!_documentView) {
233 LOG_ERROR("can't add a plug-in to a defunct WebPluginController");
237 if (![_views containsObject:view]) {
238 [_views addObject:view];
239 [[_documentView _webView] addPluginInstanceView:view];
241 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
242 if ([_viewsNotInDocument containsObject:view])
243 [_viewsNotInDocument removeObject:view];
246 BOOL oldDefersCallbacks = [[self webView] defersCallbacks];
247 if (!oldDefersCallbacks)
248 [[self webView] setDefersCallbacks:YES];
250 if (isKindOfClass(view, @"WmvPlugin"))
251 installFlip4MacPlugInWorkaroundIfNecessary();
253 LOG(Plugins, "initializing plug-in %@", view);
254 if ([view respondsToSelector:@selector(webPlugInInitialize)]) {
255 JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
256 [view webPlugInInitialize];
257 } else if ([view respondsToSelector:@selector(pluginInitialize)]) {
258 JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
259 [view pluginInitialize];
262 if (!oldDefersCallbacks)
263 [[self webView] setDefersCallbacks:NO];
266 LOG(Plugins, "starting plug-in %@", view);
267 if ([view respondsToSelector:@selector(webPlugInStart)]) {
268 JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
269 [view webPlugInStart];
270 } else if ([view respondsToSelector:@selector(pluginStart)]) {
271 JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
275 if ([view respondsToSelector:@selector(setContainingWindow:)]) {
276 JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
277 [view setContainingWindow:[_documentView window]];
283 - (void)destroyPlugin:(NSView *)view
285 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
286 if ([_views containsObject:view] || [_viewsNotInDocument containsObject:view]) {
288 if ([_views containsObject:view]) {
291 [self stopOnePlugin:view];
292 [self destroyOnePlugin:view];
294 #if ENABLE(NETSCAPE_PLUGIN_API)
295 if (Frame* frame = core([self webFrame]))
296 frame->script()->cleanupScriptObjectsForPlugin(self);
299 [pluginViews removeObject:view];
300 [[_documentView _webView] removePluginInstanceView:view];
301 [_views removeObject:view];
302 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
303 [_viewsNotInDocument removeObject:view];
308 - (void)_webPluginContainerCancelCheckIfAllowedToLoadRequest:(id)checkIdentifier
310 [checkIdentifier cancel];
311 [_checksInProgress removeObject:checkIdentifier];
314 static void cancelOutstandingCheck(const void *item, void *context)
319 - (void)_cancelOutstandingChecks
321 if (_checksInProgress) {
322 CFSetApplyFunction((CFSetRef)_checksInProgress, cancelOutstandingCheck, NULL);
323 [_checksInProgress release];
324 _checksInProgress = nil;
328 - (void)destroyAllPlugins
330 [self stopAllPlugins];
332 if ([_views count] > 0) {
333 LOG(Plugins, "destroying WebKit plugins: %@", [_views description]);
336 [self _cancelOutstandingChecks];
338 int viewsCount = [_views count];
339 for (int i = 0; i < viewsCount; i++) {
340 id aView = [_views objectAtIndex:i];
341 [self destroyOnePlugin:aView];
343 #if ENABLE(NETSCAPE_PLUGIN_API)
344 if (Frame* frame = core([self webFrame]))
345 frame->script()->cleanupScriptObjectsForPlugin(self);
348 [pluginViews removeObject:aView];
349 [[_documentView _webView] removePluginInstanceView:aView];
352 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
353 int viewsNotInDocumentCount = [_viewsNotInDocument count];
354 for (int i = 0; i < viewsNotInDocumentCount; i++)
355 [self destroyOnePlugin:[_viewsNotInDocument objectAtIndex:i]];
358 [_views makeObjectsPerformSelector:@selector(removeFromSuperviewWithoutNeedingDisplay)];
365 - (id)_webPluginContainerCheckIfAllowedToLoadRequest:(NSURLRequest *)request inFrame:(NSString *)target resultObject:(id)obj selector:(SEL)selector
367 WebPluginContainerCheck *check = [WebPluginContainerCheck checkWithRequest:request target:target resultObject:obj selector:selector controller:self contextInfo:nil];
368 [_checksInProgress addObject:check];
374 - (void)webPlugInContainerLoadRequest:(NSURLRequest *)request inFrame:(NSString *)target
377 LOG_ERROR("nil URL passed");
380 if (!_documentView) {
381 LOG_ERROR("could not load URL %@ because plug-in has already been destroyed", request);
384 WebFrame *frame = [_dataSource webFrame];
386 LOG_ERROR("could not load URL %@ because plug-in has already been stopped", request);
392 NSString *JSString = [[request URL] _webkit_scriptIfJavaScriptURL];
394 if ([frame findFrameNamed:target] != frame) {
395 LOG_ERROR("JavaScript requests can only be made on the frame that contains the plug-in");
398 [frame _stringByEvaluatingJavaScriptFromString:JSString];
401 LOG_ERROR("could not load URL %@", [request URL]);
404 core(frame)->loader()->load(request, target, false);
408 - (void)webPlugInContainerShowStatus:(NSString *)message
413 WebView *v = [_dataSource _webView];
414 [[v _UIDelegateForwarder] webView:v setStatusText:message];
417 // For compatibility only.
418 - (void)showStatus:(NSString *)message
420 [self webPlugInContainerShowStatus:message];
423 - (NSColor *)webPlugInContainerSelectionColor
426 if (Frame* frame = core([self webFrame]))
427 primary = frame->selection()->isFocusedAndActive();
428 return primary ? [NSColor selectedTextBackgroundColor] : [NSColor secondarySelectedControlColor];
431 // For compatibility only.
432 - (NSColor *)selectionColor
434 return [self webPlugInContainerSelectionColor];
437 - (WebFrame *)webFrame
439 return [_dataSource webFrame];
444 return [[self webFrame] webView];
447 - (NSString *)URLPolicyCheckReferrer
449 NSURL *responseURL = [[[[self webFrame] _dataSource] response] URL];
451 return [responseURL _web_originalDataAsString];
454 - (void)pluginView:(NSView *)pluginView receivedResponse:(NSURLResponse *)response
456 if ([pluginView respondsToSelector:@selector(webPlugInMainResourceDidReceiveResponse:)])
457 [pluginView webPlugInMainResourceDidReceiveResponse:response];
459 // Cancel the load since this plug-in does its own loading.
460 // FIXME: See <rdar://problem/4258008> for a problem with this.
461 NSError *error = [[NSError alloc] _initWithPluginErrorCode:WebKitErrorPlugInWillHandleLoad
462 contentURL:[response URL]
464 pluginName:nil // FIXME: Get this from somewhere
465 MIMEType:[response MIMEType]];
466 [_dataSource _documentLoader]->cancelMainResourceLoad(error);
471 - (void)pluginView:(NSView *)pluginView receivedData:(NSData *)data
473 if ([pluginView respondsToSelector:@selector(webPlugInMainResourceDidReceiveData:)])
474 [pluginView webPlugInMainResourceDidReceiveData:data];
477 - (void)pluginView:(NSView *)pluginView receivedError:(NSError *)error
479 if ([pluginView respondsToSelector:@selector(webPlugInMainResourceDidFailWithError:)])
480 [pluginView webPlugInMainResourceDidFailWithError:error];
483 - (void)pluginViewFinishedLoading:(NSView *)pluginView
485 if ([pluginView respondsToSelector:@selector(webPlugInMainResourceDidFinishLoading)])
486 [pluginView webPlugInMainResourceDidFinishLoading];
489 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
490 static WebCore::HTMLMediaElement* mediaProxyClient(DOMElement* element)
493 LOG_ERROR("nil element passed");
497 Element* node = core(element);
498 if (!node || (!node->hasTagName(HTMLNames::videoTag) && !node->hasTagName(HTMLNames::audioTag))) {
499 LOG_ERROR("invalid media element passed");
503 return static_cast<WebCore::HTMLMediaElement*>(node);
506 - (void)_webPluginContainerSetMediaPlayerProxy:(WebMediaPlayerProxy *)proxy forElement:(DOMElement *)element
508 WebCore::HTMLMediaElement* client = mediaProxyClient(element);
510 client->setMediaPlayerProxy(proxy);
513 - (void)_webPluginContainerPostMediaPlayerNotification:(int)notification forElement:(DOMElement *)element
515 WebCore::HTMLMediaElement* client = mediaProxyClient(element);
517 client->deliverNotification((MediaPlayerProxyNotificationType)notification);
523 static bool isKindOfClass(id object, NSString *className)
525 Class cls = NSClassFromString(className);
530 return [object isKindOfClass:cls];
534 // Existing versions of the Flip4Mac WebKit plug-in have an object lifetime bug related to an NSAlert that is
535 // used to notify the user about updates to the plug-in. This bug can result in Safari crashing if the page
536 // containing the plug-in navigates while the alert is displayed (<rdar://problem/7313430>).
538 // The gist of the bug is thus: Flip4Mac sets an instance of the TSUpdateCheck class as the modal delegate of the
539 // NSAlert instance. This TSUpdateCheck instance itself has a delegate. The delegate is set to the WmvPlugin
540 // instance which is the NSView subclass that is exposed to WebKit as the plug-in view. Since this relationship
541 // is that of delegates the TSUpdateCheck does not retain the WmvPlugin. This leads to a bug if the WmvPlugin
542 // instance is destroyed before the TSUpdateCheck instance as the TSUpdateCheck instance will be left with a
543 // pointer to a stale object. This will happen if a page containing the Flip4Mac plug-in triggers a navigation
544 // while the update sheet is visible as the WmvPlugin instance is removed from the view hierarchy and there are
545 // no other references to keep the object alive.
547 // We work around this bug by patching the following two messages:
549 // 1) -[NSAlert beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:]
550 // 2) -[TSUpdateCheck alertDidEnd:returnCode:contextInfo:]
552 // Our override of 1) detects whether it is Flip4Mac's update sheet triggering the alert by checking whether the
553 // modal delegate is an instance of TSUpdateCheck. If it is, it retains the modal delegate's delegate.
555 // Our override of 2) then autoreleases the delegate, balancing the retain we added in 1).
557 // These two overrides have the effect of ensuring that the WmvPlugin instance will always outlive the TSUpdateCheck
558 // instance, preventing the TSUpdateCheck instance from accessing a stale delegate pointer and crashing the application.
561 typedef void (*beginSheetModalForWindowIMP)(id, SEL, NSWindow *, id, SEL, void*);
562 static beginSheetModalForWindowIMP original_NSAlert_beginSheetModalForWindow_modalDelegate_didEndSelector_contextInfo_;
564 typedef void (*alertDidEndIMP)(id, SEL, NSAlert *, NSInteger, void*);
565 static alertDidEndIMP original_TSUpdateCheck_alertDidEnd_returnCode_contextInfo_;
567 static void WebKit_TSUpdateCheck_alertDidEnd_returnCode_contextInfo_(id object, SEL selector, NSAlert *alert, NSInteger returnCode, void* contextInfo)
569 [[object delegate] autorelease];
571 original_TSUpdateCheck_alertDidEnd_returnCode_contextInfo_(object, selector, alert, returnCode, contextInfo);
574 static void WebKit_NSAlert_beginSheetModalForWindow_modalDelegate_didEndSelector_contextInfo_(id object, SEL selector, NSWindow *window, id modalDelegate, SEL didEndSelector, void* contextInfo)
576 if (isKindOfClass(modalDelegate, @"TSUpdateCheck"))
577 [[modalDelegate delegate] retain];
579 original_NSAlert_beginSheetModalForWindow_modalDelegate_didEndSelector_contextInfo_(object, selector, window, modalDelegate, didEndSelector, contextInfo);
582 static void installFlip4MacPlugInWorkaroundIfNecessary()
584 static bool hasInstalledFlip4MacPlugInWorkaround;
585 if (!hasInstalledFlip4MacPlugInWorkaround) {
586 Class TSUpdateCheck = objc_lookUpClass("TSUpdateCheck");
590 Method methodToPatch = class_getInstanceMethod(TSUpdateCheck, @selector(alertDidEnd:returnCode:contextInfo:));
594 IMP originalMethod = method_setImplementation(methodToPatch, reinterpret_cast<IMP>(WebKit_TSUpdateCheck_alertDidEnd_returnCode_contextInfo_));
595 original_TSUpdateCheck_alertDidEnd_returnCode_contextInfo_ = reinterpret_cast<alertDidEndIMP>(originalMethod);
597 methodToPatch = class_getInstanceMethod(objc_getRequiredClass("NSAlert"), @selector(beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:));
598 originalMethod = method_setImplementation(methodToPatch, reinterpret_cast<IMP>(WebKit_NSAlert_beginSheetModalForWindow_modalDelegate_didEndSelector_contextInfo_));
599 original_NSAlert_beginSheetModalForWindow_modalDelegate_didEndSelector_contextInfo_ = reinterpret_cast<beginSheetModalForWindowIMP>(originalMethod);
601 hasInstalledFlip4MacPlugInWorkaround = true;