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 ReloadItem::invoke() const 85 { 86 COMPtr<IWebView> webView; 87 if (FAILED(frame->webView(&webView))) 88 return false; 89 90 COMPtr<IWebIBActions> webActions; 91 if (FAILED(webView->QueryInterface(&webActions))) 92 return false; 93 94 webActions->reload(0); 95 return true; 96 } 97 98 bool ScriptItem::invoke() const 99 { 100 COMPtr<IWebView> webView; 101 if (FAILED(frame->webView(&webView))) 102 return false; 103 104 wstring scriptString = jsStringRefToWString(m_script.get()); 105 106 BSTR result; 107 BSTR scriptBSTR = SysAllocString(scriptString.c_str()); 108 webView->stringByEvaluatingJavaScriptFromString(scriptBSTR, &result); 109 SysFreeString(result); 110 SysFreeString(scriptBSTR); 111 112 return true; 113 } 114 115 bool BackForwardItem::invoke() const 116 { 117 COMPtr<IWebView> webView; 118 if (FAILED(frame->webView(&webView))) 119 return false; 120 121 BOOL result; 122 if (m_howFar == 1) { 123 webView->goForward(&result); 124 return true; 125 } 126 127 if (m_howFar == -1) { 128 webView->goBack(&result); 129 return true; 130 } 131 132 COMPtr<IWebBackForwardList> bfList; 133 if (FAILED(webView->backForwardList(&bfList))) 134 return false; 135 136 COMPtr<IWebHistoryItem> item; 137 if (FAILED(bfList->itemAtIndex(m_howFar, &item))) 138 return false; 139 140 webView->goToBackForwardItem(item.get(), &result); 141 return true; 142 } 143