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