ResourceLoadDelegate.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. *
  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.
  16. *
  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.
  27. */
  28. #include "config.h"
  29. #include "ResourceLoadDelegate.h"
  30. #include "DumpRenderTree.h"
  31. #include "TestRunner.h"
  32. #include <WebKit/WebKitCOMAPI.h>
  33. #include <comutil.h>
  34. #include <sstream>
  35. #include <tchar.h>
  36. #include <wtf/Vector.h>
  37. using namespace std;
  38. static inline wstring wstringFromBSTR(BSTR str)
  39. {
  40. return wstring(str, ::SysStringLen(str));
  41. }
  42. static inline wstring wstringFromInt(int i)
  43. {
  44. wostringstream ss;
  45. ss << i;
  46. return ss.str();
  47. }
  48. static inline BSTR BSTRFromString(const string& str)
  49. {
  50. int length = ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), 0, 0);
  51. BSTR result = ::SysAllocStringLen(0, length);
  52. ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), result, length);
  53. return result;
  54. }
  55. wstring ResourceLoadDelegate::descriptionSuitableForTestResult(unsigned long identifier) const
  56. {
  57. IdentifierMap::const_iterator it = m_urlMap.find(identifier);
  58. if (it == m_urlMap.end())
  59. return L"<unknown>";
  60. return urlSuitableForTestResult(it->value);
  61. }
  62. wstring ResourceLoadDelegate::descriptionSuitableForTestResult(IWebURLRequest* request)
  63. {
  64. if (!request)
  65. return L"(null)";
  66. BSTR urlBSTR;
  67. if (FAILED(request->URL(&urlBSTR)))
  68. return wstring();
  69. wstring url = urlSuitableForTestResult(wstringFromBSTR(urlBSTR));
  70. ::SysFreeString(urlBSTR);
  71. BSTR mainDocumentURLBSTR;
  72. if (FAILED(request->mainDocumentURL(&mainDocumentURLBSTR)))
  73. return wstring();
  74. wstring mainDocumentURL = urlSuitableForTestResult(wstringFromBSTR(mainDocumentURLBSTR));
  75. ::SysFreeString(mainDocumentURLBSTR);
  76. BSTR httpMethodBSTR;
  77. if (FAILED(request->HTTPMethod(&httpMethodBSTR)))
  78. return wstring();
  79. wstring httpMethod = wstringFromBSTR(httpMethodBSTR);
  80. ::SysFreeString(httpMethodBSTR);
  81. return L"<NSURLRequest URL " + url + L", main document URL " + mainDocumentURL + L", http method " + httpMethod + L">";
  82. }
  83. wstring ResourceLoadDelegate::descriptionSuitableForTestResult(IWebURLResponse* response)
  84. {
  85. if (!response)
  86. return L"(null)";
  87. BSTR urlBSTR;
  88. if (FAILED(response->URL(&urlBSTR)))
  89. return wstring();
  90. wstring url = urlSuitableForTestResult(wstringFromBSTR(urlBSTR));
  91. ::SysFreeString(urlBSTR);
  92. int statusCode = 0;
  93. COMPtr<IWebHTTPURLResponse> httpResponse;
  94. if (response && SUCCEEDED(response->QueryInterface(&httpResponse)))
  95. httpResponse->statusCode(&statusCode);
  96. return L"<NSURLResponse " + url + L", http status code " + wstringFromInt(statusCode) + L">";
  97. }
  98. wstring ResourceLoadDelegate::descriptionSuitableForTestResult(IWebError* error, unsigned long identifier) const
  99. {
  100. wstring result = L"<NSError ";
  101. BSTR domainSTR;
  102. if (FAILED(error->domain(&domainSTR)))
  103. return wstring();
  104. wstring domain = wstringFromBSTR(domainSTR);
  105. ::SysFreeString(domainSTR);
  106. int code;
  107. if (FAILED(error->code(&code)))
  108. return wstring();
  109. if (domain == L"CFURLErrorDomain") {
  110. domain = L"NSURLErrorDomain";
  111. // Convert kCFURLErrorUnknown to NSURLErrorUnknown
  112. if (code == -998)
  113. code = -1;
  114. } else if (domain == L"kCFErrorDomainWinSock") {
  115. domain = L"NSURLErrorDomain";
  116. // Convert the winsock error code to an NSURLError code.
  117. if (code == WSAEADDRNOTAVAIL)
  118. code = -1004; // NSURLErrorCannotConnectToHose;
  119. }
  120. result += L"domain " + domain;
  121. result += L", code " + wstringFromInt(code);
  122. BSTR failingURLSTR;
  123. if (FAILED(error->failingURL(&failingURLSTR)))
  124. return wstring();
  125. if (failingURLSTR) {
  126. result += L", failing URL \"" + urlSuitableForTestResult(wstringFromBSTR(failingURLSTR)) + L"\"";
  127. ::SysFreeString(failingURLSTR);
  128. }
  129. result += L">";
  130. return result;
  131. }
  132. ResourceLoadDelegate::ResourceLoadDelegate()
  133. : m_refCount(1)
  134. {
  135. }
  136. ResourceLoadDelegate::~ResourceLoadDelegate()
  137. {
  138. }
  139. HRESULT STDMETHODCALLTYPE ResourceLoadDelegate::QueryInterface(REFIID riid, void** ppvObject)
  140. {
  141. *ppvObject = 0;
  142. if (IsEqualGUID(riid, IID_IUnknown))
  143. *ppvObject = static_cast<IWebResourceLoadDelegate*>(this);
  144. else if (IsEqualGUID(riid, IID_IWebResourceLoadDelegate))
  145. *ppvObject = static_cast<IWebResourceLoadDelegate*>(this);
  146. else if (IsEqualGUID(riid, IID_IWebResourceLoadDelegatePrivate2))
  147. *ppvObject = static_cast<IWebResourceLoadDelegatePrivate2*>(this);
  148. else
  149. return E_NOINTERFACE;
  150. AddRef();
  151. return S_OK;
  152. }
  153. ULONG STDMETHODCALLTYPE ResourceLoadDelegate::AddRef(void)
  154. {
  155. return ++m_refCount;
  156. }
  157. ULONG STDMETHODCALLTYPE ResourceLoadDelegate::Release(void)
  158. {
  159. ULONG newRef = --m_refCount;
  160. if (!newRef)
  161. delete(this);
  162. return newRef;
  163. }
  164. HRESULT STDMETHODCALLTYPE ResourceLoadDelegate::identifierForInitialRequest(
  165. /* [in] */ IWebView* webView,
  166. /* [in] */ IWebURLRequest* request,
  167. /* [in] */ IWebDataSource* dataSource,
  168. /* [in] */ unsigned long identifier)
  169. {
  170. if (!done && gTestRunner->dumpResourceLoadCallbacks()) {
  171. BSTR urlStr;
  172. if (FAILED(request->URL(&urlStr)))
  173. return E_FAIL;
  174. ASSERT(!urlMap().contains(identifier));
  175. urlMap().set(identifier, wstringFromBSTR(urlStr));
  176. }
  177. return S_OK;
  178. }
  179. HRESULT STDMETHODCALLTYPE ResourceLoadDelegate::removeIdentifierForRequest(
  180. /* [in] */ IWebView* webView,
  181. /* [in] */ unsigned long identifier)
  182. {
  183. urlMap().remove(identifier);
  184. return S_OK;
  185. }
  186. HRESULT STDMETHODCALLTYPE ResourceLoadDelegate::willSendRequest(
  187. /* [in] */ IWebView* webView,
  188. /* [in] */ unsigned long identifier,
  189. /* [in] */ IWebURLRequest* request,
  190. /* [in] */ IWebURLResponse* redirectResponse,
  191. /* [in] */ IWebDataSource* dataSource,
  192. /* [retval][out] */ IWebURLRequest **newRequest)
  193. {
  194. if (!done && gTestRunner->dumpResourceLoadCallbacks()) {
  195. printf("%S - willSendRequest %S redirectResponse %S\n",
  196. descriptionSuitableForTestResult(identifier).c_str(),
  197. descriptionSuitableForTestResult(request).c_str(),
  198. descriptionSuitableForTestResult(redirectResponse).c_str());
  199. }
  200. if (!done && !gTestRunner->deferMainResourceDataLoad()) {
  201. COMPtr<IWebDataSourcePrivate> dataSourcePrivate(Query, dataSource);
  202. if (!dataSourcePrivate)
  203. return E_FAIL;
  204. dataSourcePrivate->setDeferMainResourceDataLoad(FALSE);
  205. }
  206. if (!done && gTestRunner->willSendRequestReturnsNull()) {
  207. *newRequest = 0;
  208. return S_OK;
  209. }
  210. if (!done && gTestRunner->willSendRequestReturnsNullOnRedirect() && redirectResponse) {
  211. printf("Returning null for this redirect\n");
  212. *newRequest = 0;
  213. return S_OK;
  214. }
  215. IWebMutableURLRequest* requestCopy = 0;
  216. request->mutableCopy(&requestCopy);
  217. const set<string>& clearHeaders = gTestRunner->willSendRequestClearHeaders();
  218. for (set<string>::const_iterator header = clearHeaders.begin(); header != clearHeaders.end(); ++header) {
  219. BSTR bstrHeader = BSTRFromString(*header);
  220. requestCopy->setValue(0, bstrHeader);
  221. SysFreeString(bstrHeader);
  222. }
  223. *newRequest = requestCopy;
  224. return S_OK;
  225. }
  226. HRESULT STDMETHODCALLTYPE ResourceLoadDelegate::didReceiveAuthenticationChallenge(
  227. /* [in] */ IWebView *webView,
  228. /* [in] */ unsigned long identifier,
  229. /* [in] */ IWebURLAuthenticationChallenge *challenge,
  230. /* [in] */ IWebDataSource *dataSource)
  231. {
  232. COMPtr<IWebURLAuthenticationChallengeSender> sender;
  233. if (!challenge || FAILED(challenge->sender(&sender)))
  234. return E_FAIL;
  235. if (!gTestRunner->handlesAuthenticationChallenges()) {
  236. printf("%S - didReceiveAuthenticationChallenge - Simulating cancelled authentication sheet\n", descriptionSuitableForTestResult(identifier).c_str());
  237. sender->continueWithoutCredentialForAuthenticationChallenge(challenge);
  238. return S_OK;
  239. }
  240. const char* user = gTestRunner->authenticationUsername().c_str();
  241. const char* password = gTestRunner->authenticationPassword().c_str();
  242. printf("%S - didReceiveAuthenticationChallenge - Responding with %s:%s\n", descriptionSuitableForTestResult(identifier).c_str(), user, password);
  243. COMPtr<IWebURLCredential> credential;
  244. if (FAILED(WebKitCreateInstance(CLSID_WebURLCredential, 0, IID_IWebURLCredential, (void**)&credential)))
  245. return E_FAIL;
  246. credential->initWithUser(_bstr_t(user), _bstr_t(password), WebURLCredentialPersistenceForSession);
  247. sender->useCredential(credential.get(), challenge);
  248. return S_OK;
  249. }
  250. HRESULT STDMETHODCALLTYPE ResourceLoadDelegate::didReceiveResponse(
  251. /* [in] */ IWebView* webView,
  252. /* [in] */ unsigned long identifier,
  253. /* [in] */ IWebURLResponse* response,
  254. /* [in] */ IWebDataSource* dataSource)
  255. {
  256. if (!done && gTestRunner->dumpResourceLoadCallbacks()) {
  257. printf("%S - didReceiveResponse %S\n",
  258. descriptionSuitableForTestResult(identifier).c_str(),
  259. descriptionSuitableForTestResult(response).c_str());
  260. }
  261. if (!done && gTestRunner->dumpResourceResponseMIMETypes()) {
  262. BSTR mimeTypeBSTR;
  263. if (FAILED(response->MIMEType(&mimeTypeBSTR)))
  264. E_FAIL;
  265. wstring mimeType = wstringFromBSTR(mimeTypeBSTR);
  266. ::SysFreeString(mimeTypeBSTR);
  267. BSTR urlBSTR;
  268. if (FAILED(response->URL(&urlBSTR)))
  269. E_FAIL;
  270. wstring url = wstringFromBSTR(urlBSTR);
  271. ::SysFreeString(urlBSTR);
  272. printf("%S has MIME type %S\n", lastPathComponent(url).c_str(), mimeType.c_str());
  273. }
  274. return S_OK;
  275. }
  276. HRESULT STDMETHODCALLTYPE ResourceLoadDelegate::didFinishLoadingFromDataSource(
  277. /* [in] */ IWebView* webView,
  278. /* [in] */ unsigned long identifier,
  279. /* [in] */ IWebDataSource* dataSource)
  280. {
  281. if (!done && gTestRunner->dumpResourceLoadCallbacks()) {
  282. printf("%S - didFinishLoading\n",
  283. descriptionSuitableForTestResult(identifier).c_str());
  284. }
  285. removeIdentifierForRequest(webView, identifier);
  286. return S_OK;
  287. }
  288. HRESULT STDMETHODCALLTYPE ResourceLoadDelegate::didFailLoadingWithError(
  289. /* [in] */ IWebView* webView,
  290. /* [in] */ unsigned long identifier,
  291. /* [in] */ IWebError* error,
  292. /* [in] */ IWebDataSource* dataSource)
  293. {
  294. if (!done && gTestRunner->dumpResourceLoadCallbacks()) {
  295. printf("%S - didFailLoadingWithError: %S\n",
  296. descriptionSuitableForTestResult(identifier).c_str(),
  297. descriptionSuitableForTestResult(error, identifier).c_str());
  298. }
  299. removeIdentifierForRequest(webView, identifier);
  300. return S_OK;
  301. }