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