1 /*
2 * Copyright (C) 2005, 2006, 2007, 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 *
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
29 #include "config.h"
30 #include "FrameLoadDelegate.h"
31
32 #include "AccessibilityController.h"
33 #include "DumpRenderTree.h"
34 #include "EventSender.h"
35 #include "GCController.h"
36 #include "LayoutTestController.h"
37 #include "WorkQueueItem.h"
38 #include "WorkQueue.h"
39 #include <WebCore/COMPtr.h>
40 #include <JavaScriptCore/Assertions.h>
41 #include <JavaScriptCore/JavaScriptCore.h>
42 #include <WebKit/WebKit.h>
43 #include <wtf/Vector.h>
44 #include <stdio.h>
45 #include <string>
46
47 using std::string;
48
49 static FrameLoadDelegate* g_delegateWaitingOnTimer;
50
BSTRtoString(BSTR bstr)51 string BSTRtoString(BSTR bstr)
52 {
53 int result = WideCharToMultiByte(CP_UTF8, 0, bstr, SysStringLen(bstr) + 1, 0, 0, 0, 0);
54 Vector<char> utf8Vector(result);
55 result = WideCharToMultiByte(CP_UTF8, 0, bstr, SysStringLen(bstr) + 1, utf8Vector.data(), result, 0, 0);
56 if (!result)
57 return string();
58
59 return string(utf8Vector.data(), utf8Vector.size() - 1);
60 }
61
descriptionSuitableForTestResult(IWebFrame * webFrame)62 string descriptionSuitableForTestResult(IWebFrame* webFrame)
63 {
64 COMPtr<IWebView> webView;
65 if (FAILED(webFrame->webView(&webView)))
66 return string();
67
68 COMPtr<IWebFrame> mainFrame;
69 if (FAILED(webView->mainFrame(&mainFrame)))
70 return string();
71
72 BSTR frameNameBSTR;
73 if (FAILED(webFrame->name(&frameNameBSTR)) || BSTRtoString(frameNameBSTR).empty() )
74 return (webFrame == mainFrame) ? "main frame" : string();
75
76 string frameName = (webFrame == mainFrame) ? "main frame" : "frame";
77 frameName += " \"" + BSTRtoString(frameNameBSTR) + "\"";
78
79 SysFreeString(frameNameBSTR);
80
81 return frameName;
82 }
83
FrameLoadDelegate()84 FrameLoadDelegate::FrameLoadDelegate()
85 : m_refCount(1)
86 , m_gcController(new GCController)
87 , m_accessibilityController(new AccessibilityController)
88 {
89 }
90
~FrameLoadDelegate()91 FrameLoadDelegate::~FrameLoadDelegate()
92 {
93 }
94
QueryInterface(REFIID riid,void ** ppvObject)95 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::QueryInterface(REFIID riid, void** ppvObject)
96 {
97 *ppvObject = 0;
98 if (IsEqualGUID(riid, IID_IUnknown))
99 *ppvObject = static_cast<IWebFrameLoadDelegate*>(this);
100 else if (IsEqualGUID(riid, IID_IWebFrameLoadDelegate))
101 *ppvObject = static_cast<IWebFrameLoadDelegate*>(this);
102 else if (IsEqualGUID(riid, IID_IWebFrameLoadDelegatePrivate))
103 *ppvObject = static_cast<IWebFrameLoadDelegatePrivate*>(this);
104 else
105 return E_NOINTERFACE;
106
107 AddRef();
108 return S_OK;
109 }
110
AddRef(void)111 ULONG STDMETHODCALLTYPE FrameLoadDelegate::AddRef(void)
112 {
113 return ++m_refCount;
114 }
115
Release(void)116 ULONG STDMETHODCALLTYPE FrameLoadDelegate::Release(void)
117 {
118 ULONG newRef = --m_refCount;
119 if (!newRef)
120 delete(this);
121
122 return newRef;
123 }
124
125
didStartProvisionalLoadForFrame(IWebView * webView,IWebFrame * frame)126 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didStartProvisionalLoadForFrame(
127 /* [in] */ IWebView* webView,
128 /* [in] */ IWebFrame* frame)
129 {
130 if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
131 printf("%s - didStartProvisionalLoadForFrame\n", descriptionSuitableForTestResult(frame).c_str());
132
133 // Make sure we only set this once per test. If it gets cleared, and then set again, we might
134 // end up doing two dumps for one test.
135 if (!topLoadingFrame && !done)
136 topLoadingFrame = frame;
137
138 return S_OK;
139 }
140
didReceiveServerRedirectForProvisionalLoadForFrame(IWebView * webView,IWebFrame * frame)141 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didReceiveServerRedirectForProvisionalLoadForFrame(
142 /* [in] */ IWebView *webView,
143 /* [in] */ IWebFrame *frame)
144 {
145 if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
146 printf("%s - didReceiveServerRedirectForProvisionalLoadForFrame\n", descriptionSuitableForTestResult(frame).c_str());
147
148 return S_OK;
149 }
150
didFailProvisionalLoadWithError(IWebView * webView,IWebError * error,IWebFrame * frame)151 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFailProvisionalLoadWithError(
152 /* [in] */ IWebView *webView,
153 /* [in] */ IWebError *error,
154 /* [in] */ IWebFrame *frame)
155 {
156 if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
157 printf("%s - didFailProvisionalLoadWithError\n", descriptionSuitableForTestResult(frame).c_str());
158
159 return S_OK;
160 }
161
didCommitLoadForFrame(IWebView * webView,IWebFrame * frame)162 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didCommitLoadForFrame(
163 /* [in] */ IWebView *webView,
164 /* [in] */ IWebFrame *frame)
165 {
166 if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
167 printf("%s - didCommitLoadForFrame\n", descriptionSuitableForTestResult(frame).c_str());
168
169 COMPtr<IWebViewPrivate> webViewPrivate;
170 HRESULT hr = webView->QueryInterface(&webViewPrivate);
171 if (FAILED(hr))
172 return hr;
173 webViewPrivate->updateFocusedAndActiveState();
174
175 return S_OK;
176 }
177
didReceiveTitle(IWebView * webView,BSTR title,IWebFrame * frame)178 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didReceiveTitle(
179 /* [in] */ IWebView *webView,
180 /* [in] */ BSTR title,
181 /* [in] */ IWebFrame *frame)
182 {
183 if (::gLayoutTestController->dumpTitleChanges() && !done)
184 printf("TITLE CHANGED: %S\n", title ? title : L"");
185 return S_OK;
186 }
187
processWork()188 void FrameLoadDelegate::processWork()
189 {
190 // if another load started, then wait for it to complete.
191 if (topLoadingFrame)
192 return;
193
194 // if we finish all the commands, we're ready to dump state
195 if (WorkQueue::shared()->processWork() && !::gLayoutTestController->waitToDump())
196 dump();
197 }
198
processWorkTimer(HWND,UINT,UINT_PTR id,DWORD)199 static void CALLBACK processWorkTimer(HWND, UINT, UINT_PTR id, DWORD)
200 {
201 ::KillTimer(0, id);
202 FrameLoadDelegate* d = g_delegateWaitingOnTimer;
203 g_delegateWaitingOnTimer = 0;
204 d->processWork();
205 }
206
locationChangeDone(IWebError *,IWebFrame * frame)207 void FrameLoadDelegate::locationChangeDone(IWebError*, IWebFrame* frame)
208 {
209 if (frame != topLoadingFrame)
210 return;
211
212 topLoadingFrame = 0;
213 WorkQueue::shared()->setFrozen(true);
214
215 if (::gLayoutTestController->waitToDump())
216 return;
217
218 if (WorkQueue::shared()->count()) {
219 ASSERT(!g_delegateWaitingOnTimer);
220 g_delegateWaitingOnTimer = this;
221 ::SetTimer(0, 0, 0, processWorkTimer);
222 return;
223 }
224
225 dump();
226 }
227
didFinishLoadForFrame(IWebView * webView,IWebFrame * frame)228 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFinishLoadForFrame(
229 /* [in] */ IWebView* webView,
230 /* [in] */ IWebFrame* frame)
231 {
232 if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
233 printf("%s - didFinishLoadForFrame\n", descriptionSuitableForTestResult(frame).c_str());
234
235 locationChangeDone(0, frame);
236 return S_OK;
237 }
238
didFailLoadWithError(IWebView * webView,IWebError * error,IWebFrame * frame)239 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFailLoadWithError(
240 /* [in] */ IWebView* webView,
241 /* [in] */ IWebError* error,
242 /* [in] */ IWebFrame* frame)
243 {
244 if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
245 printf("%s - didFailLoadWithError\n", descriptionSuitableForTestResult(frame).c_str());
246
247 locationChangeDone(error, frame);
248 return S_OK;
249 }
250
willPerformClientRedirectToURL(IWebView * webView,BSTR url,double delaySeconds,DATE fireDate,IWebFrame * frame)251 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::willPerformClientRedirectToURL(
252 /* [in] */ IWebView *webView,
253 /* [in] */ BSTR url,
254 /* [in] */ double delaySeconds,
255 /* [in] */ DATE fireDate,
256 /* [in] */ IWebFrame *frame)
257 {
258 if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
259 printf("%s - willPerformClientRedirectToURL: %S \n", descriptionSuitableForTestResult(frame).c_str(),
260 urlSuitableForTestResult(std::wstring(url, ::SysStringLen(url))).c_str());
261
262 return S_OK;
263 }
264
didCancelClientRedirectForFrame(IWebView * webView,IWebFrame * frame)265 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didCancelClientRedirectForFrame(
266 /* [in] */ IWebView *webView,
267 /* [in] */ IWebFrame *frame)
268 {
269 if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
270 printf("%s - didCancelClientRedirectForFrame\n", descriptionSuitableForTestResult(frame).c_str());
271
272 return S_OK;
273 }
274
275
willCloseFrame(IWebView * webView,IWebFrame * frame)276 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::willCloseFrame(
277 /* [in] */ IWebView *webView,
278 /* [in] */ IWebFrame *frame)
279 {
280 return E_NOTIMPL;
281 }
282
didClearWindowObject(IWebView * webView,JSContextRef context,JSObjectRef windowObject,IWebFrame * frame)283 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didClearWindowObject(
284 /* [in] */ IWebView*webView,
285 /* [in] */ JSContextRef context,
286 /* [in] */ JSObjectRef windowObject,
287 /* [in] */ IWebFrame* frame)
288 {
289 JSValueRef exception = 0;
290
291 ::gLayoutTestController->makeWindowObject(context, windowObject, &exception);
292 ASSERT(!exception);
293
294 m_gcController->makeWindowObject(context, windowObject, &exception);
295 ASSERT(!exception);
296
297 m_accessibilityController->makeWindowObject(context, windowObject, &exception);
298 ASSERT(!exception);
299
300 JSStringRef eventSenderStr = JSStringCreateWithUTF8CString("eventSender");
301 JSValueRef eventSender = makeEventSender(context);
302 JSObjectSetProperty(context, windowObject, eventSenderStr, eventSender, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, 0);
303 JSStringRelease(eventSenderStr);
304
305 return S_OK;
306 }
307
didFinishDocumentLoadForFrame(IWebView * sender,IWebFrame * frame)308 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFinishDocumentLoadForFrame(
309 /* [in] */ IWebView *sender,
310 /* [in] */ IWebFrame *frame)
311 {
312 if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
313 printf("%s - didFinishDocumentLoadForFrame\n",
314 descriptionSuitableForTestResult(frame).c_str());
315 if (!done) {
316 COMPtr<IWebFramePrivate> webFramePrivate;
317 HRESULT hr = frame->QueryInterface(&webFramePrivate);
318 if (FAILED(hr))
319 return hr;
320 unsigned pendingFrameUnloadEvents;
321 hr = webFramePrivate->pendingFrameUnloadEventCount(&pendingFrameUnloadEvents);
322 if (FAILED(hr))
323 return hr;
324 if (pendingFrameUnloadEvents)
325 printf("%s - has %u onunload handler(s)\n",
326 descriptionSuitableForTestResult(frame).c_str(), pendingFrameUnloadEvents);
327 }
328
329 return S_OK;
330 }
331
didHandleOnloadEventsForFrame(IWebView * sender,IWebFrame * frame)332 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didHandleOnloadEventsForFrame(
333 /* [in] */ IWebView *sender,
334 /* [in] */ IWebFrame *frame)
335 {
336 if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
337 printf("%s - didHandleOnloadEventsForFrame\n",
338 descriptionSuitableForTestResult(frame).c_str());
339
340 return S_OK;
341 }
342
didFirstVisuallyNonEmptyLayoutInFrame(IWebView * sender,IWebFrame * frame)343 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFirstVisuallyNonEmptyLayoutInFrame(
344 /* [in] */ IWebView *sender,
345 /* [in] */ IWebFrame *frame)
346 {
347 return S_OK;
348 }
349