initial import
[vuplus_webkit] / 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  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include "config.h"
22 #include "Chrome.h"
23
24 #include "ChromeClient.h"
25 #include "DNS.h"
26 #include "Document.h"
27 #include "FileIconLoader.h"
28 #include "FileChooser.h"
29 #include "FileList.h"
30 #include "FloatRect.h"
31 #include "Frame.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"
38 #include "Icon.h"
39 #include "InspectorInstrumentation.h"
40 #include "Page.h"
41 #include "PageGroupLoadDeferrer.h"
42 #include "RenderObject.h"
43 #include "ResourceHandle.h"
44 #include "SecurityOrigin.h"
45 #include "Settings.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>
51
52 #if ENABLE(DOM_STORAGE)
53 #include "StorageNamespace.h"
54 #endif
55
56 #if ENABLE(INPUT_COLOR)
57 #include "ColorChooser.h"
58 #endif
59
60 namespace WebCore {
61
62 using namespace HTMLNames;
63 using namespace std;
64
65 Chrome::Chrome(Page* page, ChromeClient* client)
66     : m_page(page)
67     , m_client(client)
68 {
69     ASSERT(m_client);
70 }
71
72 Chrome::~Chrome()
73 {
74     m_client->chromeDestroyed();
75 }
76
77 void Chrome::invalidateWindow(const IntRect& updateRect, bool immediate)
78 {
79     m_client->invalidateWindow(updateRect, immediate);
80 }
81
82 void Chrome::invalidateContentsAndWindow(const IntRect& updateRect, bool immediate)
83 {
84     m_client->invalidateContentsAndWindow(updateRect, immediate);
85 }
86
87 void Chrome::invalidateContentsForSlowScroll(const IntRect& updateRect, bool immediate)
88 {
89     m_client->invalidateContentsForSlowScroll(updateRect, immediate);
90 }
91
92 void Chrome::scroll(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect)
93 {
94     m_client->scroll(scrollDelta, rectToScroll, clipRect);
95 }
96
97 #if ENABLE(TILED_BACKING_STORE)
98 void Chrome::delegatedScrollRequested(const IntPoint& scrollPoint)
99 {
100     m_client->delegatedScrollRequested(scrollPoint);
101 }
102 #endif
103
104 IntPoint Chrome::screenToWindow(const IntPoint& point) const
105 {
106     return m_client->screenToWindow(point);
107 }
108
109 IntRect Chrome::windowToScreen(const IntRect& rect) const
110 {
111     return m_client->windowToScreen(rect);
112 }
113
114 PlatformPageClient Chrome::platformPageClient() const
115 {
116     return m_client->platformPageClient();
117 }
118
119 void Chrome::contentsSizeChanged(Frame* frame, const IntSize& size) const
120 {
121     m_client->contentsSizeChanged(frame, size);
122 }
123
124 void Chrome::layoutUpdated(Frame* frame) const
125 {
126     m_client->layoutUpdated(frame);
127 }
128
129 void Chrome::scrollRectIntoView(const IntRect& rect) const
130 {
131     m_client->scrollRectIntoView(rect);
132 }
133
134 void Chrome::scrollbarsModeDidChange() const
135 {
136     m_client->scrollbarsModeDidChange();
137 }
138
139 void Chrome::setWindowRect(const FloatRect& rect) const
140 {
141     m_client->setWindowRect(rect);
142 }
143
144 FloatRect Chrome::windowRect() const
145 {
146     return m_client->windowRect();
147 }
148
149 FloatRect Chrome::pageRect() const
150 {
151     return m_client->pageRect();
152 }
153
154 void Chrome::focus() const
155 {
156     m_client->focus();
157 }
158
159 void Chrome::unfocus() const
160 {
161     m_client->unfocus();
162 }
163
164 bool Chrome::canTakeFocus(FocusDirection direction) const
165 {
166     return m_client->canTakeFocus(direction);
167 }
168
169 void Chrome::takeFocus(FocusDirection direction) const
170 {
171     m_client->takeFocus(direction);
172 }
173
174 void Chrome::focusedNodeChanged(Node* node) const
175 {
176     m_client->focusedNodeChanged(node);
177 }
178
179 void Chrome::focusedFrameChanged(Frame* frame) const
180 {
181     m_client->focusedFrameChanged(frame);
182 }
183
184 Page* Chrome::createWindow(Frame* frame, const FrameLoadRequest& request, const WindowFeatures& features, const NavigationAction& action) const
185 {
186     Page* newPage = m_client->createWindow(frame, request, features, action);
187
188 #if ENABLE(DOM_STORAGE)
189     if (newPage) {
190         if (StorageNamespace* oldSessionStorage = m_page->sessionStorage(false))
191             newPage->setSessionStorage(oldSessionStorage->copy());
192     }
193 #endif
194
195     return newPage;
196 }
197
198 void Chrome::show() const
199 {
200     m_client->show();
201 }
202
203 bool Chrome::canRunModal() const
204 {
205     return m_client->canRunModal();
206 }
207
208 static bool canRunModalIfDuringPageDismissal(Page* page, ChromeClient::DialogType dialog, const String& message)
209 {
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);
214     }
215     return true;
216 }
217
218 bool Chrome::canRunModalNow() const
219 {
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());
224 }
225
226 void Chrome::runModal() const
227 {
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);
231
232     TimerBase::fireTimersInNestedEventLoop();
233     m_client->runModal();
234 }
235
236 void Chrome::setToolbarsVisible(bool b) const
237 {
238     m_client->setToolbarsVisible(b);
239 }
240
241 bool Chrome::toolbarsVisible() const
242 {
243     return m_client->toolbarsVisible();
244 }
245
246 void Chrome::setStatusbarVisible(bool b) const
247 {
248     m_client->setStatusbarVisible(b);
249 }
250
251 bool Chrome::statusbarVisible() const
252 {
253     return m_client->statusbarVisible();
254 }
255
256 void Chrome::setScrollbarsVisible(bool b) const
257 {
258     m_client->setScrollbarsVisible(b);
259 }
260
261 bool Chrome::scrollbarsVisible() const
262 {
263     return m_client->scrollbarsVisible();
264 }
265
266 void Chrome::setMenubarVisible(bool b) const
267 {
268     m_client->setMenubarVisible(b);
269 }
270
271 bool Chrome::menubarVisible() const
272 {
273     return m_client->menubarVisible();
274 }
275
276 void Chrome::setResizable(bool b) const
277 {
278     m_client->setResizable(b);
279 }
280
281 bool Chrome::canRunBeforeUnloadConfirmPanel()
282 {
283     return m_client->canRunBeforeUnloadConfirmPanel();
284 }
285
286 bool Chrome::runBeforeUnloadConfirmPanel(const String& message, Frame* frame)
287 {
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);
291
292     return m_client->runBeforeUnloadConfirmPanel(message, frame);
293 }
294
295 void Chrome::closeWindowSoon()
296 {
297     m_client->closeWindowSoon();
298 }
299
300 void Chrome::runJavaScriptAlert(Frame* frame, const String& message)
301 {
302     if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::AlertDialog, message))
303         return;
304
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);
308
309     ASSERT(frame);
310     m_client->runJavaScriptAlert(frame, frame->displayStringModifiedByEncoding(message));
311 }
312
313 bool Chrome::runJavaScriptConfirm(Frame* frame, const String& message)
314 {
315     if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::ConfirmDialog, message))
316         return false;
317
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);
321
322     ASSERT(frame);
323     return m_client->runJavaScriptConfirm(frame, frame->displayStringModifiedByEncoding(message));
324 }
325
326 bool Chrome::runJavaScriptPrompt(Frame* frame, const String& prompt, const String& defaultValue, String& result)
327 {
328     if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::PromptDialog, prompt))
329         return false;
330
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);
334
335     ASSERT(frame);
336     bool ok = m_client->runJavaScriptPrompt(frame, frame->displayStringModifiedByEncoding(prompt), frame->displayStringModifiedByEncoding(defaultValue), result);
337
338     if (ok)
339         result = frame->displayStringModifiedByEncoding(result);
340
341     return ok;
342 }
343
344 void Chrome::setStatusbarText(Frame* frame, const String& status)
345 {
346     ASSERT(frame);
347     m_client->setStatusbarText(frame->displayStringModifiedByEncoding(status));
348 }
349
350 bool Chrome::shouldInterruptJavaScript()
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     return m_client->shouldInterruptJavaScript();
357 }
358
359 #if ENABLE(REGISTER_PROTOCOL_HANDLER)
360 void Chrome::registerProtocolHandler(const String& scheme, const String& baseURL, const String& url, const String& title) 
361 {
362     m_client->registerProtocolHandler(scheme, baseURL, url, title);
363 }
364 #endif
365
366 IntRect Chrome::windowResizerRect() const
367 {
368     return m_client->windowResizerRect();
369 }
370
371 void Chrome::mouseDidMoveOverElement(const HitTestResult& result, unsigned modifierFlags)
372 {
373     if (result.innerNode()) {
374         Document* document = result.innerNode()->document();
375         if (document && document->isDNSPrefetchEnabled())
376             ResourceHandle::prepareForURL(result.absoluteLinkURL());
377     }
378     m_client->mouseDidMoveOverElement(result, modifierFlags);
379
380     InspectorInstrumentation::mouseDidMoveOverElement(m_page, result, modifierFlags);
381 }
382
383 void Chrome::setToolTip(const HitTestResult& result)
384 {
385     // First priority is a potential toolTip representing a spelling or grammar error
386     TextDirection toolTipDirection;
387     String toolTip = result.spellingToolTip(toolTipDirection);
388
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();
400                         else
401                             toolTipDirection = LTR;
402                     }
403             }
404         }
405
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;
412         }
413     }
414
415     // Next we'll consider a tooltip for element with "title" attribute
416     if (toolTip.isEmpty())
417         toolTip = result.title(toolTipDirection);
418
419     if (toolTip.isEmpty() && m_page->settings()->showsToolTipOverTruncatedText())
420         toolTip = result.innerTextIfTruncated(toolTipDirection);
421
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();
430                     if (listSize > 1) {
431                         StringBuilder names;
432                         for (size_t i = 0; i < listSize; ++i) {
433                             names.append(files->item(i)->fileName());
434                             if (i != listSize - 1)
435                                 names.append('\n');
436                         }
437                         toolTip = names.toString();
438                         // filename always display as LTR.
439                         toolTipDirection = LTR;
440                     }
441                 }
442             }
443         }
444     }
445
446     m_client->setToolTip(toolTip, toolTipDirection);
447 }
448
449 void Chrome::print(Frame* frame)
450 {
451     // FIXME: This should have PageGroupLoadDeferrer, like runModal() or runJavaScriptAlert(), becasue it's no different from those.
452     m_client->print(frame);
453 }
454
455 void Chrome::requestGeolocationPermissionForFrame(Frame* frame, Geolocation* geolocation)
456 {
457     m_client->requestGeolocationPermissionForFrame(frame, geolocation);
458 }
459
460 void Chrome::cancelGeolocationPermissionRequestForFrame(Frame* frame, Geolocation* geolocation)
461 {
462     m_client->cancelGeolocationPermissionRequestForFrame(frame, geolocation);
463 }
464
465 #if ENABLE(DIRECTORY_UPLOAD)
466 void Chrome::enumerateChosenDirectory(FileChooser* fileChooser)
467 {
468     m_client->enumerateChosenDirectory(fileChooser);
469 }
470 #endif
471
472 #if ENABLE(INPUT_COLOR)
473 void Chrome::openColorChooser(ColorChooser* colorChooser, const Color& initialColor)
474 {
475     m_client->openColorChooser(colorChooser, initialColor);
476 }
477
478 void Chrome::cleanupColorChooser()
479 {
480     m_client->cleanupColorChooser();
481 }
482
483 void Chrome::setSelectedColorInColorChooser(const Color& color)
484 {
485     m_client->setSelectedColorInColorChooser(color);
486 }
487 #endif
488
489 void Chrome::runOpenPanel(Frame* frame, PassRefPtr<FileChooser> fileChooser)
490 {
491     m_client->runOpenPanel(frame, fileChooser);
492 }
493
494 void Chrome::loadIconForFiles(const Vector<String>& filenames, FileIconLoader* loader)
495 {
496     m_client->loadIconForFiles(filenames, loader);
497 }
498
499 void Chrome::dispatchViewportDataDidChange(const ViewportArguments& arguments) const
500 {
501     m_client->dispatchViewportDataDidChange(arguments);
502 }
503
504 void Chrome::setCursor(const Cursor& cursor)
505 {
506     m_client->setCursor(cursor);
507 }
508
509 void Chrome::setCursorHiddenUntilMouseMoves(bool hiddenUntilMouseMoves)
510 {
511     m_client->setCursorHiddenUntilMouseMoves(hiddenUntilMouseMoves);
512 }
513
514 #if ENABLE(REQUEST_ANIMATION_FRAME)
515 void Chrome::scheduleAnimation()
516 {
517 #if !USE(REQUEST_ANIMATION_FRAME_TIMER)
518     m_client->scheduleAnimation();
519 #endif
520 }
521 #endif
522
523 #if ENABLE(NOTIFICATIONS)
524 NotificationPresenter* Chrome::notificationPresenter() const
525 {
526     return m_client->notificationPresenter();
527 }
528 #endif
529
530 // --------
531
532 #if ENABLE(DASHBOARD_SUPPORT)
533 void ChromeClient::dashboardRegionsChanged()
534 {
535 }
536 #endif
537
538 void ChromeClient::populateVisitedLinks()
539 {
540 }
541
542 FloatRect ChromeClient::customHighlightRect(Node*, const AtomicString&, const FloatRect&)
543 {
544     return FloatRect();
545 }
546
547 void ChromeClient::paintCustomHighlight(Node*, const AtomicString&, const FloatRect&, const FloatRect&, bool, bool)
548 {
549 }
550
551 bool ChromeClient::shouldReplaceWithGeneratedFileForUpload(const String&, String&)
552 {
553     return false;
554 }
555
556 String ChromeClient::generateReplacementFile(const String&)
557 {
558     ASSERT_NOT_REACHED();
559     return String();
560 }
561
562 bool ChromeClient::paintCustomScrollbar(GraphicsContext*, const FloatRect&, ScrollbarControlSize,
563                                         ScrollbarControlState, ScrollbarPart, bool,
564                                         float, float, ScrollbarControlPartMask)
565 {
566     return false;
567 }
568
569 bool ChromeClient::paintCustomScrollCorner(GraphicsContext*, const FloatRect&)
570 {
571     return false;
572 }
573
574 bool ChromeClient::paintCustomOverhangArea(GraphicsContext*, const IntRect&, const IntRect&, const IntRect&)
575 {
576     return false;
577 }
578
579 bool Chrome::selectItemWritingDirectionIsNatural()
580 {
581     return m_client->selectItemWritingDirectionIsNatural();
582 }
583
584 bool Chrome::selectItemAlignmentFollowsMenuWritingDirection()
585 {
586     return m_client->selectItemAlignmentFollowsMenuWritingDirection();
587 }
588
589 PassRefPtr<PopupMenu> Chrome::createPopupMenu(PopupMenuClient* client) const
590 {
591     return m_client->createPopupMenu(client);
592 }
593
594 PassRefPtr<SearchPopupMenu> Chrome::createSearchPopupMenu(PopupMenuClient* client) const
595 {
596     return m_client->createSearchPopupMenu(client);
597 }
598
599 #if ENABLE(CONTEXT_MENUS)
600 void Chrome::showContextMenu()
601 {
602     m_client->showContextMenu();
603 }
604 #endif
605
606 bool Chrome::requiresFullscreenForVideoPlayback()
607 {
608     return m_client->requiresFullscreenForVideoPlayback();
609 }
610
611 } // namespace WebCore