2 * Copyright (C) 2005 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.
29 #import <WebKit/WebPluginDocumentView.h>
31 #import <JavaScriptCore/Assertions.h>
32 #import <WebKit/WebDataSourcePrivate.h>
33 #import <WebKit/WebFrame.h>
34 #import <WebKit/WebFrameView.h>
35 #import <WebKit/WebKitErrorsPrivate.h>
36 #import <WebKit/WebNSURLExtras.h>
37 #import <WebKit/WebNSViewExtras.h>
38 #import <WebKit/WebPlugin.h>
39 #import <WebKit/WebPluginPrivate.h>
40 #import <WebKit/WebPluginController.h>
41 #import <WebKit/WebPluginDatabase.h>
42 #import <WebKit/WebPluginPackage.h>
43 #import <WebKit/WebPluginViewFactoryPrivate.h>
44 #import <WebKit/WebView.h>
46 @implementation WebPluginDocumentView
48 - initWithFrame:(NSRect)frame
50 [super initWithFrame:frame];
51 [self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
53 pluginController = [[WebPluginController alloc] initWithDocumentView:self];
61 [pluginController destroyAllPlugins];
62 [pluginController release];
63 [_dataSource release];
67 - (void)drawRect:(NSRect)rect
72 [super drawRect:rect];
75 - (void)setDataSource:(WebDataSource *)dataSource
77 // Since this class is a WebDocumentView and WebDocumentRepresentation, setDataSource: will be called twice. Do work only once.
78 if (dataSourceHasBeenSet)
81 // As noted above, -setDataSource: will be called twice -- once for the WebDocumentRepresentation, once for the WebDocumentView.
82 // We don't want to make the plugin until we know we're being committed as the WebDocumentView for the WebFrame. This check is
83 // to ensure that we've been added to the view hierarchy before attempting to create the plugin, as some plugins currently work
84 // under this assumption.
85 if (![self superview])
88 _dataSource = [dataSource retain];
89 [pluginController setDataSource:dataSource];
90 dataSourceHasBeenSet = YES;
92 NSURLResponse *response = [dataSource response];
93 NSString *MIMEType = [response MIMEType];
94 plugin = (WebPluginPackage *)[[[WebPluginDatabase installedPlugins] pluginForMIMEType:MIMEType] retain];
95 ASSERT([plugin isKindOfClass:[WebPluginPackage class]]);
97 NSURL *URL = [response URL];
98 NSString *URLString = [URL _web_originalDataAsString];
99 NSDictionary *attributes = [[NSDictionary alloc] initWithObjectsAndKeys:MIMEType, @"type", URLString, @"src", nil];
100 NSDictionary *arguments = [[NSDictionary alloc] initWithObjectsAndKeys:
101 URL, WebPlugInBaseURLKey,
102 attributes, WebPlugInAttributesKey,
103 pluginController, WebPlugInContainerKey,
104 [NSNumber numberWithInt:WebPlugInModeFull], WebPlugInModeKey,
105 [NSNumber numberWithBool:NO], WebPlugInShouldLoadMainResourceKey, // NO because we're already loading the data!
107 [attributes release];
108 pluginView = [[WebPluginController plugInViewWithArguments:arguments fromPluginPackage:plugin] retain];
111 ASSERT(pluginView != nil);
113 [self addSubview:pluginView];
114 [pluginController addPlugin:pluginView];
115 [pluginView setFrame:[self bounds]];
116 [pluginView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
118 if ([pluginView respondsToSelector:@selector(webPlugInMainResourceDidReceiveResponse:)])
119 [pluginView webPlugInMainResourceDidReceiveResponse:[dataSource response]];
122 - (void)dataSourceUpdated:(WebDataSource *)dataSource
124 if (![pluginView respondsToSelector:@selector(webPlugInMainResourceDidReceiveData:)]) {
125 // Cancel the load since this plug-in does its own loading.
126 NSURLResponse *response = [dataSource response];
127 // FIXME: See <rdar://problem/4258008>
128 NSError *error = [[NSError alloc] _initWithPluginErrorCode:WebKitErrorPlugInWillHandleLoad
129 contentURL:[response URL]
131 pluginName:[plugin name]
132 MIMEType:[response MIMEType]];
133 [dataSource _stopLoadingWithError:error];
138 - (void)setNeedsLayout:(BOOL)flag
145 NSRect superFrame = [[[_dataSource webFrame] frameView] frame];
146 [self setFrame:NSMakeRect(0, 0, NSWidth(superFrame), NSHeight(superFrame))];
150 - (NSWindow *)currentWindow
152 NSWindow *window = [self window];
153 return window != nil ? window : [[_dataSource _webView] hostWindow];
156 - (void)viewWillMoveToWindow:(NSWindow *)window
158 if ([[_dataSource _webView] hostWindow] == nil) {
159 [pluginController stopAllPlugins];
163 - (void)viewDidMoveToWindow
165 if ([self currentWindow] != nil) {
166 [pluginController startAllPlugins];
170 - (void)viewWillMoveToHostWindow:(NSWindow *)hostWindow
172 if ([self window] == nil) {
173 [pluginController stopAllPlugins];
177 - (void)viewDidMoveToHostWindow
179 if ([self currentWindow] != nil) {
180 [pluginController startAllPlugins];
184 - (void)receivedData:(NSData *)data withDataSource:(WebDataSource *)dataSource
186 if ([pluginView respondsToSelector:@selector(webPlugInMainResourceDidReceiveData:)])
187 [pluginView webPlugInMainResourceDidReceiveData:data];
190 - (void)receivedError:(NSError *)error withDataSource:(WebDataSource *)dataSource
192 if ([pluginView respondsToSelector:@selector(webPlugInMainResourceDidFailWithError:)])
193 [pluginView webPlugInMainResourceDidFailWithError:error];
196 - (void)finishedLoadingWithDataSource:(WebDataSource *)dataSource
198 if ([pluginView respondsToSelector:@selector(webPlugInMainResourceDidFinishLoading)])
199 [pluginView webPlugInMainResourceDidFinishLoading];
202 - (BOOL)canProvideDocumentSource
207 - (NSString *)documentSource