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