Fixed:
[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/WebPluginViewFactoryPrivate.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         [NSNumber numberWithInt:WebPlugInModeFull], WebPlugInModeKey,
68         nil];
69     [attributes release];
70     NSView *view = [WebPluginController plugInViewWithArguments:arguments fromPluginPackage:plugin];
71     [arguments release];
72
73     ASSERT(view != nil);
74
75     [self addSubview:view];
76     [pluginController addPlugin:view];
77     [view setFrame:[self frame]];
78     [view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
79 }
80
81 - (void)dataSourceUpdated:(WebDataSource *)dataSource;
82 {
83     // Cancel the load since WebKit plug-ins do their own loading.
84     NSURLResponse *response = [dataSource response];
85     NSError *error = [[NSError alloc] _initWithPluginErrorCode:WebKitErrorPlugInCancelledConnection
86                                                     contentURL:[response URL]
87                                                  pluginPageURL:nil
88                                                     pluginName:[plugin name]
89                                                       MIMEType:[response MIMEType]];
90     [dataSource _stopLoadingWithError:error];
91     [error release];    
92 }
93
94 - (void)setNeedsLayout:(BOOL)flag
95 {
96     needsLayout = flag;
97 }
98
99 - (void)layout
100 {
101     NSRect superFrame = [[self _web_superviewOfClass:[WebFrameView class]] frame];
102     [self setFrame:NSMakeRect(0, 0, NSWidth(superFrame), NSHeight(superFrame))];
103     needsLayout = NO;    
104 }
105
106 - (NSWindow *)currentWindow
107 {
108     NSWindow *window = [self window];
109     return window != nil ? window : [[self _webView] hostWindow];
110 }
111
112 - (void)viewWillMoveToWindow:(NSWindow *)window
113 {
114     if ([[self _webView] hostWindow] == nil) {
115         [pluginController stopAllPlugins];
116     }
117 }
118
119 - (void)viewDidMoveToWindow
120 {
121     if ([self currentWindow] != nil) {
122         [pluginController startAllPlugins];
123     }
124 }
125
126 - (void)viewWillMoveToHostWindow:(NSWindow *)hostWindow
127 {
128     if ([self window] == nil) {
129         [pluginController stopAllPlugins];
130     }
131 }
132
133 - (void)viewDidMoveToHostWindow
134 {
135     if ([self currentWindow] != nil) {
136         [pluginController startAllPlugins];
137     }    
138 }
139
140 - (void)receivedData:(NSData *)data withDataSource:(WebDataSource *)dataSource
141 {
142     
143 }
144
145 - (void)receivedError:(NSError *)error withDataSource:(WebDataSource *)dataSource
146 {
147     
148 }
149
150 - (void)finishedLoadingWithDataSource:(WebDataSource *)dataSource
151 {
152     
153 }
154
155 - (BOOL)canProvideDocumentSource
156 {
157     return NO;
158 }
159
160 - (NSString *)documentSource
161 {
162     return nil;
163 }
164
165 - (NSString *)title
166 {
167     return nil;
168 }
169
170 @end