Roll out r81593 Need WebKit2 API for creating a page with a specific main frame name
[WebKit-https.git] / Source / WebKit2 / WebProcess / WebPage / WebPage.cpp
1 /*
2  * Copyright (C) 2010, 2011 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 "WebPage.h"
28
29 #include "Arguments.h"
30 #include "DataReference.h"
31 #include "DecoderAdapter.h"
32 #include "DrawingArea.h"
33 #include "InjectedBundle.h"
34 #include "InjectedBundleBackForwardList.h"
35 #include "MessageID.h"
36 #include "NetscapePlugin.h"
37 #include "PageOverlay.h"
38 #include "PluginProxy.h"
39 #include "PluginView.h"
40 #include "PrintInfo.h"
41 #include "RunLoop.h"
42 #include "SessionState.h"
43 #include "ShareableBitmap.h"
44 #include "WebBackForwardList.h"
45 #include "WebBackForwardListItem.h"
46 #include "WebBackForwardListProxy.h"
47 #include "WebChromeClient.h"
48 #include "WebContextMenu.h"
49 #include "WebContextMenuClient.h"
50 #include "WebContextMessages.h"
51 #include "WebCoreArgumentCoders.h"
52 #include "WebDragClient.h"
53 #include "WebEditorClient.h"
54 #include "WebEvent.h"
55 #include "WebEventConversion.h"
56 #include "WebFrame.h"
57 #include "WebFullScreenManager.h"
58 #include "WebGeolocationClient.h"
59 #include "WebImage.h"
60 #include "WebInspector.h"
61 #include "WebInspectorClient.h"
62 #include "WebOpenPanelResultListener.h"
63 #include "WebPageCreationParameters.h"
64 #include "WebPageGroupProxy.h"
65 #include "WebPageProxyMessages.h"
66 #include "WebPopupMenu.h"
67 #include "WebPreferencesStore.h"
68 #include "WebProcess.h"
69 #include "WebProcessProxyMessageKinds.h"
70 #include "WebProcessProxyMessages.h"
71 #include <WebCore/AbstractDatabase.h>
72 #include <WebCore/ArchiveResource.h>
73 #include <WebCore/Chrome.h>
74 #include <WebCore/ContextMenuController.h>
75 #include <WebCore/DocumentFragment.h>
76 #include <WebCore/DocumentLoader.h>
77 #include <WebCore/DocumentMarkerController.h>
78 #include <WebCore/DragController.h>
79 #include <WebCore/DragData.h>
80 #include <WebCore/EventHandler.h>
81 #include <WebCore/FocusController.h>
82 #include <WebCore/Frame.h>
83 #include <WebCore/FrameLoaderTypes.h>
84 #include <WebCore/FrameView.h>
85 #include <WebCore/HistoryItem.h>
86 #include <WebCore/KeyboardEvent.h>
87 #include <WebCore/Page.h>
88 #include <WebCore/PlatformKeyboardEvent.h>
89 #include <WebCore/PrintContext.h>
90 #include <WebCore/RenderTreeAsText.h>
91 #include <WebCore/RenderLayer.h>
92 #include <WebCore/RenderView.h>
93 #include <WebCore/ReplaceSelectionCommand.h>
94 #include <WebCore/ResourceRequest.h>
95 #include <WebCore/SchemeRegistry.h>
96 #include <WebCore/SerializedScriptValue.h>
97 #include <WebCore/Settings.h>
98 #include <WebCore/SharedBuffer.h>
99 #include <WebCore/SubstituteData.h>
100 #include <WebCore/TextIterator.h>
101 #include <WebCore/markup.h>
102 #include <runtime/JSLock.h>
103 #include <runtime/JSValue.h>
104
105 #include <WebCore/Range.h>
106 #include <WebCore/VisiblePosition.h>
107
108 #if PLATFORM(MAC) || PLATFORM(WIN)
109 #include <WebCore/LegacyWebArchive.h>
110 #endif
111
112 #if ENABLE(PLUGIN_PROCESS)
113 // FIXME: This is currently Mac-specific!
114 #include "MachPort.h"
115 #endif
116
117 #if PLATFORM(QT)
118 #include "HitTestResult.h"
119 #endif
120
121 #ifndef NDEBUG
122 #include <wtf/RefCountedLeakCounter.h>
123 #endif
124
125 using namespace JSC;
126 using namespace WebCore;
127
128 namespace WebKit {
129
130 #ifndef NDEBUG
131 static WTF::RefCountedLeakCounter webPageCounter("WebPage");
132 #endif
133
134 PassRefPtr<WebPage> WebPage::create(uint64_t pageID, const WebPageCreationParameters& parameters)
135 {
136     RefPtr<WebPage> page = adoptRef(new WebPage(pageID, parameters));
137
138     if (page->pageGroup()->isVisibleToInjectedBundle() && WebProcess::shared().injectedBundle())
139         WebProcess::shared().injectedBundle()->didCreatePage(page.get());
140
141     return page.release();
142 }
143
144 WebPage::WebPage(uint64_t pageID, const WebPageCreationParameters& parameters)
145     : m_viewSize(parameters.viewSize)
146     , m_drawsBackground(true)
147     , m_drawsTransparentBackground(false)
148     , m_isInRedo(false)
149     , m_isClosed(false)
150     , m_tabToLinks(false)
151 #if PLATFORM(MAC)
152     , m_windowIsVisible(false)
153     , m_isSmartInsertDeleteEnabled(parameters.isSmartInsertDeleteEnabled)
154 #elif PLATFORM(WIN)
155     , m_nativeWindow(parameters.nativeWindow)
156 #endif
157     , m_findController(this)
158     , m_geolocationPermissionRequestManager(this)
159     , m_pageID(pageID)
160     , m_canRunBeforeUnloadConfirmPanel(parameters.canRunBeforeUnloadConfirmPanel)
161     , m_canRunModal(parameters.canRunModal)
162     , m_isRunningModal(false)
163     , m_cachedMainFrameIsPinnedToLeftSide(false)
164     , m_cachedMainFrameIsPinnedToRightSide(false)
165 {
166     ASSERT(m_pageID);
167
168     Page::PageClients pageClients;
169     pageClients.chromeClient = new WebChromeClient(this);
170     pageClients.contextMenuClient = new WebContextMenuClient(this);
171     pageClients.editorClient = new WebEditorClient(this);
172     pageClients.dragClient = new WebDragClient(this);
173     pageClients.backForwardClient = WebBackForwardListProxy::create(this);
174 #if ENABLE(CLIENT_BASED_GEOLOCATION)
175     pageClients.geolocationClient = new WebGeolocationClient(this);
176 #endif
177 #if ENABLE(INSPECTOR)
178     pageClients.inspectorClient = new WebInspectorClient(this);
179 #endif
180     m_page = adoptPtr(new Page(pageClients));
181
182     // Qt does not yet call setIsInWindow. Until it does, just leave
183     // this line out so plug-ins and video will work. Eventually all platforms
184     // should call setIsInWindow and this comment and #if should be removed,
185     // leaving behind the setCanStartMedia call.
186 #if !PLATFORM(QT)
187     m_page->setCanStartMedia(false);
188 #endif
189
190     updatePreferences(parameters.store);
191
192     m_pageGroup = WebProcess::shared().webPageGroup(parameters.pageGroupData);
193     m_page->setGroupName(m_pageGroup->identifier());
194
195     platformInitialize();
196     Settings::setDefaultMinDOMTimerInterval(0.004);
197
198     m_drawingArea = DrawingArea::create(this, parameters);
199     m_mainFrame = WebFrame::createMainFrame(this);
200
201     setDrawsBackground(parameters.drawsBackground);
202     setDrawsTransparentBackground(parameters.drawsTransparentBackground);
203
204     setMemoryCacheMessagesEnabled(parameters.areMemoryCacheClientCallsEnabled);
205
206     setActive(parameters.isActive);
207     setFocused(parameters.isFocused);
208     setIsInWindow(parameters.isInWindow);
209
210     m_userAgent = parameters.userAgent;
211
212     WebBackForwardListProxy::setHighestItemIDFromUIProcess(parameters.highestUsedBackForwardItemID);
213     
214     if (!parameters.sessionState.isEmpty())
215         restoreSession(parameters.sessionState);
216
217 #ifndef NDEBUG
218     webPageCounter.increment();
219 #endif
220 }
221
222 WebPage::~WebPage()
223 {
224     if (m_backForwardList)
225         m_backForwardList->detach();
226
227     ASSERT(!m_page);
228
229     m_sandboxExtensionTracker.invalidate();
230
231 #if PLATFORM(MAC)
232     ASSERT(m_pluginViews.isEmpty());
233 #endif
234
235 #ifndef NDEBUG
236     webPageCounter.decrement();
237 #endif
238 }
239
240 void WebPage::dummy(bool&)
241 {
242 }
243
244 CoreIPC::Connection* WebPage::connection() const
245 {
246     return WebProcess::shared().connection();
247 }
248
249 void WebPage::initializeInjectedBundleContextMenuClient(WKBundlePageContextMenuClient* client)
250 {
251     m_contextMenuClient.initialize(client);
252 }
253
254 void WebPage::initializeInjectedBundleEditorClient(WKBundlePageEditorClient* client)
255 {
256     m_editorClient.initialize(client);
257 }
258
259 void WebPage::initializeInjectedBundleFormClient(WKBundlePageFormClient* client)
260 {
261     m_formClient.initialize(client);
262 }
263
264 void WebPage::initializeInjectedBundleLoaderClient(WKBundlePageLoaderClient* client)
265 {
266     m_loaderClient.initialize(client);
267 }
268
269 void WebPage::initializeInjectedBundlePolicyClient(WKBundlePagePolicyClient* client)
270 {
271     m_policyClient.initialize(client);
272 }
273
274 void WebPage::initializeInjectedBundleResourceLoadClient(WKBundlePageResourceLoadClient* client)
275 {
276     m_resourceLoadClient.initialize(client);
277 }
278
279 void WebPage::initializeInjectedBundleUIClient(WKBundlePageUIClient* client)
280 {
281     m_uiClient.initialize(client);
282 }
283
284 PassRefPtr<Plugin> WebPage::createPlugin(const Plugin::Parameters& parameters)
285 {
286     String pluginPath;
287
288     if (!WebProcess::shared().connection()->sendSync(
289             Messages::WebContext::GetPluginPath(parameters.mimeType, parameters.url.string()), 
290             Messages::WebContext::GetPluginPath::Reply(pluginPath), 0)) {
291         return 0;
292     }
293
294     if (pluginPath.isNull())
295         return 0;
296
297 #if ENABLE(PLUGIN_PROCESS)
298     return PluginProxy::create(pluginPath);
299 #else
300     return NetscapePlugin::create(NetscapePluginModule::getOrCreate(pluginPath));
301 #endif
302 }
303
304 String WebPage::renderTreeExternalRepresentation() const
305 {
306     return externalRepresentation(m_mainFrame->coreFrame(), RenderAsTextBehaviorNormal);
307 }
308
309 void WebPage::executeEditingCommand(const String& commandName, const String& argument)
310 {
311     Frame* frame = m_page->focusController()->focusedOrMainFrame();
312     if (!frame)
313         return;
314     frame->editor()->command(commandName).execute(argument);
315 }
316
317 bool WebPage::isEditingCommandEnabled(const String& commandName)
318 {
319     Frame* frame = m_page->focusController()->focusedOrMainFrame();
320     if (!frame)
321         return false;
322     
323     Editor::Command command = frame->editor()->command(commandName);
324     return command.isSupported() && command.isEnabled();
325 }
326     
327 void WebPage::clearMainFrameName()
328 {
329     mainFrame()->coreFrame()->tree()->clearName();
330 }
331
332 #if USE(ACCELERATED_COMPOSITING)
333 void WebPage::enterAcceleratedCompositingMode(GraphicsLayer* layer)
334 {
335     m_drawingArea->setRootCompositingLayer(layer);
336 }
337
338 void WebPage::exitAcceleratedCompositingMode()
339 {
340     m_drawingArea->setRootCompositingLayer(0);
341 }
342 #endif
343
344 void WebPage::close()
345 {
346     if (m_isClosed)
347         return;
348
349     m_isClosed = true;
350
351     if (pageGroup()->isVisibleToInjectedBundle() && WebProcess::shared().injectedBundle())
352         WebProcess::shared().injectedBundle()->willDestroyPage(this);
353
354 #if ENABLE(INSPECTOR)
355     m_inspector = 0;
356 #endif
357 #if ENABLE(FULLSCREEN_API)
358     m_fullScreenManager = 0;
359 #endif
360
361     if (m_activePopupMenu) {
362         m_activePopupMenu->disconnectFromPage();
363         m_activePopupMenu = 0;
364     }
365
366     if (m_activeOpenPanelResultListener) {
367         m_activeOpenPanelResultListener->disconnectFromPage();
368         m_activeOpenPanelResultListener = 0;
369     }
370
371     m_sandboxExtensionTracker.invalidate();
372
373     m_printContext = nullptr;
374
375     m_mainFrame->coreFrame()->loader()->detachFromParent();
376     m_page.clear();
377
378     m_drawingArea.clear();
379
380     bool isRunningModal = m_isRunningModal;
381     m_isRunningModal = false;
382
383     // The WebPage can be destroyed by this call.
384     WebProcess::shared().removeWebPage(m_pageID);
385
386     if (isRunningModal)
387         WebProcess::shared().runLoop()->stop();
388 }
389
390 void WebPage::tryClose()
391 {
392     if (!m_mainFrame->coreFrame()->loader()->shouldClose())
393         return;
394
395     sendClose();
396 }
397
398 void WebPage::sendClose()
399 {
400     send(Messages::WebPageProxy::ClosePage());
401 }
402
403 void WebPage::loadURL(const String& url, const SandboxExtension::Handle& sandboxExtensionHandle)
404 {
405     loadURLRequest(ResourceRequest(KURL(KURL(), url)), sandboxExtensionHandle);
406 }
407
408 void WebPage::loadURLRequest(const ResourceRequest& request, const SandboxExtension::Handle& sandboxExtensionHandle)
409 {
410     m_sandboxExtensionTracker.beginLoad(m_mainFrame.get(), sandboxExtensionHandle);
411     m_mainFrame->coreFrame()->loader()->load(request, false);
412 }
413
414 void WebPage::loadData(PassRefPtr<SharedBuffer> sharedBuffer, const String& MIMEType, const String& encodingName, const KURL& baseURL, const KURL& unreachableURL)
415 {
416     ResourceRequest request(baseURL);
417     SubstituteData substituteData(sharedBuffer, MIMEType, encodingName, unreachableURL);
418     m_mainFrame->coreFrame()->loader()->load(request, substituteData, false);
419 }
420
421 void WebPage::loadHTMLString(const String& htmlString, const String& baseURLString)
422 {
423     RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(reinterpret_cast<const char*>(htmlString.characters()), htmlString.length() * sizeof(UChar));
424     KURL baseURL = baseURLString.isEmpty() ? blankURL() : KURL(KURL(), baseURLString);
425     loadData(sharedBuffer, "text/html", "utf-16", baseURL, KURL());
426 }
427
428 void WebPage::loadAlternateHTMLString(const String& htmlString, const String& baseURLString, const String& unreachableURLString)
429 {
430     RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(reinterpret_cast<const char*>(htmlString.characters()), htmlString.length() * sizeof(UChar));
431     KURL baseURL = baseURLString.isEmpty() ? blankURL() : KURL(KURL(), baseURLString);
432     KURL unreachableURL = unreachableURLString.isEmpty() ? KURL() : KURL(KURL(), unreachableURLString)  ;
433     loadData(sharedBuffer, "text/html", "utf-16", baseURL, unreachableURL);
434 }
435
436 void WebPage::loadPlainTextString(const String& string)
437 {
438     RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(reinterpret_cast<const char*>(string.characters()), string.length() * sizeof(UChar));
439     loadData(sharedBuffer, "text/plain", "utf-16", blankURL(), KURL());
440 }
441
442 void WebPage::stopLoadingFrame(uint64_t frameID)
443 {
444     WebFrame* frame = WebProcess::shared().webFrame(frameID);
445     if (!frame)
446         return;
447
448     frame->coreFrame()->loader()->stopForUserCancel();
449 }
450
451 void WebPage::stopLoading()
452 {
453     m_mainFrame->coreFrame()->loader()->stopForUserCancel();
454 }
455
456 void WebPage::setDefersLoading(bool defersLoading)
457 {
458     m_page->setDefersLoading(defersLoading);
459 }
460
461 void WebPage::reload(bool reloadFromOrigin)
462 {
463     m_mainFrame->coreFrame()->loader()->reload(reloadFromOrigin);
464 }
465
466 void WebPage::goForward(uint64_t backForwardItemID, const SandboxExtension::Handle& sandboxExtensionHandle)
467 {
468     HistoryItem* item = WebBackForwardListProxy::itemForID(backForwardItemID);
469     ASSERT(item);
470     if (!item)
471         return;
472
473     m_sandboxExtensionTracker.beginLoad(m_mainFrame.get(), sandboxExtensionHandle);
474     m_page->goToItem(item, FrameLoadTypeForward);
475 }
476
477 void WebPage::goBack(uint64_t backForwardItemID, const SandboxExtension::Handle& sandboxExtensionHandle)
478 {
479     HistoryItem* item = WebBackForwardListProxy::itemForID(backForwardItemID);
480     ASSERT(item);
481     if (!item)
482         return;
483
484     m_sandboxExtensionTracker.beginLoad(m_mainFrame.get(), sandboxExtensionHandle);
485     m_page->goToItem(item, FrameLoadTypeBack);
486 }
487
488 void WebPage::goToBackForwardItem(uint64_t backForwardItemID, const SandboxExtension::Handle& sandboxExtensionHandle)
489 {
490     HistoryItem* item = WebBackForwardListProxy::itemForID(backForwardItemID);
491     ASSERT(item);
492     if (!item)
493         return;
494
495     m_sandboxExtensionTracker.beginLoad(m_mainFrame.get(), sandboxExtensionHandle);
496     m_page->goToItem(item, FrameLoadTypeIndexedBackForward);
497 }
498
499 void WebPage::layoutIfNeeded()
500 {
501     if (m_mainFrame->coreFrame()->view())
502         m_mainFrame->coreFrame()->view()->updateLayoutAndStyleIfNeededRecursive();
503 }
504
505 void WebPage::setSize(const WebCore::IntSize& viewSize)
506 {
507 #if ENABLE(TILED_BACKING_STORE)
508     // If we are resizing to content ignore external attempts.
509     if (!m_resizesToContentsLayoutSize.isEmpty())
510         return;
511 #endif
512
513     if (m_viewSize == viewSize)
514         return;
515
516     Frame* frame = m_page->mainFrame();
517     
518     frame->view()->resize(viewSize);
519     frame->view()->setNeedsLayout();
520     m_drawingArea->setNeedsDisplay(IntRect(IntPoint(0, 0), viewSize));
521     
522     m_viewSize = viewSize;
523 }
524
525 #if ENABLE(TILED_BACKING_STORE)
526 void WebPage::setActualVisibleContentRect(const IntRect& rect)
527 {
528     Frame* frame = m_page->mainFrame();
529
530     frame->view()->setActualVisibleContentRect(rect);
531 }
532
533 void WebPage::setResizesToContentsUsingLayoutSize(const IntSize& targetLayoutSize)
534 {
535     if (m_resizesToContentsLayoutSize == targetLayoutSize)
536         return;
537
538     m_resizesToContentsLayoutSize = targetLayoutSize;
539
540     Frame* frame = m_page->mainFrame();
541     if (m_resizesToContentsLayoutSize.isEmpty()) {
542         frame->view()->setDelegatesScrolling(false);
543         frame->view()->setUseFixedLayout(false);
544         frame->view()->setPaintsEntireContents(false);
545     } else {
546         frame->view()->setDelegatesScrolling(true);
547         frame->view()->setUseFixedLayout(true);
548         frame->view()->setPaintsEntireContents(true);
549         frame->view()->setFixedLayoutSize(m_resizesToContentsLayoutSize);
550     }
551     frame->view()->forceLayout();
552 }
553
554 void WebPage::resizeToContentsIfNeeded()
555 {
556     if (m_resizesToContentsLayoutSize.isEmpty())
557         return;
558
559     Frame* frame = m_page->mainFrame();
560
561     IntSize contentSize = frame->view()->contentsSize();
562     if (contentSize == m_viewSize)
563         return;
564
565     m_viewSize = contentSize;
566     frame->view()->resize(m_viewSize);
567     frame->view()->setNeedsLayout();
568 }
569 #endif
570
571 void WebPage::scrollMainFrameIfNotAtMaxScrollPosition(const IntSize& scrollOffset)
572 {
573     Frame* frame = m_page->mainFrame();
574
575     IntPoint scrollPosition = frame->view()->scrollPosition();
576     IntPoint maximumScrollPosition = frame->view()->maximumScrollPosition();
577
578     // If the current scroll position in a direction is the max scroll position 
579     // we don't want to scroll at all.
580     IntSize newScrollOffset;
581     if (scrollPosition.x() < maximumScrollPosition.x())
582         newScrollOffset.setWidth(scrollOffset.width());
583     if (scrollPosition.y() < maximumScrollPosition.y())
584         newScrollOffset.setHeight(scrollOffset.height());
585
586     if (newScrollOffset.isZero())
587         return;
588
589     frame->view()->setScrollPosition(frame->view()->scrollPosition() + newScrollOffset);
590 }
591
592 void WebPage::drawRect(GraphicsContext& graphicsContext, const IntRect& rect)
593 {
594     graphicsContext.save();
595     graphicsContext.clip(rect);
596     m_mainFrame->coreFrame()->view()->paint(&graphicsContext, rect);
597     graphicsContext.restore();
598 }
599
600 void WebPage::drawPageOverlay(GraphicsContext& graphicsContext, const IntRect& rect)
601 {
602     ASSERT(m_pageOverlay);
603
604     graphicsContext.save();
605     graphicsContext.clip(rect);
606     m_pageOverlay->drawRect(graphicsContext, rect);
607     graphicsContext.restore();
608 }
609
610 double WebPage::textZoomFactor() const
611 {
612     Frame* frame = m_mainFrame->coreFrame();
613     if (!frame)
614         return 1;
615     return frame->textZoomFactor();
616 }
617
618 void WebPage::setTextZoomFactor(double zoomFactor)
619 {
620     Frame* frame = m_mainFrame->coreFrame();
621     if (!frame)
622         return;
623     frame->setTextZoomFactor(static_cast<float>(zoomFactor));
624 }
625
626 double WebPage::pageZoomFactor() const
627 {
628     Frame* frame = m_mainFrame->coreFrame();
629     if (!frame)
630         return 1;
631     return frame->pageZoomFactor();
632 }
633
634 void WebPage::setPageZoomFactor(double zoomFactor)
635 {
636     Frame* frame = m_mainFrame->coreFrame();
637     if (!frame)
638         return;
639     frame->setPageZoomFactor(static_cast<float>(zoomFactor));
640 }
641
642 void WebPage::setPageAndTextZoomFactors(double pageZoomFactor, double textZoomFactor)
643 {
644     Frame* frame = m_mainFrame->coreFrame();
645     if (!frame)
646         return;
647     return frame->setPageAndTextZoomFactors(static_cast<float>(pageZoomFactor), static_cast<float>(textZoomFactor));
648 }
649
650 void WebPage::scaleWebView(double scale, const IntPoint& origin)
651 {
652     Frame* frame = m_mainFrame->coreFrame();
653     if (!frame)
654         return;
655     frame->scalePage(scale, origin);
656
657     send(Messages::WebPageProxy::ViewScaleFactorDidChange(scale));
658 }
659
660 double WebPage::viewScaleFactor() const
661 {
662     Frame* frame = m_mainFrame->coreFrame();
663     if (!frame)
664         return 1;
665     return frame->pageScaleFactor();
666 }
667
668 void WebPage::setUseFixedLayout(bool fixed)
669 {
670     Frame* frame = m_mainFrame->coreFrame();
671     if (!frame)
672         return;
673
674     FrameView* view = frame->view();
675     if (!view)
676         return;
677
678     view->setUseFixedLayout(fixed);
679     if (!fixed)
680         view->setFixedLayoutSize(IntSize());
681 }
682
683 void WebPage::setFixedLayoutSize(const IntSize& size)
684 {
685     Frame* frame = m_mainFrame->coreFrame();
686     if (!frame)
687         return;
688     
689     FrameView* view = frame->view();
690     if (!view)
691         return;
692
693     view->setFixedLayoutSize(size);
694     view->forceLayout();
695 }
696
697 void WebPage::installPageOverlay(PassRefPtr<PageOverlay> pageOverlay)
698 {
699     if (m_pageOverlay)
700         pageOverlay->setPage(0);
701
702     m_pageOverlay = pageOverlay;
703     m_pageOverlay->setPage(this);
704
705     m_drawingArea->didInstallPageOverlay();
706
707     m_pageOverlay->setNeedsDisplay();
708 }
709
710 void WebPage::uninstallPageOverlay(PageOverlay* pageOverlay)
711 {
712     if (pageOverlay != m_pageOverlay)
713         return;
714
715     m_pageOverlay->setPage(0);
716     m_pageOverlay = nullptr;
717
718     m_drawingArea->didUninstallPageOverlay();
719 }
720
721 PassRefPtr<WebImage> WebPage::snapshotInViewCoordinates(const IntRect& rect, ImageOptions options)
722 {
723     FrameView* frameView = m_mainFrame->coreFrame()->view();
724     if (!frameView)
725         return 0;
726
727     frameView->updateLayoutAndStyleIfNeededRecursive();
728
729     PaintBehavior oldBehavior = frameView->paintBehavior();
730     frameView->setPaintBehavior(oldBehavior | PaintBehaviorFlattenCompositingLayers);
731
732     RefPtr<WebImage> snapshot = WebImage::create(rect.size(), options);
733     OwnPtr<WebCore::GraphicsContext> graphicsContext = snapshot->bitmap()->createGraphicsContext();
734
735     graphicsContext->save();
736     graphicsContext->translate(-rect.x(), -rect.y());
737     frameView->paint(graphicsContext.get(), rect);
738     graphicsContext->restore();
739
740     frameView->setPaintBehavior(oldBehavior);
741
742     return snapshot.release();
743 }
744
745 PassRefPtr<WebImage> WebPage::scaledSnapshotInDocumentCoordinates(const IntRect& rect, double scaleFactor, ImageOptions options)
746 {
747     FrameView* frameView = m_mainFrame->coreFrame()->view();
748     if (!frameView)
749         return 0;
750
751     frameView->updateLayoutAndStyleIfNeededRecursive();
752
753     PaintBehavior oldBehavior = frameView->paintBehavior();
754     frameView->setPaintBehavior(oldBehavior | PaintBehaviorFlattenCompositingLayers);
755
756     bool scale = scaleFactor != 1;
757     IntSize size = rect.size();
758     if (scale) 
759         size = IntSize(ceil(rect.width() * scaleFactor), ceil(rect.height() * scaleFactor));
760
761     RefPtr<WebImage> snapshot = WebImage::create(size, options);
762     OwnPtr<WebCore::GraphicsContext> graphicsContext = snapshot->bitmap()->createGraphicsContext();
763     graphicsContext->save();
764     
765     if (scale)
766         graphicsContext->scale(FloatSize(scaleFactor, scaleFactor));
767     
768     graphicsContext->translate(-rect.x(), -rect.y());
769     frameView->paintContents(graphicsContext.get(), rect);
770     graphicsContext->restore();
771
772     frameView->setPaintBehavior(oldBehavior);
773
774     return snapshot.release();
775 }
776
777 PassRefPtr<WebImage> WebPage::snapshotInDocumentCoordinates(const IntRect& rect, ImageOptions options)
778 {
779     return scaledSnapshotInDocumentCoordinates(rect, 1, options);
780 }
781
782 void WebPage::pageDidScroll()
783 {
784     // Hide the find indicator.
785     m_findController.hideFindIndicator();
786
787     m_uiClient.pageDidScroll(this);
788
789     send(Messages::WebPageProxy::PageDidScroll());
790 }
791
792 #if ENABLE(TILED_BACKING_STORE)
793 void WebPage::pageDidRequestScroll(const IntPoint& point)
794 {
795     send(Messages::WebPageProxy::PageDidRequestScroll(point));
796 }
797 #endif
798
799 WebContextMenu* WebPage::contextMenu()
800 {
801     if (!m_contextMenu)
802         m_contextMenu = WebContextMenu::create(this);
803     return m_contextMenu.get();
804 }
805
806 void WebPage::getLocationAndLengthFromRange(Range* range, uint64_t& location, uint64_t& length)
807 {
808     location = notFound;
809     length = 0;
810
811     if (!range || !range->startContainer())
812         return;
813
814     Element* selectionRoot = range->ownerDocument()->frame()->selection()->rootEditableElement();
815     Element* scope = selectionRoot ? selectionRoot : range->ownerDocument()->documentElement();
816     
817     // Mouse events may cause TSM to attempt to create an NSRange for a portion of the view
818     // that is not inside the current editable region.  These checks ensure we don't produce
819     // potentially invalid data when responding to such requests.
820     if (range->startContainer() != scope && !range->startContainer()->isDescendantOf(scope))
821         return;
822     if (range->endContainer() != scope && !range->endContainer()->isDescendantOf(scope))
823         return;
824
825     RefPtr<Range> testRange = Range::create(scope->document(), scope, 0, range->startContainer(), range->startOffset());
826     ASSERT(testRange->startContainer() == scope);
827     location = TextIterator::rangeLength(testRange.get());
828     
829     ExceptionCode ec;
830     testRange->setEnd(range->endContainer(), range->endOffset(), ec);
831     ASSERT(testRange->startContainer() == scope);
832     length = TextIterator::rangeLength(testRange.get()) - location;
833 }
834
835 // Events 
836
837 static const WebEvent* g_currentEvent = 0;
838
839 // FIXME: WebPage::currentEvent is used by the plug-in code to avoid having to convert from DOM events back to
840 // WebEvents. When we get the event handling sorted out, this should go away and the Widgets should get the correct
841 // platform events passed to the event handler code.
842 const WebEvent* WebPage::currentEvent()
843 {
844     return g_currentEvent;
845 }
846
847 class CurrentEvent {
848 public:
849     explicit CurrentEvent(const WebEvent& event)
850         : m_previousCurrentEvent(g_currentEvent)
851     {
852         g_currentEvent = &event;
853     }
854
855     ~CurrentEvent()
856     {
857         g_currentEvent = m_previousCurrentEvent;
858     }
859
860 private:
861     const WebEvent* m_previousCurrentEvent;
862 };
863
864 static bool isContextClick(const PlatformMouseEvent& event)
865 {
866     if (event.button() == WebCore::RightButton)
867         return true;
868
869 #if PLATFORM(MAC)
870     // FIXME: this really should be about OSX-style UI, not about the Mac port
871     if (event.button() == WebCore::LeftButton && event.ctrlKey())
872         return true;
873 #endif
874
875     return false;
876 }
877
878 static bool handleMouseEvent(const WebMouseEvent& mouseEvent, Page* page)
879 {
880     Frame* frame = page->mainFrame();
881     if (!frame->view())
882         return false;
883
884     PlatformMouseEvent platformMouseEvent = platform(mouseEvent);
885
886     switch (platformMouseEvent.eventType()) {
887         case WebCore::MouseEventPressed:
888         {
889             if (isContextClick(platformMouseEvent))
890                 page->contextMenuController()->clearContextMenu();
891             
892             bool handled = frame->eventHandler()->handleMousePressEvent(platformMouseEvent);
893             
894             if (isContextClick(platformMouseEvent)) {
895                 handled = frame->eventHandler()->sendContextMenuEvent(platformMouseEvent);
896                 if (handled)
897                     page->chrome()->showContextMenu();
898             }
899
900             return handled;
901         }
902         case WebCore::MouseEventReleased:
903             return frame->eventHandler()->handleMouseReleaseEvent(platformMouseEvent);
904         case WebCore::MouseEventMoved:
905             return frame->eventHandler()->mouseMoved(platformMouseEvent);
906
907         default:
908             ASSERT_NOT_REACHED();
909             return false;
910     }
911 }
912
913 void WebPage::mouseEvent(const WebMouseEvent& mouseEvent)
914 {
915     bool handled = false;
916     
917     if (m_pageOverlay) {
918         // Let the page overlay handle the event.
919         handled = m_pageOverlay->mouseEvent(mouseEvent);
920     }
921
922     if (!handled) {
923         CurrentEvent currentEvent(mouseEvent);
924
925         handled = handleMouseEvent(mouseEvent, m_page.get());
926     }
927
928     send(Messages::WebPageProxy::DidReceiveEvent(static_cast<uint32_t>(mouseEvent.type()), handled));
929 }
930
931 static bool handleWheelEvent(const WebWheelEvent& wheelEvent, Page* page)
932 {
933     Frame* frame = page->mainFrame();
934     if (!frame->view())
935         return false;
936
937     PlatformWheelEvent platformWheelEvent = platform(wheelEvent);
938     return frame->eventHandler()->handleWheelEvent(platformWheelEvent);
939 }
940
941 void WebPage::wheelEvent(const WebWheelEvent& wheelEvent)
942 {
943     CurrentEvent currentEvent(wheelEvent);
944
945     bool handled = handleWheelEvent(wheelEvent, m_page.get());
946     send(Messages::WebPageProxy::DidReceiveEvent(static_cast<uint32_t>(wheelEvent.type()), handled));
947 }
948
949 static bool handleKeyEvent(const WebKeyboardEvent& keyboardEvent, Page* page)
950 {
951     if (!page->mainFrame()->view())
952         return false;
953
954     if (keyboardEvent.type() == WebEvent::Char && keyboardEvent.isSystemKey())
955         return page->focusController()->focusedOrMainFrame()->eventHandler()->handleAccessKey(platform(keyboardEvent));
956     return page->focusController()->focusedOrMainFrame()->eventHandler()->keyEvent(platform(keyboardEvent));
957 }
958
959 void WebPage::keyEvent(const WebKeyboardEvent& keyboardEvent)
960 {
961     CurrentEvent currentEvent(keyboardEvent);
962
963     bool handled = handleKeyEvent(keyboardEvent, m_page.get());
964     if (!handled)
965         handled = performDefaultBehaviorForKeyEvent(keyboardEvent);
966
967     send(Messages::WebPageProxy::DidReceiveEvent(static_cast<uint32_t>(keyboardEvent.type()), handled));
968 }
969
970 #if ENABLE(GESTURE_EVENTS)
971 static bool handleGestureEvent(const WebGestureEvent& gestureEvent, Page* page)
972 {
973     Frame* frame = page->mainFrame();
974     if (!frame->view())
975         return false;
976
977     PlatformGestureEvent platformGestureEvent = platform(gestureEvent);
978     return frame->eventHandler()->handleGestureEvent(platformGestureEvent);
979 }
980
981 void WebPage::gestureEvent(const WebGestureEvent& gestureEvent)
982 {
983     CurrentEvent currentEvent(gestureEvent);
984
985     bool handled = handleGestureEvent(gestureEvent, m_page.get());
986     send(Messages::WebPageProxy::DidReceiveEvent(static_cast<uint32_t>(gestureEvent.type()), handled));
987 }
988 #endif
989
990 void WebPage::validateCommand(const String& commandName, uint64_t callbackID)
991 {
992     bool isEnabled = false;
993     int32_t state = 0;
994     Frame* frame = m_page->focusController()->focusedOrMainFrame();
995     if (frame) {
996         Editor::Command command = frame->editor()->command(commandName);
997         state = command.state();
998         isEnabled = command.isSupported() && command.isEnabled();
999     }
1000
1001     send(Messages::WebPageProxy::ValidateCommandCallback(commandName, isEnabled, state, callbackID));
1002 }
1003
1004 void WebPage::executeEditCommand(const String& commandName)
1005 {
1006     executeEditingCommand(commandName, String());
1007 }
1008
1009 uint64_t WebPage::restoreSession(const SessionState& sessionState)
1010 {
1011     const BackForwardListItemVector& list = sessionState.list();
1012     size_t size = list.size();
1013     uint64_t currentItemID = 0;
1014     for (size_t i = 0; i < size; ++i) {
1015         WebBackForwardListItem* webItem = list[i].get();
1016         DecoderAdapter decoder(webItem->backForwardData().data(), webItem->backForwardData().size());
1017         
1018         RefPtr<HistoryItem> item = HistoryItem::decodeBackForwardTree(webItem->url(), webItem->title(), webItem->originalURL(), decoder);
1019         if (!item) {
1020             LOG_ERROR("Failed to decode a HistoryItem from session state data.");
1021             return 0;
1022         }
1023         
1024         if (i == sessionState.currentIndex())
1025             currentItemID = webItem->itemID();
1026         
1027         WebBackForwardListProxy::addItemFromUIProcess(list[i]->itemID(), item.release());
1028     }    
1029     ASSERT(currentItemID);
1030     return currentItemID;
1031 }
1032
1033 void WebPage::restoreSessionAndNavigateToCurrentItem(const SessionState& sessionState, const SandboxExtension::Handle& sandboxExtensionHandle)
1034 {
1035     if (uint64_t currentItemID = restoreSession(sessionState))
1036         goToBackForwardItem(currentItemID, sandboxExtensionHandle);
1037 }
1038
1039 #if ENABLE(TOUCH_EVENTS)
1040 static bool handleTouchEvent(const WebTouchEvent& touchEvent, Page* page)
1041 {
1042     Frame* frame = page->mainFrame();
1043     if (!frame->view())
1044         return false;
1045
1046     return frame->eventHandler()->handleTouchEvent(platform(touchEvent));
1047 }
1048
1049 void WebPage::touchEvent(const WebTouchEvent& touchEvent)
1050 {
1051     CurrentEvent currentEvent(touchEvent);
1052
1053     bool handled = handleTouchEvent(touchEvent, m_page.get());
1054
1055     send(Messages::WebPageProxy::DidReceiveEvent(static_cast<uint32_t>(touchEvent.type()), handled));
1056 }
1057 #endif
1058
1059 void WebPage::setActive(bool isActive)
1060 {
1061     m_page->focusController()->setActive(isActive);
1062
1063 #if PLATFORM(MAC)    
1064     // Tell all our plug-in views that the window focus changed.
1065     for (HashSet<PluginView*>::const_iterator it = m_pluginViews.begin(), end = m_pluginViews.end(); it != end; ++it)
1066         (*it)->setWindowIsFocused(isActive);
1067 #endif
1068 }
1069
1070 void WebPage::setDrawsBackground(bool drawsBackground)
1071 {
1072     if (m_drawsBackground == drawsBackground)
1073         return;
1074
1075     m_drawsBackground = drawsBackground;
1076
1077     for (Frame* coreFrame = m_mainFrame->coreFrame(); coreFrame; coreFrame = coreFrame->tree()->traverseNext()) {
1078         if (FrameView* view = coreFrame->view())
1079             view->setTransparent(!drawsBackground);
1080     }
1081
1082     m_drawingArea->pageBackgroundTransparencyChanged();
1083     m_drawingArea->setNeedsDisplay(IntRect(IntPoint(0, 0), m_viewSize));
1084 }
1085
1086 void WebPage::setDrawsTransparentBackground(bool drawsTransparentBackground)
1087 {
1088     if (m_drawsTransparentBackground == drawsTransparentBackground)
1089         return;
1090
1091     m_drawsTransparentBackground = drawsTransparentBackground;
1092
1093     Color backgroundColor = drawsTransparentBackground ? Color::transparent : Color::white;
1094     for (Frame* coreFrame = m_mainFrame->coreFrame(); coreFrame; coreFrame = coreFrame->tree()->traverseNext()) {
1095         if (FrameView* view = coreFrame->view())
1096             view->setBaseBackgroundColor(backgroundColor);
1097     }
1098
1099     m_drawingArea->pageBackgroundTransparencyChanged();
1100     m_drawingArea->setNeedsDisplay(IntRect(IntPoint(0, 0), m_viewSize));
1101 }
1102
1103 void WebPage::viewWillStartLiveResize()
1104 {
1105     if (!m_page)
1106         return;
1107
1108     // FIXME: This should propagate to all ScrollableAreas.
1109     if (Frame* frame = m_page->focusController()->focusedOrMainFrame()) {
1110         if (FrameView* view = frame->view())
1111             view->willStartLiveResize();
1112     }
1113 }
1114
1115 void WebPage::viewWillEndLiveResize()
1116 {
1117     if (!m_page)
1118         return;
1119
1120     // FIXME: This should propagate to all ScrollableAreas.
1121     if (Frame* frame = m_page->focusController()->focusedOrMainFrame()) {
1122         if (FrameView* view = frame->view())
1123             view->willEndLiveResize();
1124     }
1125 }
1126
1127 void WebPage::setFocused(bool isFocused)
1128 {
1129     m_page->focusController()->setFocused(isFocused);
1130 }
1131
1132 void WebPage::setInitialFocus(bool forward)
1133 {
1134     if (!m_page || !m_page->focusController())
1135         return;
1136
1137     Frame* frame = m_page->focusController()->focusedOrMainFrame();
1138     frame->document()->setFocusedNode(0);
1139     m_page->focusController()->setInitialFocus(forward ? FocusDirectionForward : FocusDirectionBackward, 0);
1140 }
1141
1142 void WebPage::setWindowResizerSize(const IntSize& windowResizerSize)
1143 {
1144     if (m_windowResizerSize == windowResizerSize)
1145         return;
1146
1147     m_windowResizerSize = windowResizerSize;
1148
1149     for (Frame* coreFrame = m_mainFrame->coreFrame(); coreFrame; coreFrame = coreFrame->tree()->traverseNext()) {
1150         FrameView* view = coreFrame->view();
1151         if (view)
1152             view->windowResizerRectChanged();
1153     }
1154 }
1155
1156 void WebPage::setIsInWindow(bool isInWindow)
1157 {
1158     if (!isInWindow) {
1159         m_page->setCanStartMedia(false);
1160         m_page->willMoveOffscreen();
1161     } else {
1162         m_page->setCanStartMedia(true);
1163         m_page->didMoveOnscreen();
1164     }
1165 }
1166
1167 void WebPage::didReceivePolicyDecision(uint64_t frameID, uint64_t listenerID, uint32_t policyAction, uint64_t downloadID)
1168 {
1169     WebFrame* frame = WebProcess::shared().webFrame(frameID);
1170     if (!frame)
1171         return;
1172     frame->didReceivePolicyDecision(listenerID, static_cast<PolicyAction>(policyAction), downloadID);
1173 }
1174
1175 void WebPage::show()
1176 {
1177     send(Messages::WebPageProxy::ShowPage());
1178 }
1179
1180 void WebPage::setUserAgent(const String& userAgent)
1181 {
1182     m_userAgent = userAgent;
1183 }
1184     
1185 IntRect WebPage::windowToScreen(const IntRect& rect)
1186 {
1187     IntRect screenRect;
1188     sendSync(Messages::WebPageProxy::WindowToScreen(rect), Messages::WebPageProxy::WindowToScreen::Reply(screenRect));
1189     return screenRect;
1190 }
1191
1192 IntRect WebPage::windowResizerRect() const
1193 {
1194     if (m_windowResizerSize.isEmpty())
1195         return IntRect();
1196
1197     IntSize frameViewSize;
1198     if (Frame* coreFrame = m_mainFrame->coreFrame()) {
1199         if (FrameView* view = coreFrame->view())
1200             frameViewSize = view->size();
1201     }
1202
1203     return IntRect(frameViewSize.width() - m_windowResizerSize.width(), frameViewSize.height() - m_windowResizerSize.height(), 
1204                    m_windowResizerSize.width(), m_windowResizerSize.height());
1205 }
1206
1207 KeyboardUIMode WebPage::keyboardUIMode()
1208 {
1209     bool fullKeyboardAccessEnabled = WebProcess::shared().fullKeyboardAccessEnabled();
1210     return static_cast<KeyboardUIMode>((fullKeyboardAccessEnabled ? KeyboardAccessFull : KeyboardAccessDefault) | (m_tabToLinks ? KeyboardAccessTabsToLinks : 0));
1211 }
1212
1213 void WebPage::runJavaScriptInMainFrame(const String& script, uint64_t callbackID)
1214 {
1215     // NOTE: We need to be careful when running scripts that the objects we depend on don't
1216     // disappear during script execution.
1217
1218     // Retain the SerializedScriptValue at this level so it (and the internal data) lives
1219     // long enough for the DataReference to be encoded by the sent message.
1220     RefPtr<SerializedScriptValue> serializedResultValue;
1221     CoreIPC::DataReference dataReference;
1222
1223     JSLock lock(SilenceAssertionsOnly);
1224     if (JSValue resultValue = m_mainFrame->coreFrame()->script()->executeScript(script, true).jsValue()) {
1225         if ((serializedResultValue = SerializedScriptValue::create(m_mainFrame->coreFrame()->script()->globalObject(mainThreadNormalWorld())->globalExec(), resultValue)))
1226             dataReference = CoreIPC::DataReference(serializedResultValue->data().data(), serializedResultValue->data().size());
1227     }
1228
1229     send(Messages::WebPageProxy::ScriptValueCallback(dataReference, callbackID));
1230 }
1231
1232 void WebPage::getContentsAsString(uint64_t callbackID)
1233 {
1234     String resultString = m_mainFrame->contentsAsString();
1235     send(Messages::WebPageProxy::StringCallback(resultString, callbackID));
1236 }
1237
1238 void WebPage::getRenderTreeExternalRepresentation(uint64_t callbackID)
1239 {
1240     String resultString = renderTreeExternalRepresentation();
1241     send(Messages::WebPageProxy::StringCallback(resultString, callbackID));
1242 }
1243
1244 void WebPage::getSelectionOrContentsAsString(uint64_t callbackID)
1245 {
1246     String resultString = m_mainFrame->selectionAsString();
1247     if (resultString.isEmpty())
1248         resultString = m_mainFrame->contentsAsString();
1249     send(Messages::WebPageProxy::StringCallback(resultString, callbackID));
1250 }
1251
1252 void WebPage::getSourceForFrame(uint64_t frameID, uint64_t callbackID)
1253 {
1254     String resultString;
1255     if (WebFrame* frame = WebProcess::shared().webFrame(frameID))
1256        resultString = frame->source();
1257
1258     send(Messages::WebPageProxy::StringCallback(resultString, callbackID));
1259 }
1260
1261 void WebPage::getMainResourceDataOfFrame(uint64_t frameID, uint64_t callbackID)
1262 {
1263     CoreIPC::DataReference dataReference;
1264
1265     RefPtr<SharedBuffer> buffer;
1266     if (WebFrame* frame = WebProcess::shared().webFrame(frameID)) {
1267         if (DocumentLoader* loader = frame->coreFrame()->loader()->documentLoader()) {
1268             if ((buffer = loader->mainResourceData()))
1269                 dataReference = CoreIPC::DataReference(reinterpret_cast<const uint8_t*>(buffer->data()), buffer->size());
1270         }
1271     }
1272
1273     send(Messages::WebPageProxy::DataCallback(dataReference, callbackID));
1274 }
1275
1276 void WebPage::getResourceDataFromFrame(uint64_t frameID, const String& resourceURL, uint64_t callbackID)
1277 {
1278     CoreIPC::DataReference dataReference;
1279
1280     RefPtr<SharedBuffer> buffer;
1281     if (WebFrame* frame = WebProcess::shared().webFrame(frameID)) {
1282         if (DocumentLoader* loader = frame->coreFrame()->loader()->documentLoader()) {
1283             if (RefPtr<ArchiveResource> subresource = loader->subresource(KURL(KURL(), resourceURL))) {
1284                 if ((buffer = subresource->data()))
1285                     dataReference = CoreIPC::DataReference(reinterpret_cast<const uint8_t*>(buffer->data()), buffer->size());
1286             }
1287         }
1288     }
1289
1290     send(Messages::WebPageProxy::DataCallback(dataReference, callbackID));
1291 }
1292
1293 void WebPage::getWebArchiveOfFrame(uint64_t frameID, uint64_t callbackID)
1294 {
1295     CoreIPC::DataReference dataReference;
1296
1297 #if PLATFORM(MAC) || PLATFORM(WIN)
1298     RetainPtr<CFDataRef> data;
1299     if (WebFrame* frame = WebProcess::shared().webFrame(frameID)) {
1300         if (RefPtr<LegacyWebArchive> archive = LegacyWebArchive::create(frame->coreFrame()->document())) {
1301             if ((data = archive->rawDataRepresentation()))
1302                 dataReference = CoreIPC::DataReference(CFDataGetBytePtr(data.get()), CFDataGetLength(data.get()));
1303         }
1304     }
1305 #endif
1306
1307     send(Messages::WebPageProxy::DataCallback(dataReference, callbackID));
1308 }
1309
1310 void WebPage::forceRepaintWithoutCallback()
1311 {
1312     m_drawingArea->forceRepaint();
1313 }
1314
1315 void WebPage::forceRepaint(uint64_t callbackID)
1316 {
1317     forceRepaintWithoutCallback();
1318     send(Messages::WebPageProxy::VoidCallback(callbackID));
1319 }
1320
1321 void WebPage::preferencesDidChange(const WebPreferencesStore& store)
1322 {
1323     WebPreferencesStore::removeTestRunnerOverrides();
1324     updatePreferences(store);
1325 }
1326
1327 void WebPage::updatePreferences(const WebPreferencesStore& store)
1328 {
1329     Settings* settings = m_page->settings();
1330
1331     m_tabToLinks = store.getBoolValueForKey(WebPreferencesKey::tabsToLinksKey());
1332
1333     // FIXME: This should be generated from macro expansion for all preferences,
1334     // but we currently don't match the naming of WebCore exactly so we are
1335     // handrolling the boolean and integer preferences until that is fixed.
1336
1337 #define INITIALIZE_SETTINGS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) settings->set##KeyUpper(store.get##TypeName##ValueForKey(WebPreferencesKey::KeyLower##Key()));
1338
1339     FOR_EACH_WEBKIT_STRING_PREFERENCE(INITIALIZE_SETTINGS)
1340
1341 #undef INITIALIZE_SETTINGS
1342
1343     settings->setJavaScriptEnabled(store.getBoolValueForKey(WebPreferencesKey::javaScriptEnabledKey()));
1344     settings->setLoadsImagesAutomatically(store.getBoolValueForKey(WebPreferencesKey::loadsImagesAutomaticallyKey()));
1345     settings->setPluginsEnabled(store.getBoolValueForKey(WebPreferencesKey::pluginsEnabledKey()));
1346     settings->setJavaEnabled(store.getBoolValueForKey(WebPreferencesKey::javaEnabledKey()));
1347     settings->setOfflineWebApplicationCacheEnabled(store.getBoolValueForKey(WebPreferencesKey::offlineWebApplicationCacheEnabledKey()));
1348     settings->setLocalStorageEnabled(store.getBoolValueForKey(WebPreferencesKey::localStorageEnabledKey()));
1349     settings->setXSSAuditorEnabled(store.getBoolValueForKey(WebPreferencesKey::xssAuditorEnabledKey()));
1350     settings->setFrameFlatteningEnabled(store.getBoolValueForKey(WebPreferencesKey::frameFlatteningEnabledKey()));
1351     settings->setPrivateBrowsingEnabled(store.getBoolValueForKey(WebPreferencesKey::privateBrowsingEnabledKey()));
1352     settings->setDeveloperExtrasEnabled(store.getBoolValueForKey(WebPreferencesKey::developerExtrasEnabledKey()));
1353     settings->setTextAreasAreResizable(store.getBoolValueForKey(WebPreferencesKey::textAreasAreResizableKey()));
1354     settings->setNeedsSiteSpecificQuirks(store.getBoolValueForKey(WebPreferencesKey::needsSiteSpecificQuirksKey()));
1355     settings->setJavaScriptCanOpenWindowsAutomatically(store.getBoolValueForKey(WebPreferencesKey::javaScriptCanOpenWindowsAutomaticallyKey()));
1356     settings->setForceFTPDirectoryListings(store.getBoolValueForKey(WebPreferencesKey::forceFTPDirectoryListingsKey()));
1357     settings->setDNSPrefetchingEnabled(store.getBoolValueForKey(WebPreferencesKey::dnsPrefetchingEnabledKey()));
1358 #if ENABLE(WEB_ARCHIVE)
1359     settings->setWebArchiveDebugModeEnabled(store.getBoolValueForKey(WebPreferencesKey::webArchiveDebugModeEnabledKey()));
1360 #endif
1361     settings->setLocalFileContentSniffingEnabled(store.getBoolValueForKey(WebPreferencesKey::localFileContentSniffingEnabledKey()));
1362     settings->setUsesPageCache(store.getBoolValueForKey(WebPreferencesKey::usesPageCacheKey()));
1363     settings->setAuthorAndUserStylesEnabled(store.getBoolValueForKey(WebPreferencesKey::authorAndUserStylesEnabledKey()));
1364     settings->setPaginateDuringLayoutEnabled(store.getBoolValueForKey(WebPreferencesKey::paginateDuringLayoutEnabledKey()));
1365     settings->setDOMPasteAllowed(store.getBoolValueForKey(WebPreferencesKey::domPasteAllowedKey()));
1366     settings->setJavaScriptCanAccessClipboard(store.getBoolValueForKey(WebPreferencesKey::javaScriptCanAccessClipboardKey()));
1367     settings->setShouldPrintBackgrounds(store.getBoolValueForKey(WebPreferencesKey::shouldPrintBackgroundsKey()));
1368     settings->setWebSecurityEnabled(store.getBoolValueForKey(WebPreferencesKey::webSecurityEnabledKey()));
1369     settings->setAllowUniversalAccessFromFileURLs(store.getBoolValueForKey(WebPreferencesKey::allowUniversalAccessFromFileURLsKey()));
1370     settings->setAllowFileAccessFromFileURLs(store.getBoolValueForKey(WebPreferencesKey::allowFileAccessFromFileURLsKey()));
1371
1372     settings->setMinimumFontSize(store.getUInt32ValueForKey(WebPreferencesKey::minimumFontSizeKey()));
1373     settings->setMinimumLogicalFontSize(store.getUInt32ValueForKey(WebPreferencesKey::minimumLogicalFontSizeKey()));
1374     settings->setDefaultFontSize(store.getUInt32ValueForKey(WebPreferencesKey::defaultFontSizeKey()));
1375     settings->setDefaultFixedFontSize(store.getUInt32ValueForKey(WebPreferencesKey::defaultFixedFontSizeKey()));
1376
1377 #if PLATFORM(WIN)
1378     // Temporarily turn off accelerated compositing until we have a good solution for rendering it.
1379     settings->setAcceleratedCompositingEnabled(false);
1380     settings->setAcceleratedDrawingEnabled(false);
1381     settings->setCanvasUsesAcceleratedDrawing(false);
1382 #else
1383     settings->setAcceleratedCompositingEnabled(store.getBoolValueForKey(WebPreferencesKey::acceleratedCompositingEnabledKey()));
1384     settings->setAcceleratedDrawingEnabled(store.getBoolValueForKey(WebPreferencesKey::acceleratedDrawingEnabledKey()));
1385     settings->setCanvasUsesAcceleratedDrawing(store.getBoolValueForKey(WebPreferencesKey::canvasUsesAcceleratedDrawingKey()));
1386 #endif
1387     settings->setShowDebugBorders(store.getBoolValueForKey(WebPreferencesKey::compositingBordersVisibleKey()));
1388     settings->setShowRepaintCounter(store.getBoolValueForKey(WebPreferencesKey::compositingRepaintCountersVisibleKey()));
1389     settings->setWebGLEnabled(store.getBoolValueForKey(WebPreferencesKey::webGLEnabledKey()));
1390
1391 #if ENABLE(DATABASE)
1392     AbstractDatabase::setIsAvailable(store.getBoolValueForKey(WebPreferencesKey::databasesEnabledKey()));
1393 #endif
1394
1395 #if ENABLE(FULLSCREEN_API)
1396     settings->setFullScreenEnabled(store.getBoolValueForKey(WebPreferencesKey::fullScreenEnabledKey()));
1397 #endif
1398
1399 #if ENABLE(DOM_STORAGE)
1400     settings->setLocalStorageDatabasePath(WebProcess::shared().localStorageDirectory());
1401 #endif
1402
1403     platformPreferencesDidChange(store);
1404 }
1405
1406 #if ENABLE(INSPECTOR)
1407 WebInspector* WebPage::inspector()
1408 {
1409     if (m_isClosed)
1410         return 0;
1411     if (!m_inspector)
1412         m_inspector = WebInspector::create(this);
1413     return m_inspector.get();
1414 }
1415 #endif
1416
1417 #if ENABLE(FULLSCREEN_API)
1418 WebFullScreenManager* WebPage::fullScreenManager()
1419 {
1420     if (!m_fullScreenManager)
1421         m_fullScreenManager = WebFullScreenManager::create(this);
1422     return m_fullScreenManager.get();
1423 }
1424 #endif
1425
1426 #if !PLATFORM(MAC)
1427 bool WebPage::handleEditingKeyboardEvent(KeyboardEvent* evt)
1428 {
1429     Node* node = evt->target()->toNode();
1430     ASSERT(node);
1431     Frame* frame = node->document()->frame();
1432     ASSERT(frame);
1433
1434     const PlatformKeyboardEvent* keyEvent = evt->keyEvent();
1435     if (!keyEvent)
1436         return false;
1437
1438     Editor::Command command = frame->editor()->command(interpretKeyEvent(evt));
1439
1440     if (keyEvent->type() == PlatformKeyboardEvent::RawKeyDown) {
1441         // WebKit doesn't have enough information about mode to decide how commands that just insert text if executed via Editor should be treated,
1442         // so we leave it upon WebCore to either handle them immediately (e.g. Tab that changes focus) or let a keypress event be generated
1443         // (e.g. Tab that inserts a Tab character, or Enter).
1444         return !command.isTextInsertion() && command.execute(evt);
1445     }
1446
1447     if (command.execute(evt))
1448         return true;
1449
1450     // Don't insert null or control characters as they can result in unexpected behaviour
1451     if (evt->charCode() < ' ')
1452         return false;
1453
1454     return frame->editor()->insertText(evt->keyEvent()->text(), evt);
1455 }
1456 #endif
1457
1458 #if PLATFORM(WIN)
1459 void WebPage::performDragControllerAction(uint64_t action, WebCore::IntPoint clientPosition, WebCore::IntPoint globalPosition, uint64_t draggingSourceOperationMask, const WebCore::DragDataMap& dataMap, uint32_t flags)
1460 {
1461     if (!m_page) {
1462         send(Messages::WebPageProxy::DidPerformDragControllerAction(DragOperationNone));
1463         return;
1464     }
1465
1466     DragData dragData(dataMap, clientPosition, globalPosition, static_cast<DragOperation>(draggingSourceOperationMask), static_cast<DragApplicationFlags>(flags));
1467     switch (action) {
1468     case DragControllerActionEntered:
1469         send(Messages::WebPageProxy::DidPerformDragControllerAction(m_page->dragController()->dragEntered(&dragData)));
1470         break;
1471
1472     case DragControllerActionUpdated:
1473         send(Messages::WebPageProxy::DidPerformDragControllerAction(m_page->dragController()->dragUpdated(&dragData)));
1474         break;
1475         
1476     case DragControllerActionExited:
1477         m_page->dragController()->dragExited(&dragData);
1478         break;
1479         
1480     case DragControllerActionPerformDrag:
1481         m_page->dragController()->performDrag(&dragData);
1482         break;
1483         
1484     default:
1485         ASSERT_NOT_REACHED();
1486     }
1487 }
1488 #else
1489 void WebPage::performDragControllerAction(uint64_t action, WebCore::IntPoint clientPosition, WebCore::IntPoint globalPosition, uint64_t draggingSourceOperationMask, const String& dragStorageName, uint32_t flags)
1490 {
1491     if (!m_page) {
1492         send(Messages::WebPageProxy::DidPerformDragControllerAction(DragOperationNone));
1493         return;
1494     }
1495
1496     DragData dragData(dragStorageName, clientPosition, globalPosition, static_cast<DragOperation>(draggingSourceOperationMask), static_cast<DragApplicationFlags>(flags));
1497     switch (action) {
1498     case DragControllerActionEntered:
1499         send(Messages::WebPageProxy::DidPerformDragControllerAction(m_page->dragController()->dragEntered(&dragData)));
1500         break;
1501
1502     case DragControllerActionUpdated:
1503         send(Messages::WebPageProxy::DidPerformDragControllerAction(m_page->dragController()->dragUpdated(&dragData)));
1504         break;
1505         
1506     case DragControllerActionExited:
1507         m_page->dragController()->dragExited(&dragData);
1508         break;
1509         
1510     case DragControllerActionPerformDrag:
1511         m_page->dragController()->performDrag(&dragData);
1512         break;
1513         
1514     default:
1515         ASSERT_NOT_REACHED();
1516     }
1517 }
1518 #endif
1519
1520 void WebPage::dragEnded(WebCore::IntPoint clientPosition, WebCore::IntPoint globalPosition, uint64_t operation)
1521 {
1522     IntPoint adjustedClientPosition(clientPosition.x() + m_page->dragController()->dragOffset().x(), clientPosition.y() + m_page->dragController()->dragOffset().y());
1523     IntPoint adjustedGlobalPosition(globalPosition.x() + m_page->dragController()->dragOffset().x(), globalPosition.y() + m_page->dragController()->dragOffset().y());
1524     
1525     platformDragEnded();
1526     m_page->dragController()->dragEnded();
1527     FrameView* view = m_page->mainFrame()->view();
1528     if (!view)
1529         return;
1530     // FIXME: These are fake modifier keys here, but they should be real ones instead.
1531     PlatformMouseEvent event(adjustedClientPosition, adjustedGlobalPosition, LeftButton, MouseEventMoved, 0, false, false, false, false, currentTime());
1532     m_page->mainFrame()->eventHandler()->dragSourceEndedAt(event, (DragOperation)operation);
1533 }
1534
1535 WebEditCommand* WebPage::webEditCommand(uint64_t commandID)
1536 {
1537     return m_editCommandMap.get(commandID).get();
1538 }
1539
1540 void WebPage::addWebEditCommand(uint64_t commandID, WebEditCommand* command)
1541 {
1542     m_editCommandMap.set(commandID, command);
1543 }
1544
1545 void WebPage::removeWebEditCommand(uint64_t commandID)
1546 {
1547     m_editCommandMap.remove(commandID);
1548 }
1549
1550 void WebPage::unapplyEditCommand(uint64_t commandID)
1551 {
1552     WebEditCommand* command = webEditCommand(commandID);
1553     if (!command)
1554         return;
1555
1556     command->command()->unapply();
1557 }
1558
1559 void WebPage::reapplyEditCommand(uint64_t commandID)
1560 {
1561     WebEditCommand* command = webEditCommand(commandID);
1562     if (!command)
1563         return;
1564
1565     m_isInRedo = true;
1566     command->command()->reapply();
1567     m_isInRedo = false;
1568 }
1569
1570 void WebPage::didRemoveEditCommand(uint64_t commandID)
1571 {
1572     removeWebEditCommand(commandID);
1573 }
1574
1575 void WebPage::setActivePopupMenu(WebPopupMenu* menu)
1576 {
1577     m_activePopupMenu = menu;
1578 }
1579
1580 void WebPage::setActiveOpenPanelResultListener(PassRefPtr<WebOpenPanelResultListener> openPanelResultListener)
1581 {
1582     m_activeOpenPanelResultListener = openPanelResultListener;
1583 }
1584
1585 bool WebPage::findStringFromInjectedBundle(const String& target, FindOptions options)
1586 {
1587     return m_page->findString(target, options);
1588 }
1589
1590 void WebPage::findString(const String& string, uint32_t options, uint32_t maxMatchCount)
1591 {
1592     m_findController.findString(string, static_cast<FindOptions>(options), maxMatchCount);
1593 }
1594
1595 void WebPage::hideFindUI()
1596 {
1597     m_findController.hideFindUI();
1598 }
1599
1600 void WebPage::countStringMatches(const String& string, uint32_t options, uint32_t maxMatchCount)
1601 {
1602     m_findController.countStringMatches(string, static_cast<FindOptions>(options), maxMatchCount);
1603 }
1604
1605 void WebPage::didChangeSelectedIndexForActivePopupMenu(int32_t newIndex)
1606 {
1607     if (!m_activePopupMenu)
1608         return;
1609
1610     m_activePopupMenu->didChangeSelectedIndex(newIndex);
1611     m_activePopupMenu = 0;
1612 }
1613
1614 void WebPage::didChooseFilesForOpenPanel(const Vector<String>& files)
1615 {
1616     if (!m_activeOpenPanelResultListener)
1617         return;
1618
1619     m_activeOpenPanelResultListener->didChooseFiles(files);
1620     m_activeOpenPanelResultListener = 0;
1621 }
1622
1623 void WebPage::didCancelForOpenPanel()
1624 {
1625     m_activeOpenPanelResultListener = 0;
1626 }
1627
1628 #if ENABLE(WEB_PROCESS_SANDBOX)
1629 void WebPage::extendSandboxForFileFromOpenPanel(const SandboxExtension::Handle& handle)
1630 {
1631     SandboxExtension::create(handle)->consumePermanently();
1632 }
1633 #endif
1634
1635 void WebPage::didReceiveGeolocationPermissionDecision(uint64_t geolocationID, bool allowed)
1636 {
1637     m_geolocationPermissionRequestManager.didReceiveGeolocationPermissionDecision(geolocationID, allowed);
1638 }
1639
1640 void WebPage::advanceToNextMisspelling(bool startBeforeSelection)
1641 {
1642     Frame* frame = m_page->focusController()->focusedOrMainFrame();
1643     frame->editor()->advanceToNextMisspelling(startBeforeSelection);
1644 }
1645
1646 void WebPage::changeSpellingToWord(const String& word)
1647 {
1648     replaceSelectionWithText(m_page->focusController()->focusedOrMainFrame(), word);
1649 }
1650
1651 void WebPage::unmarkAllMisspellings()
1652 {
1653     for (Frame* frame = m_page->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
1654         if (Document* document = frame->document())
1655             document->markers()->removeMarkers(DocumentMarker::Spelling);
1656     }
1657 }
1658
1659 void WebPage::unmarkAllBadGrammar()
1660 {
1661     for (Frame* frame = m_page->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
1662         if (Document* document = frame->document())
1663             document->markers()->removeMarkers(DocumentMarker::Grammar);
1664     }
1665 }
1666
1667 #if PLATFORM(MAC)
1668 void WebPage::uppercaseWord()
1669 {
1670     m_page->focusController()->focusedOrMainFrame()->editor()->uppercaseWord();
1671 }
1672
1673 void WebPage::lowercaseWord()
1674 {
1675     m_page->focusController()->focusedOrMainFrame()->editor()->lowercaseWord();
1676 }
1677
1678 void WebPage::capitalizeWord()
1679 {
1680     m_page->focusController()->focusedOrMainFrame()->editor()->capitalizeWord();
1681 }
1682 #endif
1683     
1684 void WebPage::setTextForActivePopupMenu(int32_t index)
1685 {
1686     if (!m_activePopupMenu)
1687         return;
1688
1689     m_activePopupMenu->setTextForIndex(index);
1690 }
1691
1692 void WebPage::didSelectItemFromActiveContextMenu(const WebContextMenuItemData& item)
1693 {
1694     ASSERT(m_contextMenu);
1695     m_contextMenu->itemSelected(item);
1696     m_contextMenu = 0;
1697 }
1698
1699 void WebPage::replaceSelectionWithText(Frame* frame, const String& text)
1700 {
1701     if (frame->selection()->isNone())
1702         return;
1703
1704     RefPtr<DocumentFragment> textFragment = createFragmentFromText(frame->selection()->toNormalizedRange().get(), text);
1705     applyCommand(ReplaceSelectionCommand::create(frame->document(), textFragment.release(), ReplaceSelectionCommand::SelectReplacement | ReplaceSelectionCommand::MatchStyle | ReplaceSelectionCommand::PreventNesting));
1706     frame->selection()->revealSelection(ScrollAlignment::alignToEdgeIfNeeded);
1707 }
1708
1709 bool WebPage::mainFrameHasCustomRepresentation() const
1710 {
1711     return static_cast<WebFrameLoaderClient*>(mainFrame()->coreFrame()->loader()->client())->frameHasCustomRepresentation();
1712 }
1713
1714 void WebPage::didChangeScrollOffsetForMainFrame()
1715 {
1716     Frame* frame = m_page->mainFrame();
1717     IntPoint scrollPosition = frame->view()->scrollPosition();
1718     IntPoint maximumScrollPosition = frame->view()->maximumScrollPosition();
1719
1720     bool isPinnedToLeftSide = (scrollPosition.x() <= 0);
1721     bool isPinnedToRightSide = (scrollPosition.x() >= maximumScrollPosition.x());
1722
1723     if (isPinnedToLeftSide != m_cachedMainFrameIsPinnedToLeftSide || isPinnedToRightSide != m_cachedMainFrameIsPinnedToRightSide) {
1724         send(Messages::WebPageProxy::DidChangeScrollOffsetPinningForMainFrame(isPinnedToLeftSide, isPinnedToRightSide));
1725         
1726         m_cachedMainFrameIsPinnedToLeftSide = isPinnedToLeftSide;
1727         m_cachedMainFrameIsPinnedToRightSide = isPinnedToRightSide;
1728     }
1729 }
1730
1731 #if PLATFORM(MAC)
1732
1733 void WebPage::addPluginView(PluginView* pluginView)
1734 {
1735     ASSERT(!m_pluginViews.contains(pluginView));
1736
1737     m_pluginViews.add(pluginView);
1738 }
1739
1740 void WebPage::removePluginView(PluginView* pluginView)
1741 {
1742     ASSERT(m_pluginViews.contains(pluginView));
1743
1744     m_pluginViews.remove(pluginView);
1745 }
1746
1747 void WebPage::setWindowIsVisible(bool windowIsVisible)
1748 {
1749     m_windowIsVisible = windowIsVisible;
1750
1751     // Tell all our plug-in views that the window visibility changed.
1752     for (HashSet<PluginView*>::const_iterator it = m_pluginViews.begin(), end = m_pluginViews.end(); it != end; ++it)
1753         (*it)->setWindowIsVisible(windowIsVisible);
1754 }
1755
1756 void WebPage::windowAndViewFramesChanged(const WebCore::IntRect& windowFrameInScreenCoordinates, const WebCore::IntRect& viewFrameInWindowCoordinates, const WebCore::IntPoint& accessibilityViewCoordinates)
1757 {
1758     m_windowFrameInScreenCoordinates = windowFrameInScreenCoordinates;
1759     m_viewFrameInWindowCoordinates = viewFrameInWindowCoordinates;
1760     m_accessibilityPosition = accessibilityViewCoordinates;
1761     
1762     // Tell all our plug-in views that the window and view frames have changed.
1763     for (HashSet<PluginView*>::const_iterator it = m_pluginViews.begin(), end = m_pluginViews.end(); it != end; ++it)
1764         (*it)->windowAndViewFramesChanged(windowFrameInScreenCoordinates, viewFrameInWindowCoordinates);
1765 }
1766
1767 #endif
1768     
1769 bool WebPage::windowIsFocused() const
1770 {
1771     return m_page->focusController()->isActive();
1772 }
1773     
1774 void WebPage::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments)
1775 {
1776     if (messageID.is<CoreIPC::MessageClassDrawingAreaLegacy>()) {
1777         if (m_drawingArea)
1778             m_drawingArea->didReceiveMessage(connection, messageID, arguments);
1779         return;
1780     }
1781
1782 #if PLATFORM(MAC) || PLATFORM(WIN)
1783     if (messageID.is<CoreIPC::MessageClassDrawingArea>()) {
1784         if (m_drawingArea)
1785             m_drawingArea->didReceiveDrawingAreaMessage(connection, messageID, arguments);
1786         return;
1787     }
1788 #endif
1789     
1790 #if ENABLE(INSPECTOR)
1791     if (messageID.is<CoreIPC::MessageClassWebInspector>()) {
1792         if (WebInspector* inspector = this->inspector())
1793             inspector->didReceiveWebInspectorMessage(connection, messageID, arguments);
1794         return;
1795     }
1796 #endif
1797
1798 #if ENABLE(FULLSCREEN_API)
1799     if (messageID.is<CoreIPC::MessageClassWebFullScreenManager>()) {
1800         fullScreenManager()->didReceiveMessage(connection, messageID, arguments);
1801         return;
1802     }
1803 #endif
1804
1805     didReceiveWebPageMessage(connection, messageID, arguments);
1806 }
1807
1808 CoreIPC::SyncReplyMode WebPage::didReceiveSyncMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments, CoreIPC::ArgumentEncoder* reply)
1809 {   
1810     return didReceiveSyncWebPageMessage(connection, messageID, arguments, reply);
1811 }
1812     
1813 InjectedBundleBackForwardList* WebPage::backForwardList()
1814 {
1815     if (!m_backForwardList)
1816         m_backForwardList = InjectedBundleBackForwardList::create(this);
1817     return m_backForwardList.get();
1818 }
1819
1820 #if PLATFORM(QT)
1821 void WebPage::findZoomableAreaForPoint(const WebCore::IntPoint& point)
1822 {
1823     const int minimumZoomTargetWidth = 100;
1824
1825     Frame* mainframe = m_mainFrame->coreFrame();
1826     HitTestResult result = mainframe->eventHandler()->hitTestResultAtPoint(mainframe->view()->windowToContents(point), /*allowShadowContent*/ false, /*ignoreClipping*/ true);
1827
1828     Node* node = result.innerNode();
1829     while (node && node->getRect().width() < minimumZoomTargetWidth)
1830         node = node->parentNode();
1831
1832     IntRect zoomableArea;
1833     if (node)
1834         zoomableArea = node->getRect();
1835     send(Messages::WebPageProxy::DidFindZoomableArea(zoomableArea));
1836 }
1837 #endif
1838
1839 WebPage::SandboxExtensionTracker::~SandboxExtensionTracker()
1840 {
1841     invalidate();
1842 }
1843
1844 void WebPage::SandboxExtensionTracker::invalidate()
1845 {
1846     if (m_pendingProvisionalSandboxExtension) {
1847         m_pendingProvisionalSandboxExtension->invalidate();
1848         m_pendingProvisionalSandboxExtension = 0;
1849     }
1850
1851     if (m_provisionalSandboxExtension) {
1852         m_provisionalSandboxExtension->invalidate();
1853         m_provisionalSandboxExtension = 0;
1854     }
1855
1856     if (m_committedSandboxExtension) {
1857         m_committedSandboxExtension->invalidate();
1858         m_committedSandboxExtension = 0;
1859     }
1860 }
1861
1862 void WebPage::SandboxExtensionTracker::beginLoad(WebFrame* frame, const SandboxExtension::Handle& handle)
1863 {
1864     ASSERT(frame->isMainFrame());
1865
1866     // If we get two beginLoad calls in succession, without a provisional load starting, then
1867     // m_pendingProvisionalSandboxExtension will be non-null. Invalidate and null out the extension if that is the case.
1868     if (m_pendingProvisionalSandboxExtension) {
1869         m_pendingProvisionalSandboxExtension->invalidate();
1870         m_pendingProvisionalSandboxExtension = nullptr;
1871     }
1872         
1873     m_pendingProvisionalSandboxExtension = SandboxExtension::create(handle);
1874 }
1875
1876 static bool shouldReuseCommittedSandboxExtension(WebFrame* frame)
1877 {
1878     ASSERT(frame->isMainFrame());
1879
1880     FrameLoader* frameLoader = frame->coreFrame()->loader();
1881     FrameLoadType frameLoadType = frameLoader->loadType();
1882
1883     // If the page is being reloaded, it should reuse whatever extension is committed.
1884     if (frameLoadType == FrameLoadTypeReload || frameLoadType == FrameLoadTypeReloadFromOrigin)
1885         return true;
1886
1887     DocumentLoader* documentLoader = frameLoader->documentLoader();
1888     DocumentLoader* provisionalDocumentLoader = frameLoader->provisionalDocumentLoader();
1889     if (!documentLoader || !provisionalDocumentLoader)
1890         return false;
1891
1892     if (documentLoader->url().isLocalFile() && provisionalDocumentLoader->url().isLocalFile() 
1893         && provisionalDocumentLoader->triggeringAction().type() == NavigationTypeLinkClicked)
1894         return true;
1895
1896     return false;
1897 }
1898
1899 void WebPage::SandboxExtensionTracker::didStartProvisionalLoad(WebFrame* frame)
1900 {
1901     if (!frame->isMainFrame())
1902         return;
1903
1904     if (shouldReuseCommittedSandboxExtension(frame)) {
1905         m_pendingProvisionalSandboxExtension = m_committedSandboxExtension.release();
1906         ASSERT(!m_committedSandboxExtension);
1907     }
1908
1909     ASSERT(!m_provisionalSandboxExtension);
1910
1911     m_provisionalSandboxExtension = m_pendingProvisionalSandboxExtension.release();
1912     if (!m_provisionalSandboxExtension)
1913         return;
1914
1915     m_provisionalSandboxExtension->consume();
1916 }
1917
1918 void WebPage::SandboxExtensionTracker::didCommitProvisionalLoad(WebFrame* frame)
1919 {
1920     if (!frame->isMainFrame())
1921         return;
1922     
1923     ASSERT(!m_pendingProvisionalSandboxExtension);
1924
1925     // The provisional load has been committed. Invalidate the currently committed sandbox
1926     // extension and make the provisional sandbox extension the committed sandbox extension.
1927     if (m_committedSandboxExtension)
1928         m_committedSandboxExtension->invalidate();
1929
1930     m_committedSandboxExtension = m_provisionalSandboxExtension.release();
1931 }
1932
1933 void WebPage::SandboxExtensionTracker::didFailProvisionalLoad(WebFrame* frame)
1934 {
1935     if (!frame->isMainFrame())
1936         return;
1937
1938     ASSERT(!m_pendingProvisionalSandboxExtension);
1939     if (!m_provisionalSandboxExtension)
1940         return;
1941
1942     m_provisionalSandboxExtension->invalidate();
1943     m_provisionalSandboxExtension = nullptr;
1944 }
1945
1946 bool WebPage::hasLocalDataForURL(const KURL& url)
1947 {
1948     if (url.isLocalFile())
1949         return true;
1950
1951     FrameLoader* frameLoader = m_page->mainFrame()->loader();
1952     DocumentLoader* documentLoader = frameLoader ? frameLoader->documentLoader() : 0;
1953     if (documentLoader && documentLoader->subresource(url))
1954         return true;
1955
1956     return platformHasLocalDataForURL(url);
1957 }
1958
1959 void WebPage::setCustomTextEncodingName(const String& encoding)
1960 {
1961     m_page->mainFrame()->loader()->reloadWithOverrideEncoding(encoding);
1962 }
1963
1964 void WebPage::didRemoveBackForwardItem(uint64_t itemID)
1965 {
1966     WebBackForwardListProxy::removeItem(itemID);
1967 }
1968
1969 #if PLATFORM(MAC)
1970
1971 bool WebPage::isSpeaking()
1972 {
1973     bool result;
1974     return sendSync(Messages::WebPageProxy::GetIsSpeaking(), Messages::WebPageProxy::GetIsSpeaking::Reply(result)) && result;
1975 }
1976
1977 void WebPage::speak(const String& string)
1978 {
1979     send(Messages::WebPageProxy::Speak(string));
1980 }
1981
1982 void WebPage::stopSpeaking()
1983 {
1984     send(Messages::WebPageProxy::StopSpeaking());
1985 }
1986
1987 #endif
1988
1989 void WebPage::beginPrinting(uint64_t frameID, const PrintInfo& printInfo)
1990 {
1991     WebFrame* frame = WebProcess::shared().webFrame(frameID);
1992     if (!frame)
1993         return;
1994
1995     Frame* coreFrame = frame->coreFrame();
1996     if (!coreFrame)
1997         return;
1998
1999     if (!m_printContext)
2000         m_printContext = adoptPtr(new PrintContext(coreFrame));
2001
2002     m_printContext->begin(printInfo.availablePaperWidth, printInfo.availablePaperHeight);
2003
2004     float fullPageHeight;
2005     m_printContext->computePageRects(FloatRect(0, 0, printInfo.availablePaperWidth, printInfo.availablePaperHeight), 0, 0, printInfo.pageSetupScaleFactor, fullPageHeight, true);
2006 }
2007
2008 void WebPage::endPrinting()
2009 {
2010     m_printContext = nullptr;
2011 }
2012
2013 void WebPage::computePagesForPrinting(uint64_t frameID, const PrintInfo& printInfo, uint64_t callbackID)
2014 {
2015     Vector<IntRect> resultPageRects;
2016     double resultTotalScaleFactorForPrinting = 1;
2017
2018     beginPrinting(frameID, printInfo);
2019
2020     if (m_printContext) {
2021         resultPageRects = m_printContext->pageRects();
2022         resultTotalScaleFactorForPrinting = m_printContext->computeAutomaticScaleFactor(FloatSize(printInfo.availablePaperWidth, printInfo.availablePaperHeight)) * printInfo.pageSetupScaleFactor;
2023     }
2024
2025     // If we're asked to print, we should actually print at least a blank page.
2026     if (resultPageRects.isEmpty())
2027         resultPageRects.append(IntRect(0, 0, 1, 1));
2028
2029     send(Messages::WebPageProxy::ComputedPagesCallback(resultPageRects, resultTotalScaleFactorForPrinting, callbackID));
2030 }
2031
2032 #if PLATFORM(MAC) || PLATFORM(WIN)
2033 void WebPage::drawRectToPDF(uint64_t frameID, const WebCore::IntRect& rect, uint64_t callbackID)
2034 {
2035     WebFrame* frame = WebProcess::shared().webFrame(frameID);
2036     Frame* coreFrame = frame ? frame->coreFrame() : 0;
2037
2038     RetainPtr<CFMutableDataRef> pdfPageData(AdoptCF, CFDataCreateMutable(0, 0));
2039
2040     if (coreFrame) {
2041         ASSERT(coreFrame->document()->printing());
2042
2043 #if PLATFORM(CG)
2044         // FIXME: Use CGDataConsumerCreate with callbacks to avoid copying the data.
2045         RetainPtr<CGDataConsumerRef> pdfDataConsumer(AdoptCF, CGDataConsumerCreateWithCFData(pdfPageData.get()));
2046
2047         CGRect mediaBox = CGRectMake(0, 0, rect.width(), rect.height());
2048         RetainPtr<CGContextRef> context(AdoptCF, CGPDFContextCreate(pdfDataConsumer.get(), &mediaBox, 0));
2049         RetainPtr<CFDictionaryRef> pageInfo(AdoptCF, CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
2050         CGPDFContextBeginPage(context.get(), pageInfo.get());
2051
2052         GraphicsContext ctx(context.get());
2053         ctx.scale(FloatSize(1, -1));
2054         ctx.translate(0, -rect.height());
2055         m_printContext->spoolRect(ctx, rect);
2056
2057         CGPDFContextEndPage(context.get());
2058         CGPDFContextClose(context.get());
2059 #endif
2060     }
2061
2062     send(Messages::WebPageProxy::DataCallback(CoreIPC::DataReference(CFDataGetBytePtr(pdfPageData.get()), CFDataGetLength(pdfPageData.get())), callbackID));
2063 }
2064
2065 void WebPage::drawPagesToPDF(uint64_t frameID, uint32_t first, uint32_t count, uint64_t callbackID)
2066 {
2067     WebFrame* frame = WebProcess::shared().webFrame(frameID);
2068     Frame* coreFrame = frame ? frame->coreFrame() : 0;
2069
2070     RetainPtr<CFMutableDataRef> pdfPageData(AdoptCF, CFDataCreateMutable(0, 0));
2071
2072     if (coreFrame) {
2073         ASSERT(coreFrame->document()->printing());
2074
2075 #if PLATFORM(CG)
2076         // FIXME: Use CGDataConsumerCreate with callbacks to avoid copying the data.
2077         RetainPtr<CGDataConsumerRef> pdfDataConsumer(AdoptCF, CGDataConsumerCreateWithCFData(pdfPageData.get()));
2078
2079         CGRect mediaBox = m_printContext->pageCount() ? m_printContext->pageRect(0) : CGRectMake(0, 0, 1, 1);
2080         RetainPtr<CGContextRef> context(AdoptCF, CGPDFContextCreate(pdfDataConsumer.get(), &mediaBox, 0));
2081         for (uint32_t page = first; page < first + count; ++page) {
2082             if (page >= m_printContext->pageCount())
2083                 break;
2084
2085             RetainPtr<CFDictionaryRef> pageInfo(AdoptCF, CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
2086             CGPDFContextBeginPage(context.get(), pageInfo.get());
2087
2088             GraphicsContext ctx(context.get());
2089             ctx.scale(FloatSize(1, -1));
2090             ctx.translate(0, -m_printContext->pageRect(page).height());
2091             m_printContext->spoolPage(ctx, page, m_printContext->pageRect(page).width());
2092
2093             CGPDFContextEndPage(context.get());
2094         }
2095         CGPDFContextClose(context.get());
2096 #endif
2097     }
2098
2099     send(Messages::WebPageProxy::DataCallback(CoreIPC::DataReference(CFDataGetBytePtr(pdfPageData.get()), CFDataGetLength(pdfPageData.get())), callbackID));
2100 }
2101 #endif
2102
2103 void WebPage::runModal()
2104 {
2105     if (m_isClosed)
2106         return;
2107     if (m_isRunningModal)
2108         return;
2109
2110     m_isRunningModal = true;
2111     send(Messages::WebPageProxy::RunModal());
2112     RunLoop::run();
2113     ASSERT(!m_isRunningModal);
2114 }
2115
2116 void WebPage::setMemoryCacheMessagesEnabled(bool memoryCacheMessagesEnabled)
2117 {
2118     m_page->setMemoryCacheClientCallsEnabled(memoryCacheMessagesEnabled);
2119 }
2120
2121 #if !PLATFORM(MAC)
2122 void WebPage::platformDragEnded()
2123 {
2124 }
2125 #endif
2126
2127 bool WebPage::canHandleRequest(const WebCore::ResourceRequest& request)
2128 {
2129     if (SchemeRegistry::shouldLoadURLSchemeAsEmptyDocument(request.url().protocol()))
2130         return true;
2131     return platformCanHandleRequest(request);
2132 }
2133
2134 } // namespace WebKit