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