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