2 * Copyright (C) 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
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "DumpRenderTree.h"
30 #include "ResourceLoadDelegate.h"
32 #include "LayoutTestController.h"
33 #include <wtf/HashMap.h>
34 #include <wtf/Vector.h>
40 static inline wstring wstringFromBSTR(BSTR str)
42 return wstring(str, ::SysStringLen(str));
45 wstring wstringFromInt(int i)
47 std::wostringstream ss;
52 typedef HashMap<unsigned long, wstring> IdentifierMap;
54 IdentifierMap& urlMap()
56 static IdentifierMap urlMap;
61 static wstring descriptionSuitableForTestResult(unsigned long identifier)
63 IdentifierMap::iterator it = urlMap().find(identifier);
65 if (it == urlMap().end())
68 return urlSuitableForTestResult(it->second);
71 static wstring descriptionSuitableForTestResult(IWebURLRequest* request)
77 if (FAILED(request->URL(&urlBSTR)))
80 wstring url = urlSuitableForTestResult(wstringFromBSTR(urlBSTR));
81 ::SysFreeString(urlBSTR);
83 return L"<NSURLRequest " + url + L">";
86 static wstring descriptionSuitableForTestResult(IWebURLResponse* response)
92 if (FAILED(response->URL(&urlBSTR)))
95 wstring url = urlSuitableForTestResult(wstringFromBSTR(urlBSTR));
96 ::SysFreeString(urlBSTR);
98 return L"<NSURLResponse " + url + L">";
101 static wstring descriptionSuitableForTestResult(IWebError* error, unsigned long identifier)
103 wstring result = L"<NSError ";
106 if (FAILED(error->domain(&domainSTR)))
109 wstring domain = wstringFromBSTR(domainSTR);
110 ::SysFreeString(domainSTR);
113 if (FAILED(error->code(&code)))
116 if (domain == L"CFURLErrorDomain") {
117 domain = L"NSURLErrorDomain";
119 // Convert kCFURLErrorUnknown to NSURLErrorUnknown
122 } else if (domain == L"kCFErrorDomainWinSock") {
123 domain = L"NSURLErrorDomain";
125 // Convert the winsock error code to an NSURLError code.
126 if (code == WSAEADDRNOTAVAIL)
127 code = -1004; // NSURLErrorCannotConnectToHose;
130 result += L"domain " + domain;
131 result += L", code " + wstringFromInt(code);
134 if (FAILED(error->failingURL(&failingURLSTR)))
139 // If the error doesn't have a failing URL, we fake one by using the URL the resource had
140 // at creation time. This seems to work fine for now.
141 // See <rdar://problem/5064234> CFErrors should have failingURL key.
143 failingURL = wstringFromBSTR(failingURLSTR);
145 failingURL = descriptionSuitableForTestResult(identifier);
147 ::SysFreeString(failingURLSTR);
149 result += L", failing URL \"" + urlSuitableForTestResult(failingURL) + L"\">";
154 ResourceLoadDelegate::ResourceLoadDelegate()
159 ResourceLoadDelegate::~ResourceLoadDelegate()
163 HRESULT STDMETHODCALLTYPE ResourceLoadDelegate::QueryInterface(REFIID riid, void** ppvObject)
166 if (IsEqualGUID(riid, IID_IUnknown))
167 *ppvObject = static_cast<IWebResourceLoadDelegate*>(this);
168 else if (IsEqualGUID(riid, IID_IWebResourceLoadDelegate))
169 *ppvObject = static_cast<IWebResourceLoadDelegate*>(this);
171 return E_NOINTERFACE;
177 ULONG STDMETHODCALLTYPE ResourceLoadDelegate::AddRef(void)
182 ULONG STDMETHODCALLTYPE ResourceLoadDelegate::Release(void)
184 ULONG newRef = --m_refCount;
191 HRESULT STDMETHODCALLTYPE ResourceLoadDelegate::identifierForInitialRequest(
192 /* [in] */ IWebView* webView,
193 /* [in] */ IWebURLRequest* request,
194 /* [in] */ IWebDataSource* dataSource,
195 /* [in] */ unsigned long identifier)
197 if (!done && layoutTestController->dumpResourceLoadCallbacks()) {
199 if (FAILED(request->URL(&urlStr)))
202 urlMap().set(identifier, wstringFromBSTR(urlStr));
208 HRESULT STDMETHODCALLTYPE ResourceLoadDelegate::willSendRequest(
209 /* [in] */ IWebView* webView,
210 /* [in] */ unsigned long identifier,
211 /* [in] */ IWebURLRequest* request,
212 /* [in] */ IWebURLResponse* redirectResponse,
213 /* [in] */ IWebDataSource* dataSource,
214 /* [retval][out] */ IWebURLRequest **newRequest)
216 if (!done && layoutTestController->dumpResourceLoadCallbacks()) {
217 printf("%S - willSendRequest %S redirectResponse %S\n",
218 descriptionSuitableForTestResult(identifier).c_str(),
219 descriptionSuitableForTestResult(request).c_str(),
220 descriptionSuitableForTestResult(redirectResponse).c_str());
224 *newRequest = request;
228 HRESULT STDMETHODCALLTYPE ResourceLoadDelegate::didFinishLoadingFromDataSource(
229 /* [in] */ IWebView* webView,
230 /* [in] */ unsigned long identifier,
231 /* [in] */ IWebDataSource* dataSource)
233 if (!done && layoutTestController->dumpResourceLoadCallbacks()) {
234 printf("%S - didFinishLoading\n",
235 descriptionSuitableForTestResult(identifier).c_str()),
236 urlMap().remove(identifier);
242 HRESULT STDMETHODCALLTYPE ResourceLoadDelegate::didFailLoadingWithError(
243 /* [in] */ IWebView* webView,
244 /* [in] */ unsigned long identifier,
245 /* [in] */ IWebError* error,
246 /* [in] */ IWebDataSource* dataSource)
248 if (!done && layoutTestController->dumpResourceLoadCallbacks()) {
249 printf("%S - didFailLoadingWithError: %S\n",
250 descriptionSuitableForTestResult(identifier).c_str(),
251 descriptionSuitableForTestResult(error, identifier).c_str());
252 urlMap().remove(identifier);