WKPage.cpp revision 81bc750723a18f21cd17d1b173cd2a4dda9cea6e
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 "WKPage.h"
28#include "WKPagePrivate.h"
29
30#include "PrintInfo.h"
31#include "WKAPICast.h"
32#include "WebBackForwardList.h"
33#include "WebData.h"
34#include "WebPageProxy.h"
35
36#ifdef __BLOCKS__
37#include <Block.h>
38#endif
39
40using namespace WebKit;
41
42WKTypeID WKPageGetTypeID()
43{
44    return toAPI(WebPageProxy::APIType);
45}
46
47WKContextRef WKPageGetContext(WKPageRef pageRef)
48{
49    return toAPI(toImpl(pageRef)->context());
50}
51
52WKPageGroupRef WKPageGetPageGroup(WKPageRef pageRef)
53{
54    return toAPI(toImpl(pageRef)->pageGroup());
55}
56
57void WKPageLoadURL(WKPageRef pageRef, WKURLRef URLRef)
58{
59    toImpl(pageRef)->loadURL(toImpl(URLRef)->string());
60}
61
62void WKPageLoadURLRequest(WKPageRef pageRef, WKURLRequestRef urlRequestRef)
63{
64    toImpl(pageRef)->loadURLRequest(toImpl(urlRequestRef));
65}
66
67void WKPageLoadHTMLString(WKPageRef pageRef, WKStringRef htmlStringRef, WKURLRef baseURLRef)
68{
69    toImpl(pageRef)->loadHTMLString(toWTFString(htmlStringRef), toWTFString(baseURLRef));
70}
71
72void WKPageLoadAlternateHTMLString(WKPageRef pageRef, WKStringRef htmlStringRef, WKURLRef baseURLRef, WKURLRef unreachableURLRef)
73{
74    toImpl(pageRef)->loadAlternateHTMLString(toWTFString(htmlStringRef), toWTFString(baseURLRef), toWTFString(unreachableURLRef));
75}
76
77void WKPageLoadPlainTextString(WKPageRef pageRef, WKStringRef plainTextStringRef)
78{
79    toImpl(pageRef)->loadPlainTextString(toWTFString(plainTextStringRef));
80}
81
82void WKPageStopLoading(WKPageRef pageRef)
83{
84    toImpl(pageRef)->stopLoading();
85}
86
87void WKPageReload(WKPageRef pageRef)
88{
89    toImpl(pageRef)->reload(false);
90}
91
92void WKPageReloadFromOrigin(WKPageRef pageRef)
93{
94    toImpl(pageRef)->reload(true);
95}
96
97bool WKPageTryClose(WKPageRef pageRef)
98{
99    return toImpl(pageRef)->tryClose();
100}
101
102void WKPageClose(WKPageRef pageRef)
103{
104    toImpl(pageRef)->close();
105}
106
107bool WKPageIsClosed(WKPageRef pageRef)
108{
109    return toImpl(pageRef)->isClosed();
110}
111
112void WKPageGoForward(WKPageRef pageRef)
113{
114    toImpl(pageRef)->goForward();
115}
116
117bool WKPageCanGoForward(WKPageRef pageRef)
118{
119    return toImpl(pageRef)->canGoForward();
120}
121
122void WKPageGoBack(WKPageRef pageRef)
123{
124    toImpl(pageRef)->goBack();
125}
126
127bool WKPageCanGoBack(WKPageRef pageRef)
128{
129    return toImpl(pageRef)->canGoBack();
130}
131
132void WKPageGoToBackForwardListItem(WKPageRef pageRef, WKBackForwardListItemRef itemRef)
133{
134    toImpl(pageRef)->goToBackForwardItem(toImpl(itemRef));
135}
136
137WKBackForwardListRef WKPageGetBackForwardList(WKPageRef pageRef)
138{
139    return toAPI(toImpl(pageRef)->backForwardList());
140}
141
142WKStringRef WKPageCopyTitle(WKPageRef pageRef)
143{
144    return toCopiedAPI(toImpl(pageRef)->pageTitle());
145}
146
147WKFrameRef WKPageGetMainFrame(WKPageRef pageRef)
148{
149    return toAPI(toImpl(pageRef)->mainFrame());
150}
151
152WKFrameRef WKPageGetFocusedFrame(WKPageRef pageRef)
153{
154    return toAPI(toImpl(pageRef)->focusedFrame());
155}
156
157WKFrameRef WKPageGetFrameSetLargestFrame(WKPageRef pageRef)
158{
159    return toAPI(toImpl(pageRef)->frameSetLargestFrame());
160}
161
162#if defined(ENABLE_INSPECTOR) && ENABLE_INSPECTOR
163WKInspectorRef WKPageGetInspector(WKPageRef pageRef)
164{
165    return toAPI(toImpl(pageRef)->inspector());
166}
167#endif
168
169double WKPageGetEstimatedProgress(WKPageRef pageRef)
170{
171    return toImpl(pageRef)->estimatedProgress();
172}
173
174void WKPageSetMemoryCacheClientCallsEnabled(WKPageRef pageRef, bool memoryCacheClientCallsEnabled)
175{
176    toImpl(pageRef)->setMemoryCacheClientCallsEnabled(memoryCacheClientCallsEnabled);
177}
178
179WKStringRef WKPageCopyUserAgent(WKPageRef pageRef)
180{
181    return toCopiedAPI(toImpl(pageRef)->userAgent());
182}
183
184WKStringRef WKPageCopyApplicationNameForUserAgent(WKPageRef pageRef)
185{
186    return toCopiedAPI(toImpl(pageRef)->applicationNameForUserAgent());
187}
188
189void WKPageSetApplicationNameForUserAgent(WKPageRef pageRef, WKStringRef applicationNameRef)
190{
191    toImpl(pageRef)->setApplicationNameForUserAgent(toWTFString(applicationNameRef));
192}
193
194WKStringRef WKPageCopyCustomUserAgent(WKPageRef pageRef)
195{
196    return toCopiedAPI(toImpl(pageRef)->customUserAgent());
197}
198
199void WKPageSetCustomUserAgent(WKPageRef pageRef, WKStringRef userAgentRef)
200{
201    toImpl(pageRef)->setCustomUserAgent(toWTFString(userAgentRef));
202}
203
204bool WKPageSupportsTextEncoding(WKPageRef pageRef)
205{
206    return toImpl(pageRef)->supportsTextEncoding();
207}
208
209WKStringRef WKPageCopyCustomTextEncodingName(WKPageRef pageRef)
210{
211    return toCopiedAPI(toImpl(pageRef)->customTextEncodingName());
212}
213
214void WKPageSetCustomTextEncodingName(WKPageRef pageRef, WKStringRef encodingNameRef)
215{
216    toImpl(pageRef)->setCustomTextEncodingName(toWTFString(encodingNameRef));
217}
218
219void WKPageTerminate(WKPageRef pageRef)
220{
221    toImpl(pageRef)->terminateProcess();
222}
223
224WKStringRef WKPageGetSessionHistoryURLValueType()
225{
226    static WebString* sessionHistoryURLValueType = WebString::create("SessionHistoryURL").releaseRef();
227    return toAPI(sessionHistoryURLValueType);
228}
229
230WKDataRef WKPageCopySessionState(WKPageRef pageRef, void *context, WKPageSessionStateFilterCallback filter)
231{
232    return toAPI(toImpl(pageRef)->sessionStateData(filter, context).releaseRef());
233}
234
235void WKPageRestoreFromSessionState(WKPageRef pageRef, WKDataRef sessionStateData)
236{
237    toImpl(pageRef)->restoreFromSessionStateData(toImpl(sessionStateData));
238}
239
240double WKPageGetTextZoomFactor(WKPageRef pageRef)
241{
242    return toImpl(pageRef)->textZoomFactor();
243}
244
245bool WKPageSupportsTextZoom(WKPageRef pageRef)
246{
247    return toImpl(pageRef)->supportsTextZoom();
248}
249
250void WKPageSetTextZoomFactor(WKPageRef pageRef, double zoomFactor)
251{
252    toImpl(pageRef)->setTextZoomFactor(zoomFactor);
253}
254
255double WKPageGetPageZoomFactor(WKPageRef pageRef)
256{
257    return toImpl(pageRef)->pageZoomFactor();
258}
259
260void WKPageSetPageZoomFactor(WKPageRef pageRef, double zoomFactor)
261{
262    toImpl(pageRef)->setPageZoomFactor(zoomFactor);
263}
264
265void WKPageSetPageAndTextZoomFactors(WKPageRef pageRef, double pageZoomFactor, double textZoomFactor)
266{
267    toImpl(pageRef)->setPageAndTextZoomFactors(pageZoomFactor, textZoomFactor);
268}
269
270void WKPageSetScaleFactor(WKPageRef pageRef, double scale, WKPoint origin)
271{
272    toImpl(pageRef)->scaleWebView(scale, toIntPoint(origin));
273}
274
275double WKPageGetScaleFactor(WKPageRef pageRef)
276{
277    return toImpl(pageRef)->viewScaleFactor();
278}
279
280void WKPageSetUseFixedLayout(WKPageRef pageRef, bool fixed)
281{
282    toImpl(pageRef)->setUseFixedLayout(fixed);
283}
284
285void WKPageSetFixedLayoutSize(WKPageRef pageRef, WKSize size)
286{
287    toImpl(pageRef)->setFixedLayoutSize(toIntSize(size));
288}
289
290bool WKPageUseFixedLayout(WKPageRef pageRef)
291{
292    return toImpl(pageRef)->useFixedLayout();
293}
294
295WKSize WKPageFixedLayoutSize(WKPageRef pageRef)
296{
297    return toAPI(toImpl(pageRef)->fixedLayoutSize());
298}
299
300bool WKPageHasHorizontalScrollbar(WKPageRef pageRef)
301{
302    return toImpl(pageRef)->hasHorizontalScrollbar();
303}
304
305bool WKPageHasVerticalScrollbar(WKPageRef pageRef)
306{
307    return toImpl(pageRef)->hasVerticalScrollbar();
308}
309
310bool WKPageIsPinnedToLeftSide(WKPageRef pageRef)
311{
312    return toImpl(pageRef)->isPinnedToLeftSide();
313}
314
315bool WKPageIsPinnedToRightSide(WKPageRef pageRef)
316{
317    return toImpl(pageRef)->isPinnedToRightSide();
318}
319
320bool WKPageCanDelete(WKPageRef pageRef)
321{
322    return toImpl(pageRef)->canDelete();
323}
324
325bool WKPageHasSelectedRange(WKPageRef pageRef)
326{
327    return toImpl(pageRef)->hasSelectedRange();
328}
329
330bool WKPageIsContentEditable(WKPageRef pageRef)
331{
332    return toImpl(pageRef)->isContentEditable();
333}
334
335void WKPageFindString(WKPageRef pageRef, WKStringRef string, WKFindOptions options, unsigned maxMatchCount)
336{
337    toImpl(pageRef)->findString(toImpl(string)->string(), toFindOptions(options), maxMatchCount);
338}
339
340void WKPageHideFindUI(WKPageRef pageRef)
341{
342    toImpl(pageRef)->hideFindUI();
343}
344
345void WKPageCountStringMatches(WKPageRef pageRef, WKStringRef string, WKFindOptions options, unsigned maxMatchCount)
346{
347    toImpl(pageRef)->countStringMatches(toImpl(string)->string(), toFindOptions(options), maxMatchCount);
348}
349
350void WKPageSetPageContextMenuClient(WKPageRef pageRef, const WKPageContextMenuClient* wkClient)
351{
352    if (wkClient && wkClient->version)
353        return;
354    toImpl(pageRef)->initializeContextMenuClient(wkClient);
355}
356
357void WKPageSetPageFindClient(WKPageRef pageRef, const WKPageFindClient* wkClient)
358{
359    if (wkClient && wkClient->version)
360        return;
361    toImpl(pageRef)->initializeFindClient(wkClient);
362}
363
364void WKPageSetPageFormClient(WKPageRef pageRef, const WKPageFormClient* wkClient)
365{
366    if (wkClient && wkClient->version)
367        return;
368    toImpl(pageRef)->initializeFormClient(wkClient);
369}
370
371void WKPageSetPageLoaderClient(WKPageRef pageRef, const WKPageLoaderClient* wkClient)
372{
373    if (wkClient && wkClient->version)
374        return;
375    toImpl(pageRef)->initializeLoaderClient(wkClient);
376}
377
378void WKPageSetPagePolicyClient(WKPageRef pageRef, const WKPagePolicyClient* wkClient)
379{
380    if (wkClient && wkClient->version)
381        return;
382    toImpl(pageRef)->initializePolicyClient(wkClient);
383}
384
385void WKPageSetPageResourceLoadClient(WKPageRef pageRef, const WKPageResourceLoadClient* wkClient)
386{
387    if (wkClient && wkClient->version)
388        return;
389    toImpl(pageRef)->initializeResourceLoadClient(wkClient);
390}
391
392void WKPageSetPageUIClient(WKPageRef pageRef, const WKPageUIClient* wkClient)
393{
394    if (wkClient && wkClient->version)
395        return;
396    toImpl(pageRef)->initializeUIClient(wkClient);
397}
398
399void WKPageRunJavaScriptInMainFrame(WKPageRef pageRef, WKStringRef scriptRef, void* context, WKPageRunJavaScriptFunction callback)
400{
401    toImpl(pageRef)->runJavaScriptInMainFrame(toImpl(scriptRef)->string(), ScriptValueCallback::create(context, callback));
402}
403
404#ifdef __BLOCKS__
405static void callRunJavaScriptBlockAndRelease(WKSerializedScriptValueRef resultValue, WKErrorRef error, void* context)
406{
407    WKPageRunJavaScriptBlock block = (WKPageRunJavaScriptBlock)context;
408    block(resultValue, error);
409    Block_release(block);
410}
411
412void WKPageRunJavaScriptInMainFrame_b(WKPageRef pageRef, WKStringRef scriptRef, WKPageRunJavaScriptBlock block)
413{
414    WKPageRunJavaScriptInMainFrame(pageRef, scriptRef, Block_copy(block), callRunJavaScriptBlockAndRelease);
415}
416#endif
417
418void WKPageRenderTreeExternalRepresentation(WKPageRef pageRef, void* context, WKPageRenderTreeExternalRepresentationFunction callback)
419{
420    toImpl(pageRef)->getRenderTreeExternalRepresentation(StringCallback::create(context, callback));
421}
422
423#ifdef __BLOCKS__
424static void callRenderTreeExternalRepresentationBlockAndDispose(WKStringRef resultValue, WKErrorRef error, void* context)
425{
426    WKPageRenderTreeExternalRepresentationBlock block = (WKPageRenderTreeExternalRepresentationBlock)context;
427    block(resultValue, error);
428    Block_release(block);
429}
430
431void WKPageRenderTreeExternalRepresentation_b(WKPageRef pageRef, WKPageRenderTreeExternalRepresentationBlock block)
432{
433    WKPageRenderTreeExternalRepresentation(pageRef, Block_copy(block), callRenderTreeExternalRepresentationBlockAndDispose);
434}
435#endif
436
437void WKPageGetSourceForFrame(WKPageRef pageRef, WKFrameRef frameRef, void* context, WKPageGetSourceForFrameFunction callback)
438{
439    toImpl(pageRef)->getSourceForFrame(toImpl(frameRef), StringCallback::create(context, callback));
440}
441
442#ifdef __BLOCKS__
443static void callGetSourceForFrameBlockBlockAndDispose(WKStringRef resultValue, WKErrorRef error, void* context)
444{
445    WKPageGetSourceForFrameBlock block = (WKPageGetSourceForFrameBlock)context;
446    block(resultValue, error);
447    Block_release(block);
448}
449
450void WKPageGetSourceForFrame_b(WKPageRef pageRef, WKFrameRef frameRef, WKPageGetSourceForFrameBlock block)
451{
452    WKPageGetSourceForFrame(pageRef, frameRef, Block_copy(block), callGetSourceForFrameBlockBlockAndDispose);
453}
454#endif
455
456void WKPageGetContentsAsString(WKPageRef pageRef, void* context, WKPageGetContentsAsStringFunction callback)
457{
458    toImpl(pageRef)->getContentsAsString(StringCallback::create(context, callback));
459}
460
461#ifdef __BLOCKS__
462static void callContentsAsStringBlockBlockAndDispose(WKStringRef resultValue, WKErrorRef error, void* context)
463{
464    WKPageGetContentsAsStringBlock block = (WKPageGetContentsAsStringBlock)context;
465    block(resultValue, error);
466    Block_release(block);
467}
468
469void WKPageGetContentsAsString_b(WKPageRef pageRef, WKPageGetSourceForFrameBlock block)
470{
471    WKPageGetContentsAsString(pageRef, Block_copy(block), callContentsAsStringBlockBlockAndDispose);
472}
473#endif
474
475void WKPageForceRepaint(WKPageRef pageRef, void* context, WKPageForceRepaintFunction callback)
476{
477    toImpl(pageRef)->forceRepaint(VoidCallback::create(context, callback));
478}
479
480WK_EXPORT WKURLRef WKPageCopyPendingAPIRequestURL(WKPageRef pageRef)
481{
482    if (toImpl(pageRef)->pendingAPIRequestURL().isNull())
483        return 0;
484    return toCopiedURLAPI(toImpl(pageRef)->pendingAPIRequestURL());
485}
486
487void WKPageSetDebugPaintFlags(WKPageDebugPaintFlags flags)
488{
489    WebPageProxy::setDebugPaintFlags(flags);
490}
491
492WKPageDebugPaintFlags WKPageGetDebugPaintFlags()
493{
494    return WebPageProxy::debugPaintFlags();
495}
496
497void WKPageValidateCommand(WKPageRef pageRef, WKStringRef command, void* context, WKPageValidateCommandCallback callback)
498{
499    toImpl(pageRef)->validateCommand(toImpl(command)->string(), ValidateCommandCallback::create(context, callback));
500}
501
502void WKPageExecuteCommand(WKPageRef pageRef, WKStringRef command)
503{
504    toImpl(pageRef)->executeEditCommand(toImpl(command)->string());
505}
506
507#if PLATFORM(MAC) || PLATFORM(WIN)
508struct ComputedPagesContext {
509    ComputedPagesContext(WKPageComputePagesForPrintingFunction callback, void* context)
510        : callback(callback)
511        , context(context)
512    {
513    }
514    WKPageComputePagesForPrintingFunction callback;
515    void* context;
516};
517
518static void computedPagesCallback(const Vector<WebCore::IntRect>& rects, double scaleFactor, WKErrorRef error, void* untypedContext)
519{
520    OwnPtr<ComputedPagesContext> context = adoptPtr(static_cast<ComputedPagesContext*>(untypedContext));
521    Vector<WKRect> wkRects(rects.size());
522    for (size_t i = 0; i < rects.size(); ++i)
523        wkRects[i] = toAPI(rects[i]);
524    context->callback(wkRects.data(), wkRects.size(), scaleFactor, error, context->context);
525}
526
527static PrintInfo printInfoFromWKPrintInfo(const WKPrintInfo& printInfo)
528{
529    PrintInfo result;
530    result.pageSetupScaleFactor = printInfo.pageSetupScaleFactor;
531    result.availablePaperWidth = printInfo.availablePaperWidth;
532    result.availablePaperHeight = printInfo.availablePaperHeight;
533    return result;
534}
535
536void WKPageComputePagesForPrinting(WKPageRef page, WKFrameRef frame, WKPrintInfo printInfo, WKPageComputePagesForPrintingFunction callback, void* context)
537{
538    toImpl(page)->computePagesForPrinting(toImpl(frame), printInfoFromWKPrintInfo(printInfo), ComputedPagesCallback::create(new ComputedPagesContext(callback, context), computedPagesCallback));
539}
540
541void WKPageBeginPrinting(WKPageRef page, WKFrameRef frame, WKPrintInfo printInfo)
542{
543    toImpl(page)->beginPrinting(toImpl(frame), printInfoFromWKPrintInfo(printInfo));
544}
545
546void WKPageDrawPagesToPDF(WKPageRef page, WKFrameRef frame, uint32_t first, uint32_t count, WKPageDrawToPDFFunction callback, void* context)
547{
548    toImpl(page)->drawPagesToPDF(toImpl(frame), first, count, DataCallback::create(context, callback));
549}
550#endif
551
552