WebKitTools/DumpRenderTree/win/FrameLoadDelegate.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 Computer, 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 "LayoutTestController.h"
       
    38 #include "WorkQueueItem.h"
       
    39 #include "WorkQueue.h"
       
    40 #include <WebCore/COMPtr.h>
       
    41 #include <JavaScriptCore/Assertions.h>
       
    42 #include <JavaScriptCore/JavaScriptCore.h>
       
    43 #include <WebKit/WebKit.h>
       
    44 #include <wtf/Vector.h>
       
    45 #include <stdio.h>
       
    46 #include <string>
       
    47 
       
    48 using std::string;
       
    49 
       
    50 static FrameLoadDelegate* g_delegateWaitingOnTimer;
       
    51 
       
    52 string descriptionSuitableForTestResult(IWebFrame* webFrame)
       
    53 {
       
    54     COMPtr<IWebView> webView;
       
    55     if (FAILED(webFrame->webView(&webView)))
       
    56         return string();
       
    57 
       
    58     COMPtr<IWebFrame> mainFrame;
       
    59     if (FAILED(webView->mainFrame(&mainFrame)))
       
    60         return string();
       
    61 
       
    62     BSTR frameNameBSTR;
       
    63     if (FAILED(webFrame->name(&frameNameBSTR)) || toUTF8(frameNameBSTR).empty())
       
    64         return (webFrame == mainFrame) ? "main frame" : string();
       
    65 
       
    66     string frameName = (webFrame == mainFrame) ? "main frame" : "frame";
       
    67     frameName += " \"" + toUTF8(frameNameBSTR) + "\""; 
       
    68 
       
    69     SysFreeString(frameNameBSTR); 
       
    70     return frameName;
       
    71 }
       
    72 
       
    73 FrameLoadDelegate::FrameLoadDelegate()
       
    74     : m_refCount(1)
       
    75     , m_gcController(new GCController)
       
    76     , m_accessibilityController(new AccessibilityController)
       
    77 {
       
    78 }
       
    79 
       
    80 FrameLoadDelegate::~FrameLoadDelegate()
       
    81 {
       
    82 }
       
    83 
       
    84 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::QueryInterface(REFIID riid, void** ppvObject)
       
    85 {
       
    86     *ppvObject = 0;
       
    87     if (IsEqualGUID(riid, IID_IUnknown))
       
    88         *ppvObject = static_cast<IWebFrameLoadDelegate*>(this);
       
    89     else if (IsEqualGUID(riid, IID_IWebFrameLoadDelegate))
       
    90         *ppvObject = static_cast<IWebFrameLoadDelegate*>(this);
       
    91     else if (IsEqualGUID(riid, IID_IWebFrameLoadDelegatePrivate))
       
    92         *ppvObject = static_cast<IWebFrameLoadDelegatePrivate*>(this);
       
    93     else if (IsEqualGUID(riid, IID_IWebFrameLoadDelegatePrivate2))
       
    94         *ppvObject = static_cast<IWebFrameLoadDelegatePrivate2*>(this);
       
    95     else
       
    96         return E_NOINTERFACE;
       
    97 
       
    98     AddRef();
       
    99     return S_OK;
       
   100 }
       
   101 
       
   102 ULONG STDMETHODCALLTYPE FrameLoadDelegate::AddRef(void)
       
   103 {
       
   104     return ++m_refCount;
       
   105 }
       
   106 
       
   107 ULONG STDMETHODCALLTYPE FrameLoadDelegate::Release(void)
       
   108 {
       
   109     ULONG newRef = --m_refCount;
       
   110     if (!newRef)
       
   111         delete(this);
       
   112 
       
   113     return newRef;
       
   114 }
       
   115 
       
   116 
       
   117 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didStartProvisionalLoadForFrame( 
       
   118     /* [in] */ IWebView* webView,
       
   119     /* [in] */ IWebFrame* frame) 
       
   120 {
       
   121     if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
       
   122         printf("%s - didStartProvisionalLoadForFrame\n", descriptionSuitableForTestResult(frame).c_str());
       
   123 
       
   124     // Make sure we only set this once per test.  If it gets cleared, and then set again, we might
       
   125     // end up doing two dumps for one test.
       
   126     if (!topLoadingFrame && !done)
       
   127         topLoadingFrame = frame;
       
   128 
       
   129     return S_OK; 
       
   130 }
       
   131 
       
   132 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didReceiveServerRedirectForProvisionalLoadForFrame( 
       
   133     /* [in] */ IWebView *webView,
       
   134     /* [in] */ IWebFrame *frame)
       
   135 { 
       
   136     if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
       
   137         printf("%s - didReceiveServerRedirectForProvisionalLoadForFrame\n", descriptionSuitableForTestResult(frame).c_str());
       
   138 
       
   139     return S_OK;
       
   140 }
       
   141 
       
   142 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFailProvisionalLoadWithError( 
       
   143     /* [in] */ IWebView *webView,
       
   144     /* [in] */ IWebError *error,
       
   145     /* [in] */ IWebFrame *frame)
       
   146 {
       
   147     if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
       
   148         printf("%s - didFailProvisionalLoadWithError\n", descriptionSuitableForTestResult(frame).c_str());
       
   149 
       
   150     locationChangeDone(error, frame);
       
   151     return S_OK;
       
   152 }
       
   153 
       
   154 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didCommitLoadForFrame( 
       
   155     /* [in] */ IWebView *webView,
       
   156     /* [in] */ IWebFrame *frame)
       
   157 {
       
   158     if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
       
   159         printf("%s - didCommitLoadForFrame\n", descriptionSuitableForTestResult(frame).c_str());
       
   160 
       
   161     COMPtr<IWebViewPrivate> webViewPrivate;
       
   162     HRESULT hr = webView->QueryInterface(&webViewPrivate);
       
   163     if (FAILED(hr))
       
   164         return hr;
       
   165     webViewPrivate->updateFocusedAndActiveState();
       
   166 
       
   167     return S_OK;
       
   168 }
       
   169 
       
   170 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didReceiveTitle( 
       
   171     /* [in] */ IWebView *webView,
       
   172     /* [in] */ BSTR title,
       
   173     /* [in] */ IWebFrame *frame)
       
   174 {
       
   175     if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
       
   176         printf("%s - didReceiveTitle: %S\n", descriptionSuitableForTestResult(frame).c_str(), title);
       
   177 
       
   178     if (::gLayoutTestController->dumpTitleChanges() && !done)
       
   179         printf("TITLE CHANGED: %S\n", title ? title : L"");
       
   180     return S_OK;
       
   181 }
       
   182 
       
   183 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didChangeIcons(
       
   184     /* [in] */ IWebView* webView,
       
   185     /* [in] */ IWebFrame* frame)
       
   186 {
       
   187     if (!done && gLayoutTestController->dumpIconChanges())
       
   188         printf("%s - didChangeIcons\n", descriptionSuitableForTestResult(frame).c_str());
       
   189 
       
   190     return S_OK;
       
   191 }
       
   192 
       
   193 void FrameLoadDelegate::processWork()
       
   194 {
       
   195     // if another load started, then wait for it to complete.
       
   196     if (topLoadingFrame)
       
   197         return;
       
   198 
       
   199     // if we finish all the commands, we're ready to dump state
       
   200     if (WorkQueue::shared()->processWork() && !::gLayoutTestController->waitToDump())
       
   201         dump();
       
   202 }
       
   203 
       
   204 void FrameLoadDelegate::resetToConsistentState()
       
   205 {
       
   206     m_accessibilityController->resetToConsistentState();
       
   207 }
       
   208 
       
   209 static void CALLBACK processWorkTimer(HWND, UINT, UINT_PTR id, DWORD)
       
   210 {
       
   211     ::KillTimer(0, id);
       
   212     FrameLoadDelegate* d = g_delegateWaitingOnTimer;
       
   213     g_delegateWaitingOnTimer = 0;
       
   214     d->processWork();
       
   215 }
       
   216 
       
   217 void FrameLoadDelegate::locationChangeDone(IWebError*, IWebFrame* frame)
       
   218 {
       
   219     if (frame != topLoadingFrame)
       
   220         return;
       
   221 
       
   222     topLoadingFrame = 0;
       
   223     WorkQueue::shared()->setFrozen(true);
       
   224 
       
   225     if (::gLayoutTestController->waitToDump())
       
   226         return;
       
   227 
       
   228     if (WorkQueue::shared()->count()) {
       
   229         ASSERT(!g_delegateWaitingOnTimer);
       
   230         g_delegateWaitingOnTimer = this;
       
   231         ::SetTimer(0, 0, 0, processWorkTimer);
       
   232         return;
       
   233     }
       
   234 
       
   235     dump();
       
   236 }
       
   237 
       
   238 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFinishLoadForFrame( 
       
   239     /* [in] */ IWebView* webView,
       
   240     /* [in] */ IWebFrame* frame)
       
   241 {
       
   242     if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
       
   243         printf("%s - didFinishLoadForFrame\n", descriptionSuitableForTestResult(frame).c_str());
       
   244 
       
   245     locationChangeDone(0, frame);
       
   246     return S_OK;
       
   247 }
       
   248 
       
   249 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFailLoadWithError( 
       
   250     /* [in] */ IWebView* webView,
       
   251     /* [in] */ IWebError* error,
       
   252     /* [in] */ IWebFrame* frame)
       
   253 {
       
   254     if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
       
   255         printf("%s - didFailLoadWithError\n", descriptionSuitableForTestResult(frame).c_str());
       
   256 
       
   257     locationChangeDone(error, frame);
       
   258     return S_OK;
       
   259 }
       
   260 
       
   261 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::willPerformClientRedirectToURL( 
       
   262     /* [in] */ IWebView *webView,
       
   263     /* [in] */ BSTR url,  
       
   264     /* [in] */ double delaySeconds,
       
   265     /* [in] */ DATE fireDate,
       
   266     /* [in] */ IWebFrame *frame)
       
   267 {
       
   268     if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
       
   269         printf("%s - willPerformClientRedirectToURL: %S \n", descriptionSuitableForTestResult(frame).c_str(),
       
   270                 urlSuitableForTestResult(std::wstring(url, ::SysStringLen(url))).c_str());
       
   271 
       
   272     return S_OK;
       
   273 }
       
   274 
       
   275 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didCancelClientRedirectForFrame( 
       
   276     /* [in] */ IWebView *webView,
       
   277     /* [in] */ IWebFrame *frame)
       
   278 {
       
   279     if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
       
   280         printf("%s - didCancelClientRedirectForFrame\n", descriptionSuitableForTestResult(frame).c_str());
       
   281 
       
   282     return S_OK;
       
   283 }
       
   284 
       
   285 
       
   286 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::willCloseFrame( 
       
   287     /* [in] */ IWebView *webView,
       
   288     /* [in] */ IWebFrame *frame)
       
   289 {
       
   290     return E_NOTIMPL;
       
   291 }
       
   292 
       
   293 HRESULT FrameLoadDelegate::didClearWindowObject(IWebView*, JSContextRef, JSObjectRef, IWebFrame*)
       
   294 {
       
   295     return E_NOTIMPL;
       
   296 }
       
   297 
       
   298 HRESULT FrameLoadDelegate::didClearWindowObjectForFrameInScriptWorld(IWebView* webView, IWebFrame* frame, IWebScriptWorld* world)
       
   299 {
       
   300     ASSERT_ARG(webView, webView);
       
   301     ASSERT_ARG(frame, frame);
       
   302     ASSERT_ARG(world, world);
       
   303     if (!webView || !frame || !world)
       
   304         return E_POINTER;
       
   305 
       
   306     COMPtr<IWebScriptWorld> standardWorld;
       
   307     if (FAILED(world->standardWorld(&standardWorld)))
       
   308         return S_OK;
       
   309 
       
   310     if (world == standardWorld)
       
   311         didClearWindowObjectForFrameInStandardWorld(frame);
       
   312     else
       
   313         didClearWindowObjectForFrameInIsolatedWorld(frame, world);
       
   314     return S_OK;
       
   315 }
       
   316 
       
   317 void FrameLoadDelegate::didClearWindowObjectForFrameInIsolatedWorld(IWebFrame* frame, IWebScriptWorld* world)
       
   318 {
       
   319     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
       
   320     if (!framePrivate)
       
   321         return;
       
   322 
       
   323     JSGlobalContextRef ctx = framePrivate->globalContextForScriptWorld(world);
       
   324     if (!ctx)
       
   325         return;
       
   326 
       
   327     JSObjectRef globalObject = JSContextGetGlobalObject(ctx);
       
   328     if (!globalObject)
       
   329         return;
       
   330 
       
   331     JSObjectSetProperty(ctx, globalObject, JSRetainPtr<JSStringRef>(Adopt, JSStringCreateWithUTF8CString("__worldID")).get(), JSValueMakeNumber(ctx, worldIDForWorld(world)), kJSPropertyAttributeReadOnly, 0);
       
   332     return;
       
   333 }
       
   334 
       
   335 void FrameLoadDelegate::didClearWindowObjectForFrameInStandardWorld(IWebFrame* frame)
       
   336 {
       
   337     JSGlobalContextRef context = frame->globalContext();
       
   338     JSObjectRef windowObject = JSContextGetGlobalObject(context);
       
   339 
       
   340     IWebFrame* parentFrame = 0;
       
   341     frame->parentFrame(&parentFrame);
       
   342 
       
   343     JSValueRef exception = 0;
       
   344 
       
   345     ::gLayoutTestController->makeWindowObject(context, windowObject, &exception);
       
   346     ASSERT(!exception);
       
   347 
       
   348     m_gcController->makeWindowObject(context, windowObject, &exception);
       
   349     ASSERT(!exception);
       
   350 
       
   351     m_accessibilityController->makeWindowObject(context, windowObject, &exception);
       
   352     ASSERT(!exception);
       
   353 
       
   354     JSStringRef eventSenderStr = JSStringCreateWithUTF8CString("eventSender");
       
   355     JSValueRef eventSender = makeEventSender(context, !parentFrame);
       
   356     JSObjectSetProperty(context, windowObject, eventSenderStr, eventSender, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, 0);
       
   357     JSStringRelease(eventSenderStr);
       
   358 }
       
   359 
       
   360 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFinishDocumentLoadForFrame( 
       
   361     /* [in] */ IWebView *sender,
       
   362     /* [in] */ IWebFrame *frame)
       
   363 {
       
   364     if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
       
   365         printf("%s - didFinishDocumentLoadForFrame\n",
       
   366                 descriptionSuitableForTestResult(frame).c_str());
       
   367     if (!done) {
       
   368         COMPtr<IWebFramePrivate> webFramePrivate;
       
   369         HRESULT hr = frame->QueryInterface(&webFramePrivate);
       
   370         if (FAILED(hr))
       
   371             return hr;
       
   372         unsigned pendingFrameUnloadEvents;
       
   373         hr = webFramePrivate->pendingFrameUnloadEventCount(&pendingFrameUnloadEvents);
       
   374         if (FAILED(hr))
       
   375             return hr;
       
   376         if (pendingFrameUnloadEvents)
       
   377             printf("%s - has %u onunload handler(s)\n",
       
   378                     descriptionSuitableForTestResult(frame).c_str(), pendingFrameUnloadEvents);
       
   379     }
       
   380 
       
   381     return S_OK;
       
   382 }
       
   383 
       
   384 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didHandleOnloadEventsForFrame( 
       
   385     /* [in] */ IWebView *sender,
       
   386     /* [in] */ IWebFrame *frame)
       
   387 {
       
   388     if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
       
   389         printf("%s - didHandleOnloadEventsForFrame\n",
       
   390                 descriptionSuitableForTestResult(frame).c_str());
       
   391 
       
   392     return S_OK;
       
   393 }
       
   394 
       
   395 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFirstVisuallyNonEmptyLayoutInFrame( 
       
   396     /* [in] */ IWebView *sender,
       
   397     /* [in] */ IWebFrame *frame)
       
   398 {
       
   399     return S_OK;
       
   400 }
       
   401 
       
   402 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didDisplayInsecureContent( 
       
   403     /* [in] */ IWebView *sender)
       
   404 {
       
   405     if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
       
   406         printf("didDisplayInsecureContent\n");
       
   407 
       
   408     return S_OK;
       
   409 }
       
   410 
       
   411 HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didRunInsecureContent( 
       
   412     /* [in] */ IWebView *sender,
       
   413     /* [in] */ IWebSecurityOrigin *origin)
       
   414 {
       
   415     if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
       
   416         printf("didRunInsecureContent\n");
       
   417 
       
   418     return S_OK;
       
   419 }
       
   420