cf44ee40f8645a39358db2dcab5e827750505c9e
[WebKit-https.git] / 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 "WebPage.h"
27
28 #include "Arguments.h"
29 #include "DataReference.h"
30 #include "DecoderAdapter.h"
31 #include "DrawingArea.h"
32 #include "InjectedBundle.h"
33 #include "InjectedBundleBackForwardList.h"
34 #include "MessageID.h"
35 #include "NetscapePlugin.h"
36 #include "PageOverlay.h"
37 #include "PluginProxy.h"
38 #include "PluginView.h"
39 #include "SessionState.h"
40 #include "WebBackForwardList.h"
41 #include "WebBackForwardListItem.h"
42 #include "WebBackForwardListProxy.h"
43 #include "WebChromeClient.h"
44 #include "WebContextMenu.h"
45 #include "WebContextMenuClient.h"
46 #include "WebContextMessages.h"
47 #include "WebCoreArgumentCoders.h"
48 #include "WebDragClient.h"
49 #include "WebEditorClient.h"
50 #include "WebEvent.h"
51 #include "WebEventConversion.h"
52 #include "WebFrame.h"
53 #include "WebImage.h"
54 #include "WebInspector.h"
55 #include "WebInspectorClient.h"
56 #include "WebOpenPanelResultListener.h"
57 #include "WebPageCreationParameters.h"
58 #include "WebPageGroupProxy.h"
59 #include "WebPageProxyMessages.h"
60 #include "WebPopupMenu.h"
61 #include "WebPreferencesStore.h"
62 #include "WebProcess.h"
63 #include "WebProcessProxyMessageKinds.h"
64 #include "WebProcessProxyMessages.h"
65 #include <WebCore/AbstractDatabase.h>
66 #include <WebCore/AnimationTimeController.h>
67 #include <WebCore/ArchiveResource.h>
68 #include <WebCore/Chrome.h>
69 #include <WebCore/ContextMenuController.h>
70 #include <WebCore/DocumentFragment.h>
71 #include <WebCore/DocumentLoader.h>
72 #include <WebCore/DocumentMarkerController.h>
73 #include <WebCore/EventHandler.h>
74 #include <WebCore/FocusController.h>
75 #include <WebCore/Frame.h>
76 #include <WebCore/FrameLoaderTypes.h>
77 #include <WebCore/FrameView.h>
78 #include <WebCore/HistoryItem.h>
79 #include <WebCore/KeyboardEvent.h>
80 #include <WebCore/Page.h>
81 #include <WebCore/PlatformKeyboardEvent.h>
82 #include <WebCore/RenderTreeAsText.h>
83 #include <WebCore/ReplaceSelectionCommand.h>
84 #include <WebCore/ResourceRequest.h>
85 #include <WebCore/Settings.h>
86 #include <WebCore/SharedBuffer.h>
87 #include <WebCore/SubstituteData.h>
88 #include <WebCore/TextIterator.h>
89 #include <WebCore/markup.h>
90 #include <runtime/JSLock.h>
91 #include <runtime/JSValue.h>
92
93 #if PLATFORM(MAC) || PLATFORM(WIN)
94 #include <WebCore/LegacyWebArchive.h>
95 #endif
96
97 #if ENABLE(PLUGIN_PROCESS)
98 // FIXME: This is currently Mac-specific!
99 #include "MachPort.h"
100 #endif
101
102 #if PLATFORM(QT)
103 #include "HitTestResult.h"
104 #endif
105
106 #ifndef NDEBUG
107 #include <wtf/RefCountedLeakCounter.h>
108 #endif
109
110 using namespace JSC;
111 using namespace WebCore;
112
113 namespace WebKit {
114
115 #ifndef NDEBUG
116 static WTF::RefCountedLeakCounter webPageCounter("WebPage");
117 #endif
118
119 PassRefPtr<WebPage> WebPage::create(uint64_t pageID, const WebPageCreationParameters& parameters)
120 {
121     RefPtr<WebPage> page = adoptRef(new WebPage(pageID, parameters));
122
123     if (page->pageGroup()->isVisibleToInjectedBundle() && WebProcess::shared().injectedBundle())
124         WebProcess::shared().injectedBundle()->didCreatePage(page.get());
125
126     return page.release();
127 }
128
129 WebPage::WebPage(uint64_t pageID, const WebPageCreationParameters& parameters)
130     : m_viewSize(parameters.viewSize)
131     , m_drawsBackground(true)
132     , m_drawsTransparentBackground(false)
133     , m_isInRedo(false)
134     , m_isClosed(false)
135     , m_tabToLinks(false)
136 #if PLATFORM(MAC)
137     , m_windowIsVisible(false)
138     , m_isSmartInsertDeleteEnabled(parameters.isSmartInsertDeleteEnabled)
139 #elif PLATFORM(WIN)
140     , m_nativeWindow(parameters.nativeWindow)
141 #endif
142     , m_findController(this)
143     , m_pageID(pageID)
144 {
145     ASSERT(m_pageID);
146
147     Page::PageClients pageClients;
148     pageClients.chromeClient = new WebChromeClient(this);
149     pageClients.contextMenuClient = new WebContextMenuClient(this);
150     pageClients.editorClient = new WebEditorClient(this);
151     pageClients.dragClient = new WebDragClient(this);
152 #if ENABLE(INSPECTOR)
153     pageClients.inspectorClient = new WebInspectorClient(this);
154 #endif
155     pageClients.backForwardClient = WebBackForwardListProxy::create(this);
156     m_page = adoptPtr(new Page(pageClients));
157
158     // Qt does not yet call setIsInWindow. Until it does, just leave
159     // this line out so plug-ins and video will work. Eventually all platforms
160     // should call setIsInWindow and this comment and #if should be removed,
161     // leaving behind the setCanStartMedia call.
162 #if !PLATFORM(QT)
163     m_page->setCanStartMedia(false);
164 #endif
165
166     updatePreferences(parameters.store);
167
168     m_pageGroup = WebProcess::shared().webPageGroup(parameters.pageGroupData);
169     m_page->setGroupName(m_pageGroup->identifier());
170
171     platformInitialize();
172     Settings::setMinDOMTimerInterval(0.004);
173
174     m_drawingArea = DrawingArea::create(parameters.drawingAreaInfo.type, parameters.drawingAreaInfo.identifier, this);
175     m_mainFrame = WebFrame::createMainFrame(this);
176
177     setDrawsBackground(parameters.drawsBackground);
178     setDrawsTransparentBackground(parameters.drawsTransparentBackground);
179
180     m_userAgent = parameters.userAgent;
181
182 #ifndef NDEBUG
183     webPageCounter.increment();
184 #endif
185 }
186
187 WebPage::~WebPage()
188 {
189     if (m_backForwardList)
190         m_backForwardList->detach();
191
192     ASSERT(!m_page);
193
194     m_sandboxExtensionTracker.invalidate();
195
196 #if PLATFORM(MAC)
197     ASSERT(m_pluginViews.isEmpty());
198 #endif
199
200 #ifndef NDEBUG
201     webPageCounter.decrement();
202 #endif
203 }
204
205 void WebPage::dummy(bool&)
206 {
207 }
208
209 CoreIPC::Connection* WebPage::connection() const
210 {
211     return WebProcess::shared().connection();
212 }
213
214 void WebPage::initializeInjectedBundleContextMenuClient(WKBundlePageContextMenuClient* client)
215 {
216     m_contextMenuClient.initialize(client);
217 }
218
219 void WebPage::initializeInjectedBundleEditorClient(WKBundlePageEditorClient* client)
220 {
221     m_editorClient.initialize(client);
222 }
223
224 void WebPage::initializeInjectedBundleFormClient(WKBundlePageFormClient* client)
225 {
226     m_formClient.initialize(client);
227 }
228
229 void WebPage::initializeInjectedBundleLoaderClient(WKBundlePageLoaderClient* client)
230 {
231     m_loaderClient.initialize(client);
232 }
233
234 void WebPage::initializeInjectedBundleUIClient(WKBundlePageUIClient* client)
235 {
236     m_uiClient.initialize(client);
237 }
238
239 PassRefPtr<Plugin> WebPage::createPlugin(const Plugin::Parameters& parameters)
240 {
241     String pluginPath;
242
243     if (!WebProcess::shared().connection()->sendSync(
244             Messages::WebContext::GetPluginPath(parameters.mimeType, parameters.url.string()), 
245             Messages::WebContext::GetPluginPath::Reply(pluginPath), 0)) {
246         return 0;
247     }
248
249     if (pluginPath.isNull())
250         return 0;
251
252 #if ENABLE(PLUGIN_PROCESS)
253     return PluginProxy::create(pluginPath);
254 #else
255     return NetscapePlugin::create(NetscapePluginModule::getOrCreate(pluginPath));
256 #endif
257 }
258
259 String WebPage::renderTreeExternalRepresentation() const
260 {
261     return externalRepresentation(m_mainFrame->coreFrame(), RenderAsTextBehaviorNormal);
262 }
263
264 void WebPage::executeEditingCommand(const String& commandName, const String& argument)
265 {
266     Frame* frame = m_page->focusController()->focusedOrMainFrame();
267     if (!frame)
268         return;
269     frame->editor()->command(commandName).execute(argument);
270 }
271
272 bool WebPage::isEditingCommandEnabled(const String& commandName)
273 {
274     Frame* frame = m_page->focusController()->focusedOrMainFrame();
275     if (!frame)
276         return false;
277     
278     Editor::Command command = frame->editor()->command(commandName);
279     return command.isSupported() && command.isEnabled();
280 }
281     
282 void WebPage::clearMainFrameName()
283 {
284     mainFrame()->coreFrame()->tree()->clearName();
285 }
286
287 #if USE(ACCELERATED_COMPOSITING)
288 void WebPage::changeAcceleratedCompositingMode(WebCore::GraphicsLayer* layer)
289 {
290     if (m_isClosed)
291         return;
292
293     bool compositing = layer;
294     
295     // Tell the UI process that accelerated compositing changed. It may respond by changing
296     // drawing area types.
297     DrawingAreaInfo newDrawingAreaInfo;
298
299     if (!sendSync(Messages::WebPageProxy::DidChangeAcceleratedCompositing(compositing), Messages::WebPageProxy::DidChangeAcceleratedCompositing::Reply(newDrawingAreaInfo)))
300         return;
301     
302     if (newDrawingAreaInfo.type != drawingArea()->info().type) {
303         m_drawingArea = 0;
304         if (newDrawingAreaInfo.type != DrawingAreaInfo::None) {
305             m_drawingArea = DrawingArea::create(newDrawingAreaInfo.type, newDrawingAreaInfo.identifier, this);
306             m_drawingArea->setNeedsDisplay(IntRect(IntPoint(0, 0), m_viewSize));
307         }
308     }
309 }
310
311 void WebPage::enterAcceleratedCompositingMode(GraphicsLayer* layer)
312 {
313     changeAcceleratedCompositingMode(layer);
314     m_drawingArea->setRootCompositingLayer(layer);
315 }
316
317 void WebPage::exitAcceleratedCompositingMode()
318 {
319     changeAcceleratedCompositingMode(0);
320 }
321 #endif
322
323 void WebPage::close()
324 {
325     if (m_isClosed)
326         return;
327
328     m_isClosed = true;
329
330     if (pageGroup()->isVisibleToInjectedBundle() && WebProcess::shared().injectedBundle())
331         WebProcess::shared().injectedBundle()->willDestroyPage(this);
332
333 #if ENABLE(INSPECTOR)
334     m_inspector = 0;
335 #endif
336
337     if (m_activePopupMenu) {
338         m_activePopupMenu->disconnectFromPage();
339         m_activePopupMenu = 0;
340     }
341
342     if (m_activeOpenPanelResultListener) {
343         m_activeOpenPanelResultListener->disconnectFromPage();
344         m_activeOpenPanelResultListener = 0;
345     }
346
347     m_sandboxExtensionTracker.invalidate();
348
349     m_mainFrame->coreFrame()->loader()->detachFromParent();
350     m_page.clear();
351
352     m_drawingArea->onPageClose();
353     m_drawingArea.clear();
354
355     WebProcess::shared().removeWebPage(m_pageID);
356 }
357
358 void WebPage::tryClose()
359 {
360     if (!m_mainFrame->coreFrame()->loader()->shouldClose())
361         return;
362
363     sendClose();
364 }
365
366 void WebPage::sendClose()
367 {
368     send(Messages::WebPageProxy::ClosePage());
369 }
370
371 void WebPage::loadURL(const String& url, const SandboxExtension::Handle& sandboxExtensionHandle)
372 {
373     loadURLRequest(ResourceRequest(KURL(KURL(), url)), sandboxExtensionHandle);
374 }
375
376 void WebPage::loadURLRequest(const ResourceRequest& request, const SandboxExtension::Handle& sandboxExtensionHandle)
377 {
378     m_sandboxExtensionTracker.beginLoad(m_mainFrame.get(), sandboxExtensionHandle);
379     m_mainFrame->coreFrame()->loader()->load(request, false);
380 }
381
382 void WebPage::loadData(PassRefPtr<SharedBuffer> sharedBuffer, const String& MIMEType, const String& encodingName, const KURL& baseURL, const KURL& unreachableURL)
383 {
384     ResourceRequest request(baseURL);
385     SubstituteData substituteData(sharedBuffer, MIMEType, encodingName, unreachableURL);
386     m_mainFrame->coreFrame()->loader()->load(request, substituteData, false);
387 }
388
389 void WebPage::loadHTMLString(const String& htmlString, const String& baseURLString)
390 {
391     RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(reinterpret_cast<const char*>(htmlString.characters()), htmlString.length() * sizeof(UChar));
392     KURL baseURL = baseURLString.isEmpty() ? blankURL() : KURL(KURL(), baseURLString);
393     loadData(sharedBuffer, "text/html", "utf-16", baseURL, KURL());
394 }
395
396 void WebPage::loadAlternateHTMLString(const String& htmlString, const String& baseURLString, const String& unreachableURLString)
397 {
398     RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(reinterpret_cast<const char*>(htmlString.characters()), htmlString.length() * sizeof(UChar));
399     KURL baseURL = baseURLString.isEmpty() ? blankURL() : KURL(KURL(), baseURLString);
400     KURL unreachableURL = unreachableURLString.isEmpty() ? KURL() : KURL(KURL(), unreachableURLString)  ;
401     loadData(sharedBuffer, "text/html", "utf-16", baseURL, unreachableURL);
402 }
403
404 void WebPage::loadPlainTextString(const String& string)
405 {
406     RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(reinterpret_cast<const char*>(string.characters()), string.length() * sizeof(UChar));
407     loadData(sharedBuffer, "text/plain", "utf-16", blankURL(), KURL());
408 }
409
410 void WebPage::stopLoading()
411 {
412     m_mainFrame->coreFrame()->loader()->stopForUserCancel();
413 }
414
415 void WebPage::setDefersLoading(bool defersLoading)
416 {
417     m_page->setDefersLoading(defersLoading);
418 }
419
420 void WebPage::reload(bool reloadFromOrigin)
421 {
422     m_mainFrame->coreFrame()->loader()->reload(reloadFromOrigin);
423 }
424
425 void WebPage::goForward(uint64_t backForwardItemID)
426 {
427     HistoryItem* item = WebBackForwardListProxy::itemForID(backForwardItemID);
428     m_page->goToItem(item, FrameLoadTypeForward);
429 }
430
431 void WebPage::goBack(uint64_t backForwardItemID)
432 {
433     HistoryItem* item = WebBackForwardListProxy::itemForID(backForwardItemID);
434     m_page->goToItem(item, FrameLoadTypeBack);
435 }
436
437 void WebPage::goToBackForwardItem(uint64_t backForwardItemID)
438 {
439     HistoryItem* item = WebBackForwardListProxy::itemForID(backForwardItemID);
440     m_page->goToItem(item, FrameLoadTypeIndexedBackForward);
441 }
442
443 void WebPage::layoutIfNeeded()
444 {
445     if (m_mainFrame->coreFrame()->view())
446         m_mainFrame->coreFrame()->view()->updateLayoutAndStyleIfNeededRecursive();
447 }
448
449 void WebPage::setSize(const WebCore::IntSize& viewSize)
450 {
451 #if ENABLE(TILED_BACKING_STORE)
452     // If we are resizing to content ignore external attempts.
453     if (!m_resizesToContentsLayoutSize.isEmpty())
454         return;
455 #endif
456
457     if (m_viewSize == viewSize)
458         return;
459
460     Frame* frame = m_page->mainFrame();
461     
462     frame->view()->resize(viewSize);
463     frame->view()->setNeedsLayout();
464     m_drawingArea->setNeedsDisplay(IntRect(IntPoint(0, 0), viewSize));
465     
466     m_viewSize = viewSize;
467 }
468
469 #if ENABLE(TILED_BACKING_STORE)
470 void WebPage::setActualVisibleContentRect(const IntRect& rect)
471 {
472     Frame* frame = m_page->mainFrame();
473
474     frame->view()->setActualVisibleContentRect(rect);
475 }
476
477 void WebPage::setResizesToContentsUsingLayoutSize(const IntSize& targetLayoutSize)
478 {
479     if (m_resizesToContentsLayoutSize == targetLayoutSize)
480         return;
481
482     m_resizesToContentsLayoutSize = targetLayoutSize;
483
484     Frame* frame = m_page->mainFrame();
485     if (m_resizesToContentsLayoutSize.isEmpty()) {
486         frame->view()->setDelegatesScrolling(false);
487         frame->view()->setUseFixedLayout(false);
488         frame->view()->setPaintsEntireContents(false);
489     } else {
490         frame->view()->setDelegatesScrolling(true);
491         frame->view()->setUseFixedLayout(true);
492         frame->view()->setPaintsEntireContents(true);
493         frame->view()->setFixedLayoutSize(m_resizesToContentsLayoutSize);
494     }
495     frame->view()->forceLayout();
496 }
497
498 void WebPage::resizeToContentsIfNeeded()
499 {
500     if (m_resizesToContentsLayoutSize.isEmpty())
501         return;
502
503     Frame* frame = m_page->mainFrame();
504
505     IntSize contentSize = frame->view()->contentsSize();
506     if (contentSize == m_viewSize)
507         return;
508
509     m_viewSize = contentSize;
510     frame->view()->resize(m_viewSize);
511     frame->view()->setNeedsLayout();
512 }
513 #endif
514
515 void WebPage::drawRect(GraphicsContext& graphicsContext, const IntRect& rect)
516 {
517     graphicsContext.save();
518     graphicsContext.clip(rect);
519     m_mainFrame->coreFrame()->view()->paint(&graphicsContext, rect);
520     graphicsContext.restore();
521
522     if (m_pageOverlay) {
523         graphicsContext.save();
524         graphicsContext.clip(rect);
525         m_pageOverlay->drawRect(graphicsContext, rect);
526         graphicsContext.restore();
527     }
528     
529     m_page->animationTime()->clearCurrentAnimationTime();
530 }
531
532 double WebPage::textZoomFactor() const
533 {
534     Frame* frame = m_mainFrame->coreFrame();
535     if (!frame)
536         return 1;
537     return frame->textZoomFactor();
538 }
539
540 void WebPage::setTextZoomFactor(double zoomFactor)
541 {
542     Frame* frame = m_mainFrame->coreFrame();
543     if (!frame)
544         return;
545     frame->setTextZoomFactor(static_cast<float>(zoomFactor));
546 }
547
548 double WebPage::pageZoomFactor() const
549 {
550     Frame* frame = m_mainFrame->coreFrame();
551     if (!frame)
552         return 1;
553     return frame->pageZoomFactor();
554 }
555
556 void WebPage::setPageZoomFactor(double zoomFactor)
557 {
558     Frame* frame = m_mainFrame->coreFrame();
559     if (!frame)
560         return;
561     frame->setPageZoomFactor(static_cast<float>(zoomFactor));
562 }
563
564 void WebPage::setPageAndTextZoomFactors(double pageZoomFactor, double textZoomFactor)
565 {
566     Frame* frame = m_mainFrame->coreFrame();
567     if (!frame)
568         return;
569     return frame->setPageAndTextZoomFactors(static_cast<float>(pageZoomFactor), static_cast<float>(textZoomFactor));
570 }
571
572 void WebPage::scaleWebView(double scale, const IntPoint& origin)
573 {
574     Frame* frame = m_mainFrame->coreFrame();
575     if (!frame)
576         return;
577     frame->scalePage(scale, origin);
578 }
579
580 double WebPage::viewScaleFactor() const
581 {
582     Frame* frame = m_mainFrame->coreFrame();
583     if (!frame)
584         return 1;
585     return frame->pageScaleFactor();
586 }
587
588 void WebPage::installPageOverlay(PassRefPtr<PageOverlay> pageOverlay)
589 {
590     if (m_pageOverlay)
591         pageOverlay->setPage(0);
592
593     m_pageOverlay = pageOverlay;
594     m_pageOverlay->setPage(this);
595     m_pageOverlay->setNeedsDisplay();
596 }
597
598 void WebPage::uninstallPageOverlay(PageOverlay* pageOverlay)
599 {
600     if (pageOverlay != m_pageOverlay)
601         return;
602
603     m_pageOverlay->setPage(0);
604     m_pageOverlay = nullptr;
605     m_drawingArea->setNeedsDisplay(IntRect(IntPoint(0, 0), m_viewSize));
606 }
607
608 PassRefPtr<WebImage> WebPage::snapshotInViewCoordinates(const IntRect& rect, ImageOptions options)
609 {
610     FrameView* frameView = m_mainFrame->coreFrame()->view();
611     if (!frameView)
612         return 0;
613
614     frameView->updateLayoutAndStyleIfNeededRecursive();
615
616     PaintBehavior oldBehavior = frameView->paintBehavior();
617     frameView->setPaintBehavior(oldBehavior | PaintBehaviorFlattenCompositingLayers);
618
619     RefPtr<WebImage> snapshot = WebImage::create(rect.size(), options);
620     OwnPtr<WebCore::GraphicsContext> graphicsContext = snapshot->backingStore()->createGraphicsContext();
621
622     graphicsContext->save();
623     graphicsContext->translate(-rect.x(), -rect.y());
624     frameView->paint(graphicsContext.get(), rect);
625     graphicsContext->restore();
626
627     frameView->setPaintBehavior(oldBehavior);
628
629     return snapshot.release();
630 }
631
632 PassRefPtr<WebImage> WebPage::snapshotInDocumentCoordinates(const IntRect& rect, ImageOptions options)
633 {
634     FrameView* frameView = m_mainFrame->coreFrame()->view();
635     if (!frameView)
636         return 0;
637
638     frameView->updateLayoutAndStyleIfNeededRecursive();
639
640     PaintBehavior oldBehavior = frameView->paintBehavior();
641     frameView->setPaintBehavior(oldBehavior | PaintBehaviorFlattenCompositingLayers);
642
643     RefPtr<WebImage> snapshot = WebImage::create(rect.size(), options);
644     OwnPtr<WebCore::GraphicsContext> graphicsContext = snapshot->backingStore()->createGraphicsContext();
645
646     graphicsContext->save();
647     graphicsContext->translate(-rect.x(), -rect.y());
648     frameView->paintContents(graphicsContext.get(), rect);
649     graphicsContext->restore();
650
651     frameView->setPaintBehavior(oldBehavior);
652
653     return snapshot.release();
654 }
655
656 void WebPage::pageDidScroll()
657 {
658     // Hide the find indicator.
659     m_findController.hideFindIndicator();
660
661     m_uiClient.pageDidScroll(this);
662
663     send(Messages::WebPageProxy::PageDidScroll());
664 }
665
666 #if ENABLE(TILED_BACKING_STORE)
667 void WebPage::pageDidRequestScroll(const IntSize& delta)
668 {
669     send(Messages::WebPageProxy::PageDidRequestScroll(delta));
670 }
671 #endif
672
673 WebContextMenu* WebPage::contextMenu()
674 {
675     if (!m_contextMenu)
676         m_contextMenu = WebContextMenu::create(this);
677     return m_contextMenu.get();
678 }
679
680 void WebPage::getLocationAndLengthFromRange(Range* range, uint64_t& location, uint64_t& length)
681 {
682     location = notFound;
683     length = 0;
684
685     if (!range || !range->startContainer())
686         return;
687
688     Element* selectionRoot = range->ownerDocument()->frame()->selection()->rootEditableElement();
689     Element* scope = selectionRoot ? selectionRoot : range->ownerDocument()->documentElement();
690     
691     // Mouse events may cause TSM to attempt to create an NSRange for a portion of the view
692     // that is not inside the current editable region.  These checks ensure we don't produce
693     // potentially invalid data when responding to such requests.
694     if (range->startContainer() != scope && !range->startContainer()->isDescendantOf(scope))
695         return;
696     if (range->endContainer() != scope && !range->endContainer()->isDescendantOf(scope))
697         return;
698
699     RefPtr<Range> testRange = Range::create(scope->document(), scope, 0, range->startContainer(), range->startOffset());
700     ASSERT(testRange->startContainer() == scope);
701     location = TextIterator::rangeLength(testRange.get());
702     
703     ExceptionCode ec;
704     testRange->setEnd(range->endContainer(), range->endOffset(), ec);
705     ASSERT(testRange->startContainer() == scope);
706     length = TextIterator::rangeLength(testRange.get()) - location;
707 }
708
709 // Events 
710
711 static const WebEvent* g_currentEvent = 0;
712
713 // FIXME: WebPage::currentEvent is used by the plug-in code to avoid having to convert from DOM events back to
714 // WebEvents. When we get the event handling sorted out, this should go away and the Widgets should get the correct
715 // platform events passed to the event handler code.
716 const WebEvent* WebPage::currentEvent()
717 {
718     return g_currentEvent;
719 }
720
721 class CurrentEvent {
722 public:
723     explicit CurrentEvent(const WebEvent& event)
724         : m_previousCurrentEvent(g_currentEvent)
725     {
726         g_currentEvent = &event;
727     }
728
729     ~CurrentEvent()
730     {
731         g_currentEvent = m_previousCurrentEvent;
732     }
733
734 private:
735     const WebEvent* m_previousCurrentEvent;
736 };
737
738 static bool isContextClick(const PlatformMouseEvent& event)
739 {
740     if (event.button() == WebCore::RightButton)
741         return true;
742
743 #if PLATFORM(MAC)
744     // FIXME: this really should be about OSX-style UI, not about the Mac port
745     if (event.button() == WebCore::LeftButton && event.ctrlKey())
746         return true;
747 #endif
748
749     return false;
750 }
751
752 static bool handleMouseEvent(const WebMouseEvent& mouseEvent, Page* page)
753 {
754     Frame* frame = page->mainFrame();
755     if (!frame->view())
756         return false;
757
758     PlatformMouseEvent platformMouseEvent = platform(mouseEvent);
759
760     switch (platformMouseEvent.eventType()) {
761         case WebCore::MouseEventPressed:
762         {
763             if (isContextClick(platformMouseEvent))
764                 page->contextMenuController()->clearContextMenu();
765             
766             bool handled = frame->eventHandler()->handleMousePressEvent(platformMouseEvent);
767             
768             if (isContextClick(platformMouseEvent)) {
769                 handled = frame->eventHandler()->sendContextMenuEvent(platformMouseEvent);
770                 if (handled)
771                     page->chrome()->showContextMenu();
772             }
773
774             return handled;
775         }
776         case WebCore::MouseEventReleased:
777             return frame->eventHandler()->handleMouseReleaseEvent(platformMouseEvent);
778         case WebCore::MouseEventMoved:
779             return frame->eventHandler()->mouseMoved(platformMouseEvent);
780         default:
781             ASSERT_NOT_REACHED();
782             return false;
783     }
784 }
785
786 void WebPage::mouseEvent(const WebMouseEvent& mouseEvent)
787 {
788     bool handled = false;
789     
790     if (m_pageOverlay) {
791         // Let the page overlay handle the event.
792         handled = m_pageOverlay->mouseEvent(mouseEvent);
793     }
794
795     if (!handled) {
796         CurrentEvent currentEvent(mouseEvent);
797
798         handled = handleMouseEvent(mouseEvent, m_page.get());
799     }
800
801     send(Messages::WebPageProxy::DidReceiveEvent(static_cast<uint32_t>(mouseEvent.type()), handled));
802 }
803
804 static bool handleWheelEvent(const WebWheelEvent& wheelEvent, Page* page)
805 {
806     Frame* frame = page->mainFrame();
807     if (!frame->view())
808         return false;
809
810     PlatformWheelEvent platformWheelEvent = platform(wheelEvent);
811     return frame->eventHandler()->handleWheelEvent(platformWheelEvent);
812 }
813
814 void WebPage::wheelEvent(const WebWheelEvent& wheelEvent)
815 {
816     CurrentEvent currentEvent(wheelEvent);
817
818     bool handled = handleWheelEvent(wheelEvent, m_page.get());
819     send(Messages::WebPageProxy::DidReceiveEvent(static_cast<uint32_t>(wheelEvent.type()), handled));
820 }
821
822 static bool handleKeyEvent(const WebKeyboardEvent& keyboardEvent, Page* page)
823 {
824     if (!page->mainFrame()->view())
825         return false;
826
827     if (keyboardEvent.type() == WebEvent::Char && keyboardEvent.isSystemKey())
828         return page->focusController()->focusedOrMainFrame()->eventHandler()->handleAccessKey(platform(keyboardEvent));
829     return page->focusController()->focusedOrMainFrame()->eventHandler()->keyEvent(platform(keyboardEvent));
830 }
831
832 void WebPage::keyEvent(const WebKeyboardEvent& keyboardEvent)
833 {
834     CurrentEvent currentEvent(keyboardEvent);
835
836     bool handled = handleKeyEvent(keyboardEvent, m_page.get());
837     if (!handled)
838         handled = performDefaultBehaviorForKeyEvent(keyboardEvent);
839
840     send(Messages::WebPageProxy::DidReceiveEvent(static_cast<uint32_t>(keyboardEvent.type()), handled));
841 }
842
843 void WebPage::validateMenuItem(const String& commandName)
844 {
845     bool isEnabled = false;
846     int32_t state = 0;
847     Frame* frame = m_page->focusController()->focusedOrMainFrame();
848     if (frame) {
849         Editor::Command command = frame->editor()->command(commandName);
850         state = command.state();
851         isEnabled = command.isSupported() && command.isEnabled();
852     }
853
854     send(Messages::WebPageProxy::DidValidateMenuItem(commandName, isEnabled, state));
855 }
856
857 void WebPage::executeEditCommand(const String& commandName)
858 {
859     executeEditingCommand(commandName, String());
860 }
861
862 void WebPage::restoreSession(const SessionState& sessionState)
863 {
864     const BackForwardListItemVector& list = sessionState.list();
865     size_t size = list.size();
866     RefPtr<HistoryItem> currentItem;
867     for (size_t i = 0; i < size; ++i) {
868         WebBackForwardListItem* webItem = list[i].get();
869         DecoderAdapter decoder(webItem->backForwardData().data(), webItem->backForwardData().size());
870         
871         RefPtr<HistoryItem> item = HistoryItem::decodeBackForwardTree(webItem->url(), webItem->title(), webItem->originalURL(), decoder);
872         if (!item) {
873             LOG_ERROR("Failed to decode a HistoryItem from session state data.");
874             return;
875         }
876         
877         if (i == sessionState.currentIndex())
878             currentItem = item;
879         
880         WebBackForwardListProxy::addItemFromUIProcess(list[i]->itemID(), item.release());
881     }    
882     ASSERT(currentItem);
883
884     m_page->goToItem(currentItem.get(), FrameLoadTypeIndexedBackForward);
885 }
886
887 #if ENABLE(TOUCH_EVENTS)
888 static bool handleTouchEvent(const WebTouchEvent& touchEvent, Page* page)
889 {
890     Frame* frame = page->mainFrame();
891     if (!frame->view())
892         return false;
893
894     return frame->eventHandler()->handleTouchEvent(platform(touchEvent));
895 }
896
897 void WebPage::touchEvent(const WebTouchEvent& touchEvent)
898 {
899     CurrentEvent currentEvent(touchEvent);
900
901     bool handled = handleTouchEvent(touchEvent, m_page.get());
902
903     send(Messages::WebPageProxy::DidReceiveEvent(static_cast<uint32_t>(touchEvent.type()), handled));
904 }
905 #endif
906
907 void WebPage::setActive(bool isActive)
908 {
909     m_page->focusController()->setActive(isActive);
910
911 #if PLATFORM(MAC)    
912     // Tell all our plug-in views that the window focus changed.
913     for (HashSet<PluginView*>::const_iterator it = m_pluginViews.begin(), end = m_pluginViews.end(); it != end; ++it)
914         (*it)->setWindowIsFocused(isActive);
915 #endif
916 }
917
918 void WebPage::setDrawsBackground(bool drawsBackground)
919 {
920     if (m_drawsBackground == drawsBackground)
921         return;
922
923     m_drawsBackground = drawsBackground;
924
925     for (Frame* coreFrame = m_mainFrame->coreFrame(); coreFrame; coreFrame = coreFrame->tree()->traverseNext()) {
926         if (FrameView* view = coreFrame->view())
927             view->setTransparent(!drawsBackground);
928     }
929
930     m_drawingArea->pageBackgroundTransparencyChanged();
931     m_drawingArea->setNeedsDisplay(IntRect(IntPoint(0, 0), m_viewSize));
932 }
933
934 void WebPage::setDrawsTransparentBackground(bool drawsTransparentBackground)
935 {
936     if (m_drawsTransparentBackground == drawsTransparentBackground)
937         return;
938
939     m_drawsTransparentBackground = drawsTransparentBackground;
940
941     Color backgroundColor = drawsTransparentBackground ? Color::transparent : Color::white;
942     for (Frame* coreFrame = m_mainFrame->coreFrame(); coreFrame; coreFrame = coreFrame->tree()->traverseNext()) {
943         if (FrameView* view = coreFrame->view())
944             view->setBaseBackgroundColor(backgroundColor);
945     }
946
947     m_drawingArea->pageBackgroundTransparencyChanged();
948     m_drawingArea->setNeedsDisplay(IntRect(IntPoint(0, 0), m_viewSize));
949 }
950
951 void WebPage::setFocused(bool isFocused)
952 {
953     m_page->focusController()->setFocused(isFocused);
954 }
955
956 void WebPage::setInitialFocus(bool forward)
957 {
958     if (!m_page || !m_page->focusController())
959         return;
960
961     Frame* frame = m_page->focusController()->focusedOrMainFrame();
962     frame->document()->setFocusedNode(0);
963     m_page->focusController()->setInitialFocus(forward ? FocusDirectionForward : FocusDirectionBackward, 0);
964 }
965
966 void WebPage::setWindowResizerSize(const IntSize& windowResizerSize)
967 {
968     if (m_windowResizerSize == windowResizerSize)
969         return;
970
971     m_windowResizerSize = windowResizerSize;
972
973     for (Frame* coreFrame = m_mainFrame->coreFrame(); coreFrame; coreFrame = coreFrame->tree()->traverseNext()) {
974         FrameView* view = coreFrame->view();
975         if (view)
976             view->windowResizerRectChanged();
977     }
978 }
979
980 void WebPage::setIsInWindow(bool isInWindow)
981 {
982     if (!isInWindow) {
983         m_page->setCanStartMedia(false);
984         m_page->willMoveOffscreen();
985     } else {
986         m_page->setCanStartMedia(true);
987         m_page->didMoveOnscreen();
988     }
989 }
990
991 void WebPage::didReceivePolicyDecision(uint64_t frameID, uint64_t listenerID, uint32_t policyAction, uint64_t downloadID)
992 {
993     WebFrame* frame = WebProcess::shared().webFrame(frameID);
994     if (!frame)
995         return;
996     frame->didReceivePolicyDecision(listenerID, static_cast<PolicyAction>(policyAction), downloadID);
997 }
998
999 void WebPage::show()
1000 {
1001     send(Messages::WebPageProxy::ShowPage());
1002 }
1003
1004 void WebPage::setUserAgent(const String& userAgent)
1005 {
1006     m_userAgent = userAgent;
1007 }
1008
1009 IntRect WebPage::windowResizerRect() const
1010 {
1011     if (m_windowResizerSize.isEmpty())
1012         return IntRect();
1013
1014     IntSize frameViewSize;
1015     if (Frame* coreFrame = m_mainFrame->coreFrame()) {
1016         if (FrameView* view = coreFrame->view())
1017             frameViewSize = view->size();
1018     }
1019
1020     return IntRect(frameViewSize.width() - m_windowResizerSize.width(), frameViewSize.height() - m_windowResizerSize.height(), 
1021                    m_windowResizerSize.width(), m_windowResizerSize.height());
1022 }
1023
1024 void WebPage::runJavaScriptInMainFrame(const String& script, uint64_t callbackID)
1025 {
1026     // NOTE: We need to be careful when running scripts that the objects we depend on don't
1027     // disappear during script execution.
1028
1029     JSLock lock(SilenceAssertionsOnly);
1030     JSValue resultValue = m_mainFrame->coreFrame()->script()->executeScript(script, true).jsValue();
1031     String resultString;
1032     if (resultValue)
1033         resultString = ustringToString(resultValue.toString(m_mainFrame->coreFrame()->script()->globalObject(mainThreadNormalWorld())->globalExec()));
1034
1035     send(Messages::WebPageProxy::StringCallback(resultString, callbackID));
1036 }
1037
1038 void WebPage::getContentsAsString(uint64_t callbackID)
1039 {
1040     String resultString = m_mainFrame->contentsAsString();
1041     send(Messages::WebPageProxy::StringCallback(resultString, callbackID));
1042 }
1043
1044 void WebPage::getRenderTreeExternalRepresentation(uint64_t callbackID)
1045 {
1046     String resultString = renderTreeExternalRepresentation();
1047     send(Messages::WebPageProxy::StringCallback(resultString, callbackID));
1048 }
1049
1050 void WebPage::getSelectionOrContentsAsString(uint64_t callbackID)
1051 {
1052     String resultString = m_mainFrame->selectionAsString();
1053     if (resultString.isEmpty())
1054         resultString = m_mainFrame->contentsAsString();
1055     send(Messages::WebPageProxy::StringCallback(resultString, callbackID));
1056 }
1057
1058 void WebPage::getSourceForFrame(uint64_t frameID, uint64_t callbackID)
1059 {
1060     String resultString;
1061     if (WebFrame* frame = WebProcess::shared().webFrame(frameID))
1062        resultString = frame->source();
1063
1064     send(Messages::WebPageProxy::StringCallback(resultString, callbackID));
1065 }
1066
1067 void WebPage::getMainResourceDataOfFrame(uint64_t frameID, uint64_t callbackID)
1068 {
1069     CoreIPC::DataReference dataReference;
1070
1071     RefPtr<SharedBuffer> buffer;
1072     if (WebFrame* frame = WebProcess::shared().webFrame(frameID)) {
1073         if (DocumentLoader* loader = frame->coreFrame()->loader()->documentLoader()) {
1074             if ((buffer = loader->mainResourceData()))
1075                 dataReference = CoreIPC::DataReference(reinterpret_cast<const uint8_t*>(buffer->data()), buffer->size());
1076         }
1077     }
1078
1079     send(Messages::WebPageProxy::DataCallback(dataReference, callbackID));
1080 }
1081
1082 void WebPage::getWebArchiveOfFrame(uint64_t frameID, uint64_t callbackID)
1083 {
1084     CoreIPC::DataReference dataReference;
1085
1086 #if PLATFORM(MAC) || PLATFORM(WIN)
1087     RetainPtr<CFDataRef> data;
1088     if (WebFrame* frame = WebProcess::shared().webFrame(frameID)) {
1089         if (RefPtr<LegacyWebArchive> archive = LegacyWebArchive::create(frame->coreFrame())) {
1090             if ((data = archive->rawDataRepresentation()))
1091                 dataReference = CoreIPC::DataReference(CFDataGetBytePtr(data.get()), CFDataGetLength(data.get()));
1092         }
1093     }
1094 #endif
1095
1096     send(Messages::WebPageProxy::DataCallback(dataReference, callbackID));
1097 }
1098
1099 void WebPage::preferencesDidChange(const WebPreferencesStore& store)
1100 {
1101     WebPreferencesStore::removeTestRunnerOverrides();
1102     updatePreferences(store);
1103 }
1104
1105 void WebPage::updatePreferences(const WebPreferencesStore& store)
1106 {
1107     Settings* settings = m_page->settings();
1108
1109     m_tabToLinks = store.getBoolValueForKey(WebPreferencesKey::tabsToLinksKey());
1110
1111     // FIXME: This should be generated from macro expansion for all preferences,
1112     // but we currently don't match the naming of WebCore exactly so we are
1113     // handrolling the boolean and integer preferences until that is fixed.
1114
1115 #define INITIALIZE_SETTINGS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) settings->set##KeyUpper(store.get##TypeName##ValueForKey(WebPreferencesKey::KeyLower##Key()));
1116
1117     FOR_EACH_WEBKIT_STRING_PREFERENCE(INITIALIZE_SETTINGS)
1118
1119 #undef INITIALIZE_SETTINGS
1120
1121     settings->setJavaScriptEnabled(store.getBoolValueForKey(WebPreferencesKey::javaScriptEnabledKey()));
1122     settings->setLoadsImagesAutomatically(store.getBoolValueForKey(WebPreferencesKey::loadsImagesAutomaticallyKey()));
1123     settings->setPluginsEnabled(store.getBoolValueForKey(WebPreferencesKey::pluginsEnabledKey()));
1124     settings->setJavaEnabled(store.getBoolValueForKey(WebPreferencesKey::javaEnabledKey()));
1125     settings->setOfflineWebApplicationCacheEnabled(store.getBoolValueForKey(WebPreferencesKey::offlineWebApplicationCacheEnabledKey()));
1126     settings->setLocalStorageEnabled(store.getBoolValueForKey(WebPreferencesKey::localStorageEnabledKey()));
1127     settings->setXSSAuditorEnabled(store.getBoolValueForKey(WebPreferencesKey::xssAuditorEnabledKey()));
1128     settings->setFrameFlatteningEnabled(store.getBoolValueForKey(WebPreferencesKey::frameFlatteningEnabledKey()));
1129     settings->setPrivateBrowsingEnabled(store.getBoolValueForKey(WebPreferencesKey::privateBrowsingEnabledKey()));
1130     settings->setDeveloperExtrasEnabled(store.getBoolValueForKey(WebPreferencesKey::developerExtrasEnabledKey()));
1131     settings->setTextAreasAreResizable(store.getBoolValueForKey(WebPreferencesKey::textAreasAreResizableKey()));
1132     settings->setNeedsSiteSpecificQuirks(store.getBoolValueForKey(WebPreferencesKey::needsSiteSpecificQuirksKey()));
1133     settings->setJavaScriptCanOpenWindowsAutomatically(store.getBoolValueForKey(WebPreferencesKey::javaScriptCanOpenWindowsAutomaticallyKey()));
1134     settings->setForceFTPDirectoryListings(store.getBoolValueForKey(WebPreferencesKey::forceFTPDirectoryListingsKey()));
1135     settings->setDNSPrefetchingEnabled(store.getBoolValueForKey(WebPreferencesKey::dnsPrefetchingEnabledKey()));
1136     settings->setWebArchiveDebugModeEnabled(store.getBoolValueForKey(WebPreferencesKey::webArchiveDebugModeEnabledKey()));
1137     settings->setLocalFileContentSniffingEnabled(store.getBoolValueForKey(WebPreferencesKey::localFileContentSniffingEnabledKey()));
1138     settings->setUsesPageCache(store.getBoolValueForKey(WebPreferencesKey::usesPageCacheKey()));
1139     settings->setAuthorAndUserStylesEnabled(store.getBoolValueForKey(WebPreferencesKey::authorAndUserStylesEnabledKey()));
1140     settings->setPaginateDuringLayoutEnabled(store.getBoolValueForKey(WebPreferencesKey::paginateDuringLayoutEnabledKey()));
1141     settings->setDOMPasteAllowed(store.getBoolValueForKey(WebPreferencesKey::domPasteAllowedKey()));
1142
1143     settings->setMinimumFontSize(store.getUInt32ValueForKey(WebPreferencesKey::minimumFontSizeKey()));
1144     settings->setMinimumLogicalFontSize(store.getUInt32ValueForKey(WebPreferencesKey::minimumLogicalFontSizeKey()));
1145     settings->setDefaultFontSize(store.getUInt32ValueForKey(WebPreferencesKey::defaultFontSizeKey()));
1146     settings->setDefaultFixedFontSize(store.getUInt32ValueForKey(WebPreferencesKey::defaultFixedFontSizeKey()));
1147
1148 #if PLATFORM(WIN)
1149     // Temporarily turn off accelerated compositing until we have a good solution for rendering it.
1150     settings->setAcceleratedCompositingEnabled(false);
1151 #else
1152     settings->setAcceleratedCompositingEnabled(store.getBoolValueForKey(WebPreferencesKey::acceleratedCompositingEnabledKey()));
1153 #endif
1154     settings->setShowDebugBorders(store.getBoolValueForKey(WebPreferencesKey::compositingBordersVisibleKey()));
1155     settings->setShowRepaintCounter(store.getBoolValueForKey(WebPreferencesKey::compositingRepaintCountersVisibleKey()));
1156
1157 #if ENABLE(DATABASE)
1158     AbstractDatabase::setIsAvailable(store.getBoolValueForKey(WebPreferencesKey::databasesEnabledKey()));
1159 #endif
1160
1161     platformPreferencesDidChange(store);
1162 }
1163
1164 #if ENABLE(INSPECTOR)
1165 WebInspector* WebPage::inspector()
1166 {
1167     if (m_isClosed)
1168         return 0;
1169     if (!m_inspector)
1170         m_inspector = adoptPtr(new WebInspector(this));
1171     return m_inspector.get();
1172 }
1173 #endif
1174
1175 #if !PLATFORM(MAC)
1176 bool WebPage::handleEditingKeyboardEvent(KeyboardEvent* evt)
1177 {
1178     Node* node = evt->target()->toNode();
1179     ASSERT(node);
1180     Frame* frame = node->document()->frame();
1181     ASSERT(frame);
1182
1183     const PlatformKeyboardEvent* keyEvent = evt->keyEvent();
1184     if (!keyEvent)
1185         return false;
1186
1187     Editor::Command command = frame->editor()->command(interpretKeyEvent(evt));
1188
1189     if (keyEvent->type() == PlatformKeyboardEvent::RawKeyDown) {
1190         // WebKit doesn't have enough information about mode to decide how commands that just insert text if executed via Editor should be treated,
1191         // so we leave it upon WebCore to either handle them immediately (e.g. Tab that changes focus) or let a keypress event be generated
1192         // (e.g. Tab that inserts a Tab character, or Enter).
1193         return !command.isTextInsertion() && command.execute(evt);
1194     }
1195
1196     if (command.execute(evt))
1197         return true;
1198
1199     // Don't insert null or control characters as they can result in unexpected behaviour
1200     if (evt->charCode() < ' ')
1201         return false;
1202
1203     return frame->editor()->insertText(evt->keyEvent()->text(), evt);
1204 }
1205 #endif
1206
1207 WebEditCommand* WebPage::webEditCommand(uint64_t commandID)
1208 {
1209     return m_editCommandMap.get(commandID).get();
1210 }
1211
1212 void WebPage::addWebEditCommand(uint64_t commandID, WebEditCommand* command)
1213 {
1214     m_editCommandMap.set(commandID, command);
1215 }
1216
1217 void WebPage::removeWebEditCommand(uint64_t commandID)
1218 {
1219     m_editCommandMap.remove(commandID);
1220 }
1221
1222 void WebPage::unapplyEditCommand(uint64_t commandID)
1223 {
1224     WebEditCommand* command = webEditCommand(commandID);
1225     if (!command)
1226         return;
1227
1228     command->command()->unapply();
1229 }
1230
1231 void WebPage::reapplyEditCommand(uint64_t commandID)
1232 {
1233     WebEditCommand* command = webEditCommand(commandID);
1234     if (!command)
1235         return;
1236
1237     m_isInRedo = true;
1238     command->command()->reapply();
1239     m_isInRedo = false;
1240 }
1241
1242 void WebPage::didRemoveEditCommand(uint64_t commandID)
1243 {
1244     removeWebEditCommand(commandID);
1245 }
1246
1247 void WebPage::setActivePopupMenu(WebPopupMenu* menu)
1248 {
1249     m_activePopupMenu = menu;
1250 }
1251
1252 void WebPage::setActiveOpenPanelResultListener(PassRefPtr<WebOpenPanelResultListener> openPanelResultListener)
1253 {
1254     m_activeOpenPanelResultListener = openPanelResultListener;
1255 }
1256
1257 bool WebPage::findStringFromInjectedBundle(const String& target, FindOptions options)
1258 {
1259     return m_page->findString(target, options);
1260 }
1261
1262 void WebPage::findString(const String& string, uint32_t options, uint32_t maxMatchCount)
1263 {
1264     m_findController.findString(string, static_cast<FindOptions>(options), maxMatchCount);
1265 }
1266
1267 void WebPage::hideFindUI()
1268 {
1269     m_findController.hideFindUI();
1270 }
1271
1272 void WebPage::countStringMatches(const String& string, uint32_t options, uint32_t maxMatchCount)
1273 {
1274     m_findController.countStringMatches(string, static_cast<FindOptions>(options), maxMatchCount);
1275 }
1276
1277 void WebPage::didChangeSelectedIndexForActivePopupMenu(int32_t newIndex)
1278 {
1279     if (!m_activePopupMenu)
1280         return;
1281
1282     m_activePopupMenu->didChangeSelectedIndex(newIndex);
1283     m_activePopupMenu = 0;
1284 }
1285
1286 void WebPage::didChooseFilesForOpenPanel(const Vector<String>& files)
1287 {
1288     if (!m_activeOpenPanelResultListener)
1289         return;
1290
1291     m_activeOpenPanelResultListener->didChooseFiles(files);
1292     m_activeOpenPanelResultListener = 0;
1293 }
1294
1295 void WebPage::didCancelForOpenPanel()
1296 {
1297     m_activeOpenPanelResultListener = 0;
1298 }
1299
1300 void WebPage::advanceToNextMisspelling(bool startBeforeSelection)
1301 {
1302     Frame* frame = m_page->focusController()->focusedOrMainFrame();
1303     frame->editor()->advanceToNextMisspelling(startBeforeSelection);
1304 }
1305
1306 void WebPage::changeSpellingToWord(const String& word)
1307 {
1308     replaceSelectionWithText(m_page->focusController()->focusedOrMainFrame(), word);
1309 }
1310
1311 void WebPage::unmarkAllMisspellings()
1312 {
1313     for (Frame* frame = m_page->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
1314         if (Document* document = frame->document())
1315             document->markers()->removeMarkers(DocumentMarker::Spelling);
1316     }
1317 }
1318
1319 void WebPage::unmarkAllBadGrammar()
1320 {
1321     for (Frame* frame = m_page->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
1322         if (Document* document = frame->document())
1323             document->markers()->removeMarkers(DocumentMarker::Grammar);
1324     }
1325 }
1326
1327 #if PLATFORM(MAC)
1328 void WebPage::uppercaseWord()
1329 {
1330     m_page->focusController()->focusedOrMainFrame()->editor()->uppercaseWord();
1331 }
1332
1333 void WebPage::lowercaseWord()
1334 {
1335     m_page->focusController()->focusedOrMainFrame()->editor()->lowercaseWord();
1336 }
1337
1338 void WebPage::capitalizeWord()
1339 {
1340     m_page->focusController()->focusedOrMainFrame()->editor()->capitalizeWord();
1341 }
1342 #endif
1343     
1344 void WebPage::setTextForActivePopupMenu(int32_t index)
1345 {
1346     if (!m_activePopupMenu)
1347         return;
1348
1349     m_activePopupMenu->setTextForIndex(index);
1350 }
1351
1352 void WebPage::didSelectItemFromActiveContextMenu(const WebContextMenuItemData& item)
1353 {
1354     ASSERT(m_contextMenu);
1355     m_contextMenu->itemSelected(item);
1356     m_contextMenu = 0;
1357 }
1358
1359 void WebPage::replaceSelectionWithText(Frame* frame, const String& text)
1360 {
1361     if (frame->selection()->isNone())
1362         return;
1363     
1364     RefPtr<DocumentFragment> textFragment = createFragmentFromText(frame->selection()->toNormalizedRange().get(), text);
1365     applyCommand(ReplaceSelectionCommand::create(frame->document(), textFragment.release(), true, false, true));
1366     frame->selection()->revealSelection(ScrollAlignment::alignToEdgeIfNeeded);
1367 }
1368
1369 #if PLATFORM(MAC)
1370
1371 void WebPage::addPluginView(PluginView* pluginView)
1372 {
1373     ASSERT(!m_pluginViews.contains(pluginView));
1374
1375     m_pluginViews.add(pluginView);
1376 }
1377
1378 void WebPage::removePluginView(PluginView* pluginView)
1379 {
1380     ASSERT(m_pluginViews.contains(pluginView));
1381
1382     m_pluginViews.remove(pluginView);
1383 }
1384
1385 void WebPage::setWindowIsVisible(bool windowIsVisible)
1386 {
1387     m_windowIsVisible = windowIsVisible;
1388
1389     // Tell all our plug-in views that the window visibility changed.
1390     for (HashSet<PluginView*>::const_iterator it = m_pluginViews.begin(), end = m_pluginViews.end(); it != end; ++it)
1391         (*it)->setWindowIsVisible(windowIsVisible);
1392 }
1393
1394 void WebPage::windowAndViewFramesChanged(const WebCore::IntRect& windowFrameInScreenCoordinates, const WebCore::IntRect& viewFrameInWindowCoordinates, const WebCore::IntPoint& accessibilityViewCoordinates)
1395 {
1396     m_windowFrameInScreenCoordinates = windowFrameInScreenCoordinates;
1397     m_viewFrameInWindowCoordinates = viewFrameInWindowCoordinates;
1398     m_accessibilityPosition = accessibilityViewCoordinates;
1399     
1400     // Tell all our plug-in views that the window and view frames have changed.
1401     for (HashSet<PluginView*>::const_iterator it = m_pluginViews.begin(), end = m_pluginViews.end(); it != end; ++it)
1402         (*it)->windowAndViewFramesChanged(windowFrameInScreenCoordinates, viewFrameInWindowCoordinates);
1403 }
1404
1405 bool WebPage::windowIsFocused() const
1406 {
1407     return m_page->focusController()->isActive();
1408 }
1409
1410 #endif
1411
1412 void WebPage::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments)
1413 {
1414     if (messageID.is<CoreIPC::MessageClassDrawingAreaLegacy>()) {
1415         if (m_drawingArea)
1416             m_drawingArea->didReceiveMessage(connection, messageID, arguments);
1417         return;
1418     }
1419
1420 #if ENABLE(INSPECTOR)
1421     if (messageID.is<CoreIPC::MessageClassWebInspector>()) {
1422         if (WebInspector* inspector = this->inspector())
1423             inspector->didReceiveWebInspectorMessage(connection, messageID, arguments);
1424         return;
1425     }
1426 #endif
1427
1428     didReceiveWebPageMessage(connection, messageID, arguments);
1429 }
1430
1431 CoreIPC::SyncReplyMode WebPage::didReceiveSyncMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments, CoreIPC::ArgumentEncoder* reply)
1432 {   
1433     return didReceiveSyncWebPageMessage(connection, messageID, arguments, reply);
1434 }
1435     
1436 InjectedBundleBackForwardList* WebPage::backForwardList()
1437 {
1438     if (!m_backForwardList)
1439         m_backForwardList = InjectedBundleBackForwardList::create(this);
1440     return m_backForwardList.get();
1441 }
1442
1443 #if PLATFORM(QT)
1444 void WebPage::findZoomableAreaForPoint(const WebCore::IntPoint& point)
1445 {
1446     const int minimumZoomTargetWidth = 100;
1447
1448     Frame* mainframe = m_mainFrame->coreFrame();
1449     HitTestResult result = mainframe->eventHandler()->hitTestResultAtPoint(mainframe->view()->windowToContents(point), /*allowShadowContent*/ false, /*ignoreClipping*/ true);
1450
1451     Node* node = result.innerNode();
1452     while (node && node->getRect().width() < minimumZoomTargetWidth)
1453         node = node->parentNode();
1454
1455     IntRect zoomableArea;
1456     if (node)
1457         zoomableArea = node->getRect();
1458     send(Messages::WebPageProxy::DidFindZoomableArea(zoomableArea));
1459 }
1460 #endif
1461
1462 WebPage::SandboxExtensionTracker::~SandboxExtensionTracker()
1463 {
1464     invalidate();
1465 }
1466
1467 void WebPage::SandboxExtensionTracker::invalidate()
1468 {
1469     if (m_pendingProvisionalSandboxExtension) {
1470         m_pendingProvisionalSandboxExtension->invalidate();
1471         m_pendingProvisionalSandboxExtension = 0;
1472     }
1473
1474     if (m_provisionalSandboxExtension) {
1475         m_provisionalSandboxExtension->invalidate();
1476         m_provisionalSandboxExtension = 0;
1477     }
1478
1479     if (m_committedSandboxExtension) {
1480         m_committedSandboxExtension->invalidate();
1481         m_committedSandboxExtension = 0;
1482     }
1483 }
1484
1485 void WebPage::SandboxExtensionTracker::beginLoad(WebFrame* frame, const SandboxExtension::Handle& handle)
1486 {
1487     ASSERT(frame->isMainFrame());
1488
1489     ASSERT(!m_pendingProvisionalSandboxExtension);
1490     m_pendingProvisionalSandboxExtension = SandboxExtension::create(handle);
1491 }
1492
1493 void WebPage::SandboxExtensionTracker::didStartProvisionalLoad(WebFrame* frame)
1494 {
1495     if (!frame->isMainFrame())
1496         return;
1497
1498     ASSERT(!m_provisionalSandboxExtension);
1499
1500     m_provisionalSandboxExtension = m_pendingProvisionalSandboxExtension.release();
1501     if (!m_provisionalSandboxExtension)
1502         return;
1503
1504     m_provisionalSandboxExtension->consume();
1505 }
1506
1507 void WebPage::SandboxExtensionTracker::didCommitProvisionalLoad(WebFrame* frame)
1508 {
1509     if (!frame->isMainFrame())
1510         return;
1511     
1512     ASSERT(!m_pendingProvisionalSandboxExtension);
1513
1514     // The provisional load has been committed. Invalidate the currently committed sandbox
1515     // extension and make the provisional sandbox extension the committed sandbox extension.
1516     if (m_committedSandboxExtension)
1517         m_committedSandboxExtension->invalidate();
1518
1519     m_committedSandboxExtension = m_provisionalSandboxExtension.release();
1520 }
1521
1522 void WebPage::SandboxExtensionTracker::didFailProvisionalLoad(WebFrame* frame)
1523 {
1524     if (!frame->isMainFrame())
1525         return;
1526
1527     ASSERT(!m_pendingProvisionalSandboxExtension);
1528     if (!m_provisionalSandboxExtension)
1529         return;
1530
1531     m_provisionalSandboxExtension->invalidate();
1532     m_provisionalSandboxExtension = 0;
1533 }
1534
1535 bool WebPage::hasLocalDataForURL(const KURL& url)
1536 {
1537     if (url.isLocalFile())
1538         return true;
1539
1540     FrameLoader* frameLoader = m_page->mainFrame()->loader();
1541     DocumentLoader* documentLoader = frameLoader ? frameLoader->documentLoader() : 0;
1542     if (documentLoader && documentLoader->subresource(url))
1543         return true;
1544
1545     return platformHasLocalDataForURL(url);
1546 }
1547
1548 void WebPage::setCustomTextEncodingName(const String& encoding)
1549 {
1550     m_page->mainFrame()->loader()->reloadWithOverrideEncoding(encoding);
1551 }
1552
1553 void WebPage::didRemoveBackForwardItem(uint64_t itemID)
1554 {
1555     WebBackForwardListProxy::removeItem(itemID);
1556 }
1557
1558 #if PLATFORM(MAC)
1559
1560 bool WebPage::isSpeaking()
1561 {
1562     bool result;
1563     return sendSync(Messages::WebPageProxy::GetIsSpeaking(), Messages::WebPageProxy::GetIsSpeaking::Reply(result)) && result;
1564 }
1565
1566 void WebPage::speak(const String& string)
1567 {
1568     send(Messages::WebPageProxy::Speak(string));
1569 }
1570
1571 void WebPage::stopSpeaking()
1572 {
1573     send(Messages::WebPageProxy::StopSpeaking());
1574 }
1575
1576 #endif
1577
1578 } // namespace WebKit