2 * Copyright (C) 2006, 2007, 2009, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies)
4 * Copyright (C) 2012, Samsung Electronics. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
25 #include "ChromeClient.h"
28 #include "FileIconLoader.h"
29 #include "FileChooser.h"
31 #include "FloatRect.h"
33 #include "FrameTree.h"
34 #include "Geolocation.h"
35 #include "HTMLFormElement.h"
36 #include "HTMLInputElement.h"
37 #include "HTMLNames.h"
38 #include "HitTestResult.h"
40 #include "InspectorInstrumentation.h"
42 #include "PageGroupLoadDeferrer.h"
43 #include "RenderObject.h"
44 #include "ResourceHandle.h"
45 #include "SecurityOrigin.h"
47 #include "StorageNamespace.h"
48 #include "WindowFeatures.h"
49 #include <wtf/PassRefPtr.h>
50 #include <wtf/RefPtr.h>
51 #include <wtf/Vector.h>
52 #include <wtf/text/StringBuilder.h>
54 #if ENABLE(INPUT_TYPE_COLOR)
55 #include "ColorChooser.h"
60 using namespace HTMLNames;
63 Chrome::Chrome(Page* page, ChromeClient* client)
72 m_client->chromeDestroyed();
75 PassOwnPtr<Chrome> Chrome::create(Page* page, ChromeClient* client)
77 return adoptPtr(new Chrome(page, client));
80 void Chrome::invalidateRootView(const IntRect& updateRect, bool immediate)
82 m_client->invalidateRootView(updateRect, immediate);
85 void Chrome::invalidateContentsAndRootView(const IntRect& updateRect, bool immediate)
87 m_client->invalidateContentsAndRootView(updateRect, immediate);
90 void Chrome::invalidateContentsForSlowScroll(const IntRect& updateRect, bool immediate)
92 m_client->invalidateContentsForSlowScroll(updateRect, immediate);
95 void Chrome::scroll(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect)
97 m_client->scroll(scrollDelta, rectToScroll, clipRect);
100 #if USE(TILED_BACKING_STORE)
101 void Chrome::delegatedScrollRequested(const IntPoint& scrollPoint)
103 m_client->delegatedScrollRequested(scrollPoint);
107 #if ENABLE(TIZEN_WEBKIT2_HISTORICAL_RESTORE_VISIBLE_CONTENT_RECT)
108 void Chrome::requestVisibleContentRectRestore(const IntPoint& scrollOffset, float scale)
110 m_client->requestVisibleContentRectRestore(scrollOffset, scale);
113 float Chrome::contentsScaleFactor() const
115 return m_client->contentsScaleFactor();
119 IntPoint Chrome::screenToRootView(const IntPoint& point) const
121 return m_client->screenToRootView(point);
124 IntRect Chrome::rootViewToScreen(const IntRect& rect) const
126 return m_client->rootViewToScreen(rect);
129 PlatformPageClient Chrome::platformPageClient() const
131 return m_client->platformPageClient();
134 void Chrome::contentsSizeChanged(Frame* frame, const IntSize& size) const
136 m_client->contentsSizeChanged(frame, size);
139 void Chrome::layoutUpdated(Frame* frame) const
141 m_client->layoutUpdated(frame);
144 void Chrome::scrollRectIntoView(const IntRect& rect) const
146 m_client->scrollRectIntoView(rect);
149 void Chrome::scrollbarsModeDidChange() const
151 m_client->scrollbarsModeDidChange();
154 void Chrome::setWindowRect(const FloatRect& rect) const
156 m_client->setWindowRect(rect);
159 FloatRect Chrome::windowRect() const
161 return m_client->windowRect();
164 FloatRect Chrome::pageRect() const
166 return m_client->pageRect();
169 void Chrome::focus() const
174 void Chrome::unfocus() const
179 bool Chrome::canTakeFocus(FocusDirection direction) const
181 return m_client->canTakeFocus(direction);
184 void Chrome::takeFocus(FocusDirection direction) const
186 m_client->takeFocus(direction);
189 void Chrome::focusedNodeChanged(Node* node) const
191 m_client->focusedNodeChanged(node);
194 void Chrome::focusedFrameChanged(Frame* frame) const
196 m_client->focusedFrameChanged(frame);
199 Page* Chrome::createWindow(Frame* frame, const FrameLoadRequest& request, const WindowFeatures& features, const NavigationAction& action) const
201 Page* newPage = m_client->createWindow(frame, request, features, action);
204 if (StorageNamespace* oldSessionStorage = m_page->sessionStorage(false))
205 newPage->setSessionStorage(oldSessionStorage->copy());
211 void Chrome::show() const
216 bool Chrome::canRunModal() const
218 return m_client->canRunModal();
221 static bool canRunModalIfDuringPageDismissal(Page* page, ChromeClient::DialogType dialog, const String& message)
223 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
224 FrameLoader::PageDismissalType dismissal = frame->loader()->pageDismissalEventBeingDispatched();
225 if (dismissal != FrameLoader::NoDismissal)
226 return page->chrome()->client()->shouldRunModalDialogDuringPageDismissal(dialog, message, dismissal);
231 bool Chrome::canRunModalNow() const
233 // If loads are blocked, we can't run modal because the contents
234 // of the modal dialog will never show up!
235 return canRunModal() && !ResourceHandle::loadsBlocked()
236 && canRunModalIfDuringPageDismissal(m_page, ChromeClient::HTMLDialog, String());
239 void Chrome::runModal() const
241 // Defer callbacks in all the other pages in this group, so we don't try to run JavaScript
242 // in a way that could interact with this view.
243 PageGroupLoadDeferrer deferrer(m_page, false);
245 TimerBase::fireTimersInNestedEventLoop();
246 m_client->runModal();
249 void Chrome::setToolbarsVisible(bool b) const
251 m_client->setToolbarsVisible(b);
254 bool Chrome::toolbarsVisible() const
256 return m_client->toolbarsVisible();
259 void Chrome::setStatusbarVisible(bool b) const
261 m_client->setStatusbarVisible(b);
264 bool Chrome::statusbarVisible() const
266 return m_client->statusbarVisible();
269 void Chrome::setScrollbarsVisible(bool b) const
271 m_client->setScrollbarsVisible(b);
274 bool Chrome::scrollbarsVisible() const
276 return m_client->scrollbarsVisible();
279 void Chrome::setMenubarVisible(bool b) const
281 m_client->setMenubarVisible(b);
284 bool Chrome::menubarVisible() const
286 return m_client->menubarVisible();
289 void Chrome::setResizable(bool b) const
291 m_client->setResizable(b);
294 bool Chrome::canRunBeforeUnloadConfirmPanel()
296 return m_client->canRunBeforeUnloadConfirmPanel();
299 bool Chrome::runBeforeUnloadConfirmPanel(const String& message, Frame* frame)
301 // Defer loads in case the client method runs a new event loop that would
302 // otherwise cause the load to continue while we're in the middle of executing JavaScript.
303 PageGroupLoadDeferrer deferrer(m_page, true);
305 return m_client->runBeforeUnloadConfirmPanel(message, frame);
308 void Chrome::closeWindowSoon()
310 m_client->closeWindowSoon();
313 void Chrome::runJavaScriptAlert(Frame* frame, const String& message)
315 if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::AlertDialog, message))
318 #if ENABLE(TIZEN_ISF_PORT)
319 Node* node = frame ? frame->document()->focusedNode() : 0;
321 && node->nodeType() == Node::ELEMENT_NODE
322 && node->hasTagName(HTMLNames::inputTag))
323 static_cast<HTMLInputElement*>(node)->blur();
326 // Defer loads in case the client method runs a new event loop that would
327 // otherwise cause the load to continue while we're in the middle of executing JavaScript.
328 PageGroupLoadDeferrer deferrer(m_page, true);
331 m_client->runJavaScriptAlert(frame, frame->displayStringModifiedByEncoding(message));
334 bool Chrome::runJavaScriptConfirm(Frame* frame, const String& message)
336 if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::ConfirmDialog, message))
339 // Defer loads in case the client method runs a new event loop that would
340 // otherwise cause the load to continue while we're in the middle of executing JavaScript.
341 PageGroupLoadDeferrer deferrer(m_page, true);
344 return m_client->runJavaScriptConfirm(frame, frame->displayStringModifiedByEncoding(message));
347 bool Chrome::runJavaScriptPrompt(Frame* frame, const String& prompt, const String& defaultValue, String& result)
349 if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::PromptDialog, prompt))
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);
357 bool ok = m_client->runJavaScriptPrompt(frame, frame->displayStringModifiedByEncoding(prompt), frame->displayStringModifiedByEncoding(defaultValue), result);
360 result = frame->displayStringModifiedByEncoding(result);
365 void Chrome::setStatusbarText(Frame* frame, const String& status)
368 m_client->setStatusbarText(frame->displayStringModifiedByEncoding(status));
371 bool Chrome::shouldInterruptJavaScript()
373 // Defer loads in case the client method runs a new event loop that would
374 // otherwise cause the load to continue while we're in the middle of executing JavaScript.
375 PageGroupLoadDeferrer deferrer(m_page, true);
377 return m_client->shouldInterruptJavaScript();
380 #if ENABLE(TIZEN_SEARCH_PROVIDER)
381 void Chrome::addSearchProvider(const String& baseURL, const String& engineURL)
383 return m_client->addSearchProvider(baseURL, engineURL);
386 unsigned long Chrome::isSearchProviderInstalled(const String& baseURL, const String& engineURL)
388 return m_client->isSearchProviderInstalled(baseURL, engineURL);
392 IntRect Chrome::windowResizerRect() const
394 return m_client->windowResizerRect();
397 void Chrome::mouseDidMoveOverElement(const HitTestResult& result, unsigned modifierFlags)
399 if (result.innerNode()) {
400 Document* document = result.innerNode()->document();
401 if (document && document->isDNSPrefetchEnabled())
402 prefetchDNS(result.absoluteLinkURL().host());
404 m_client->mouseDidMoveOverElement(result, modifierFlags);
406 InspectorInstrumentation::mouseDidMoveOverElement(m_page, result, modifierFlags);
409 void Chrome::setToolTip(const HitTestResult& result)
411 // First priority is a potential toolTip representing a spelling or grammar error
412 TextDirection toolTipDirection;
413 String toolTip = result.spellingToolTip(toolTipDirection);
415 // Next priority is a toolTip from a URL beneath the mouse (if preference is set to show those).
416 if (toolTip.isEmpty() && m_page->settings()->showsURLsInToolTips()) {
417 if (Node* node = result.innerNonSharedNode()) {
418 // Get tooltip representing form action, if relevant
419 if (node->hasTagName(inputTag)) {
420 HTMLInputElement* input = static_cast<HTMLInputElement*>(node);
421 if (input->isSubmitButton())
422 if (HTMLFormElement* form = input->form()) {
423 toolTip = form->action();
424 if (form->renderer())
425 toolTipDirection = form->renderer()->style()->direction();
427 toolTipDirection = LTR;
432 // Get tooltip representing link's URL
433 if (toolTip.isEmpty()) {
434 // FIXME: Need to pass this URL through userVisibleString once that's in WebCore
435 toolTip = result.absoluteLinkURL().string();
436 // URL always display as LTR.
437 toolTipDirection = LTR;
441 // Next we'll consider a tooltip for element with "title" attribute
442 if (toolTip.isEmpty())
443 toolTip = result.title(toolTipDirection);
445 if (toolTip.isEmpty() && m_page->settings()->showsToolTipOverTruncatedText())
446 toolTip = result.innerTextIfTruncated(toolTipDirection);
448 // Lastly, for <input type="file"> that allow multiple files, we'll consider a tooltip for the selected filenames
449 if (toolTip.isEmpty()) {
450 if (Node* node = result.innerNonSharedNode()) {
451 if (node->hasTagName(inputTag)) {
452 HTMLInputElement* input = static_cast<HTMLInputElement*>(node);
453 toolTip = input->defaultToolTip();
455 // FIXME: We should obtain text direction of tooltip from
456 // ChromeClient or platform. As of October 2011, all client
457 // implementations don't use text direction information for
458 // ChromeClient::setToolTip. We'll work on tooltip text
459 // direction during bidi cleanup in form inputs.
460 toolTipDirection = LTR;
465 m_client->setToolTip(toolTip, toolTipDirection);
468 void Chrome::print(Frame* frame)
470 // FIXME: This should have PageGroupLoadDeferrer, like runModal() or runJavaScriptAlert(), becasue it's no different from those.
471 m_client->print(frame);
474 #if ENABLE(DIRECTORY_UPLOAD)
475 void Chrome::enumerateChosenDirectory(FileChooser* fileChooser)
477 m_client->enumerateChosenDirectory(fileChooser);
481 #if ENABLE(INPUT_TYPE_COLOR)
482 PassOwnPtr<ColorChooser> Chrome::createColorChooser(ColorChooserClient* client, const Color& initialColor)
484 return m_client->createColorChooser(client, initialColor);
488 void Chrome::runOpenPanel(Frame* frame, PassRefPtr<FileChooser> fileChooser)
490 m_client->runOpenPanel(frame, fileChooser);
493 void Chrome::loadIconForFiles(const Vector<String>& filenames, FileIconLoader* loader)
495 m_client->loadIconForFiles(filenames, loader);
498 void Chrome::dispatchViewportPropertiesDidChange(const ViewportArguments& arguments) const
500 m_client->dispatchViewportPropertiesDidChange(arguments);
503 void Chrome::setCursor(const Cursor& cursor)
505 m_client->setCursor(cursor);
508 void Chrome::setCursorHiddenUntilMouseMoves(bool hiddenUntilMouseMoves)
510 m_client->setCursorHiddenUntilMouseMoves(hiddenUntilMouseMoves);
513 #if ENABLE(REQUEST_ANIMATION_FRAME)
514 void Chrome::scheduleAnimation()
516 #if !USE(REQUEST_ANIMATION_FRAME_TIMER)
517 m_client->scheduleAnimation();
524 #if ENABLE(DASHBOARD_SUPPORT)
525 void ChromeClient::dashboardRegionsChanged()
530 void ChromeClient::populateVisitedLinks()
534 FloatRect ChromeClient::customHighlightRect(Node*, const AtomicString&, const FloatRect&)
539 void ChromeClient::paintCustomHighlight(Node*, const AtomicString&, const FloatRect&, const FloatRect&, bool, bool)
543 bool ChromeClient::shouldReplaceWithGeneratedFileForUpload(const String&, String&)
548 String ChromeClient::generateReplacementFile(const String&)
550 ASSERT_NOT_REACHED();
554 bool ChromeClient::paintCustomOverhangArea(GraphicsContext*, const IntRect&, const IntRect&, const IntRect&)
559 bool Chrome::selectItemWritingDirectionIsNatural()
561 return m_client->selectItemWritingDirectionIsNatural();
564 bool Chrome::selectItemAlignmentFollowsMenuWritingDirection()
566 return m_client->selectItemAlignmentFollowsMenuWritingDirection();
569 bool Chrome::hasOpenedPopup() const
571 return m_client->hasOpenedPopup();
574 PassRefPtr<PopupMenu> Chrome::createPopupMenu(PopupMenuClient* client) const
576 return m_client->createPopupMenu(client);
579 PassRefPtr<SearchPopupMenu> Chrome::createSearchPopupMenu(PopupMenuClient* client) const
581 return m_client->createSearchPopupMenu(client);
584 bool Chrome::requiresFullscreenForVideoPlayback()
586 return m_client->requiresFullscreenForVideoPlayback();
589 } // namespace WebCore