HistoryDelegate.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2009 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. #include "config.h"
  26. #include "HistoryDelegate.h"
  27. #include "DumpRenderTree.h"
  28. #include "DumpRenderTreeWin.h"
  29. #include "TestRunner.h"
  30. #include <string>
  31. #include <WebKit/WebKit.h>
  32. using std::wstring;
  33. static inline wstring wstringFromBSTR(BSTR str)
  34. {
  35. return wstring(str, ::SysStringLen(str));
  36. }
  37. HistoryDelegate::HistoryDelegate()
  38. : m_refCount(1)
  39. {
  40. }
  41. HistoryDelegate::~HistoryDelegate()
  42. {
  43. }
  44. // IUnknown
  45. HRESULT HistoryDelegate::QueryInterface(REFIID riid, void** ppvObject)
  46. {
  47. *ppvObject = 0;
  48. if (IsEqualGUID(riid, IID_IUnknown))
  49. *ppvObject = static_cast<IWebHistoryDelegate*>(this);
  50. else if (IsEqualGUID(riid, IID_IWebHistoryDelegate))
  51. *ppvObject = static_cast<IWebHistoryDelegate*>(this);
  52. else
  53. return E_NOINTERFACE;
  54. AddRef();
  55. return S_OK;
  56. }
  57. ULONG HistoryDelegate::AddRef(void)
  58. {
  59. return ++m_refCount;
  60. }
  61. ULONG HistoryDelegate::Release(void)
  62. {
  63. ULONG newRef = --m_refCount;
  64. if (!newRef)
  65. delete(this);
  66. return newRef;
  67. }
  68. // IWebHistoryDelegate
  69. HRESULT HistoryDelegate::didNavigateWithNavigationData(IWebView* webView, IWebNavigationData* navigationData, IWebFrame* webFrame)
  70. {
  71. if (!gTestRunner->dumpHistoryDelegateCallbacks())
  72. return S_OK;
  73. BSTR urlBSTR;
  74. if (FAILED(navigationData->url(&urlBSTR)))
  75. return E_FAIL;
  76. wstring url;
  77. if (urlBSTR)
  78. url = urlSuitableForTestResult(wstringFromBSTR(urlBSTR));
  79. SysFreeString(urlBSTR);
  80. BSTR titleBSTR;
  81. if (FAILED(navigationData->title(&titleBSTR)))
  82. return E_FAIL;
  83. wstring title;
  84. if (titleBSTR)
  85. title = wstringFromBSTR(titleBSTR);
  86. SysFreeString(titleBSTR);
  87. COMPtr<IWebURLRequest> request;
  88. if (FAILED(navigationData->originalRequest(&request)))
  89. return E_FAIL;
  90. BSTR httpMethodBSTR;
  91. if (FAILED(request->HTTPMethod(&httpMethodBSTR)))
  92. return E_FAIL;
  93. wstring httpMethod;
  94. if (httpMethodBSTR)
  95. httpMethod = wstringFromBSTR(httpMethodBSTR);
  96. SysFreeString(httpMethodBSTR);
  97. COMPtr<IWebURLResponse> response;
  98. if (FAILED(navigationData->response(&response)))
  99. return E_FAIL;
  100. COMPtr<IWebHTTPURLResponse> httpResponse;
  101. if (FAILED(response->QueryInterface(&httpResponse)))
  102. return E_FAIL;
  103. int statusCode = 0;
  104. if (FAILED(httpResponse->statusCode(&statusCode)))
  105. return E_FAIL;
  106. BOOL hasSubstituteData;
  107. if (FAILED(navigationData->hasSubstituteData(&hasSubstituteData)))
  108. return E_FAIL;
  109. BSTR clientRedirectSourceBSTR;
  110. if (FAILED(navigationData->clientRedirectSource(&clientRedirectSourceBSTR)))
  111. return E_FAIL;
  112. bool hasClientRedirect = clientRedirectSourceBSTR && SysStringLen(clientRedirectSourceBSTR);
  113. wstring redirectSource;
  114. if (clientRedirectSourceBSTR)
  115. redirectSource = urlSuitableForTestResult(wstringFromBSTR(clientRedirectSourceBSTR));
  116. SysFreeString(clientRedirectSourceBSTR);
  117. bool wasFailure = hasSubstituteData || (httpResponse && statusCode >= 400);
  118. printf("WebView navigated to url \"%S\" with title \"%S\" with HTTP equivalent method \"%S\". The navigation was %s and was %s%S.\n",
  119. url.c_str(),
  120. title.c_str(),
  121. httpMethod.c_str(),
  122. wasFailure ? "a failure" : "successful",
  123. hasClientRedirect ? "a client redirect from " : "not a client redirect",
  124. redirectSource.c_str());
  125. return S_OK;
  126. }
  127. HRESULT HistoryDelegate::didPerformClientRedirectFromURL(IWebView*, BSTR sourceURL, BSTR destinationURL, IWebFrame*)
  128. {
  129. if (!gTestRunner->dumpHistoryDelegateCallbacks())
  130. return S_OK;
  131. wstring source;
  132. if (sourceURL)
  133. source = urlSuitableForTestResult(wstringFromBSTR(sourceURL));
  134. wstring destination;
  135. if (destinationURL)
  136. destination = urlSuitableForTestResult(wstringFromBSTR(destinationURL));
  137. printf("WebView performed a client redirect from \"%S\" to \"%S\".\n", source.c_str(), destination.c_str());
  138. return S_OK;
  139. }
  140. HRESULT HistoryDelegate::didPerformServerRedirectFromURL(IWebView* webView, BSTR sourceURL, BSTR destinationURL, IWebFrame* webFrame)
  141. {
  142. if (!gTestRunner->dumpHistoryDelegateCallbacks())
  143. return S_OK;
  144. wstring source;
  145. if (sourceURL)
  146. source = urlSuitableForTestResult(wstringFromBSTR(sourceURL));
  147. wstring destination;
  148. if (destinationURL)
  149. destination = urlSuitableForTestResult(wstringFromBSTR(destinationURL));
  150. printf("WebView performed a server redirect from \"%S\" to \"%S\".\n", source.c_str(), destination.c_str());
  151. return S_OK;
  152. }
  153. HRESULT HistoryDelegate::updateHistoryTitle(IWebView* webView, BSTR titleBSTR, BSTR urlBSTR)
  154. {
  155. if (!gTestRunner->dumpHistoryDelegateCallbacks())
  156. return S_OK;
  157. wstring url;
  158. if (urlBSTR)
  159. url = urlSuitableForTestResult(wstringFromBSTR(urlBSTR));
  160. wstring title;
  161. if (titleBSTR)
  162. title = wstringFromBSTR(titleBSTR);
  163. printf("WebView updated the title for history URL \"%S\" to \"%S\".\n", url.c_str(), title.c_str());
  164. return S_OK;
  165. }
  166. HRESULT HistoryDelegate::populateVisitedLinksForWebView(IWebView* webView)
  167. {
  168. if (!gTestRunner->dumpHistoryDelegateCallbacks())
  169. return S_OK;
  170. BSTR urlBSTR;
  171. if (FAILED(webView->mainFrameURL(&urlBSTR)))
  172. return E_FAIL;
  173. wstring url;
  174. if (urlBSTR)
  175. url = urlSuitableForTestResult(wstringFromBSTR(urlBSTR));
  176. SysFreeString(urlBSTR);
  177. if (gTestRunner->dumpVisitedLinksCallback())
  178. printf("Asked to populate visited links for WebView \"%S\"\n", url.c_str());
  179. return S_OK;
  180. }