Fix for https://bugs.webkit.org/show_bug.cgi?id=38471
[WebKit-https.git] / WebKit2 / UIProcess / API / C / WKPage.cpp
1 /*
2  * Copyright (C) 2010 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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "WKPage.h"
27 #include "WKPagePrivate.h"
28
29 #include "WKAPICast.h"
30 #include "WebPageProxy.h"
31
32 #if __BLOCKS__
33 #include <Block.h>
34 #endif
35
36 using namespace WebKit;
37
38 WKPageNamespaceRef WKPageGetPageNamespace(WKPageRef pageRef)
39 {
40     return toRef(toWK(pageRef)->pageNamespace());
41 }
42
43 void WKPageLoadURL(WKPageRef pageRef, WKURLRef URLRef)
44 {
45     toWK(pageRef)->loadURL(toWK(URLRef));
46 }
47
48 void WKPageStopLoading(WKPageRef pageRef)
49 {
50     toWK(pageRef)->stopLoading();
51 }
52
53 void WKPageReload(WKPageRef pageRef)
54 {
55     toWK(pageRef)->reload();
56 }
57
58 bool WKPageTryClose(WKPageRef pageRef)
59 {
60     return toWK(pageRef)->tryClose();
61 }
62
63 void WKPageClose(WKPageRef pageRef)
64 {
65     toWK(pageRef)->close();
66 }
67
68 bool WKPageIsClosed(WKPageRef pageRef)
69 {
70     return toWK(pageRef)->isClosed();
71 }
72
73 void WKPageGoForward(WKPageRef pageRef)
74 {
75     toWK(pageRef)->goForward();
76 }
77
78 bool WKPageCanGoForward(WKPageRef pageRef)
79 {
80     return toWK(pageRef)->canGoForward();
81 }
82
83 void WKPageGoBack(WKPageRef pageRef)
84 {
85     toWK(pageRef)->goBack();
86 }
87
88 bool WKPageCanGoBack(WKPageRef pageRef)
89 {
90     return toWK(pageRef)->canGoBack();
91 }
92
93 WKStringRef WKPageGetTitle(WKPageRef pageRef)
94 {
95     return toRef(toWK(pageRef)->pageTitle().impl());
96 }
97
98 WKFrameRef WKPageGetMainFrame(WKPageRef pageRef)
99 {
100     return toRef(toWK(pageRef)->mainFrame());
101 }
102
103 void WKPageTerminate(WKPageRef pageRef)
104 {
105     toWK(pageRef)->terminateProcess();
106 }
107
108 void WKPageSetPageLoaderClient(WKPageRef pageRef, WKPageLoaderClient* wkClient)
109 {
110     if (wkClient && !wkClient->version)
111         toWK(pageRef)->initializeLoaderClient(wkClient);
112 }
113
114 void WKPageSetPagePolicyClient(WKPageRef pageRef, WKPagePolicyClient * wkClient)
115 {
116     if (wkClient && !wkClient->version)
117         toWK(pageRef)->initializePolicyClient(wkClient);
118 }
119
120 void WKPageSetPageUIClient(WKPageRef pageRef, WKPageUIClient * wkClient)
121 {
122     if (wkClient && !wkClient->version)
123         toWK(pageRef)->initializeUIClient(wkClient);
124 }
125
126 void WKPageSetPageHistoryClient(WKPageRef pageRef, WKPageHistoryClient * wkClient)
127 {
128     if (wkClient && !wkClient->version)
129         toWK(pageRef)->initializeHistoryClient(wkClient);
130 }
131
132 void WKPageRunJavaScriptInMainFrame(WKPageRef pageRef, WKStringRef scriptRef, void* context, WKPageRunJavaScriptFunction callback, WKPageRunJavaScriptDisposeFunction disposeFunction)
133 {
134     toWK(pageRef)->runJavaScriptInMainFrame(toWK(scriptRef), ScriptReturnValueCallback::create(context, callback, disposeFunction));
135 }
136
137 #if __BLOCKS__
138 static void callRunJavaScriptBlockAndRelease(WKStringRef resultValue, void* context)
139 {
140     WKPageRunJavaScriptBlock block = (WKPageRunJavaScriptBlock)context;
141     block(resultValue);
142     Block_release(block);
143 }
144
145 static void disposeRunJavaScriptBlock(void* context)
146 {
147     WKPageRunJavaScriptBlock block = (WKPageRunJavaScriptBlock)context;
148     Block_release(block);
149 }
150
151 void WKPageRunJavaScriptInMainFrame_b(WKPageRef pageRef, WKStringRef scriptRef, WKPageRunJavaScriptBlock block)
152 {
153     WKPageRunJavaScriptInMainFrame(pageRef, scriptRef, Block_copy(block), callRunJavaScriptBlockAndRelease, disposeRunJavaScriptBlock);
154 }
155 #endif
156
157 void WKPageRenderTreeExternalRepresentation(WKPageRef pageRef, void *context, WKPageRenderTreeExternalRepresentationFunction callback, WKPageRenderTreeExternalRepresentationDisposeFunction disposeFunction)
158 {
159     toWK(pageRef)->getRenderTreeExternalRepresentation(RenderTreeExternalRepresentationCallback::create(context, callback, disposeFunction));
160 }
161
162 #if __BLOCKS__
163 static void callRenderTreeExternalRepresentationBlockAndDispose(WKStringRef resultValue, void* context)
164 {
165     WKPageRenderTreeExternalRepresentationBlock block = (WKPageRenderTreeExternalRepresentationBlock)context;
166     block(resultValue);
167     Block_release(block);
168 }
169
170 static void disposeRenderTreeExternalRepresentationBlock(void* context)
171 {
172     WKPageRenderTreeExternalRepresentationBlock block = (WKPageRenderTreeExternalRepresentationBlock)context;
173     Block_release(block);
174 }
175
176 void WKPageRenderTreeExternalRepresentation_b(WKPageRef pageRef, WKPageRenderTreeExternalRepresentationBlock block)
177 {
178     WKPageRenderTreeExternalRepresentation(pageRef, Block_copy(block), callRenderTreeExternalRepresentationBlockAndDispose, disposeRenderTreeExternalRepresentationBlock);
179 }
180 #endif
181
182 WKPageRef WKPageRetain(WKPageRef pageRef)
183 {
184     toWK(pageRef)->ref();
185     return pageRef;
186 }
187
188 void WKPageRelease(WKPageRef pageRef)
189 {
190     toWK(pageRef)->deref();
191 }