#include "WebKitDLL.h"
#include "WebDataSource.h"
#include "WebKit.h"
#include "MemoryStream.h"
#include "WebDocumentLoader.h"
#include "WebError.h"
#include "WebFrame.h"
#include "WebFrameLoaderClient.h"
#include "WebKit.h"
#include "WebHTMLRepresentation.h"
#include "WebKitStatisticsPrivate.h"
#include "WebMutableURLRequest.h"
#include "WebResource.h"
#include "WebURLResponse.h"
#include <WebCore/BString.h>
#include <WebCore/CachedResourceLoader.h>
#include <WebCore/Document.h>
#include <WebCore/Frame.h>
#include <WebCore/FrameLoader.h>
#include <WebCore/URL.h>
using namespace WebCore;
const GUID IID_WebDataSource =
{ 0x5c2f9099, 0xe65e, 0x4a0f, { 0x9c, 0xa0, 0x6a, 0xd6, 0x92, 0x52, 0xa6, 0x2a } };
WebDataSource::WebDataSource(WebDocumentLoader* loader)
: m_refCount(0)
, m_loader(loader)
{
WebDataSourceCount++;
gClassCount++;
gClassNameCount().add("WebDataSource");
}
WebDataSource::~WebDataSource()
{
if (m_loader)
m_loader->detachDataSource();
WebDataSourceCount--;
gClassCount--;
gClassNameCount().remove("WebDataSource");
}
WebDataSource* WebDataSource::createInstance(WebDocumentLoader* loader)
{
WebDataSource* instance = new WebDataSource(loader);
instance->AddRef();
return instance;
}
WebDocumentLoader* WebDataSource::documentLoader() const
{
return m_loader.get();
}
HRESULT STDMETHODCALLTYPE WebDataSource::overrideEncoding(
BSTR* )
{
ASSERT_NOT_REACHED();
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebDataSource::setOverrideEncoding(
BSTR )
{
ASSERT_NOT_REACHED();
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebDataSource::mainDocumentError(
IWebError** error)
{
if (!error) {
ASSERT_NOT_REACHED();
return E_POINTER;
}
*error = 0;
if (!m_loader)
return E_FAIL;
if (m_loader->mainDocumentError().isNull())
return S_OK;
*error = WebError::createInstance(m_loader->mainDocumentError());
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDataSource::setDeferMainResourceDataLoad(
BOOL flag)
{
if (!m_loader)
return E_FAIL;
m_loader->setDeferMainResourceDataLoad(flag);
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDataSource::QueryInterface(REFIID riid, void** ppvObject)
{
*ppvObject = 0;
if (IsEqualGUID(riid, IID_WebDataSource))
*ppvObject = this;
else if (IsEqualGUID(riid, IID_IUnknown))
*ppvObject = static_cast<IWebDataSource*>(this);
else if (IsEqualGUID(riid, IID_IWebDataSource))
*ppvObject = static_cast<IWebDataSource*>(this);
else if (IsEqualGUID(riid, IID_IWebDataSourcePrivate))
*ppvObject = static_cast<IWebDataSourcePrivate*>(this);
else
return E_NOINTERFACE;
AddRef();
return S_OK;
}
ULONG STDMETHODCALLTYPE WebDataSource::AddRef(void)
{
return ++m_refCount;
}
ULONG STDMETHODCALLTYPE WebDataSource::Release(void)
{
ULONG newRef = --m_refCount;
if (!newRef)
delete(this);
return newRef;
}
HRESULT STDMETHODCALLTYPE WebDataSource::initWithRequest(
IWebURLRequest* )
{
ASSERT_NOT_REACHED();
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebDataSource::data(
IStream** stream)
{
*stream = 0;
if (!m_loader)
return E_FAIL;
return MemoryStream::createInstance(m_loader->mainResourceData()).copyRefTo(stream);
}
HRESULT WebDataSource::representation( IWebDocumentRepresentation** rep)
{
HRESULT hr = S_OK;
if (!m_representation) {
WebHTMLRepresentation* htmlRep = WebHTMLRepresentation::createInstance(static_cast<WebFrameLoaderClient&>(m_loader->frameLoader()->client()).webFrame());
hr = htmlRep->QueryInterface(IID_IWebDocumentRepresentation, (void**) &m_representation);
htmlRep->Release();
}
return m_representation.copyRefTo(rep);
}
HRESULT WebDataSource::webFrame( IWebFrame** frame)
{
*frame = static_cast<WebFrameLoaderClient&>(m_loader->frameLoader()->client()).webFrame();
(*frame)->AddRef();
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDataSource::initialRequest(
IWebURLRequest** request)
{
*request = WebMutableURLRequest::createInstance(m_loader->originalRequest());
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDataSource::request(
IWebMutableURLRequest** request)
{
*request = WebMutableURLRequest::createInstance(m_loader->request());
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDataSource::response(
IWebURLResponse** response)
{
*response = WebURLResponse::createInstance(m_loader->response());
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDataSource::textEncodingName(
BSTR* name)
{
String encoding = m_loader->overrideEncoding();
if (encoding.isNull())
encoding = m_loader->response().textEncodingName();
*name = BString(encoding).release();
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDataSource::isLoading(
BOOL* loading)
{
*loading = m_loader->isLoadingInAPISense();
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDataSource::pageTitle(
BSTR* title)
{
*title = BString(m_loader->title().string()).release();
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDataSource::unreachableURL(
BSTR* url)
{
URL unreachableURL = m_loader->unreachableURL();
BString urlString(unreachableURL.string());
*url = urlString.release();
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDataSource::webArchive(
IWebArchive** )
{
ASSERT_NOT_REACHED();
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebDataSource::mainResource(
IWebResource** )
{
ASSERT_NOT_REACHED();
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebDataSource::subresources(
IEnumVARIANT** )
{
ASSERT_NOT_REACHED();
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebDataSource::subresourceForURL(
BSTR url,
IWebResource** resource)
{
if (!resource) {
ASSERT_NOT_REACHED();
return E_POINTER;
}
*resource = nullptr;
Document* doc = m_loader->frameLoader()->frame().document();
if (!doc)
return E_FAIL;
CachedResource* cachedResource = doc->cachedResourceLoader().cachedResource(String(url));
if (!cachedResource)
return E_FAIL;
*resource = WebResource::createInstance(cachedResource->resourceBuffer(), cachedResource->response());
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDataSource::addSubresource(
IWebResource* )
{
ASSERT_NOT_REACHED();
return E_NOTIMPL;
}