Reviewed by Tim O.
[WebKit-https.git] / WebKit / Plugins / WebNetscapePluginStream.m
1 /*
2  * Copyright (C) 2005 Apple Computer, Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
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. 
16  *
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.
27  */
28
29 #import <WebKit/WebNetscapePluginStream.h>
30
31 #import <WebKit/WebLoader.h>
32 #import <WebKit/WebFrameBridge.h>
33 #import <WebKit/WebDataSourceInternal.h>
34 #import <WebKit/WebKitErrorsPrivate.h>
35 #import <WebKit/WebKitLogging.h>
36 #import <WebKit/WebNetscapePluginEmbeddedView.h>
37 #import <WebKit/WebNetscapePluginPackage.h>
38 #import <WebKit/WebNSURLRequestExtras.h>
39 #import <WebKit/WebViewInternal.h>
40
41 #import <Foundation/NSURLConnection.h>
42
43 @interface WebNetscapePlugInStreamLoader : WebLoader
44 {
45     WebNetscapePluginStream *stream;
46     WebBaseNetscapePluginView *view;
47 }
48 - initWithStream:(WebNetscapePluginStream *)theStream view:(WebBaseNetscapePluginView *)theView;
49 - (BOOL)isDone;
50 @end
51
52 @implementation WebNetscapePluginStream
53
54 - initWithRequest:(NSURLRequest *)theRequest
55     pluginPointer:(NPP)thePluginPointer
56        notifyData:(void *)theNotifyData 
57  sendNotification:(BOOL)flag
58 {   
59     WebBaseNetscapePluginView *view = (WebBaseNetscapePluginView *)thePluginPointer->ndata;
60
61     WebFrameBridge *bridge = [[view webFrame] _bridge];
62     BOOL hideReferrer;
63     if (![bridge canLoadURL:[theRequest URL] fromReferrer:[bridge referrer] hideReferrer:&hideReferrer])
64         return nil;
65
66     if ([self initWithRequestURL:[theRequest URL]
67                     pluginPointer:thePluginPointer
68                        notifyData:theNotifyData
69                  sendNotification:flag] == nil) {
70         return nil;
71     }
72     
73     // Temporarily set isTerminated to YES to avoid assertion failure in dealloc in case we are released in this method.
74     isTerminated = YES;
75     
76     request = [theRequest mutableCopy];
77     if (hideReferrer) {
78         [(NSMutableURLRequest *)request _web_setHTTPReferrer:nil];
79     }
80
81     _loader = [[WebNetscapePlugInStreamLoader alloc] initWithStream:self view:view]; 
82     [_loader setDataSource:[view dataSource]];
83     
84     isTerminated = NO;
85
86     return self;
87 }
88
89 - (void)dealloc
90 {
91     [_loader release];
92     [request release];
93     [super dealloc];
94 }
95
96 - (void)start
97 {
98     ASSERT(request);
99
100     [[_loader dataSource] _addPlugInStreamLoader:_loader];
101
102     BOOL succeeded = [_loader loadWithRequest:request];
103     if (!succeeded) {
104         [[_loader dataSource] _removePlugInStreamLoader:_loader];
105     }
106 }
107
108 - (void)cancelLoadWithError:(NSError *)error
109 {
110     if (![_loader isDone]) {
111         [_loader cancelWithError:error];
112     }
113 }
114
115 - (void)stop
116 {
117     [self cancelLoadAndDestroyStreamWithError:[_loader cancelledError]];
118 }
119
120 @end
121
122 @implementation WebNetscapePlugInStreamLoader
123
124 - initWithStream:(WebNetscapePluginStream *)theStream view:(WebBaseNetscapePluginView *)theView
125 {
126     [super init];
127     stream = [theStream retain];
128     view = [theView retain];
129     return self;
130 }
131
132 - (BOOL)isDone
133 {
134     return stream == nil;
135 }
136
137 - (void)releaseResources
138 {
139     [stream release];
140     stream = nil;
141     [view release];
142     view = nil;
143     [super releaseResources];
144 }
145
146 - (void)didReceiveResponse:(NSURLResponse *)theResponse
147 {
148     // retain/release self in this delegate method since the additional processing can do
149     // anything including possibly releasing self; one example of this is 3266216
150     [self retain];
151     [stream startStreamWithResponse:theResponse];
152     
153     // Don't continue if the stream is cancelled in startStreamWithResponse or didReceiveResponse.
154     if (stream) {
155         [super didReceiveResponse:theResponse];
156         if (stream) {
157             if ([theResponse isKindOfClass:[NSHTTPURLResponse class]] &&
158                 ([(NSHTTPURLResponse *)theResponse statusCode] >= 400 || [(NSHTTPURLResponse *)theResponse statusCode] < 100)) {
159                 NSError *error = [NSError _webKitErrorWithDomain:NSURLErrorDomain
160                                                             code:NSURLErrorFileDoesNotExist
161                                                             URL:[theResponse URL]];
162                 [stream cancelLoadAndDestroyStreamWithError:error];
163             }
164         }
165     }
166     [self release];
167 }
168
169 - (void)didReceiveData:(NSData *)data lengthReceived:(long long)lengthReceived
170 {
171     // retain/release self in this delegate method since the additional processing can do
172     // anything including possibly releasing self; one example of this is 3266216
173     [self retain];
174     [stream receivedData:data];
175     [super didReceiveData:data lengthReceived:lengthReceived];
176     [self release];
177 }
178
179 - (void)didFinishLoading
180 {
181     // Calling _removePlugInStreamLoader will likely result in a call to release, so we must retain.
182     [self retain];
183
184     [[self dataSource] _removePlugInStreamLoader:self];
185     [[self dataSource] _finishedLoadingResource];
186     [stream finishedLoadingWithData:[self resourceData]];
187     [super didFinishLoading];
188
189     [self release];
190 }
191
192 - (void)didFailWithError:(NSError *)error
193 {
194     // Calling _removePlugInStreamLoader will likely result in a call to release, so we must retain.
195     // The other additional processing can do anything including possibly releasing self;
196     // one example of this is 3266216
197     [self retain];
198
199     [[self dataSource] _removePlugInStreamLoader:self];
200     [[self dataSource] _receivedError:error];
201     [stream destroyStreamWithError:error];
202     [super didFailWithError:error];
203
204     [self release];
205 }
206
207 - (void)cancelWithError:(NSError *)error
208 {
209     // Calling _removePlugInStreamLoader will likely result in a call to release, so we must retain.
210     [self retain];
211
212     [[self dataSource] _removePlugInStreamLoader:self];
213     [super cancelWithError:error];
214
215     [self release];
216 }
217
218 @end