72ccc2658cf77450de3bfd8f277717d4b7b4f57d
[WebKit-https.git] / Tools / DumpRenderTree / win / FrameLoadDelegate.cpp
1 /*
2  * Copyright (C) 2005, 2006, 2007, 2009 Apple Inc. All rights reserved.
3  * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer. 
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution. 
14  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission. 
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "config.h"
31 #include "FrameLoadDelegate.h"
32
33 #include "AccessibilityController.h"
34 #include "DumpRenderTree.h"
35 #include "EventSender.h"
36 #include "GCController.h"
37 #include "TestRunner.h"
38 #include "TextInputController.h"
39 #include "WebCoreTestSupport.h"
40 #include "WorkQueueItem.h"
41 #include "WorkQueue.h"
42 #include <WebCore/COMPtr.h>
43 #include <JavaScriptCore/JavaScriptCore.h>
44 #include <WebKit/WebKit.h>
45 #include <stdio.h>
46 #include <string>
47 #include <wtf/Assertions.h>
48 #include <wtf/PassOwnPtr.h>
49 #include <wtf/Vector.h>
50
51 using std::string;
52
53 static FrameLoadDelegate* g_delegateWaitingOnTimer;
54
55 string descriptionSuitableForTestResult(IWebFrame* webFrame)
56 {
57     COMPtr<IWebView> webView;
58     if (FAILED(webFrame->webView(&webView)))
59         return string();
60
61     COMPtr<IWebFrame> mainFrame;
62     if (FAILED(webView->mainFrame(&mainFrame)))
63         return string();
64
65     BSTR frameNameBSTR;
66     if (FAILED(webFrame->name(&frameNameBSTR)) || toUTF8(frameNameBSTR).empty())
67         return (webFrame == mainFrame) ? "main frame" : string();
68
69     string frameName = (webFrame == mainFrame) ? "main frame" : "frame";
70     frameName += " \"" + toUTF8(frameNameBSTR) + "\""; 
71
72     SysFreeString(frameNameBSTR); 
73     return frameName;
74 }
75
76 FrameLoadDelegate::FrameLoadDelegate()
77     : m_refCount(1)
78     , m_gcController(adoptPtr(new GCController))
79     , m_accessibilityController(adoptPtr(new AccessibilityController))
80     , m_textInputController(adoptPtr(new TextInputController))
81 {
82 }
83
84 FrameLoadDelegate::~FrameLoadDelegate()
85 {
86 }
87
88 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::QueryInterface(REFIID riid, void** ppvObject)
89 {
90     *ppvObject = 0;
91     if (IsEqualGUID(riid, IID_IUnknown))
92         *ppvObject = static_cast<IWebFrameLoadDelegate*>(this);
93     else if (IsEqualGUID(riid, IID_IWebFrameLoadDelegate))
94         *ppvObject = static_cast<IWebFrameLoadDelegate*>(this);
95     else if (IsEqualGUID(riid, IID_IWebFrameLoadDelegatePrivate))
96         *ppvObject = static_cast<IWebFrameLoadDelegatePrivate*>(this);
97     else if (IsEqualGUID(riid, IID_IWebFrameLoadDelegatePrivate2))
98         *ppvObject = static_cast<IWebFrameLoadDelegatePrivate2*>(this);
99     else
100         return E_NOINTERFACE;
101
102     AddRef();
103     return S_OK;
104 }
105
106 ULONG STDMETHODCALLTYPE FrameLoadDelegate::AddRef(void)
107 {
108     return ++m_refCount;
109 }
110
111 ULONG STDMETHODCALLTYPE FrameLoadDelegate::Release(void)
112 {
113     ULONG newRef = --m_refCount;
114     if (!newRef)
115         delete(this);
116
117     return newRef;
118 }
119
120
121 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didStartProvisionalLoadForFrame( 
122     /* [in] */ IWebView* webView,
123     /* [in] */ IWebFrame* frame) 
124 {
125     if (!done && gTestRunner->dumpFrameLoadCallbacks())
126         printf("%s - didStartProvisionalLoadForFrame\n", descriptionSuitableForTestResult(frame).c_str());
127
128     // Make sure we only set this once per test.  If it gets cleared, and then set again, we might
129     // end up doing two dumps for one test.
130     if (!topLoadingFrame && !done)
131         topLoadingFrame = frame;
132
133     return S_OK; 
134 }
135
136 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didReceiveServerRedirectForProvisionalLoadForFrame( 
137     /* [in] */ IWebView *webView,
138     /* [in] */ IWebFrame *frame)
139
140     if (!done && gTestRunner->dumpFrameLoadCallbacks())
141         printf("%s - didReceiveServerRedirectForProvisionalLoadForFrame\n", descriptionSuitableForTestResult(frame).c_str());
142
143     return S_OK;
144 }
145
146 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFailProvisionalLoadWithError( 
147     /* [in] */ IWebView *webView,
148     /* [in] */ IWebError *error,
149     /* [in] */ IWebFrame *frame)
150 {
151     if (!done && gTestRunner->dumpFrameLoadCallbacks())
152         printf("%s - didFailProvisionalLoadWithError\n", descriptionSuitableForTestResult(frame).c_str());
153
154     locationChangeDone(error, frame);
155     return S_OK;
156 }
157
158 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didCommitLoadForFrame( 
159     /* [in] */ IWebView *webView,
160     /* [in] */ IWebFrame *frame)
161 {
162     if (!done && gTestRunner->dumpFrameLoadCallbacks())
163         printf("%s - didCommitLoadForFrame\n", descriptionSuitableForTestResult(frame).c_str());
164
165     COMPtr<IWebViewPrivate> webViewPrivate;
166     HRESULT hr = webView->QueryInterface(&webViewPrivate);
167     if (FAILED(hr))
168         return hr;
169     webViewPrivate->updateFocusedAndActiveState();
170
171     return S_OK;
172 }
173
174 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didReceiveTitle( 
175     /* [in] */ IWebView *webView,
176     /* [in] */ BSTR title,
177     /* [in] */ IWebFrame *frame)
178 {
179     if (!done && gTestRunner->dumpFrameLoadCallbacks())
180         printf("%s - didReceiveTitle: %S\n", descriptionSuitableForTestResult(frame).c_str(), title);
181
182     if (::gTestRunner->dumpTitleChanges() && !done)
183         printf("TITLE CHANGED: '%S'\n", title ? title : L"");
184     return S_OK;
185 }
186
187 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didChangeIcons(
188     /* [in] */ IWebView* webView,
189     /* [in] */ IWebFrame* frame)
190 {
191     if (!done && gTestRunner->dumpIconChanges())
192         printf("%s - didChangeIcons\n", descriptionSuitableForTestResult(frame).c_str());
193
194     return S_OK;
195 }
196
197 void FrameLoadDelegate::processWork()
198 {
199     // if another load started, then wait for it to complete.
200     if (topLoadingFrame)
201         return;
202
203     // if we finish all the commands, we're ready to dump state
204     if (WorkQueue::shared()->processWork() && !::gTestRunner->waitToDump())
205         dump();
206 }
207
208 void FrameLoadDelegate::resetToConsistentState()
209 {
210     m_accessibilityController->resetToConsistentState();
211 }
212
213 typedef Vector<COMPtr<FrameLoadDelegate> > DelegateVector;
214 static DelegateVector& delegatesWithDelayedWork()
215 {
216     DEPRECATED_DEFINE_STATIC_LOCAL(DelegateVector, delegates, ());
217     return delegates;
218 }
219
220 static UINT_PTR processWorkTimerID;
221
222 static void CALLBACK processWorkTimer(HWND hwnd, UINT, UINT_PTR id, DWORD)
223 {
224     ASSERT_ARG(id, id == processWorkTimerID);
225     ::KillTimer(hwnd, id);
226     processWorkTimerID = 0;
227
228     DelegateVector delegates;
229     delegates.swap(delegatesWithDelayedWork());
230
231     for (size_t i = 0; i < delegates.size(); ++i)
232         delegates[i]->processWork();
233 }
234
235 void FrameLoadDelegate::locationChangeDone(IWebError*, IWebFrame* frame)
236 {
237     if (frame != topLoadingFrame)
238         return;
239
240     topLoadingFrame = 0;
241     WorkQueue::shared()->setFrozen(true);
242
243     if (::gTestRunner->waitToDump())
244         return;
245
246     if (WorkQueue::shared()->count()) {
247         if (!processWorkTimerID)
248             processWorkTimerID = ::SetTimer(0, 0, 0, processWorkTimer);
249         delegatesWithDelayedWork().append(this);
250         return;
251     }
252
253     dump();
254 }
255
256 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFinishLoadForFrame( 
257     /* [in] */ IWebView* webView,
258     /* [in] */ IWebFrame* frame)
259 {
260     if (!done && gTestRunner->dumpFrameLoadCallbacks())
261         printf("%s - didFinishLoadForFrame\n", descriptionSuitableForTestResult(frame).c_str());
262
263     locationChangeDone(0, frame);
264     return S_OK;
265 }
266
267 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFailLoadWithError( 
268     /* [in] */ IWebView* webView,
269     /* [in] */ IWebError* error,
270     /* [in] */ IWebFrame* frame)
271 {
272     if (!done && gTestRunner->dumpFrameLoadCallbacks())
273         printf("%s - didFailLoadWithError\n", descriptionSuitableForTestResult(frame).c_str());
274
275     locationChangeDone(error, frame);
276     return S_OK;
277 }
278
279 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::willPerformClientRedirectToURL( 
280     /* [in] */ IWebView *webView,
281     /* [in] */ BSTR url,  
282     /* [in] */ double delaySeconds,
283     /* [in] */ DATE fireDate,
284     /* [in] */ IWebFrame *frame)
285 {
286     if (!done && gTestRunner->dumpFrameLoadCallbacks())
287         printf("%s - willPerformClientRedirectToURL: %S \n", descriptionSuitableForTestResult(frame).c_str(),
288                 urlSuitableForTestResult(std::wstring(url, ::SysStringLen(url))).c_str());
289
290     return S_OK;
291 }
292
293 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didCancelClientRedirectForFrame( 
294     /* [in] */ IWebView *webView,
295     /* [in] */ IWebFrame *frame)
296 {
297     if (!done && gTestRunner->dumpFrameLoadCallbacks())
298         printf("%s - didCancelClientRedirectForFrame\n", descriptionSuitableForTestResult(frame).c_str());
299
300     return S_OK;
301 }
302
303
304 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::willCloseFrame( 
305     /* [in] */ IWebView *webView,
306     /* [in] */ IWebFrame *frame)
307 {
308     return E_NOTIMPL;
309 }
310
311 HRESULT FrameLoadDelegate::didClearWindowObject(IWebView*, JSContextRef, JSObjectRef, IWebFrame*)
312 {
313     return E_NOTIMPL;
314 }
315
316 HRESULT FrameLoadDelegate::didClearWindowObjectForFrameInScriptWorld(IWebView* webView, IWebFrame* frame, IWebScriptWorld* world)
317 {
318     ASSERT_ARG(webView, webView);
319     ASSERT_ARG(frame, frame);
320     ASSERT_ARG(world, world);
321     if (!webView || !frame || !world)
322         return E_POINTER;
323
324     COMPtr<IWebScriptWorld> standardWorld;
325     if (FAILED(world->standardWorld(&standardWorld)))
326         return S_OK;
327
328     if (world == standardWorld)
329         didClearWindowObjectForFrameInStandardWorld(frame);
330     else
331         didClearWindowObjectForFrameInIsolatedWorld(frame, world);
332     return S_OK;
333 }
334
335 void FrameLoadDelegate::didClearWindowObjectForFrameInIsolatedWorld(IWebFrame* frame, IWebScriptWorld* world)
336 {
337     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
338     if (!framePrivate)
339         return;
340
341     JSGlobalContextRef ctx = framePrivate->globalContextForScriptWorld(world);
342     if (!ctx)
343         return;
344
345     JSObjectRef globalObject = JSContextGetGlobalObject(ctx);
346     if (!globalObject)
347         return;
348
349     JSObjectSetProperty(ctx, globalObject, JSRetainPtr<JSStringRef>(Adopt, JSStringCreateWithUTF8CString("__worldID")).get(), JSValueMakeNumber(ctx, worldIDForWorld(world)), kJSPropertyAttributeReadOnly, 0);
350     return;
351 }
352
353 void FrameLoadDelegate::didClearWindowObjectForFrameInStandardWorld(IWebFrame* frame)
354 {
355     JSGlobalContextRef context = frame->globalContext();
356     JSObjectRef windowObject = JSContextGetGlobalObject(context);
357
358     IWebFrame* parentFrame = 0;
359     frame->parentFrame(&parentFrame);
360
361     JSValueRef exception = 0;
362
363     ::gTestRunner->makeWindowObject(context, windowObject, &exception);
364     ASSERT(!exception);
365
366     m_gcController->makeWindowObject(context, windowObject, &exception);
367     ASSERT(!exception);
368
369     m_accessibilityController->makeWindowObject(context, windowObject, &exception);
370     ASSERT(!exception);
371
372     m_textInputController->makeWindowObject(context, windowObject, &exception);
373     ASSERT(!exception);
374
375     JSStringRef eventSenderStr = JSStringCreateWithUTF8CString("eventSender");
376     JSValueRef eventSender = makeEventSender(context, !parentFrame);
377     JSObjectSetProperty(context, windowObject, eventSenderStr, eventSender, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, 0);
378     JSStringRelease(eventSenderStr);
379
380     WebCoreTestSupport::injectInternalsObject(context);
381 }
382
383 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFinishDocumentLoadForFrame( 
384     /* [in] */ IWebView *sender,
385     /* [in] */ IWebFrame *frame)
386 {
387     if (!done && gTestRunner->dumpFrameLoadCallbacks())
388         printf("%s - didFinishDocumentLoadForFrame\n",
389                 descriptionSuitableForTestResult(frame).c_str());
390     if (!done) {
391         COMPtr<IWebFramePrivate> webFramePrivate;
392         HRESULT hr = frame->QueryInterface(&webFramePrivate);
393         if (FAILED(hr))
394             return hr;
395         unsigned pendingFrameUnloadEvents;
396         hr = webFramePrivate->pendingFrameUnloadEventCount(&pendingFrameUnloadEvents);
397         if (FAILED(hr))
398             return hr;
399         if (pendingFrameUnloadEvents)
400             printf("%s - has %u onunload handler(s)\n",
401                     descriptionSuitableForTestResult(frame).c_str(), pendingFrameUnloadEvents);
402     }
403
404     return S_OK;
405 }
406
407 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didHandleOnloadEventsForFrame( 
408     /* [in] */ IWebView *sender,
409     /* [in] */ IWebFrame *frame)
410 {
411     if (!done && gTestRunner->dumpFrameLoadCallbacks())
412         printf("%s - didHandleOnloadEventsForFrame\n",
413                 descriptionSuitableForTestResult(frame).c_str());
414
415     return S_OK;
416 }
417
418 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFirstVisuallyNonEmptyLayoutInFrame( 
419     /* [in] */ IWebView *sender,
420     /* [in] */ IWebFrame *frame)
421 {
422     return S_OK;
423 }
424
425 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didDisplayInsecureContent( 
426     /* [in] */ IWebView *sender)
427 {
428     if (!done && gTestRunner->dumpFrameLoadCallbacks())
429         printf("didDisplayInsecureContent\n");
430
431     return S_OK;
432 }
433
434 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didRunInsecureContent( 
435     /* [in] */ IWebView *sender,
436     /* [in] */ IWebSecurityOrigin *origin)
437 {
438     if (!done && gTestRunner->dumpFrameLoadCallbacks())
439         printf("didRunInsecureContent\n");
440
441     return S_OK;
442 }
443