889a7dc97009888f6d2336317e9dc4a6e88e9b87
[WebKit-https.git] / WebKit / Plugins.subproj / WebPluginDocumentView.m
1 /*      
2     WebPluginDocumentView.m
3     Copyright 2004, Apple, Inc. All rights reserved.
4 */
5
6 #import <WebKit/WebPluginDocumentView.h>
7
8 #import <WebKit/WebAssertions.h>
9 #import <WebKit/WebDataSourcePrivate.h>
10 #import <WebKit/WebFrameView.h>
11 #import <WebKit/WebKitErrorsPrivate.h>
12 #import <WebKit/WebNSURLExtras.h>
13 #import <WebKit/WebNSViewExtras.h>
14 #import <WebKit/WebPluginController.h>
15 #import <WebKit/WebPluginDatabase.h>
16 #import <WebKit/WebPluginPackage.h>
17 #import <WebKit/WebPluginViewFactory.h>
18 #import <WebKit/WebView.h>
19
20 @implementation WebPluginDocumentView
21
22 - initWithFrame:(NSRect)frame
23 {
24     [super initWithFrame:frame];
25     [self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
26     needsLayout = YES;
27     pluginController = [[WebPluginController alloc] initWithDocumentView:self];
28     return self;
29 }
30
31 - (void)dealloc
32 {
33     [plugin release];
34     [pluginController destroyAllPlugins];
35     [pluginController release];
36     [super dealloc];
37 }
38
39 - (void)drawRect:(NSRect)rect
40 {
41     if (needsLayout) {
42         [self layout];
43     }
44     [super drawRect:rect];
45 }
46
47 - (void)setDataSource:(WebDataSource *)dataSource
48 {
49     // Since this class is a WebDocumentView and WebDocumentRepresentation, setDataSource: will be called twice. Do work only once.
50     if (dataSourceHasBeenSet) {
51         return;
52     }
53     dataSourceHasBeenSet = YES;
54     
55     NSURLResponse *response = [dataSource response];
56     NSString *MIMEType = [response MIMEType];    
57     plugin = (WebPluginPackage *)[[[WebPluginDatabase installedPlugins] pluginForMIMEType:MIMEType] retain];
58     ASSERT([plugin isKindOfClass:[WebPluginPackage class]]);
59     
60     NSURL *URL = [response URL];
61     NSString *URLString = [URL _web_originalDataAsString];
62     NSDictionary *attributes = [[NSDictionary alloc] initWithObjectsAndKeys:MIMEType, @"type", URLString, @"src", nil];
63     NSDictionary *arguments = [[NSDictionary alloc] initWithObjectsAndKeys:
64         URL,                WebPlugInBaseURLKey,
65         attributes,         WebPlugInAttributesKey,
66         pluginController,   WebPlugInContainerKey,
67         nil];
68     [attributes release];
69     NSView *view = [WebPluginController plugInViewWithArguments:arguments fromPluginPackage:plugin];
70     [arguments release];
71
72     ASSERT(view != nil);
73
74     [self addSubview:view];
75     [pluginController addPlugin:view];
76     [view setFrame:[self frame]];
77     [view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
78 }
79
80 - (void)dataSourceUpdated:(WebDataSource *)dataSource;
81 {
82     // Cancel the load since WebKit plug-ins do their own loading.
83     NSURLResponse *response = [dataSource response];
84     NSError *error = [[NSError alloc] _initWithPluginErrorCode:WebKitErrorPlugInCancelledConnection
85                                                     contentURL:[response URL]
86                                                  pluginPageURL:nil
87                                                     pluginName:[plugin name]
88                                                       MIMEType:[response MIMEType]];
89     [dataSource _stopLoadingWithError:error];
90     [error release];    
91 }
92
93 - (void)setNeedsLayout:(BOOL)flag
94 {
95     needsLayout = flag;
96 }
97
98 - (void)layout
99 {
100     NSRect superFrame = [[self _web_superviewOfClass:[WebFrameView class]] frame];
101     [self setFrame:NSMakeRect(0, 0, NSWidth(superFrame), NSHeight(superFrame))];
102     needsLayout = NO;    
103 }
104
105 - (NSWindow *)currentWindow
106 {
107     NSWindow *window = [self window];
108     return window != nil ? window : [[self _webView] hostWindow];
109 }
110
111 - (void)viewWillMoveToWindow:(NSWindow *)window
112 {
113     if ([[self _webView] hostWindow] == nil) {
114         [pluginController stopAllPlugins];
115     }
116 }
117
118 - (void)viewDidMoveToWindow
119 {
120     if ([self currentWindow] != nil) {
121         [pluginController startAllPlugins];
122     }
123 }
124
125 - (void)viewWillMoveToHostWindow:(NSWindow *)hostWindow
126 {
127     if ([self window] == nil) {
128         [pluginController stopAllPlugins];
129     }
130 }
131
132 - (void)viewDidMoveToHostWindow
133 {
134     if ([self currentWindow] != nil) {
135         [pluginController startAllPlugins];
136     }    
137 }
138
139 - (void)receivedData:(NSData *)data withDataSource:(WebDataSource *)dataSource
140 {
141     
142 }
143
144 - (void)receivedError:(NSError *)error withDataSource:(WebDataSource *)dataSource
145 {
146     
147 }
148
149 - (void)finishedLoadingWithDataSource:(WebDataSource *)dataSource
150 {
151     
152 }
153
154 - (BOOL)canProvideDocumentSource
155 {
156     return NO;
157 }
158
159 - (NSString *)documentSource
160 {
161     return nil;
162 }
163
164 - (NSString *)title
165 {
166     return nil;
167 }
168
169 @end