Unreviewed, rolling out r126169.
[WebKit-https.git] / Source / WebKit / chromium / src / WebViewImpl.cpp
1 /*
2  * Copyright (C) 2011, 2012 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "WebViewImpl.h"
33
34 #include "AXObjectCache.h"
35 #include "ActivePlatformGestureAnimation.h"
36 #include "AutofillPopupMenuClient.h"
37 #include "BackForwardListChromium.h"
38 #include "BatteryClientImpl.h"
39 #include "BatteryController.h"
40 #include "CSSValueKeywords.h"
41 #include "Chrome.h"
42 #include "Color.h"
43 #include "ColorSpace.h"
44 #include "CompositionUnderlineVectorBuilder.h"
45 #include "CompositorHUDFontAtlas.h"
46 #include "ContextFeaturesClientImpl.h"
47 #include "ContextMenu.h"
48 #include "ContextMenuController.h"
49 #include "ContextMenuItem.h"
50 #include "Cursor.h"
51 #include "DOMUtilitiesPrivate.h"
52 #include "DeviceOrientationClientProxy.h"
53 #include "Document.h"
54 #include "DocumentLoader.h"
55 #include "DragController.h"
56 #include "DragData.h"
57 #include "DragScrollTimer.h"
58 #include "DragSession.h"
59 #include "Editor.h"
60 #include "EventHandler.h"
61 #include "Extensions3D.h"
62 #include "FocusController.h"
63 #include "FontDescription.h"
64 #include "Frame.h"
65 #include "FrameLoader.h"
66 #include "FrameSelection.h"
67 #include "FrameTree.h"
68 #include "FrameView.h"
69 #include "GeolocationClientProxy.h"
70 #include "GeolocationController.h"
71 #include "GraphicsContext.h"
72 #include "GraphicsContext3D.h"
73 #include "GraphicsContext3DPrivate.h"
74 #include "HTMLInputElement.h"
75 #include "HTMLMediaElement.h"
76 #include "HTMLNames.h"
77 #include "HTMLTextAreaElement.h"
78 #include "HitTestResult.h"
79 #include "Image.h"
80 #include "ImageBuffer.h"
81 #include "InspectorController.h"
82 #include "InspectorInstrumentation.h"
83 #include "KeyboardCodes.h"
84 #include "KeyboardEvent.h"
85 #include "LayerPainterChromium.h"
86 #include "MIMETypeRegistry.h"
87 #include "NodeRenderStyle.h"
88 #include "NonCompositedContentHost.h"
89 #include "Page.h"
90 #include "PageGroup.h"
91 #include "PageGroupLoadDeferrer.h"
92 #include "PagePopupClient.h"
93 #include "PageWidgetDelegate.h"
94 #include "Pasteboard.h"
95 #include "PlatformContextSkia.h"
96 #include "PlatformKeyboardEvent.h"
97 #include "PlatformMouseEvent.h"
98 #include "PlatformThemeChromiumLinux.h"
99 #include "PlatformWheelEvent.h"
100 #include "PointerLockController.h"
101 #include "PopupContainer.h"
102 #include "PopupMenuClient.h"
103 #include "PrerendererClientImpl.h"
104 #include "ProgressTracker.h"
105 #include "RenderLayerCompositor.h"
106 #include "RenderView.h"
107 #include "RenderWidget.h"
108 #include "ResourceHandle.h"
109 #include "SchemeRegistry.h"
110 #include "SecurityOrigin.h"
111 #include "SecurityPolicy.h"
112 #include "Settings.h"
113 #include "SharedGraphicsContext3D.h"
114 #include "SpeechInputClientImpl.h"
115 #include "SpeechRecognitionClientProxy.h"
116 #include "StyleResolver.h"
117 #include "Text.h"
118 #include "TextFieldDecoratorImpl.h"
119 #include "TextIterator.h"
120 #include "Timer.h"
121 #include "TouchpadFlingPlatformGestureCurve.h"
122 #include "TraceEvent.h"
123 #include "UserGestureIndicator.h"
124 #include "WebAccessibilityObject.h"
125 #include "WebActiveWheelFlingParameters.h"
126 #include "WebAutofillClient.h"
127 #include "WebCompositorImpl.h"
128 #include "WebDevToolsAgentImpl.h"
129 #include "WebDevToolsAgentPrivate.h"
130 #include "WebFrameImpl.h"
131 #include "WebHelperPluginImpl.h"
132 #include "WebHitTestResult.h"
133 #include "WebInputElement.h"
134 #include "WebInputEvent.h"
135 #include "WebInputEventConversion.h"
136 #include "WebMediaPlayerAction.h"
137 #include "WebNode.h"
138 #include "WebPagePopupImpl.h"
139 #include "WebPlugin.h"
140 #include "WebPluginAction.h"
141 #include "WebPluginContainerImpl.h"
142 #include "WebPopupMenuImpl.h"
143 #include "WebRange.h"
144 #include "WebRuntimeFeatures.h"
145 #include "WebSettingsImpl.h"
146 #include "WebTextInputInfo.h"
147 #include "WebTouchCandidatesInfo.h"
148 #include "WebViewClient.h"
149 #include "WheelEvent.h"
150 #include "painting/GraphicsContextBuilder.h"
151 #include <public/Platform.h>
152 #include <public/WebCompositor.h>
153 #include <public/WebCompositorOutputSurface.h>
154 #include <public/WebDragData.h>
155 #include <public/WebFloatPoint.h>
156 #include <public/WebGraphicsContext3D.h>
157 #include <public/WebImage.h>
158 #include <public/WebLayer.h>
159 #include <public/WebLayerTreeView.h>
160 #include <public/WebPoint.h>
161 #include <public/WebRect.h>
162 #include <public/WebString.h>
163 #include <public/WebVector.h>
164 #include <wtf/CurrentTime.h>
165 #include <wtf/MainThread.h>
166 #include <wtf/RefPtr.h>
167 #include <wtf/TemporaryChange.h>
168 #include <wtf/Uint8ClampedArray.h>
169
170 #if ENABLE(GESTURE_EVENTS)
171 #include "PlatformGestureEvent.h"
172 #endif
173
174 #if OS(WINDOWS)
175 #include "RenderThemeChromiumWin.h"
176 #else
177 #if OS(UNIX) && !OS(DARWIN)
178 #include "RenderThemeChromiumLinux.h"
179 #endif
180 #include "RenderTheme.h"
181 #endif
182
183 // Get rid of WTF's pow define so we can use std::pow.
184 #undef pow
185 #include <cmath> // for std::pow
186
187 using namespace WebCore;
188 using namespace std;
189
190 // The following constants control parameters for automated scaling of webpages
191 // (such as due to a double tap gesture or find in page etc.). These are
192 // experimentally determined.
193 static const int touchPointPadding = 32;
194 static const float minScaleDifference = 0.01f;
195 static const float doubleTapZoomContentDefaultMargin = 5;
196 static const float doubleTapZoomContentMinimumMargin = 2;
197 static const double doubleTapZoomAnimationDurationInSeconds = 0.25;
198
199 // Constants for zooming in on a focused text field.
200 static const double scrollAndScaleAnimationDurationInSeconds = 0.2;
201
202 namespace WebKit {
203
204 // Change the text zoom level by kTextSizeMultiplierRatio each time the user
205 // zooms text in or out (ie., change by 20%).  The min and max values limit
206 // text zoom to half and 3x the original text size.  These three values match
207 // those in Apple's port in WebKit/WebKit/WebView/WebView.mm
208 const double WebView::textSizeMultiplierRatio = 1.2;
209 const double WebView::minTextSizeMultiplier = 0.5;
210 const double WebView::maxTextSizeMultiplier = 3.0;
211 const float WebView::minPageScaleFactor = 0.25;
212 const float WebView::maxPageScaleFactor = 4.0;
213
214
215 // The group name identifies a namespace of pages. Page group is used on PLATFORM(MAC)
216 // for some programs that use HTML views to display things that don't seem like
217 // web pages to the user (so shouldn't have visited link coloring).  We only use
218 // one page group.
219 const char* pageGroupName = "default";
220
221 // Used to defer all page activity in cases where the embedder wishes to run
222 // a nested event loop. Using a stack enables nesting of message loop invocations.
223 static Vector<PageGroupLoadDeferrer*>& pageGroupLoadDeferrerStack()
224 {
225     DEFINE_STATIC_LOCAL(Vector<PageGroupLoadDeferrer*>, deferrerStack, ());
226     return deferrerStack;
227 }
228
229 // Ensure that the WebDragOperation enum values stay in sync with the original
230 // DragOperation constants.
231 #define COMPILE_ASSERT_MATCHING_ENUM(coreName) \
232     COMPILE_ASSERT(int(coreName) == int(Web##coreName), dummy##coreName)
233 COMPILE_ASSERT_MATCHING_ENUM(DragOperationNone);
234 COMPILE_ASSERT_MATCHING_ENUM(DragOperationCopy);
235 COMPILE_ASSERT_MATCHING_ENUM(DragOperationLink);
236 COMPILE_ASSERT_MATCHING_ENUM(DragOperationGeneric);
237 COMPILE_ASSERT_MATCHING_ENUM(DragOperationPrivate);
238 COMPILE_ASSERT_MATCHING_ENUM(DragOperationMove);
239 COMPILE_ASSERT_MATCHING_ENUM(DragOperationDelete);
240 COMPILE_ASSERT_MATCHING_ENUM(DragOperationEvery);
241
242 static const PopupContainerSettings autofillPopupSettings = {
243     false, // setTextOnIndexChange
244     false, // acceptOnAbandon
245     true, // loopSelectionNavigation
246     false // restrictWidthOfListBox (For security reasons show the entire entry
247           // so the user doesn't enter information he did not intend to.)
248 };
249
250 static bool shouldUseExternalPopupMenus = false;
251
252 static int webInputEventKeyStateToPlatformEventKeyState(int webInputEventKeyState)
253 {
254     int platformEventKeyState = 0;
255     if (webInputEventKeyState & WebInputEvent::ShiftKey)
256         platformEventKeyState = platformEventKeyState | WebCore::PlatformEvent::ShiftKey;
257     if (webInputEventKeyState & WebInputEvent::ControlKey)
258         platformEventKeyState = platformEventKeyState | WebCore::PlatformEvent::CtrlKey;
259     if (webInputEventKeyState & WebInputEvent::AltKey)
260         platformEventKeyState = platformEventKeyState | WebCore::PlatformEvent::AltKey;
261     if (webInputEventKeyState & WebInputEvent::MetaKey)
262         platformEventKeyState = platformEventKeyState | WebCore::PlatformEvent::MetaKey;
263     return platformEventKeyState;
264 }
265
266 // WebView ----------------------------------------------------------------
267
268 WebView* WebView::create(WebViewClient* client)
269 {
270     // Pass the WebViewImpl's self-reference to the caller.
271     return adoptRef(new WebViewImpl(client)).leakRef();
272 }
273
274 void WebView::setUseExternalPopupMenus(bool useExternalPopupMenus)
275 {
276     shouldUseExternalPopupMenus = useExternalPopupMenus;
277 }
278
279 void WebView::updateVisitedLinkState(unsigned long long linkHash)
280 {
281     Page::visitedStateChanged(PageGroup::pageGroup(pageGroupName), linkHash);
282 }
283
284 void WebView::resetVisitedLinkState()
285 {
286     Page::allVisitedStateChanged(PageGroup::pageGroup(pageGroupName));
287 }
288
289 void WebView::willEnterModalLoop()
290 {
291     PageGroup* pageGroup = PageGroup::pageGroup(pageGroupName);
292     ASSERT(pageGroup);
293
294     if (pageGroup->pages().isEmpty())
295         pageGroupLoadDeferrerStack().append(static_cast<PageGroupLoadDeferrer*>(0));
296     else {
297         // Pick any page in the page group since we are deferring all pages.
298         pageGroupLoadDeferrerStack().append(new PageGroupLoadDeferrer(*pageGroup->pages().begin(), true));
299     }
300 }
301
302 void WebView::didExitModalLoop()
303 {
304     ASSERT(pageGroupLoadDeferrerStack().size());
305
306     delete pageGroupLoadDeferrerStack().last();
307     pageGroupLoadDeferrerStack().removeLast();
308 }
309
310 void WebViewImpl::initializeMainFrame(WebFrameClient* frameClient)
311 {
312     // NOTE: The WebFrameImpl takes a reference to itself within InitMainFrame
313     // and releases that reference once the corresponding Frame is destroyed.
314     RefPtr<WebFrameImpl> frame = WebFrameImpl::create(frameClient);
315
316     frame->initializeAsMainFrame(page());
317
318     // Restrict the access to the local file system
319     // (see WebView.mm WebView::_commonInitializationWithFrameName).
320     SecurityPolicy::setLocalLoadPolicy(SecurityPolicy::AllowLocalLoadsForLocalOnly);
321 }
322
323 void WebViewImpl::initializeHelperPluginFrame(WebFrameClient* client)
324 {
325     RefPtr<WebFrameImpl> frame = WebFrameImpl::create(client);
326 }
327
328 void WebViewImpl::setAutofillClient(WebAutofillClient* autofillClient)
329 {
330     m_autofillClient = autofillClient;
331 }
332
333 void WebViewImpl::setDevToolsAgentClient(WebDevToolsAgentClient* devToolsClient)
334 {
335     if (devToolsClient)
336         m_devToolsAgent = adoptPtr(new WebDevToolsAgentImpl(this, devToolsClient));
337     else
338         m_devToolsAgent.clear();
339 }
340
341 void WebViewImpl::setPermissionClient(WebPermissionClient* permissionClient)
342 {
343     m_permissionClient = permissionClient;
344     m_featureSwitchClient->setPermissionClient(permissionClient);
345 }
346
347 void WebViewImpl::setPrerendererClient(WebPrerendererClient* prerendererClient)
348 {
349     providePrerendererClientTo(m_page.get(), new PrerendererClientImpl(prerendererClient));
350 }
351
352 void WebViewImpl::setSpellCheckClient(WebSpellCheckClient* spellCheckClient)
353 {
354     m_spellCheckClient = spellCheckClient;
355 }
356
357 void WebViewImpl::addTextFieldDecoratorClient(WebTextFieldDecoratorClient* client)
358 {
359     ASSERT(client);
360     // We limit the number of decorators because it affects performance of text
361     // field creation. If you'd like to add more decorators, consider moving
362     // your decorator or existing decorators to WebCore.
363     const unsigned maximumNumberOfDecorators = 8;
364     if (m_textFieldDecorators.size() >= maximumNumberOfDecorators)
365         CRASH();
366     m_textFieldDecorators.append(TextFieldDecoratorImpl::create(client));
367 }
368
369 WebViewImpl::WebViewImpl(WebViewClient* client)
370     : m_client(client)
371     , m_autofillClient(0)
372     , m_permissionClient(0)
373     , m_spellCheckClient(0)
374     , m_chromeClientImpl(this)
375     , m_contextMenuClientImpl(this)
376     , m_dragClientImpl(this)
377     , m_editorClientImpl(this)
378     , m_inspectorClientImpl(this)
379     , m_shouldAutoResize(false)
380     , m_observedNewNavigation(false)
381 #ifndef NDEBUG
382     , m_newNavigationLoader(0)
383 #endif
384     , m_zoomLevel(0)
385     , m_minimumZoomLevel(zoomFactorToZoomLevel(minTextSizeMultiplier))
386     , m_maximumZoomLevel(zoomFactorToZoomLevel(maxTextSizeMultiplier))
387     , m_pageDefinedMinimumPageScaleFactor(-1)
388     , m_pageDefinedMaximumPageScaleFactor(-1)
389     , m_minimumPageScaleFactor(minPageScaleFactor)
390     , m_maximumPageScaleFactor(maxPageScaleFactor)
391     , m_ignoreViewportTagMaximumScale(false)
392     , m_pageScaleFactorIsSet(false)
393     , m_savedPageScaleFactor(0)
394     , m_contextMenuAllowed(false)
395     , m_doingDragAndDrop(false)
396     , m_ignoreInputEvents(false)
397     , m_suppressNextKeypressEvent(false)
398     , m_initialNavigationPolicy(WebNavigationPolicyIgnore)
399     , m_imeAcceptEvents(true)
400     , m_operationsAllowed(WebDragOperationNone)
401     , m_dragOperation(WebDragOperationNone)
402     , m_featureSwitchClient(adoptPtr(new ContextFeaturesClientImpl()))
403     , m_autofillPopupShowing(false)
404     , m_autofillPopup(0)
405     , m_isTransparent(false)
406     , m_tabsToLinks(false)
407     , m_dragScrollTimer(adoptPtr(new DragScrollTimer))
408     , m_isCancelingFullScreen(false)
409     , m_benchmarkSupport(this)
410 #if USE(ACCELERATED_COMPOSITING)
411     , m_rootLayer(0)
412     , m_rootGraphicsLayer(0)
413     , m_isAcceleratedCompositingActive(false)
414     , m_compositorCreationFailed(false)
415     , m_recreatingGraphicsContext(false)
416     , m_compositorSurfaceReady(false)
417     , m_deviceScaleInCompositor(1)
418 #endif
419 #if ENABLE(INPUT_SPEECH)
420     , m_speechInputClient(SpeechInputClientImpl::create(client))
421 #endif
422 #if ENABLE(SCRIPTED_SPEECH)
423     , m_speechRecognitionClient(SpeechRecognitionClientProxy::create(client ? client->speechRecognizer() : 0))
424 #endif
425     , m_deviceOrientationClientProxy(adoptPtr(new DeviceOrientationClientProxy(client ? client->deviceOrientationClient() : 0)))
426     , m_geolocationClientProxy(adoptPtr(new GeolocationClientProxy(client ? client->geolocationClient() : 0)))
427 #if ENABLE(BATTERY_STATUS)
428     , m_batteryClient(adoptPtr(new BatteryClientImpl(client ? client->batteryStatusClient() : 0)))
429 #endif
430     , m_emulatedTextZoomFactor(1)
431 #if ENABLE(MEDIA_STREAM)
432     , m_userMediaClientImpl(this)
433 #endif
434 #if ENABLE(REGISTER_PROTOCOL_HANDLER)
435     , m_registerProtocolHandlerClient(RegisterProtocolHandlerClientImpl::create(this))
436 #endif
437     , m_flingModifier(0)
438 {
439     // WebKit/win/WebView.cpp does the same thing, except they call the
440     // KJS specific wrapper around this method. We need to have threading
441     // initialized because CollatorICU requires it.
442     WTF::initializeThreading();
443     WTF::initializeMainThread();
444
445     Page::PageClients pageClients;
446     pageClients.chromeClient = &m_chromeClientImpl;
447     pageClients.contextMenuClient = &m_contextMenuClientImpl;
448     pageClients.editorClient = &m_editorClientImpl;
449     pageClients.dragClient = &m_dragClientImpl;
450     pageClients.inspectorClient = &m_inspectorClientImpl;
451     pageClients.backForwardClient = BackForwardListChromium::create(this);
452
453     m_page = adoptPtr(new Page(pageClients));
454 #if ENABLE(MEDIA_STREAM)
455     provideUserMediaTo(m_page.get(), &m_userMediaClientImpl);
456 #endif
457 #if ENABLE(INPUT_SPEECH)
458     provideSpeechInputTo(m_page.get(), m_speechInputClient.get());
459 #endif
460 #if ENABLE(SCRIPTED_SPEECH)
461     provideSpeechRecognitionTo(m_page.get(), m_speechRecognitionClient.get());
462 #endif
463 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
464     provideNotification(m_page.get(), notificationPresenterImpl());
465 #endif
466 #if ENABLE(REGISTER_PROTOCOL_HANDLER)
467     provideRegisterProtocolHandlerTo(m_page.get(), m_registerProtocolHandlerClient.get());
468 #endif
469
470     provideContextFeaturesTo(m_page.get(), m_featureSwitchClient.get());
471     provideDeviceOrientationTo(m_page.get(), m_deviceOrientationClientProxy.get());
472     provideGeolocationTo(m_page.get(), m_geolocationClientProxy.get());
473     m_geolocationClientProxy->setController(GeolocationController::from(m_page.get()));
474
475 #if ENABLE(BATTERY_STATUS)
476     provideBatteryTo(m_page.get(), m_batteryClient.get());
477     m_batteryClient->setController(BatteryController::from(m_page.get()));
478 #endif
479
480     m_page->setGroupName(pageGroupName);
481
482 #if ENABLE(PAGE_VISIBILITY_API)
483     if (m_client)
484         setVisibilityState(m_client->visibilityState(), true);
485 #endif
486
487     m_inspectorSettingsMap = adoptPtr(new SettingsMap);
488 }
489
490 WebViewImpl::~WebViewImpl()
491 {
492     ASSERT(!m_page);
493 }
494
495 RenderTheme* WebViewImpl::theme() const
496 {
497     return m_page ? m_page->theme() : RenderTheme::defaultTheme().get();
498 }
499
500 WebFrameImpl* WebViewImpl::mainFrameImpl()
501 {
502     return m_page ? WebFrameImpl::fromFrame(m_page->mainFrame()) : 0;
503 }
504
505 bool WebViewImpl::tabKeyCyclesThroughElements() const
506 {
507     ASSERT(m_page);
508     return m_page->tabKeyCyclesThroughElements();
509 }
510
511 void WebViewImpl::setTabKeyCyclesThroughElements(bool value)
512 {
513     if (m_page)
514         m_page->setTabKeyCyclesThroughElements(value);
515 }
516
517 void WebViewImpl::handleMouseLeave(Frame& mainFrame, const WebMouseEvent& event)
518 {
519     m_client->setMouseOverURL(WebURL());
520     PageWidgetEventHandler::handleMouseLeave(mainFrame, event);
521 }
522
523 void WebViewImpl::handleMouseDown(Frame& mainFrame, const WebMouseEvent& event)
524 {
525     // If there is a popup open, close it as the user is clicking on the page (outside of the
526     // popup). We also save it so we can prevent a click on an element from immediately
527     // reopening the same popup.
528     RefPtr<WebCore::PopupContainer> selectPopup;
529 #if ENABLE(PAGE_POPUP)
530     RefPtr<WebPagePopupImpl> pagePopup;
531 #endif
532     if (event.button == WebMouseEvent::ButtonLeft) {
533         selectPopup = m_selectPopup;
534 #if ENABLE(PAGE_POPUP)
535         pagePopup = m_pagePopup;
536 #endif
537         hidePopups();
538         ASSERT(!m_selectPopup);
539 #if ENABLE(PAGE_POPUP)
540         ASSERT(!m_pagePopup);
541 #endif
542     }
543
544     m_lastMouseDownPoint = WebPoint(event.x, event.y);
545
546     if (event.button == WebMouseEvent::ButtonLeft) {
547         IntPoint point(event.x, event.y);
548         point = m_page->mainFrame()->view()->windowToContents(point);
549         HitTestResult result(m_page->mainFrame()->eventHandler()->hitTestResultAtPoint(point, false));
550         Node* hitNode = result.innerNonSharedNode();
551
552         // Take capture on a mouse down on a plugin so we can send it mouse events.
553         if (hitNode && hitNode->renderer() && hitNode->renderer()->isEmbeddedObject())
554             m_mouseCaptureNode = hitNode;
555     }
556
557     PageWidgetEventHandler::handleMouseDown(mainFrame, event);
558
559     if (m_selectPopup && m_selectPopup == selectPopup) {
560         // That click triggered a select popup which is the same as the one that
561         // was showing before the click.  It means the user clicked the select
562         // while the popup was showing, and as a result we first closed then
563         // immediately reopened the select popup.  It needs to be closed.
564         hideSelectPopup();
565     }
566
567 #if ENABLE(PAGE_POPUP)
568     if (m_pagePopup && pagePopup && m_pagePopup->hasSamePopupClient(pagePopup.get())) {
569         // That click triggered a page popup that is the same as the one we just closed.
570         // It needs to be closed.
571         closePagePopup(m_pagePopup.get());
572     }
573 #endif
574
575     // Dispatch the contextmenu event regardless of if the click was swallowed.
576     // On Windows, we handle it on mouse up, not down.
577 #if OS(DARWIN)
578     if (event.button == WebMouseEvent::ButtonRight
579         || (event.button == WebMouseEvent::ButtonLeft
580             && event.modifiers & WebMouseEvent::ControlKey))
581         mouseContextMenu(event);
582 #elif OS(UNIX) || OS(ANDROID)
583     if (event.button == WebMouseEvent::ButtonRight)
584         mouseContextMenu(event);
585 #endif
586 }
587
588 void WebViewImpl::mouseContextMenu(const WebMouseEvent& event)
589 {
590     if (!mainFrameImpl() || !mainFrameImpl()->frameView())
591         return;
592
593     m_page->contextMenuController()->clearContextMenu();
594
595     PlatformMouseEventBuilder pme(mainFrameImpl()->frameView(), event);
596
597     // Find the right target frame. See issue 1186900.
598     HitTestResult result = hitTestResultForWindowPos(pme.position());
599     Frame* targetFrame;
600     if (result.innerNonSharedNode())
601         targetFrame = result.innerNonSharedNode()->document()->frame();
602     else
603         targetFrame = m_page->focusController()->focusedOrMainFrame();
604
605 #if OS(WINDOWS)
606     targetFrame->view()->setCursor(pointerCursor());
607 #endif
608
609     m_contextMenuAllowed = true;
610     targetFrame->eventHandler()->sendContextMenuEvent(pme);
611     m_contextMenuAllowed = false;
612     // Actually showing the context menu is handled by the ContextMenuClient
613     // implementation...
614 }
615
616 void WebViewImpl::handleMouseUp(Frame& mainFrame, const WebMouseEvent& event)
617 {
618 #if OS(UNIX) && !OS(DARWIN)
619     // If the event was a middle click, attempt to copy text into the focused
620     // frame. We execute this before we let the page have a go at the event
621     // because the page may change what is focused during in its event handler.
622     //
623     // This code is in the mouse up handler. There is some debate about putting
624     // this here, as opposed to the mouse down handler.
625     //   xterm: pastes on up.
626     //   GTK: pastes on down.
627     //   Firefox: pastes on up.
628     //   Midori: couldn't paste at all with 0.1.2
629     //
630     // There is something of a webcompat angle to this well, as highlighted by
631     // crbug.com/14608. Pages can clear text boxes 'onclick' and, if we paste on
632     // down then the text is pasted just before the onclick handler runs and
633     // clears the text box. So it's important this happens after the
634     // handleMouseReleaseEvent() earlier in this function
635     if (event.button == WebMouseEvent::ButtonMiddle) {
636         Frame* focused = focusedWebCoreFrame();
637         FrameView* view = m_page->mainFrame()->view();
638         IntPoint clickPoint(m_lastMouseDownPoint.x, m_lastMouseDownPoint.y);
639         IntPoint contentPoint = view->windowToContents(clickPoint);
640         HitTestResult hitTestResult = focused->eventHandler()->hitTestResultAtPoint(contentPoint, false, false, ShouldHitTestScrollbars);
641         // We don't want to send a paste when middle clicking a scroll bar or a
642         // link (which will navigate later in the code).  The main scrollbars
643         // have to be handled separately.
644         if (!hitTestResult.scrollbar() && !hitTestResult.isLiveLink() && focused && !view->scrollbarAtPoint(clickPoint)) {
645             Editor* editor = focused->editor();
646             Pasteboard* pasteboard = Pasteboard::generalPasteboard();
647             bool oldSelectionMode = pasteboard->isSelectionMode();
648             pasteboard->setSelectionMode(true);
649             editor->command(AtomicString("Paste")).execute();
650             pasteboard->setSelectionMode(oldSelectionMode);
651         }
652     }
653 #endif
654
655     PageWidgetEventHandler::handleMouseUp(mainFrame, event);
656
657 #if OS(WINDOWS)
658     // Dispatch the contextmenu event regardless of if the click was swallowed.
659     // On Mac/Linux, we handle it on mouse down, not up.
660     if (event.button == WebMouseEvent::ButtonRight)
661         mouseContextMenu(event);
662 #endif
663 }
664
665 void WebViewImpl::scrollBy(const WebCore::IntPoint& delta)
666 {
667     WebMouseWheelEvent syntheticWheel;
668     const float tickDivisor = WebCore::WheelEvent::tickMultiplier;
669
670     syntheticWheel.deltaX = delta.x();
671     syntheticWheel.deltaY = delta.y();
672     syntheticWheel.wheelTicksX = delta.x() / tickDivisor;
673     syntheticWheel.wheelTicksY = delta.y() / tickDivisor;
674     syntheticWheel.hasPreciseScrollingDeltas = true;
675     syntheticWheel.x = m_lastWheelPosition.x;
676     syntheticWheel.y = m_lastWheelPosition.y;
677     syntheticWheel.globalX = m_lastWheelGlobalPosition.x;
678     syntheticWheel.globalY = m_lastWheelGlobalPosition.y;
679     syntheticWheel.modifiers = m_flingModifier;
680
681     if (m_page && m_page->mainFrame() && m_page->mainFrame()->view())
682         handleMouseWheel(*m_page->mainFrame(), syntheticWheel);
683 }
684
685 #if ENABLE(GESTURE_EVENTS)
686 bool WebViewImpl::handleGestureEvent(const WebGestureEvent& event)
687 {
688     switch (event.type) {
689     case WebInputEvent::GestureFlingStart: {
690         m_lastWheelPosition = WebPoint(event.x, event.y);
691         m_lastWheelGlobalPosition = WebPoint(event.globalX, event.globalY);
692         m_flingModifier = event.modifiers;
693         // FIXME: Make the curve parametrizable from the browser.
694         m_gestureAnimation = ActivePlatformGestureAnimation::create(TouchpadFlingPlatformGestureCurve::create(FloatPoint(event.deltaX, event.deltaY)), this);
695         scheduleAnimation();
696         return true;
697     }
698     case WebInputEvent::GestureFlingCancel:
699         if (m_gestureAnimation) {
700             m_gestureAnimation.clear();
701             return true;
702         }
703         return false;
704     case WebInputEvent::GestureTap: {
705         PlatformGestureEventBuilder platformEvent(mainFrameImpl()->frameView(), event);
706         RefPtr<WebCore::PopupContainer> selectPopup;
707         selectPopup = m_selectPopup;
708         hideSelectPopup();
709         ASSERT(!m_selectPopup);
710         bool gestureHandled = mainFrameImpl()->frame()->eventHandler()->handleGestureEvent(platformEvent);
711         if (m_selectPopup && m_selectPopup == selectPopup) {
712             // That tap triggered a select popup which is the same as the one that
713             // was showing before the tap. It means the user tapped the select
714             // while the popup was showing, and as a result we first closed then
715             // immediately reopened the select popup. It needs to be closed.
716             hideSelectPopup();
717         }
718         return gestureHandled;
719     }
720     case WebInputEvent::GestureTwoFingerTap:
721     case WebInputEvent::GestureLongPress: {
722         if (!mainFrameImpl() || !mainFrameImpl()->frameView())
723             return false;
724
725         m_page->contextMenuController()->clearContextMenu();
726         m_contextMenuAllowed = true;
727         PlatformGestureEventBuilder platformEvent(mainFrameImpl()->frameView(), event);
728         bool handled = mainFrameImpl()->frame()->eventHandler()->sendContextMenuEventForGesture(platformEvent);
729         m_contextMenuAllowed = false;
730         return handled;
731     }
732     case WebInputEvent::GestureScrollBegin:
733     case WebInputEvent::GestureScrollEnd:
734     case WebInputEvent::GestureScrollUpdate:
735     case WebInputEvent::GestureTapDown:
736     case WebInputEvent::GestureDoubleTap:
737     case WebInputEvent::GesturePinchBegin:
738     case WebInputEvent::GesturePinchEnd:
739     case WebInputEvent::GesturePinchUpdate: {
740         PlatformGestureEventBuilder platformEvent(mainFrameImpl()->frameView(), event);
741         return mainFrameImpl()->frame()->eventHandler()->handleGestureEvent(platformEvent);
742     }
743     default:
744         ASSERT_NOT_REACHED();
745     }
746     return false;
747 }
748
749 void WebViewImpl::transferActiveWheelFlingAnimation(const WebActiveWheelFlingParameters& parameters)
750 {
751     TRACE_EVENT0("webkit", "WebViewImpl::transferActiveWheelFlingAnimation");
752     ASSERT(!m_gestureAnimation);
753     m_lastWheelPosition = parameters.point;
754     m_lastWheelGlobalPosition = parameters.globalPoint;
755     m_flingModifier = parameters.modifiers;
756     OwnPtr<PlatformGestureCurve> curve = TouchpadFlingPlatformGestureCurve::create(parameters.delta, IntPoint(parameters.cumulativeScroll));
757     m_gestureAnimation = ActivePlatformGestureAnimation::create(curve.release(), this, parameters.startTime);
758     scheduleAnimation();
759 }
760
761 void WebViewImpl::renderingStats(WebRenderingStats& stats) const
762 {
763     if (!m_layerTreeView.isNull())
764         m_layerTreeView.renderingStats(stats);
765 }
766
767 void WebViewImpl::startPageScaleAnimation(const IntPoint& targetPosition, bool useAnchor, float newScale, double durationInSeconds)
768 {
769     if (m_layerTreeView.isNull())
770         return;
771
772     IntPoint clampedPoint = targetPosition;
773     if (!useAnchor)
774         clampedPoint = clampOffsetAtScale(targetPosition, newScale);
775
776     if (!durationInSeconds && !useAnchor) {
777         setPageScaleFactor(newScale, clampedPoint);
778         return;
779     }
780
781     m_layerTreeView.startPageScaleAnimation(targetPosition, useAnchor, newScale, durationInSeconds);
782 }
783 #endif
784
785 WebViewBenchmarkSupport* WebViewImpl::benchmarkSupport()
786 {
787     return &m_benchmarkSupport;
788 }
789
790 WebVector<WebFloatQuad> WebViewImpl::getTouchHighlightQuads(const WebPoint& point,
791                                                             int padding,
792                                                             WebTouchCandidatesInfo& outTouchInfo,
793                                                             WebColor& outTapHighlightColor)
794 {
795     // FIXME: Upstream this function from the chromium-android branch.
796     notImplemented();
797
798     return WebVector<WebFloatQuad>();
799 }
800
801 bool WebViewImpl::handleKeyEvent(const WebKeyboardEvent& event)
802 {
803     ASSERT((event.type == WebInputEvent::RawKeyDown)
804         || (event.type == WebInputEvent::KeyDown)
805         || (event.type == WebInputEvent::KeyUp));
806
807     // Halt an in-progress fling on a key event.
808     if (m_gestureAnimation)
809         m_gestureAnimation.clear();
810
811     // Please refer to the comments explaining the m_suppressNextKeypressEvent
812     // member.
813     // The m_suppressNextKeypressEvent is set if the KeyDown is handled by
814     // Webkit. A keyDown event is typically associated with a keyPress(char)
815     // event and a keyUp event. We reset this flag here as this is a new keyDown
816     // event.
817     m_suppressNextKeypressEvent = false;
818
819     // If there is a select popup, it should be the one processing the event,
820     // not the page.
821     if (m_selectPopup)
822         return m_selectPopup->handleKeyEvent(PlatformKeyboardEventBuilder(event));
823 #if ENABLE(PAGE_POPUP)
824     if (m_pagePopup) {
825         m_pagePopup->handleKeyEvent(PlatformKeyboardEventBuilder(event));
826         // We need to ignore the next Char event after this otherwise pressing
827         // enter when selecting an item in the popup will go to the page.
828         if (WebInputEvent::RawKeyDown == event.type)
829             m_suppressNextKeypressEvent = true;
830         return true;
831     }
832 #endif
833
834     // Give Autocomplete a chance to consume the key events it is interested in.
835     if (autocompleteHandleKeyEvent(event))
836         return true;
837
838     RefPtr<Frame> frame = focusedWebCoreFrame();
839     if (!frame)
840         return false;
841
842     EventHandler* handler = frame->eventHandler();
843     if (!handler)
844         return keyEventDefault(event);
845
846 #if !OS(DARWIN)
847     const WebInputEvent::Type contextMenuTriggeringEventType =
848 #if OS(WINDOWS)
849         WebInputEvent::KeyUp;
850 #elif OS(UNIX)
851         WebInputEvent::RawKeyDown;
852 #endif
853
854     bool isUnmodifiedMenuKey = !(event.modifiers & WebInputEvent::InputModifiers) && event.windowsKeyCode == VKEY_APPS;
855     bool isShiftF10 = event.modifiers == WebInputEvent::ShiftKey && event.windowsKeyCode == VKEY_F10;
856     if ((isUnmodifiedMenuKey || isShiftF10) && event.type == contextMenuTriggeringEventType) {
857         sendContextMenuEvent(event);
858         return true;
859     }
860 #endif // !OS(DARWIN)
861
862     PlatformKeyboardEventBuilder evt(event);
863
864     if (handler->keyEvent(evt)) {
865         if (WebInputEvent::RawKeyDown == event.type) {
866             // Suppress the next keypress event unless the focused node is a plug-in node.
867             // (Flash needs these keypress events to handle non-US keyboards.)
868             Node* node = focusedWebCoreNode();
869             if (!node || !node->renderer() || !node->renderer()->isEmbeddedObject())
870                 m_suppressNextKeypressEvent = true;
871         }
872         return true;
873     }
874
875     return keyEventDefault(event);
876 }
877
878 bool WebViewImpl::autocompleteHandleKeyEvent(const WebKeyboardEvent& event)
879 {
880     if (!m_autofillPopupShowing
881         // Home and End should be left to the text field to process.
882         || event.windowsKeyCode == VKEY_HOME
883         || event.windowsKeyCode == VKEY_END)
884       return false;
885
886     // Pressing delete triggers the removal of the selected suggestion from the DB.
887     if (event.windowsKeyCode == VKEY_DELETE
888         && m_autofillPopup->selectedIndex() != -1) {
889         Node* node = focusedWebCoreNode();
890         if (!node || (node->nodeType() != Node::ELEMENT_NODE)) {
891             ASSERT_NOT_REACHED();
892             return false;
893         }
894         Element* element = static_cast<Element*>(node);
895         if (!element->hasLocalName(HTMLNames::inputTag)) {
896             ASSERT_NOT_REACHED();
897             return false;
898         }
899
900         int selectedIndex = m_autofillPopup->selectedIndex();
901
902         if (!m_autofillPopupClient->canRemoveSuggestionAtIndex(selectedIndex))
903             return false;
904
905         WebString name = WebInputElement(static_cast<HTMLInputElement*>(element)).nameForAutofill();
906         WebString value = m_autofillPopupClient->itemText(selectedIndex);
907         m_autofillClient->removeAutocompleteSuggestion(name, value);
908         // Update the entries in the currently showing popup to reflect the
909         // deletion.
910         m_autofillPopupClient->removeSuggestionAtIndex(selectedIndex);
911         refreshAutofillPopup();
912         return false;
913     }
914
915     if (!m_autofillPopup->isInterestedInEventForKey(event.windowsKeyCode))
916         return false;
917
918     if (m_autofillPopup->handleKeyEvent(PlatformKeyboardEventBuilder(event))) {
919         // We need to ignore the next Char event after this otherwise pressing
920         // enter when selecting an item in the menu will go to the page.
921         if (WebInputEvent::RawKeyDown == event.type)
922             m_suppressNextKeypressEvent = true;
923         return true;
924     }
925
926     return false;
927 }
928
929 bool WebViewImpl::handleCharEvent(const WebKeyboardEvent& event)
930 {
931     ASSERT(event.type == WebInputEvent::Char);
932
933     // Please refer to the comments explaining the m_suppressNextKeypressEvent
934     // member.  The m_suppressNextKeypressEvent is set if the KeyDown is
935     // handled by Webkit. A keyDown event is typically associated with a
936     // keyPress(char) event and a keyUp event. We reset this flag here as it
937     // only applies to the current keyPress event.
938     bool suppress = m_suppressNextKeypressEvent;
939     m_suppressNextKeypressEvent = false;
940
941     // If there is a select popup, it should be the one processing the event,
942     // not the page.
943     if (m_selectPopup)
944         return m_selectPopup->handleKeyEvent(PlatformKeyboardEventBuilder(event));
945 #if ENABLE(PAGE_POPUP)
946     if (m_pagePopup)
947         return m_pagePopup->handleKeyEvent(PlatformKeyboardEventBuilder(event));
948 #endif
949
950     Frame* frame = focusedWebCoreFrame();
951     if (!frame)
952         return suppress;
953
954     EventHandler* handler = frame->eventHandler();
955     if (!handler)
956         return suppress || keyEventDefault(event);
957
958     PlatformKeyboardEventBuilder evt(event);
959     if (!evt.isCharacterKey())
960         return true;
961
962     // Accesskeys are triggered by char events and can't be suppressed.
963     if (handler->handleAccessKey(evt))
964         return true;
965
966     // Safari 3.1 does not pass off windows system key messages (WM_SYSCHAR) to
967     // the eventHandler::keyEvent. We mimic this behavior on all platforms since
968     // for now we are converting other platform's key events to windows key
969     // events.
970     if (evt.isSystemKey())
971         return false;
972
973     if (!suppress && !handler->keyEvent(evt))
974         return keyEventDefault(event);
975
976     return true;
977 }
978
979 #if ENABLE(GESTURE_EVENTS)
980 WebRect WebViewImpl::computeBlockBounds(const WebRect& rect, AutoZoomType zoomType)
981 {
982     if (!mainFrameImpl())
983         return WebRect();
984
985     // Use the rect-based hit test to find the node.
986     IntPoint point = mainFrameImpl()->frameView()->windowToContents(IntPoint(rect.x, rect.y));
987     HitTestResult result = mainFrameImpl()->frame()->eventHandler()->hitTestResultAtPoint(point,
988             false, zoomType == FindInPage, DontHitTestScrollbars, HitTestRequest::Active | HitTestRequest::ReadOnly,
989             IntSize(rect.width, rect.height));
990
991     Node* node = result.innerNonSharedNode();
992     if (!node)
993         return WebRect();
994
995     // Find the block type node based on the hit node.
996     while (node && (!node->renderer() || node->renderer()->isInline()))
997         node = node->parentNode();
998
999     // Return the bounding box in the window coordinate system.
1000     if (node) {
1001         IntRect rect = node->Node::getPixelSnappedRect();
1002         Frame* frame = node->document()->frame();
1003         return frame->view()->contentsToWindow(rect);
1004     }
1005     return WebRect();
1006 }
1007
1008 WebRect WebViewImpl::widenRectWithinPageBounds(const WebRect& source, int targetMargin, int minimumMargin)
1009 {
1010     WebSize maxSize;
1011     if (mainFrame())
1012         maxSize = mainFrame()->contentsSize();
1013     IntSize scrollOffset;
1014     if (mainFrame())
1015         scrollOffset = mainFrame()->scrollOffset();
1016     int leftMargin = targetMargin;
1017     int rightMargin = targetMargin;
1018
1019     const int absoluteSourceX = source.x + scrollOffset.width();
1020     if (leftMargin > absoluteSourceX) {
1021         leftMargin = absoluteSourceX;
1022         rightMargin = max(leftMargin, minimumMargin);
1023     }
1024
1025     const int maximumRightMargin = maxSize.width - (source.width + absoluteSourceX);
1026     if (rightMargin > maximumRightMargin) {
1027         rightMargin = maximumRightMargin;
1028         leftMargin = min(leftMargin, max(rightMargin, minimumMargin));
1029     }
1030
1031     const int newWidth = source.width + leftMargin + rightMargin;
1032     const int newX = source.x - leftMargin;
1033
1034     ASSERT(newWidth >= 0);
1035     ASSERT(scrollOffset.width() + newX + newWidth <= maxSize.width);
1036
1037     return WebRect(newX, source.y, newWidth, source.height);
1038 }
1039
1040 void WebViewImpl::computeScaleAndScrollForHitRect(const WebRect& hitRect, AutoZoomType zoomType, float& scale, WebPoint& scroll)
1041 {
1042     scale = pageScaleFactor();
1043     scroll.x = scroll.y = 0;
1044     WebRect targetRect = hitRect;
1045     if (targetRect.isEmpty())
1046         targetRect.width = targetRect.height = touchPointPadding;
1047
1048     WebRect rect = computeBlockBounds(targetRect, zoomType);
1049
1050     const float overviewScale = m_minimumPageScaleFactor;
1051     bool scaleUnchanged = true;
1052     if (!rect.isEmpty()) {
1053         // Pages should be as legible as on desktop when at dpi scale, so no
1054         // need to zoom in further when automatically determining zoom level
1055         // (after double tap, find in page, etc), though the user should still
1056         // be allowed to manually pinch zoom in further if they desire.
1057         const float maxScale = deviceScaleFactor();
1058
1059         const float defaultMargin = doubleTapZoomContentDefaultMargin * deviceScaleFactor();
1060         const float minimumMargin = doubleTapZoomContentMinimumMargin * deviceScaleFactor();
1061         // We want the margins to have the same physical size, which means we
1062         // need to express them in post-scale size. To do that we'd need to know
1063         // the scale we're scaling to, but that depends on the margins. Instead
1064         // we express them as a fraction of the target rectangle: this will be
1065         // correct if we end up fully zooming to it, and won't matter if we
1066         // don't.
1067         rect = widenRectWithinPageBounds(rect,
1068                 static_cast<int>(defaultMargin * rect.width / m_size.width),
1069                 static_cast<int>(minimumMargin * rect.width / m_size.width));
1070
1071         // Fit block to screen, respecting limits.
1072         scale *= static_cast<float>(m_size.width) / rect.width;
1073         scale = min(scale, maxScale);
1074         scale = clampPageScaleFactorToLimits(scale);
1075
1076         scaleUnchanged = fabs(pageScaleFactor() - scale) < minScaleDifference;
1077     }
1078
1079     if (zoomType == DoubleTap) {
1080         if (rect.isEmpty() || scaleUnchanged) {
1081             // Zoom out to overview mode.
1082             if (overviewScale)
1083                 scale = overviewScale;
1084             return;
1085         }
1086     } else if (rect.isEmpty()) {
1087         // Keep current scale (no need to scroll as x,y will normally already
1088         // be visible). FIXME: Revisit this if it isn't always true.
1089         return;
1090     }
1091
1092     // FIXME: If this is being called for auto zoom during find in page,
1093     // then if the user manually zooms in it'd be nice to preserve the relative
1094     // increase in zoom they caused (if they zoom out then it's ok to zoom
1095     // them back in again). This isn't compatible with our current double-tap
1096     // zoom strategy (fitting the containing block to the screen) though.
1097
1098     float screenHeight = m_size.height / scale * pageScaleFactor();
1099     float screenWidth = m_size.width / scale * pageScaleFactor();
1100
1101     // Scroll to vertically align the block.
1102     if (rect.height < screenHeight) {
1103         // Vertically center short blocks.
1104         rect.y -= 0.5 * (screenHeight - rect.height);
1105     } else {
1106         // Ensure position we're zooming to (+ padding) isn't off the bottom of
1107         // the screen.
1108         rect.y = max<float>(rect.y, hitRect.y + touchPointPadding - screenHeight);
1109     } // Otherwise top align the block.
1110
1111     // Do the same thing for horizontal alignment.
1112     if (rect.width < screenWidth)
1113         rect.x -= 0.5 * (screenWidth - rect.width);
1114     else
1115         rect.x = max<float>(rect.x, hitRect.x + touchPointPadding - screenWidth);
1116
1117     scroll.x = rect.x;
1118     scroll.y = rect.y;
1119 }
1120 #endif
1121
1122 void WebViewImpl::animateZoomAroundPoint(const IntPoint& point, AutoZoomType zoomType)
1123 {
1124 #if ENABLE(GESTURE_EVENTS)
1125     if (!mainFrameImpl())
1126         return;
1127
1128     float scale;
1129     WebPoint scroll;
1130     computeScaleAndScrollForHitRect(WebRect(point.x(), point.y(), 0, 0), zoomType, scale, scroll);
1131
1132     bool isDoubleTap = (zoomType == DoubleTap);
1133     double durationInSeconds = isDoubleTap ? doubleTapZoomAnimationDurationInSeconds : 0;
1134     startPageScaleAnimation(scroll, isDoubleTap, scale, durationInSeconds);
1135 #endif
1136 }
1137
1138 void WebViewImpl::zoomToFindInPageRect(const WebRect& rect)
1139 {
1140     animateZoomAroundPoint(IntRect(rect).center(), FindInPage);
1141 }
1142
1143 void WebViewImpl::numberOfWheelEventHandlersChanged(unsigned numberOfWheelHandlers)
1144 {
1145     if (m_client)
1146         m_client->numberOfWheelEventHandlersChanged(numberOfWheelHandlers);
1147 }
1148
1149 void WebViewImpl::hasTouchEventHandlers(bool hasTouchHandlers)
1150 {
1151     if (m_client)
1152         m_client->hasTouchEventHandlers(hasTouchHandlers);
1153 }
1154
1155 #if !OS(DARWIN)
1156 // Mac has no way to open a context menu based on a keyboard event.
1157 bool WebViewImpl::sendContextMenuEvent(const WebKeyboardEvent& event)
1158 {
1159     // The contextMenuController() holds onto the last context menu that was
1160     // popped up on the page until a new one is created. We need to clear
1161     // this menu before propagating the event through the DOM so that we can
1162     // detect if we create a new menu for this event, since we won't create
1163     // a new menu if the DOM swallows the event and the defaultEventHandler does
1164     // not run.
1165     page()->contextMenuController()->clearContextMenu();
1166
1167     m_contextMenuAllowed = true;
1168     Frame* focusedFrame = page()->focusController()->focusedOrMainFrame();
1169     bool handled = focusedFrame->eventHandler()->sendContextMenuEventForKey();
1170     m_contextMenuAllowed = false;
1171     return handled;
1172 }
1173 #endif
1174
1175 bool WebViewImpl::keyEventDefault(const WebKeyboardEvent& event)
1176 {
1177     Frame* frame = focusedWebCoreFrame();
1178     if (!frame)
1179         return false;
1180
1181     switch (event.type) {
1182     case WebInputEvent::Char:
1183         if (event.windowsKeyCode == VKEY_SPACE) {
1184             int keyCode = ((event.modifiers & WebInputEvent::ShiftKey) ? VKEY_PRIOR : VKEY_NEXT);
1185             return scrollViewWithKeyboard(keyCode, event.modifiers);
1186         }
1187         break;
1188     case WebInputEvent::RawKeyDown:
1189         if (event.modifiers == WebInputEvent::ControlKey) {
1190             switch (event.windowsKeyCode) {
1191 #if !OS(DARWIN)
1192             case 'A':
1193                 focusedFrame()->executeCommand(WebString::fromUTF8("SelectAll"));
1194                 return true;
1195             case VKEY_INSERT:
1196             case 'C':
1197                 focusedFrame()->executeCommand(WebString::fromUTF8("Copy"));
1198                 return true;
1199 #endif
1200             // Match FF behavior in the sense that Ctrl+home/end are the only Ctrl
1201             // key combinations which affect scrolling. Safari is buggy in the
1202             // sense that it scrolls the page for all Ctrl+scrolling key
1203             // combinations. For e.g. Ctrl+pgup/pgdn/up/down, etc.
1204             case VKEY_HOME:
1205             case VKEY_END:
1206                 break;
1207             default:
1208                 return false;
1209             }
1210         }
1211         if (!event.isSystemKey && !(event.modifiers & WebInputEvent::ShiftKey))
1212             return scrollViewWithKeyboard(event.windowsKeyCode, event.modifiers);
1213         break;
1214     default:
1215         break;
1216     }
1217     return false;
1218 }
1219
1220 bool WebViewImpl::scrollViewWithKeyboard(int keyCode, int modifiers)
1221 {
1222     ScrollDirection scrollDirection;
1223     ScrollGranularity scrollGranularity;
1224 #if OS(DARWIN)
1225     // Control-Up/Down should be PageUp/Down on Mac.
1226     if (modifiers & WebMouseEvent::ControlKey) {
1227       if (keyCode == VKEY_UP)
1228         keyCode = VKEY_PRIOR;
1229       else if (keyCode == VKEY_DOWN)
1230         keyCode = VKEY_NEXT;
1231     }
1232 #endif
1233     if (!mapKeyCodeForScroll(keyCode, &scrollDirection, &scrollGranularity))
1234         return false;
1235     return propagateScroll(scrollDirection, scrollGranularity);
1236 }
1237
1238 bool WebViewImpl::mapKeyCodeForScroll(int keyCode,
1239                                       WebCore::ScrollDirection* scrollDirection,
1240                                       WebCore::ScrollGranularity* scrollGranularity)
1241 {
1242     switch (keyCode) {
1243     case VKEY_LEFT:
1244         *scrollDirection = ScrollLeft;
1245         *scrollGranularity = ScrollByLine;
1246         break;
1247     case VKEY_RIGHT:
1248         *scrollDirection = ScrollRight;
1249         *scrollGranularity = ScrollByLine;
1250         break;
1251     case VKEY_UP:
1252         *scrollDirection = ScrollUp;
1253         *scrollGranularity = ScrollByLine;
1254         break;
1255     case VKEY_DOWN:
1256         *scrollDirection = ScrollDown;
1257         *scrollGranularity = ScrollByLine;
1258         break;
1259     case VKEY_HOME:
1260         *scrollDirection = ScrollUp;
1261         *scrollGranularity = ScrollByDocument;
1262         break;
1263     case VKEY_END:
1264         *scrollDirection = ScrollDown;
1265         *scrollGranularity = ScrollByDocument;
1266         break;
1267     case VKEY_PRIOR:  // page up
1268         *scrollDirection = ScrollUp;
1269         *scrollGranularity = ScrollByPage;
1270         break;
1271     case VKEY_NEXT:  // page down
1272         *scrollDirection = ScrollDown;
1273         *scrollGranularity = ScrollByPage;
1274         break;
1275     default:
1276         return false;
1277     }
1278
1279     return true;
1280 }
1281
1282 void WebViewImpl::hideSelectPopup()
1283 {
1284     if (m_selectPopup)
1285         m_selectPopup->hidePopup();
1286 }
1287
1288 bool WebViewImpl::propagateScroll(ScrollDirection scrollDirection,
1289                                   ScrollGranularity scrollGranularity)
1290 {
1291     Frame* frame = focusedWebCoreFrame();
1292     if (!frame)
1293         return false;
1294
1295     bool scrollHandled = frame->eventHandler()->scrollOverflow(scrollDirection, scrollGranularity);
1296     Frame* currentFrame = frame;
1297     while (!scrollHandled && currentFrame) {
1298         scrollHandled = currentFrame->view()->scroll(scrollDirection, scrollGranularity);
1299         currentFrame = currentFrame->tree()->parent();
1300     }
1301     return scrollHandled;
1302 }
1303
1304 void  WebViewImpl::popupOpened(WebCore::PopupContainer* popupContainer)
1305 {
1306     if (popupContainer->popupType() == WebCore::PopupContainer::Select) {
1307         ASSERT(!m_selectPopup);
1308         m_selectPopup = popupContainer;
1309     }
1310 }
1311
1312 void  WebViewImpl::popupClosed(WebCore::PopupContainer* popupContainer)
1313 {
1314     if (popupContainer->popupType() == WebCore::PopupContainer::Select) {
1315         ASSERT(m_selectPopup);
1316         m_selectPopup = 0;
1317     }
1318 }
1319
1320 #if ENABLE(PAGE_POPUP)
1321 PagePopup* WebViewImpl::openPagePopup(PagePopupClient* client, const IntRect& originBoundsInRootView)
1322 {
1323     ASSERT(client);
1324     if (hasOpenedPopup())
1325         hidePopups();
1326     ASSERT(!m_pagePopup);
1327
1328     WebWidget* popupWidget = m_client->createPopupMenu(WebPopupTypePage);
1329     ASSERT(popupWidget);
1330     m_pagePopup = static_cast<WebPagePopupImpl*>(popupWidget);
1331     if (!m_pagePopup->init(this, client, originBoundsInRootView)) {
1332         m_pagePopup->closePopup();
1333         m_pagePopup = 0;
1334     }
1335
1336     if (Frame* frame = focusedWebCoreFrame())
1337         frame->selection()->setCaretVisible(false);
1338     return m_pagePopup.get();
1339 }
1340
1341 void WebViewImpl::closePagePopup(PagePopup* popup)
1342 {
1343     ASSERT(popup);
1344     WebPagePopupImpl* popupImpl = static_cast<WebPagePopupImpl*>(popup);
1345     ASSERT(m_pagePopup.get() == popupImpl);
1346     if (m_pagePopup.get() != popupImpl)
1347         return;
1348     m_pagePopup->closePopup();
1349     m_pagePopup = 0;
1350
1351     if (Frame* frame = focusedWebCoreFrame())
1352         frame->selection()->pageActivationChanged();
1353 }
1354 #endif
1355
1356 void WebViewImpl::hideAutofillPopup()
1357 {
1358     if (m_autofillPopupShowing) {
1359         m_autofillPopup->hidePopup();
1360         m_autofillPopupShowing = false;
1361     }
1362 }
1363
1364 WebHelperPluginImpl* WebViewImpl::createHelperPlugin(const String& pluginType)
1365 {
1366     WebWidget* popupWidget = m_client->createPopupMenu(WebPopupTypeHelperPlugin);
1367     ASSERT(popupWidget);
1368     WebHelperPluginImpl* helperPlugin = static_cast<WebHelperPluginImpl*>(popupWidget);
1369
1370     if (!helperPlugin->init(this, pluginType)) {
1371         helperPlugin->closeHelperPlugin();
1372         helperPlugin = 0;
1373     }
1374     return helperPlugin;
1375 }
1376
1377 Frame* WebViewImpl::focusedWebCoreFrame() const
1378 {
1379     return m_page ? m_page->focusController()->focusedOrMainFrame() : 0;
1380 }
1381
1382 WebViewImpl* WebViewImpl::fromPage(Page* page)
1383 {
1384     if (!page)
1385         return 0;
1386
1387     ChromeClientImpl* chromeClient = static_cast<ChromeClientImpl*>(page->chrome()->client());
1388     return static_cast<WebViewImpl*>(chromeClient->webView());
1389 }
1390
1391 PageGroup* WebViewImpl::defaultPageGroup()
1392 {
1393     return PageGroup::pageGroup(pageGroupName);
1394 }
1395
1396 // WebWidget ------------------------------------------------------------------
1397
1398 void WebViewImpl::close()
1399 {
1400     RefPtr<WebFrameImpl> mainFrameImpl;
1401
1402     if (m_page) {
1403         // Initiate shutdown for the entire frameset.  This will cause a lot of
1404         // notifications to be sent.
1405         if (m_page->mainFrame()) {
1406             mainFrameImpl = WebFrameImpl::fromFrame(m_page->mainFrame());
1407             m_page->mainFrame()->loader()->frameDetached();
1408         }
1409         m_page.clear();
1410     }
1411
1412     // Should happen after m_page.clear().
1413     if (m_devToolsAgent)
1414         m_devToolsAgent.clear();
1415
1416     // Reset the delegate to prevent notifications being sent as we're being
1417     // deleted.
1418     m_client = 0;
1419
1420     deref();  // Balances ref() acquired in WebView::create
1421 }
1422
1423 void WebViewImpl::willStartLiveResize()
1424 {
1425     if (mainFrameImpl() && mainFrameImpl()->frameView())
1426         mainFrameImpl()->frameView()->willStartLiveResize();
1427
1428     Frame* frame = mainFrameImpl()->frame();
1429     WebPluginContainerImpl* pluginContainer = WebFrameImpl::pluginContainerFromFrame(frame);
1430     if (pluginContainer)
1431         pluginContainer->willStartLiveResize();
1432 }
1433
1434 void WebViewImpl::resize(const WebSize& newSize)
1435 {
1436     if (m_shouldAutoResize || m_size == newSize)
1437         return;
1438
1439     FrameView* view = mainFrameImpl()->frameView();
1440     if (!view)
1441         return;
1442
1443     WebSize oldSize = m_size;
1444     float oldPageScaleFactor = pageScaleFactor();
1445     IntSize oldScrollOffset = view->scrollOffset();
1446     int oldFixedLayoutWidth = fixedLayoutSize().width;
1447
1448     m_size = newSize;
1449
1450 #if ENABLE(VIEWPORT)
1451     if (settings()->viewportEnabled()) {
1452         // Fallback width is used to layout sites designed for desktop. The
1453         // conventional size used by all mobile browsers is 980. When a mobile
1454         // device has a particularly wide screen (such as a 10" tablet held in
1455         // landscape), it can be larger.
1456         const int standardFallbackWidth = 980;
1457         int dpiIndependentViewportWidth = newSize.width / page()->deviceScaleFactor();
1458         settings()->setLayoutFallbackWidth(std::max(standardFallbackWidth, dpiIndependentViewportWidth));
1459
1460         ViewportArguments viewportArguments = mainFrameImpl()->frame()->document()->viewportArguments();
1461         m_page->chrome()->client()->dispatchViewportPropertiesDidChange(viewportArguments);
1462     }
1463 #endif
1464
1465     WebDevToolsAgentPrivate* agentPrivate = devToolsAgentPrivate();
1466     if (agentPrivate && agentPrivate->metricsOverridden())
1467         agentPrivate->webViewResized();
1468     else {
1469         WebFrameImpl* webFrame = mainFrameImpl();
1470         if (webFrame->frameView())
1471             webFrame->frameView()->resize(newSize.width, newSize.height);
1472     }
1473
1474 #if ENABLE(VIEWPORT)
1475     if (settings()->viewportEnabled()) {
1476         // Relayout immediately to obtain the new content width, which is needed
1477         // to calculate the minimum scale limit.
1478         view->layout();
1479         computePageScaleFactorLimits();
1480         // When the device rotates:
1481         // - If the page width is unchanged, then zoom by new width/old width
1482         //   such as to keep the same content horizontally onscreen.
1483         // - If the page width stretches proportionally to the change in
1484         //   screen width, then don't zoom at all (assuming the content has
1485         //   scaled uniformly, then the same content will be horizontally
1486         //   onscreen).
1487         //   - If the page width partially stretches, then zoom partially to
1488         //   make up the difference.
1489         // In all cases try to keep the same content at the top of the screen.
1490         float viewportWidthRatio = !oldSize.width ? 1 : newSize.width / (float) oldSize.width;
1491         float fixedLayoutWidthRatio = !oldFixedLayoutWidth ? 1 : fixedLayoutSize().width / (float) oldFixedLayoutWidth;
1492         float scaleMultiplier = viewportWidthRatio / fixedLayoutWidthRatio;
1493         if (scaleMultiplier != 1) {
1494             IntSize scrollOffsetAtNewScale = oldScrollOffset;
1495             scrollOffsetAtNewScale.scale(scaleMultiplier);
1496             setPageScaleFactor(oldPageScaleFactor * scaleMultiplier, IntPoint(scrollOffsetAtNewScale));
1497         }
1498     }
1499 #endif
1500
1501     sendResizeEventAndRepaint();
1502 }
1503
1504 void WebViewImpl::willEndLiveResize()
1505 {
1506     if (mainFrameImpl() && mainFrameImpl()->frameView())
1507         mainFrameImpl()->frameView()->willEndLiveResize();
1508
1509     Frame* frame = mainFrameImpl()->frame();
1510     WebPluginContainerImpl* pluginContainer = WebFrameImpl::pluginContainerFromFrame(frame);
1511     if (pluginContainer)
1512         pluginContainer->willEndLiveResize();
1513 }
1514
1515 void WebViewImpl::willEnterFullScreen()
1516 {
1517 #if ENABLE(FULLSCREEN_API)
1518     if (!m_provisionalFullScreenElement)
1519         return;
1520
1521     // Ensure that this element's document is still attached.
1522     Document* doc = m_provisionalFullScreenElement->document();
1523     if (doc->frame()) {
1524         doc->webkitWillEnterFullScreenForElement(m_provisionalFullScreenElement.get());
1525         m_fullScreenFrame = doc->frame();
1526     }
1527     m_provisionalFullScreenElement.clear();
1528 #endif
1529 }
1530
1531 void WebViewImpl::didEnterFullScreen()
1532 {
1533 #if ENABLE(FULLSCREEN_API)
1534     if (!m_fullScreenFrame)
1535         return;
1536
1537     if (Document* doc = m_fullScreenFrame->document()) {
1538         if (doc->webkitIsFullScreen())
1539             doc->webkitDidEnterFullScreenForElement(0);
1540     }
1541 #endif
1542 }
1543
1544 void WebViewImpl::willExitFullScreen()
1545 {
1546 #if ENABLE(FULLSCREEN_API)
1547     if (!m_fullScreenFrame)
1548         return;
1549
1550     if (Document* doc = m_fullScreenFrame->document()) {
1551         if (doc->webkitIsFullScreen()) {
1552             // When the client exits from full screen we have to call webkitCancelFullScreen to
1553             // notify the document. While doing that, suppress notifications back to the client.
1554             m_isCancelingFullScreen = true;
1555             doc->webkitCancelFullScreen();
1556             m_isCancelingFullScreen = false;
1557             doc->webkitWillExitFullScreenForElement(0);
1558         }
1559     }
1560 #endif
1561 }
1562
1563 void WebViewImpl::didExitFullScreen()
1564 {
1565 #if ENABLE(FULLSCREEN_API)
1566     if (!m_fullScreenFrame)
1567         return;
1568
1569     if (Document* doc = m_fullScreenFrame->document()) {
1570         if (doc->webkitIsFullScreen())
1571             doc->webkitDidExitFullScreenForElement(0);
1572     }
1573
1574     m_fullScreenFrame.clear();
1575 #endif
1576 }
1577
1578 void WebViewImpl::instrumentBeginFrame()
1579 {
1580     InspectorInstrumentation::didBeginFrame(m_page.get());
1581 }
1582
1583 void WebViewImpl::instrumentCancelFrame()
1584 {
1585     InspectorInstrumentation::didCancelFrame(m_page.get());
1586 }
1587
1588 #if ENABLE(BATTERY_STATUS)
1589 void WebViewImpl::updateBatteryStatus(const WebBatteryStatus& status)
1590 {
1591     m_batteryClient->updateBatteryStatus(status);
1592 }
1593 #endif
1594
1595 void WebViewImpl::setCompositorSurfaceReady()
1596 {
1597     m_compositorSurfaceReady = true;
1598     if (!m_layerTreeView.isNull())
1599         m_layerTreeView.setSurfaceReady();
1600 }
1601
1602 void WebViewImpl::animate(double)
1603 {
1604 #if ENABLE(REQUEST_ANIMATION_FRAME)
1605     double monotonicFrameBeginTime = monotonicallyIncreasingTime();
1606
1607 #if USE(ACCELERATED_COMPOSITING)
1608     // In composited mode, we always go through the compositor so it can apply
1609     // appropriate flow-control mechanisms.
1610     if (isAcceleratedCompositingActive())
1611         m_layerTreeView.updateAnimations(monotonicFrameBeginTime);
1612     else
1613 #endif
1614         updateAnimations(monotonicFrameBeginTime);
1615 #endif
1616 }
1617
1618 void WebViewImpl::willBeginFrame()
1619 {
1620     instrumentBeginFrame();
1621     m_client->willBeginCompositorFrame();
1622 }
1623
1624 void WebViewImpl::didBeginFrame()
1625 {
1626     InspectorInstrumentation::didComposite(m_page.get());
1627 }
1628
1629 void WebViewImpl::updateAnimations(double monotonicFrameBeginTime)
1630 {
1631 #if ENABLE(REQUEST_ANIMATION_FRAME)
1632     TRACE_EVENT0("webkit", "WebViewImpl::updateAnimations");
1633
1634     WebFrameImpl* webframe = mainFrameImpl();
1635     if (!webframe)
1636         return;
1637     FrameView* view = webframe->frameView();
1638     if (!view)
1639         return;
1640
1641     // Create synthetic wheel events as necessary for fling.
1642     if (m_gestureAnimation) {
1643         if (m_gestureAnimation->animate(monotonicFrameBeginTime))
1644             scheduleAnimation();
1645         else
1646             m_gestureAnimation.clear();
1647     }
1648
1649     PageWidgetDelegate::animate(m_page.get(), monotonicFrameBeginTime);
1650 #endif
1651 }
1652
1653 void WebViewImpl::layout()
1654 {
1655     TRACE_EVENT0("webkit", "WebViewImpl::layout");
1656     PageWidgetDelegate::layout(m_page.get());
1657 }
1658
1659 #if USE(ACCELERATED_COMPOSITING)
1660 void WebViewImpl::doPixelReadbackToCanvas(WebCanvas* canvas, const IntRect& rect)
1661 {
1662     ASSERT(!m_layerTreeView.isNull());
1663
1664     PlatformContextSkia context(canvas);
1665
1666     // PlatformGraphicsContext is actually a pointer to PlatformContextSkia
1667     GraphicsContext gc(reinterpret_cast<PlatformGraphicsContext*>(&context));
1668     int bitmapHeight = canvas->getDevice()->accessBitmap(false).height();
1669
1670     // Compute rect to sample from inverted GPU buffer.
1671     IntRect invertRect(rect.x(), bitmapHeight - rect.maxY(), rect.width(), rect.height());
1672
1673     OwnPtr<ImageBuffer> imageBuffer(ImageBuffer::create(rect.size()));
1674     RefPtr<Uint8ClampedArray> pixelArray(Uint8ClampedArray::createUninitialized(rect.width() * rect.height() * 4));
1675     if (imageBuffer && pixelArray) {
1676         m_layerTreeView.compositeAndReadback(pixelArray->data(), invertRect);
1677         imageBuffer->putByteArray(Premultiplied, pixelArray.get(), rect.size(), IntRect(IntPoint(), rect.size()), IntPoint());
1678         gc.save();
1679         gc.translate(IntSize(0, bitmapHeight));
1680         gc.scale(FloatSize(1.0f, -1.0f));
1681         // Use invertRect in next line, so that transform above inverts it back to
1682         // desired destination rect.
1683         gc.drawImageBuffer(imageBuffer.get(), ColorSpaceDeviceRGB, invertRect.location());
1684         gc.restore();
1685     }
1686 }
1687 #endif
1688
1689 void WebViewImpl::paint(WebCanvas* canvas, const WebRect& rect)
1690 {
1691     if (isAcceleratedCompositingActive()) {
1692 #if USE(ACCELERATED_COMPOSITING)
1693         // If a canvas was passed in, we use it to grab a copy of the
1694         // freshly-rendered pixels.
1695         if (canvas) {
1696             // Clip rect to the confines of the rootLayerTexture.
1697             IntRect resizeRect(rect);
1698             resizeRect.intersect(IntRect(IntPoint(0, 0), m_layerTreeView.deviceViewportSize()));
1699             doPixelReadbackToCanvas(canvas, resizeRect);
1700         }
1701 #endif
1702     } else {
1703         double paintStart = currentTime();
1704         PageWidgetDelegate::paint(m_page.get(), pageOverlays(), canvas, rect, isTransparent() ? PageWidgetDelegate::Translucent : PageWidgetDelegate::Opaque);
1705         double paintEnd = currentTime();
1706         double pixelsPerSec = (rect.width * rect.height) / (paintEnd - paintStart);
1707         WebKit::Platform::current()->histogramCustomCounts("Renderer4.SoftwarePaintDurationMS", (paintEnd - paintStart) * 1000, 0, 120, 30);
1708         WebKit::Platform::current()->histogramCustomCounts("Renderer4.SoftwarePaintMegapixPerSecond", pixelsPerSec / 1000000, 10, 210, 30);
1709     }
1710 }
1711
1712 void WebViewImpl::themeChanged()
1713 {
1714     if (!page())
1715         return;
1716     FrameView* view = page()->mainFrame()->view();
1717
1718     WebRect damagedRect(0, 0, m_size.width, m_size.height);
1719     view->invalidateRect(damagedRect);
1720 }
1721
1722 void WebViewImpl::composite(bool)
1723 {
1724 #if USE(ACCELERATED_COMPOSITING)
1725     if (WebCompositor::threadingEnabled())
1726         m_layerTreeView.setNeedsRedraw();
1727     else {
1728         ASSERT(isAcceleratedCompositingActive());
1729         if (!page())
1730             return;
1731
1732         if (m_pageOverlays)
1733             m_pageOverlays->update();
1734
1735         m_layerTreeView.composite();
1736     }
1737 #endif
1738 }
1739
1740 void WebViewImpl::setNeedsRedraw()
1741 {
1742 #if USE(ACCELERATED_COMPOSITING)
1743     if (!m_layerTreeView.isNull() && isAcceleratedCompositingActive())
1744         m_layerTreeView.setNeedsRedraw();
1745 #endif
1746 }
1747
1748 bool WebViewImpl::isInputThrottled() const
1749 {
1750 #if USE(ACCELERATED_COMPOSITING)
1751     if (!m_layerTreeView.isNull() && isAcceleratedCompositingActive())
1752         return m_layerTreeView.commitRequested();
1753 #endif
1754     return false;
1755 }
1756
1757 void WebViewImpl::loseCompositorContext(int numTimes)
1758 {
1759 #if USE(ACCELERATED_COMPOSITING)
1760     if (!m_layerTreeView.isNull())
1761         m_layerTreeView.loseCompositorContext(numTimes);
1762 #endif
1763 }
1764
1765 void WebViewImpl::enterFullScreenForElement(WebCore::Element* element)
1766 {
1767     // We are already transitioning to fullscreen for a different element.
1768     if (m_provisionalFullScreenElement) {
1769         m_provisionalFullScreenElement = element;
1770         return;
1771     }
1772
1773     // We are already in fullscreen mode.
1774     if (m_fullScreenFrame) {
1775         m_provisionalFullScreenElement = element;
1776         willEnterFullScreen();
1777         didEnterFullScreen();
1778         return;
1779     }
1780
1781 #if USE(NATIVE_FULLSCREEN_VIDEO)
1782     if (element && element->isMediaElement()) {
1783         HTMLMediaElement* mediaElement = static_cast<HTMLMediaElement*>(element);
1784         if (mediaElement->player() && mediaElement->player()->canEnterFullscreen()) {
1785             mediaElement->player()->enterFullscreen();
1786             m_provisionalFullScreenElement = element;
1787         }
1788         return;
1789     }
1790 #endif
1791
1792     // We need to transition to fullscreen mode.
1793     if (m_client && m_client->enterFullScreen())
1794         m_provisionalFullScreenElement = element;
1795 }
1796
1797 void WebViewImpl::exitFullScreenForElement(WebCore::Element* element)
1798 {
1799     // The client is exiting full screen, so don't send a notification.
1800     if (m_isCancelingFullScreen)
1801         return;
1802 #if USE(NATIVE_FULLSCREEN_VIDEO)
1803     if (element && element->isMediaElement()) {
1804         HTMLMediaElement* mediaElement = static_cast<HTMLMediaElement*>(element);
1805         if (mediaElement->player())
1806             mediaElement->player()->exitFullscreen();
1807         return;
1808     }
1809 #endif
1810     if (m_client)
1811         m_client->exitFullScreen();
1812 }
1813
1814 bool WebViewImpl::hasHorizontalScrollbar()
1815 {
1816     return mainFrameImpl()->frameView()->horizontalScrollbar();
1817 }
1818
1819 bool WebViewImpl::hasVerticalScrollbar()
1820 {
1821     return mainFrameImpl()->frameView()->verticalScrollbar();
1822 }
1823
1824 const WebInputEvent* WebViewImpl::m_currentInputEvent = 0;
1825
1826 bool WebViewImpl::handleInputEvent(const WebInputEvent& inputEvent)
1827 {
1828     UserGestureIndicator gestureIndicator(WebInputEvent::isUserGestureEventType(inputEvent.type) ? DefinitelyProcessingUserGesture : PossiblyProcessingUserGesture);
1829
1830     // If we've started a drag and drop operation, ignore input events until
1831     // we're done.
1832     if (m_doingDragAndDrop)
1833         return true;
1834
1835     // Report the event to be NOT processed by WebKit, so that the browser can handle it appropriately.
1836     if (m_ignoreInputEvents)
1837         return false;
1838
1839     TemporaryChange<const WebInputEvent*> currentEventChange(m_currentInputEvent, &inputEvent);
1840
1841 #if ENABLE(POINTER_LOCK)
1842     if (isPointerLocked() && WebInputEvent::isMouseEventType(inputEvent.type)) {
1843       pointerLockMouseEvent(inputEvent);
1844       return true;
1845     }
1846 #endif
1847
1848     if (m_mouseCaptureNode && WebInputEvent::isMouseEventType(inputEvent.type)) {
1849         // Save m_mouseCaptureNode since mouseCaptureLost() will clear it.
1850         RefPtr<Node> node = m_mouseCaptureNode;
1851
1852         // Not all platforms call mouseCaptureLost() directly.
1853         if (inputEvent.type == WebInputEvent::MouseUp)
1854             mouseCaptureLost();
1855
1856         AtomicString eventType;
1857         switch (inputEvent.type) {
1858         case WebInputEvent::MouseMove:
1859             eventType = eventNames().mousemoveEvent;
1860             break;
1861         case WebInputEvent::MouseLeave:
1862             eventType = eventNames().mouseoutEvent;
1863             break;
1864         case WebInputEvent::MouseDown:
1865             eventType = eventNames().mousedownEvent;
1866             break;
1867         case WebInputEvent::MouseUp:
1868             eventType = eventNames().mouseupEvent;
1869             break;
1870         default:
1871             ASSERT_NOT_REACHED();
1872         }
1873
1874         node->dispatchMouseEvent(
1875               PlatformMouseEventBuilder(mainFrameImpl()->frameView(), *static_cast<const WebMouseEvent*>(&inputEvent)),
1876               eventType, static_cast<const WebMouseEvent*>(&inputEvent)->clickCount);
1877         return true;
1878     }
1879
1880     bool handled = PageWidgetDelegate::handleInputEvent(m_page.get(), *this, inputEvent);
1881     return handled;
1882 }
1883
1884 void WebViewImpl::mouseCaptureLost()
1885 {
1886     m_mouseCaptureNode = 0;
1887 }
1888
1889 void WebViewImpl::setFocus(bool enable)
1890 {
1891     m_page->focusController()->setFocused(enable);
1892     if (enable) {
1893         m_page->focusController()->setActive(true);
1894         RefPtr<Frame> focusedFrame = m_page->focusController()->focusedFrame();
1895         if (focusedFrame) {
1896             Node* focusedNode = focusedFrame->document()->focusedNode();
1897             if (focusedNode && focusedNode->isElementNode()
1898                 && focusedFrame->selection()->selection().isNone()) {
1899                 // If the selection was cleared while the WebView was not
1900                 // focused, then the focus element shows with a focus ring but
1901                 // no caret and does respond to keyboard inputs.
1902                 Element* element = static_cast<Element*>(focusedNode);
1903                 if (element->isTextFormControl())
1904                     element->updateFocusAppearance(true);
1905                 else if (focusedNode->isContentEditable()) {
1906                     // updateFocusAppearance() selects all the text of
1907                     // contentseditable DIVs. So we set the selection explicitly
1908                     // instead. Note that this has the side effect of moving the
1909                     // caret back to the beginning of the text.
1910                     Position position(focusedNode, 0,
1911                                       Position::PositionIsOffsetInAnchor);
1912                     focusedFrame->selection()->setSelection(
1913                         VisibleSelection(position, SEL_DEFAULT_AFFINITY));
1914                 }
1915             }
1916         }
1917         m_imeAcceptEvents = true;
1918     } else {
1919         hidePopups();
1920
1921         // Clear focus on the currently focused frame if any.
1922         if (!m_page)
1923             return;
1924
1925         Frame* frame = m_page->mainFrame();
1926         if (!frame)
1927             return;
1928
1929         RefPtr<Frame> focusedFrame = m_page->focusController()->focusedFrame();
1930         if (focusedFrame) {
1931             // Finish an ongoing composition to delete the composition node.
1932             Editor* editor = focusedFrame->editor();
1933             if (editor && editor->hasComposition())
1934                 editor->confirmComposition();
1935             m_imeAcceptEvents = false;
1936         }
1937     }
1938 }
1939
1940 bool WebViewImpl::setComposition(
1941     const WebString& text,
1942     const WebVector<WebCompositionUnderline>& underlines,
1943     int selectionStart,
1944     int selectionEnd)
1945 {
1946     Frame* focused = focusedWebCoreFrame();
1947     if (!focused || !m_imeAcceptEvents)
1948         return false;
1949     Editor* editor = focused->editor();
1950     if (!editor)
1951         return false;
1952
1953     // The input focus has been moved to another WebWidget object.
1954     // We should use this |editor| object only to complete the ongoing
1955     // composition.
1956     if (!editor->canEdit() && !editor->hasComposition())
1957         return false;
1958
1959     // We should verify the parent node of this IME composition node are
1960     // editable because JavaScript may delete a parent node of the composition
1961     // node. In this case, WebKit crashes while deleting texts from the parent
1962     // node, which doesn't exist any longer.
1963     PassRefPtr<Range> range = editor->compositionRange();
1964     if (range) {
1965         Node* node = range->startContainer();
1966         if (!node || !node->isContentEditable())
1967             return false;
1968     }
1969
1970     // If we're not going to fire a keypress event, then the keydown event was
1971     // canceled.  In that case, cancel any existing composition.
1972     if (text.isEmpty() || m_suppressNextKeypressEvent) {
1973         // A browser process sent an IPC message which does not contain a valid
1974         // string, which means an ongoing composition has been canceled.
1975         // If the ongoing composition has been canceled, replace the ongoing
1976         // composition string with an empty string and complete it.
1977         String emptyString;
1978         Vector<CompositionUnderline> emptyUnderlines;
1979         editor->setComposition(emptyString, emptyUnderlines, 0, 0);
1980         return text.isEmpty();
1981     }
1982
1983     // When the range of composition underlines overlap with the range between
1984     // selectionStart and selectionEnd, WebKit somehow won't paint the selection
1985     // at all (see InlineTextBox::paint() function in InlineTextBox.cpp).
1986     // But the selection range actually takes effect.
1987     editor->setComposition(String(text),
1988                            CompositionUnderlineVectorBuilder(underlines),
1989                            selectionStart, selectionEnd);
1990
1991     return editor->hasComposition();
1992 }
1993
1994 bool WebViewImpl::confirmComposition()
1995 {
1996     return confirmComposition(WebString());
1997 }
1998
1999 bool WebViewImpl::confirmComposition(const WebString& text)
2000 {
2001     Frame* focused = focusedWebCoreFrame();
2002     if (!focused || !m_imeAcceptEvents)
2003         return false;
2004     Editor* editor = focused->editor();
2005     if (!editor || (!editor->hasComposition() && !text.length()))
2006         return false;
2007
2008     // We should verify the parent node of this IME composition node are
2009     // editable because JavaScript may delete a parent node of the composition
2010     // node. In this case, WebKit crashes while deleting texts from the parent
2011     // node, which doesn't exist any longer.
2012     PassRefPtr<Range> range = editor->compositionRange();
2013     if (range) {
2014         Node* node = range->startContainer();
2015         if (!node || !node->isContentEditable())
2016             return false;
2017     }
2018
2019     if (editor->hasComposition()) {
2020         if (text.length())
2021             editor->confirmComposition(String(text));
2022         else
2023             editor->confirmComposition();
2024     } else
2025         editor->insertText(String(text), 0);
2026
2027     return true;
2028 }
2029
2030 bool WebViewImpl::compositionRange(size_t* location, size_t* length)
2031 {
2032     Frame* focused = focusedWebCoreFrame();
2033     if (!focused || !focused->selection() || !m_imeAcceptEvents)
2034         return false;
2035     Editor* editor = focused->editor();
2036     if (!editor || !editor->hasComposition())
2037         return false;
2038
2039     RefPtr<Range> range = editor->compositionRange();
2040     if (!range)
2041         return false;
2042
2043     if (TextIterator::getLocationAndLengthFromRange(focused->selection()->rootEditableElementOrDocumentElement(), range.get(), *location, *length))
2044         return true;
2045     return false;
2046 }
2047
2048 WebTextInputInfo WebViewImpl::textInputInfo()
2049 {
2050     WebTextInputInfo info;
2051
2052     Frame* focused = focusedWebCoreFrame();
2053     if (!focused)
2054         return info;
2055
2056     Editor* editor = focused->editor();
2057     if (!editor || !editor->canEdit())
2058         return info;
2059
2060     FrameSelection* selection = focused->selection();
2061     if (!selection)
2062         return info;
2063
2064     Node* node = selection->selection().rootEditableElement();
2065     if (!node)
2066         return info;
2067
2068     info.type = textInputType();
2069     if (info.type == WebTextInputTypeNone)
2070         return info;
2071
2072     info.value = plainText(rangeOfContents(node).get());
2073
2074     if (info.value.isEmpty())
2075         return info;
2076
2077     size_t location;
2078     size_t length;
2079     RefPtr<Range> range = selection->selection().firstRange();
2080     if (range && TextIterator::getLocationAndLengthFromRange(selection->rootEditableElement(), range.get(), location, length)) {
2081         info.selectionStart = location;
2082         info.selectionEnd = location + length;
2083     }
2084     range = editor->compositionRange();
2085     if (range && TextIterator::getLocationAndLengthFromRange(selection->rootEditableElement(), range.get(), location, length)) {
2086         info.compositionStart = location;
2087         info.compositionEnd = location + length;
2088     }
2089
2090     return info;
2091 }
2092
2093 WebTextInputType WebViewImpl::textInputType()
2094 {
2095     Node* node = focusedWebCoreNode();
2096     if (!node)
2097         return WebTextInputTypeNone;
2098
2099     if (node->hasTagName(HTMLNames::inputTag)) {
2100         HTMLInputElement* input = static_cast<HTMLInputElement*>(node);
2101
2102         if (input->readOnly() || input->disabled())
2103             return WebTextInputTypeNone;
2104
2105         if (input->isPasswordField())
2106             return WebTextInputTypePassword;
2107         if (input->isSearchField())
2108             return WebTextInputTypeSearch;
2109         if (input->isEmailField())
2110             return WebTextInputTypeEmail;
2111         if (input->isNumberField())
2112             return WebTextInputTypeNumber;
2113         if (input->isTelephoneField())
2114             return WebTextInputTypeTelephone;
2115         if (input->isURLField())
2116             return WebTextInputTypeURL;
2117         if (input->isDateField())
2118             return WebTextInputTypeDate;
2119         if (input->isDateTimeField())
2120             return WebTextInputTypeDateTime;
2121         if (input->isDateTimeLocalField())
2122             return WebTextInputTypeDateTimeLocal;
2123         if (input->isMonthField())
2124             return WebTextInputTypeMonth;
2125         if (input->isTimeField())
2126             return WebTextInputTypeTime;
2127         if (input->isWeekField())
2128             return WebTextInputTypeWeek;
2129         if (input->isTextField())
2130             return WebTextInputTypeText;
2131
2132         return WebTextInputTypeNone;
2133     }
2134
2135     if (node->hasTagName(HTMLNames::textareaTag)) {
2136         HTMLTextAreaElement* textarea = static_cast<HTMLTextAreaElement*>(node);
2137
2138         if (textarea->readOnly() || textarea->disabled())
2139             return WebTextInputTypeNone;
2140
2141         return WebTextInputTypeTextArea;
2142     }
2143
2144     if (node->shouldUseInputMethod())
2145         return WebTextInputTypeContentEditable;
2146
2147     return WebTextInputTypeNone;
2148 }
2149
2150 bool WebViewImpl::selectionBounds(WebRect& start, WebRect& end) const
2151 {
2152     const Frame* frame = focusedWebCoreFrame();
2153     if (!frame)
2154         return false;
2155     FrameSelection* selection = frame->selection();
2156     if (!selection)
2157         return false;
2158
2159     if (selection->isCaret()) {
2160         start = end = frame->view()->contentsToWindow(selection->absoluteCaretBounds());
2161         return true;
2162     }
2163
2164     RefPtr<Range> selectedRange = frame->selection()->toNormalizedRange();
2165     if (!selectedRange)
2166         return false;
2167
2168     RefPtr<Range> range(Range::create(selectedRange->startContainer()->document(),
2169                                       selectedRange->startContainer(),
2170                                       selectedRange->startOffset(),
2171                                       selectedRange->startContainer(),
2172                                       selectedRange->startOffset()));
2173     start = frame->editor()->firstRectForRange(range.get());
2174
2175     range = Range::create(selectedRange->endContainer()->document(),
2176                           selectedRange->endContainer(),
2177                           selectedRange->endOffset(),
2178                           selectedRange->endContainer(),
2179                           selectedRange->endOffset());
2180     end = frame->editor()->firstRectForRange(range.get());
2181
2182     start = frame->view()->contentsToWindow(start);
2183     end = frame->view()->contentsToWindow(end);
2184
2185     if (!frame->selection()->selection().isBaseFirst())
2186         std::swap(start, end);
2187     return true;
2188 }
2189
2190 bool WebViewImpl::selectionTextDirection(WebTextDirection& start, WebTextDirection& end) const
2191 {
2192     const Frame* frame = focusedWebCoreFrame();
2193     if (!frame)
2194         return false;
2195     FrameSelection* selection = frame->selection();
2196     if (!selection)
2197         return false;
2198     if (!selection->toNormalizedRange())
2199         return false;
2200     start = selection->start().primaryDirection() == RTL ? WebTextDirectionRightToLeft : WebTextDirectionLeftToRight;
2201     end = selection->end().primaryDirection() == RTL ? WebTextDirectionRightToLeft : WebTextDirectionLeftToRight;
2202     return true;
2203 }
2204
2205 bool WebViewImpl::setEditableSelectionOffsets(int start, int end)
2206 {
2207     const Frame* focused = focusedWebCoreFrame();
2208     if (!focused)
2209         return false;
2210
2211     Editor* editor = focused->editor();
2212     if (!editor || !editor->canEdit())
2213         return false;
2214
2215     return editor->setSelectionOffsets(start, end);
2216 }
2217
2218 bool WebViewImpl::isSelectionEditable() const
2219 {
2220     const Frame* frame = focusedWebCoreFrame();
2221     if (!frame)
2222         return false;
2223     return frame->selection()->isContentEditable();
2224 }
2225
2226 WebColor WebViewImpl::backgroundColor() const
2227 {
2228     if (!m_page)
2229         return Color::white;
2230     FrameView* view = m_page->mainFrame()->view();
2231     Color backgroundColor = view->documentBackgroundColor();
2232     if (!backgroundColor.isValid())
2233         return Color::white;
2234     return backgroundColor.rgb();
2235 }
2236
2237 bool WebViewImpl::caretOrSelectionRange(size_t* location, size_t* length)
2238 {
2239     const Frame* focused = focusedWebCoreFrame();
2240     if (!focused)
2241         return false;
2242
2243     FrameSelection* selection = focused->selection();
2244     if (!selection)
2245         return false;
2246
2247     RefPtr<Range> range = selection->selection().firstRange();
2248     if (!range)
2249         return false;
2250
2251     if (TextIterator::getLocationAndLengthFromRange(selection->rootEditableElementOrDocumentElement(), range.get(), *location, *length))
2252         return true;
2253     return false;
2254 }
2255
2256 void WebViewImpl::setTextDirection(WebTextDirection direction)
2257 {
2258     // The Editor::setBaseWritingDirection() function checks if we can change
2259     // the text direction of the selected node and updates its DOM "dir"
2260     // attribute and its CSS "direction" property.
2261     // So, we just call the function as Safari does.
2262     const Frame* focused = focusedWebCoreFrame();
2263     if (!focused)
2264         return;
2265
2266     Editor* editor = focused->editor();
2267     if (!editor || !editor->canEdit())
2268         return;
2269
2270     switch (direction) {
2271     case WebTextDirectionDefault:
2272         editor->setBaseWritingDirection(NaturalWritingDirection);
2273         break;
2274
2275     case WebTextDirectionLeftToRight:
2276         editor->setBaseWritingDirection(LeftToRightWritingDirection);
2277         break;
2278
2279     case WebTextDirectionRightToLeft:
2280         editor->setBaseWritingDirection(RightToLeftWritingDirection);
2281         break;
2282
2283     default:
2284         notImplemented();
2285         break;
2286     }
2287 }
2288
2289 bool WebViewImpl::isAcceleratedCompositingActive() const
2290 {
2291 #if USE(ACCELERATED_COMPOSITING)
2292     return m_isAcceleratedCompositingActive;
2293 #else
2294     return false;
2295 #endif
2296 }
2297
2298 void WebViewImpl::didAcquirePointerLock()
2299 {
2300 #if ENABLE(POINTER_LOCK)
2301     if (page())
2302         page()->pointerLockController()->didAcquirePointerLock();
2303 #endif
2304 }
2305
2306 void WebViewImpl::didNotAcquirePointerLock()
2307 {
2308 #if ENABLE(POINTER_LOCK)
2309     if (page())
2310         page()->pointerLockController()->didNotAcquirePointerLock();
2311 #endif
2312 }
2313
2314 void WebViewImpl::didLosePointerLock()
2315 {
2316 #if ENABLE(POINTER_LOCK)
2317     if (page())
2318         page()->pointerLockController()->didLosePointerLock();
2319 #endif
2320 }
2321
2322 void WebViewImpl::didChangeWindowResizerRect()
2323 {
2324     if (mainFrameImpl()->frameView())
2325         mainFrameImpl()->frameView()->windowResizerRectChanged();
2326 }
2327
2328 // WebView --------------------------------------------------------------------
2329
2330 WebSettingsImpl* WebViewImpl::settingsImpl()
2331 {
2332     if (!m_webSettings)
2333         m_webSettings = adoptPtr(new WebSettingsImpl(m_page->settings()));
2334     ASSERT(m_webSettings);
2335     return m_webSettings.get();
2336 }
2337
2338 WebSettings* WebViewImpl::settings()
2339 {
2340     return settingsImpl();
2341 }
2342
2343 WebString WebViewImpl::pageEncoding() const
2344 {
2345     if (!m_page)
2346         return WebString();
2347
2348     // FIXME: Is this check needed?
2349     if (!m_page->mainFrame()->document()->loader())
2350         return WebString();
2351
2352     return m_page->mainFrame()->document()->encoding();
2353 }
2354
2355 void WebViewImpl::setPageEncoding(const WebString& encodingName)
2356 {
2357     if (!m_page)
2358         return;
2359
2360     // Only change override encoding, don't change default encoding.
2361     // Note that the new encoding must be 0 if it isn't supposed to be set.
2362     String newEncodingName;
2363     if (!encodingName.isEmpty())
2364         newEncodingName = encodingName;
2365     m_page->mainFrame()->loader()->reloadWithOverrideEncoding(newEncodingName);
2366 }
2367
2368 bool WebViewImpl::dispatchBeforeUnloadEvent()
2369 {
2370     // FIXME: This should really cause a recursive depth-first walk of all
2371     // frames in the tree, calling each frame's onbeforeunload.  At the moment,
2372     // we're consistent with Safari 3.1, not IE/FF.
2373     Frame* frame = m_page->mainFrame();
2374     if (!frame)
2375         return true;
2376
2377     return frame->loader()->shouldClose();
2378 }
2379
2380 void WebViewImpl::dispatchUnloadEvent()
2381 {
2382     // Run unload handlers.
2383     m_page->mainFrame()->loader()->closeURL();
2384 }
2385
2386 WebFrame* WebViewImpl::mainFrame()
2387 {
2388     return mainFrameImpl();
2389 }
2390
2391 WebFrame* WebViewImpl::findFrameByName(
2392     const WebString& name, WebFrame* relativeToFrame)
2393 {
2394     if (!relativeToFrame)
2395         relativeToFrame = mainFrame();
2396     Frame* frame = static_cast<WebFrameImpl*>(relativeToFrame)->frame();
2397     frame = frame->tree()->find(name);
2398     return WebFrameImpl::fromFrame(frame);
2399 }
2400
2401 WebFrame* WebViewImpl::focusedFrame()
2402 {
2403     return WebFrameImpl::fromFrame(focusedWebCoreFrame());
2404 }
2405
2406 void WebViewImpl::setFocusedFrame(WebFrame* frame)
2407 {
2408     if (!frame) {
2409         // Clears the focused frame if any.
2410         Frame* frame = focusedWebCoreFrame();
2411         if (frame)
2412             frame->selection()->setFocused(false);
2413         return;
2414     }
2415     WebFrameImpl* frameImpl = static_cast<WebFrameImpl*>(frame);
2416     Frame* webcoreFrame = frameImpl->frame();
2417     webcoreFrame->page()->focusController()->setFocusedFrame(webcoreFrame);
2418 }
2419
2420 void WebViewImpl::setInitialFocus(bool reverse)
2421 {
2422     if (!m_page)
2423         return;
2424
2425     // Since we don't have a keyboard event, we'll create one.
2426     WebKeyboardEvent keyboardEvent;
2427     keyboardEvent.type = WebInputEvent::RawKeyDown;
2428     if (reverse)
2429         keyboardEvent.modifiers = WebInputEvent::ShiftKey;
2430
2431     // VK_TAB which is only defined on Windows.
2432     keyboardEvent.windowsKeyCode = 0x09;
2433     PlatformKeyboardEventBuilder platformEvent(keyboardEvent);
2434     RefPtr<KeyboardEvent> webkitEvent = KeyboardEvent::create(platformEvent, 0);
2435
2436     Frame* frame = page()->focusController()->focusedOrMainFrame();
2437     if (Document* document = frame->document())
2438         document->setFocusedNode(0);
2439     page()->focusController()->setInitialFocus(
2440         reverse ? FocusDirectionBackward : FocusDirectionForward,
2441         webkitEvent.get());
2442 }
2443
2444 void WebViewImpl::clearFocusedNode()
2445 {
2446     RefPtr<Frame> frame = focusedWebCoreFrame();
2447     if (!frame)
2448         return;
2449
2450     RefPtr<Document> document = frame->document();
2451     if (!document)
2452         return;
2453
2454     RefPtr<Node> oldFocusedNode = document->focusedNode();
2455
2456     // Clear the focused node.
2457     document->setFocusedNode(0);
2458
2459     if (!oldFocusedNode)
2460         return;
2461
2462     // If a text field has focus, we need to make sure the selection controller
2463     // knows to remove selection from it. Otherwise, the text field is still
2464     // processing keyboard events even though focus has been moved to the page and
2465     // keystrokes get eaten as a result.
2466     if (oldFocusedNode->isContentEditable()
2467         || (oldFocusedNode->isElementNode()
2468             && static_cast<Element*>(oldFocusedNode.get())->isTextFormControl())) {
2469         frame->selection()->clear();
2470     }
2471 }
2472
2473 void WebViewImpl::scrollFocusedNodeIntoView()
2474 {
2475     Node* focusedNode = focusedWebCoreNode();
2476     if (focusedNode && focusedNode->isElementNode()) {
2477         Element* elementNode = static_cast<Element*>(focusedNode);
2478         elementNode->scrollIntoViewIfNeeded(true);
2479     }
2480 }
2481
2482 void WebViewImpl::scrollFocusedNodeIntoRect(const WebRect& rect)
2483 {
2484     Frame* frame = page()->mainFrame();
2485     Node* focusedNode = focusedWebCoreNode();
2486     if (!frame || !frame->view() || !focusedNode || !focusedNode->isElementNode())
2487         return;
2488     Element* elementNode = static_cast<Element*>(focusedNode);
2489     frame->view()->scrollElementToRect(elementNode, IntRect(rect.x, rect.y, rect.width, rect.height));
2490 }
2491
2492 void WebViewImpl::advanceFocus(bool reverse)
2493 {
2494     page()->focusController()->advanceFocus(reverse ? FocusDirectionBackward : FocusDirectionForward, 0);
2495 }
2496
2497 double WebViewImpl::zoomLevel()
2498 {
2499     return m_zoomLevel;
2500 }
2501
2502 double WebViewImpl::setZoomLevel(bool textOnly, double zoomLevel)
2503 {
2504     if (zoomLevel < m_minimumZoomLevel)
2505         m_zoomLevel = m_minimumZoomLevel;
2506     else if (zoomLevel > m_maximumZoomLevel)
2507         m_zoomLevel = m_maximumZoomLevel;
2508     else
2509         m_zoomLevel = zoomLevel;
2510
2511     Frame* frame = mainFrameImpl()->frame();
2512     WebPluginContainerImpl* pluginContainer = WebFrameImpl::pluginContainerFromFrame(frame);
2513     if (pluginContainer)
2514         pluginContainer->plugin()->setZoomLevel(m_zoomLevel, textOnly);
2515     else {
2516         float zoomFactor = static_cast<float>(zoomLevelToZoomFactor(m_zoomLevel));
2517         if (textOnly)
2518             frame->setPageAndTextZoomFactors(1, zoomFactor * m_emulatedTextZoomFactor);
2519         else
2520             frame->setPageAndTextZoomFactors(zoomFactor, m_emulatedTextZoomFactor);
2521     }
2522     return m_zoomLevel;
2523 }
2524
2525 void WebViewImpl::zoomLimitsChanged(double minimumZoomLevel,
2526                                     double maximumZoomLevel)
2527 {
2528     m_minimumZoomLevel = minimumZoomLevel;
2529     m_maximumZoomLevel = maximumZoomLevel;
2530     m_client->zoomLimitsChanged(m_minimumZoomLevel, m_maximumZoomLevel);
2531 }
2532
2533 void WebViewImpl::fullFramePluginZoomLevelChanged(double zoomLevel)
2534 {
2535     if (zoomLevel == m_zoomLevel)
2536         return;
2537
2538     m_zoomLevel = max(min(zoomLevel, m_maximumZoomLevel), m_minimumZoomLevel);
2539     m_client->zoomLevelChanged();
2540 }
2541
2542 double WebView::zoomLevelToZoomFactor(double zoomLevel)
2543 {
2544     return pow(textSizeMultiplierRatio, zoomLevel);
2545 }
2546
2547 double WebView::zoomFactorToZoomLevel(double factor)
2548 {
2549     // Since factor = 1.2^level, level = log(factor) / log(1.2)
2550     return log(factor) / log(textSizeMultiplierRatio);
2551 }
2552
2553 float WebViewImpl::pageScaleFactor() const
2554 {
2555     if (!page())
2556         return 1;
2557
2558     return page()->pageScaleFactor();
2559 }
2560
2561 bool WebViewImpl::isPageScaleFactorSet() const
2562 {
2563     return m_pageScaleFactorIsSet;
2564 }
2565
2566 float WebViewImpl::clampPageScaleFactorToLimits(float scaleFactor)
2567 {
2568     return min(max(scaleFactor, m_minimumPageScaleFactor), m_maximumPageScaleFactor);
2569 }
2570
2571 WebPoint WebViewImpl::clampOffsetAtScale(const WebPoint& offset, float scale)
2572 {
2573     // This is the scaled content size. We need to convert it to the new scale factor.
2574     WebSize contentSize = mainFrame()->contentsSize();
2575     float deltaScale = scale / pageScaleFactor();
2576     int docWidthAtNewScale = contentSize.width * deltaScale;
2577     int docHeightAtNewScale = contentSize.height * deltaScale;
2578     int viewWidth = m_size.width;
2579     int viewHeight = m_size.height;
2580
2581     // Enforce the maximum and minimum scroll positions at the new scale.
2582     IntPoint clampedOffset = offset;
2583     clampedOffset = clampedOffset.shrunkTo(IntPoint(docWidthAtNewScale - viewWidth, docHeightAtNewScale - viewHeight));
2584     clampedOffset.clampNegativeToZero();
2585     return clampedOffset;
2586 }
2587
2588 void WebViewImpl::setPageScaleFactorPreservingScrollOffset(float scaleFactor)
2589 {
2590     // Pick a scale factor that is within the expected limits
2591     scaleFactor = clampPageScaleFactorToLimits(scaleFactor);
2592
2593     IntPoint scrollOffsetAtNewScale(mainFrame()->scrollOffset().width, mainFrame()->scrollOffset().height);
2594     float deltaScale = scaleFactor / pageScaleFactor();
2595     scrollOffsetAtNewScale.scale(deltaScale, deltaScale);
2596
2597     WebPoint clampedOffsetAtNewScale = clampOffsetAtScale(scrollOffsetAtNewScale, scaleFactor);
2598     setPageScaleFactor(scaleFactor, clampedOffsetAtNewScale);
2599 }
2600
2601 void WebViewImpl::setPageScaleFactor(float scaleFactor, const WebPoint& origin)
2602 {
2603     if (!page())
2604         return;
2605
2606     if (!scaleFactor)
2607         scaleFactor = 1;
2608
2609     if (m_deviceScaleInCompositor != 1) {
2610         // Don't allow page scaling when compositor scaling is being used,
2611         // as they are currently incompatible.
2612         ASSERT(scaleFactor == 1);
2613     }
2614
2615     scaleFactor = clampPageScaleFactorToLimits(scaleFactor);
2616     WebPoint clampedOrigin = clampOffsetAtScale(origin, scaleFactor);
2617     page()->setPageScaleFactor(scaleFactor, clampedOrigin);
2618     m_pageScaleFactorIsSet = true;
2619 }
2620
2621 float WebViewImpl::deviceScaleFactor() const
2622 {
2623     if (!page())
2624         return 1;
2625
2626     return page()->deviceScaleFactor();
2627 }
2628
2629 void WebViewImpl::setDeviceScaleFactor(float scaleFactor)
2630 {
2631     if (!page())
2632         return;
2633
2634     page()->setDeviceScaleFactor(scaleFactor);
2635
2636     if (!m_layerTreeView.isNull() && m_webSettings->applyDefaultDeviceScaleFactorInCompositor()) {
2637         m_deviceScaleInCompositor = page()->deviceScaleFactor();
2638         m_layerTreeView.setDeviceScaleFactor(m_deviceScaleInCompositor);
2639     }
2640     if (m_deviceScaleInCompositor != 1) {
2641         // Don't allow page scaling when compositor scaling is being used,
2642         // as they are currently incompatible. This means the deviceScale
2643         // needs to match the one in the compositor.
2644         ASSERT(scaleFactor == m_deviceScaleInCompositor);
2645     }
2646 }
2647
2648 bool WebViewImpl::isFixedLayoutModeEnabled() const
2649 {
2650     if (!page())
2651         return false;
2652
2653     Frame* frame = page()->mainFrame();
2654     if (!frame || !frame->view())
2655         return false;
2656
2657     return frame->view()->useFixedLayout();
2658 }
2659
2660 void WebViewImpl::enableFixedLayoutMode(bool enable)
2661 {
2662     if (!page())
2663         return;
2664
2665     Frame* frame = page()->mainFrame();
2666     if (!frame || !frame->view())
2667         return;
2668
2669     frame->view()->setUseFixedLayout(enable);
2670
2671 #if USE(ACCELERATED_COMPOSITING)
2672     // Also notify the base layer, which RenderLayerCompositor does not see.
2673     if (m_nonCompositedContentHost)
2674         updateLayerTreeViewport();
2675 #endif
2676 }
2677
2678
2679 void WebViewImpl::enableAutoResizeMode(const WebSize& minSize, const WebSize& maxSize)
2680 {
2681     m_shouldAutoResize = true;
2682     m_minAutoSize = minSize;
2683     m_maxAutoSize = maxSize;
2684     configureAutoResizeMode();
2685 }
2686
2687 void WebViewImpl::disableAutoResizeMode()
2688 {
2689     m_shouldAutoResize = false;
2690     configureAutoResizeMode();
2691 }
2692
2693 void WebViewImpl::setPageScaleFactorLimits(float minPageScale, float maxPageScale)
2694 {
2695     m_pageDefinedMinimumPageScaleFactor = minPageScale;
2696     m_pageDefinedMaximumPageScaleFactor = maxPageScale;
2697     computePageScaleFactorLimits();
2698 }
2699
2700 void WebViewImpl::setIgnoreViewportTagMaximumScale(bool flag)
2701 {
2702     m_ignoreViewportTagMaximumScale = flag;
2703
2704     if (!page() || !page()->mainFrame())
2705         return;
2706
2707     m_page->chrome()->client()->dispatchViewportPropertiesDidChange(page()->mainFrame()->document()->viewportArguments());
2708 }
2709
2710 bool WebViewImpl::computePageScaleFactorLimits()
2711 {
2712     if (m_pageDefinedMinimumPageScaleFactor == -1 || m_pageDefinedMaximumPageScaleFactor == -1)
2713         return false;
2714
2715     if (!mainFrame() || !page() || !page()->mainFrame() || !page()->mainFrame()->view())
2716         return false;
2717
2718     m_minimumPageScaleFactor = min(max(m_pageDefinedMinimumPageScaleFactor, minPageScaleFactor), maxPageScaleFactor) * (deviceScaleFactor() / m_deviceScaleInCompositor);
2719     m_maximumPageScaleFactor = max(min(m_pageDefinedMaximumPageScaleFactor, maxPageScaleFactor), minPageScaleFactor) * (deviceScaleFactor() / m_deviceScaleInCompositor);
2720
2721     int viewWidthNotIncludingScrollbars = page()->mainFrame()->view()->visibleContentRect(false).width();
2722     int contentsWidth = mainFrame()->contentsSize().width;
2723     if (viewWidthNotIncludingScrollbars && contentsWidth) {
2724         // Limit page scaling down to the document width.
2725         int unscaledContentWidth = contentsWidth / pageScaleFactor();
2726         m_minimumPageScaleFactor = max(m_minimumPageScaleFactor, static_cast<float>(viewWidthNotIncludingScrollbars) / unscaledContentWidth);
2727         m_maximumPageScaleFactor = max(m_minimumPageScaleFactor, m_maximumPageScaleFactor);
2728     }
2729     ASSERT(m_minimumPageScaleFactor <= m_maximumPageScaleFactor);
2730
2731     float clampedScale = clampPageScaleFactorToLimits(pageScaleFactor());
2732 #if USE(ACCELERATED_COMPOSITING)
2733     if (!m_layerTreeView.isNull())
2734         m_layerTreeView.setPageScaleFactorAndLimits(clampedScale, m_minimumPageScaleFactor, m_maximumPageScaleFactor);
2735 #endif
2736     if (clampedScale != pageScaleFactor()) {
2737         setPageScaleFactorPreservingScrollOffset(clampedScale);
2738         return true;
2739     }
2740
2741     return false;
2742 }
2743
2744 float WebViewImpl::minimumPageScaleFactor() const
2745 {
2746     return m_minimumPageScaleFactor;
2747 }
2748
2749 float WebViewImpl::maximumPageScaleFactor() const
2750 {
2751     return m_maximumPageScaleFactor;
2752 }
2753
2754 void WebViewImpl::saveScrollAndScaleState()
2755 {
2756     m_savedPageScaleFactor = pageScaleFactor();
2757     m_savedScrollOffset = mainFrame()->scrollOffset();
2758 }
2759
2760 void WebViewImpl::restoreScrollAndScaleState()
2761 {
2762     if (!m_savedPageScaleFactor)
2763         return;
2764
2765 #if ENABLE(GESTURE_EVENTS)
2766     startPageScaleAnimation(IntPoint(m_savedScrollOffset), false, m_savedPageScaleFactor, scrollAndScaleAnimationDurationInSeconds);
2767 #else
2768     setPageScaleFactor(m_savedPageScaleFactor, WebPoint());
2769     mainFrame()->setScrollOffset(m_savedScrollOffset);
2770 #endif
2771
2772     resetSavedScrollAndScaleState();
2773 }
2774
2775 void WebViewImpl::resetSavedScrollAndScaleState()
2776 {
2777     m_savedPageScaleFactor = 0;
2778     m_savedScrollOffset = IntSize();
2779 }
2780
2781 WebSize WebViewImpl::fixedLayoutSize() const
2782 {
2783     if (!page())
2784         return WebSize();
2785
2786     Frame* frame = page()->mainFrame();
2787     if (!frame || !frame->view())
2788         return WebSize();
2789
2790     return frame->view()->fixedLayoutSize();
2791 }
2792
2793 void WebViewImpl::setFixedLayoutSize(const WebSize& layoutSize)
2794 {
2795     if (!page())
2796         return;
2797
2798     Frame* frame = page()->mainFrame();
2799     if (!frame || !frame->view())
2800         return;
2801
2802     frame->view()->setFixedLayoutSize(layoutSize);
2803 }
2804
2805 void WebViewImpl::performMediaPlayerAction(const WebMediaPlayerAction& action,
2806                                            const WebPoint& location)
2807 {
2808     HitTestResult result = hitTestResultForWindowPos(location);
2809     RefPtr<Node> node = result.innerNonSharedNode();
2810     if (!node->hasTagName(HTMLNames::videoTag) && !node->hasTagName(HTMLNames::audioTag))
2811         return;
2812
2813     RefPtr<HTMLMediaElement> mediaElement =
2814         static_pointer_cast<HTMLMediaElement>(node);
2815     switch (action.type) {
2816     case WebMediaPlayerAction::Play:
2817         if (action.enable)
2818             mediaElement->play();
2819         else
2820             mediaElement->pause();
2821         break;
2822     case WebMediaPlayerAction::Mute:
2823         mediaElement->setMuted(action.enable);
2824         break;
2825     case WebMediaPlayerAction::Loop:
2826         mediaElement->setLoop(action.enable);
2827         break;
2828     case WebMediaPlayerAction::Controls:
2829         mediaElement->setControls(action.enable);
2830         break;
2831     default:
2832         ASSERT_NOT_REACHED();
2833     }
2834 }
2835
2836 void WebViewImpl::performPluginAction(const WebPluginAction& action,
2837                                       const WebPoint& location)
2838 {
2839     HitTestResult result = hitTestResultForWindowPos(location);
2840     RefPtr<Node> node = result.innerNonSharedNode();
2841     if (!node->hasTagName(HTMLNames::objectTag) && !node->hasTagName(HTMLNames::embedTag))
2842         return;
2843
2844     RenderObject* object = node->renderer();
2845     if (object && object->isWidget()) {
2846         Widget* widget = toRenderWidget(object)->widget();
2847         if (widget && widget->isPluginContainer()) {
2848             WebPluginContainerImpl* plugin = static_cast<WebPluginContainerImpl*>(widget);
2849             switch (action.type) {
2850             case WebPluginAction::Rotate90Clockwise:
2851                 plugin->plugin()->rotateView(WebPlugin::RotationType90Clockwise);
2852                 break;
2853             case WebPluginAction::Rotate90Counterclockwise:
2854                 plugin->plugin()->rotateView(WebPlugin::RotationType90Counterclockwise);
2855                 break;
2856             default:
2857                 ASSERT_NOT_REACHED();
2858             }
2859         }
2860     }
2861 }
2862
2863 void WebViewImpl::copyImageAt(const WebPoint& point)
2864 {
2865     if (!m_page)
2866         return;
2867
2868     HitTestResult result = hitTestResultForWindowPos(point);
2869
2870     if (result.absoluteImageURL().isEmpty()) {
2871         // There isn't actually an image at these coordinates.  Might be because
2872         // the window scrolled while the context menu was open or because the page
2873         // changed itself between when we thought there was an image here and when
2874         // we actually tried to retreive the image.
2875         //
2876         // FIXME: implement a cache of the most recent HitTestResult to avoid having
2877         //        to do two hit tests.
2878         return;
2879     }
2880
2881     m_page->mainFrame()->editor()->copyImage(result);
2882 }
2883
2884 void WebViewImpl::dragSourceEndedAt(
2885     const WebPoint& clientPoint,
2886     const WebPoint& screenPoint,
2887     WebDragOperation operation)
2888 {
2889     PlatformMouseEvent pme(clientPoint,
2890                            screenPoint,
2891                            LeftButton, PlatformEvent::MouseMoved, 0, false, false, false,
2892                            false, 0);
2893     m_page->mainFrame()->eventHandler()->dragSourceEndedAt(pme,
2894         static_cast<DragOperation>(operation));
2895     m_dragScrollTimer->stop();
2896 }
2897
2898 void WebViewImpl::dragSourceMovedTo(
2899     const WebPoint& clientPoint,
2900     const WebPoint& screenPoint,
2901     WebDragOperation operation)
2902 {
2903     m_dragScrollTimer->triggerScroll(mainFrameImpl()->frameView(), clientPoint);
2904 }
2905
2906 void WebViewImpl::dragSourceSystemDragEnded()
2907 {
2908     // It's possible for us to get this callback while not doing a drag if
2909     // it's from a previous page that got unloaded.
2910     if (m_doingDragAndDrop) {
2911         m_page->dragController()->dragEnded();
2912         m_doingDragAndDrop = false;
2913     }
2914 }
2915
2916 WebDragOperation WebViewImpl::dragTargetDragEnter(
2917     const WebDragData& webDragData,
2918     const WebPoint& clientPoint,
2919     const WebPoint& screenPoint,
2920     WebDragOperationsMask operationsAllowed,
2921     int keyModifiers)
2922 {
2923     ASSERT(!m_currentDragData);
2924
2925     m_currentDragData = webDragData;
2926     m_operationsAllowed = operationsAllowed;
2927
2928     return dragTargetDragEnterOrOver(clientPoint, screenPoint, DragEnter, keyModifiers);
2929 }
2930
2931 WebDragOperation WebViewImpl::dragTargetDragOver(
2932     const WebPoint& clientPoint,
2933     const WebPoint& screenPoint,
2934     WebDragOperationsMask operationsAllowed,
2935     int keyModifiers)
2936 {
2937     m_operationsAllowed = operationsAllowed;
2938
2939     return dragTargetDragEnterOrOver(clientPoint, screenPoint, DragOver, keyModifiers);
2940 }
2941
2942 void WebViewImpl::dragTargetDragLeave()
2943 {
2944     ASSERT(m_currentDragData);
2945
2946     DragData dragData(
2947         m_currentDragData.get(),
2948         IntPoint(),
2949         IntPoint(),
2950         static_cast<DragOperation>(m_operationsAllowed));
2951
2952     m_page->dragController()->dragExited(&dragData);
2953
2954     // FIXME: why is the drag scroll timer not stopped here?
2955
2956     m_dragOperation = WebDragOperationNone;
2957     m_currentDragData = 0;
2958 }
2959
2960 void WebViewImpl::dragTargetDrop(const WebPoint& clientPoint,
2961                                  const WebPoint& screenPoint,
2962                                  int keyModifiers)
2963 {
2964     ASSERT(m_currentDragData);
2965
2966     // If this webview transitions from the "drop accepting" state to the "not
2967     // accepting" state, then our IPC message reply indicating that may be in-
2968     // flight, or else delayed by javascript processing in this webview.  If a
2969     // drop happens before our IPC reply has reached the browser process, then
2970     // the browser forwards the drop to this webview.  So only allow a drop to
2971     // proceed if our webview m_dragOperation state is not DragOperationNone.
2972
2973     if (m_dragOperation == WebDragOperationNone) { // IPC RACE CONDITION: do not allow this drop.
2974         dragTargetDragLeave();
2975         return;
2976     }
2977
2978     m_currentDragData->setModifierKeyState(webInputEventKeyStateToPlatformEventKeyState(keyModifiers));
2979     DragData dragData(
2980         m_currentDragData.get(),
2981         clientPoint,
2982         screenPoint,
2983         static_cast<DragOperation>(m_operationsAllowed));
2984
2985     m_page->dragController()->performDrag(&dragData);
2986
2987     m_dragOperation = WebDragOperationNone;
2988     m_currentDragData = 0;
2989
2990     m_dragScrollTimer->stop();
2991 }
2992
2993 WebDragOperation WebViewImpl::dragTargetDragEnterOrOver(const WebPoint& clientPoint, const WebPoint& screenPoint, DragAction dragAction, int keyModifiers)
2994 {
2995     ASSERT(m_currentDragData);
2996
2997     m_currentDragData->setModifierKeyState(webInputEventKeyStateToPlatformEventKeyState(keyModifiers));
2998     DragData dragData(
2999         m_currentDragData.get(),
3000         clientPoint,
3001         screenPoint,
3002         static_cast<DragOperation>(m_operationsAllowed));
3003
3004     DragSession dragSession;
3005     if (dragAction == DragEnter)
3006         dragSession = m_page->dragController()->dragEntered(&dragData);
3007     else
3008         dragSession = m_page->dragController()->dragUpdated(&dragData);
3009
3010     DragOperation dropEffect = dragSession.operation;
3011
3012     // Mask the drop effect operation against the drag source's allowed operations.
3013     if (!(dropEffect & dragData.draggingSourceOperationMask()))
3014         dropEffect = DragOperationNone;
3015
3016      m_dragOperation = static_cast<WebDragOperation>(dropEffect);
3017
3018     if (dragAction == DragOver)
3019         m_dragScrollTimer->triggerScroll(mainFrameImpl()->frameView(), clientPoint);
3020     else
3021         m_dragScrollTimer->stop();
3022
3023     return m_dragOperation;
3024 }
3025
3026 void WebViewImpl::sendResizeEventAndRepaint()
3027 {
3028     if (mainFrameImpl()->frameView()) {
3029         // Enqueues the resize event.
3030         mainFrameImpl()->frame()->eventHandler()->sendResizeEvent();
3031     }
3032
3033     if (m_client) {
3034         if (isAcceleratedCompositingActive()) {
3035 #if USE(ACCELERATED_COMPOSITING)
3036             updateLayerTreeViewport();
3037 #endif
3038         } else {
3039             WebRect damagedRect(0, 0, m_size.width, m_size.height);
3040             m_client->didInvalidateRect(damagedRect);
3041         }
3042     }
3043 }
3044
3045 void WebViewImpl::configureAutoResizeMode()
3046 {
3047     if (!mainFrameImpl() || !mainFrameImpl()->frame() || !mainFrameImpl()->frame()->view())
3048         return;
3049
3050     mainFrameImpl()->frame()->view()->enableAutoSizeMode(m_shouldAutoResize, m_minAutoSize, m_maxAutoSize);
3051 }
3052
3053 unsigned long WebViewImpl::createUniqueIdentifierForRequest()
3054 {
3055     if (m_page)
3056         return m_page->progress()->createUniqueIdentifier();
3057     return 0;
3058 }
3059
3060 void WebViewImpl::inspectElementAt(const WebPoint& point)
3061 {
3062     if (!m_page)
3063         return;
3064
3065     if (point.x == -1 || point.y == -1)
3066         m_page->inspectorController()->inspect(0);
3067     else {
3068         HitTestResult result = hitTestResultForWindowPos(point);
3069
3070         if (!result.innerNonSharedNode())
3071             return;
3072
3073         m_page->inspectorController()->inspect(result.innerNonSharedNode());
3074     }
3075 }
3076
3077 WebString WebViewImpl::inspectorSettings() const
3078 {
3079     return m_inspectorSettings;
3080 }
3081
3082 void WebViewImpl::setInspectorSettings(const WebString& settings)
3083 {
3084     m_inspectorSettings = settings;
3085 }
3086
3087 bool WebViewImpl::inspectorSetting(const WebString& key, WebString* value) const
3088 {
3089     if (!m_inspectorSettingsMap->contains(key))
3090         return false;
3091     *value = m_inspectorSettingsMap->get(key);
3092     return true;
3093 }
3094
3095 void WebViewImpl::setInspectorSetting(const WebString& key,
3096                                       const WebString& value)
3097 {
3098     m_inspectorSettingsMap->set(key, value);
3099     client()->didUpdateInspectorSetting(key, value);
3100 }
3101
3102 WebDevToolsAgent* WebViewImpl::devToolsAgent()
3103 {
3104     return m_devToolsAgent.get();
3105 }
3106
3107 WebAccessibilityObject WebViewImpl::accessibilityObject()
3108 {
3109     if (!mainFrameImpl())
3110         return WebAccessibilityObject();
3111
3112     Document* document = mainFrameImpl()->frame()->document();
3113     return WebAccessibilityObject(
3114         document->axObjectCache()->getOrCreate(document->renderer()));
3115 }
3116
3117 void WebViewImpl::applyAutofillSuggestions(
3118     const WebNode& node,
3119     const WebVector<WebString>& names,
3120     const WebVector<WebString>& labels,
3121     const WebVector<WebString>& icons,
3122     const WebVector<int>& itemIDs,
3123     int separatorIndex)
3124 {
3125     ASSERT(names.size() == labels.size());
3126     ASSERT(names.size() == itemIDs.size());
3127
3128     if (names.isEmpty()) {
3129         hideAutofillPopup();
3130         return;
3131     }
3132
3133     RefPtr<Node> focusedNode = focusedWebCoreNode();
3134     // If the node for which we queried the Autofill suggestions is not the
3135     // focused node, then we have nothing to do.  FIXME: also check the
3136     // caret is at the end and that the text has not changed.
3137     if (!focusedNode || focusedNode != PassRefPtr<Node>(node)) {
3138         hideAutofillPopup();
3139         return;
3140     }
3141
3142     HTMLInputElement* inputElem = focusedNode->toInputElement();
3143     ASSERT(inputElem);
3144
3145     // The first time the Autofill popup is shown we'll create the client and
3146     // the popup.
3147     if (!m_autofillPopupClient)
3148         m_autofillPopupClient = adoptPtr(new AutofillPopupMenuClient);
3149
3150     m_autofillPopupClient->initialize(
3151         inputElem, names, labels, icons, itemIDs, separatorIndex);
3152
3153     if (!m_autofillPopup) {
3154         PopupContainerSettings popupSettings = autofillPopupSettings;
3155         popupSettings.deviceSupportsTouch = settingsImpl()->deviceSupportsTouch();
3156         m_autofillPopup = PopupContainer::create(m_autofillPopupClient.get(),
3157                                                  PopupContainer::Suggestion,
3158                                                  popupSettings);
3159     }
3160
3161     if (m_autofillPopupShowing) {
3162         refreshAutofillPopup();
3163     } else {
3164         m_autofillPopupShowing = true;
3165         m_autofillPopup->showInRect(focusedNode->getPixelSnappedRect(), focusedNode->ownerDocument()->view(), 0);
3166     }
3167 }
3168
3169 void WebViewImpl::hidePopups()
3170 {
3171     hideSelectPopup();
3172     hideAutofillPopup();
3173 #if ENABLE(PAGE_POPUP)
3174     if (m_pagePopup)
3175         closePagePopup(m_pagePopup.get());
3176 #endif
3177 }
3178
3179 void WebViewImpl::performCustomContextMenuAction(unsigned action)
3180 {
3181     if (!m_page)
3182         return;
3183     ContextMenu* menu = m_page->contextMenuController()->contextMenu();
3184     if (!menu)
3185         return;
3186     ContextMenuItem* item = menu->itemWithAction(static_cast<ContextMenuAction>(ContextMenuItemBaseCustomTag + action));
3187     if (item)
3188         m_page->contextMenuController()->contextMenuItemSelected(item);
3189     m_page->contextMenuController()->clearContextMenu();
3190 }
3191
3192 // WebView --------------------------------------------------------------------
3193
3194 void WebViewImpl::setIsTransparent(bool isTransparent)
3195 {
3196     // Set any existing frames to be transparent.
3197     Frame* frame = m_page->mainFrame();
3198     while (frame) {
3199         frame->view()->setTransparent(isTransparent);
3200         frame = frame->tree()->traverseNext();
3201     }
3202
3203     // Future frames check this to know whether to be transparent.
3204     m_isTransparent = isTransparent;
3205
3206     if (m_nonCompositedContentHost)
3207         m_nonCompositedContentHost->setOpaque(!isTransparent);
3208
3209     if (!m_layerTreeView.isNull())
3210         m_layerTreeView.setHasTransparentBackground(isTransparent);
3211 }
3212
3213 bool WebViewImpl::isTransparent() const
3214 {
3215     return m_isTransparent;
3216 }
3217
3218 void WebViewImpl::setIsActive(bool active)
3219 {
3220     if (page() && page()->focusController())
3221         page()->focusController()->setActive(active);
3222 }
3223
3224 bool WebViewImpl::isActive() const
3225 {
3226     return (page() && page()->focusController()) ? page()->focusController()->isActive() : false;
3227 }
3228
3229 void WebViewImpl::setDomainRelaxationForbidden(bool forbidden, const WebString& scheme)
3230 {
3231     SchemeRegistry::setDomainRelaxationForbiddenForURLScheme(forbidden, String(scheme));
3232 }
3233
3234 void WebViewImpl::setScrollbarColors(unsigned inactiveColor,
3235                                      unsigned activeColor,
3236                                      unsigned trackColor) {
3237 #if OS(UNIX) && !OS(DARWIN) && !OS(ANDROID)
3238     PlatformThemeChromiumLinux::setScrollbarColors(inactiveColor, activeColor, trackColor);
3239 #endif
3240 }
3241
3242 void WebViewImpl::setSelectionColors(unsigned activeBackgroundColor,
3243                                      unsigned activeForegroundColor,
3244                                      unsigned inactiveBackgroundColor,
3245                                      unsigned inactiveForegroundColor) {
3246 #if OS(UNIX) && !OS(DARWIN) && !OS(ANDROID)
3247     RenderThemeChromiumLinux::setSelectionColors(activeBackgroundColor,
3248                                                  activeForegroundColor,
3249                                                  inactiveBackgroundColor,
3250                                                  inactiveForegroundColor);
3251     theme()->platformColorsDidChange();
3252 #endif
3253 }
3254
3255 void WebView::addUserScript(const WebString& sourceCode,
3256                             const WebVector<WebString>& patternsIn,
3257                             WebView::UserScriptInjectAt injectAt,
3258                             WebView::UserContentInjectIn injectIn)
3259 {
3260     OwnPtr<Vector<String> > patterns = adoptPtr(new Vector<String>);
3261     for (size_t i = 0; i < patternsIn.size(); ++i)
3262         patterns->append(patternsIn[i]);
3263
3264     PageGroup* pageGroup = PageGroup::pageGroup(pageGroupName);
3265     RefPtr<DOMWrapperWorld> world(DOMWrapperWorld::create());
3266     pageGroup->addUserScriptToWorld(world.get(), sourceCode, WebURL(), patterns.release(), nullptr,
3267                                     static_cast<UserScriptInjectionTime>(injectAt),
3268                                     static_cast<UserContentInjectedFrames>(injectIn));
3269 }
3270
3271 void WebView::addUserStyleSheet(const WebString& sourceCode,
3272                                 const WebVector<WebString>& patternsIn,
3273                                 WebView::UserContentInjectIn injectIn,
3274                                 WebView::UserStyleInjectionTime injectionTime)
3275 {
3276     OwnPtr<Vector<String> > patterns = adoptPtr(new Vector<String>);
3277     for (size_t i = 0; i < patternsIn.size(); ++i)
3278         patterns->append(patternsIn[i]);
3279
3280     PageGroup* pageGroup = PageGroup::pageGroup(pageGroupName);
3281     RefPtr<DOMWrapperWorld> world(DOMWrapperWorld::create());
3282
3283     // FIXME: Current callers always want the level to be "author". It probably makes sense to let
3284     // callers specify this though, since in other cases the caller will probably want "user" level.
3285     //
3286     // FIXME: It would be nice to populate the URL correctly, instead of passing an empty URL.
3287     pageGroup->addUserStyleSheetToWorld(world.get(), sourceCode, WebURL(), patterns.release(), nullptr,
3288                                         static_cast<UserContentInjectedFrames>(injectIn),
3289                                         UserStyleAuthorLevel,
3290                                         static_cast<WebCore::UserStyleInjectionTime>(injectionTime));
3291 }
3292
3293 void WebView::removeAllUserContent()
3294 {
3295     PageGroup* pageGroup = PageGroup::pageGroup(pageGroupName);
3296     pageGroup->removeAllUserContent();
3297 }
3298
3299 void WebViewImpl::didCommitLoad(bool* isNewNavigation, bool isNavigationWithinPage)
3300 {
3301     if (isNewNavigation)
3302         *isNewNavigation = m_observedNewNavigation;
3303
3304 #ifndef NDEBUG
3305     ASSERT(!m_observedNewNavigation
3306         || m_page->mainFrame()->loader()->documentLoader() == m_newNavigationLoader);
3307     m_newNavigationLoader = 0;
3308 #endif
3309     m_observedNewNavigation = false;
3310     if (*isNewNavigation && !isNavigationWithinPage)
3311         m_pageScaleFactorIsSet = false;
3312
3313     m_gestureAnimation.clear();
3314     resetSavedScrollAndScaleState();
3315 }
3316
3317 void WebViewImpl::layoutUpdated(WebFrameImpl* webframe)
3318 {
3319     if (!m_client || webframe != mainFrameImpl())
3320         return;
3321
3322     if (m_shouldAutoResize && mainFrameImpl()->frame() && mainFrameImpl()->frame()->view()) {
3323         WebSize frameSize = mainFrameImpl()->frame()->view()->frameRect().size();
3324         if (frameSize != m_size) {
3325             m_size = frameSize;
3326             m_client->didAutoResize(m_size);
3327             sendResizeEventAndRepaint();
3328         }
3329     }
3330
3331     m_client->didUpdateLayout();
3332 }
3333
3334 void WebViewImpl::didChangeContentsSize()
3335 {
3336 #if ENABLE(VIEWPORT)
3337     if (!settings()->viewportEnabled())
3338         return;
3339
3340     bool didChangeScale = false;
3341     if (!isPageScaleFactorSet()) {
3342         // If the viewport tag failed to be processed earlier, we need
3343         // to recompute it now.
3344         ViewportArguments viewportArguments = mainFrameImpl()->frame()->document()->viewportArguments();
3345         m_page->chrome()->client()->dispatchViewportPropertiesDidChange(viewportArguments);
3346         didChangeScale = true;
3347     } else
3348         didChangeScale = computePageScaleFactorLimits();
3349
3350     if (!didChangeScale)
3351         return;
3352
3353     if (!mainFrameImpl())
3354         return;
3355
3356     FrameView* view = mainFrameImpl()->frameView();
3357     if (view && view->needsLayout())
3358         view->layout();
3359 #endif
3360 }
3361
3362 bool WebViewImpl::useExternalPopupMenus()
3363 {
3364     return shouldUseExternalPopupMenus;
3365 }
3366
3367 void WebViewImpl::setEmulatedTextZoomFactor(float textZoomFactor)
3368 {
3369     m_emulatedTextZoomFactor = textZoomFactor;
3370     Frame* frame = mainFrameImpl()->frame();
3371     if (frame)
3372         frame->setPageAndTextZoomFactors(frame->pageZoomFactor(), m_emulatedTextZoomFactor);
3373 }
3374
3375 bool WebViewImpl::navigationPolicyFromMouseEvent(unsigned short button,
3376                                                  bool ctrl, bool shift,
3377                                                  bool alt, bool meta,
3378                                                  WebNavigationPolicy* policy)
3379 {
3380 #if OS(DARWIN)
3381     const bool newTabModifier = (button == 1) || meta;
3382 #else
3383     const bool newTabModifier = (button == 1) || ctrl;
3384 #endif
3385     if (!newTabModifier && !shift && !alt)
3386       return false;
3387
3388     ASSERT(policy);
3389     if (newTabModifier) {
3390         if (shift)
3391           *policy = WebNavigationPolicyNewForegroundTab;
3392         else
3393           *policy = WebNavigationPolicyNewBackgroundTab;
3394     } else {
3395         if (shift)
3396           *policy = WebNavigationPolicyNewWindow;
3397         else
3398           *policy = WebNavigationPolicyDownload;
3399     }
3400     return true;
3401 }
3402
3403 void WebViewImpl::startDragging(Frame* frame,
3404                                 const WebDragData& dragData,
3405                                 WebDragOperationsMask mask,
3406                                 const WebImage& dragImage,
3407                                 const WebPoint& dragImageOffset)
3408 {
3409     if (!m_client)
3410         return;
3411     ASSERT(!m_doingDragAndDrop);
3412     m_doingDragAndDrop = true;
3413     m_client->startDragging(WebFrameImpl::fromFrame(frame), dragData, mask, dragImage, dragImageOffset);
3414 }
3415
3416 void WebViewImpl::observeNewNavigation()
3417 {
3418     m_observedNewNavigation = true;
3419 #ifndef NDEBUG
3420     m_newNavigationLoader = m_page->mainFrame()->loader()->documentLoader();
3421 #endif
3422 }
3423
3424 void WebViewImpl::setIgnoreInputEvents(bool newValue)
3425 {
3426     ASSERT(m_ignoreInputEvents != newValue);
3427     m_ignoreInputEvents = newValue;
3428 }
3429
3430 void WebViewImpl::addPageOverlay(WebPageOverlay* overlay, int zOrder)
3431 {
3432     if (!m_pageOverlays)
3433         m_pageOverlays = PageOverlayList::create(this);
3434
3435     m_pageOverlays->add(overlay, zOrder);
3436 }
3437
3438 void WebViewImpl::removePageOverlay(WebPageOverlay* overlay)
3439 {
3440     if (m_pageOverlays && m_pageOverlays->remove(overlay) && m_pageOverlays->empty())
3441         m_pageOverlays = nullptr;
3442 }
3443
3444 void WebViewImpl::setOverlayLayer(WebCore::GraphicsLayer* layer)
3445 {
3446     if (m_rootGraphicsLayer) {
3447         if (layer->parent() != m_rootGraphicsLayer)
3448             m_rootGraphicsLayer->addChild(layer);
3449     }
3450 }
3451
3452 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
3453 NotificationPresenterImpl* WebViewImpl::notificationPresenterImpl()
3454 {
3455     if (!m_notificationPresenter.isInitialized() && m_client)
3456         m_notificationPresenter.initialize(m_client->notificationPresenter());
3457     return &m_notificationPresenter;
3458 }
3459 #endif
3460
3461 void WebViewImpl::refreshAutofillPopup()
3462 {
3463     ASSERT(m_autofillPopupShowing);
3464
3465     // Hide the popup if it has become empty.
3466     if (!m_autofillPopupClient->listSize()) {
3467         hideAutofillPopup();
3468         return;
3469     }
3470
3471     WebRect newWidgetRect = m_autofillPopup->refresh(focusedWebCoreNode()->getPixelSnappedRect());
3472     // Let's resize the backing window if necessary.
3473     WebPopupMenuImpl* popupMenu = static_cast<WebPopupMenuImpl*>(m_autofillPopup->client());
3474     if (popupMenu && popupMenu->client()->windowRect() != newWidgetRect)
3475         popupMenu->client()->setWindowRect(newWidgetRect);
3476 }
3477
3478 Node* WebViewImpl::focusedWebCoreNode()
3479 {
3480     Frame* frame = m_page->focusController()->focusedFrame();
3481     if (!frame)
3482         return 0;
3483
3484     Document* document = frame->document();
3485     if (!document)
3486         return 0;
3487
3488     return document->focusedNode();
3489 }
3490
3491 HitTestResult WebViewImpl::hitTestResultForWindowPos(const IntPoint& pos)
3492 {
3493     IntPoint docPoint(m_page->mainFrame()->view()->windowToContents(pos));
3494     return m_page->mainFrame()->eventHandler()->hitTestResultAtPoint(docPoint, false);
3495 }
3496
3497 void WebViewImpl::setTabsToLinks(bool enable)
3498 {
3499     m_tabsToLinks = enable;
3500 }
3501
3502 bool WebViewImpl::tabsToL