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.
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.
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.
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.
23 #include "core/page/Chrome.h"
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"
48 using namespace HTMLNames;
50 Chrome::Chrome(Page* page, ChromeClient* client)
59 m_client->chromeDestroyed();
62 PassOwnPtr<Chrome> Chrome::create(Page* page, ChromeClient* client)
64 return adoptPtr(new Chrome(page, client));
67 void Chrome::invalidateContentsAndRootView(const IntRect& updateRect)
69 m_client->invalidateContentsAndRootView(updateRect);
72 void Chrome::invalidateContentsForSlowScroll(const IntRect& updateRect)
74 m_client->invalidateContentsForSlowScroll(updateRect);
77 void Chrome::scroll(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect)
79 m_client->scroll(scrollDelta, rectToScroll, clipRect);
80 InspectorInstrumentation::didScroll(m_page);
83 IntRect Chrome::rootViewToScreen(const IntRect& rect) const
85 return m_client->rootViewToScreen(rect);
88 blink::WebScreenInfo Chrome::screenInfo() const
90 return m_client->screenInfo();
93 void Chrome::contentsSizeChanged(Frame* frame, const IntSize& size) const
95 m_client->contentsSizeChanged(frame, size);
98 void Chrome::setWindowRect(const FloatRect& rect) const
100 m_client->setWindowRect(rect);
103 FloatRect Chrome::windowRect() const
105 return m_client->windowRect();
108 FloatRect Chrome::pageRect() const
110 return m_client->pageRect();
113 void Chrome::focus() const
118 bool Chrome::canTakeFocus(FocusType type) const
120 return m_client->canTakeFocus(type);
123 void Chrome::takeFocus(FocusType type) const
125 m_client->takeFocus(type);
128 void Chrome::focusedNodeChanged(Node* node) const
130 m_client->focusedNodeChanged(node);
133 void Chrome::show(NavigationPolicy policy) const
135 m_client->show(policy);
138 bool Chrome::canRunModal() const
140 return m_client->canRunModal();
143 static bool canRunModalIfDuringPageDismissal(Page* page, ChromeClient::DialogType dialog, const String& message)
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);
153 bool Chrome::canRunModalNow() const
155 return canRunModal() && canRunModalIfDuringPageDismissal(m_page, ChromeClient::HTMLDialog, String());
158 void Chrome::runModal() const
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);
164 TimerBase::fireTimersInNestedEventLoop();
165 m_client->runModal();
168 void Chrome::setWindowFeatures(const WindowFeatures& features) const
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);
177 bool Chrome::toolbarsVisible() const
179 return m_client->toolbarsVisible();
182 bool Chrome::statusbarVisible() const
184 return m_client->statusbarVisible();
187 bool Chrome::scrollbarsVisible() const
189 return m_client->scrollbarsVisible();
192 bool Chrome::menubarVisible() const
194 return m_client->menubarVisible();
197 bool Chrome::canRunBeforeUnloadConfirmPanel()
199 return m_client->canRunBeforeUnloadConfirmPanel();
202 bool Chrome::runBeforeUnloadConfirmPanel(const String& message, Frame* frame)
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);
208 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willRunJavaScriptDialog(m_page, message);
209 bool ok = m_client->runBeforeUnloadConfirmPanel(message, frame);
210 InspectorInstrumentation::didRunJavaScriptDialog(cookie);
214 void Chrome::closeWindowSoon()
216 m_client->closeWindowSoon();
219 void Chrome::runJavaScriptAlert(Frame* frame, const String& message)
221 if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::AlertDialog, message))
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);
229 notifyPopupOpeningObservers();
231 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willRunJavaScriptDialog(m_page, message);
232 m_client->runJavaScriptAlert(frame, message);
233 InspectorInstrumentation::didRunJavaScriptDialog(cookie);
236 bool Chrome::runJavaScriptConfirm(Frame* frame, const String& message)
238 if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::ConfirmDialog, message))
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);
246 notifyPopupOpeningObservers();
248 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willRunJavaScriptDialog(m_page, message);
249 bool ok = m_client->runJavaScriptConfirm(frame, message);
250 InspectorInstrumentation::didRunJavaScriptDialog(cookie);
254 bool Chrome::runJavaScriptPrompt(Frame* frame, const String& prompt, const String& defaultValue, String& result)
256 if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::PromptDialog, prompt))
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);
264 notifyPopupOpeningObservers();
266 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willRunJavaScriptDialog(m_page, prompt);
267 bool ok = m_client->runJavaScriptPrompt(frame, prompt, defaultValue, result);
268 InspectorInstrumentation::didRunJavaScriptDialog(cookie);
273 void Chrome::setStatusbarText(Frame* frame, const String& status)
276 m_client->setStatusbarText(status);
279 IntRect Chrome::windowResizerRect() const
281 return m_client->windowResizerRect();
284 void Chrome::mouseDidMoveOverElement(const HitTestResult& result, unsigned modifierFlags)
286 if (result.innerNode()) {
287 if (result.innerNode()->document().isDNSPrefetchEnabled())
288 prefetchDNS(result.absoluteLinkURL().host());
290 m_client->mouseDidMoveOverElement(result, modifierFlags);
293 void Chrome::setToolTip(const HitTestResult& result)
295 // First priority is a potential toolTip representing a spelling or grammar error
296 TextDirection toolTipDirection;
297 String toolTip = result.spellingToolTip(toolTipDirection);
299 // Next we'll consider a tooltip for element with "title" attribute
300 if (toolTip.isEmpty())
301 toolTip = result.title(toolTipDirection);
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();
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;
320 m_client->setToolTip(toolTip, toolTipDirection);
323 void Chrome::print(Frame* frame)
325 // FIXME: This should have PageGroupLoadDeferrer, like runModal() or runJavaScriptAlert(), becasue it's no different from those.
326 m_client->print(frame);
329 void Chrome::enumerateChosenDirectory(FileChooser* fileChooser)
331 m_client->enumerateChosenDirectory(fileChooser);
334 PassOwnPtr<ColorChooser> Chrome::createColorChooser(ColorChooserClient* client, const Color& initialColor)
336 notifyPopupOpeningObservers();
337 return m_client->createColorChooser(client, initialColor);
340 PassRefPtr<DateTimeChooser> Chrome::openDateTimeChooser(DateTimeChooserClient* client, const DateTimeChooserParameters& parameters)
342 notifyPopupOpeningObservers();
343 return m_client->openDateTimeChooser(client, parameters);
346 void Chrome::openTextDataListChooser(HTMLInputElement& input)
348 notifyPopupOpeningObservers();
349 m_client->openTextDataListChooser(input);
352 void Chrome::runOpenPanel(Frame* frame, PassRefPtr<FileChooser> fileChooser)
354 notifyPopupOpeningObservers();
355 m_client->runOpenPanel(frame, fileChooser);
358 void Chrome::dispatchViewportPropertiesDidChange(const ViewportDescription& description) const
360 m_client->dispatchViewportPropertiesDidChange(description);
363 void Chrome::setCursor(const Cursor& cursor)
365 m_client->setCursor(cursor);
368 void Chrome::scheduleAnimation()
370 m_client->scheduleAnimation();
375 bool Chrome::hasOpenedPopup() const
377 return m_client->hasOpenedPopup();
380 PassRefPtr<PopupMenu> Chrome::createPopupMenu(Frame& frame, PopupMenuClient* client) const
382 notifyPopupOpeningObservers();
383 return m_client->createPopupMenu(frame, client);
386 void Chrome::registerPopupOpeningObserver(PopupOpeningObserver* observer)
389 m_popupOpeningObservers.append(observer);
392 void Chrome::unregisterPopupOpeningObserver(PopupOpeningObserver* observer)
394 size_t index = m_popupOpeningObservers.find(observer);
395 ASSERT(index != kNotFound);
396 m_popupOpeningObservers.remove(index);
399 void Chrome::notifyPopupOpeningObservers() const
401 const Vector<PopupOpeningObserver*> observers(m_popupOpeningObservers);
402 for (size_t i = 0; i < observers.size(); ++i)
403 observers[i]->willOpenPopup();
406 } // namespace WebCore