2 * Copyright (C) 2006, 2007, 2009, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
24 #include "ChromeClient.h"
27 #include "FileIconLoader.h"
28 #include "FileChooser.h"
30 #include "FloatRect.h"
32 #include "FrameTree.h"
33 #include "Geolocation.h"
34 #include "HTMLFormElement.h"
35 #include "HTMLInputElement.h"
36 #include "HTMLNames.h"
37 #include "HitTestResult.h"
39 #include "InspectorInstrumentation.h"
41 #include "PageGroupLoadDeferrer.h"
42 #include "RenderObject.h"
43 #include "ResourceHandle.h"
44 #include "SecurityOrigin.h"
46 #include "WindowFeatures.h"
47 #include <wtf/PassRefPtr.h>
48 #include <wtf/RefPtr.h>
49 #include <wtf/Vector.h>
50 #include <wtf/text/StringBuilder.h>
52 #if ENABLE(DOM_STORAGE)
53 #include "StorageNamespace.h"
56 #if ENABLE(INPUT_COLOR)
57 #include "ColorChooser.h"
62 using namespace HTMLNames;
65 Chrome::Chrome(Page* page, ChromeClient* client)
74 m_client->chromeDestroyed();
77 void Chrome::invalidateWindow(const IntRect& updateRect, bool immediate)
79 m_client->invalidateWindow(updateRect, immediate);
82 void Chrome::invalidateContentsAndWindow(const IntRect& updateRect, bool immediate)
84 m_client->invalidateContentsAndWindow(updateRect, immediate);
87 void Chrome::invalidateContentsForSlowScroll(const IntRect& updateRect, bool immediate)
89 m_client->invalidateContentsForSlowScroll(updateRect, immediate);
92 void Chrome::scroll(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect)
94 m_client->scroll(scrollDelta, rectToScroll, clipRect);
97 #if ENABLE(TILED_BACKING_STORE)
98 void Chrome::delegatedScrollRequested(const IntPoint& scrollPoint)
100 m_client->delegatedScrollRequested(scrollPoint);
104 IntPoint Chrome::screenToWindow(const IntPoint& point) const
106 return m_client->screenToWindow(point);
109 IntRect Chrome::windowToScreen(const IntRect& rect) const
111 return m_client->windowToScreen(rect);
114 PlatformPageClient Chrome::platformPageClient() const
116 return m_client->platformPageClient();
119 void Chrome::contentsSizeChanged(Frame* frame, const IntSize& size) const
121 m_client->contentsSizeChanged(frame, size);
124 void Chrome::layoutUpdated(Frame* frame) const
126 m_client->layoutUpdated(frame);
129 void Chrome::scrollRectIntoView(const IntRect& rect) const
131 m_client->scrollRectIntoView(rect);
134 void Chrome::scrollbarsModeDidChange() const
136 m_client->scrollbarsModeDidChange();
139 void Chrome::setWindowRect(const FloatRect& rect) const
141 m_client->setWindowRect(rect);
144 FloatRect Chrome::windowRect() const
146 return m_client->windowRect();
149 FloatRect Chrome::pageRect() const
151 return m_client->pageRect();
154 void Chrome::focus() const
159 void Chrome::unfocus() const
164 bool Chrome::canTakeFocus(FocusDirection direction) const
166 return m_client->canTakeFocus(direction);
169 void Chrome::takeFocus(FocusDirection direction) const
171 m_client->takeFocus(direction);
174 void Chrome::focusedNodeChanged(Node* node) const
176 m_client->focusedNodeChanged(node);
179 void Chrome::focusedFrameChanged(Frame* frame) const
181 m_client->focusedFrameChanged(frame);
184 Page* Chrome::createWindow(Frame* frame, const FrameLoadRequest& request, const WindowFeatures& features, const NavigationAction& action) const
186 Page* newPage = m_client->createWindow(frame, request, features, action);
188 #if ENABLE(DOM_STORAGE)
190 if (StorageNamespace* oldSessionStorage = m_page->sessionStorage(false))
191 newPage->setSessionStorage(oldSessionStorage->copy());
198 void Chrome::show() const
203 bool Chrome::canRunModal() const
205 return m_client->canRunModal();
208 static bool canRunModalIfDuringPageDismissal(Page* page, ChromeClient::DialogType dialog, const String& message)
210 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
211 FrameLoader::PageDismissalType dismissal = frame->loader()->pageDismissalEventBeingDispatched();
212 if (dismissal != FrameLoader::NoDismissal)
213 return page->chrome()->client()->shouldRunModalDialogDuringPageDismissal(dialog, message, dismissal);
218 bool Chrome::canRunModalNow() const
220 // If loads are blocked, we can't run modal because the contents
221 // of the modal dialog will never show up!
222 return canRunModal() && !ResourceHandle::loadsBlocked()
223 && canRunModalIfDuringPageDismissal(m_page, ChromeClient::HTMLDialog, String());
226 void Chrome::runModal() const
228 // Defer callbacks in all the other pages in this group, so we don't try to run JavaScript
229 // in a way that could interact with this view.
230 PageGroupLoadDeferrer deferrer(m_page, false);
232 TimerBase::fireTimersInNestedEventLoop();
233 m_client->runModal();
236 void Chrome::setToolbarsVisible(bool b) const
238 m_client->setToolbarsVisible(b);
241 bool Chrome::toolbarsVisible() const
243 return m_client->toolbarsVisible();
246 void Chrome::setStatusbarVisible(bool b) const
248 m_client->setStatusbarVisible(b);
251 bool Chrome::statusbarVisible() const
253 return m_client->statusbarVisible();
256 void Chrome::setScrollbarsVisible(bool b) const
258 m_client->setScrollbarsVisible(b);
261 bool Chrome::scrollbarsVisible() const
263 return m_client->scrollbarsVisible();
266 void Chrome::setMenubarVisible(bool b) const
268 m_client->setMenubarVisible(b);
271 bool Chrome::menubarVisible() const
273 return m_client->menubarVisible();
276 void Chrome::setResizable(bool b) const
278 m_client->setResizable(b);
281 bool Chrome::canRunBeforeUnloadConfirmPanel()
283 return m_client->canRunBeforeUnloadConfirmPanel();
286 bool Chrome::runBeforeUnloadConfirmPanel(const String& message, Frame* frame)
288 // Defer loads in case the client method runs a new event loop that would
289 // otherwise cause the load to continue while we're in the middle of executing JavaScript.
290 PageGroupLoadDeferrer deferrer(m_page, true);
292 return m_client->runBeforeUnloadConfirmPanel(message, frame);
295 void Chrome::closeWindowSoon()
297 m_client->closeWindowSoon();
300 void Chrome::runJavaScriptAlert(Frame* frame, const String& message)
302 if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::AlertDialog, message))
305 // Defer loads in case the client method runs a new event loop that would
306 // otherwise cause the load to continue while we're in the middle of executing JavaScript.
307 PageGroupLoadDeferrer deferrer(m_page, true);
310 m_client->runJavaScriptAlert(frame, frame->displayStringModifiedByEncoding(message));
313 bool Chrome::runJavaScriptConfirm(Frame* frame, const String& message)
315 if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::ConfirmDialog, message))
318 // Defer loads in case the client method runs a new event loop that would
319 // otherwise cause the load to continue while we're in the middle of executing JavaScript.
320 PageGroupLoadDeferrer deferrer(m_page, true);
323 return m_client->runJavaScriptConfirm(frame, frame->displayStringModifiedByEncoding(message));
326 bool Chrome::runJavaScriptPrompt(Frame* frame, const String& prompt, const String& defaultValue, String& result)
328 if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::PromptDialog, prompt))
331 // Defer loads in case the client method runs a new event loop that would
332 // otherwise cause the load to continue while we're in the middle of executing JavaScript.
333 PageGroupLoadDeferrer deferrer(m_page, true);
336 bool ok = m_client->runJavaScriptPrompt(frame, frame->displayStringModifiedByEncoding(prompt), frame->displayStringModifiedByEncoding(defaultValue), result);
339 result = frame->displayStringModifiedByEncoding(result);
344 void Chrome::setStatusbarText(Frame* frame, const String& status)
347 m_client->setStatusbarText(frame->displayStringModifiedByEncoding(status));
350 bool Chrome::shouldInterruptJavaScript()
352 // Defer loads in case the client method runs a new event loop that would
353 // otherwise cause the load to continue while we're in the middle of executing JavaScript.
354 PageGroupLoadDeferrer deferrer(m_page, true);
356 return m_client->shouldInterruptJavaScript();
359 #if ENABLE(REGISTER_PROTOCOL_HANDLER)
360 void Chrome::registerProtocolHandler(const String& scheme, const String& baseURL, const String& url, const String& title)
362 m_client->registerProtocolHandler(scheme, baseURL, url, title);
366 IntRect Chrome::windowResizerRect() const
368 return m_client->windowResizerRect();
371 void Chrome::mouseDidMoveOverElement(const HitTestResult& result, unsigned modifierFlags)
373 if (result.innerNode()) {
374 Document* document = result.innerNode()->document();
375 if (document && document->isDNSPrefetchEnabled())
376 ResourceHandle::prepareForURL(result.absoluteLinkURL());
378 m_client->mouseDidMoveOverElement(result, modifierFlags);
380 InspectorInstrumentation::mouseDidMoveOverElement(m_page, result, modifierFlags);
383 void Chrome::setToolTip(const HitTestResult& result)
385 // First priority is a potential toolTip representing a spelling or grammar error
386 TextDirection toolTipDirection;
387 String toolTip = result.spellingToolTip(toolTipDirection);
389 // Next priority is a toolTip from a URL beneath the mouse (if preference is set to show those).
390 if (toolTip.isEmpty() && m_page->settings()->showsURLsInToolTips()) {
391 if (Node* node = result.innerNonSharedNode()) {
392 // Get tooltip representing form action, if relevant
393 if (node->hasTagName(inputTag)) {
394 HTMLInputElement* input = static_cast<HTMLInputElement*>(node);
395 if (input->isSubmitButton())
396 if (HTMLFormElement* form = input->form()) {
397 toolTip = form->action();
398 if (form->renderer())
399 toolTipDirection = form->renderer()->style()->direction();
401 toolTipDirection = LTR;
406 // Get tooltip representing link's URL
407 if (toolTip.isEmpty()) {
408 // FIXME: Need to pass this URL through userVisibleString once that's in WebCore
409 toolTip = result.absoluteLinkURL().string();
410 // URL always display as LTR.
411 toolTipDirection = LTR;
415 // Next we'll consider a tooltip for element with "title" attribute
416 if (toolTip.isEmpty())
417 toolTip = result.title(toolTipDirection);
419 if (toolTip.isEmpty() && m_page->settings()->showsToolTipOverTruncatedText())
420 toolTip = result.innerTextIfTruncated(toolTipDirection);
422 // Lastly, for <input type="file"> that allow multiple files, we'll consider a tooltip for the selected filenames
423 if (toolTip.isEmpty()) {
424 if (Node* node = result.innerNonSharedNode()) {
425 if (node->hasTagName(inputTag)) {
426 HTMLInputElement* input = static_cast<HTMLInputElement*>(node);
427 if (input->isFileUpload()) {
428 FileList* files = input->files();
429 unsigned listSize = files->length();
432 for (size_t i = 0; i < listSize; ++i) {
433 names.append(files->item(i)->fileName());
434 if (i != listSize - 1)
437 toolTip = names.toString();
438 // filename always display as LTR.
439 toolTipDirection = LTR;
446 m_client->setToolTip(toolTip, toolTipDirection);
449 void Chrome::print(Frame* frame)
451 // FIXME: This should have PageGroupLoadDeferrer, like runModal() or runJavaScriptAlert(), becasue it's no different from those.
452 m_client->print(frame);
455 void Chrome::requestGeolocationPermissionForFrame(Frame* frame, Geolocation* geolocation)
457 m_client->requestGeolocationPermissionForFrame(frame, geolocation);
460 void Chrome::cancelGeolocationPermissionRequestForFrame(Frame* frame, Geolocation* geolocation)
462 m_client->cancelGeolocationPermissionRequestForFrame(frame, geolocation);
465 #if ENABLE(DIRECTORY_UPLOAD)
466 void Chrome::enumerateChosenDirectory(FileChooser* fileChooser)
468 m_client->enumerateChosenDirectory(fileChooser);
472 #if ENABLE(INPUT_COLOR)
473 void Chrome::openColorChooser(ColorChooser* colorChooser, const Color& initialColor)
475 m_client->openColorChooser(colorChooser, initialColor);
478 void Chrome::cleanupColorChooser()
480 m_client->cleanupColorChooser();
483 void Chrome::setSelectedColorInColorChooser(const Color& color)
485 m_client->setSelectedColorInColorChooser(color);
489 void Chrome::runOpenPanel(Frame* frame, PassRefPtr<FileChooser> fileChooser)
491 m_client->runOpenPanel(frame, fileChooser);
494 void Chrome::loadIconForFiles(const Vector<String>& filenames, FileIconLoader* loader)
496 m_client->loadIconForFiles(filenames, loader);
499 void Chrome::dispatchViewportDataDidChange(const ViewportArguments& arguments) const
501 m_client->dispatchViewportDataDidChange(arguments);
504 void Chrome::setCursor(const Cursor& cursor)
506 m_client->setCursor(cursor);
509 void Chrome::setCursorHiddenUntilMouseMoves(bool hiddenUntilMouseMoves)
511 m_client->setCursorHiddenUntilMouseMoves(hiddenUntilMouseMoves);
514 #if ENABLE(REQUEST_ANIMATION_FRAME)
515 void Chrome::scheduleAnimation()
517 #if !USE(REQUEST_ANIMATION_FRAME_TIMER)
518 m_client->scheduleAnimation();
523 #if ENABLE(NOTIFICATIONS)
524 NotificationPresenter* Chrome::notificationPresenter() const
526 return m_client->notificationPresenter();
532 #if ENABLE(DASHBOARD_SUPPORT)
533 void ChromeClient::dashboardRegionsChanged()
538 void ChromeClient::populateVisitedLinks()
542 FloatRect ChromeClient::customHighlightRect(Node*, const AtomicString&, const FloatRect&)
547 void ChromeClient::paintCustomHighlight(Node*, const AtomicString&, const FloatRect&, const FloatRect&, bool, bool)
551 bool ChromeClient::shouldReplaceWithGeneratedFileForUpload(const String&, String&)
556 String ChromeClient::generateReplacementFile(const String&)
558 ASSERT_NOT_REACHED();
562 bool ChromeClient::paintCustomScrollbar(GraphicsContext*, const FloatRect&, ScrollbarControlSize,
563 ScrollbarControlState, ScrollbarPart, bool,
564 float, float, ScrollbarControlPartMask)
569 bool ChromeClient::paintCustomScrollCorner(GraphicsContext*, const FloatRect&)
574 bool ChromeClient::paintCustomOverhangArea(GraphicsContext*, const IntRect&, const IntRect&, const IntRect&)
579 bool Chrome::selectItemWritingDirectionIsNatural()
581 return m_client->selectItemWritingDirectionIsNatural();
584 bool Chrome::selectItemAlignmentFollowsMenuWritingDirection()
586 return m_client->selectItemAlignmentFollowsMenuWritingDirection();
589 PassRefPtr<PopupMenu> Chrome::createPopupMenu(PopupMenuClient* client) const
591 return m_client->createPopupMenu(client);
594 PassRefPtr<SearchPopupMenu> Chrome::createSearchPopupMenu(PopupMenuClient* client) const
596 return m_client->createSearchPopupMenu(client);
599 #if ENABLE(CONTEXT_MENUS)
600 void Chrome::showContextMenu()
602 m_client->showContextMenu();
606 bool Chrome::requiresFullscreenForVideoPlayback()
608 return m_client->requiresFullscreenForVideoPlayback();
611 } // namespace WebCore