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 "WebKit.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
WebDataSource(WebDocumentLoader * loader)58 WebDataSource::WebDataSource(WebDocumentLoader* loader)
59 : m_refCount(0)
60 , m_loader(loader)
61 {
62 WebDataSourceCount++;
63 gClassCount++;
64 gClassNameCount.add("WebDataSource");
65 }
66
~WebDataSource()67 WebDataSource::~WebDataSource()
68 {
69 if (m_loader)
70 m_loader->detachDataSource();
71 WebDataSourceCount--;
72 gClassCount--;
73 gClassNameCount.remove("WebDataSource");
74 }
75
createInstance(WebDocumentLoader * loader)76 WebDataSource* WebDataSource::createInstance(WebDocumentLoader* loader)
77 {
78 WebDataSource* instance = new WebDataSource(loader);
79 instance->AddRef();
80 return instance;
81 }
82
documentLoader() const83 WebDocumentLoader* WebDataSource::documentLoader() const
84 {
85 return m_loader.get();
86 }
87
88 // IWebDataSourcePrivate ------------------------------------------------------
89
overrideEncoding(BSTR *)90 HRESULT STDMETHODCALLTYPE WebDataSource::overrideEncoding(
91 /* [retval][out] */ BSTR* /*encoding*/)
92 {
93 ASSERT_NOT_REACHED();
94 return E_NOTIMPL;
95 }
96
setOverrideEncoding(BSTR)97 HRESULT STDMETHODCALLTYPE WebDataSource::setOverrideEncoding(
98 /* [in] */ BSTR /*encoding*/)
99 {
100 ASSERT_NOT_REACHED();
101 return E_NOTIMPL;
102 }
103
mainDocumentError(IWebError ** error)104 HRESULT STDMETHODCALLTYPE WebDataSource::mainDocumentError(
105 /* [retval][out] */ IWebError** error)
106 {
107 if (!error) {
108 ASSERT_NOT_REACHED();
109 return E_POINTER;
110 }
111
112 *error = 0;
113
114 if (!m_loader)
115 return E_FAIL;
116
117 if (m_loader->mainDocumentError().isNull())
118 return S_OK;
119
120 *error = WebError::createInstance(m_loader->mainDocumentError());
121 return S_OK;
122 }
123
124 // IUnknown -------------------------------------------------------------------
125
QueryInterface(REFIID riid,void ** ppvObject)126 HRESULT STDMETHODCALLTYPE WebDataSource::QueryInterface(REFIID riid, void** ppvObject)
127 {
128 *ppvObject = 0;
129 if (IsEqualGUID(riid, IID_WebDataSource))
130 *ppvObject = this;
131 else if (IsEqualGUID(riid, IID_IUnknown))
132 *ppvObject = static_cast<IWebDataSource*>(this);
133 else if (IsEqualGUID(riid, IID_IWebDataSource))
134 *ppvObject = static_cast<IWebDataSource*>(this);
135 else if (IsEqualGUID(riid, IID_IWebDataSourcePrivate))
136 *ppvObject = static_cast<IWebDataSourcePrivate*>(this);
137 else
138 return E_NOINTERFACE;
139
140 AddRef();
141 return S_OK;
142 }
143
AddRef(void)144 ULONG STDMETHODCALLTYPE WebDataSource::AddRef(void)
145 {
146 return ++m_refCount;
147 }
148
Release(void)149 ULONG STDMETHODCALLTYPE WebDataSource::Release(void)
150 {
151 ULONG newRef = --m_refCount;
152 if (!newRef)
153 delete(this);
154
155 return newRef;
156 }
157
158 // IWebDataSource --------------------------------------------------------------
159
initWithRequest(IWebURLRequest *)160 HRESULT STDMETHODCALLTYPE WebDataSource::initWithRequest(
161 /* [in] */ IWebURLRequest* /*request*/)
162 {
163 ASSERT_NOT_REACHED();
164 return E_NOTIMPL;
165 }
166
data(IStream ** stream)167 HRESULT STDMETHODCALLTYPE WebDataSource::data(
168 /* [retval][out] */ IStream** stream)
169 {
170 *stream = 0;
171 if (!m_loader)
172 return E_FAIL;
173
174 *stream = MemoryStream::createInstance(m_loader->mainResourceData());
175 return S_OK;
176 }
177
representation(IWebDocumentRepresentation ** rep)178 HRESULT STDMETHODCALLTYPE WebDataSource::representation(
179 /* [retval][out] */ IWebDocumentRepresentation** rep)
180 {
181 HRESULT hr = S_OK;
182 if (!m_representation) {
183 WebHTMLRepresentation* htmlRep = WebHTMLRepresentation::createInstance(static_cast<WebFrame*>(m_loader->frameLoader()->client()));
184 hr = htmlRep->QueryInterface(IID_IWebDocumentRepresentation, (void**) &m_representation);
185 htmlRep->Release();
186 }
187
188 return m_representation.copyRefTo(rep);
189 }
190
webFrame(IWebFrame ** frame)191 HRESULT STDMETHODCALLTYPE WebDataSource::webFrame(
192 /* [retval][out] */ IWebFrame** frame)
193 {
194 *frame = static_cast<WebFrame*>(m_loader->frameLoader()->client());
195 (*frame)->AddRef();
196 return S_OK;
197 }
198
initialRequest(IWebURLRequest ** request)199 HRESULT STDMETHODCALLTYPE WebDataSource::initialRequest(
200 /* [retval][out] */ IWebURLRequest** request)
201 {
202 *request = WebMutableURLRequest::createInstance(m_loader->originalRequest());
203 return S_OK;
204 }
205
request(IWebMutableURLRequest ** request)206 HRESULT STDMETHODCALLTYPE WebDataSource::request(
207 /* [retval][out] */ IWebMutableURLRequest** request)
208 {
209 *request = WebMutableURLRequest::createInstance(m_loader->request());
210 return S_OK;
211 }
212
response(IWebURLResponse ** response)213 HRESULT STDMETHODCALLTYPE WebDataSource::response(
214 /* [retval][out] */ IWebURLResponse** response)
215 {
216 *response = WebURLResponse::createInstance(m_loader->response());
217 return S_OK;
218 }
219
textEncodingName(BSTR * name)220 HRESULT STDMETHODCALLTYPE WebDataSource::textEncodingName(
221 /* [retval][out] */ BSTR* name)
222 {
223 String encoding = m_loader->overrideEncoding();
224 if (encoding.isNull())
225 encoding = m_loader->response().textEncodingName();
226
227 *name = BString(encoding).release();
228
229 return S_OK;
230 }
231
isLoading(BOOL * loading)232 HRESULT STDMETHODCALLTYPE WebDataSource::isLoading(
233 /* [retval][out] */ BOOL* loading)
234 {
235 *loading = m_loader->isLoadingInAPISense();
236 return S_OK;
237 }
238
pageTitle(BSTR * title)239 HRESULT STDMETHODCALLTYPE WebDataSource::pageTitle(
240 /* [retval][out] */ BSTR* title)
241 {
242 *title = BString(m_loader->title()).release();
243 return S_OK;
244 }
245
unreachableURL(BSTR * url)246 HRESULT STDMETHODCALLTYPE WebDataSource::unreachableURL(
247 /* [retval][out] */ BSTR* url)
248 {
249 KURL unreachableURL = m_loader->unreachableURL();
250 BString urlString((LPOLESTR)unreachableURL.string().characters(), unreachableURL.string().length());
251
252 *url = urlString.release();
253 return S_OK;
254 }
255
webArchive(IWebArchive **)256 HRESULT STDMETHODCALLTYPE WebDataSource::webArchive(
257 /* [retval][out] */ IWebArchive** /*archive*/)
258 {
259 ASSERT_NOT_REACHED();
260 return E_NOTIMPL;
261 }
262
mainResource(IWebResource **)263 HRESULT STDMETHODCALLTYPE WebDataSource::mainResource(
264 /* [retval][out] */ IWebResource** /*resource*/)
265 {
266 ASSERT_NOT_REACHED();
267 return E_NOTIMPL;
268 }
269
subresources(IEnumVARIANT **)270 HRESULT STDMETHODCALLTYPE WebDataSource::subresources(
271 /* [retval][out] */ IEnumVARIANT** /*result*/)
272 {
273 ASSERT_NOT_REACHED();
274 return E_NOTIMPL;
275 }
276
subresourceForURL(BSTR url,IWebResource ** resource)277 HRESULT STDMETHODCALLTYPE WebDataSource::subresourceForURL(
278 /* [in] */ BSTR url,
279 /* [retval][out] */ IWebResource** resource)
280 {
281 if (!resource) {
282 ASSERT_NOT_REACHED();
283 return E_POINTER;
284 }
285
286 *resource = 0;
287
288 Document *doc = m_loader->frameLoader()->frame()->document();
289
290 if (!doc)
291 return E_FAIL;
292
293 CachedResource *cachedResource = doc->docLoader()->cachedResource(String(url));
294
295 if (!cachedResource)
296 return E_FAIL;
297
298 *resource = WebResource::createInstance(cachedResource->data(), cachedResource->response());
299 return S_OK;
300 }
301
addSubresource(IWebResource *)302 HRESULT STDMETHODCALLTYPE WebDataSource::addSubresource(
303 /* [in] */ IWebResource* /*subresource*/)
304 {
305 ASSERT_NOT_REACHED();
306 return E_NOTIMPL;
307 }
308