Merge "[CherryPick] Refactoring: Move the content of HTMLInputElement::subtreeHasChan...
[framework/web/webkit-efl.git] / Source / WebCore / page / Chrome.cpp
1 /*
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.
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include "config.h"
23 #include "Chrome.h"
24
25 #include "ChromeClient.h"
26 #include "DNS.h"
27 #include "Document.h"
28 #include "FileIconLoader.h"
29 #include "FileChooser.h"
30 #include "FileList.h"
31 #include "FloatRect.h"
32 #include "Frame.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"
39 #include "Icon.h"
40 #include "InspectorInstrumentation.h"
41 #include "Page.h"
42 #include "PageGroupLoadDeferrer.h"
43 #include "RenderObject.h"
44 #include "ResourceHandle.h"
45 #include "SecurityOrigin.h"
46 #include "Settings.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>
53
54 #if ENABLE(INPUT_TYPE_COLOR)
55 #include "ColorChooser.h"
56 #endif
57
58 namespace WebCore {
59
60 using namespace HTMLNames;
61 using namespace std;
62
63 Chrome::Chrome(Page* page, ChromeClient* client)
64     : m_page(page)
65     , m_client(client)
66 {
67     ASSERT(m_client);
68 }
69
70 Chrome::~Chrome()
71 {
72     m_client->chromeDestroyed();
73 }
74
75 PassOwnPtr<Chrome> Chrome::create(Page* page, ChromeClient* client)
76 {
77     return adoptPtr(new Chrome(page, client));
78 }
79
80 void Chrome::invalidateRootView(const IntRect& updateRect, bool immediate)
81 {
82     m_client->invalidateRootView(updateRect, immediate);
83 }
84
85 void Chrome::invalidateContentsAndRootView(const IntRect& updateRect, bool immediate)
86 {
87     m_client->invalidateContentsAndRootView(updateRect, immediate);
88 }
89
90 void Chrome::invalidateContentsForSlowScroll(const IntRect& updateRect, bool immediate)
91 {
92     m_client->invalidateContentsForSlowScroll(updateRect, immediate);
93 }
94
95 void Chrome::scroll(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect)
96 {
97     m_client->scroll(scrollDelta, rectToScroll, clipRect);
98 }
99
100 #if USE(TILED_BACKING_STORE)
101 void Chrome::delegatedScrollRequested(const IntPoint& scrollPoint)
102 {
103     m_client->delegatedScrollRequested(scrollPoint);
104 }
105 #endif
106
107 #if ENABLE(TIZEN_WEBKIT2_HISTORICAL_RESTORE_VISIBLE_CONTENT_RECT)
108 void Chrome::requestVisibleContentRectRestore(const IntPoint& scrollOffset, float scale)
109 {
110     m_client->requestVisibleContentRectRestore(scrollOffset, scale);
111 }
112
113 float Chrome::contentsScaleFactor() const
114 {
115     return m_client->contentsScaleFactor();
116 }
117 #endif
118
119 IntPoint Chrome::screenToRootView(const IntPoint& point) const
120 {
121     return m_client->screenToRootView(point);
122 }
123
124 IntRect Chrome::rootViewToScreen(const IntRect& rect) const
125 {
126     return m_client->rootViewToScreen(rect);
127 }
128
129 PlatformPageClient Chrome::platformPageClient() const
130 {
131     return m_client->platformPageClient();
132 }
133
134 void Chrome::contentsSizeChanged(Frame* frame, const IntSize& size) const
135 {
136     m_client->contentsSizeChanged(frame, size);
137 }
138
139 void Chrome::layoutUpdated(Frame* frame) const
140 {
141     m_client->layoutUpdated(frame);
142 }
143
144 void Chrome::scrollRectIntoView(const IntRect& rect) const
145 {
146     m_client->scrollRectIntoView(rect);
147 }
148
149 void Chrome::scrollbarsModeDidChange() const
150 {
151     m_client->scrollbarsModeDidChange();
152 }
153
154 void Chrome::setWindowRect(const FloatRect& rect) const
155 {
156     m_client->setWindowRect(rect);
157 }
158
159 FloatRect Chrome::windowRect() const
160 {
161     return m_client->windowRect();
162 }
163
164 FloatRect Chrome::pageRect() const
165 {
166     return m_client->pageRect();
167 }
168
169 void Chrome::focus() const
170 {
171     m_client->focus();
172 }
173
174 void Chrome::unfocus() const
175 {
176     m_client->unfocus();
177 }
178
179 bool Chrome::canTakeFocus(FocusDirection direction) const
180 {
181     return m_client->canTakeFocus(direction);
182 }
183
184 void Chrome::takeFocus(FocusDirection direction) const
185 {
186     m_client->takeFocus(direction);
187 }
188
189 void Chrome::focusedNodeChanged(Node* node) const
190 {
191     m_client->focusedNodeChanged(node);
192 }
193
194 void Chrome::focusedFrameChanged(Frame* frame) const
195 {
196     m_client->focusedFrameChanged(frame);
197 }
198
199 Page* Chrome::createWindow(Frame* frame, const FrameLoadRequest& request, const WindowFeatures& features, const NavigationAction& action) const
200 {
201     Page* newPage = m_client->createWindow(frame, request, features, action);
202
203     if (newPage) {
204         if (StorageNamespace* oldSessionStorage = m_page->sessionStorage(false))
205             newPage->setSessionStorage(oldSessionStorage->copy());
206     }
207
208     return newPage;
209 }
210
211 void Chrome::show() const
212 {
213     m_client->show();
214 }
215
216 bool Chrome::canRunModal() const
217 {
218     return m_client->canRunModal();
219 }
220
221 static bool canRunModalIfDuringPageDismissal(Page* page, ChromeClient::DialogType dialog, const String& message)
222 {
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);
227     }
228     return true;
229 }
230
231 bool Chrome::canRunModalNow() const
232 {
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());
237 }
238
239 void Chrome::runModal() const
240 {
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);
244
245     TimerBase::fireTimersInNestedEventLoop();
246     m_client->runModal();
247 }
248
249 void Chrome::setToolbarsVisible(bool b) const
250 {
251     m_client->setToolbarsVisible(b);
252 }
253
254 bool Chrome::toolbarsVisible() const
255 {
256     return m_client->toolbarsVisible();
257 }
258
259 void Chrome::setStatusbarVisible(bool b) const
260 {
261     m_client->setStatusbarVisible(b);
262 }
263
264 bool Chrome::statusbarVisible() const
265 {
266     return m_client->statusbarVisible();
267 }
268
269 void Chrome::setScrollbarsVisible(bool b) const
270 {
271     m_client->setScrollbarsVisible(b);
272 }
273
274 bool Chrome::scrollbarsVisible() const
275 {
276     return m_client->scrollbarsVisible();
277 }
278
279 void Chrome::setMenubarVisible(bool b) const
280 {
281     m_client->setMenubarVisible(b);
282 }
283
284 bool Chrome::menubarVisible() const
285 {
286     return m_client->menubarVisible();
287 }
288
289 void Chrome::setResizable(bool b) const
290 {
291     m_client->setResizable(b);
292 }
293
294 bool Chrome::canRunBeforeUnloadConfirmPanel()
295 {
296     return m_client->canRunBeforeUnloadConfirmPanel();
297 }
298
299 bool Chrome::runBeforeUnloadConfirmPanel(const String& message, Frame* frame)
300 {
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);
304
305     return m_client->runBeforeUnloadConfirmPanel(message, frame);
306 }
307
308 void Chrome::closeWindowSoon()
309 {
310     m_client->closeWindowSoon();
311 }
312
313 void Chrome::runJavaScriptAlert(Frame* frame, const String& message)
314 {
315     if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::AlertDialog, message))
316         return;
317
318 #if ENABLE(TIZEN_ISF_PORT)
319     Node* node = frame ? frame->document()->focusedNode() : 0;
320     if (node
321         && node->nodeType() == Node::ELEMENT_NODE
322         && node->hasTagName(HTMLNames::inputTag))
323         static_cast<HTMLInputElement*>(node)->blur();
324 #endif
325
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);
329
330     ASSERT(frame);
331     m_client->runJavaScriptAlert(frame, frame->displayStringModifiedByEncoding(message));
332 }
333
334 bool Chrome::runJavaScriptConfirm(Frame* frame, const String& message)
335 {
336     if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::ConfirmDialog, message))
337         return false;
338
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);
342
343     ASSERT(frame);
344     return m_client->runJavaScriptConfirm(frame, frame->displayStringModifiedByEncoding(message));
345 }
346
347 bool Chrome::runJavaScriptPrompt(Frame* frame, const String& prompt, const String& defaultValue, String& result)
348 {
349     if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::PromptDialog, prompt))
350         return false;
351
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);
355
356     ASSERT(frame);
357     bool ok = m_client->runJavaScriptPrompt(frame, frame->displayStringModifiedByEncoding(prompt), frame->displayStringModifiedByEncoding(defaultValue), result);
358
359     if (ok)
360         result = frame->displayStringModifiedByEncoding(result);
361
362     return ok;
363 }
364
365 void Chrome::setStatusbarText(Frame* frame, const String& status)
366 {
367     ASSERT(frame);
368     m_client->setStatusbarText(frame->displayStringModifiedByEncoding(status));
369 }
370
371 bool Chrome::shouldInterruptJavaScript()
372 {
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);
376
377     return m_client->shouldInterruptJavaScript();
378 }
379
380 #if ENABLE(TIZEN_SEARCH_PROVIDER)
381 void Chrome::addSearchProvider(const String& baseURL, const String& engineURL)
382 {
383     return m_client->addSearchProvider(baseURL, engineURL);
384 }
385
386 unsigned long Chrome::isSearchProviderInstalled(const String& baseURL, const String& engineURL)
387 {
388     return m_client->isSearchProviderInstalled(baseURL, engineURL);
389 }
390 #endif
391
392 IntRect Chrome::windowResizerRect() const
393 {
394     return m_client->windowResizerRect();
395 }
396
397 void Chrome::mouseDidMoveOverElement(const HitTestResult& result, unsigned modifierFlags)
398 {
399     if (result.innerNode()) {
400         Document* document = result.innerNode()->document();
401         if (document && document->isDNSPrefetchEnabled())
402             prefetchDNS(result.absoluteLinkURL().host());
403     }
404     m_client->mouseDidMoveOverElement(result, modifierFlags);
405
406     InspectorInstrumentation::mouseDidMoveOverElement(m_page, result, modifierFlags);
407 }
408
409 void Chrome::setToolTip(const HitTestResult& result)
410 {
411     // First priority is a potential toolTip representing a spelling or grammar error
412     TextDirection toolTipDirection;
413     String toolTip = result.spellingToolTip(toolTipDirection);
414
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();
426                         else
427                             toolTipDirection = LTR;
428                     }
429             }
430         }
431
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;
438         }
439     }
440
441     // Next we'll consider a tooltip for element with "title" attribute
442     if (toolTip.isEmpty())
443         toolTip = result.title(toolTipDirection);
444
445     if (toolTip.isEmpty() && m_page->settings()->showsToolTipOverTruncatedText())
446         toolTip = result.innerTextIfTruncated(toolTipDirection);
447
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();
454
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;
461             }
462         }
463     }
464
465     m_client->setToolTip(toolTip, toolTipDirection);
466 }
467
468 void Chrome::print(Frame* frame)
469 {
470     // FIXME: This should have PageGroupLoadDeferrer, like runModal() or runJavaScriptAlert(), becasue it's no different from those.
471     m_client->print(frame);
472 }
473
474 #if ENABLE(DIRECTORY_UPLOAD)
475 void Chrome::enumerateChosenDirectory(FileChooser* fileChooser)
476 {
477     m_client->enumerateChosenDirectory(fileChooser);
478 }
479 #endif
480
481 #if ENABLE(INPUT_TYPE_COLOR)
482 PassOwnPtr<ColorChooser> Chrome::createColorChooser(ColorChooserClient* client, const Color& initialColor)
483 {
484     return m_client->createColorChooser(client, initialColor);
485 }
486 #endif
487
488 void Chrome::runOpenPanel(Frame* frame, PassRefPtr<FileChooser> fileChooser)
489 {
490     m_client->runOpenPanel(frame, fileChooser);
491 }
492
493 void Chrome::loadIconForFiles(const Vector<String>& filenames, FileIconLoader* loader)
494 {
495     m_client->loadIconForFiles(filenames, loader);
496 }
497
498 void Chrome::dispatchViewportPropertiesDidChange(const ViewportArguments& arguments) const
499 {
500     m_client->dispatchViewportPropertiesDidChange(arguments);
501 }
502
503 void Chrome::setCursor(const Cursor& cursor)
504 {
505     m_client->setCursor(cursor);
506 }
507
508 void Chrome::setCursorHiddenUntilMouseMoves(bool hiddenUntilMouseMoves)
509 {
510     m_client->setCursorHiddenUntilMouseMoves(hiddenUntilMouseMoves);
511 }
512
513 #if ENABLE(REQUEST_ANIMATION_FRAME)
514 void Chrome::scheduleAnimation()
515 {
516 #if !USE(REQUEST_ANIMATION_FRAME_TIMER)
517     m_client->scheduleAnimation();
518 #endif
519 }
520 #endif
521
522 // --------
523
524 #if ENABLE(DASHBOARD_SUPPORT)
525 void ChromeClient::dashboardRegionsChanged()
526 {
527 }
528 #endif
529
530 void ChromeClient::populateVisitedLinks()
531 {
532 }
533
534 FloatRect ChromeClient::customHighlightRect(Node*, const AtomicString&, const FloatRect&)
535 {
536     return FloatRect();
537 }
538
539 void ChromeClient::paintCustomHighlight(Node*, const AtomicString&, const FloatRect&, const FloatRect&, bool, bool)
540 {
541 }
542
543 bool ChromeClient::shouldReplaceWithGeneratedFileForUpload(const String&, String&)
544 {
545     return false;
546 }
547
548 String ChromeClient::generateReplacementFile(const String&)
549 {
550     ASSERT_NOT_REACHED();
551     return String();
552 }
553
554 bool ChromeClient::paintCustomOverhangArea(GraphicsContext*, const IntRect&, const IntRect&, const IntRect&)
555 {
556     return false;
557 }
558
559 bool Chrome::selectItemWritingDirectionIsNatural()
560 {
561     return m_client->selectItemWritingDirectionIsNatural();
562 }
563
564 bool Chrome::selectItemAlignmentFollowsMenuWritingDirection()
565 {
566     return m_client->selectItemAlignmentFollowsMenuWritingDirection();
567 }
568
569 bool Chrome::hasOpenedPopup() const
570 {
571     return m_client->hasOpenedPopup();
572 }
573
574 PassRefPtr<PopupMenu> Chrome::createPopupMenu(PopupMenuClient* client) const
575 {
576     return m_client->createPopupMenu(client);
577 }
578
579 PassRefPtr<SearchPopupMenu> Chrome::createSearchPopupMenu(PopupMenuClient* client) const
580 {
581     return m_client->createSearchPopupMenu(client);
582 }
583
584 bool Chrome::requiresFullscreenForVideoPlayback()
585 {
586     return m_client->requiresFullscreenForVideoPlayback();
587 }
588
589 } // namespace WebCore