06ddc3b5d9b1feae01e89230429a21ccc5ca958a
[WebKit-https.git] / Source / WebKit2 / WebProcess / InjectedBundle / API / c / WKBundleFrame.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 "config.h"
27 #include "WKBundleFrame.h"
28 #include "WKBundleFramePrivate.h"
29
30 #include "InjectedBundleHitTestResult.h"
31 #include "WKAPICast.h"
32 #include "WKBundleAPICast.h"
33 #include "WKData.h"
34 #include "WebFrame.h"
35 #include "WebSecurityOrigin.h"
36 #include <WebCore/Frame.h>
37 #include <WebCore/FrameView.h>
38
39 using namespace WebCore;
40 using namespace WebKit;
41
42 WKTypeID WKBundleFrameGetTypeID()
43 {
44     return toAPI(WebFrame::APIType);
45 }
46
47 bool WKBundleFrameIsMainFrame(WKBundleFrameRef frameRef)
48 {
49     return toImpl(frameRef)->isMainFrame();
50 }
51
52 WKBundleFrameRef WKBundleFrameGetParentFrame(WKBundleFrameRef frameRef)
53 {
54     return toAPI(toImpl(frameRef)->parentFrame());
55 }
56
57 WKURLRef WKBundleFrameCopyURL(WKBundleFrameRef frameRef)
58 {
59     return toCopiedURLAPI(toImpl(frameRef)->url());
60 }
61
62 WKURLRef WKBundleFrameCopyProvisionalURL(WKBundleFrameRef frameRef)
63 {
64     return toCopiedURLAPI(toImpl(frameRef)->provisionalURL());
65 }
66
67 WKFrameLoadState WKBundleFrameGetFrameLoadState(WKBundleFrameRef frameRef)
68 {
69     Frame* coreFrame = toImpl(frameRef)->coreFrame();
70     if (!coreFrame)
71         return kWKFrameLoadStateFinished;
72
73     FrameLoader* loader = coreFrame->loader();
74     if (!loader)
75         return kWKFrameLoadStateFinished;
76
77     switch (loader->state()) {
78     case FrameStateProvisional:
79         return kWKFrameLoadStateProvisional;
80     case FrameStateCommittedPage:
81         return kWKFrameLoadStateCommitted;
82     case FrameStateComplete:
83         return kWKFrameLoadStateFinished;
84     }
85
86     ASSERT_NOT_REACHED();
87     return kWKFrameLoadStateFinished;
88 }
89
90 WKArrayRef WKBundleFrameCopyChildFrames(WKBundleFrameRef frameRef)
91 {
92     return toAPI(toImpl(frameRef)->childFrames().leakRef());    
93 }
94
95 JSGlobalContextRef WKBundleFrameGetJavaScriptContext(WKBundleFrameRef frameRef)
96 {
97     return toImpl(frameRef)->jsContext();
98 }
99
100 WKBundleFrameRef WKBundleFrameForJavaScriptContext(JSContextRef context)
101 {
102     return toAPI(WebFrame::frameForContext(context));
103 }
104
105 JSGlobalContextRef WKBundleFrameGetJavaScriptContextForWorld(WKBundleFrameRef frameRef, WKBundleScriptWorldRef worldRef)
106 {
107     return toImpl(frameRef)->jsContextForWorld(toImpl(worldRef));
108 }
109
110 JSValueRef WKBundleFrameGetJavaScriptWrapperForNodeForWorld(WKBundleFrameRef frameRef, WKBundleNodeHandleRef nodeHandleRef, WKBundleScriptWorldRef worldRef)
111 {
112     return toImpl(frameRef)->jsWrapperForWorld(toImpl(nodeHandleRef), toImpl(worldRef));
113 }
114
115 JSValueRef WKBundleFrameGetJavaScriptWrapperForRangeForWorld(WKBundleFrameRef frameRef, WKBundleRangeHandleRef rangeHandleRef, WKBundleScriptWorldRef worldRef)
116 {
117     return toImpl(frameRef)->jsWrapperForWorld(toImpl(rangeHandleRef), toImpl(worldRef));
118 }
119
120 WKStringRef WKBundleFrameCopyName(WKBundleFrameRef frameRef)
121 {
122     return toCopiedAPI(toImpl(frameRef)->name());
123 }
124
125 JSValueRef WKBundleFrameGetComputedStyleIncludingVisitedInfo(WKBundleFrameRef frameRef, JSObjectRef element)
126 {
127     return toImpl(frameRef)->computedStyleIncludingVisitedInfo(element);
128 }
129
130 WKStringRef WKBundleFrameCopyCounterValue(WKBundleFrameRef frameRef, JSObjectRef element)
131 {
132     return toCopiedAPI(toImpl(frameRef)->counterValue(element));
133 }
134
135 WKStringRef WKBundleFrameCopyMarkerText(WKBundleFrameRef frameRef, JSObjectRef element)
136 {
137     return toCopiedAPI(toImpl(frameRef)->markerText(element));
138 }
139
140 WKStringRef WKBundleFrameCopyInnerText(WKBundleFrameRef frameRef)
141 {
142     return toCopiedAPI(toImpl(frameRef)->innerText());
143 }
144
145 unsigned WKBundleFrameGetPendingUnloadCount(WKBundleFrameRef frameRef)
146 {
147     return toImpl(frameRef)->pendingUnloadCount();
148 }
149
150 WKBundlePageRef WKBundleFrameGetPage(WKBundleFrameRef frameRef)
151 {
152     return toAPI(toImpl(frameRef)->page());
153 }
154
155 void WKBundleFrameClearOpener(WKBundleFrameRef frameRef)
156 {
157     Frame* coreFrame = toImpl(frameRef)->coreFrame();
158     if (coreFrame)
159         coreFrame->loader()->setOpener(0);
160 }
161
162 void WKBundleFrameStopLoading(WKBundleFrameRef frameRef)
163 {
164     toImpl(frameRef)->stopLoading();
165 }
166
167 WKStringRef WKBundleFrameCopyLayerTreeAsText(WKBundleFrameRef frameRef)
168 {
169     return toCopiedAPI(toImpl(frameRef)->layerTreeAsText());
170 }
171
172 bool WKBundleFrameAllowsFollowingLink(WKBundleFrameRef frameRef, WKURLRef urlRef)
173 {
174     return toImpl(frameRef)->allowsFollowingLink(WebCore::KURL(WebCore::KURL(), toWTFString(urlRef)));
175 }
176
177 bool WKBundleFrameHandlesPageScaleGesture(WKBundleFrameRef frameRef)
178 {
179     return toAPI(toImpl(frameRef)->handlesPageScaleGesture());
180 }
181
182 WKRect WKBundleFrameGetContentBounds(WKBundleFrameRef frameRef)
183 {
184     return toAPI(toImpl(frameRef)->contentBounds());
185 }
186
187 WKRect WKBundleFrameGetVisibleContentBounds(WKBundleFrameRef frameRef)
188 {
189     return toAPI(toImpl(frameRef)->visibleContentBounds());
190 }
191
192 WKRect WKBundleFrameGetVisibleContentBoundsExcludingScrollbars(WKBundleFrameRef frameRef)
193 {
194     return toAPI(toImpl(frameRef)->visibleContentBoundsExcludingScrollbars());
195 }
196
197 WKSize WKBundleFrameGetScrollOffset(WKBundleFrameRef frameRef)
198 {
199     return toAPI(toImpl(frameRef)->scrollOffset());
200 }
201
202 bool WKBundleFrameHasHorizontalScrollbar(WKBundleFrameRef frameRef)
203 {
204     return toImpl(frameRef)->hasHorizontalScrollbar();
205 }
206
207 bool WKBundleFrameHasVerticalScrollbar(WKBundleFrameRef frameRef)
208 {
209     return toImpl(frameRef)->hasVerticalScrollbar();
210 }
211
212 bool WKBundleFrameGetDocumentBackgroundColor(WKBundleFrameRef frameRef, double* red, double* green, double* blue, double* alpha)
213 {
214     return toImpl(frameRef)->getDocumentBackgroundColor(red, green, blue, alpha);
215 }
216
217 WKStringRef WKBundleFrameCopySuggestedFilenameForResourceWithURL(WKBundleFrameRef frameRef, WKURLRef urlRef)
218 {
219     return toCopiedAPI(toImpl(frameRef)->suggestedFilenameForResourceWithURL(WebCore::KURL(WebCore::KURL(), toWTFString(urlRef))));
220 }
221
222 WKStringRef WKBundleFrameCopyMIMETypeForResourceWithURL(WKBundleFrameRef frameRef, WKURLRef urlRef)
223 {
224     return toCopiedAPI(toImpl(frameRef)->mimeTypeForResourceWithURL(WebCore::KURL(WebCore::KURL(), toWTFString(urlRef))));
225 }
226
227 bool WKBundleFrameContainsAnyFormElements(WKBundleFrameRef frameRef)
228 {
229     return toImpl(frameRef)->containsAnyFormElements();
230 }
231
232 void WKBundleFrameSetTextDirection(WKBundleFrameRef frameRef, WKStringRef directionRef)
233 {
234     toImpl(frameRef)->setTextDirection(toWTFString(directionRef));
235 }
236
237 WKDataRef WKBundleFrameCopyWebArchive(WKBundleFrameRef frameRef)
238 {
239     return WKBundleFrameCopyWebArchiveFilteringSubframes(frameRef, 0, 0);
240 }
241
242 WKDataRef WKBundleFrameCopyWebArchiveFilteringSubframes(WKBundleFrameRef frameRef, WKBundleFrameFrameFilterCallback frameFilterCallback, void* context)
243 {
244 #if PLATFORM(MAC)
245     RetainPtr<CFDataRef> data = toImpl(frameRef)->webArchiveData(frameFilterCallback, context);
246     if (data)
247         return WKDataCreate(CFDataGetBytePtr(data.get()), CFDataGetLength(data.get()));
248 #else
249     UNUSED_PARAM(frameRef);
250     UNUSED_PARAM(frameFilterCallback);
251     UNUSED_PARAM(context);
252 #endif
253     
254     return 0;
255 }
256
257 bool WKBundleFrameCallShouldCloseOnWebView(WKBundleFrameRef frameRef)
258 {
259     Frame* coreFrame = toImpl(frameRef)->coreFrame();
260     if (!coreFrame)
261         return true;
262
263     return coreFrame->loader()->shouldClose();
264 }
265
266 WKBundleHitTestResultRef WKBundleFrameCreateHitTestResult(WKBundleFrameRef frameRef, WKPoint point)
267 {
268     return toAPI(toImpl(frameRef)->hitTest(toIntPoint(point)).leakRef());
269 }
270
271 WKSecurityOriginRef WKBundleFrameCopySecurityOrigin(WKBundleFrameRef frameRef)
272 {
273     Frame* coreFrame = toImpl(frameRef)->coreFrame();
274     if (!coreFrame)
275         return 0;
276
277     return toCopiedAPI(coreFrame->document()->securityOrigin());
278 }