2011-01-24 Ryosuke Niwa <rniwa@webkit.org>
[WebKit.git] / Source / WebKit2 / UIProcess / WebPageProxy.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 "WebPageProxy.h"
27
28 #include "AuthenticationChallengeProxy.h"
29 #include "AuthenticationDecisionListener.h"
30 #include "DataReference.h"
31 #include "DrawingAreaProxy.h"
32 #include "FindIndicator.h"
33 #include "MessageID.h"
34 #include "NativeWebKeyboardEvent.h"
35 #include "PageClient.h"
36 #include "PrintInfo.h"
37 #include "SessionState.h"
38 #include "StringPairVector.h"
39 #include "TextChecker.h"
40 #include "TextCheckerState.h"
41 #include "WKContextPrivate.h"
42 #include "WebBackForwardList.h"
43 #include "WebBackForwardListItem.h"
44 #include "WebCertificateInfo.h"
45 #include "WebContext.h"
46 #include "WebContextMenuProxy.h"
47 #include "WebContextUserMessageCoders.h"
48 #include "WebCoreArgumentCoders.h"
49 #include "WebData.h"
50 #include "WebEditCommandProxy.h"
51 #include "WebEvent.h"
52 #include "WebFormSubmissionListenerProxy.h"
53 #include "WebFramePolicyListenerProxy.h"
54 #include "WebOpenPanelResultListenerProxy.h"
55 #include "WebPageCreationParameters.h"
56 #include "WebPageGroup.h"
57 #include "WebPageGroupData.h"
58 #include "WebPageMessages.h"
59 #include "WebPopupItem.h"
60 #include "WebPopupMenuProxy.h"
61 #include "WebPreferences.h"
62 #include "WebProcessManager.h"
63 #include "WebProcessMessages.h"
64 #include "WebProcessProxy.h"
65 #include "WebProtectionSpace.h"
66 #include "WebSecurityOrigin.h"
67 #include "WebURLRequest.h"
68 #include <WebCore/DragData.h>
69 #include <WebCore/FloatRect.h>
70 #include <WebCore/MIMETypeRegistry.h>
71 #include <WebCore/WindowFeatures.h>
72 #include <stdio.h>
73
74 #ifndef NDEBUG
75 #include <wtf/RefCountedLeakCounter.h>
76 #endif
77
78 // This controls what strategy we use for mouse wheel coalesing.
79 #define MERGE_WHEEL_EVENTS 0
80
81 #define MESSAGE_CHECK(assertion) MESSAGE_CHECK_BASE(assertion, process()->connection())
82
83 using namespace WebCore;
84
85 namespace WebKit {
86
87 #ifndef NDEBUG
88 static WTF::RefCountedLeakCounter webPageProxyCounter("WebPageProxy");
89 #endif
90
91 PassRefPtr<WebPageProxy> WebPageProxy::create(PageClient* pageClient, WebContext* context, WebPageGroup* pageGroup, uint64_t pageID)
92 {
93     return adoptRef(new WebPageProxy(pageClient, context, pageGroup, pageID));
94 }
95
96 WebPageProxy::WebPageProxy(PageClient* pageClient, WebContext* context, WebPageGroup* pageGroup, uint64_t pageID)
97     : m_pageClient(pageClient)
98     , m_context(context)
99     , m_pageGroup(pageGroup)
100     , m_mainFrame(0)
101     , m_userAgent(standardUserAgent())
102     , m_geolocationPermissionRequestManager(this)
103     , m_estimatedProgress(0)
104     , m_isInWindow(m_pageClient->isViewInWindow())
105     , m_isVisible(m_pageClient->isViewVisible())
106     , m_backForwardList(WebBackForwardList::create(this))
107     , m_textZoomFactor(1)
108     , m_pageZoomFactor(1)
109     , m_viewScaleFactor(1)
110     , m_drawsBackground(true)
111     , m_drawsTransparentBackground(false)
112     , m_useFixedLayout(false)
113     , m_isValid(true)
114     , m_isClosed(false)
115     , m_isInPrintingMode(false)
116     , m_inDecidePolicyForMIMEType(false)
117     , m_syncMimeTypePolicyActionIsValid(false)
118     , m_syncMimeTypePolicyAction(PolicyUse)
119     , m_syncMimeTypePolicyDownloadID(0)
120     , m_processingWheelEvent(false)
121     , m_processingMouseMoveEvent(false)
122     , m_pageID(pageID)
123 #if PLATFORM(MAC)
124     , m_isSmartInsertDeleteEnabled(TextChecker::isSmartInsertDeleteEnabled())
125 #endif
126     , m_spellDocumentTag(0)
127     , m_hasSpellDocumentTag(false)
128     , m_pendingLearnOrIgnoreWordMessageCount(0)
129     , m_mainFrameHasCustomRepresentation(false)
130     , m_currentDragOperation(DragOperationNone)
131 {
132 #ifndef NDEBUG
133     webPageProxyCounter.increment();
134 #endif
135
136     WebContext::statistics().wkPageCount++;
137
138     m_pageGroup->addPage(this);
139 }
140
141 WebPageProxy::~WebPageProxy()
142 {
143     WebContext::statistics().wkPageCount--;
144
145     if (m_hasSpellDocumentTag)
146         TextChecker::closeSpellDocumentWithTag(m_spellDocumentTag);
147
148     m_pageGroup->removePage(this);
149
150 #ifndef NDEBUG
151     webPageProxyCounter.decrement();
152 #endif
153 }
154
155 WebProcessProxy* WebPageProxy::process() const
156 {
157     return m_context->process();
158 }
159
160 bool WebPageProxy::isValid()
161 {
162     // A page that has been explicitly closed is never valid.
163     if (m_isClosed)
164         return false;
165
166     return m_isValid;
167 }
168
169 void WebPageProxy::setDrawingArea(PassOwnPtr<DrawingAreaProxy> drawingArea)
170 {
171     if (drawingArea == m_drawingArea)
172         return;
173
174     m_drawingArea = drawingArea;
175 }
176
177 void WebPageProxy::initializeLoaderClient(const WKPageLoaderClient* loadClient)
178 {
179     m_loaderClient.initialize(loadClient);
180 }
181
182 void WebPageProxy::initializePolicyClient(const WKPagePolicyClient* policyClient)
183 {
184     m_policyClient.initialize(policyClient);
185 }
186
187 void WebPageProxy::initializeFormClient(const WKPageFormClient* formClient)
188 {
189     m_formClient.initialize(formClient);
190 }
191
192 void WebPageProxy::initializeResourceLoadClient(const WKPageResourceLoadClient* client)
193 {
194     m_resourceLoadClient.initialize(client);
195 }
196
197 void WebPageProxy::initializeUIClient(const WKPageUIClient* client)
198 {
199     m_uiClient.initialize(client);
200 }
201
202 void WebPageProxy::initializeFindClient(const WKPageFindClient* client)
203 {
204     m_findClient.initialize(client);
205 }
206
207 void WebPageProxy::initializeContextMenuClient(const WKPageContextMenuClient* client)
208 {
209     m_contextMenuClient.initialize(client);
210 }
211
212 void WebPageProxy::reattachToWebProcess()
213 {
214     m_isValid = true;
215
216     context()->relaunchProcessIfNecessary();
217     process()->addExistingWebPage(this, m_pageID);
218
219     initializeWebPage();
220
221     m_pageClient->didRelaunchProcess();
222 }
223
224 void WebPageProxy::reattachToWebProcessWithItem(WebBackForwardListItem* item)
225 {
226     if (item && item != m_backForwardList->currentItem())
227         m_backForwardList->goToItem(item);
228     
229     reattachToWebProcess();
230     
231     if (item)
232         process()->send(Messages::WebPage::GoToBackForwardItem(item->itemID()), m_pageID);
233 }
234
235 void WebPageProxy::initializeWebPage()
236 {
237     ASSERT(isValid());
238
239     BackForwardListItemVector items = m_backForwardList->entries();
240     for (size_t i = 0; i < items.size(); ++i)
241         process()->registerNewWebBackForwardListItem(items[i].get());
242
243     m_drawingArea = m_pageClient->createDrawingAreaProxy();
244     ASSERT(m_drawingArea);
245
246     process()->send(Messages::WebProcess::CreateWebPage(m_pageID, creationParameters()), 0);
247 }
248
249 void WebPageProxy::close()
250 {
251     if (!isValid())
252         return;
253
254     m_isClosed = true;
255
256     m_backForwardList->pageClosed();
257
258     process()->disconnectFramesFromPage(this);
259     m_mainFrame = 0;
260
261 #if ENABLE(INSPECTOR)
262     if (m_inspector) {
263         m_inspector->invalidate();
264         m_inspector = 0;
265     }
266 #endif
267
268     if (m_openPanelResultListener) {
269         m_openPanelResultListener->invalidate();
270         m_openPanelResultListener = 0;
271     }
272
273     m_geolocationPermissionRequestManager.invalidateRequests();
274
275     m_toolTip = String();
276
277     invalidateCallbackMap(m_dataCallbacks);
278     invalidateCallbackMap(m_stringCallbacks);
279
280     Vector<WebEditCommandProxy*> editCommandVector;
281     copyToVector(m_editCommandSet, editCommandVector);
282     m_editCommandSet.clear();
283     for (size_t i = 0, size = editCommandVector.size(); i < size; ++i)
284         editCommandVector[i]->invalidate();
285
286     m_activePopupMenu = 0;
287
288     m_estimatedProgress = 0.0;
289     
290     m_loaderClient.initialize(0);
291     m_policyClient.initialize(0);
292     m_uiClient.initialize(0);
293
294     m_drawingArea.clear();
295
296     process()->send(Messages::WebPage::Close(), m_pageID);
297     process()->removeWebPage(m_pageID);
298 }
299
300 bool WebPageProxy::tryClose()
301 {
302     if (!isValid())
303         return true;
304
305     process()->send(Messages::WebPage::TryClose(), m_pageID);
306     return false;
307 }
308
309 static void initializeSandboxExtensionHandle(const KURL& url, SandboxExtension::Handle& sandboxExtensionHandle)
310 {
311     if (!url.isLocalFile())
312         return;
313
314     SandboxExtension::createHandle("/", SandboxExtension::ReadOnly, sandboxExtensionHandle);
315 }
316
317 void WebPageProxy::loadURL(const String& url)
318 {
319     if (!isValid())
320         reattachToWebProcess();
321
322     SandboxExtension::Handle sandboxExtensionHandle;
323     initializeSandboxExtensionHandle(KURL(KURL(), url), sandboxExtensionHandle);
324     process()->send(Messages::WebPage::LoadURL(url, sandboxExtensionHandle), m_pageID);
325 }
326
327 void WebPageProxy::loadURLRequest(WebURLRequest* urlRequest)
328 {
329     if (!isValid())
330         reattachToWebProcess();
331
332     SandboxExtension::Handle sandboxExtensionHandle;
333     initializeSandboxExtensionHandle(urlRequest->resourceRequest().url(), sandboxExtensionHandle);
334     process()->send(Messages::WebPage::LoadURLRequest(urlRequest->resourceRequest(), sandboxExtensionHandle), m_pageID);
335 }
336
337 void WebPageProxy::loadHTMLString(const String& htmlString, const String& baseURL)
338 {
339     if (!isValid())
340         return;
341     process()->send(Messages::WebPage::LoadHTMLString(htmlString, baseURL), m_pageID);
342 }
343
344 void WebPageProxy::loadAlternateHTMLString(const String& htmlString, const String& baseURL, const String& unreachableURL)
345 {
346     if (!isValid())
347         return;
348
349     if (!m_mainFrame)
350         return;
351
352     m_mainFrame->setUnreachableURL(unreachableURL);
353     process()->send(Messages::WebPage::LoadAlternateHTMLString(htmlString, baseURL, unreachableURL), m_pageID);
354 }
355
356 void WebPageProxy::loadPlainTextString(const String& string)
357 {
358     if (!isValid())
359         return;
360     process()->send(Messages::WebPage::LoadPlainTextString(string), m_pageID);
361 }
362
363 void WebPageProxy::stopLoading()
364 {
365     if (!isValid())
366         return;
367     process()->send(Messages::WebPage::StopLoading(), m_pageID);
368 }
369
370 void WebPageProxy::reload(bool reloadFromOrigin)
371 {
372     if (!isValid()) {
373         reattachToWebProcessWithItem(m_backForwardList->currentItem());
374         return;
375     }
376
377     process()->send(Messages::WebPage::Reload(reloadFromOrigin), m_pageID);
378 }
379
380 void WebPageProxy::goForward()
381 {
382     if (!isValid()) {
383         reattachToWebProcessWithItem(m_backForwardList->forwardItem());
384         return;
385     }
386
387     if (!canGoForward())
388         return;
389
390     process()->send(Messages::WebPage::GoForward(m_backForwardList->forwardItem()->itemID()), m_pageID);
391 }
392
393 bool WebPageProxy::canGoForward() const
394 {
395     return m_backForwardList->forwardItem();
396 }
397
398 void WebPageProxy::goBack()
399 {
400     if (!isValid()) {
401         reattachToWebProcessWithItem(m_backForwardList->backItem());
402         return;
403     }
404
405     if (!canGoBack())
406         return;
407
408     process()->send(Messages::WebPage::GoBack(m_backForwardList->backItem()->itemID()), m_pageID);
409 }
410
411 bool WebPageProxy::canGoBack() const
412 {
413     return m_backForwardList->backItem();
414 }
415
416 void WebPageProxy::goToBackForwardItem(WebBackForwardListItem* item)
417 {
418     if (!isValid()) {
419         reattachToWebProcessWithItem(item);
420         return;
421     }
422
423     process()->send(Messages::WebPage::GoToBackForwardItem(item->itemID()), m_pageID);
424 }
425
426 void WebPageProxy::didChangeBackForwardList()
427 {
428     m_loaderClient.didChangeBackForwardList(this);
429 }
430
431     
432 bool WebPageProxy::canShowMIMEType(const String& mimeType) const
433 {
434     if (MIMETypeRegistry::isSupportedNonImageMIMEType(mimeType))
435         return true;
436
437     if (MIMETypeRegistry::isSupportedImageMIMEType(mimeType))
438         return true;
439     
440     String newMimeType = mimeType;
441     PluginInfoStore::Plugin plugin = context()->pluginInfoStore()->findPlugin(newMimeType, KURL());
442     if (!plugin.path.isNull())
443         return true;
444
445     return false;
446 }
447
448 void WebPageProxy::setDrawsBackground(bool drawsBackground)
449 {
450     if (m_drawsBackground == drawsBackground)
451         return;
452
453     m_drawsBackground = drawsBackground;
454
455     if (isValid())
456         process()->send(Messages::WebPage::SetDrawsBackground(drawsBackground), m_pageID);
457 }
458
459 void WebPageProxy::setDrawsTransparentBackground(bool drawsTransparentBackground)
460 {
461     if (m_drawsTransparentBackground == drawsTransparentBackground)
462         return;
463
464     m_drawsTransparentBackground = drawsTransparentBackground;
465
466     if (isValid())
467         process()->send(Messages::WebPage::SetDrawsTransparentBackground(drawsTransparentBackground), m_pageID);
468 }
469
470 void WebPageProxy::setViewNeedsDisplay(const IntRect& rect)
471 {
472     m_pageClient->setViewNeedsDisplay(rect);
473 }
474
475 void WebPageProxy::displayView()
476 {
477     m_pageClient->displayView();
478 }
479
480 void WebPageProxy::scrollView(const IntRect& scrollRect, const IntSize& scrollOffset)
481 {
482     m_pageClient->scrollView(scrollRect, scrollOffset);
483 }
484
485 void WebPageProxy::viewStateDidChange(ViewStateFlags flags)
486 {
487     if (!isValid())
488         return;
489
490     if (flags & ViewIsFocused)
491         process()->send(Messages::WebPage::SetFocused(m_pageClient->isViewFocused()), m_pageID);
492
493     if (flags & ViewWindowIsActive)
494         process()->send(Messages::WebPage::SetActive(m_pageClient->isViewWindowActive()), m_pageID);
495
496     if (flags & ViewIsVisible) {
497         bool isVisible = m_pageClient->isViewVisible();
498         if (isVisible != m_isVisible) {
499             m_isVisible = isVisible;
500             m_drawingArea->visibilityDidChange();
501             m_drawingArea->setPageIsVisible(isVisible);
502         }
503     }
504
505     if (flags & ViewIsInWindow) {
506         bool isInWindow = m_pageClient->isViewInWindow();
507         if (m_isInWindow != isInWindow) {
508             m_isInWindow = isInWindow;
509             process()->send(Messages::WebPage::SetIsInWindow(isInWindow), m_pageID);
510         }
511     }
512 }
513
514 IntSize WebPageProxy::viewSize() const
515 {
516     return m_pageClient->viewSize();
517 }
518
519 void WebPageProxy::setInitialFocus(bool forward)
520 {
521     if (!isValid())
522         return;
523     process()->send(Messages::WebPage::SetInitialFocus(forward), m_pageID);
524 }
525
526 void WebPageProxy::setWindowResizerSize(const IntSize& windowResizerSize)
527 {
528     if (!isValid())
529         return;
530     process()->send(Messages::WebPage::SetWindowResizerSize(windowResizerSize), m_pageID);
531 }
532
533 void WebPageProxy::validateMenuItem(const String& commandName)
534 {
535     if (!isValid())
536         return;
537     process()->send(Messages::WebPage::ValidateMenuItem(commandName), m_pageID);
538 }
539     
540 void WebPageProxy::executeEditCommand(const String& commandName)
541 {
542     if (!isValid())
543         return;
544
545     process()->send(Messages::WebPage::ExecuteEditCommand(commandName), m_pageID);
546 }
547     
548 #if PLATFORM(MAC)
549 void WebPageProxy::updateWindowIsVisible(bool windowIsVisible)
550 {
551     if (!isValid())
552         return;
553     process()->send(Messages::WebPage::SetWindowIsVisible(windowIsVisible), m_pageID);
554 }
555
556 void WebPageProxy::windowAndViewFramesChanged(const IntRect& windowFrameInScreenCoordinates, const IntRect& viewFrameInWindowCoordinates, const IntPoint& accessibilityViewCoordinates)
557 {
558     if (!isValid())
559         return;
560
561     process()->send(Messages::WebPage::WindowAndViewFramesChanged(windowFrameInScreenCoordinates, viewFrameInWindowCoordinates, accessibilityViewCoordinates), m_pageID);
562 }
563
564 void WebPageProxy::getMarkedRange(uint64_t& location, uint64_t& length)
565 {
566     process()->sendSync(Messages::WebPage::GetMarkedRange(), Messages::WebPage::GetMarkedRange::Reply(location, length), m_pageID);
567 }
568     
569 uint64_t WebPageProxy::characterIndexForPoint(const IntPoint point)
570 {
571     uint64_t result;
572     process()->sendSync(Messages::WebPage::CharacterIndexForPoint(point), Messages::WebPage::CharacterIndexForPoint::Reply(result), m_pageID);
573     return result;
574 }
575
576 WebCore::IntRect WebPageProxy::firstRectForCharacterRange(uint64_t location, uint64_t length)
577 {
578     IntRect resultRect;
579     process()->sendSync(Messages::WebPage::FirstRectForCharacterRange(location, length), Messages::WebPage::FirstRectForCharacterRange::Reply(resultRect), m_pageID);
580     return resultRect;
581 }
582 #elif PLATFORM(WIN)
583 WebCore::IntRect WebPageProxy::firstRectForCharacterInSelectedRange(int characterPosition)
584 {
585     IntRect resultRect;
586     process()->sendSync(Messages::WebPage::FirstRectForCharacterInSelectedRange(characterPosition), Messages::WebPage::FirstRectForCharacterInSelectedRange::Reply(resultRect), m_pageID);
587     return resultRect;
588 }
589
590 String WebPageProxy::getSelectedText()
591 {
592     String text;
593     process()->sendSync(Messages::WebPage::GetSelectedText(), Messages::WebPage::GetSelectedText::Reply(text), m_pageID);
594     return text;
595 }
596 #endif
597
598 #if ENABLE(TILED_BACKING_STORE)
599 void WebPageProxy::setActualVisibleContentRect(const IntRect& rect)
600 {
601     if (!isValid())
602         return;
603
604     process()->send(Messages::WebPage::SetActualVisibleContentRect(rect), m_pageID);
605 }
606 #endif
607
608 void WebPageProxy::performDragControllerAction(DragControllerAction action, WebCore::DragData* dragData, const String& dragStorageName)
609 {
610     if (!isValid())
611         return;
612     process()->send(Messages::WebPage::PerformDragControllerAction(action, dragData->clientPosition(), dragData->globalPosition(), dragData->draggingSourceOperationMask(), dragStorageName, dragData->flags()), m_pageID);
613 }
614
615 void WebPageProxy::didPerformDragControllerAction(uint64_t resultOperation)
616 {
617     m_currentDragOperation = static_cast<DragOperation>(resultOperation);
618 }
619
620 #if PLATFORM(MAC)
621 void WebPageProxy::setDragImage(const WebCore::IntPoint& clientPosition, const IntSize& imageSize, const SharedMemory::Handle& dragImageHandle, bool isLinkDrag)
622 {
623     RefPtr<ShareableBitmap> dragImage = ShareableBitmap::create(imageSize, dragImageHandle);
624     if (!dragImage)
625         return;
626     
627     m_pageClient->setDragImage(clientPosition, imageSize, dragImage.release(), isLinkDrag);
628 }
629 #endif
630
631 void WebPageProxy::dragEnded(const WebCore::IntPoint& clientPosition, const WebCore::IntPoint& globalPosition, uint64_t operation)
632 {
633     if (!isValid())
634         return;
635     process()->send(Messages::WebPage::DragEnded(clientPosition, globalPosition, operation), m_pageID);
636 }
637
638 void WebPageProxy::handleMouseEvent(const WebMouseEvent& event)
639 {
640     if (!isValid())
641         return;
642
643     // NOTE: This does not start the responsiveness timer because mouse move should not indicate interaction.
644     if (event.type() != WebEvent::MouseMove)
645         process()->responsivenessTimer()->start();
646     else {
647         if (m_processingMouseMoveEvent) {
648             m_nextMouseMoveEvent = adoptPtr(new WebMouseEvent(event));
649             return;
650         }
651
652         m_processingMouseMoveEvent = true;
653     }
654
655     process()->send(Messages::WebPage::MouseEvent(event), m_pageID);
656 }
657
658 static PassOwnPtr<WebWheelEvent> coalesceWheelEvents(WebWheelEvent* oldNextWheelEvent, const WebWheelEvent& newWheelEvent)
659 {
660 #if MERGE_WHEEL_EVENTS
661     // Merge model: Combine wheel event deltas (and wheel ticks) into a single wheel event.
662     if (!oldNextWheelEvent)
663         return adoptPtr(new WebWheelEvent(newWheelEvent));
664
665     if (oldNextWheelEvent->position() != newWheelEvent.position() || oldNextWheelEvent->modifiers() != newWheelEvent.modifiers() || oldNextWheelEvent->granularity() != newWheelEvent.granularity())
666         return adoptPtr(new WebWheelEvent(newWheelEvent));
667
668     FloatSize mergedDelta = oldNextWheelEvent->delta() + newWheelEvent.delta();
669     FloatSize mergedWheelTicks = oldNextWheelEvent->wheelTicks() + newWheelEvent.wheelTicks();
670
671     return adoptPtr(new WebWheelEvent(WebEvent::Wheel, newWheelEvent.position(), newWheelEvent.globalPosition(), mergedDelta, mergedWheelTicks, newWheelEvent.granularity(), newWheelEvent.modifiers(), newWheelEvent.timestamp()));
672 #else
673     // Simple model: Just keep the last event, dropping all interim events.
674     return adoptPtr(new WebWheelEvent(newWheelEvent));
675 #endif
676 }
677
678 void WebPageProxy::handleWheelEvent(const WebWheelEvent& event)
679 {
680     if (!isValid())
681         return;
682
683     if (m_processingWheelEvent) {
684         m_nextWheelEvent = coalesceWheelEvents(m_nextWheelEvent.get(), event);
685         return;
686     }
687
688     process()->responsivenessTimer()->start();
689     process()->send(Messages::WebPage::WheelEvent(event), m_pageID);
690     m_processingWheelEvent = true;
691 }
692
693 void WebPageProxy::handleKeyboardEvent(const NativeWebKeyboardEvent& event)
694 {
695     if (!isValid())
696         return;
697
698     m_keyEventQueue.append(event);
699
700     process()->responsivenessTimer()->start();
701     process()->send(Messages::WebPage::KeyEvent(event), m_pageID);
702 }
703
704 #if ENABLE(TOUCH_EVENTS)
705 void WebPageProxy::handleTouchEvent(const WebTouchEvent& event)
706 {
707     if (!isValid())
708         return;
709     process()->send(Messages::WebPage::TouchEvent(event), m_pageID); 
710 }
711 #endif
712
713 void WebPageProxy::receivedPolicyDecision(PolicyAction action, WebFrameProxy* frame, uint64_t listenerID)
714 {
715     if (!isValid())
716         return;
717
718     uint64_t downloadID = 0;
719     if (action == PolicyDownload) {
720         // Create a download proxy.
721         downloadID = context()->createDownloadProxy();
722     }
723
724     // If we received a policy decision while in decidePolicyForMIMEType the decision will 
725     // be sent back to the web process by decidePolicyForMIMEType. 
726     if (m_inDecidePolicyForMIMEType) {
727         m_syncMimeTypePolicyActionIsValid = true;
728         m_syncMimeTypePolicyAction = action;
729         m_syncMimeTypePolicyDownloadID = downloadID;
730         return;
731     }
732
733     process()->send(Messages::WebPage::DidReceivePolicyDecision(frame->frameID(), listenerID, action, downloadID), m_pageID);
734 }
735
736 String WebPageProxy::pageTitle() const
737 {
738     // Return the null string if there is no main frame (e.g. nothing has been loaded in the page yet, WebProcess has
739     // crashed, page has been closed).
740     if (!m_mainFrame)
741         return String();
742
743     return m_mainFrame->title();
744 }
745
746 void WebPageProxy::setUserAgent(const String& userAgent)
747 {
748     if (m_userAgent == userAgent)
749         return;
750     m_userAgent = userAgent;
751
752     if (!isValid())
753         return;
754     process()->send(Messages::WebPage::SetUserAgent(m_userAgent), m_pageID);
755 }
756
757 void WebPageProxy::setApplicationNameForUserAgent(const String& applicationName)
758 {
759     if (m_applicationNameForUserAgent == applicationName)
760         return;
761
762     m_applicationNameForUserAgent = applicationName;
763     if (!m_customUserAgent.isEmpty())
764         return;
765
766     setUserAgent(standardUserAgent(m_applicationNameForUserAgent));
767 }
768
769 void WebPageProxy::setCustomUserAgent(const String& customUserAgent)
770 {
771     if (m_customUserAgent == customUserAgent)
772         return;
773
774     m_customUserAgent = customUserAgent;
775
776     if (m_customUserAgent.isEmpty()) {
777         setUserAgent(standardUserAgent(m_applicationNameForUserAgent));
778         return;
779     }
780
781     setUserAgent(m_customUserAgent);
782 }
783
784 bool WebPageProxy::supportsTextEncoding() const
785 {
786     return !m_mainFrameHasCustomRepresentation && m_mainFrame && !m_mainFrame->isDisplayingStandaloneImageDocument();
787 }
788
789 void WebPageProxy::setCustomTextEncodingName(const String& encodingName)
790 {
791     if (m_customTextEncodingName == encodingName)
792         return;
793     m_customTextEncodingName = encodingName;
794
795     if (!isValid())
796         return;
797     process()->send(Messages::WebPage::SetCustomTextEncodingName(encodingName), m_pageID);
798 }
799
800 void WebPageProxy::terminateProcess()
801 {
802     if (!isValid())
803         return;
804
805     process()->terminate();
806 }
807
808 #if !PLATFORM(CF) || defined(BUILDING_QT__)
809 PassRefPtr<WebData> WebPageProxy::sessionStateData(WebPageProxySessionStateFilterCallback, void* context) const
810 {
811     // FIXME: Return session state data for saving Page state.
812     return 0;
813 }
814
815 void WebPageProxy::restoreFromSessionStateData(WebData*)
816 {
817     // FIXME: Restore the Page from the passed in session state data.
818 }
819 #endif
820
821 bool WebPageProxy::supportsTextZoom() const
822 {
823     if (m_mainFrameHasCustomRepresentation)
824         return false;
825
826     // FIXME: This should also return false for standalone media and plug-in documents.
827     if (!m_mainFrame || m_mainFrame->isDisplayingStandaloneImageDocument())
828         return false;
829
830     return true;
831 }
832  
833 void WebPageProxy::setTextZoomFactor(double zoomFactor)
834 {
835     if (!isValid())
836         return;
837
838     if (m_mainFrameHasCustomRepresentation)
839         return;
840
841     if (m_textZoomFactor == zoomFactor)
842         return;
843
844     m_textZoomFactor = zoomFactor;
845     process()->send(Messages::WebPage::SetTextZoomFactor(m_textZoomFactor), m_pageID); 
846 }
847
848 double WebPageProxy::pageZoomFactor() const
849 {
850     return m_mainFrameHasCustomRepresentation ? m_pageClient->customRepresentationZoomFactor() : m_pageZoomFactor;
851 }
852
853 void WebPageProxy::setPageZoomFactor(double zoomFactor)
854 {
855     if (!isValid())
856         return;
857
858     if (m_mainFrameHasCustomRepresentation) {
859         m_pageClient->setCustomRepresentationZoomFactor(zoomFactor);
860         return;
861     }
862
863     if (m_pageZoomFactor == zoomFactor)
864         return;
865
866     m_pageZoomFactor = zoomFactor;
867     process()->send(Messages::WebPage::SetPageZoomFactor(m_pageZoomFactor), m_pageID); 
868 }
869
870 void WebPageProxy::setPageAndTextZoomFactors(double pageZoomFactor, double textZoomFactor)
871 {
872     if (!isValid())
873         return;
874
875     if (m_mainFrameHasCustomRepresentation) {
876         m_pageClient->setCustomRepresentationZoomFactor(pageZoomFactor);
877         return;
878     }
879
880     if (m_pageZoomFactor == pageZoomFactor && m_textZoomFactor == textZoomFactor)
881         return;
882
883     m_pageZoomFactor = pageZoomFactor;
884     m_textZoomFactor = textZoomFactor;
885     process()->send(Messages::WebPage::SetPageAndTextZoomFactors(m_pageZoomFactor, m_textZoomFactor), m_pageID); 
886 }
887
888 void WebPageProxy::scaleWebView(double scale, const IntPoint& origin)
889 {
890     if (!isValid())
891         return;
892
893     m_viewScaleFactor = scale;
894     process()->send(Messages::WebPage::ScaleWebView(scale, origin), m_pageID);
895 }
896
897 void WebPageProxy::setUseFixedLayout(bool fixed)
898 {
899     if (!isValid())
900         return;
901
902     if (fixed == m_useFixedLayout)
903         return;
904
905     m_useFixedLayout = fixed;
906     if (!fixed)
907         m_fixedLayoutSize = IntSize();
908     process()->send(Messages::WebPage::SetUseFixedLayout(fixed), m_pageID);
909 }
910
911 void WebPageProxy::setFixedLayoutSize(const IntSize& size)
912 {
913     if (!isValid())
914         return;
915
916     if (size == m_fixedLayoutSize)
917         return;
918
919     m_fixedLayoutSize = size;
920     process()->send(Messages::WebPage::SetFixedLayoutSize(size), m_pageID);
921 }
922
923 void WebPageProxy::findString(const String& string, FindOptions options, unsigned maxMatchCount)
924 {
925     process()->send(Messages::WebPage::FindString(string, options, maxMatchCount), m_pageID);
926 }
927
928 void WebPageProxy::hideFindUI()
929 {
930     process()->send(Messages::WebPage::HideFindUI(), m_pageID);
931 }
932
933 void WebPageProxy::countStringMatches(const String& string, FindOptions options, unsigned maxMatchCount)
934 {
935     process()->send(Messages::WebPage::CountStringMatches(string, options, maxMatchCount), m_pageID);
936 }
937     
938 void WebPageProxy::runJavaScriptInMainFrame(const String& script, PassRefPtr<StringCallback> prpCallback)
939 {
940     RefPtr<StringCallback> callback = prpCallback;
941     uint64_t callbackID = callback->callbackID();
942     m_stringCallbacks.set(callbackID, callback.get());
943     process()->send(Messages::WebPage::RunJavaScriptInMainFrame(script, callbackID), m_pageID);
944 }
945
946 void WebPageProxy::getRenderTreeExternalRepresentation(PassRefPtr<StringCallback> prpCallback)
947 {
948     RefPtr<StringCallback> callback = prpCallback;
949     uint64_t callbackID = callback->callbackID();
950     m_stringCallbacks.set(callbackID, callback.get());
951     process()->send(Messages::WebPage::GetRenderTreeExternalRepresentation(callbackID), m_pageID);
952 }
953
954 void WebPageProxy::getSourceForFrame(WebFrameProxy* frame, PassRefPtr<StringCallback> prpCallback)
955 {
956     RefPtr<StringCallback> callback = prpCallback;
957     uint64_t callbackID = callback->callbackID();
958     m_stringCallbacks.set(callbackID, callback.get());
959     process()->send(Messages::WebPage::GetSourceForFrame(frame->frameID(), callbackID), m_pageID);
960 }
961
962 void WebPageProxy::getContentsAsString(PassRefPtr<StringCallback> prpCallback)
963 {
964     RefPtr<StringCallback> callback = prpCallback;
965     uint64_t callbackID = callback->callbackID();
966     m_stringCallbacks.set(callbackID, callback.get());
967     process()->send(Messages::WebPage::GetContentsAsString(callbackID), m_pageID);
968 }
969
970 void WebPageProxy::getSelectionOrContentsAsString(PassRefPtr<StringCallback> prpCallback)
971 {
972     RefPtr<StringCallback> callback = prpCallback;
973     uint64_t callbackID = callback->callbackID();
974     m_stringCallbacks.set(callbackID, callback.get());
975     process()->send(Messages::WebPage::GetSelectionOrContentsAsString(callbackID), m_pageID);
976 }
977
978 void WebPageProxy::getMainResourceDataOfFrame(WebFrameProxy* frame, PassRefPtr<DataCallback> prpCallback)
979 {
980     RefPtr<DataCallback> callback = prpCallback;
981     uint64_t callbackID = callback->callbackID();
982     m_dataCallbacks.set(callbackID, callback.get());
983     process()->send(Messages::WebPage::GetMainResourceDataOfFrame(frame->frameID(), callbackID), m_pageID);
984 }
985
986 void WebPageProxy::getResourceDataFromFrame(WebFrameProxy* frame, WebURL* resourceURL, PassRefPtr<DataCallback> prpCallback)
987 {
988     RefPtr<DataCallback> callback = prpCallback;
989     uint64_t callbackID = callback->callbackID();
990     m_dataCallbacks.set(callbackID, callback.get());
991     process()->send(Messages::WebPage::GetResourceDataFromFrame(frame->frameID(), resourceURL->string(), callbackID), m_pageID);
992 }
993
994 void WebPageProxy::getWebArchiveOfFrame(WebFrameProxy* frame, PassRefPtr<DataCallback> prpCallback)
995 {
996     RefPtr<DataCallback> callback = prpCallback;
997     uint64_t callbackID = callback->callbackID();
998     m_dataCallbacks.set(callbackID, callback.get());
999     process()->send(Messages::WebPage::GetWebArchiveOfFrame(frame->frameID(), callbackID), m_pageID);
1000 }
1001
1002 void WebPageProxy::forceRepaint(PassRefPtr<VoidCallback> prpCallback)
1003 {
1004     RefPtr<VoidCallback> callback = prpCallback;
1005
1006     if (!isValid()) {
1007         callback->invalidate();
1008         return;
1009     }
1010
1011     uint64_t callbackID = callback->callbackID();
1012     m_voidCallbacks.set(callbackID, callback.get());
1013     process()->send(Messages::WebPage::ForceRepaint(callbackID), m_pageID); 
1014 }
1015
1016 void WebPageProxy::preferencesDidChange()
1017 {
1018     if (!isValid())
1019         return;
1020
1021     // FIXME: It probably makes more sense to send individual preference changes.
1022     // However, WebKitTestRunner depends on getting a preference change notification
1023     // even if nothing changed in UI process, so that overrides get removed.
1024     process()->send(Messages::WebPage::PreferencesDidChange(pageGroup()->preferences()->store()), m_pageID);
1025 }
1026
1027 #if ENABLE(TILED_BACKING_STORE)
1028 void WebPageProxy::setResizesToContentsUsingLayoutSize(const WebCore::IntSize& targetLayoutSize)
1029 {
1030     process()->send(Messages::WebPage::SetResizesToContentsUsingLayoutSize(targetLayoutSize), m_pageID);
1031 }
1032 #endif
1033
1034 void WebPageProxy::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments)
1035 {
1036 #ifdef __APPLE__
1037     if (messageID.is<CoreIPC::MessageClassDrawingAreaProxy>()) {
1038         m_drawingArea->didReceiveDrawingAreaProxyMessage(connection, messageID, arguments);
1039         return;
1040     }
1041 #endif
1042
1043     if (messageID.is<CoreIPC::MessageClassDrawingAreaProxyLegacy>()) {
1044         m_drawingArea->didReceiveMessage(connection, messageID, arguments);
1045         return;
1046     }
1047
1048 #if ENABLE(INSPECTOR)
1049     if (messageID.is<CoreIPC::MessageClassWebInspectorProxy>()) {
1050         if (WebInspectorProxy* inspector = this->inspector())
1051             inspector->didReceiveWebInspectorProxyMessage(connection, messageID, arguments);
1052         return;
1053     }
1054 #endif
1055
1056     didReceiveWebPageProxyMessage(connection, messageID, arguments);
1057 }
1058
1059 void WebPageProxy::didReceiveSyncMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments, CoreIPC::ArgumentEncoder* reply)
1060 {
1061     if (messageID.is<CoreIPC::MessageClassDrawingAreaProxyLegacy>()) {
1062         m_drawingArea->didReceiveSyncMessage(connection, messageID, arguments, reply);
1063         return;
1064     }
1065
1066 #if ENABLE(INSPECTOR)
1067     if (messageID.is<CoreIPC::MessageClassWebInspectorProxy>()) {
1068         if (WebInspectorProxy* inspector = this->inspector())
1069             inspector->didReceiveSyncWebInspectorProxyMessage(connection, messageID, arguments, reply);
1070         return;
1071     }
1072 #endif
1073
1074     // FIXME: Do something with reply.
1075     didReceiveSyncWebPageProxyMessage(connection, messageID, arguments, reply);
1076 }
1077
1078 #if PLATFORM(MAC)
1079 void WebPageProxy::interpretKeyEvent(uint32_t type, Vector<KeypressCommand>& commandsList, uint32_t selectionStart, uint32_t selectionEnd, Vector<CompositionUnderline>& underlines)
1080 {
1081     m_pageClient->interceptKeyEvent(m_keyEventQueue.first(), commandsList, selectionStart, selectionEnd, underlines);
1082 }
1083 #endif
1084
1085 void WebPageProxy::didCreateMainFrame(uint64_t frameID)
1086 {
1087     MESSAGE_CHECK(!m_mainFrame);
1088     MESSAGE_CHECK(process()->canCreateFrame(frameID));
1089
1090     m_mainFrame = WebFrameProxy::create(this, frameID);
1091
1092     // Add the frame to the process wide map.
1093     process()->frameCreated(frameID, m_mainFrame.get());
1094 }
1095
1096 void WebPageProxy::didCreateSubframe(uint64_t frameID, uint64_t parentFrameID)
1097 {
1098     MESSAGE_CHECK(m_mainFrame);
1099
1100     WebFrameProxy* parentFrame = process()->webFrame(parentFrameID);
1101     MESSAGE_CHECK(parentFrame);
1102     MESSAGE_CHECK(parentFrame->isDescendantOf(m_mainFrame.get()));
1103
1104     MESSAGE_CHECK(process()->canCreateFrame(frameID));
1105     
1106     RefPtr<WebFrameProxy> subFrame = WebFrameProxy::create(this, frameID);
1107
1108     // Add the frame to the process wide map.
1109     process()->frameCreated(frameID, subFrame.get());
1110
1111     // Insert the frame into the frame hierarchy.
1112     parentFrame->appendChild(subFrame.get());
1113 }
1114
1115 static bool isDisconnectedFrame(WebFrameProxy* frame)
1116 {
1117     return !frame->page() || !frame->page()->mainFrame() || !frame->isDescendantOf(frame->page()->mainFrame());
1118 }
1119
1120 void WebPageProxy::didSaveFrameToPageCache(uint64_t frameID)
1121 {
1122     MESSAGE_CHECK(m_mainFrame);
1123
1124     WebFrameProxy* subframe = process()->webFrame(frameID);
1125     MESSAGE_CHECK(subframe);
1126
1127     if (isDisconnectedFrame(subframe))
1128         return;
1129
1130     MESSAGE_CHECK(subframe->isDescendantOf(m_mainFrame.get()));
1131
1132     subframe->didRemoveFromHierarchy();
1133 }
1134
1135 void WebPageProxy::didRestoreFrameFromPageCache(uint64_t frameID, uint64_t parentFrameID)
1136 {
1137     MESSAGE_CHECK(m_mainFrame);
1138
1139     WebFrameProxy* subframe = process()->webFrame(frameID);
1140     MESSAGE_CHECK(subframe);
1141     MESSAGE_CHECK(!subframe->parentFrame());
1142     MESSAGE_CHECK(subframe->page() == m_mainFrame->page());
1143
1144     WebFrameProxy* parentFrame = process()->webFrame(parentFrameID);
1145     MESSAGE_CHECK(parentFrame);
1146     MESSAGE_CHECK(parentFrame->isDescendantOf(m_mainFrame.get()));
1147
1148     // Insert the frame into the frame hierarchy.
1149     parentFrame->appendChild(subframe);
1150 }
1151
1152 void WebPageProxy::didStartProgress()
1153 {
1154     m_estimatedProgress = 0.0;
1155     
1156     m_loaderClient.didStartProgress(this);
1157 }
1158
1159 void WebPageProxy::didChangeProgress(double value)
1160 {
1161     m_estimatedProgress = value;
1162     
1163     m_loaderClient.didChangeProgress(this);
1164 }
1165
1166 void WebPageProxy::didFinishProgress()
1167 {
1168     m_estimatedProgress = 1.0;
1169
1170     m_loaderClient.didFinishProgress(this);
1171 }
1172
1173 void WebPageProxy::didStartProvisionalLoadForFrame(uint64_t frameID, const String& url, bool loadingSubstituteDataForUnreachableURL, CoreIPC::ArgumentDecoder* arguments)
1174 {
1175     RefPtr<APIObject> userData;
1176     WebContextUserMessageDecoder messageDecoder(userData, context());
1177     if (!arguments->decode(messageDecoder))
1178         return;
1179
1180     WebFrameProxy* frame = process()->webFrame(frameID);
1181     MESSAGE_CHECK(frame);
1182
1183     if (!loadingSubstituteDataForUnreachableURL)
1184         frame->setUnreachableURL(String());
1185
1186     frame->didStartProvisionalLoad(url);
1187     m_loaderClient.didStartProvisionalLoadForFrame(this, frame, userData.get());
1188 }
1189
1190 void WebPageProxy::didReceiveServerRedirectForProvisionalLoadForFrame(uint64_t frameID, const String& url, CoreIPC::ArgumentDecoder* arguments)
1191 {
1192     RefPtr<APIObject> userData;
1193     WebContextUserMessageDecoder messageDecoder(userData, context());
1194     if (!arguments->decode(messageDecoder))
1195         return;
1196
1197     WebFrameProxy* frame = process()->webFrame(frameID);
1198     MESSAGE_CHECK(frame);
1199
1200     frame->didReceiveServerRedirectForProvisionalLoad(url);
1201
1202     m_loaderClient.didReceiveServerRedirectForProvisionalLoadForFrame(this, frame, userData.get());
1203 }
1204
1205 void WebPageProxy::didFailProvisionalLoadForFrame(uint64_t frameID, const ResourceError& error, CoreIPC::ArgumentDecoder* arguments)
1206 {
1207     RefPtr<APIObject> userData;
1208     WebContextUserMessageDecoder messageDecoder(userData, context());
1209     if (!arguments->decode(messageDecoder))
1210         return;
1211
1212     WebFrameProxy* frame = process()->webFrame(frameID);
1213     MESSAGE_CHECK(frame);
1214
1215     frame->didFailProvisionalLoad();
1216
1217     m_loaderClient.didFailProvisionalLoadWithErrorForFrame(this, frame, error, userData.get());
1218 }
1219
1220 void WebPageProxy::didCommitLoadForFrame(uint64_t frameID, const String& mimeType, bool frameHasCustomRepresentation, const PlatformCertificateInfo& certificateInfo, CoreIPC::ArgumentDecoder* arguments)
1221 {
1222     RefPtr<APIObject> userData;
1223     WebContextUserMessageDecoder messageDecoder(userData, context());
1224     if (!arguments->decode(messageDecoder))
1225         return;
1226
1227     WebFrameProxy* frame = process()->webFrame(frameID);
1228     MESSAGE_CHECK(frame);
1229
1230     frame->didCommitLoad(mimeType, certificateInfo);
1231
1232     if (frame->isMainFrame()) {
1233         m_mainFrameHasCustomRepresentation = frameHasCustomRepresentation;
1234         m_pageClient->didCommitLoadForMainFrame(frameHasCustomRepresentation);
1235     }
1236
1237     m_loaderClient.didCommitLoadForFrame(this, frame, userData.get());
1238 }
1239
1240 void WebPageProxy::didFinishDocumentLoadForFrame(uint64_t frameID, CoreIPC::ArgumentDecoder* arguments)
1241 {
1242     RefPtr<APIObject> userData;
1243     WebContextUserMessageDecoder messageDecoder(userData, context());
1244     if (!arguments->decode(messageDecoder))
1245         return;
1246
1247     WebFrameProxy* frame = process()->webFrame(frameID);
1248     MESSAGE_CHECK(frame);
1249
1250     m_loaderClient.didFinishDocumentLoadForFrame(this, frame, userData.get());
1251 }
1252
1253 void WebPageProxy::didFinishLoadForFrame(uint64_t frameID, CoreIPC::ArgumentDecoder* arguments)
1254 {
1255     RefPtr<APIObject> userData;
1256     WebContextUserMessageDecoder messageDecoder(userData, context());
1257     if (!arguments->decode(messageDecoder))
1258         return;
1259
1260     WebFrameProxy* frame = process()->webFrame(frameID);
1261     MESSAGE_CHECK(frame);
1262
1263     frame->didFinishLoad();
1264
1265     m_loaderClient.didFinishLoadForFrame(this, frame, userData.get());
1266 }
1267
1268 void WebPageProxy::didFailLoadForFrame(uint64_t frameID, const ResourceError& error, CoreIPC::ArgumentDecoder* arguments)
1269 {
1270     RefPtr<APIObject> userData;
1271     WebContextUserMessageDecoder messageDecoder(userData, context());
1272     if (!arguments->decode(messageDecoder))
1273         return;
1274
1275     WebFrameProxy* frame = process()->webFrame(frameID);
1276     MESSAGE_CHECK(frame);
1277
1278     frame->didFailLoad();
1279
1280     m_loaderClient.didFailLoadWithErrorForFrame(this, frame, error, userData.get());
1281 }
1282
1283 void WebPageProxy::didSameDocumentNavigationForFrame(uint64_t frameID, uint32_t opaqueSameDocumentNavigationType, const String& url, CoreIPC::ArgumentDecoder* arguments)
1284 {
1285     RefPtr<APIObject> userData;
1286     WebContextUserMessageDecoder messageDecoder(userData, context());
1287     if (!arguments->decode(messageDecoder))
1288         return;
1289
1290     WebFrameProxy* frame = process()->webFrame(frameID);
1291     MESSAGE_CHECK(frame);
1292
1293     frame->didSameDocumentNavigation(url);
1294
1295     m_loaderClient.didSameDocumentNavigationForFrame(this, frame, static_cast<SameDocumentNavigationType>(opaqueSameDocumentNavigationType), userData.get());
1296 }
1297
1298 void WebPageProxy::didReceiveTitleForFrame(uint64_t frameID, const String& title, CoreIPC::ArgumentDecoder* arguments)
1299 {
1300     RefPtr<APIObject> userData;
1301     WebContextUserMessageDecoder messageDecoder(userData, context());
1302     if (!arguments->decode(messageDecoder))
1303         return;
1304
1305     WebFrameProxy* frame = process()->webFrame(frameID);
1306     MESSAGE_CHECK(frame);
1307
1308     frame->didChangeTitle(title);
1309     
1310     m_loaderClient.didReceiveTitleForFrame(this, title, frame, userData.get());
1311 }
1312
1313 void WebPageProxy::didFirstLayoutForFrame(uint64_t frameID, CoreIPC::ArgumentDecoder* arguments)
1314 {
1315     RefPtr<APIObject> userData;
1316     WebContextUserMessageDecoder messageDecoder(userData, context());
1317     if (!arguments->decode(messageDecoder))
1318         return;
1319
1320     WebFrameProxy* frame = process()->webFrame(frameID);
1321     MESSAGE_CHECK(frame);
1322
1323     m_loaderClient.didFirstLayoutForFrame(this, frame, userData.get());
1324 }
1325
1326 void WebPageProxy::didFirstVisuallyNonEmptyLayoutForFrame(uint64_t frameID, CoreIPC::ArgumentDecoder* arguments)
1327 {
1328     RefPtr<APIObject> userData;
1329     WebContextUserMessageDecoder messageDecoder(userData, context());
1330     if (!arguments->decode(messageDecoder))
1331         return;
1332
1333     WebFrameProxy* frame = process()->webFrame(frameID);
1334     MESSAGE_CHECK(frame);
1335
1336     m_loaderClient.didFirstVisuallyNonEmptyLayoutForFrame(this, frame, userData.get());
1337 }
1338
1339 void WebPageProxy::didRemoveFrameFromHierarchy(uint64_t frameID, CoreIPC::ArgumentDecoder* arguments)
1340 {
1341     RefPtr<APIObject> userData;
1342     WebContextUserMessageDecoder messageDecoder(userData, context());
1343     if (!arguments->decode(messageDecoder))
1344         return;
1345
1346     WebFrameProxy* frame = process()->webFrame(frameID);
1347     MESSAGE_CHECK(frame);
1348
1349     frame->didRemoveFromHierarchy();
1350
1351     m_loaderClient.didRemoveFrameFromHierarchy(this, frame, userData.get());
1352 }
1353
1354 void WebPageProxy::didDisplayInsecureContentForFrame(uint64_t frameID, CoreIPC::ArgumentDecoder* arguments)
1355 {
1356     RefPtr<APIObject> userData;
1357     WebContextUserMessageDecoder messageDecoder(userData, context());
1358     if (!arguments->decode(messageDecoder))
1359         return;
1360
1361     WebFrameProxy* frame = process()->webFrame(frameID);
1362     MESSAGE_CHECK(frame);
1363
1364     m_loaderClient.didDisplayInsecureContentForFrame(this, frame, userData.get());
1365 }
1366
1367 void WebPageProxy::didRunInsecureContentForFrame(uint64_t frameID, CoreIPC::ArgumentDecoder* arguments)
1368 {
1369     RefPtr<APIObject> userData;
1370     WebContextUserMessageDecoder messageDecoder(userData, context());
1371     if (!arguments->decode(messageDecoder))
1372         return;
1373
1374     WebFrameProxy* frame = process()->webFrame(frameID);
1375     MESSAGE_CHECK(frame);
1376
1377     m_loaderClient.didRunInsecureContentForFrame(this, frame, userData.get());
1378 }
1379
1380 void WebPageProxy::didReceiveAccessibilityPageToken(const CoreIPC::DataReference& data)
1381 {
1382 #if PLATFORM(MAC)
1383     m_pageClient->accessibilityChildTokenReceived(data);
1384 #endif
1385 }
1386     
1387 void WebPageProxy::frameDidBecomeFrameSet(uint64_t frameID, bool value)
1388 {
1389     WebFrameProxy* frame = process()->webFrame(frameID);
1390     MESSAGE_CHECK(frame);
1391
1392     frame->setIsFrameSet(value);
1393     if (frame->isMainFrame())
1394         m_frameSetLargestFrame = value ? m_mainFrame : 0;
1395 }
1396
1397 // PolicyClient
1398
1399 void WebPageProxy::decidePolicyForNavigationAction(uint64_t frameID, uint32_t opaqueNavigationType, uint32_t opaqueModifiers, int32_t opaqueMouseButton, const String& url, uint64_t listenerID)
1400 {
1401     WebFrameProxy* frame = process()->webFrame(frameID);
1402     MESSAGE_CHECK(frame);
1403
1404     NavigationType navigationType = static_cast<NavigationType>(opaqueNavigationType);
1405     WebEvent::Modifiers modifiers = static_cast<WebEvent::Modifiers>(opaqueModifiers);
1406     WebMouseEvent::Button mouseButton = static_cast<WebMouseEvent::Button>(opaqueMouseButton);
1407     
1408     RefPtr<WebFramePolicyListenerProxy> listener = frame->setUpPolicyListenerProxy(listenerID);
1409     if (!m_policyClient.decidePolicyForNavigationAction(this, navigationType, modifiers, mouseButton, url, frame, listener.get()))
1410         listener->use();
1411 }
1412
1413 void WebPageProxy::decidePolicyForNewWindowAction(uint64_t frameID, uint32_t opaqueNavigationType, uint32_t opaqueModifiers, int32_t opaqueMouseButton, const String& url, uint64_t listenerID)
1414 {
1415     WebFrameProxy* frame = process()->webFrame(frameID);
1416     MESSAGE_CHECK(frame);
1417
1418     NavigationType navigationType = static_cast<NavigationType>(opaqueNavigationType);
1419     WebEvent::Modifiers modifiers = static_cast<WebEvent::Modifiers>(opaqueModifiers);
1420     WebMouseEvent::Button mouseButton = static_cast<WebMouseEvent::Button>(opaqueMouseButton);
1421
1422     RefPtr<WebFramePolicyListenerProxy> listener = frame->setUpPolicyListenerProxy(listenerID);
1423     if (!m_policyClient.decidePolicyForNewWindowAction(this, navigationType, modifiers, mouseButton, url, frame, listener.get()))
1424         listener->use();
1425 }
1426
1427 void WebPageProxy::decidePolicyForMIMEType(uint64_t frameID, const String& MIMEType, const String& url, uint64_t listenerID, bool& receivedPolicyAction, uint64_t& policyAction, uint64_t& downloadID)
1428 {
1429     WebFrameProxy* frame = process()->webFrame(frameID);
1430     MESSAGE_CHECK(frame);
1431
1432     RefPtr<WebFramePolicyListenerProxy> listener = frame->setUpPolicyListenerProxy(listenerID);
1433
1434     ASSERT(!m_inDecidePolicyForMIMEType);
1435
1436     m_inDecidePolicyForMIMEType = true;
1437     m_syncMimeTypePolicyActionIsValid = false;
1438
1439     if (!m_policyClient.decidePolicyForMIMEType(this, MIMEType, url, frame, listener.get()))
1440         listener->use();
1441
1442     m_inDecidePolicyForMIMEType = false;
1443
1444     // Check if we received a policy decision already. If we did, we can just pass it back.
1445     if (m_syncMimeTypePolicyActionIsValid) {
1446         receivedPolicyAction = true;
1447         policyAction = m_syncMimeTypePolicyAction;
1448         downloadID = m_syncMimeTypePolicyDownloadID;
1449     }
1450 }
1451
1452 // FormClient
1453
1454 void WebPageProxy::willSubmitForm(uint64_t frameID, uint64_t sourceFrameID, const StringPairVector& textFieldValues, uint64_t listenerID, CoreIPC::ArgumentDecoder* arguments)
1455 {
1456     RefPtr<APIObject> userData;
1457     WebContextUserMessageDecoder messageDecoder(userData, context());
1458     if (!arguments->decode(messageDecoder))
1459         return;
1460
1461     WebFrameProxy* frame = process()->webFrame(frameID);
1462     MESSAGE_CHECK(frame);
1463
1464     WebFrameProxy* sourceFrame = process()->webFrame(sourceFrameID);
1465     MESSAGE_CHECK(sourceFrame);
1466
1467     RefPtr<WebFormSubmissionListenerProxy> listener = frame->setUpFormSubmissionListenerProxy(listenerID);
1468     if (!m_formClient.willSubmitForm(this, frame, sourceFrame, textFieldValues.stringPairVector(), userData.get(), listener.get()))
1469         listener->continueSubmission();
1470 }
1471
1472 // ResourceLoad Client
1473
1474 void WebPageProxy::didInitiateLoadForResource(uint64_t frameID, uint64_t resourceIdentifier, const ResourceRequest& request)
1475 {
1476     WebFrameProxy* frame = process()->webFrame(frameID);
1477     MESSAGE_CHECK(frame);
1478
1479     m_resourceLoadClient.didInitiateLoadForResource(this, frame, resourceIdentifier, request);
1480 }
1481
1482 void WebPageProxy::didSendRequestForResource(uint64_t frameID, uint64_t resourceIdentifier, const ResourceRequest& request, const ResourceResponse& redirectResponse)
1483 {
1484     WebFrameProxy* frame = process()->webFrame(frameID);
1485     MESSAGE_CHECK(frame);
1486
1487     m_resourceLoadClient.didSendRequestForResource(this, frame, resourceIdentifier, request, redirectResponse);
1488 }
1489
1490 void WebPageProxy::didReceiveResponseForResource(uint64_t frameID, uint64_t resourceIdentifier, const ResourceResponse& response)
1491 {
1492     WebFrameProxy* frame = process()->webFrame(frameID);
1493     MESSAGE_CHECK(frame);
1494
1495     m_resourceLoadClient.didReceiveResponseForResource(this, frame, resourceIdentifier, response);
1496 }
1497
1498 void WebPageProxy::didReceiveContentLengthForResource(uint64_t frameID, uint64_t resourceIdentifier, uint64_t contentLength)
1499 {
1500     WebFrameProxy* frame = process()->webFrame(frameID);
1501     MESSAGE_CHECK(frame);
1502
1503     m_resourceLoadClient.didReceiveContentLengthForResource(this, frame, resourceIdentifier, contentLength);
1504 }
1505
1506 void WebPageProxy::didFinishLoadForResource(uint64_t frameID, uint64_t resourceIdentifier)
1507 {
1508     WebFrameProxy* frame = process()->webFrame(frameID);
1509     MESSAGE_CHECK(frame);
1510
1511     m_resourceLoadClient.didFinishLoadForResource(this, frame, resourceIdentifier);
1512 }
1513
1514 void WebPageProxy::didFailLoadForResource(uint64_t frameID, uint64_t resourceIdentifier, const ResourceError& error)
1515 {
1516     WebFrameProxy* frame = process()->webFrame(frameID);
1517     MESSAGE_CHECK(frame);
1518
1519     m_resourceLoadClient.didFailLoadForResource(this, frame, resourceIdentifier, error);
1520 }
1521
1522 // UIClient
1523
1524 void WebPageProxy::createNewPage(const WindowFeatures& windowFeatures, uint32_t opaqueModifiers, int32_t opaqueMouseButton, uint64_t& newPageID, WebPageCreationParameters& newPageParameters)
1525 {
1526     RefPtr<WebPageProxy> newPage = m_uiClient.createNewPage(this, windowFeatures, static_cast<WebEvent::Modifiers>(opaqueModifiers), static_cast<WebMouseEvent::Button>(opaqueMouseButton));
1527     if (newPage) {
1528         newPageID = newPage->pageID();
1529         newPageParameters = newPage->creationParameters();
1530     } else
1531         newPageID = 0;
1532 }
1533     
1534 void WebPageProxy::showPage()
1535 {
1536     m_uiClient.showPage(this);
1537 }
1538
1539 void WebPageProxy::closePage()
1540 {
1541     m_uiClient.close(this);
1542 }
1543
1544 void WebPageProxy::runJavaScriptAlert(uint64_t frameID, const String& message)
1545 {
1546     WebFrameProxy* frame = process()->webFrame(frameID);
1547     MESSAGE_CHECK(frame);
1548
1549     m_uiClient.runJavaScriptAlert(this, message, frame);
1550 }
1551
1552 void WebPageProxy::runJavaScriptConfirm(uint64_t frameID, const String& message, bool& result)
1553 {
1554     WebFrameProxy* frame = process()->webFrame(frameID);
1555     MESSAGE_CHECK(frame);
1556
1557     result = m_uiClient.runJavaScriptConfirm(this, message, frame);
1558 }
1559
1560 void WebPageProxy::runJavaScriptPrompt(uint64_t frameID, const String& message, const String& defaultValue, String& result)
1561 {
1562     WebFrameProxy* frame = process()->webFrame(frameID);
1563     MESSAGE_CHECK(frame);
1564
1565     result = m_uiClient.runJavaScriptPrompt(this, message, defaultValue, frame);
1566 }
1567
1568 void WebPageProxy::setStatusText(const String& text)
1569 {
1570     m_uiClient.setStatusText(this, text);
1571 }
1572
1573 void WebPageProxy::mouseDidMoveOverElement(uint32_t opaqueModifiers, CoreIPC::ArgumentDecoder* arguments)
1574 {
1575     RefPtr<APIObject> userData;
1576     WebContextUserMessageDecoder messageDecoder(userData, context());
1577     if (!arguments->decode(messageDecoder))
1578         return;
1579
1580     WebEvent::Modifiers modifiers = static_cast<WebEvent::Modifiers>(opaqueModifiers);
1581
1582     m_uiClient.mouseDidMoveOverElement(this, modifiers, userData.get());
1583 }
1584
1585 void WebPageProxy::missingPluginButtonClicked(const String& mimeType, const String& url)
1586 {
1587     m_uiClient.missingPluginButtonClicked(this, mimeType, url);
1588 }
1589
1590 void WebPageProxy::setToolbarsAreVisible(bool toolbarsAreVisible)
1591 {
1592     m_uiClient.setToolbarsAreVisible(this, toolbarsAreVisible);
1593 }
1594
1595 void WebPageProxy::getToolbarsAreVisible(bool& toolbarsAreVisible)
1596 {
1597     toolbarsAreVisible = m_uiClient.toolbarsAreVisible(this);
1598 }
1599
1600 void WebPageProxy::setMenuBarIsVisible(bool menuBarIsVisible)
1601 {
1602     m_uiClient.setMenuBarIsVisible(this, menuBarIsVisible);
1603 }
1604
1605 void WebPageProxy::getMenuBarIsVisible(bool& menuBarIsVisible)
1606 {
1607     menuBarIsVisible = m_uiClient.menuBarIsVisible(this);
1608 }
1609
1610 void WebPageProxy::setStatusBarIsVisible(bool statusBarIsVisible)
1611 {
1612     m_uiClient.setStatusBarIsVisible(this, statusBarIsVisible);
1613 }
1614
1615 void WebPageProxy::getStatusBarIsVisible(bool& statusBarIsVisible)
1616 {
1617     statusBarIsVisible = m_uiClient.statusBarIsVisible(this);
1618 }
1619
1620 void WebPageProxy::setIsResizable(bool isResizable)
1621 {
1622     m_uiClient.setIsResizable(this, isResizable);
1623 }
1624
1625 void WebPageProxy::getIsResizable(bool& isResizable)
1626 {
1627     isResizable = m_uiClient.isResizable(this);
1628 }
1629
1630 void WebPageProxy::setWindowFrame(const FloatRect& newWindowFrame)
1631 {
1632     m_uiClient.setWindowFrame(this, m_pageClient->convertToDeviceSpace(newWindowFrame));
1633 }
1634
1635 void WebPageProxy::getWindowFrame(FloatRect& newWindowFrame)
1636 {
1637     newWindowFrame = m_pageClient->convertToUserSpace(m_uiClient.windowFrame(this));
1638 }
1639
1640 void WebPageProxy::canRunBeforeUnloadConfirmPanel(bool& canRun)
1641 {
1642     canRun = m_uiClient.canRunBeforeUnloadConfirmPanel();
1643 }
1644
1645 void WebPageProxy::runBeforeUnloadConfirmPanel(const String& message, uint64_t frameID, bool& shouldClose)
1646 {
1647     WebFrameProxy* frame = process()->webFrame(frameID);
1648     MESSAGE_CHECK(frame);
1649
1650     shouldClose = m_uiClient.runBeforeUnloadConfirmPanel(this, message, frame);
1651 }
1652
1653 #if ENABLE(TILED_BACKING_STORE)
1654 void WebPageProxy::pageDidRequestScroll(const IntSize& delta)
1655 {
1656     m_pageClient->pageDidRequestScroll(delta);
1657 }
1658 #endif
1659
1660 void WebPageProxy::didChangeViewportData(const ViewportArguments& args)
1661 {
1662     m_pageClient->setViewportArguments(args);
1663 }
1664
1665 void WebPageProxy::pageDidScroll()
1666 {
1667     m_uiClient.pageDidScroll(this);
1668 }
1669
1670 void WebPageProxy::runOpenPanel(uint64_t frameID, const WebOpenPanelParameters::Data& data)
1671 {
1672     if (m_openPanelResultListener) {
1673         m_openPanelResultListener->invalidate();
1674         m_openPanelResultListener = 0;
1675     }
1676
1677     WebFrameProxy* frame = process()->webFrame(frameID);
1678     MESSAGE_CHECK(frame);
1679
1680     m_openPanelResultListener = WebOpenPanelResultListenerProxy::create(this);
1681
1682     if (!m_uiClient.runOpenPanel(this, frame, data, m_openPanelResultListener.get()))
1683         didCancelForOpenPanel();
1684 }
1685
1686 void WebPageProxy::printFrame(uint64_t frameID)
1687 {
1688     WebFrameProxy* frame = process()->webFrame(frameID);
1689     MESSAGE_CHECK(frame);
1690
1691     m_uiClient.printFrame(this, frame);
1692 }
1693
1694 #if PLATFORM(QT)
1695 void WebPageProxy::didChangeContentsSize(const WebCore::IntSize& size)
1696 {
1697     m_pageClient->didChangeContentsSize(size);
1698 }
1699
1700 void WebPageProxy::didFindZoomableArea(const WebCore::IntRect& area)
1701 {
1702     m_pageClient->didFindZoomableArea(area);
1703 }
1704
1705 void WebPageProxy::findZoomableAreaForPoint(const WebCore::IntPoint& point)
1706 {
1707     if (!isValid())
1708         return;
1709
1710     process()->send(Messages::WebPage::FindZoomableAreaForPoint(point), m_pageID);
1711 }
1712 #endif
1713
1714 void WebPageProxy::didDraw()
1715 {
1716     m_uiClient.didDraw(this);
1717 }
1718
1719 // Inspector
1720
1721 #if ENABLE(INSPECTOR)
1722
1723 WebInspectorProxy* WebPageProxy::inspector()
1724 {
1725     if (isClosed() || !isValid())
1726         return 0;
1727     if (!m_inspector)
1728         m_inspector = WebInspectorProxy::create(this);
1729     return m_inspector.get();
1730 }
1731
1732 #endif
1733
1734 // BackForwardList
1735
1736 void WebPageProxy::backForwardAddItem(uint64_t itemID)
1737 {
1738     m_backForwardList->addItem(process()->webBackForwardItem(itemID));
1739 }
1740
1741 void WebPageProxy::backForwardGoToItem(uint64_t itemID)
1742 {
1743     m_backForwardList->goToItem(process()->webBackForwardItem(itemID));
1744 }
1745
1746 void WebPageProxy::backForwardItemAtIndex(int32_t index, uint64_t& itemID)
1747 {
1748     WebBackForwardListItem* item = m_backForwardList->itemAtIndex(index);
1749     itemID = item ? item->itemID() : 0;
1750 }
1751
1752 void WebPageProxy::backForwardBackListCount(int32_t& count)
1753 {
1754     count = m_backForwardList->backListCount();
1755 }
1756
1757 void WebPageProxy::backForwardForwardListCount(int32_t& count)
1758 {
1759     count = m_backForwardList->forwardListCount();
1760 }
1761
1762 void WebPageProxy::selectionStateChanged(const SelectionState& selectionState)
1763 {
1764     m_selectionState = selectionState;
1765 }
1766
1767 #if PLATFORM(MAC)
1768 // Complex text input support for plug-ins.
1769 void WebPageProxy::sendComplexTextInputToPlugin(uint64_t pluginComplexTextInputIdentifier, const String& textInput)
1770 {
1771     if (!isValid())
1772         return;
1773     
1774     process()->send(Messages::WebPage::SendComplexTextInputToPlugin(pluginComplexTextInputIdentifier, textInput), m_pageID);
1775 }
1776 #endif
1777
1778 #if PLATFORM(WIN)
1779 void WebPageProxy::didChangeCompositionSelection(bool hasComposition)
1780 {
1781     m_pageClient->compositionSelectionChanged(hasComposition);
1782 }
1783
1784 void WebPageProxy::confirmComposition(const String& compositionString)
1785 {
1786     process()->send(Messages::WebPage::ConfirmComposition(compositionString), m_pageID);
1787 }
1788
1789 void WebPageProxy::setComposition(const String& compositionString, Vector<WebCore::CompositionUnderline>& underlines, int cursorPosition)
1790 {
1791     process()->send(Messages::WebPage::SetComposition(compositionString, underlines, cursorPosition), m_pageID);
1792 }
1793 #endif
1794     
1795 // Undo management
1796
1797 void WebPageProxy::registerEditCommandForUndo(uint64_t commandID, uint32_t editAction)
1798 {
1799     registerEditCommand(WebEditCommandProxy::create(commandID, static_cast<EditAction>(editAction), this), Undo);
1800 }
1801
1802 void WebPageProxy::clearAllEditCommands()
1803 {
1804     m_pageClient->clearAllEditCommands();
1805 }
1806
1807 void WebPageProxy::didCountStringMatches(const String& string, uint32_t matchCount)
1808 {
1809     m_findClient.didCountStringMatches(this, string, matchCount);
1810 }
1811
1812 void WebPageProxy::setFindIndicator(const FloatRect& selectionRect, const Vector<FloatRect>& textRects, const SharedMemory::Handle& contentImageHandle, bool fadeOut)
1813 {
1814     RefPtr<FindIndicator> findIndicator = FindIndicator::create(selectionRect, textRects, contentImageHandle);
1815     m_pageClient->setFindIndicator(findIndicator.release(), fadeOut);
1816 }
1817
1818 void WebPageProxy::didFindString(const String& string, uint32_t matchCount)
1819 {
1820     m_findClient.didFindString(this, string, matchCount);
1821 }
1822
1823 void WebPageProxy::didFailToFindString(const String& string)
1824 {
1825     m_findClient.didFailToFindString(this, string);
1826 }
1827
1828 void WebPageProxy::valueChangedForPopupMenu(WebPopupMenuProxy*, int32_t newSelectedIndex)
1829 {
1830     process()->send(Messages::WebPage::DidChangeSelectedIndexForActivePopupMenu(newSelectedIndex), m_pageID);
1831 }
1832
1833 void WebPageProxy::setTextFromItemForPopupMenu(WebPopupMenuProxy*, int32_t index)
1834 {
1835     process()->send(Messages::WebPage::SetTextForActivePopupMenu(index), m_pageID);
1836 }
1837
1838 void WebPageProxy::showPopupMenu(const IntRect& rect, const Vector<WebPopupItem>& items, int32_t selectedIndex, const PlatformPopupMenuData& data)
1839 {
1840     if (m_activePopupMenu)
1841         m_activePopupMenu->hidePopupMenu();
1842     else
1843         m_activePopupMenu = m_pageClient->createPopupMenuProxy(this);
1844
1845 #if PLATFORM(WIN)
1846     // On Windows, we're about to run our own message pump in showPopupMenu(), so turn off the responsiveness timer.
1847     process()->responsivenessTimer()->stop();
1848 #endif
1849
1850     m_activePopupMenu->showPopupMenu(rect, items, data, selectedIndex);
1851     m_activePopupMenu = 0;
1852 }
1853
1854 void WebPageProxy::hidePopupMenu()
1855 {
1856     if (!m_activePopupMenu)
1857         return;
1858
1859     m_activePopupMenu->hidePopupMenu();
1860     m_activePopupMenu = 0;
1861 }
1862
1863 void WebPageProxy::showContextMenu(const IntPoint& menuLocation, const ContextMenuState& contextMenuState, const Vector<WebContextMenuItemData>& proposedItems, CoreIPC::ArgumentDecoder* arguments)
1864 {
1865     RefPtr<APIObject> userData;
1866     WebContextUserMessageDecoder messageDecoder(userData, context());
1867     if (!arguments->decode(messageDecoder))
1868         return;
1869
1870     m_activeContextMenuState = contextMenuState;
1871
1872     if (m_activeContextMenu)
1873         m_activeContextMenu->hideContextMenu();
1874     else
1875         m_activeContextMenu = m_pageClient->createContextMenuProxy(this);
1876
1877     // Give the PageContextMenuClient one last swipe at changing the menu.
1878     Vector<WebContextMenuItemData> items;
1879         
1880     if (!m_contextMenuClient.getContextMenuFromProposedMenu(this, proposedItems, items, userData.get())) {
1881         m_activeContextMenu->showContextMenu(menuLocation, proposedItems);
1882         return;
1883     }
1884     
1885     if (items.size())
1886         m_activeContextMenu->showContextMenu(menuLocation, items);
1887 }
1888
1889 void WebPageProxy::contextMenuItemSelected(const WebContextMenuItemData& item)
1890 {
1891     // Application custom items don't need to round-trip through to WebCore in the WebProcess.
1892     if (item.action() >= ContextMenuItemBaseApplicationTag) {
1893         m_contextMenuClient.customContextMenuItemSelected(this, item);
1894         return;
1895     }
1896
1897 #if PLATFORM(MAC)
1898     if (item.action() == ContextMenuItemTagSmartCopyPaste) {
1899         setSmartInsertDeleteEnabled(!isSmartInsertDeleteEnabled());
1900         return;
1901     }
1902     if (item.action() == ContextMenuItemTagSmartQuotes) {
1903         TextChecker::setAutomaticQuoteSubstitutionEnabled(!TextChecker::state().isAutomaticQuoteSubstitutionEnabled);
1904         process()->updateTextCheckerState();
1905         return;
1906     }
1907     if (item.action() == ContextMenuItemTagSmartDashes) {
1908         TextChecker::setAutomaticDashSubstitutionEnabled(!TextChecker::state().isAutomaticDashSubstitutionEnabled);
1909         process()->updateTextCheckerState();
1910         return;
1911     }
1912     if (item.action() == ContextMenuItemTagSmartLinks) {
1913         TextChecker::setAutomaticLinkDetectionEnabled(!TextChecker::state().isAutomaticLinkDetectionEnabled);
1914         process()->updateTextCheckerState();
1915         return;
1916     }
1917     if (item.action() == ContextMenuItemTagTextReplacement) {
1918         TextChecker::setAutomaticTextReplacementEnabled(!TextChecker::state().isAutomaticTextReplacementEnabled);
1919         process()->updateTextCheckerState();
1920         return;
1921     }
1922 #endif
1923     if (item.action() == ContextMenuItemTagDownloadImageToDisk) {
1924         m_context->download(this, KURL(KURL(), m_activeContextMenuState.absoluteImageURLString));
1925         return;    
1926     }
1927     if (item.action() == ContextMenuItemTagDownloadLinkToDisk) {
1928         m_context->download(this, KURL(KURL(), m_activeContextMenuState.absoluteLinkURLString));
1929         return;
1930     }
1931     
1932     if (item.action() == ContextMenuItemTagLearnSpelling || item.action() == ContextMenuItemTagIgnoreSpelling)
1933         ++m_pendingLearnOrIgnoreWordMessageCount;
1934
1935     process()->send(Messages::WebPage::DidSelectItemFromActiveContextMenu(item), m_pageID);
1936 }
1937
1938 void WebPageProxy::didChooseFilesForOpenPanel(const Vector<String>& fileURLs)
1939 {
1940     if (!isValid())
1941         return;
1942
1943     // FIXME: This also needs to send a sandbox extension for these paths.
1944     process()->send(Messages::WebPage::DidChooseFilesForOpenPanel(fileURLs), m_pageID);
1945
1946     m_openPanelResultListener->invalidate();
1947     m_openPanelResultListener = 0;
1948 }
1949
1950 void WebPageProxy::didCancelForOpenPanel()
1951 {
1952     if (!isValid())
1953         return;
1954
1955     process()->send(Messages::WebPage::DidCancelForOpenPanel(), m_pageID);
1956     
1957     m_openPanelResultListener->invalidate();
1958     m_openPanelResultListener = 0;
1959 }
1960
1961 void WebPageProxy::advanceToNextMisspelling(bool startBeforeSelection)
1962 {
1963     process()->send(Messages::WebPage::AdvanceToNextMisspelling(startBeforeSelection), m_pageID);
1964 }
1965
1966 void WebPageProxy::changeSpellingToWord(const String& word)
1967 {
1968     if (word.isEmpty())
1969         return;
1970
1971     process()->send(Messages::WebPage::ChangeSpellingToWord(word), m_pageID);
1972 }
1973
1974 void WebPageProxy::unmarkAllMisspellings()
1975 {
1976     process()->send(Messages::WebPage::UnmarkAllMisspellings(), m_pageID);
1977 }
1978
1979 void WebPageProxy::unmarkAllBadGrammar()
1980 {
1981     process()->send(Messages::WebPage::UnmarkAllBadGrammar(), m_pageID);
1982 }
1983
1984 #if PLATFORM(MAC)
1985 void WebPageProxy::uppercaseWord()
1986 {
1987     process()->send(Messages::WebPage::UppercaseWord(), m_pageID);
1988 }
1989
1990 void WebPageProxy::lowercaseWord()
1991 {
1992     process()->send(Messages::WebPage::LowercaseWord(), m_pageID);
1993 }
1994
1995 void WebPageProxy::capitalizeWord()
1996 {
1997     process()->send(Messages::WebPage::CapitalizeWord(), m_pageID);
1998 }
1999
2000 void WebPageProxy::setSmartInsertDeleteEnabled(bool isSmartInsertDeleteEnabled)
2001
2002     if (m_isSmartInsertDeleteEnabled == isSmartInsertDeleteEnabled)
2003         return;
2004
2005     TextChecker::setSmartInsertDeleteEnabled(isSmartInsertDeleteEnabled);
2006     m_isSmartInsertDeleteEnabled = isSmartInsertDeleteEnabled;
2007     process()->send(Messages::WebPage::SetSmartInsertDeleteEnabled(isSmartInsertDeleteEnabled), m_pageID);
2008 }
2009 #endif
2010
2011 void WebPageProxy::registerEditCommand(PassRefPtr<WebEditCommandProxy> commandProxy, UndoOrRedo undoOrRedo)
2012 {
2013     m_pageClient->registerEditCommand(commandProxy, undoOrRedo);
2014 }
2015
2016 void WebPageProxy::addEditCommand(WebEditCommandProxy* command)
2017 {
2018     m_editCommandSet.add(command);
2019 }
2020
2021 void WebPageProxy::removeEditCommand(WebEditCommandProxy* command)
2022 {
2023     m_editCommandSet.remove(command);
2024
2025     if (!isValid())
2026         return;
2027     process()->send(Messages::WebPage::DidRemoveEditCommand(command->commandID()), m_pageID);
2028 }
2029
2030 int64_t WebPageProxy::spellDocumentTag()
2031 {
2032     if (!m_hasSpellDocumentTag) {
2033         m_spellDocumentTag = TextChecker::uniqueSpellDocumentTag();
2034         m_hasSpellDocumentTag = true;
2035     }
2036
2037     return m_spellDocumentTag;
2038 }
2039
2040 void WebPageProxy::checkTextOfParagraph(const String& text, uint64_t checkingTypes, Vector<TextCheckingResult>& results)
2041 {
2042     results = TextChecker::checkTextOfParagraph(spellDocumentTag(), text.characters(), text.length(), checkingTypes);
2043 }
2044
2045 void WebPageProxy::updateSpellingUIWithMisspelledWord(const String& misspelledWord)
2046 {
2047     TextChecker::updateSpellingUIWithMisspelledWord(misspelledWord);
2048 }
2049
2050 void WebPageProxy::getGuessesForWord(const String& word, const String& context, Vector<String>& guesses)
2051 {
2052     TextChecker::getGuessesForWord(spellDocumentTag(), word, context, guesses);
2053 }
2054
2055 void WebPageProxy::learnWord(const String& word)
2056 {
2057     MESSAGE_CHECK(m_pendingLearnOrIgnoreWordMessageCount);
2058     --m_pendingLearnOrIgnoreWordMessageCount;
2059
2060     TextChecker::learnWord(word);
2061 }
2062
2063 void WebPageProxy::ignoreWord(const String& word)
2064 {
2065     MESSAGE_CHECK(m_pendingLearnOrIgnoreWordMessageCount);
2066     --m_pendingLearnOrIgnoreWordMessageCount;
2067
2068     TextChecker::ignoreWord(spellDocumentTag(), word);
2069 }
2070
2071 // Other
2072
2073 void WebPageProxy::takeFocus(bool direction)
2074 {
2075     m_pageClient->takeFocus(direction);
2076 }
2077
2078 void WebPageProxy::setToolTip(const String& toolTip)
2079 {
2080     String oldToolTip = m_toolTip;
2081     m_toolTip = toolTip;
2082     m_pageClient->toolTipChanged(oldToolTip, m_toolTip);
2083 }
2084
2085 void WebPageProxy::setCursor(const WebCore::Cursor& cursor)
2086 {
2087     m_pageClient->setCursor(cursor);
2088 }
2089
2090 void WebPageProxy::didValidateMenuItem(const String& commandName, bool isEnabled, int32_t state)
2091 {
2092     m_pageClient->setEditCommandState(commandName, isEnabled, state);
2093 }
2094
2095 void WebPageProxy::didReceiveEvent(uint32_t opaqueType, bool handled)
2096 {
2097     WebEvent::Type type = static_cast<WebEvent::Type>(opaqueType);
2098
2099     switch (type) {
2100     case WebEvent::MouseMove:
2101         break;
2102
2103     case WebEvent::MouseDown:
2104     case WebEvent::MouseUp:
2105     case WebEvent::Wheel:
2106     case WebEvent::KeyDown:
2107     case WebEvent::KeyUp:
2108     case WebEvent::RawKeyDown:
2109     case WebEvent::Char:
2110         process()->responsivenessTimer()->stop();
2111         break;
2112     }
2113
2114     switch (type) {
2115     case WebEvent::MouseMove:
2116         m_processingMouseMoveEvent = false;
2117         if (m_nextMouseMoveEvent) {
2118             handleMouseEvent(*m_nextMouseMoveEvent);
2119             m_nextMouseMoveEvent = nullptr;
2120         }
2121         break;
2122     case WebEvent::MouseDown:
2123     case WebEvent::MouseUp:
2124         break;
2125
2126     case WebEvent::Wheel: {
2127         m_processingWheelEvent = false;
2128         if (m_nextWheelEvent) {
2129             handleWheelEvent(*m_nextWheelEvent);
2130             m_nextWheelEvent = nullptr;
2131         }
2132         break;
2133     }
2134
2135     case WebEvent::KeyDown:
2136     case WebEvent::KeyUp:
2137     case WebEvent::RawKeyDown:
2138     case WebEvent::Char: {
2139         NativeWebKeyboardEvent event = m_keyEventQueue.first();
2140         MESSAGE_CHECK(type == event.type());
2141
2142         m_keyEventQueue.removeFirst();
2143
2144         if (handled)
2145             break;
2146
2147         m_pageClient->didNotHandleKeyEvent(event);
2148         m_uiClient.didNotHandleKeyEvent(this, event);
2149         break;
2150     }
2151     }
2152 }
2153
2154 void WebPageProxy::voidCallback(uint64_t callbackID)
2155 {
2156     RefPtr<VoidCallback> callback = m_voidCallbacks.take(callbackID);
2157     if (!callback) {
2158         // FIXME: Log error or assert.
2159         return;
2160     }
2161
2162     callback->performCallback();
2163 }
2164
2165 void WebPageProxy::dataCallback(const CoreIPC::DataReference& dataReference, uint64_t callbackID)
2166 {
2167     RefPtr<DataCallback> callback = m_dataCallbacks.take(callbackID);
2168     if (!callback) {
2169         // FIXME: Log error or assert.
2170         return;
2171     }
2172
2173     callback->performCallbackWithReturnValue(WebData::create(dataReference.data(), dataReference.size()).get());
2174 }
2175
2176 void WebPageProxy::stringCallback(const String& resultString, uint64_t callbackID)
2177 {
2178     RefPtr<StringCallback> callback = m_stringCallbacks.take(callbackID);
2179     if (!callback) {
2180         // FIXME: Log error or assert.
2181         return;
2182     }
2183
2184     callback->performCallbackWithReturnValue(resultString.impl());
2185 }
2186
2187 #if PLATFORM(MAC)
2188 void WebPageProxy::sendAccessibilityPresenterToken(const CoreIPC::DataReference& token)
2189 {
2190     if (!isValid())
2191         return;
2192
2193     process()->send(Messages::WebPage::SendAccessibilityPresenterToken(token), m_pageID);
2194 }
2195 #endif
2196
2197 void WebPageProxy::focusedFrameChanged(uint64_t frameID)
2198 {
2199     if (!frameID) {
2200         m_focusedFrame = 0;
2201         return;
2202     }
2203
2204     WebFrameProxy* frame = process()->webFrame(frameID);
2205     MESSAGE_CHECK(frame);
2206
2207     m_focusedFrame = frame;
2208 }
2209
2210 void WebPageProxy::frameSetLargestFrameChanged(uint64_t frameID)
2211 {
2212     if (!frameID) {
2213         m_frameSetLargestFrame = 0;
2214         return;
2215     }
2216
2217     WebFrameProxy* frame = process()->webFrame(frameID);
2218     MESSAGE_CHECK(frame);
2219
2220     m_frameSetLargestFrame = frame;
2221 }
2222
2223 #if USE(ACCELERATED_COMPOSITING)
2224 void WebPageProxy::didChangeAcceleratedCompositing(bool compositing, DrawingAreaInfo& drawingAreaInfo)
2225 {
2226     if (compositing)
2227         didEnterAcceleratedCompositing();
2228     else
2229         didLeaveAcceleratedCompositing();
2230
2231     drawingAreaInfo = drawingArea()->info();
2232 }
2233 #endif
2234
2235 void WebPageProxy::processDidBecomeUnresponsive()
2236 {
2237     m_loaderClient.processDidBecomeUnresponsive(this);
2238 }
2239
2240 void WebPageProxy::processDidBecomeResponsive()
2241 {
2242     m_loaderClient.processDidBecomeResponsive(this);
2243 }
2244
2245 void WebPageProxy::processDidCrash()
2246 {
2247     ASSERT(m_pageClient);
2248
2249     m_isValid = false;
2250
2251     if (m_mainFrame)
2252         m_urlAtProcessExit = m_mainFrame->url();
2253
2254     m_mainFrame = 0;
2255
2256     m_drawingArea = nullptr;
2257
2258 #if ENABLE(INSPECTOR)
2259     if (m_inspector) {
2260         m_inspector->invalidate();
2261         m_inspector = 0;
2262     }
2263 #endif
2264
2265     if (m_openPanelResultListener) {
2266         m_openPanelResultListener->invalidate();
2267         m_openPanelResultListener = 0;
2268     }
2269
2270     m_geolocationPermissionRequestManager.invalidateRequests();
2271
2272     m_toolTip = String();
2273
2274     invalidateCallbackMap(m_voidCallbacks);
2275     invalidateCallbackMap(m_dataCallbacks);
2276     invalidateCallbackMap(m_stringCallbacks);
2277
2278     Vector<WebEditCommandProxy*> editCommandVector;
2279     copyToVector(m_editCommandSet, editCommandVector);
2280     m_editCommandSet.clear();
2281     for (size_t i = 0, size = editCommandVector.size(); i < size; ++i)
2282         editCommandVector[i]->invalidate();
2283     m_pageClient->clearAllEditCommands();
2284
2285     m_activePopupMenu = 0;
2286
2287     m_estimatedProgress = 0.0;
2288
2289     m_pendingLearnOrIgnoreWordMessageCount = 0;
2290
2291     m_pageClient->processDidCrash();
2292     m_loaderClient.processDidCrash(this);
2293 }
2294
2295 WebPageCreationParameters WebPageProxy::creationParameters() const
2296 {
2297     WebPageCreationParameters parameters;
2298
2299     parameters.viewSize = m_pageClient->viewSize();
2300     parameters.isActive = m_pageClient->isViewWindowActive();
2301     parameters.isFocused = m_pageClient->isViewFocused();
2302     parameters.isVisible = m_pageClient->isViewVisible();
2303     parameters.isInWindow = m_pageClient->isViewInWindow();
2304     parameters.drawingAreaInfo = m_drawingArea->info();
2305     parameters.store = m_pageGroup->preferences()->store();
2306     parameters.pageGroupData = m_pageGroup->data();
2307     parameters.drawsBackground = m_drawsBackground;
2308     parameters.drawsTransparentBackground = m_drawsTransparentBackground;
2309     parameters.useFixedLayout = m_useFixedLayout;
2310     parameters.fixedLayoutSize = m_fixedLayoutSize;
2311     parameters.userAgent = userAgent();
2312     parameters.sessionState = SessionState(m_backForwardList->entries(), m_backForwardList->currentIndex());
2313     parameters.highestUsedBackForwardItemID = WebBackForwardListItem::highedUsedItemID();
2314     parameters.canRunModal = m_uiClient.canRunModal();
2315
2316 #if PLATFORM(MAC)
2317     parameters.isSmartInsertDeleteEnabled = m_isSmartInsertDeleteEnabled;
2318 #endif
2319
2320 #if PLATFORM(WIN)
2321     parameters.nativeWindow = m_pageClient->nativeWindow();
2322 #endif
2323
2324     return parameters;
2325 }
2326
2327 #if USE(ACCELERATED_COMPOSITING)
2328
2329 void WebPageProxy::didEnterAcceleratedCompositing()
2330 {
2331     m_pageClient->pageDidEnterAcceleratedCompositing();
2332 }
2333
2334 void WebPageProxy::didLeaveAcceleratedCompositing()
2335 {
2336     m_pageClient->pageDidLeaveAcceleratedCompositing();
2337 }
2338
2339 #endif // USE(ACCELERATED_COMPOSITING)
2340
2341 void WebPageProxy::backForwardClear()
2342 {
2343     m_backForwardList->clear();
2344 }
2345
2346 void WebPageProxy::canAuthenticateAgainstProtectionSpaceInFrame(uint64_t frameID, const WebCore::ProtectionSpace& coreProtectionSpace, bool& canAuthenticate)
2347 {
2348     WebFrameProxy* frame = process()->webFrame(frameID);
2349     MESSAGE_CHECK(frame);
2350
2351     RefPtr<WebProtectionSpace> protectionSpace = WebProtectionSpace::create(coreProtectionSpace);
2352     
2353     canAuthenticate = m_loaderClient.canAuthenticateAgainstProtectionSpaceInFrame(this, frame, protectionSpace.get());
2354 }
2355
2356 void WebPageProxy::didReceiveAuthenticationChallenge(uint64_t frameID, const WebCore::AuthenticationChallenge& coreChallenge, uint64_t challengeID)
2357 {
2358     WebFrameProxy* frame = process()->webFrame(frameID);
2359     MESSAGE_CHECK(frame);
2360
2361     RefPtr<AuthenticationChallengeProxy> authenticationChallenge = AuthenticationChallengeProxy::create(coreChallenge, challengeID, this);
2362     
2363     m_loaderClient.didReceiveAuthenticationChallengeInFrame(this, frame, authenticationChallenge.get());
2364 }
2365
2366 void WebPageProxy::exceededDatabaseQuota(uint64_t frameID, const String& originIdentifier, const String& databaseName, const String& displayName, uint64_t currentQuota, uint64_t currentUsage, uint64_t expectedUsage, uint64_t& newQuota)
2367 {
2368     WebFrameProxy* frame = process()->webFrame(frameID);
2369     MESSAGE_CHECK(frame);
2370
2371     RefPtr<WebSecurityOrigin> origin = WebSecurityOrigin::create(originIdentifier);
2372
2373     newQuota = m_uiClient.exceededDatabaseQuota(this, frame, origin.get(), databaseName, displayName, currentQuota, currentUsage, expectedUsage);
2374 }
2375
2376 void WebPageProxy::requestGeolocationPermissionForFrame(uint64_t geolocationID, uint64_t frameID, String originIdentifier)
2377 {
2378     WebFrameProxy* frame = process()->webFrame(frameID);
2379     MESSAGE_CHECK(frame);
2380
2381     RefPtr<WebSecurityOrigin> origin = WebSecurityOrigin::create(originIdentifier);
2382     RefPtr<GeolocationPermissionRequestProxy> request = m_geolocationPermissionRequestManager.createRequest(geolocationID);
2383
2384     if (!m_uiClient.decidePolicyForGeolocationPermissionRequest(this, frame, origin.get(), request.get()))
2385         request->deny();
2386 }
2387
2388 float WebPageProxy::headerHeight(WebFrameProxy* frame)
2389 {
2390     return m_uiClient.headerHeight(this, frame);
2391 }
2392
2393 float WebPageProxy::footerHeight(WebFrameProxy* frame)
2394 {
2395     return m_uiClient.footerHeight(this, frame);
2396 }
2397
2398 void WebPageProxy::drawHeader(WebFrameProxy* frame, const WebCore::FloatRect& rect)
2399 {
2400     m_uiClient.drawHeader(this, frame, rect);
2401 }
2402
2403 void WebPageProxy::drawFooter(WebFrameProxy* frame, const WebCore::FloatRect& rect)
2404 {
2405     m_uiClient.drawFooter(this, frame, rect);
2406 }
2407
2408 void WebPageProxy::didFinishLoadingDataForCustomRepresentation(const CoreIPC::DataReference& dataReference)
2409 {
2410     m_pageClient->didFinishLoadingDataForCustomRepresentation(dataReference);
2411 }
2412
2413 #if PLATFORM(MAC)
2414 void WebPageProxy::setComplexTextInputEnabled(uint64_t pluginComplexTextInputIdentifier, bool complexTextInputEnabled)
2415 {
2416     m_pageClient->setComplexTextInputEnabled(pluginComplexTextInputIdentifier, complexTextInputEnabled);
2417 }
2418 #endif
2419
2420 void WebPageProxy::backForwardRemovedItem(uint64_t itemID)
2421 {
2422     process()->send(Messages::WebPage::DidRemoveBackForwardItem(itemID), m_pageID);
2423 }
2424
2425 void WebPageProxy::beginPrinting(WebFrameProxy* frame, const PrintInfo& printInfo)
2426 {
2427     if (m_isInPrintingMode)
2428         return;
2429
2430     m_isInPrintingMode = true;
2431     process()->send(Messages::WebPage::BeginPrinting(frame->frameID(), printInfo), m_pageID);
2432 }
2433
2434 void WebPageProxy::endPrinting()
2435 {
2436     if (!m_isInPrintingMode)
2437         return;
2438
2439     m_isInPrintingMode = false;
2440     process()->send(Messages::WebPage::EndPrinting(), m_pageID);
2441 }
2442
2443 void WebPageProxy::computePagesForPrinting(WebFrameProxy* frame, const PrintInfo& printInfo, Vector<WebCore::IntRect>& resultPageRects, double& resultTotalScaleFactorForPrinting)
2444 {
2445     // Layout for printing can take a long time, but we need to have the answer.
2446     process()->sendSync(Messages::WebPage::ComputePagesForPrinting(frame->frameID(), printInfo), Messages::WebPage::ComputePagesForPrinting::Reply(resultPageRects, resultTotalScaleFactorForPrinting), m_pageID);
2447 }
2448
2449 #if PLATFORM(MAC)
2450 void WebPageProxy::drawRectToPDF(WebFrameProxy* frame, const IntRect& rect, Vector<uint8_t>& pdfData)
2451 {
2452     // Printing can take a long time, but we need to have the answer.
2453     process()->sendSync(Messages::WebPage::DrawRectToPDF(frame->frameID(), rect), Messages::WebPage::DrawRectToPDF::Reply(pdfData), m_pageID);
2454 }
2455 #endif
2456
2457 } // namespace WebKit