2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "WebKitDLL.h"
28 #include "WebDataSource.h"
31 #include "MemoryStream.h"
32 #include "WebDocumentLoader.h"
36 #include "WebHTMLRepresentation.h"
37 #include "WebKitStatisticsPrivate.h"
38 #include "WebMutableURLRequest.h"
39 #include "WebResource.h"
40 #include "WebURLResponse.h"
41 #include <WebCore/BString.h>
42 #include <WebCore/CachedResourceLoader.h>
43 #include <WebCore/Document.h>
44 #include <WebCore/Frame.h>
45 #include <WebCore/FrameLoader.h>
46 #include <WebCore/KURL.h>
48 using namespace WebCore;
50 // WebDataSource ----------------------------------------------------------------
52 // {F230854D-7091-428a-8DB5-37CABA44C105}
53 const GUID IID_WebDataSource =
54 { 0x5c2f9099, 0xe65e, 0x4a0f, { 0x9c, 0xa0, 0x6a, 0xd6, 0x92, 0x52, 0xa6, 0x2a } };
56 WebDataSource::WebDataSource(WebDocumentLoader* loader)
62 gClassNameCount.add("WebDataSource");
65 WebDataSource::~WebDataSource()
68 m_loader->detachDataSource();
71 gClassNameCount.remove("WebDataSource");
74 WebDataSource* WebDataSource::createInstance(WebDocumentLoader* loader)
76 WebDataSource* instance = new WebDataSource(loader);
81 WebDocumentLoader* WebDataSource::documentLoader() const
83 return m_loader.get();
86 // IWebDataSourcePrivate ------------------------------------------------------
88 HRESULT STDMETHODCALLTYPE WebDataSource::overrideEncoding(
89 /* [retval][out] */ BSTR* /*encoding*/)
95 HRESULT STDMETHODCALLTYPE WebDataSource::setOverrideEncoding(
96 /* [in] */ BSTR /*encoding*/)
102 HRESULT STDMETHODCALLTYPE WebDataSource::mainDocumentError(
103 /* [retval][out] */ IWebError** error)
106 ASSERT_NOT_REACHED();
115 if (m_loader->mainDocumentError().isNull())
118 *error = WebError::createInstance(m_loader->mainDocumentError());
122 HRESULT STDMETHODCALLTYPE WebDataSource::setDeferMainResourceDataLoad(
123 /* [in] */ BOOL flag)
128 m_loader->setDeferMainResourceDataLoad(flag);
132 // IUnknown -------------------------------------------------------------------
134 HRESULT STDMETHODCALLTYPE WebDataSource::QueryInterface(REFIID riid, void** ppvObject)
137 if (IsEqualGUID(riid, IID_WebDataSource))
139 else if (IsEqualGUID(riid, IID_IUnknown))
140 *ppvObject = static_cast<IWebDataSource*>(this);
141 else if (IsEqualGUID(riid, IID_IWebDataSource))
142 *ppvObject = static_cast<IWebDataSource*>(this);
143 else if (IsEqualGUID(riid, IID_IWebDataSourcePrivate))
144 *ppvObject = static_cast<IWebDataSourcePrivate*>(this);
146 return E_NOINTERFACE;
152 ULONG STDMETHODCALLTYPE WebDataSource::AddRef(void)
157 ULONG STDMETHODCALLTYPE WebDataSource::Release(void)
159 ULONG newRef = --m_refCount;
166 // IWebDataSource --------------------------------------------------------------
168 HRESULT STDMETHODCALLTYPE WebDataSource::initWithRequest(
169 /* [in] */ IWebURLRequest* /*request*/)
171 ASSERT_NOT_REACHED();
175 HRESULT STDMETHODCALLTYPE WebDataSource::data(
176 /* [retval][out] */ IStream** stream)
182 return MemoryStream::createInstance(m_loader->mainResourceData()).copyRefTo(stream);
185 HRESULT STDMETHODCALLTYPE WebDataSource::representation(
186 /* [retval][out] */ IWebDocumentRepresentation** rep)
189 if (!m_representation) {
190 WebHTMLRepresentation* htmlRep = WebHTMLRepresentation::createInstance(static_cast<WebFrame*>(m_loader->frameLoader()->client()));
191 hr = htmlRep->QueryInterface(IID_IWebDocumentRepresentation, (void**) &m_representation);
195 return m_representation.copyRefTo(rep);
198 HRESULT STDMETHODCALLTYPE WebDataSource::webFrame(
199 /* [retval][out] */ IWebFrame** frame)
201 *frame = static_cast<WebFrame*>(m_loader->frameLoader()->client());
206 HRESULT STDMETHODCALLTYPE WebDataSource::initialRequest(
207 /* [retval][out] */ IWebURLRequest** request)
209 *request = WebMutableURLRequest::createInstance(m_loader->originalRequest());
213 HRESULT STDMETHODCALLTYPE WebDataSource::request(
214 /* [retval][out] */ IWebMutableURLRequest** request)
216 *request = WebMutableURLRequest::createInstance(m_loader->request());
220 HRESULT STDMETHODCALLTYPE WebDataSource::response(
221 /* [retval][out] */ IWebURLResponse** response)
223 *response = WebURLResponse::createInstance(m_loader->response());
227 HRESULT STDMETHODCALLTYPE WebDataSource::textEncodingName(
228 /* [retval][out] */ BSTR* name)
230 String encoding = m_loader->overrideEncoding();
231 if (encoding.isNull())
232 encoding = m_loader->response().textEncodingName();
234 *name = BString(encoding).release();
239 HRESULT STDMETHODCALLTYPE WebDataSource::isLoading(
240 /* [retval][out] */ BOOL* loading)
242 *loading = m_loader->isLoadingInAPISense();
246 HRESULT STDMETHODCALLTYPE WebDataSource::pageTitle(
247 /* [retval][out] */ BSTR* title)
249 *title = BString(m_loader->title().string()).release();
253 HRESULT STDMETHODCALLTYPE WebDataSource::unreachableURL(
254 /* [retval][out] */ BSTR* url)
256 KURL unreachableURL = m_loader->unreachableURL();
257 BString urlString((LPOLESTR)unreachableURL.string().characters(), unreachableURL.string().length());
259 *url = urlString.release();
263 HRESULT STDMETHODCALLTYPE WebDataSource::webArchive(
264 /* [retval][out] */ IWebArchive** /*archive*/)
266 ASSERT_NOT_REACHED();
270 HRESULT STDMETHODCALLTYPE WebDataSource::mainResource(
271 /* [retval][out] */ IWebResource** /*resource*/)
273 ASSERT_NOT_REACHED();
277 HRESULT STDMETHODCALLTYPE WebDataSource::subresources(
278 /* [retval][out] */ IEnumVARIANT** /*result*/)
280 ASSERT_NOT_REACHED();
284 HRESULT STDMETHODCALLTYPE WebDataSource::subresourceForURL(
286 /* [retval][out] */ IWebResource** resource)
289 ASSERT_NOT_REACHED();
295 Document *doc = m_loader->frameLoader()->frame()->document();
300 CachedResource *cachedResource = doc->cachedResourceLoader()->cachedResource(String(url));
305 *resource = WebResource::createInstance(cachedResource->data(), cachedResource->response());
309 HRESULT STDMETHODCALLTYPE WebDataSource::addSubresource(
310 /* [in] */ IWebResource* /*subresource*/)
312 ASSERT_NOT_REACHED();