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