Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/page/Chrome.h"
24
25 #include "public/platform/WebScreenInfo.h"
26 #include "HTMLNames.h"
27 #include "core/dom/Document.h"
28 #include "core/html/HTMLInputElement.h"
29 #include "core/inspector/InspectorInstrumentation.h"
30 #include "core/page/ChromeClient.h"
31 #include "core/frame/Frame.h"
32 #include "core/page/FrameTree.h"
33 #include "core/page/Page.h"
34 #include "core/page/PageGroupLoadDeferrer.h"
35 #include "core/page/PopupOpeningObserver.h"
36 #include "core/page/WindowFeatures.h"
37 #include "core/rendering/HitTestResult.h"
38 #include "platform/ColorChooser.h"
39 #include "platform/DateTimeChooser.h"
40 #include "platform/FileChooser.h"
41 #include "platform/geometry/FloatRect.h"
42 #include "platform/network/DNS.h"
43 #include "wtf/PassRefPtr.h"
44 #include "wtf/Vector.h"
45
46 namespace WebCore {
47
48 using namespace HTMLNames;
49
50 Chrome::Chrome(Page* page, ChromeClient* client)
51     : m_page(page)
52     , m_client(client)
53 {
54     ASSERT(m_client);
55 }
56
57 Chrome::~Chrome()
58 {
59     m_client->chromeDestroyed();
60 }
61
62 PassOwnPtr<Chrome> Chrome::create(Page* page, ChromeClient* client)
63 {
64     return adoptPtr(new Chrome(page, client));
65 }
66
67 void Chrome::invalidateContentsAndRootView(const IntRect& updateRect)
68 {
69     m_client->invalidateContentsAndRootView(updateRect);
70 }
71
72 void Chrome::invalidateContentsForSlowScroll(const IntRect& updateRect)
73 {
74     m_client->invalidateContentsForSlowScroll(updateRect);
75 }
76
77 void Chrome::scroll(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect)
78 {
79     m_client->scroll(scrollDelta, rectToScroll, clipRect);
80     InspectorInstrumentation::didScroll(m_page);
81 }
82
83 IntPoint Chrome::screenToRootView(const IntPoint& point) const
84 {
85     return m_client->screenToRootView(point);
86 }
87
88 IntRect Chrome::rootViewToScreen(const IntRect& rect) const
89 {
90     return m_client->rootViewToScreen(rect);
91 }
92
93 blink::WebScreenInfo Chrome::screenInfo() const
94 {
95     return m_client->screenInfo();
96 }
97
98 void Chrome::contentsSizeChanged(Frame* frame, const IntSize& size) const
99 {
100     m_client->contentsSizeChanged(frame, size);
101 }
102
103 void Chrome::layoutUpdated(Frame* frame) const
104 {
105     m_client->layoutUpdated(frame);
106 }
107
108 void Chrome::setWindowRect(const FloatRect& rect) const
109 {
110     m_client->setWindowRect(rect);
111 }
112
113 FloatRect Chrome::windowRect() const
114 {
115     return m_client->windowRect();
116 }
117
118 FloatRect Chrome::pageRect() const
119 {
120     return m_client->pageRect();
121 }
122
123 void Chrome::focus() const
124 {
125     m_client->focus();
126 }
127
128 void Chrome::unfocus() const
129 {
130     m_client->unfocus();
131 }
132
133 bool Chrome::canTakeFocus(FocusDirection direction) const
134 {
135     return m_client->canTakeFocus(direction);
136 }
137
138 void Chrome::takeFocus(FocusDirection direction) const
139 {
140     m_client->takeFocus(direction);
141 }
142
143 void Chrome::focusedNodeChanged(Node* node) const
144 {
145     m_client->focusedNodeChanged(node);
146 }
147
148 void Chrome::show(NavigationPolicy policy) const
149 {
150     m_client->show(policy);
151 }
152
153 bool Chrome::canRunModal() const
154 {
155     return m_client->canRunModal();
156 }
157
158 static bool canRunModalIfDuringPageDismissal(Page* page, ChromeClient::DialogType dialog, const String& message)
159 {
160     for (Frame* frame = page->mainFrame(); frame; frame = frame->tree().traverseNext()) {
161         Document::PageDismissalType dismissal = frame->document()->pageDismissalEventBeingDispatched();
162         if (dismissal != Document::NoDismissal)
163             return page->chrome().client().shouldRunModalDialogDuringPageDismissal(dialog, message, dismissal);
164     }
165     return true;
166 }
167
168 bool Chrome::canRunModalNow() const
169 {
170     return canRunModal() && canRunModalIfDuringPageDismissal(m_page, ChromeClient::HTMLDialog, String());
171 }
172
173 void Chrome::runModal() const
174 {
175     // Defer callbacks in all the other pages in this group, so we don't try to run JavaScript
176     // in a way that could interact with this view.
177     PageGroupLoadDeferrer deferrer(m_page, false);
178
179     TimerBase::fireTimersInNestedEventLoop();
180     m_client->runModal();
181 }
182
183 void Chrome::setWindowFeatures(const WindowFeatures& features) const
184 {
185     m_client->setToolbarsVisible(features.toolBarVisible || features.locationBarVisible);
186     m_client->setStatusbarVisible(features.statusBarVisible);
187     m_client->setScrollbarsVisible(features.scrollbarsVisible);
188     m_client->setMenubarVisible(features.menuBarVisible);
189     m_client->setResizable(features.resizable);
190 }
191
192 bool Chrome::toolbarsVisible() const
193 {
194     return m_client->toolbarsVisible();
195 }
196
197 bool Chrome::statusbarVisible() const
198 {
199     return m_client->statusbarVisible();
200 }
201
202 bool Chrome::scrollbarsVisible() const
203 {
204     return m_client->scrollbarsVisible();
205 }
206
207 bool Chrome::menubarVisible() const
208 {
209     return m_client->menubarVisible();
210 }
211
212 bool Chrome::canRunBeforeUnloadConfirmPanel()
213 {
214     return m_client->canRunBeforeUnloadConfirmPanel();
215 }
216
217 bool Chrome::runBeforeUnloadConfirmPanel(const String& message, Frame* frame)
218 {
219     // Defer loads in case the client method runs a new event loop that would
220     // otherwise cause the load to continue while we're in the middle of executing JavaScript.
221     PageGroupLoadDeferrer deferrer(m_page, true);
222
223     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willRunJavaScriptDialog(m_page, message);
224     bool ok = m_client->runBeforeUnloadConfirmPanel(message, frame);
225     InspectorInstrumentation::didRunJavaScriptDialog(cookie);
226     return ok;
227 }
228
229 void Chrome::closeWindowSoon()
230 {
231     m_client->closeWindowSoon();
232 }
233
234 void Chrome::runJavaScriptAlert(Frame* frame, const String& message)
235 {
236     if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::AlertDialog, message))
237         return;
238
239     // Defer loads in case the client method runs a new event loop that would
240     // otherwise cause the load to continue while we're in the middle of executing JavaScript.
241     PageGroupLoadDeferrer deferrer(m_page, true);
242
243     ASSERT(frame);
244     notifyPopupOpeningObservers();
245
246     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willRunJavaScriptDialog(m_page, message);
247     m_client->runJavaScriptAlert(frame, message);
248     InspectorInstrumentation::didRunJavaScriptDialog(cookie);
249 }
250
251 bool Chrome::runJavaScriptConfirm(Frame* frame, const String& message)
252 {
253     if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::ConfirmDialog, message))
254         return false;
255
256     // Defer loads in case the client method runs a new event loop that would
257     // otherwise cause the load to continue while we're in the middle of executing JavaScript.
258     PageGroupLoadDeferrer deferrer(m_page, true);
259
260     ASSERT(frame);
261     notifyPopupOpeningObservers();
262
263     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willRunJavaScriptDialog(m_page, message);
264     bool ok = m_client->runJavaScriptConfirm(frame, message);
265     InspectorInstrumentation::didRunJavaScriptDialog(cookie);
266     return ok;
267 }
268
269 bool Chrome::runJavaScriptPrompt(Frame* frame, const String& prompt, const String& defaultValue, String& result)
270 {
271     if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::PromptDialog, prompt))
272         return false;
273
274     // Defer loads in case the client method runs a new event loop that would
275     // otherwise cause the load to continue while we're in the middle of executing JavaScript.
276     PageGroupLoadDeferrer deferrer(m_page, true);
277
278     ASSERT(frame);
279     notifyPopupOpeningObservers();
280
281     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willRunJavaScriptDialog(m_page, prompt);
282     bool ok = m_client->runJavaScriptPrompt(frame, prompt, defaultValue, result);
283     InspectorInstrumentation::didRunJavaScriptDialog(cookie);
284
285     return ok;
286 }
287
288 void Chrome::setStatusbarText(Frame* frame, const String& status)
289 {
290     ASSERT(frame);
291     m_client->setStatusbarText(status);
292 }
293
294 IntRect Chrome::windowResizerRect() const
295 {
296     return m_client->windowResizerRect();
297 }
298
299 void Chrome::mouseDidMoveOverElement(const HitTestResult& result, unsigned modifierFlags)
300 {
301     if (result.innerNode()) {
302         if (result.innerNode()->document().isDNSPrefetchEnabled())
303             prefetchDNS(result.absoluteLinkURL().host());
304     }
305     m_client->mouseDidMoveOverElement(result, modifierFlags);
306 }
307
308 void Chrome::setToolTip(const HitTestResult& result)
309 {
310     // First priority is a potential toolTip representing a spelling or grammar error
311     TextDirection toolTipDirection;
312     String toolTip = result.spellingToolTip(toolTipDirection);
313
314     // Next we'll consider a tooltip for element with "title" attribute
315     if (toolTip.isEmpty())
316         toolTip = result.title(toolTipDirection);
317
318     // Lastly, for <input type="file"> that allow multiple files, we'll consider a tooltip for the selected filenames
319     if (toolTip.isEmpty()) {
320         if (Node* node = result.innerNonSharedNode()) {
321             if (node->hasTagName(inputTag)) {
322                 HTMLInputElement* input = toHTMLInputElement(node);
323                 toolTip = input->defaultToolTip();
324
325                 // FIXME: We should obtain text direction of tooltip from
326                 // ChromeClient or platform. As of October 2011, all client
327                 // implementations don't use text direction information for
328                 // ChromeClient::setToolTip. We'll work on tooltip text
329                 // direction during bidi cleanup in form inputs.
330                 toolTipDirection = LTR;
331             }
332         }
333     }
334
335     m_client->setToolTip(toolTip, toolTipDirection);
336 }
337
338 void Chrome::print(Frame* frame)
339 {
340     // FIXME: This should have PageGroupLoadDeferrer, like runModal() or runJavaScriptAlert(), becasue it's no different from those.
341     m_client->print(frame);
342 }
343
344 void Chrome::enumerateChosenDirectory(FileChooser* fileChooser)
345 {
346     m_client->enumerateChosenDirectory(fileChooser);
347 }
348
349 PassOwnPtr<ColorChooser> Chrome::createColorChooser(ColorChooserClient* client, const Color& initialColor)
350 {
351     notifyPopupOpeningObservers();
352     return m_client->createColorChooser(client, initialColor);
353 }
354
355 PassRefPtr<DateTimeChooser> Chrome::openDateTimeChooser(DateTimeChooserClient* client, const DateTimeChooserParameters& parameters)
356 {
357     notifyPopupOpeningObservers();
358     return m_client->openDateTimeChooser(client, parameters);
359 }
360
361 void Chrome::openTextDataListChooser(HTMLInputElement& input)
362 {
363     notifyPopupOpeningObservers();
364     m_client->openTextDataListChooser(input);
365 }
366
367 void Chrome::runOpenPanel(Frame* frame, PassRefPtr<FileChooser> fileChooser)
368 {
369     notifyPopupOpeningObservers();
370     m_client->runOpenPanel(frame, fileChooser);
371 }
372
373 void Chrome::dispatchViewportPropertiesDidChange(const ViewportDescription& description) const
374 {
375     m_client->dispatchViewportPropertiesDidChange(description);
376 }
377
378 void Chrome::setCursor(const Cursor& cursor)
379 {
380     m_client->setCursor(cursor);
381 }
382
383 void Chrome::scheduleAnimation()
384 {
385     m_client->scheduleAnimation();
386 }
387
388 // --------
389
390 bool Chrome::hasOpenedPopup() const
391 {
392     return m_client->hasOpenedPopup();
393 }
394
395 PassRefPtr<PopupMenu> Chrome::createPopupMenu(Frame& frame, PopupMenuClient* client) const
396 {
397     notifyPopupOpeningObservers();
398     return m_client->createPopupMenu(frame, client);
399 }
400
401 void Chrome::registerPopupOpeningObserver(PopupOpeningObserver* observer)
402 {
403     ASSERT(observer);
404     m_popupOpeningObservers.append(observer);
405 }
406
407 void Chrome::unregisterPopupOpeningObserver(PopupOpeningObserver* observer)
408 {
409     size_t index = m_popupOpeningObservers.find(observer);
410     ASSERT(index != kNotFound);
411     m_popupOpeningObservers.remove(index);
412 }
413
414 void Chrome::notifyPopupOpeningObservers() const
415 {
416     const Vector<PopupOpeningObserver*> observers(m_popupOpeningObservers);
417     for (size_t i = 0; i < observers.size(); ++i)
418         observers[i]->willOpenPopup();
419 }
420
421 } // namespace WebCore