3ce65fb5ecf12bf71027b0f0c9f7f12bbd3662e9
[WebKit.git] / WebKit2 / UIProcess / WebPageProxy.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "WebPageProxy.h"
27
28 #include "DrawingAreaProxy.h"
29 #include "MessageID.h"
30 #include "PageClient.h"
31 #include "WebBackForwardList.h"
32 #include "WebBackForwardListItem.h"
33 #include "WebContext.h"
34 #include "WebContextUserMessageCoders.h"
35 #include "WebCoreArgumentCoders.h"
36 #include "WebData.h"
37 #include "WebEditCommandProxy.h"
38 #include "WebEvent.h"
39 #include "WebFormSubmissionListenerProxy.h"
40 #include "WebFramePolicyListenerProxy.h"
41 #include "WebPageMessageKinds.h"
42 #include "WebPageNamespace.h"
43 #include "WebPageProxyMessageKinds.h"
44 #include "WebPreferences.h"
45 #include "WebProcessManager.h"
46 #include "WebProcessMessageKinds.h"
47 #include "WebProcessProxy.h"
48 #include "WebURLRequest.h"
49
50 #include "WKContextPrivate.h"
51 #include <stdio.h>
52
53 #ifndef NDEBUG
54 #include <wtf/RefCountedLeakCounter.h>
55 #endif
56
57 using namespace WebCore;
58
59 namespace WebKit {
60
61 #ifndef NDEBUG
62 static WTF::RefCountedLeakCounter webPageProxyCounter("WebPageProxy");
63 #endif
64
65 PassRefPtr<WebPageProxy> WebPageProxy::create(WebPageNamespace* pageNamespace, uint64_t pageID)
66 {
67     return adoptRef(new WebPageProxy(pageNamespace, pageID));
68 }
69
70 WebPageProxy::WebPageProxy(WebPageNamespace* pageNamespace, uint64_t pageID)
71     : m_pageClient(0)
72     , m_pageNamespace(pageNamespace)
73     , m_mainFrame(0)
74     , m_estimatedProgress(0.0)
75     , m_isInWindow(false)
76     , m_backForwardList(WebBackForwardList::create(this))
77     , m_textZoomFactor(1)
78     , m_pageZoomFactor(1)
79     , m_valid(true)
80     , m_closed(false)
81     , m_pageID(pageID)
82 {
83 #ifndef NDEBUG
84     webPageProxyCounter.increment();
85 #endif
86 }
87
88 WebPageProxy::~WebPageProxy()
89 {
90 #ifndef NDEBUG
91     webPageProxyCounter.decrement();
92 #endif
93 }
94
95 WebProcessProxy* WebPageProxy::process() const
96 {
97     return m_pageNamespace->process();
98 }
99
100 bool WebPageProxy::isValid()
101 {
102     return m_valid;
103 }
104
105 void WebPageProxy::setPageClient(PageClient* pageClient)
106 {
107     m_pageClient = pageClient;
108 }
109
110 void WebPageProxy::setDrawingArea(PassOwnPtr<DrawingAreaProxy> drawingArea)
111 {
112     if (drawingArea == m_drawingArea)
113         return;
114
115     m_drawingArea = drawingArea;
116 }
117
118 void WebPageProxy::initializeLoaderClient(const WKPageLoaderClient* loadClient)
119 {
120     m_loaderClient.initialize(loadClient);
121 }
122
123 void WebPageProxy::initializePolicyClient(const WKPagePolicyClient* policyClient)
124 {
125     m_policyClient.initialize(policyClient);
126 }
127
128 void WebPageProxy::initializeFormClient(const WKPageFormClient* formClient)
129 {
130     m_formClient.initialize(formClient);
131 }
132
133 void WebPageProxy::initializeUIClient(const WKPageUIClient* client)
134 {
135     m_uiClient.initialize(client);
136 }
137
138 void WebPageProxy::revive()
139 {
140     m_valid = true;
141     m_pageNamespace->reviveIfNecessary();
142     m_pageNamespace->process()->addExistingWebPage(this, m_pageID);
143
144     processDidRevive();
145 }
146
147 void WebPageProxy::initializeWebPage(const IntSize& size, PassOwnPtr<DrawingAreaProxy> drawingArea)
148 {
149     if (!isValid()) {
150         puts("initializeWebPage called with a dead WebProcess");
151         revive();
152         return;
153     }
154
155     m_drawingArea = drawingArea;
156     process()->send(WebProcessMessage::Create, m_pageID, CoreIPC::In(size, pageNamespace()->context()->preferences()->store(), *(m_drawingArea.get())));
157 }
158
159 void WebPageProxy::reinitializeWebPage(const WebCore::IntSize& size)
160 {
161     if (!isValid())
162         return;
163
164     ASSERT(m_drawingArea);
165     process()->send(WebProcessMessage::Create, m_pageID, CoreIPC::In(size, pageNamespace()->context()->preferences()->store(), *(m_drawingArea.get())));
166 }
167
168 void WebPageProxy::close()
169 {
170     if (!isValid())
171         return;
172
173     m_closed = true;
174
175     process()->disconnectFramesFromPage(this);
176     m_mainFrame = 0;
177
178     m_pageTitle = String();
179     m_toolTip = String();
180
181     Vector<RefPtr<ScriptReturnValueCallback> > scriptReturnValueCallbacks;
182     copyValuesToVector(m_scriptReturnValueCallbacks, scriptReturnValueCallbacks);
183     for (size_t i = 0, size = scriptReturnValueCallbacks.size(); i < size; ++i)
184         scriptReturnValueCallbacks[i]->invalidate();
185     m_scriptReturnValueCallbacks.clear();
186
187     Vector<RefPtr<RenderTreeExternalRepresentationCallback> > renderTreeExternalRepresentationCallbacks;
188     copyValuesToVector(m_renderTreeExternalRepresentationCallbacks, renderTreeExternalRepresentationCallbacks);
189     for (size_t i = 0, size = renderTreeExternalRepresentationCallbacks.size(); i < size; ++i)
190         renderTreeExternalRepresentationCallbacks[i]->invalidate();
191     m_renderTreeExternalRepresentationCallbacks.clear();
192
193     Vector<RefPtr<FrameSourceCallback> > frameSourceCallbacks;
194     copyValuesToVector(m_frameSourceCallbacks, frameSourceCallbacks);
195     m_frameSourceCallbacks.clear();
196     for (size_t i = 0, size = frameSourceCallbacks.size(); i < size; ++i)
197         frameSourceCallbacks[i]->invalidate();
198
199     Vector<WebEditCommandProxy*> editCommandVector;
200     copyToVector(m_editCommandSet, editCommandVector);
201     m_editCommandSet.clear();
202     for (size_t i = 0, size = editCommandVector.size(); i < size; ++i)
203         editCommandVector[i]->invalidate();
204
205     m_estimatedProgress = 0.0;
206     
207     m_loaderClient.initialize(0);
208     m_policyClient.initialize(0);
209     m_uiClient.initialize(0);
210
211     m_drawingArea.clear();
212
213     process()->send(WebPageMessage::Close, m_pageID, CoreIPC::In());
214     process()->removeWebPage(m_pageID);
215 }
216
217 bool WebPageProxy::tryClose()
218 {
219     if (!isValid())
220         return true;
221
222     process()->send(WebPageMessage::TryClose, m_pageID, CoreIPC::In());
223     return false;
224 }
225
226 void WebPageProxy::loadURL(const String& url)
227 {
228     if (!isValid()) {
229         puts("loadURL called with a dead WebProcess");
230         revive();
231     }
232
233     process()->send(WebPageMessage::LoadURL, m_pageID, CoreIPC::In(url));
234 }
235
236 void WebPageProxy::loadURLRequest(WebURLRequest* urlRequest)
237 {
238     if (!isValid()) {
239         puts("loadURLRequest called with a dead WebProcess");
240         revive();
241     }
242
243     process()->send(WebPageMessage::LoadURLRequest, m_pageID, CoreIPC::In(urlRequest->resourceRequest()));
244 }
245
246 void WebPageProxy::stopLoading()
247 {
248     if (!isValid())
249         return;
250     process()->send(WebPageMessage::StopLoading, m_pageID, CoreIPC::In());
251 }
252
253 void WebPageProxy::reload(bool reloadFromOrigin)
254 {
255     if (!isValid())
256         return;
257     process()->send(WebPageMessage::Reload, m_pageID, CoreIPC::In(reloadFromOrigin));
258 }
259
260 void WebPageProxy::goForward()
261 {
262     if (!isValid())
263         return;
264
265     if (!canGoForward())
266         return;
267
268     process()->send(WebPageMessage::GoForward, m_pageID, CoreIPC::In(m_backForwardList->forwardItem()->itemID()));
269 }
270
271 bool WebPageProxy::canGoForward() const
272 {
273     return m_backForwardList->forwardItem();
274 }
275
276 void WebPageProxy::goBack()
277 {
278     if (!isValid())
279         return;
280
281     if (!canGoBack())
282         return;
283
284     process()->send(WebPageMessage::GoBack, m_pageID, CoreIPC::In(m_backForwardList->backItem()->itemID()));
285 }
286
287 bool WebPageProxy::canGoBack() const
288 {
289     return m_backForwardList->backItem();
290 }
291
292 void WebPageProxy::goToBackForwardItem(WebBackForwardListItem* item)
293 {
294     if (!isValid())
295         return;
296
297     process()->send(WebPageMessage::GoToBackForwardItem, m_pageID, CoreIPC::In(item->itemID()));
298 }
299
300 void WebPageProxy::didChangeBackForwardList()
301 {
302     m_loaderClient.didChangeBackForwardList(this);
303 }
304
305 void WebPageProxy::setFocused(bool isFocused)
306 {
307     if (!isValid())
308         return;
309     process()->send(WebPageMessage::SetFocused, m_pageID, CoreIPC::In(isFocused));
310 }
311
312 void WebPageProxy::setActive(bool active)
313 {
314     if (!isValid())
315         return;
316     process()->send(WebPageMessage::SetActive, m_pageID, CoreIPC::In(active));
317 }
318
319 void WebPageProxy::setIsInWindow(bool isInWindow)
320 {
321     if (m_isInWindow == isInWindow)
322         return;
323     
324     m_isInWindow = isInWindow;
325     if (!isValid())
326         return;
327     process()->send(WebPageMessage::SetIsInWindow, m_pageID, CoreIPC::In(isInWindow));
328 }
329     
330 void WebPageProxy::mouseEvent(const WebMouseEvent& event)
331 {
332     if (!isValid())
333         return;
334
335     // NOTE: This does not start the responsiveness timer because mouse move should not indicate interaction.
336     if (event.type() != WebEvent::MouseMove)
337         process()->responsivenessTimer()->start();
338     process()->send(WebPageMessage::MouseEvent, m_pageID, CoreIPC::In(event));
339 }
340
341 void WebPageProxy::wheelEvent(const WebWheelEvent& event)
342 {
343     if (!isValid())
344         return;
345
346     process()->responsivenessTimer()->start();
347     process()->send(WebPageMessage::WheelEvent, m_pageID, CoreIPC::In(event));
348 }
349
350 void WebPageProxy::keyEvent(const WebKeyboardEvent& event)
351 {
352     if (!isValid())
353         return;
354
355     process()->responsivenessTimer()->start();
356     process()->send(WebPageMessage::KeyEvent, m_pageID, CoreIPC::In(event));
357 }
358
359 #if ENABLE(TOUCH_EVENTS)
360 void WebPageProxy::touchEvent(const WebTouchEvent& event)
361 {
362     if (!isValid())
363         return;
364     process()->send(WebPageMessage::TouchEvent, m_pageID, CoreIPC::In(event)); 
365 }
366 #endif
367
368 void WebPageProxy::receivedPolicyDecision(WebCore::PolicyAction action, WebFrameProxy* frame, uint64_t listenerID)
369 {
370     if (!isValid())
371         return;
372
373     process()->send(WebPageMessage::DidReceivePolicyDecision, m_pageID, CoreIPC::In(frame->frameID(), listenerID, (uint32_t)action));
374 }
375
376 void WebPageProxy::setCustomUserAgent(const String& userAgent)
377 {
378     if (!isValid())
379         return;
380
381     process()->send(WebPageMessage::SetCustomUserAgent, m_pageID, CoreIPC::In(userAgent));
382 }
383
384 void WebPageProxy::terminateProcess()
385 {
386     if (!isValid())
387         return;
388
389     process()->terminate();
390 }
391
392 PassRefPtr<WebData> WebPageProxy::sessionState() const
393 {
394     // FIXME: Return session state data for saving Page state.
395     return 0;
396 }
397
398 void WebPageProxy::restoreFromSessionState(WebData*)
399 {
400     // FIXME: Restore the Page from the passed in session state data.
401 }
402
403 void WebPageProxy::setTextZoomFactor(double zoomFactor)
404 {
405     if (!isValid())
406         return;
407
408     if (m_textZoomFactor == zoomFactor)
409         return;
410
411     m_textZoomFactor = zoomFactor;
412     process()->send(WebPageMessage::SetTextZoomFactor, m_pageID, CoreIPC::In(m_textZoomFactor)); 
413 }
414
415 void WebPageProxy::setPageZoomFactor(double zoomFactor)
416 {
417     if (!isValid())
418         return;
419
420     if (m_pageZoomFactor == zoomFactor)
421         return;
422
423     m_pageZoomFactor = zoomFactor;
424     process()->send(WebPageMessage::SetPageZoomFactor, m_pageID, CoreIPC::In(m_pageZoomFactor)); 
425 }
426
427 void WebPageProxy::setPageAndTextZoomFactors(double pageZoomFactor, double textZoomFactor)
428 {
429     if (!isValid())
430         return;
431
432     if (m_pageZoomFactor == pageZoomFactor && m_textZoomFactor == textZoomFactor)
433         return;
434
435     m_pageZoomFactor = pageZoomFactor;
436     m_textZoomFactor = textZoomFactor;
437     process()->send(WebPageMessage::SetPageAndTextZoomFactors, m_pageID, CoreIPC::In(m_pageZoomFactor, m_textZoomFactor)); 
438 }
439
440 void WebPageProxy::runJavaScriptInMainFrame(const String& script, PassRefPtr<ScriptReturnValueCallback> prpCallback)
441 {
442     RefPtr<ScriptReturnValueCallback> callback = prpCallback;
443     uint64_t callbackID = callback->callbackID();
444     m_scriptReturnValueCallbacks.set(callbackID, callback.get());
445     process()->send(WebPageMessage::RunJavaScriptInMainFrame, m_pageID, CoreIPC::In(script, callbackID));
446 }
447
448 void WebPageProxy::getRenderTreeExternalRepresentation(PassRefPtr<RenderTreeExternalRepresentationCallback> prpCallback)
449 {
450     RefPtr<RenderTreeExternalRepresentationCallback> callback = prpCallback;
451     uint64_t callbackID = callback->callbackID();
452     m_renderTreeExternalRepresentationCallbacks.set(callbackID, callback.get());
453     process()->send(WebPageMessage::GetRenderTreeExternalRepresentation, m_pageID, callbackID);
454 }
455
456 void WebPageProxy::getSourceForFrame(WebFrameProxy* frame, PassRefPtr<FrameSourceCallback> prpCallback)
457 {
458     RefPtr<FrameSourceCallback> callback = prpCallback;
459     uint64_t callbackID = callback->callbackID();
460     m_frameSourceCallbacks.set(callbackID, callback.get());
461     process()->send(WebPageMessage::GetSourceForFrame, m_pageID, CoreIPC::In(frame->frameID(), callbackID));
462 }
463
464 void WebPageProxy::preferencesDidChange()
465 {
466     if (!isValid())
467         return;
468
469     // FIXME: It probably makes more sense to send individual preference changes.
470     // However, WebKitTestRunner depends on getting a preference change notification even if nothing changed in UI process, so that overrides get removed.
471     process()->send(WebPageMessage::PreferencesDidChange, m_pageID, CoreIPC::In(pageNamespace()->context()->preferences()->store()));
472 }
473
474 void WebPageProxy::getStatistics(WKContextStatistics* statistics)
475 {
476     statistics->numberOfWKFrames += process()->frameCountInPage(this);
477 }
478
479
480 void WebPageProxy::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments)
481 {
482     if (messageID.is<CoreIPC::MessageClassDrawingAreaProxy>()) {
483         m_drawingArea->didReceiveMessage(connection, messageID, arguments);
484         return;
485     }
486
487     switch (messageID.get<WebPageProxyMessage::Kind>()) {
488         case WebPageProxyMessage::DidCreateMainFrame: {
489             uint64_t frameID;
490             if (!arguments->decode(frameID))
491                 return;
492             didCreateMainFrame(frameID);
493             break;
494         }
495         case WebPageProxyMessage::DidCreateSubFrame: {
496             uint64_t frameID;
497             if (!arguments->decode(frameID))
498                 return;
499             didCreateSubFrame(frameID);
500             break;
501         }
502         case WebPageProxyMessage::DidStartProvisionalLoadForFrame: {
503             uint64_t frameID;
504             String url;
505             if (!arguments->decode(CoreIPC::Out(frameID, url)))
506                 return;
507             didStartProvisionalLoadForFrame(process()->webFrame(frameID), url);
508             break;
509         }
510         case WebPageProxyMessage::DidReceiveServerRedirectForProvisionalLoadForFrame: {
511             uint64_t frameID;
512             if (!arguments->decode(frameID))
513                 return;
514             didReceiveServerRedirectForProvisionalLoadForFrame(process()->webFrame(frameID));
515             break;
516         }
517         case WebPageProxyMessage::DidFailProvisionalLoadForFrame: {
518             uint64_t frameID;
519             if (!arguments->decode(frameID))
520                 return;
521             didFailProvisionalLoadForFrame(process()->webFrame(frameID));
522             break;
523         }
524         case WebPageProxyMessage::DidCommitLoadForFrame: {
525             uint64_t frameID;
526             if (!arguments->decode(frameID))
527                 return;
528             didCommitLoadForFrame(process()->webFrame(frameID));
529             break;
530         }
531         case WebPageProxyMessage::DidFinishDocumentLoadForFrame: {
532             uint64_t frameID;
533             if (!arguments->decode(frameID))
534                 return;
535             didFinishDocumentLoadForFrame(process()->webFrame(frameID));
536             break;
537         }
538         case WebPageProxyMessage::DidFinishLoadForFrame: {
539             uint64_t frameID;
540             if (!arguments->decode(frameID))
541                 return;
542             didFinishLoadForFrame(process()->webFrame(frameID));
543             break;
544         }
545         case WebPageProxyMessage::DidFailLoadForFrame: {
546             uint64_t frameID;
547             if (!arguments->decode(frameID))
548                 return;
549             didFailLoadForFrame(process()->webFrame(frameID));
550             break;
551         }
552         case WebPageProxyMessage::DidReceiveTitleForFrame: {
553             uint64_t frameID;
554             String title;
555             if (!arguments->decode(CoreIPC::Out(frameID, title)))
556                 return;
557             didReceiveTitleForFrame(process()->webFrame(frameID), title);
558             break;
559         }
560         case WebPageProxyMessage::DidFirstLayoutForFrame: {
561             uint64_t frameID;
562             if (!arguments->decode(frameID))
563                 return;
564             didFirstLayoutForFrame(process()->webFrame(frameID));
565             break;
566         }
567         case WebPageProxyMessage::DidFirstVisuallyNonEmptyLayoutForFrame: {
568             uint64_t frameID;
569             if (!arguments->decode(frameID))
570                 return;
571             didFirstVisuallyNonEmptyLayoutForFrame(process()->webFrame(frameID));
572             break;
573         }
574         case WebPageProxyMessage::DidStartProgress:
575             didStartProgress();
576             break;
577         case WebPageProxyMessage::DidChangeProgress: {
578             double value;
579             if (!arguments->decode(value))
580                 return;
581             didChangeProgress(value);
582             break;
583         }
584         case WebPageProxyMessage::DidFinishProgress:
585             didFinishProgress();
586             break;
587         case WebPageProxyMessage::DidReceiveEvent: {
588             uint32_t type;
589             if (!arguments->decode(type))
590                 return;
591             didReceiveEvent((WebEvent::Type)type);
592             break;
593         }
594         case WebPageProxyMessage::TakeFocus: {
595             // FIXME: Use enum here.
596             bool direction;
597             if (!arguments->decode(direction))
598                 return;
599             takeFocus(direction);
600             break;
601         }
602         case WebPageProxyMessage::DecidePolicyForNavigationAction: {
603             uint64_t frameID;
604             uint32_t navigationType;
605             uint32_t modifiers;
606             String url;
607             uint64_t listenerID;
608             if (!arguments->decode(CoreIPC::Out(frameID, navigationType, modifiers, url, listenerID)))
609                 return;
610             decidePolicyForNavigationAction(process()->webFrame(frameID), static_cast<NavigationType>(navigationType), static_cast<WebEvent::Modifiers>(modifiers), url, listenerID);
611             break;
612         }
613         case WebPageProxyMessage::DecidePolicyForNewWindowAction: {
614             uint64_t frameID;
615             uint32_t navigationType;
616             uint32_t modifiers;
617             String url;
618             uint64_t listenerID;
619             if (!arguments->decode(CoreIPC::Out(frameID, navigationType, modifiers, url, listenerID)))
620                 return;
621             decidePolicyForNewWindowAction(process()->webFrame(frameID), static_cast<NavigationType>(navigationType), static_cast<WebEvent::Modifiers>(modifiers), url, listenerID);
622             break;
623         }
624         case WebPageProxyMessage::DecidePolicyForMIMEType: {
625             uint64_t frameID;
626             String MIMEType;
627             String url;
628             uint64_t listenerID;
629             if (!arguments->decode(CoreIPC::Out(frameID, MIMEType, url, listenerID)))
630                 return;
631             decidePolicyForMIMEType(process()->webFrame(frameID), MIMEType, url, listenerID);
632             break;
633         }
634         case WebPageProxyMessage::WillSubmitForm: {
635             uint64_t frameID;
636             uint64_t sourceFrameID;
637             Vector<std::pair<String, String> > textFieldValues;
638             uint64_t listenerID;
639
640             RefPtr<APIObject> userData;
641             WebContextUserMessageDecoder messageDecoder(userData, pageNamespace()->context());
642
643             if (!arguments->decode(CoreIPC::Out(frameID, sourceFrameID, textFieldValues, listenerID, messageDecoder)))
644                 return;
645
646             willSubmitForm(process()->webFrame(frameID), process()->webFrame(sourceFrameID), textFieldValues, userData.get(), listenerID);
647             break;
648         }
649         case WebPageProxyMessage::DidRunJavaScriptInMainFrame: {
650             String resultString;
651             uint64_t callbackID;
652             if (!arguments->decode(CoreIPC::Out(resultString, callbackID)))
653                 return;
654             didRunJavaScriptInMainFrame(resultString, callbackID);
655             break;
656         }
657         case WebPageProxyMessage::DidGetRenderTreeExternalRepresentation: {
658             String resultString;
659             uint64_t callbackID;
660             if (!arguments->decode(CoreIPC::Out(resultString, callbackID)))
661                 return;
662             didGetRenderTreeExternalRepresentation(resultString, callbackID);
663             break;
664         }
665         case WebPageProxyMessage::DidGetSourceForFrame: {
666             String resultString;
667             uint64_t callbackID;
668             if (!arguments->decode(CoreIPC::Out(resultString, callbackID)))
669                 return;
670             didGetSourceForFrame(resultString, callbackID);
671             break;
672         }
673         case WebPageProxyMessage::SetToolTip: {
674             String toolTip;
675             if (!arguments->decode(toolTip))
676                 return;
677             setToolTip(toolTip);
678             break;
679         }
680         case WebPageProxyMessage::SetCursor: {
681 #if USE(LAZY_NATIVE_CURSOR)
682             Cursor cursor;
683             if (!arguments->decode(cursor))
684                 return;
685             setCursor(cursor);
686 #endif
687             break;
688         }
689         case WebPageProxyMessage::ShowPage: {
690             showPage();
691             break;
692         }
693         case WebPageProxyMessage::ClosePage: {
694             closePage();
695             break;
696         }
697         case WebPageProxyMessage::BackForwardAddItem: {
698             uint64_t itemID;
699             if (!arguments->decode(CoreIPC::Out(itemID)))
700                 return;
701             addItemToBackForwardList(process()->webBackForwardItem(itemID));
702             break;
703         }
704         case WebPageProxyMessage::BackForwardGoToItem: {
705             uint64_t itemID;
706             if (!arguments->decode(CoreIPC::Out(itemID)))
707                 return;
708             goToItemInBackForwardList(process()->webBackForwardItem(itemID));
709             break;
710         }
711         case WebPageProxyMessage::ContentsSizeChanged: {
712             IntSize size;
713             uint64_t frameID;
714             if (!arguments->decode(CoreIPC::Out(frameID, size)))
715                 return;
716             contentsSizeChanged(process()->webFrame(frameID), size);
717             break;
718         }
719         case WebPageProxyMessage::RegisterEditCommandForUndo: {
720             uint64_t commandID;
721             uint32_t editAction;
722             if (!arguments->decode(CoreIPC::Out(commandID, editAction)))
723                 return;
724                 
725             registerEditCommandForUndo(commandID, static_cast<EditAction>(editAction));
726             break;
727         }
728         case WebPageProxyMessage::ClearAllEditCommands:
729             clearAllEditCommands();
730             break;
731         default:
732             ASSERT_NOT_REACHED();
733             break;
734     }
735 }
736
737 void WebPageProxy::didReceiveSyncMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments, CoreIPC::ArgumentEncoder* reply)
738 {
739     if (messageID.is<CoreIPC::MessageClassDrawingAreaProxy>()) {
740         m_drawingArea->didReceiveSyncMessage(connection, messageID, arguments, reply);
741         return;
742     }
743
744     switch (messageID.get<WebPageProxyMessage::Kind>()) {
745         case WebPageProxyMessage::CreateNewPage: {
746             RefPtr<WebPageProxy> newPage = createNewPage();
747             if (newPage) {
748                 // FIXME: Pass the real size.
749                 reply->encode(CoreIPC::In(newPage->pageID(), IntSize(100, 100), 
750                                           newPage->pageNamespace()->context()->preferences()->store(),
751                                           *(newPage->drawingArea())));
752             } else {
753                 reply->encode(CoreIPC::In(static_cast<uint64_t>(0), IntSize(), WebPreferencesStore(), DrawingAreaBase::DrawingAreaInfo()));
754             }
755             break;
756         }
757         case WebPageProxyMessage::RunJavaScriptAlert: {
758             uint64_t frameID;
759             String message;
760             if (!arguments->decode(CoreIPC::Out(frameID, message)))
761                 return;
762             runJavaScriptAlert(process()->webFrame(frameID), message);
763             break;
764         }
765         case WebPageProxyMessage::RunJavaScriptConfirm: {
766             // FIXME: We should probably encode something in the case that the arguments do not decode correctly.
767             uint64_t frameID;
768             String message;
769             if (!arguments->decode(CoreIPC::Out(frameID, message)))
770                 return;
771
772             bool result = runJavaScriptConfirm(process()->webFrame(frameID), message);
773             reply->encode(CoreIPC::In(result));
774             break;
775         }
776         case WebPageProxyMessage::RunJavaScriptPrompt: {
777             // FIXME: We should probably encode something in the case that the arguments do not decode correctly.
778             uint64_t frameID;
779             String message;
780             String defaultValue;
781             if (!arguments->decode(CoreIPC::Out(frameID, message, defaultValue)))
782                 return;
783
784             String result = runJavaScriptPrompt(process()->webFrame(frameID), message, defaultValue);
785             reply->encode(CoreIPC::In(result));
786             break;
787         }
788
789         case WebPageProxyMessage::BackForwardBackItem: {
790             WebBackForwardListItem* backItem = m_backForwardList->backItem();
791             uint64_t backItemID = backItem ? backItem->itemID() : 0;
792             reply->encode(CoreIPC::In(backItemID));
793             break;
794         }
795         case WebPageProxyMessage::BackForwardCurrentItem: {
796             WebBackForwardListItem* currentItem = m_backForwardList->currentItem();
797             uint64_t currentItemID = currentItem ? currentItem->itemID() : 0;
798             reply->encode(CoreIPC::In(currentItemID));
799             break;
800         }
801         case WebPageProxyMessage::BackForwardForwardItem: {
802             WebBackForwardListItem* forwardItem = m_backForwardList->forwardItem();
803             uint64_t forwardItemID = forwardItem ? forwardItem->itemID() : 0;
804             reply->encode(CoreIPC::In(forwardItemID));
805             break;
806         }
807         case WebPageProxyMessage::BackForwardItemAtIndex: {
808             int itemIndex;
809             if (!arguments->decode(CoreIPC::Out(itemIndex)))
810                 return;
811
812             WebBackForwardListItem* item = m_backForwardList->itemAtIndex(itemIndex);
813             uint64_t itemID = item ? item->itemID() : 0;
814             reply->encode(CoreIPC::In(itemID));
815             break;
816         }
817         case WebPageProxyMessage::BackForwardBackListCount: {
818             int backListCount = m_backForwardList->backListCount();
819             reply->encode(CoreIPC::In(backListCount));
820             break;
821         }
822         case WebPageProxyMessage::BackForwardForwardListCount: {
823             int forwardListCount = m_backForwardList->forwardListCount();
824             reply->encode(CoreIPC::In(forwardListCount));
825             break;
826         }
827 #if USE(ACCELERATED_COMPOSITING)
828         case WebPageProxyMessage::DidChangeAcceleratedCompositing: {
829             bool compositing;
830             if (!arguments->decode(CoreIPC::Out(compositing)))
831                 return;
832
833             didChangeAcceleratedCompositing(compositing);
834             reply->encode(*(drawingArea()));
835             break;
836         }
837 #endif // USE(ACCELERATED_COMPOSITING)
838
839         default:
840             ASSERT_NOT_REACHED();
841             break;
842     }
843 }
844
845 void WebPageProxy::didCreateMainFrame(uint64_t frameID)
846 {
847     ASSERT(!m_mainFrame);
848
849     m_mainFrame = WebFrameProxy::create(this, frameID);
850     process()->frameCreated(frameID, m_mainFrame.get());
851 }
852
853 void WebPageProxy::didCreateSubFrame(uint64_t frameID)
854 {
855     ASSERT(m_mainFrame);
856     process()->frameCreated(frameID, WebFrameProxy::create(this, frameID).get());
857 }
858
859 void WebPageProxy::didStartProgress()
860 {
861     m_estimatedProgress = 0.0;
862     
863     m_loaderClient.didStartProgress(this);
864 }
865
866 void WebPageProxy::didChangeProgress(double value)
867 {
868     m_estimatedProgress = value;
869     
870     m_loaderClient.didChangeProgress(this);
871 }
872
873 void WebPageProxy::didFinishProgress()
874 {
875     m_estimatedProgress = 1.0;
876
877     m_loaderClient.didFinishProgress(this);
878 }
879
880 void WebPageProxy::didStartProvisionalLoadForFrame(WebFrameProxy* frame, const String& url)
881 {
882     frame->didStartProvisionalLoad(url);
883     m_loaderClient.didStartProvisionalLoadForFrame(this, frame);
884 }
885
886 void WebPageProxy::didReceiveServerRedirectForProvisionalLoadForFrame(WebFrameProxy* frame)
887 {
888     m_loaderClient.didReceiveServerRedirectForProvisionalLoadForFrame(this, frame);
889 }
890
891 void WebPageProxy::didFailProvisionalLoadForFrame(WebFrameProxy* frame)
892 {
893     m_loaderClient.didFailProvisionalLoadWithErrorForFrame(this, frame);
894 }
895
896 void WebPageProxy::didCommitLoadForFrame(WebFrameProxy* frame)
897 {
898     frame->didCommitLoad();
899     m_loaderClient.didCommitLoadForFrame(this, frame);
900 }
901
902 void WebPageProxy::didFinishDocumentLoadForFrame(WebFrameProxy* frame)
903 {
904     m_loaderClient.didFinishDocumentLoadForFrame(this, frame);
905 }
906
907 void WebPageProxy::didFinishLoadForFrame(WebFrameProxy* frame)
908 {
909     frame->didFinishLoad();
910     m_loaderClient.didFinishLoadForFrame(this, frame);
911 }
912
913 void WebPageProxy::didFailLoadForFrame(WebFrameProxy* frame)
914 {
915     m_loaderClient.didFailLoadWithErrorForFrame(this, frame);
916 }
917
918 void WebPageProxy::didReceiveTitleForFrame(WebFrameProxy* frame, const String& title)
919 {
920     frame->didReceiveTitle(title);
921
922     // Cache the title for the main frame in the page.
923     if (frame == m_mainFrame)
924         m_pageTitle = title;
925
926     m_loaderClient.didReceiveTitleForFrame(this, title.impl(), frame);
927 }
928
929 void WebPageProxy::didFirstLayoutForFrame(WebFrameProxy* frame)
930 {
931     m_loaderClient.didFirstLayoutForFrame(this, frame);
932 }
933
934 void WebPageProxy::didFirstVisuallyNonEmptyLayoutForFrame(WebFrameProxy* frame)
935 {
936     m_loaderClient.didFirstVisuallyNonEmptyLayoutForFrame(this, frame);
937 }
938
939 // PolicyClient
940
941 void WebPageProxy::decidePolicyForNavigationAction(WebFrameProxy* frame, NavigationType navigationType, WebEvent::Modifiers modifiers, const String& url, uint64_t listenerID)
942 {
943     RefPtr<WebFramePolicyListenerProxy> listener = frame->setUpPolicyListenerProxy(listenerID);
944     if (!m_policyClient.decidePolicyForNavigationAction(this, navigationType, modifiers, url, frame, listener.get()))
945         listener->use();
946 }
947
948 void WebPageProxy::decidePolicyForNewWindowAction(WebFrameProxy* frame, NavigationType navigationType, WebEvent::Modifiers modifiers, const String& url, uint64_t listenerID)
949 {
950     RefPtr<WebFramePolicyListenerProxy> listener = frame->setUpPolicyListenerProxy(listenerID);
951     if (!m_policyClient.decidePolicyForNewWindowAction(this, navigationType, modifiers, url, frame, listener.get()))
952         listener->use();
953 }
954
955 void WebPageProxy::decidePolicyForMIMEType(WebFrameProxy* frame, const String& MIMEType, const String& url, uint64_t listenerID)
956 {
957     RefPtr<WebFramePolicyListenerProxy> listener = frame->setUpPolicyListenerProxy(listenerID);
958     if (!m_policyClient.decidePolicyForMIMEType(this, MIMEType, url, frame, listener.get()))
959         listener->use();
960 }
961
962 // FormClient
963
964 void WebPageProxy::willSubmitForm(WebFrameProxy* frame, WebFrameProxy* sourceFrame, Vector<std::pair<String, String> >& textFieldValues, APIObject* userData, uint64_t listenerID)
965 {
966     RefPtr<WebFormSubmissionListenerProxy> listener = frame->setUpFormSubmissionListenerProxy(listenerID);
967     if (!m_formClient.willSubmitForm(this, frame, sourceFrame, textFieldValues, userData, listener.get()))
968         listener->continueSubmission();
969 }
970
971 // UIClient
972
973 PassRefPtr<WebPageProxy> WebPageProxy::createNewPage()
974 {
975     return m_uiClient.createNewPage(this);
976 }
977     
978 void WebPageProxy::showPage()
979 {
980     m_uiClient.showPage(this);
981 }
982
983 void WebPageProxy::closePage()
984 {
985     m_uiClient.close(this);
986 }
987
988 void WebPageProxy::runJavaScriptAlert(WebFrameProxy* frame, const String& message)
989 {
990     m_uiClient.runJavaScriptAlert(this, message, frame);
991 }
992
993 bool WebPageProxy::runJavaScriptConfirm(WebFrameProxy* frame, const String& message)
994 {
995     return m_uiClient.runJavaScriptConfirm(this, message, frame);
996 }
997
998 String WebPageProxy::runJavaScriptPrompt(WebFrameProxy* frame, const String& message, const String& defaultValue)
999 {
1000     return m_uiClient.runJavaScriptPrompt(this, message, defaultValue, frame);
1001 }
1002
1003 void WebPageProxy::contentsSizeChanged(WebFrameProxy* frame, const WebCore::IntSize& size)
1004 {
1005     m_uiClient.contentsSizeChanged(this, size, frame);
1006 }
1007
1008 // BackForwardList
1009
1010 void WebPageProxy::addItemToBackForwardList(WebBackForwardListItem* item)
1011 {
1012     m_backForwardList->addItem(item);
1013 }
1014
1015 void WebPageProxy::goToItemInBackForwardList(WebBackForwardListItem* item)
1016 {
1017     m_backForwardList->goToItem(item);
1018 }
1019
1020 // Undo management
1021
1022 void WebPageProxy::registerEditCommandForUndo(uint64_t commandID, EditAction editAction)
1023 {
1024     registerEditCommandForUndo(WebEditCommandProxy::create(commandID, editAction, this));
1025 }
1026
1027 void WebPageProxy::clearAllEditCommands()
1028 {
1029     m_pageClient->clearAllEditCommands();
1030 }
1031
1032 void WebPageProxy::registerEditCommandForUndo(PassRefPtr<WebEditCommandProxy> commandProxy)
1033 {
1034     m_pageClient->registerEditCommand(commandProxy, PageClient::Undo);
1035 }
1036
1037 void WebPageProxy::registerEditCommandForRedo(PassRefPtr<WebEditCommandProxy> commandProxy)
1038 {
1039     m_pageClient->registerEditCommand(commandProxy, PageClient::Redo);
1040 }
1041
1042 void WebPageProxy::addEditCommand(WebEditCommandProxy* command)
1043 {
1044     m_editCommandSet.add(command);
1045 }
1046
1047 void WebPageProxy::removeEditCommand(WebEditCommandProxy* command)
1048 {
1049     m_editCommandSet.remove(command);
1050
1051     if (!isValid())
1052         return;
1053     process()->send(WebPageMessage::DidRemoveEditCommand, m_pageID, command->commandID());
1054 }
1055
1056 // Other
1057
1058 void WebPageProxy::takeFocus(bool direction)
1059 {
1060     m_pageClient->takeFocus(direction);
1061 }
1062
1063 void WebPageProxy::setToolTip(const String& toolTip)
1064 {
1065     String oldToolTip = m_toolTip;
1066     m_toolTip = toolTip;
1067     m_pageClient->toolTipChanged(oldToolTip, m_toolTip);
1068 }
1069
1070 void WebPageProxy::setCursor(const WebCore::Cursor& cursor)
1071 {
1072     m_pageClient->setCursor(cursor);
1073 }
1074
1075 void WebPageProxy::didReceiveEvent(WebEvent::Type type)
1076 {
1077     switch (type) {
1078         case WebEvent::MouseMove:
1079             break;
1080
1081         case WebEvent::MouseDown:
1082         case WebEvent::MouseUp:
1083         case WebEvent::Wheel:
1084         case WebEvent::KeyDown:
1085         case WebEvent::KeyUp:
1086         case WebEvent::RawKeyDown:
1087         case WebEvent::Char:
1088             process()->responsivenessTimer()->stop();
1089             break;
1090     }
1091 }
1092
1093 void WebPageProxy::didRunJavaScriptInMainFrame(const String& resultString, uint64_t callbackID)
1094 {
1095     RefPtr<ScriptReturnValueCallback> callback = m_scriptReturnValueCallbacks.take(callbackID);
1096     if (!callback) {
1097         // FIXME: Log error or assert.
1098         return;
1099     }
1100
1101     callback->performCallbackWithReturnValue(resultString.impl());
1102 }
1103
1104 void WebPageProxy::didGetRenderTreeExternalRepresentation(const String& resultString, uint64_t callbackID)
1105 {
1106     RefPtr<RenderTreeExternalRepresentationCallback> callback = m_renderTreeExternalRepresentationCallbacks.take(callbackID);
1107     if (!callback) {
1108         // FIXME: Log error or assert.
1109         return;
1110     }
1111
1112     callback->performCallbackWithReturnValue(resultString.impl());
1113 }
1114
1115 void WebPageProxy::didGetSourceForFrame(const String& resultString, uint64_t callbackID)
1116 {
1117     RefPtr<FrameSourceCallback> callback = m_frameSourceCallbacks.take(callbackID);
1118     if (!callback) {
1119         // FIXME: Log error or assert.
1120         return;
1121     }
1122
1123     callback->performCallbackWithReturnValue(resultString.impl());
1124 }
1125
1126 #if USE(ACCELERATED_COMPOSITING)
1127 void WebPageProxy::didChangeAcceleratedCompositing(bool compositing)
1128 {
1129     if (compositing)
1130         didEnterAcceleratedCompositing();
1131     else
1132         didLeaveAcceleratedCompositing();
1133 }
1134 #endif
1135
1136 void WebPageProxy::processDidBecomeUnresponsive()
1137 {
1138     m_loaderClient.didBecomeUnresponsive(this);
1139 }
1140
1141 void WebPageProxy::processDidBecomeResponsive()
1142 {
1143     m_loaderClient.didBecomeResponsive(this);
1144 }
1145
1146 void WebPageProxy::processDidExit()
1147 {
1148     ASSERT(m_pageClient);
1149
1150     m_valid = false;
1151
1152     if (m_mainFrame)
1153         m_urlAtProcessExit = m_mainFrame->url();
1154
1155     m_mainFrame = 0;
1156
1157     m_pageTitle = String();
1158     m_toolTip = String();
1159
1160     Vector<RefPtr<ScriptReturnValueCallback> > scriptReturnValueCallbacks;
1161     copyValuesToVector(m_scriptReturnValueCallbacks, scriptReturnValueCallbacks);
1162     for (size_t i = 0, size = scriptReturnValueCallbacks.size(); i < size; ++i)
1163         scriptReturnValueCallbacks[i]->invalidate();
1164     m_scriptReturnValueCallbacks.clear();
1165
1166     Vector<RefPtr<RenderTreeExternalRepresentationCallback> > renderTreeExternalRepresentationCallbacks;
1167     copyValuesToVector(m_renderTreeExternalRepresentationCallbacks, renderTreeExternalRepresentationCallbacks);
1168     for (size_t i = 0, size = renderTreeExternalRepresentationCallbacks.size(); i < size; ++i)
1169         renderTreeExternalRepresentationCallbacks[i]->invalidate();
1170     m_renderTreeExternalRepresentationCallbacks.clear();
1171
1172     Vector<RefPtr<FrameSourceCallback> > frameSourceCallbacks;
1173     copyValuesToVector(m_frameSourceCallbacks, frameSourceCallbacks);
1174     m_frameSourceCallbacks.clear();
1175     for (size_t i = 0, size = frameSourceCallbacks.size(); i < size; ++i)
1176         frameSourceCallbacks[i]->invalidate();
1177
1178     Vector<WebEditCommandProxy*> editCommandVector;
1179     copyToVector(m_editCommandSet, editCommandVector);
1180     m_editCommandSet.clear();
1181     for (size_t i = 0, size = editCommandVector.size(); i < size; ++i)
1182         editCommandVector[i]->invalidate();
1183     m_pageClient->clearAllEditCommands();
1184
1185     m_estimatedProgress = 0.0;
1186
1187     m_pageClient->processDidExit();
1188     m_loaderClient.processDidExit(this);
1189 }
1190
1191 void WebPageProxy::processDidRevive()
1192 {
1193     ASSERT(m_pageClient);
1194     m_pageClient->processDidRevive();
1195 }
1196
1197 #if USE(ACCELERATED_COMPOSITING)
1198 void WebPageProxy::didEnterAcceleratedCompositing()
1199 {
1200     m_pageClient->pageDidEnterAcceleratedCompositing();
1201 }
1202
1203 void WebPageProxy::didLeaveAcceleratedCompositing()
1204 {
1205     m_pageClient->pageDidLeaveAcceleratedCompositing();
1206 }
1207 #endif // USE(ACCELERATED_COMPOSITING)
1208
1209 } // namespace WebKit