WebKitTools/DumpRenderTree/win/WorkQueueItemWin.cpp
changeset 2 303757a437d3
parent 0 4f2f89ce4247
equal deleted inserted replaced
0:4f2f89ce4247 2:303757a437d3
     1 /*
       
     2  * Copyright (C) 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 "WorkQueueItem.h"
       
    31 
       
    32 #include "DumpRenderTree.h"
       
    33 #include <WebCore/COMPtr.h>
       
    34 #include <WebKit/WebKit.h>
       
    35 #include <WebKit/WebKitCOMAPI.h>
       
    36 #include <JavaScriptCore/JSStringRef.h>
       
    37 #include <JavaScriptCore/JSStringRefCF.h>
       
    38 #include <JavaScriptCore/RetainPtr.h>
       
    39 #include <wtf/Vector.h>
       
    40 #include <string>
       
    41 
       
    42 using std::wstring;
       
    43 
       
    44 static wstring jsStringRefToWString(JSStringRef jsStr)
       
    45 {
       
    46     size_t length = JSStringGetLength(jsStr);
       
    47     Vector<WCHAR> buffer(length + 1);
       
    48     memcpy(buffer.data(), JSStringGetCharactersPtr(jsStr), length * sizeof(WCHAR));
       
    49     buffer[length] = '\0';
       
    50 
       
    51     return buffer.data();
       
    52 }
       
    53 
       
    54 bool LoadItem::invoke() const
       
    55 {
       
    56     wstring targetString = jsStringRefToWString(m_target.get());
       
    57 
       
    58     COMPtr<IWebFrame> targetFrame;
       
    59     if (targetString.empty())
       
    60         targetFrame = frame;
       
    61     else {
       
    62         BSTR targetBSTR = SysAllocString(targetString.c_str());
       
    63         bool failed = FAILED(frame->findFrameNamed(targetBSTR, &targetFrame));
       
    64         SysFreeString(targetBSTR);
       
    65         if (failed)
       
    66             return false;
       
    67     }
       
    68 
       
    69     COMPtr<IWebURLRequest> request;
       
    70     if (FAILED(WebKitCreateInstance(CLSID_WebURLRequest, 0, IID_IWebURLRequest, (void**)&request)))
       
    71         return false;
       
    72 
       
    73     wstring urlString = jsStringRefToWString(m_url.get());
       
    74     BSTR urlBSTR = SysAllocString(urlString.c_str());
       
    75     bool failed = FAILED(request->initWithURL(urlBSTR, WebURLRequestUseProtocolCachePolicy, 60));
       
    76     SysFreeString(urlBSTR);
       
    77     if (failed)
       
    78         return false;
       
    79 
       
    80     targetFrame->loadRequest(request.get());
       
    81     return true;
       
    82 }
       
    83 
       
    84 bool LoadHTMLStringItem::invoke() const
       
    85 {
       
    86     wstring content = jsStringRefToWString(m_content.get());
       
    87     wstring baseURL = jsStringRefToWString(m_baseURL.get());
       
    88 
       
    89     BSTR contentBSTR = SysAllocString(content.c_str());
       
    90     BSTR baseURLBSTR = SysAllocString(baseURL.c_str());
       
    91 
       
    92     frame->loadHTMLString(contentBSTR, baseURLBSTR);
       
    93 
       
    94     SysFreeString(contentBSTR);
       
    95     SysFreeString(baseURLBSTR);
       
    96 
       
    97     return true;
       
    98 }
       
    99 
       
   100 bool ReloadItem::invoke() const
       
   101 {
       
   102     COMPtr<IWebView> webView;
       
   103     if (FAILED(frame->webView(&webView)))
       
   104         return false;
       
   105 
       
   106     COMPtr<IWebIBActions> webActions;
       
   107     if (FAILED(webView->QueryInterface(&webActions)))
       
   108         return false;
       
   109 
       
   110     webActions->reload(0);
       
   111     return true;
       
   112 }
       
   113 
       
   114 bool ScriptItem::invoke() const
       
   115 {
       
   116     COMPtr<IWebView> webView;
       
   117     if (FAILED(frame->webView(&webView)))
       
   118         return false;
       
   119 
       
   120     wstring scriptString = jsStringRefToWString(m_script.get());
       
   121 
       
   122     BSTR result;
       
   123     BSTR scriptBSTR = SysAllocString(scriptString.c_str());
       
   124     webView->stringByEvaluatingJavaScriptFromString(scriptBSTR, &result);
       
   125     SysFreeString(result);
       
   126     SysFreeString(scriptBSTR);
       
   127 
       
   128     return true;
       
   129 }
       
   130 
       
   131 bool BackForwardItem::invoke() const
       
   132 {
       
   133     COMPtr<IWebView> webView;
       
   134     if (FAILED(frame->webView(&webView)))
       
   135         return false;
       
   136 
       
   137     BOOL result;
       
   138     if (m_howFar == 1) {
       
   139         webView->goForward(&result);
       
   140         return true;
       
   141     }
       
   142 
       
   143     if (m_howFar == -1) {
       
   144         webView->goBack(&result);
       
   145         return true;
       
   146     }
       
   147     
       
   148     COMPtr<IWebBackForwardList> bfList;
       
   149     if (FAILED(webView->backForwardList(&bfList)))
       
   150         return false;
       
   151 
       
   152     COMPtr<IWebHistoryItem> item;
       
   153     if (FAILED(bfList->itemAtIndex(m_howFar, &item)))
       
   154         return false;
       
   155 
       
   156     webView->goToBackForwardItem(item.get(), &result);
       
   157     return true;
       
   158 }