1/*
2 * Copyright (C) 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 "WebResource.h"
29
30#include "MarshallingHelpers.h"
31#include "MemoryStream.h"
32#include <WebCore/BString.h>
33
34using namespace WebCore;
35
36// WebResource ---------------------------------------------------------------------
37
38WebResource::WebResource(IStream* data, const WebCore::KURL& url, const WTF::String& mimeType, const WTF::String& textEncodingName, const WTF::String& frameName)
39    : m_refCount(0)
40    , m_data(data)
41    , m_url(url)
42    , m_mimeType(mimeType)
43    , m_textEncodingName(textEncodingName)
44    , m_frameName(frameName)
45{
46    gClassCount++;
47    gClassNameCount.add("WebResource");
48}
49
50WebResource::~WebResource()
51{
52    gClassCount--;
53    gClassNameCount.remove("WebResource");
54}
55
56WebResource* WebResource::createInstance(PassRefPtr<WebCore::SharedBuffer> data, const WebCore::ResourceResponse& response)
57{
58    COMPtr<MemoryStream> memoryStream = MemoryStream::createInstance(data);
59
60    WebResource* instance = new WebResource(memoryStream.get(), response.url(), response.mimeType(), response.textEncodingName(), String());
61    instance->AddRef();
62    return instance;
63}
64
65// IUnknown -------------------------------------------------------------------
66
67HRESULT STDMETHODCALLTYPE WebResource::QueryInterface(REFIID riid, void** ppvObject)
68{
69    *ppvObject = 0;
70    if (IsEqualGUID(riid, IID_IUnknown))
71        *ppvObject = static_cast<IUnknown*>(this);
72    else if (IsEqualGUID(riid, IID_IWebResource))
73        *ppvObject = static_cast<IWebResource*>(this);
74    else
75        return E_NOINTERFACE;
76
77    AddRef();
78    return S_OK;
79}
80
81ULONG STDMETHODCALLTYPE WebResource::AddRef(void)
82{
83    return ++m_refCount;
84}
85
86ULONG STDMETHODCALLTYPE WebResource::Release(void)
87{
88    ULONG newRef = --m_refCount;
89    if (!newRef)
90        delete(this);
91
92    return newRef;
93}
94
95// WebResource ------------------------------------------------------------------
96
97HRESULT STDMETHODCALLTYPE WebResource::initWithData(
98    /* [in] */ IStream *data,
99    /* [in] */ BSTR url,
100    /* [in] */ BSTR mimeType,
101    /* [in] */ BSTR textEncodingName,
102    /* [in] */ BSTR frameName)
103{
104    m_data = data;
105    m_url = MarshallingHelpers::BSTRToKURL(url);
106    m_mimeType = String(mimeType);
107    m_textEncodingName = String(textEncodingName);
108    m_frameName = String(frameName);
109
110    return S_OK;
111}
112
113
114HRESULT STDMETHODCALLTYPE WebResource::data(
115    /* [retval][out] */ IStream **data)
116{
117    return m_data.copyRefTo(data);
118}
119
120HRESULT STDMETHODCALLTYPE WebResource::URL(
121    /* [retval][out] */ BSTR *url)
122{
123    if (!url) {
124        ASSERT_NOT_REACHED();
125        return E_POINTER;
126    }
127
128    *url = BString(String(m_url.string())).release();
129    return S_OK;
130}
131
132HRESULT STDMETHODCALLTYPE WebResource::MIMEType(
133    /* [retval][out] */ BSTR *mime)
134{
135    if (!mime) {
136        ASSERT_NOT_REACHED();
137        return E_POINTER;
138    }
139
140    *mime = BString(m_mimeType).release();
141    return S_OK;
142}
143
144HRESULT STDMETHODCALLTYPE WebResource::textEncodingName(
145    /* [retval][out] */ BSTR *encodingName)
146{
147    if (!encodingName) {
148        ASSERT_NOT_REACHED();
149        return E_POINTER;
150    }
151
152    *encodingName = BString(m_textEncodingName).release();
153    return S_OK;
154}
155
156HRESULT STDMETHODCALLTYPE WebResource::frameName(
157    /* [retval][out] */ BSTR *name)
158{
159    if (!name) {
160        ASSERT_NOT_REACHED();
161        return E_POINTER;
162    }
163
164    *name = BString(m_frameName).release();
165    return S_OK;
166}
167