|
1 /* |
|
2 * Copyright (C) 2006, 2007 Apple 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 * 1. Redistributions of source code must retain the above copyright |
|
8 * notice, this list of conditions and the following disclaimer. |
|
9 * 2. Redistributions in binary form must reproduce the above copyright |
|
10 * notice, this list of conditions and the following disclaimer in the |
|
11 * documentation and/or other materials provided with the distribution. |
|
12 * |
|
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
|
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
|
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
24 */ |
|
25 |
|
26 #include "config.h" |
|
27 #include "WebKitDLL.h" |
|
28 #include "WebDataSource.h" |
|
29 |
|
30 #include "IWebMutableURLRequest.h" |
|
31 #include "MemoryStream.h" |
|
32 #include "WebDocumentLoader.h" |
|
33 #include "WebError.h" |
|
34 #include "WebFrame.h" |
|
35 #include "WebKit.h" |
|
36 #include "WebHTMLRepresentation.h" |
|
37 #include "WebKitStatisticsPrivate.h" |
|
38 #include "WebMutableURLRequest.h" |
|
39 #include "WebResource.h" |
|
40 #include "WebURLResponse.h" |
|
41 |
|
42 #pragma warning(push, 0) |
|
43 #include <WebCore/BString.h> |
|
44 #include <WebCore/DocLoader.h> |
|
45 #include <WebCore/Document.h> |
|
46 #include <WebCore/FrameLoader.h> |
|
47 #include <WebCore/KURL.h> |
|
48 #pragma warning(pop) |
|
49 |
|
50 using namespace WebCore; |
|
51 |
|
52 // WebDataSource ---------------------------------------------------------------- |
|
53 |
|
54 // {F230854D-7091-428a-8DB5-37CABA44C105} |
|
55 const GUID IID_WebDataSource = |
|
56 { 0x5c2f9099, 0xe65e, 0x4a0f, { 0x9c, 0xa0, 0x6a, 0xd6, 0x92, 0x52, 0xa6, 0x2a } }; |
|
57 |
|
58 WebDataSource::WebDataSource(WebDocumentLoader* loader) |
|
59 : m_refCount(0) |
|
60 , m_loader(loader) |
|
61 { |
|
62 WebDataSourceCount++; |
|
63 gClassCount++; |
|
64 } |
|
65 |
|
66 WebDataSource::~WebDataSource() |
|
67 { |
|
68 if (m_loader) |
|
69 m_loader->detachDataSource(); |
|
70 WebDataSourceCount--; |
|
71 gClassCount--; |
|
72 } |
|
73 |
|
74 WebDataSource* WebDataSource::createInstance(WebDocumentLoader* loader) |
|
75 { |
|
76 WebDataSource* instance = new WebDataSource(loader); |
|
77 instance->AddRef(); |
|
78 return instance; |
|
79 } |
|
80 |
|
81 WebDocumentLoader* WebDataSource::documentLoader() const |
|
82 { |
|
83 return m_loader.get(); |
|
84 } |
|
85 |
|
86 // IWebDataSourcePrivate ------------------------------------------------------ |
|
87 |
|
88 HRESULT STDMETHODCALLTYPE WebDataSource::overrideEncoding( |
|
89 /* [retval][out] */ BSTR* /*encoding*/) |
|
90 { |
|
91 ASSERT_NOT_REACHED(); |
|
92 return E_NOTIMPL; |
|
93 } |
|
94 |
|
95 HRESULT STDMETHODCALLTYPE WebDataSource::setOverrideEncoding( |
|
96 /* [in] */ BSTR /*encoding*/) |
|
97 { |
|
98 ASSERT_NOT_REACHED(); |
|
99 return E_NOTIMPL; |
|
100 } |
|
101 |
|
102 HRESULT STDMETHODCALLTYPE WebDataSource::mainDocumentError( |
|
103 /* [retval][out] */ IWebError** error) |
|
104 { |
|
105 if (!error) { |
|
106 ASSERT_NOT_REACHED(); |
|
107 return E_POINTER; |
|
108 } |
|
109 |
|
110 *error = 0; |
|
111 |
|
112 if (!m_loader) |
|
113 return E_FAIL; |
|
114 |
|
115 if (m_loader->mainDocumentError().isNull()) |
|
116 return S_OK; |
|
117 |
|
118 *error = WebError::createInstance(m_loader->mainDocumentError()); |
|
119 return S_OK; |
|
120 } |
|
121 |
|
122 // IUnknown ------------------------------------------------------------------- |
|
123 |
|
124 HRESULT STDMETHODCALLTYPE WebDataSource::QueryInterface(REFIID riid, void** ppvObject) |
|
125 { |
|
126 *ppvObject = 0; |
|
127 if (IsEqualGUID(riid, IID_WebDataSource)) |
|
128 *ppvObject = this; |
|
129 else if (IsEqualGUID(riid, IID_IUnknown)) |
|
130 *ppvObject = static_cast<IWebDataSource*>(this); |
|
131 else if (IsEqualGUID(riid, IID_IWebDataSource)) |
|
132 *ppvObject = static_cast<IWebDataSource*>(this); |
|
133 else if (IsEqualGUID(riid, IID_IWebDataSourcePrivate)) |
|
134 *ppvObject = static_cast<IWebDataSourcePrivate*>(this); |
|
135 else |
|
136 return E_NOINTERFACE; |
|
137 |
|
138 AddRef(); |
|
139 return S_OK; |
|
140 } |
|
141 |
|
142 ULONG STDMETHODCALLTYPE WebDataSource::AddRef(void) |
|
143 { |
|
144 return ++m_refCount; |
|
145 } |
|
146 |
|
147 ULONG STDMETHODCALLTYPE WebDataSource::Release(void) |
|
148 { |
|
149 ULONG newRef = --m_refCount; |
|
150 if (!newRef) |
|
151 delete(this); |
|
152 |
|
153 return newRef; |
|
154 } |
|
155 |
|
156 // IWebDataSource -------------------------------------------------------------- |
|
157 |
|
158 HRESULT STDMETHODCALLTYPE WebDataSource::initWithRequest( |
|
159 /* [in] */ IWebURLRequest* /*request*/) |
|
160 { |
|
161 ASSERT_NOT_REACHED(); |
|
162 return E_NOTIMPL; |
|
163 } |
|
164 |
|
165 HRESULT STDMETHODCALLTYPE WebDataSource::data( |
|
166 /* [retval][out] */ IStream** stream) |
|
167 { |
|
168 *stream = 0; |
|
169 if (!m_loader) |
|
170 return E_FAIL; |
|
171 |
|
172 *stream = MemoryStream::createInstance(m_loader->mainResourceData()); |
|
173 return S_OK; |
|
174 } |
|
175 |
|
176 HRESULT STDMETHODCALLTYPE WebDataSource::representation( |
|
177 /* [retval][out] */ IWebDocumentRepresentation** rep) |
|
178 { |
|
179 HRESULT hr = S_OK; |
|
180 if (!m_representation) { |
|
181 WebHTMLRepresentation* htmlRep = WebHTMLRepresentation::createInstance(static_cast<WebFrame*>(m_loader->frameLoader()->client())); |
|
182 hr = htmlRep->QueryInterface(IID_IWebDocumentRepresentation, (void**) &m_representation); |
|
183 htmlRep->Release(); |
|
184 } |
|
185 |
|
186 return m_representation.copyRefTo(rep); |
|
187 } |
|
188 |
|
189 HRESULT STDMETHODCALLTYPE WebDataSource::webFrame( |
|
190 /* [retval][out] */ IWebFrame** frame) |
|
191 { |
|
192 *frame = static_cast<WebFrame*>(m_loader->frameLoader()->client()); |
|
193 (*frame)->AddRef(); |
|
194 return S_OK; |
|
195 } |
|
196 |
|
197 HRESULT STDMETHODCALLTYPE WebDataSource::initialRequest( |
|
198 /* [retval][out] */ IWebURLRequest** request) |
|
199 { |
|
200 *request = WebMutableURLRequest::createInstance(m_loader->initialRequest()); |
|
201 return S_OK; |
|
202 } |
|
203 |
|
204 HRESULT STDMETHODCALLTYPE WebDataSource::request( |
|
205 /* [retval][out] */ IWebMutableURLRequest** request) |
|
206 { |
|
207 *request = WebMutableURLRequest::createInstance(m_loader->request()); |
|
208 return S_OK; |
|
209 } |
|
210 |
|
211 HRESULT STDMETHODCALLTYPE WebDataSource::response( |
|
212 /* [retval][out] */ IWebURLResponse** response) |
|
213 { |
|
214 *response = WebURLResponse::createInstance(m_loader->response()); |
|
215 return S_OK; |
|
216 } |
|
217 |
|
218 HRESULT STDMETHODCALLTYPE WebDataSource::textEncodingName( |
|
219 /* [retval][out] */ BSTR* name) |
|
220 { |
|
221 String encoding = m_loader->overrideEncoding(); |
|
222 if (encoding.isNull()) |
|
223 encoding = m_loader->response().textEncodingName(); |
|
224 |
|
225 *name = BString(encoding).release(); |
|
226 |
|
227 return S_OK; |
|
228 } |
|
229 |
|
230 HRESULT STDMETHODCALLTYPE WebDataSource::isLoading( |
|
231 /* [retval][out] */ BOOL* loading) |
|
232 { |
|
233 *loading = m_loader->isLoadingInAPISense(); |
|
234 return S_OK; |
|
235 } |
|
236 |
|
237 HRESULT STDMETHODCALLTYPE WebDataSource::pageTitle( |
|
238 /* [retval][out] */ BSTR* title) |
|
239 { |
|
240 *title = BString(m_loader->title()).release(); |
|
241 return S_OK; |
|
242 } |
|
243 |
|
244 HRESULT STDMETHODCALLTYPE WebDataSource::unreachableURL( |
|
245 /* [retval][out] */ BSTR* url) |
|
246 { |
|
247 KURL unreachableURL = m_loader->unreachableURL(); |
|
248 BString urlString((LPOLESTR)unreachableURL.url().unicode(), unreachableURL.url().length()); |
|
249 |
|
250 *url = urlString.release(); |
|
251 return S_OK; |
|
252 } |
|
253 |
|
254 HRESULT STDMETHODCALLTYPE WebDataSource::webArchive( |
|
255 /* [retval][out] */ IWebArchive** /*archive*/) |
|
256 { |
|
257 ASSERT_NOT_REACHED(); |
|
258 return E_NOTIMPL; |
|
259 } |
|
260 |
|
261 HRESULT STDMETHODCALLTYPE WebDataSource::mainResource( |
|
262 /* [retval][out] */ IWebResource** /*resource*/) |
|
263 { |
|
264 ASSERT_NOT_REACHED(); |
|
265 return E_NOTIMPL; |
|
266 } |
|
267 |
|
268 HRESULT STDMETHODCALLTYPE WebDataSource::subresources( |
|
269 /* [retval][out] */ IEnumVARIANT** /*result*/) |
|
270 { |
|
271 ASSERT_NOT_REACHED(); |
|
272 return E_NOTIMPL; |
|
273 } |
|
274 |
|
275 HRESULT STDMETHODCALLTYPE WebDataSource::subresourceForURL( |
|
276 /* [in] */ BSTR url, |
|
277 /* [retval][out] */ IWebResource** resource) |
|
278 { |
|
279 if (!resource) { |
|
280 ASSERT_NOT_REACHED(); |
|
281 return E_POINTER; |
|
282 } |
|
283 |
|
284 *resource = 0; |
|
285 |
|
286 Document *doc = m_loader->frameLoader()->frame()->document(); |
|
287 |
|
288 if (!doc) |
|
289 return E_FAIL; |
|
290 |
|
291 CachedResource *cachedResource = doc->docLoader()->cachedResource(String(url)); |
|
292 |
|
293 if (!cachedResource) |
|
294 return E_FAIL; |
|
295 |
|
296 *resource = WebResource::createInstance(cachedResource->data(), cachedResource->response()); |
|
297 return S_OK; |
|
298 } |
|
299 |
|
300 HRESULT STDMETHODCALLTYPE WebDataSource::addSubresource( |
|
301 /* [in] */ IWebResource* /*subresource*/) |
|
302 { |
|
303 ASSERT_NOT_REACHED(); |
|
304 return E_NOTIMPL; |
|
305 } |