771d5616dcd47c32937292609b4d284f30151da9
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / ChromeClientImpl.cpp
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "config.h"
33 #include "web/ChromeClientImpl.h"
34
35 #include "bindings/v8/ScriptController.h"
36 #include "core/HTMLNames.h"
37 #include "core/accessibility/AXObject.h"
38 #include "core/accessibility/AXObjectCache.h"
39 #include "core/dom/Document.h"
40 #include "core/dom/DocumentFullscreen.h"
41 #include "core/dom/Node.h"
42 #include "core/events/KeyboardEvent.h"
43 #include "core/events/MouseEvent.h"
44 #include "core/events/WheelEvent.h"
45 #include "core/frame/Console.h"
46 #include "core/frame/FrameView.h"
47 #include "core/frame/Settings.h"
48 #include "core/html/HTMLInputElement.h"
49 #include "core/loader/DocumentLoader.h"
50 #include "core/loader/FrameLoadRequest.h"
51 #include "core/page/Page.h"
52 #include "core/page/PagePopupDriver.h"
53 #include "core/page/WindowFeatures.h"
54 #include "core/rendering/HitTestResult.h"
55 #include "core/rendering/RenderWidget.h"
56 #include "platform/ColorChooser.h"
57 #include "platform/ColorChooserClient.h"
58 #include "platform/Cursor.h"
59 #include "platform/DateTimeChooser.h"
60 #include "platform/FileChooser.h"
61 #include "platform/NotImplemented.h"
62 #include "platform/PlatformScreen.h"
63 #include "platform/RuntimeEnabledFeatures.h"
64 #include "platform/exported/WrappedResourceRequest.h"
65 #include "platform/geometry/FloatRect.h"
66 #include "platform/geometry/IntRect.h"
67 #include "platform/graphics/GraphicsLayer.h"
68 #include "platform/weborigin/SecurityOrigin.h"
69 #include "public/platform/Platform.h"
70 #include "public/platform/WebCursorInfo.h"
71 #include "public/platform/WebRect.h"
72 #include "public/platform/WebURLRequest.h"
73 #include "public/web/WebAXObject.h"
74 #include "public/web/WebAutofillClient.h"
75 #include "public/web/WebColorChooser.h"
76 #include "public/web/WebColorSuggestion.h"
77 #include "public/web/WebConsoleMessage.h"
78 #include "public/web/WebFrameClient.h"
79 #include "public/web/WebInputElement.h"
80 #include "public/web/WebInputEvent.h"
81 #include "public/web/WebKit.h"
82 #include "public/web/WebNode.h"
83 #include "public/web/WebPlugin.h"
84 #include "public/web/WebPopupMenuInfo.h"
85 #include "public/web/WebSettings.h"
86 #include "public/web/WebTextDirection.h"
87 #include "public/web/WebTouchAction.h"
88 #include "public/web/WebUserGestureIndicator.h"
89 #include "public/web/WebUserGestureToken.h"
90 #include "public/web/WebViewClient.h"
91 #include "public/web/WebWindowFeatures.h"
92 #include "web/ColorChooserPopupUIController.h"
93 #include "web/ColorChooserUIController.h"
94 #include "web/DateTimeChooserImpl.h"
95 #include "web/ExternalDateTimeChooser.h"
96 #include "web/ExternalPopupMenu.h"
97 #include "web/PopupMenuChromium.h"
98 #include "web/WebFileChooserCompletionImpl.h"
99 #include "web/WebInputEventConversion.h"
100 #include "web/WebLocalFrameImpl.h"
101 #include "web/WebPluginContainerImpl.h"
102 #include "web/WebPopupMenuImpl.h"
103 #include "web/WebSettingsImpl.h"
104 #include "web/WebViewImpl.h"
105 #include "wtf/text/CString.h"
106 #include "wtf/text/StringBuilder.h"
107 #include "wtf/text/StringConcatenate.h"
108 #include "wtf/unicode/CharacterNames.h"
109
110 using namespace WebCore;
111
112 namespace blink {
113
114 // Converts a WebCore::AXObjectCache::AXNotification to a blink::WebAXEvent
115 static WebAXEvent toWebAXEvent(AXObjectCache::AXNotification notification)
116 {
117     // These enums have the same values; enforced in AssertMatchingEnums.cpp.
118     return static_cast<WebAXEvent>(notification);
119 }
120
121 ChromeClientImpl::ChromeClientImpl(WebViewImpl* webView)
122     : m_webView(webView)
123     , m_toolbarsVisible(true)
124     , m_statusbarVisible(true)
125     , m_scrollbarsVisible(true)
126     , m_menubarVisible(true)
127     , m_resizable(true)
128     , m_pagePopupDriver(webView)
129 {
130 }
131
132 ChromeClientImpl::~ChromeClientImpl()
133 {
134 }
135
136 void* ChromeClientImpl::webView() const
137 {
138     return static_cast<void*>(m_webView);
139 }
140
141 void ChromeClientImpl::chromeDestroyed()
142 {
143     // Our lifetime is bound to the WebViewImpl.
144 }
145
146 void ChromeClientImpl::setWindowRect(const FloatRect& r)
147 {
148     if (m_webView->client())
149         m_webView->client()->setWindowRect(IntRect(r));
150 }
151
152 FloatRect ChromeClientImpl::windowRect()
153 {
154     WebRect rect;
155     if (m_webView->client())
156         rect = m_webView->client()->rootWindowRect();
157     else {
158         // These numbers will be fairly wrong. The window's x/y coordinates will
159         // be the top left corner of the screen and the size will be the content
160         // size instead of the window size.
161         rect.width = m_webView->size().width;
162         rect.height = m_webView->size().height;
163     }
164     return FloatRect(rect);
165 }
166
167 FloatRect ChromeClientImpl::pageRect()
168 {
169     // We hide the details of the window's border thickness from the web page by
170     // simple re-using the window position here.  So, from the point-of-view of
171     // the web page, the window has no border.
172     return windowRect();
173 }
174
175 void ChromeClientImpl::focus()
176 {
177     if (m_webView->client())
178         m_webView->client()->didFocus();
179 }
180
181 bool ChromeClientImpl::canTakeFocus(FocusType)
182 {
183     // For now the browser can always take focus if we're not running layout
184     // tests.
185     return !layoutTestMode();
186 }
187
188 void ChromeClientImpl::takeFocus(FocusType type)
189 {
190     if (!m_webView->client())
191         return;
192     if (type == FocusTypeBackward)
193         m_webView->client()->focusPrevious();
194     else
195         m_webView->client()->focusNext();
196 }
197
198 void ChromeClientImpl::focusedNodeChanged(Node* node)
199 {
200     m_webView->client()->focusedNodeChanged(WebNode(node));
201
202     WebURL focusURL;
203     if (node && node->isLink()) {
204         // This HitTestResult hack is the easiest way to get a link URL out of a
205         // WebCore::Node.
206         HitTestResult hitTest(IntPoint(0, 0));
207         // This cast must be valid because of the isLink() check.
208         hitTest.setURLElement(toElement(node));
209         if (hitTest.isLiveLink())
210             focusURL = hitTest.absoluteLinkURL();
211     }
212     m_webView->client()->setKeyboardFocusURL(focusURL);
213 }
214
215 void ChromeClientImpl::focusedFrameChanged(WebCore::LocalFrame* frame)
216 {
217     WebLocalFrameImpl* webframe = WebLocalFrameImpl::fromFrame(frame);
218     if (webframe && webframe->client())
219         webframe->client()->frameFocused();
220 }
221
222 Page* ChromeClientImpl::createWindow(LocalFrame* frame, const FrameLoadRequest& r, const WindowFeatures& features,
223     NavigationPolicy navigationPolicy, ShouldSendReferrer shouldSendReferrer)
224 {
225     if (!m_webView->client())
226         return 0;
227
228     WebNavigationPolicy policy = static_cast<WebNavigationPolicy>(navigationPolicy);
229     if (policy == WebNavigationPolicyIgnore)
230         policy = getNavigationPolicy();
231
232     ASSERT(frame->document());
233     DocumentFullscreen::webkitCancelFullScreen(*frame->document());
234
235     WebViewImpl* newView = toWebViewImpl(
236         m_webView->client()->createView(WebLocalFrameImpl::fromFrame(frame), WrappedResourceRequest(r.resourceRequest()), features, r.frameName(), policy, shouldSendReferrer == NeverSendReferrer));
237     if (!newView)
238         return 0;
239     return newView->page();
240 }
241
242 static inline void updatePolicyForEvent(const WebInputEvent* inputEvent, NavigationPolicy* policy)
243 {
244     if (!inputEvent || inputEvent->type != WebInputEvent::MouseUp)
245         return;
246
247     const WebMouseEvent* mouseEvent = static_cast<const WebMouseEvent*>(inputEvent);
248
249     unsigned short buttonNumber;
250     switch (mouseEvent->button) {
251     case WebMouseEvent::ButtonLeft:
252         buttonNumber = 0;
253         break;
254     case WebMouseEvent::ButtonMiddle:
255         buttonNumber = 1;
256         break;
257     case WebMouseEvent::ButtonRight:
258         buttonNumber = 2;
259         break;
260     default:
261         return;
262     }
263     bool ctrl = mouseEvent->modifiers & WebMouseEvent::ControlKey;
264     bool shift = mouseEvent->modifiers & WebMouseEvent::ShiftKey;
265     bool alt = mouseEvent->modifiers & WebMouseEvent::AltKey;
266     bool meta = mouseEvent->modifiers & WebMouseEvent::MetaKey;
267
268     NavigationPolicy userPolicy = *policy;
269     navigationPolicyFromMouseEvent(buttonNumber, ctrl, shift, alt, meta, &userPolicy);
270     // User and app agree that we want a new window; let the app override the decorations.
271     if (userPolicy == NavigationPolicyNewWindow && *policy == NavigationPolicyNewPopup)
272         return;
273     *policy = userPolicy;
274 }
275
276 WebNavigationPolicy ChromeClientImpl::getNavigationPolicy()
277 {
278     // If our default configuration was modified by a script or wasn't
279     // created by a user gesture, then show as a popup. Else, let this
280     // new window be opened as a toplevel window.
281     bool asPopup = !m_toolbarsVisible
282         || !m_statusbarVisible
283         || !m_scrollbarsVisible
284         || !m_menubarVisible
285         || !m_resizable;
286
287     NavigationPolicy policy = NavigationPolicyNewForegroundTab;
288     if (asPopup)
289         policy = NavigationPolicyNewPopup;
290     updatePolicyForEvent(WebViewImpl::currentInputEvent(), &policy);
291
292     return static_cast<WebNavigationPolicy>(policy);
293 }
294
295 void ChromeClientImpl::show(NavigationPolicy navigationPolicy)
296 {
297     if (!m_webView->client())
298         return;
299
300     WebNavigationPolicy policy = static_cast<WebNavigationPolicy>(navigationPolicy);
301     if (policy == WebNavigationPolicyIgnore)
302         policy = getNavigationPolicy();
303     m_webView->client()->show(policy);
304 }
305
306 bool ChromeClientImpl::canRunModal()
307 {
308     return !!m_webView->client();
309 }
310
311 void ChromeClientImpl::runModal()
312 {
313     if (m_webView->client())
314         m_webView->client()->runModal();
315 }
316
317 void ChromeClientImpl::setToolbarsVisible(bool value)
318 {
319     m_toolbarsVisible = value;
320 }
321
322 bool ChromeClientImpl::toolbarsVisible()
323 {
324     return m_toolbarsVisible;
325 }
326
327 void ChromeClientImpl::setStatusbarVisible(bool value)
328 {
329     m_statusbarVisible = value;
330 }
331
332 bool ChromeClientImpl::statusbarVisible()
333 {
334     return m_statusbarVisible;
335 }
336
337 void ChromeClientImpl::setScrollbarsVisible(bool value)
338 {
339     m_scrollbarsVisible = value;
340     WebLocalFrameImpl* webFrame = toWebLocalFrameImpl(m_webView->mainFrame());
341     if (webFrame)
342         webFrame->setCanHaveScrollbars(value);
343 }
344
345 bool ChromeClientImpl::scrollbarsVisible()
346 {
347     return m_scrollbarsVisible;
348 }
349
350 void ChromeClientImpl::setMenubarVisible(bool value)
351 {
352     m_menubarVisible = value;
353 }
354
355 bool ChromeClientImpl::menubarVisible()
356 {
357     return m_menubarVisible;
358 }
359
360 void ChromeClientImpl::setResizable(bool value)
361 {
362     m_resizable = value;
363 }
364
365 bool ChromeClientImpl::shouldReportDetailedMessageForSource(const String& url)
366 {
367     WebLocalFrameImpl* webframe = m_webView->mainFrameImpl();
368     return webframe->client() && webframe->client()->shouldReportDetailedMessageForSource(url);
369 }
370
371 void ChromeClientImpl::addMessageToConsole(LocalFrame* localFrame, MessageSource source, MessageLevel level, const String& message, unsigned lineNumber, const String& sourceID, const String& stackTrace)
372 {
373     WebLocalFrameImpl* frame = WebLocalFrameImpl::fromFrame(localFrame);
374     if (frame && frame->client()) {
375         frame->client()->didAddMessageToConsole(
376             WebConsoleMessage(static_cast<WebConsoleMessage::Level>(level), message),
377             sourceID,
378             lineNumber,
379             stackTrace);
380     }
381 }
382
383 bool ChromeClientImpl::canRunBeforeUnloadConfirmPanel()
384 {
385     return !!m_webView->client();
386 }
387
388 bool ChromeClientImpl::runBeforeUnloadConfirmPanel(const String& message, LocalFrame* frame)
389 {
390     WebLocalFrameImpl* webframe = WebLocalFrameImpl::fromFrame(frame);
391
392     bool isReload = false;
393     WebDataSource* ds = webframe->provisionalDataSource();
394     if (ds)
395         isReload = (ds->navigationType() == blink::WebNavigationTypeReload);
396
397     if (webframe->client())
398         return webframe->client()->runModalBeforeUnloadDialog(isReload, message);
399     return false;
400 }
401
402 void ChromeClientImpl::closeWindowSoon()
403 {
404     // Make sure this Page can no longer be found by JS.
405     Page::ordinaryPages().remove(m_webView->page());
406
407     // Make sure that all loading is stopped.  Ensures that JS stops executing!
408     m_webView->mainFrame()->stopLoading();
409
410     if (m_webView->client())
411         m_webView->client()->closeWidgetSoon();
412 }
413
414 // Although a LocalFrame is passed in, we don't actually use it, since we
415 // already know our own m_webView.
416 void ChromeClientImpl::runJavaScriptAlert(LocalFrame* frame, const String& message)
417 {
418     WebLocalFrameImpl* webframe = WebLocalFrameImpl::fromFrame(frame);
419     if (webframe->client()) {
420         if (WebUserGestureIndicator::isProcessingUserGesture())
421             WebUserGestureIndicator::currentUserGestureToken().setJavascriptPrompt();
422         webframe->client()->runModalAlertDialog(message);
423     }
424 }
425
426 // See comments for runJavaScriptAlert().
427 bool ChromeClientImpl::runJavaScriptConfirm(LocalFrame* frame, const String& message)
428 {
429     WebLocalFrameImpl* webframe = WebLocalFrameImpl::fromFrame(frame);
430     if (webframe->client()) {
431         if (WebUserGestureIndicator::isProcessingUserGesture())
432             WebUserGestureIndicator::currentUserGestureToken().setJavascriptPrompt();
433         return webframe->client()->runModalConfirmDialog(message);
434     }
435     return false;
436 }
437
438 // See comments for runJavaScriptAlert().
439 bool ChromeClientImpl::runJavaScriptPrompt(LocalFrame* frame,
440                                            const String& message,
441                                            const String& defaultValue,
442                                            String& result)
443 {
444     WebLocalFrameImpl* webframe = WebLocalFrameImpl::fromFrame(frame);
445     if (webframe->client()) {
446         if (WebUserGestureIndicator::isProcessingUserGesture())
447             WebUserGestureIndicator::currentUserGestureToken().setJavascriptPrompt();
448         WebString actualValue;
449         bool ok = webframe->client()->runModalPromptDialog(
450             message,
451             defaultValue,
452             &actualValue);
453         if (ok)
454             result = actualValue;
455         return ok;
456     }
457     return false;
458 }
459
460 void ChromeClientImpl::setStatusbarText(const String& message)
461 {
462     if (m_webView->client())
463         m_webView->client()->setStatusText(message);
464 }
465
466 bool ChromeClientImpl::tabsToLinks()
467 {
468     return m_webView->tabsToLinks();
469 }
470
471 IntRect ChromeClientImpl::windowResizerRect() const
472 {
473     IntRect result;
474     if (m_webView->client())
475         result = m_webView->client()->windowResizerRect();
476     return result;
477 }
478
479 void ChromeClientImpl::invalidateContentsAndRootView(const IntRect& updateRect)
480 {
481     if (updateRect.isEmpty())
482         return;
483     m_webView->invalidateRect(updateRect);
484 }
485
486 void ChromeClientImpl::invalidateContentsForSlowScroll(const IntRect& updateRect)
487 {
488     invalidateContentsAndRootView(updateRect);
489 }
490
491 void ChromeClientImpl::scheduleAnimation()
492 {
493     m_webView->scheduleAnimation();
494 }
495
496 void ChromeClientImpl::scroll(
497     const IntSize& scrollDelta, const IntRect& scrollRect,
498     const IntRect& clipRect)
499 {
500     if (!m_webView->isAcceleratedCompositingActive()) {
501         if (m_webView->client()) {
502             int dx = scrollDelta.width();
503             int dy = scrollDelta.height();
504             m_webView->client()->didScrollRect(dx, dy, intersection(scrollRect, clipRect));
505         }
506     } else {
507         m_webView->scrollRootLayer();
508     }
509 }
510
511 IntRect ChromeClientImpl::rootViewToScreen(const IntRect& rect) const
512 {
513     IntRect screenRect(rect);
514
515     if (m_webView->client()) {
516         WebRect windowRect = m_webView->client()->windowRect();
517         screenRect.move(windowRect.x, windowRect.y);
518     }
519
520     return screenRect;
521 }
522
523 WebScreenInfo ChromeClientImpl::screenInfo() const
524 {
525     return m_webView->client() ? m_webView->client()->screenInfo() : WebScreenInfo();
526 }
527
528 void ChromeClientImpl::contentsSizeChanged(LocalFrame* frame, const IntSize& size) const
529 {
530     m_webView->didChangeContentsSize();
531
532     WebLocalFrameImpl* webframe = WebLocalFrameImpl::fromFrame(frame);
533     webframe->didChangeContentsSize(size);
534
535     frame->loader().restoreScrollPositionAndViewState();
536 }
537
538 void ChromeClientImpl::deviceOrPageScaleFactorChanged() const
539 {
540     m_webView->deviceOrPageScaleFactorChanged();
541 }
542
543 void ChromeClientImpl::layoutUpdated(LocalFrame* frame) const
544 {
545     m_webView->layoutUpdated(WebLocalFrameImpl::fromFrame(frame));
546 }
547
548 void ChromeClientImpl::mouseDidMoveOverElement(
549     const HitTestResult& result, unsigned modifierFlags)
550 {
551     if (!m_webView->client())
552         return;
553
554     WebURL url;
555     // Find out if the mouse is over a link, and if so, let our UI know...
556     if (result.isLiveLink() && !result.absoluteLinkURL().string().isEmpty()) {
557         url = result.absoluteLinkURL();
558     } else if (result.innerNonSharedNode()
559         && (isHTMLObjectElement(*result.innerNonSharedNode())
560             || isHTMLEmbedElement(*result.innerNonSharedNode()))) {
561         RenderObject* object = result.innerNonSharedNode()->renderer();
562         if (object && object->isWidget()) {
563             Widget* widget = toRenderWidget(object)->widget();
564             if (widget && widget->isPluginContainer()) {
565                 WebPluginContainerImpl* plugin = toWebPluginContainerImpl(widget);
566                 url = plugin->plugin()->linkAtPosition(result.roundedPointInInnerNodeFrame());
567             }
568         }
569     }
570
571     m_webView->client()->setMouseOverURL(url);
572 }
573
574 void ChromeClientImpl::setToolTip(const String& tooltipText, TextDirection dir)
575 {
576     if (!m_webView->client())
577         return;
578     WebTextDirection textDirection = (dir == RTL) ?
579         WebTextDirectionRightToLeft :
580         WebTextDirectionLeftToRight;
581     m_webView->client()->setToolTipText(
582         tooltipText, textDirection);
583 }
584
585 void ChromeClientImpl::dispatchViewportPropertiesDidChange(const ViewportDescription& description) const
586 {
587     m_webView->updatePageDefinedViewportConstraints(description);
588 }
589
590 void ChromeClientImpl::print(LocalFrame* frame)
591 {
592     if (m_webView->client())
593         m_webView->client()->printPage(WebLocalFrameImpl::fromFrame(frame));
594 }
595
596 PassOwnPtr<ColorChooser> ChromeClientImpl::createColorChooser(LocalFrame* frame, ColorChooserClient* chooserClient, const Color&)
597 {
598     OwnPtr<ColorChooserUIController> controller;
599     if (RuntimeEnabledFeatures::pagePopupEnabled())
600         controller = adoptPtr(new ColorChooserPopupUIController(frame, this, chooserClient));
601     else
602         controller = adoptPtr(new ColorChooserUIController(frame, chooserClient));
603     controller->openUI();
604     return controller.release();
605 }
606
607 PassRefPtrWillBeRawPtr<DateTimeChooser> ChromeClientImpl::openDateTimeChooser(DateTimeChooserClient* pickerClient, const DateTimeChooserParameters& parameters)
608 {
609 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
610     return DateTimeChooserImpl::create(this, pickerClient, parameters);
611 #else
612     return ExternalDateTimeChooser::create(this, m_webView->client(), pickerClient, parameters);
613 #endif
614 }
615
616 void ChromeClientImpl::runOpenPanel(LocalFrame* frame, PassRefPtr<FileChooser> fileChooser)
617 {
618     WebViewClient* client = m_webView->client();
619     if (!client)
620         return;
621
622     WebFileChooserParams params;
623     params.multiSelect = fileChooser->settings().allowsMultipleFiles;
624     params.directory = fileChooser->settings().allowsDirectoryUpload;
625     params.acceptTypes = fileChooser->settings().acceptTypes();
626     params.selectedFiles = fileChooser->settings().selectedFiles;
627     if (params.selectedFiles.size() > 0)
628         params.initialValue = params.selectedFiles[0];
629 #if ENABLE(MEDIA_CAPTURE)
630     params.useMediaCapture = fileChooser->settings().useMediaCapture;
631 #endif
632     WebFileChooserCompletionImpl* chooserCompletion =
633         new WebFileChooserCompletionImpl(fileChooser);
634
635     if (client->runFileChooser(params, chooserCompletion))
636         return;
637
638     // Choosing failed, so do callback with an empty list.
639     chooserCompletion->didChooseFile(WebVector<WebString>());
640 }
641
642 void ChromeClientImpl::enumerateChosenDirectory(FileChooser* fileChooser)
643 {
644     WebViewClient* client = m_webView->client();
645     if (!client)
646         return;
647
648     WebFileChooserCompletionImpl* chooserCompletion =
649         new WebFileChooserCompletionImpl(fileChooser);
650
651     ASSERT(fileChooser && fileChooser->settings().selectedFiles.size());
652
653     // If the enumeration can't happen, call the callback with an empty list.
654     if (!client->enumerateChosenDirectory(fileChooser->settings().selectedFiles[0], chooserCompletion))
655         chooserCompletion->didChooseFile(WebVector<WebString>());
656 }
657
658 void ChromeClientImpl::setCursor(const WebCore::Cursor& cursor)
659 {
660     setCursor(WebCursorInfo(cursor));
661 }
662
663 void ChromeClientImpl::setCursor(const WebCursorInfo& cursor)
664 {
665 #if OS(MACOSX)
666     // On Mac the mousemove event propagates to both the popup and main window.
667     // If a popup is open we don't want the main window to change the cursor.
668     if (m_webView->hasOpenedPopup())
669         return;
670 #endif
671     if (m_webView->client())
672         m_webView->client()->didChangeCursor(cursor);
673 }
674
675 void ChromeClientImpl::setCursorForPlugin(const WebCursorInfo& cursor)
676 {
677     setCursor(cursor);
678 }
679
680 void ChromeClientImpl::postAccessibilityNotification(AXObject* obj, AXObjectCache::AXNotification notification)
681 {
682     // Alert assistive technology about the accessibility object notification.
683     if (!obj)
684         return;
685     if (m_webView->client())
686         m_webView->client()->postAccessibilityEvent(WebAXObject(obj), toWebAXEvent(notification));
687 }
688
689 String ChromeClientImpl::acceptLanguages()
690 {
691     return m_webView->client()->acceptLanguages();
692 }
693
694 bool ChromeClientImpl::paintCustomOverhangArea(GraphicsContext* context, const IntRect& horizontalOverhangArea, const IntRect& verticalOverhangArea, const IntRect& dirtyRect)
695 {
696     LocalFrame* frame = m_webView->mainFrameImpl()->frame();
697     WebPluginContainerImpl* pluginContainer = WebLocalFrameImpl::pluginContainerFromFrame(frame);
698     if (pluginContainer)
699         return pluginContainer->paintCustomOverhangArea(context, horizontalOverhangArea, verticalOverhangArea, dirtyRect);
700     return false;
701 }
702
703 GraphicsLayerFactory* ChromeClientImpl::graphicsLayerFactory() const
704 {
705     return m_webView->graphicsLayerFactory();
706 }
707
708 void ChromeClientImpl::attachRootGraphicsLayer(GraphicsLayer* rootLayer)
709 {
710     m_webView->setRootGraphicsLayer(rootLayer);
711 }
712
713 void ChromeClientImpl::enterFullScreenForElement(Element* element)
714 {
715     m_webView->enterFullScreenForElement(element);
716 }
717
718 void ChromeClientImpl::exitFullScreenForElement(Element* element)
719 {
720     m_webView->exitFullScreenForElement(element);
721 }
722
723 bool ChromeClientImpl::hasOpenedPopup() const
724 {
725     return m_webView->hasOpenedPopup();
726 }
727
728 PassRefPtr<PopupMenu> ChromeClientImpl::createPopupMenu(LocalFrame& frame, PopupMenuClient* client) const
729 {
730     if (WebViewImpl::useExternalPopupMenus())
731         return adoptRef(new ExternalPopupMenu(frame, client, *m_webView));
732
733     return adoptRef(new PopupMenuChromium(frame, client));
734 }
735
736 PagePopup* ChromeClientImpl::openPagePopup(PagePopupClient* client, const IntRect& originBoundsInRootView)
737 {
738     ASSERT(m_pagePopupDriver);
739     return m_pagePopupDriver->openPagePopup(client, originBoundsInRootView);
740 }
741
742 void ChromeClientImpl::closePagePopup(PagePopup* popup)
743 {
744     ASSERT(m_pagePopupDriver);
745     m_pagePopupDriver->closePagePopup(popup);
746 }
747
748 void ChromeClientImpl::setPagePopupDriver(PagePopupDriver* driver)
749 {
750     ASSERT(driver);
751     m_pagePopupDriver = driver;
752 }
753
754 void ChromeClientImpl::resetPagePopupDriver()
755 {
756     m_pagePopupDriver = m_webView;
757 }
758
759 bool ChromeClientImpl::shouldRunModalDialogDuringPageDismissal(const DialogType& dialogType, const String& dialogMessage, Document::PageDismissalType dismissalType) const
760 {
761     const char* kDialogs[] = {"alert", "confirm", "prompt", "showModalDialog"};
762     int dialog = static_cast<int>(dialogType);
763     ASSERT_WITH_SECURITY_IMPLICATION(0 <= dialog && dialog < static_cast<int>(arraysize(kDialogs)));
764
765     const char* kDismissals[] = {"beforeunload", "pagehide", "unload"};
766     int dismissal = static_cast<int>(dismissalType) - 1; // Exclude NoDismissal.
767     ASSERT_WITH_SECURITY_IMPLICATION(0 <= dismissal && dismissal < static_cast<int>(arraysize(kDismissals)));
768
769     blink::Platform::current()->histogramEnumeration("Renderer.ModalDialogsDuringPageDismissal", dismissal * arraysize(kDialogs) + dialog, arraysize(kDialogs) * arraysize(kDismissals));
770
771     String message = String("Blocked ") + kDialogs[dialog] + "('" + dialogMessage + "') during " + kDismissals[dismissal] + ".";
772     m_webView->mainFrame()->addMessageToConsole(WebConsoleMessage(WebConsoleMessage::LevelError, message));
773
774     return false;
775 }
776
777 void ChromeClientImpl::needTouchEvents(bool needsTouchEvents)
778 {
779     m_webView->hasTouchEventHandlers(needsTouchEvents);
780 }
781
782 void ChromeClientImpl::setTouchAction(TouchAction touchAction)
783 {
784     if (WebViewClient* client = m_webView->client()) {
785         WebTouchAction webTouchAction = static_cast<WebTouchAction>(touchAction);
786         client->setTouchAction(webTouchAction);
787     }
788 }
789
790 bool ChromeClientImpl::requestPointerLock()
791 {
792     return m_webView->requestPointerLock();
793 }
794
795 void ChromeClientImpl::requestPointerUnlock()
796 {
797     return m_webView->requestPointerUnlock();
798 }
799
800 void ChromeClientImpl::annotatedRegionsChanged()
801 {
802     WebViewClient* client = m_webView->client();
803     if (client)
804         client->draggableRegionsChanged();
805 }
806
807 void ChromeClientImpl::didAssociateFormControls(const WillBeHeapVector<RefPtrWillBeMember<Element> >& elements)
808 {
809     if (m_webView->autofillClient())
810         m_webView->autofillClient()->didAssociateFormControls(elements);
811 }
812
813 void ChromeClientImpl::didCancelCompositionOnSelectionChange()
814 {
815     if (m_webView->client())
816         m_webView->client()->didCancelCompositionOnSelectionChange();
817 }
818
819 void ChromeClientImpl::willSetInputMethodState()
820 {
821     if (m_webView->client())
822         m_webView->client()->resetInputMethod();
823 }
824
825 void ChromeClientImpl::didUpdateTextOfFocusedElementByNonUserInput()
826 {
827     if (m_webView->client())
828         m_webView->client()->didUpdateTextOfFocusedElementByNonUserInput();
829 }
830
831 void ChromeClientImpl::handleKeyboardEventOnTextField(HTMLInputElement& inputElement, KeyboardEvent& event)
832 {
833     if (!m_webView->autofillClient())
834         return;
835     m_webView->autofillClient()->textFieldDidReceiveKeyDown(WebInputElement(&inputElement), WebKeyboardEventBuilder(event));
836 }
837
838 // FIXME: Remove this code once we have input routing in the browser
839 // process. See http://crbug.com/339659.
840 void ChromeClientImpl::forwardInputEvent(
841     WebCore::Frame* frame, WebCore::Event* event)
842 {
843     WebLocalFrameImpl* webFrame = WebLocalFrameImpl::fromFrame(toLocalFrameTemporary(frame));
844
845     // This is only called when we have out-of-process iframes, which
846     // need to forward input events across processes.
847     // FIXME: Add a check for out-of-process iframes enabled.
848     if (event->isKeyboardEvent()) {
849         WebKeyboardEventBuilder webEvent(*static_cast<WebCore::KeyboardEvent*>(event));
850         webFrame->client()->forwardInputEvent(&webEvent);
851     } else if (event->isMouseEvent()) {
852         WebMouseEventBuilder webEvent(webFrame->frameView(), frame->ownerRenderer(), *static_cast<WebCore::MouseEvent*>(event));
853         // Internal Blink events should not be forwarded.
854         if (webEvent.type == WebInputEvent::Undefined)
855             return;
856
857         webFrame->client()->forwardInputEvent(&webEvent);
858     } else if (event->isWheelEvent()) {
859         WebMouseWheelEventBuilder webEvent(webFrame->frameView(), frame->ownerRenderer(), *static_cast<WebCore::WheelEvent*>(event));
860         if (webEvent.type == WebInputEvent::Undefined)
861             return;
862         webFrame->client()->forwardInputEvent(&webEvent);
863     }
864 }
865
866 void ChromeClientImpl::didChangeValueInTextField(HTMLFormControlElement& element)
867 {
868     if (!m_webView->autofillClient())
869         return;
870     m_webView->autofillClient()->textFieldDidChange(WebFormControlElement(&element));
871 }
872
873 void ChromeClientImpl::didEndEditingOnTextField(HTMLInputElement& inputElement)
874 {
875     if (m_webView->autofillClient())
876         m_webView->autofillClient()->textFieldDidEndEditing(WebInputElement(&inputElement));
877 }
878
879 void ChromeClientImpl::openTextDataListChooser(HTMLInputElement& input)
880 {
881     if (m_webView->autofillClient())
882         m_webView->autofillClient()->openTextDataListChooser(WebInputElement(&input));
883 }
884
885 bool ChromeClientImpl::usesGpuRasterization()
886 {
887     return m_webView->layerTreeView()->usesGpuRasterization();
888 }
889
890 } // namespace blink