|
1 /* |
|
2 * Copyright (C) 2007 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 "DumpRenderTree.h" |
|
30 #include "WorkQueueItem.h" |
|
31 |
|
32 #include <WebCore/COMPtr.h> |
|
33 #include <WebKit/IWebFrame.h> |
|
34 #include <WebKit/IWebURLRequest.h> |
|
35 #include <WebKit/IWebView.h> |
|
36 #include <WebKit/WebKit.h> |
|
37 #include <JavaScriptCore/JSStringRef.h> |
|
38 #include <JavaScriptCore/JSStringRefCF.h> |
|
39 #include <JavaScriptCore/RetainPtr.h> |
|
40 #include <wtf/Vector.h> |
|
41 #include <string> |
|
42 |
|
43 using std::wstring; |
|
44 |
|
45 static wstring jsStringRefToWString(JSStringRef jsStr) |
|
46 { |
|
47 size_t length = JSStringGetLength(jsStr); |
|
48 Vector<WCHAR> buffer(length + 1); |
|
49 memcpy(buffer.data(), JSStringGetCharactersPtr(jsStr), length * sizeof(WCHAR)); |
|
50 buffer[length] = '\0'; |
|
51 |
|
52 return buffer.data(); |
|
53 } |
|
54 |
|
55 void LoadItem::invoke() const |
|
56 { |
|
57 wstring targetString = jsStringRefToWString(target()); |
|
58 |
|
59 COMPtr<IWebFrame> targetFrame; |
|
60 if (targetString.empty()) |
|
61 targetFrame = frame; |
|
62 else { |
|
63 BSTR targetBSTR = SysAllocString(targetString.c_str()); |
|
64 bool failed = FAILED(frame->findFrameNamed(targetBSTR, &targetFrame)); |
|
65 SysFreeString(targetBSTR); |
|
66 if (failed) |
|
67 return; |
|
68 } |
|
69 |
|
70 COMPtr<IWebURLRequest> request; |
|
71 if (FAILED(CoCreateInstance(CLSID_WebURLRequest, 0, CLSCTX_ALL, IID_IWebURLRequest, (void**)&request))) |
|
72 return; |
|
73 |
|
74 wstring urlString = jsStringRefToWString(url()); |
|
75 BSTR urlBSTR = SysAllocString(urlString.c_str()); |
|
76 bool failed = FAILED(request->initWithURL(urlBSTR, WebURLRequestUseProtocolCachePolicy, 60)); |
|
77 SysFreeString(urlBSTR); |
|
78 if (failed) |
|
79 return; |
|
80 |
|
81 targetFrame->loadRequest(request.get()); |
|
82 } |
|
83 |
|
84 void ReloadItem::invoke() const |
|
85 { |
|
86 COMPtr<IWebView> webView; |
|
87 if (FAILED(frame->webView(&webView))) |
|
88 return; |
|
89 |
|
90 COMPtr<IWebIBActions> webActions; |
|
91 if (SUCCEEDED(webView->QueryInterface(&webActions))) |
|
92 webActions->reload(0); |
|
93 } |
|
94 |
|
95 void ScriptItem::invoke() const |
|
96 { |
|
97 COMPtr<IWebView> webView; |
|
98 if (FAILED(frame->webView(&webView))) |
|
99 return; |
|
100 |
|
101 wstring scriptString = jsStringRefToWString(script()); |
|
102 |
|
103 BSTR result; |
|
104 BSTR scriptBSTR = SysAllocString(scriptString.c_str()); |
|
105 webView->stringByEvaluatingJavaScriptFromString(scriptBSTR, &result); |
|
106 SysFreeString(result); |
|
107 SysFreeString(scriptBSTR); |
|
108 } |
|
109 |
|
110 void BackForwardItem::invoke() const |
|
111 { |
|
112 COMPtr<IWebView> webView; |
|
113 if (FAILED(frame->webView(&webView))) |
|
114 return; |
|
115 |
|
116 BOOL result; |
|
117 if (m_howFar == 1) { |
|
118 webView->goForward(&result); |
|
119 return; |
|
120 } |
|
121 |
|
122 if (m_howFar == -1) { |
|
123 webView->goBack(&result); |
|
124 return; |
|
125 } |
|
126 |
|
127 COMPtr<IWebBackForwardList> bfList; |
|
128 if (FAILED(webView->backForwardList(&bfList))) |
|
129 return; |
|
130 |
|
131 COMPtr<IWebHistoryItem> item; |
|
132 if (FAILED(bfList->itemAtIndex(m_howFar, &item))) |
|
133 return; |
|
134 |
|
135 webView->goToBackForwardItem(item.get(), &result); |
|
136 } |