2 * Copyright (C) 2009, 2014 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
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE 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 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.
27 #include "HistoryDelegate.h"
29 #include "DumpRenderTree.h"
30 #include "DumpRenderTreeWin.h"
31 #include "TestRunner.h"
34 #include <WebKit/WebKit.h>
38 static inline wstring wstringFromBSTR(BSTR str)
40 return wstring(str, ::SysStringLen(str));
43 HistoryDelegate::HistoryDelegate()
48 HistoryDelegate::~HistoryDelegate()
53 HRESULT HistoryDelegate::QueryInterface(REFIID riid, void** ppvObject)
56 if (IsEqualGUID(riid, IID_IUnknown))
57 *ppvObject = static_cast<IWebHistoryDelegate*>(this);
58 else if (IsEqualGUID(riid, IID_IWebHistoryDelegate))
59 *ppvObject = static_cast<IWebHistoryDelegate*>(this);
67 ULONG HistoryDelegate::AddRef(void)
72 ULONG HistoryDelegate::Release(void)
74 ULONG newRef = --m_refCount;
81 // IWebHistoryDelegate
82 HRESULT HistoryDelegate::didNavigateWithNavigationData(IWebView* webView, IWebNavigationData* navigationData, IWebFrame* webFrame)
84 if (!gTestRunner->dumpHistoryDelegateCallbacks())
88 if (FAILED(navigationData->url(&urlBSTR.GetBSTR())))
92 url = urlSuitableForTestResult(wstringFromBSTR(urlBSTR));
95 if (FAILED(navigationData->title(&titleBSTR.GetBSTR())))
98 COMPtr<IWebURLRequest> request;
99 if (FAILED(navigationData->originalRequest(&request)))
102 _bstr_t httpMethodBSTR;
103 if (FAILED(request->HTTPMethod(&httpMethodBSTR.GetBSTR())))
106 COMPtr<IWebURLResponse> response;
107 if (FAILED(navigationData->response(&response)))
110 COMPtr<IWebHTTPURLResponse> httpResponse;
111 if (FAILED(response->QueryInterface(&httpResponse)))
115 if (FAILED(httpResponse->statusCode(&statusCode)))
118 BOOL hasSubstituteData;
119 if (FAILED(navigationData->hasSubstituteData(&hasSubstituteData)))
122 _bstr_t clientRedirectSourceBSTR;
123 if (FAILED(navigationData->clientRedirectSource(&clientRedirectSourceBSTR.GetBSTR())))
125 bool hasClientRedirect = clientRedirectSourceBSTR.length();
126 wstring redirectSource;
127 if (clientRedirectSourceBSTR.length())
128 redirectSource = urlSuitableForTestResult(wstringFromBSTR(clientRedirectSourceBSTR));
130 bool wasFailure = hasSubstituteData || (httpResponse && statusCode >= 400);
132 printf("WebView navigated to url \"%S\" with title \"%S\" with HTTP equivalent method \"%S\". The navigation was %s and was %s%S.\n",
134 static_cast<wchar_t*>(titleBSTR),
135 static_cast<wchar_t*>(httpMethodBSTR),
136 wasFailure ? "a failure" : "successful",
137 hasClientRedirect ? "a client redirect from " : "not a client redirect",
138 redirectSource.c_str());
143 HRESULT HistoryDelegate::didPerformClientRedirectFromURL(IWebView*, BSTR sourceURL, BSTR destinationURL, IWebFrame*)
145 if (!gTestRunner->dumpHistoryDelegateCallbacks())
150 source = urlSuitableForTestResult(wstringFromBSTR(sourceURL));
154 destination = urlSuitableForTestResult(wstringFromBSTR(destinationURL));
156 printf("WebView performed a client redirect from \"%S\" to \"%S\".\n", source.c_str(), destination.c_str());
160 HRESULT HistoryDelegate::didPerformServerRedirectFromURL(IWebView* webView, BSTR sourceURL, BSTR destinationURL, IWebFrame* webFrame)
162 if (!gTestRunner->dumpHistoryDelegateCallbacks())
167 source = urlSuitableForTestResult(wstringFromBSTR(sourceURL));
171 destination = urlSuitableForTestResult(wstringFromBSTR(destinationURL));
173 printf("WebView performed a server redirect from \"%S\" to \"%S\".\n", source.c_str(), destination.c_str());
177 HRESULT HistoryDelegate::updateHistoryTitle(IWebView* webView, BSTR titleBSTR, BSTR urlBSTR)
179 if (!gTestRunner->dumpHistoryDelegateCallbacks())
184 url = urlSuitableForTestResult(wstringFromBSTR(urlBSTR));
186 printf("WebView updated the title for history URL \"%S\" to \"%S\".\n", url.c_str(), titleBSTR ? titleBSTR : L"");
190 HRESULT HistoryDelegate::populateVisitedLinksForWebView(IWebView* webView)
192 if (!gTestRunner->dumpHistoryDelegateCallbacks())
196 if (FAILED(webView->mainFrameURL(&urlBSTR.GetBSTR())))
200 if (urlBSTR.length())
201 url = urlSuitableForTestResult(wstringFromBSTR(urlBSTR));
203 if (gTestRunner->dumpVisitedLinksCallback())
204 printf("Asked to populate visited links for WebView \"%S\"\n", url.c_str());