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