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 "StorageNamespace.h"
47 #include "WindowFeatures.h"
48 #include <wtf/PassRefPtr.h>
49 #include <wtf/RefPtr.h>
50 #include <wtf/Vector.h>
51 #include <wtf/text/StringBuilder.h>
53 #if ENABLE(INPUT_COLOR)
54 #include "ColorChooser.h"
59 using namespace HTMLNames;
62 Chrome::Chrome(Page* page, ChromeClient* client)
71 m_client->chromeDestroyed();
74 void Chrome::invalidateRootView(const IntRect& updateRect, bool immediate)
76 m_client->invalidateRootView(updateRect, immediate);
79 void Chrome::invalidateContentsAndRootView(const IntRect& updateRect, bool immediate)
81 m_client->invalidateContentsAndRootView(updateRect, immediate);
84 void Chrome::invalidateContentsForSlowScroll(const IntRect& updateRect, bool immediate)
86 m_client->invalidateContentsForSlowScroll(updateRect, immediate);
89 void Chrome::scroll(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect)
91 m_client->scroll(scrollDelta, rectToScroll, clipRect);
94 #if USE(TILED_BACKING_STORE)
95 void Chrome::delegatedScrollRequested(const IntPoint& scrollPoint)
97 m_client->delegatedScrollRequested(scrollPoint);
101 IntPoint Chrome::screenToRootView(const IntPoint& point) const
103 return m_client->screenToRootView(point);
106 IntRect Chrome::rootViewToScreen(const IntRect& rect) const
108 return m_client->rootViewToScreen(rect);
111 PlatformPageClient Chrome::platformPageClient() const
113 return m_client->platformPageClient();
116 void Chrome::contentsSizeChanged(Frame* frame, const IntSize& size) const
118 m_client->contentsSizeChanged(frame, size);
121 #if ENABLE(TIZEN_SUPPORT_VIEWPORT)
122 bool Chrome::canContentsSizeChange(Frame* frame, const IntSize& size) const
124 m_client->canContentsSizeChange(frame, size);
128 void Chrome::layoutUpdated(Frame* frame) const
130 m_client->layoutUpdated(frame);
133 void Chrome::scrollRectIntoView(const IntRect& rect) const
135 m_client->scrollRectIntoView(rect);
138 void Chrome::scrollbarsModeDidChange() const
140 m_client->scrollbarsModeDidChange();
143 void Chrome::setWindowRect(const FloatRect& rect) const
145 m_client->setWindowRect(rect);
148 FloatRect Chrome::windowRect() const
150 return m_client->windowRect();
153 FloatRect Chrome::pageRect() const
155 return m_client->pageRect();
158 void Chrome::focus() const
163 void Chrome::unfocus() const
168 bool Chrome::canTakeFocus(FocusDirection direction) const
170 return m_client->canTakeFocus(direction);
173 void Chrome::takeFocus(FocusDirection direction) const
175 m_client->takeFocus(direction);
178 void Chrome::focusedNodeChanged(Node* node) const
180 m_client->focusedNodeChanged(node);
183 void Chrome::focusedFrameChanged(Frame* frame) const
185 m_client->focusedFrameChanged(frame);
188 Page* Chrome::createWindow(Frame* frame, const FrameLoadRequest& request, const WindowFeatures& features, const NavigationAction& action) const
190 Page* newPage = m_client->createWindow(frame, request, features, action);
193 if (StorageNamespace* oldSessionStorage = m_page->sessionStorage(false))
194 newPage->setSessionStorage(oldSessionStorage->copy());
200 void Chrome::show() const
205 bool Chrome::canRunModal() const
207 return m_client->canRunModal();
210 static bool canRunModalIfDuringPageDismissal(Page* page, ChromeClient::DialogType dialog, const String& message)
212 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
213 FrameLoader::PageDismissalType dismissal = frame->loader()->pageDismissalEventBeingDispatched();
214 if (dismissal != FrameLoader::NoDismissal)
215 return page->chrome()->client()->shouldRunModalDialogDuringPageDismissal(dialog, message, dismissal);
220 bool Chrome::canRunModalNow() const
222 // If loads are blocked, we can't run modal because the contents
223 // of the modal dialog will never show up!
224 return canRunModal() && !ResourceHandle::loadsBlocked()
225 && canRunModalIfDuringPageDismissal(m_page, ChromeClient::HTMLDialog, String());
228 void Chrome::runModal() const
230 // Defer callbacks in all the other pages in this group, so we don't try to run JavaScript
231 // in a way that could interact with this view.
232 PageGroupLoadDeferrer deferrer(m_page, false);
234 TimerBase::fireTimersInNestedEventLoop();
235 m_client->runModal();
238 void Chrome::setToolbarsVisible(bool b) const
240 m_client->setToolbarsVisible(b);
243 bool Chrome::toolbarsVisible() const
245 return m_client->toolbarsVisible();
248 void Chrome::setStatusbarVisible(bool b) const
250 m_client->setStatusbarVisible(b);
253 bool Chrome::statusbarVisible() const
255 return m_client->statusbarVisible();
258 void Chrome::setScrollbarsVisible(bool b) const
260 m_client->setScrollbarsVisible(b);
263 bool Chrome::scrollbarsVisible() const
265 return m_client->scrollbarsVisible();
268 void Chrome::setMenubarVisible(bool b) const
270 m_client->setMenubarVisible(b);
273 bool Chrome::menubarVisible() const
275 return m_client->menubarVisible();
278 void Chrome::setResizable(bool b) const
280 m_client->setResizable(b);
283 bool Chrome::canRunBeforeUnloadConfirmPanel()
285 return m_client->canRunBeforeUnloadConfirmPanel();
288 bool Chrome::runBeforeUnloadConfirmPanel(const String& message, Frame* frame)
290 // Defer loads in case the client method runs a new event loop that would
291 // otherwise cause the load to continue while we're in the middle of executing JavaScript.
292 PageGroupLoadDeferrer deferrer(m_page, true);
294 return m_client->runBeforeUnloadConfirmPanel(message, frame);
297 void Chrome::closeWindowSoon()
299 m_client->closeWindowSoon();
302 void Chrome::runJavaScriptAlert(Frame* frame, const String& message)
304 if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::AlertDialog, message))
307 // Defer loads in case the client method runs a new event loop that would
308 // otherwise cause the load to continue while we're in the middle of executing JavaScript.
309 PageGroupLoadDeferrer deferrer(m_page, true);
312 m_client->runJavaScriptAlert(frame, frame->displayStringModifiedByEncoding(message));
315 bool Chrome::runJavaScriptConfirm(Frame* frame, const String& message)
317 if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::ConfirmDialog, message))
320 // Defer loads in case the client method runs a new event loop that would
321 // otherwise cause the load to continue while we're in the middle of executing JavaScript.
322 PageGroupLoadDeferrer deferrer(m_page, true);
325 return m_client->runJavaScriptConfirm(frame, frame->displayStringModifiedByEncoding(message));
328 bool Chrome::runJavaScriptPrompt(Frame* frame, const String& prompt, const String& defaultValue, String& result)
330 if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::PromptDialog, prompt))
333 // Defer loads in case the client method runs a new event loop that would
334 // otherwise cause the load to continue while we're in the middle of executing JavaScript.
335 PageGroupLoadDeferrer deferrer(m_page, true);
338 bool ok = m_client->runJavaScriptPrompt(frame, frame->displayStringModifiedByEncoding(prompt), frame->displayStringModifiedByEncoding(defaultValue), result);
341 result = frame->displayStringModifiedByEncoding(result);
346 void Chrome::setStatusbarText(Frame* frame, const String& status)
349 m_client->setStatusbarText(frame->displayStringModifiedByEncoding(status));
352 bool Chrome::shouldInterruptJavaScript()
354 // Defer loads in case the client method runs a new event loop that would
355 // otherwise cause the load to continue while we're in the middle of executing JavaScript.
356 PageGroupLoadDeferrer deferrer(m_page, true);
358 return m_client->shouldInterruptJavaScript();
361 #if ENABLE(REGISTER_PROTOCOL_HANDLER)
362 void Chrome::registerProtocolHandler(const String& scheme, const String& baseURL, const String& url, const String& title)
364 m_client->registerProtocolHandler(scheme, baseURL, url, title);
368 IntRect Chrome::windowResizerRect() const
370 return m_client->windowResizerRect();
373 void Chrome::mouseDidMoveOverElement(const HitTestResult& result, unsigned modifierFlags)
375 if (result.innerNode()) {
376 Document* document = result.innerNode()->document();
377 if (document && document->isDNSPrefetchEnabled())
378 ResourceHandle::prepareForURL(result.absoluteLinkURL());
380 m_client->mouseDidMoveOverElement(result, modifierFlags);
382 InspectorInstrumentation::mouseDidMoveOverElement(m_page, result, modifierFlags);
385 void Chrome::setToolTip(const HitTestResult& result)
387 // First priority is a potential toolTip representing a spelling or grammar error
388 TextDirection toolTipDirection;
389 String toolTip = result.spellingToolTip(toolTipDirection);
391 // Next priority is a toolTip from a URL beneath the mouse (if preference is set to show those).
392 if (toolTip.isEmpty() && m_page->settings()->showsURLsInToolTips()) {
393 if (Node* node = result.innerNonSharedNode()) {
394 // Get tooltip representing form action, if relevant
395 if (node->hasTagName(inputTag)) {
396 HTMLInputElement* input = static_cast<HTMLInputElement*>(node);
397 if (input->isSubmitButton())
398 if (HTMLFormElement* form = input->form()) {
399 toolTip = form->action();
400 if (form->renderer())
401 toolTipDirection = form->renderer()->style()->direction();
403 toolTipDirection = LTR;
408 // Get tooltip representing link's URL
409 if (toolTip.isEmpty()) {
410 // FIXME: Need to pass this URL through userVisibleString once that's in WebCore
411 toolTip = result.absoluteLinkURL().string();
412 // URL always display as LTR.
413 toolTipDirection = LTR;
417 // Next we'll consider a tooltip for element with "title" attribute
418 if (toolTip.isEmpty())
419 toolTip = result.title(toolTipDirection);
421 if (toolTip.isEmpty() && m_page->settings()->showsToolTipOverTruncatedText())
422 toolTip = result.innerTextIfTruncated(toolTipDirection);
424 // Lastly, for <input type="file"> that allow multiple files, we'll consider a tooltip for the selected filenames
425 if (toolTip.isEmpty()) {
426 if (Node* node = result.innerNonSharedNode()) {
427 if (node->hasTagName(inputTag)) {
428 HTMLInputElement* input = static_cast<HTMLInputElement*>(node);
429 toolTip = input->defaultToolTip();
431 // FIXME: We should obtain text direction of tooltip from
432 // ChromeClient or platform. As of October 2011, all client
433 // implementations don't use text direction information for
434 // ChromeClient::setToolTip. We'll work on tooltip text
435 // direction during bidi cleanup in form inputs.
436 toolTipDirection = LTR;
441 m_client->setToolTip(toolTip, toolTipDirection);
444 void Chrome::print(Frame* frame)
446 // FIXME: This should have PageGroupLoadDeferrer, like runModal() or runJavaScriptAlert(), becasue it's no different from those.
447 m_client->print(frame);
450 void Chrome::requestGeolocationPermissionForFrame(Frame* frame, Geolocation* geolocation)
452 m_client->requestGeolocationPermissionForFrame(frame, geolocation);
455 void Chrome::cancelGeolocationPermissionRequestForFrame(Frame* frame, Geolocation* geolocation)
457 m_client->cancelGeolocationPermissionRequestForFrame(frame, geolocation);
460 #if ENABLE(DIRECTORY_UPLOAD)
461 void Chrome::enumerateChosenDirectory(FileChooser* fileChooser)
463 m_client->enumerateChosenDirectory(fileChooser);
467 #if ENABLE(INPUT_COLOR)
468 void Chrome::openColorChooser(ColorChooser* colorChooser, const Color& initialColor)
470 m_client->openColorChooser(colorChooser, initialColor);
473 void Chrome::cleanupColorChooser(ColorChooser* colorChooser)
475 m_client->cleanupColorChooser(colorChooser);
478 void Chrome::setSelectedColorInColorChooser(ColorChooser* colorChooser, const Color& color)
480 m_client->setSelectedColorInColorChooser(colorChooser, color);
484 void Chrome::runOpenPanel(Frame* frame, PassRefPtr<FileChooser> fileChooser)
486 m_client->runOpenPanel(frame, fileChooser);
489 void Chrome::loadIconForFiles(const Vector<String>& filenames, FileIconLoader* loader)
491 m_client->loadIconForFiles(filenames, loader);
494 void Chrome::dispatchViewportPropertiesDidChange(const ViewportArguments& arguments) const
496 m_client->dispatchViewportPropertiesDidChange(arguments);
499 void Chrome::setCursor(const Cursor& cursor)
501 m_client->setCursor(cursor);
504 void Chrome::setCursorHiddenUntilMouseMoves(bool hiddenUntilMouseMoves)
506 m_client->setCursorHiddenUntilMouseMoves(hiddenUntilMouseMoves);
509 #if ENABLE(REQUEST_ANIMATION_FRAME)
510 void Chrome::scheduleAnimation()
512 #if !USE(REQUEST_ANIMATION_FRAME_TIMER)
513 m_client->scheduleAnimation();
520 #if ENABLE(DASHBOARD_SUPPORT)
521 void ChromeClient::dashboardRegionsChanged()
526 void ChromeClient::populateVisitedLinks()
530 FloatRect ChromeClient::customHighlightRect(Node*, const AtomicString&, const FloatRect&)
535 void ChromeClient::paintCustomHighlight(Node*, const AtomicString&, const FloatRect&, const FloatRect&, bool, bool)
539 bool ChromeClient::shouldReplaceWithGeneratedFileForUpload(const String&, String&)
544 String ChromeClient::generateReplacementFile(const String&)
546 ASSERT_NOT_REACHED();
550 bool ChromeClient::paintCustomOverhangArea(GraphicsContext*, const IntRect&, const IntRect&, const IntRect&)
555 bool Chrome::selectItemWritingDirectionIsNatural()
557 return m_client->selectItemWritingDirectionIsNatural();
560 bool Chrome::selectItemAlignmentFollowsMenuWritingDirection()
562 return m_client->selectItemAlignmentFollowsMenuWritingDirection();
565 PassRefPtr<PopupMenu> Chrome::createPopupMenu(PopupMenuClient* client) const
567 return m_client->createPopupMenu(client);
570 PassRefPtr<SearchPopupMenu> Chrome::createSearchPopupMenu(PopupMenuClient* client) const
572 return m_client->createSearchPopupMenu(client);
575 #if ENABLE(CONTEXT_MENUS)
576 void Chrome::showContextMenu()
578 m_client->showContextMenu();
582 bool Chrome::requiresFullscreenForVideoPlayback()
584 return m_client->requiresFullscreenForVideoPlayback();
587 } // namespace WebCore