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