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