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 IntPoint Chrome::screenToRootView(const IntPoint& point) const
85 return m_client->screenToRootView(point);
88 IntRect Chrome::rootViewToScreen(const IntRect& rect) const
90 return m_client->rootViewToScreen(rect);
93 blink::WebScreenInfo Chrome::screenInfo() const
95 return m_client->screenInfo();
98 void Chrome::contentsSizeChanged(Frame* frame, const IntSize& size) const
100 m_client->contentsSizeChanged(frame, size);
103 void Chrome::layoutUpdated(Frame* frame) const
105 m_client->layoutUpdated(frame);
108 void Chrome::setWindowRect(const FloatRect& rect) const
110 m_client->setWindowRect(rect);
113 FloatRect Chrome::windowRect() const
115 return m_client->windowRect();
118 FloatRect Chrome::pageRect() const
120 return m_client->pageRect();
123 void Chrome::focus() const
128 void Chrome::unfocus() const
133 bool Chrome::canTakeFocus(FocusDirection direction) const
135 return m_client->canTakeFocus(direction);
138 void Chrome::takeFocus(FocusDirection direction) const
140 m_client->takeFocus(direction);
143 void Chrome::focusedNodeChanged(Node* node) const
145 m_client->focusedNodeChanged(node);
148 void Chrome::show(NavigationPolicy policy) const
150 m_client->show(policy);
153 bool Chrome::canRunModal() const
155 return m_client->canRunModal();
158 static bool canRunModalIfDuringPageDismissal(Page* page, ChromeClient::DialogType dialog, const String& message)
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);
168 bool Chrome::canRunModalNow() const
170 return canRunModal() && canRunModalIfDuringPageDismissal(m_page, ChromeClient::HTMLDialog, String());
173 void Chrome::runModal() const
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);
179 TimerBase::fireTimersInNestedEventLoop();
180 m_client->runModal();
183 void Chrome::setWindowFeatures(const WindowFeatures& features) const
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);
192 bool Chrome::toolbarsVisible() const
194 return m_client->toolbarsVisible();
197 bool Chrome::statusbarVisible() const
199 return m_client->statusbarVisible();
202 bool Chrome::scrollbarsVisible() const
204 return m_client->scrollbarsVisible();
207 bool Chrome::menubarVisible() const
209 return m_client->menubarVisible();
212 bool Chrome::canRunBeforeUnloadConfirmPanel()
214 return m_client->canRunBeforeUnloadConfirmPanel();
217 bool Chrome::runBeforeUnloadConfirmPanel(const String& message, Frame* frame)
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);
223 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willRunJavaScriptDialog(m_page, message);
224 bool ok = m_client->runBeforeUnloadConfirmPanel(message, frame);
225 InspectorInstrumentation::didRunJavaScriptDialog(cookie);
229 void Chrome::closeWindowSoon()
231 m_client->closeWindowSoon();
234 void Chrome::runJavaScriptAlert(Frame* frame, const String& message)
236 if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::AlertDialog, message))
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);
244 notifyPopupOpeningObservers();
246 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willRunJavaScriptDialog(m_page, message);
247 m_client->runJavaScriptAlert(frame, message);
248 InspectorInstrumentation::didRunJavaScriptDialog(cookie);
251 bool Chrome::runJavaScriptConfirm(Frame* frame, const String& message)
253 if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::ConfirmDialog, message))
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);
261 notifyPopupOpeningObservers();
263 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willRunJavaScriptDialog(m_page, message);
264 bool ok = m_client->runJavaScriptConfirm(frame, message);
265 InspectorInstrumentation::didRunJavaScriptDialog(cookie);
269 bool Chrome::runJavaScriptPrompt(Frame* frame, const String& prompt, const String& defaultValue, String& result)
271 if (!canRunModalIfDuringPageDismissal(m_page, ChromeClient::PromptDialog, prompt))
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);
279 notifyPopupOpeningObservers();
281 InspectorInstrumentationCookie cookie = InspectorInstrumentation::willRunJavaScriptDialog(m_page, prompt);
282 bool ok = m_client->runJavaScriptPrompt(frame, prompt, defaultValue, result);
283 InspectorInstrumentation::didRunJavaScriptDialog(cookie);
288 void Chrome::setStatusbarText(Frame* frame, const String& status)
291 m_client->setStatusbarText(status);
294 IntRect Chrome::windowResizerRect() const
296 return m_client->windowResizerRect();
299 void Chrome::mouseDidMoveOverElement(const HitTestResult& result, unsigned modifierFlags)
301 if (result.innerNode()) {
302 if (result.innerNode()->document().isDNSPrefetchEnabled())
303 prefetchDNS(result.absoluteLinkURL().host());
305 m_client->mouseDidMoveOverElement(result, modifierFlags);
308 void Chrome::setToolTip(const HitTestResult& result)
310 // First priority is a potential toolTip representing a spelling or grammar error
311 TextDirection toolTipDirection;
312 String toolTip = result.spellingToolTip(toolTipDirection);
314 // Next we'll consider a tooltip for element with "title" attribute
315 if (toolTip.isEmpty())
316 toolTip = result.title(toolTipDirection);
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();
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;
335 m_client->setToolTip(toolTip, toolTipDirection);
338 void Chrome::print(Frame* frame)
340 // FIXME: This should have PageGroupLoadDeferrer, like runModal() or runJavaScriptAlert(), becasue it's no different from those.
341 m_client->print(frame);
344 void Chrome::enumerateChosenDirectory(FileChooser* fileChooser)
346 m_client->enumerateChosenDirectory(fileChooser);
349 PassOwnPtr<ColorChooser> Chrome::createColorChooser(ColorChooserClient* client, const Color& initialColor)
351 notifyPopupOpeningObservers();
352 return m_client->createColorChooser(client, initialColor);
355 PassRefPtr<DateTimeChooser> Chrome::openDateTimeChooser(DateTimeChooserClient* client, const DateTimeChooserParameters& parameters)
357 notifyPopupOpeningObservers();
358 return m_client->openDateTimeChooser(client, parameters);
361 void Chrome::openTextDataListChooser(HTMLInputElement& input)
363 notifyPopupOpeningObservers();
364 m_client->openTextDataListChooser(input);
367 void Chrome::runOpenPanel(Frame* frame, PassRefPtr<FileChooser> fileChooser)
369 notifyPopupOpeningObservers();
370 m_client->runOpenPanel(frame, fileChooser);
373 void Chrome::dispatchViewportPropertiesDidChange(const ViewportDescription& description) const
375 m_client->dispatchViewportPropertiesDidChange(description);
378 void Chrome::setCursor(const Cursor& cursor)
380 m_client->setCursor(cursor);
383 void Chrome::scheduleAnimation()
385 m_client->scheduleAnimation();
390 bool Chrome::hasOpenedPopup() const
392 return m_client->hasOpenedPopup();
395 PassRefPtr<PopupMenu> Chrome::createPopupMenu(Frame& frame, PopupMenuClient* client) const
397 notifyPopupOpeningObservers();
398 return m_client->createPopupMenu(frame, client);
401 void Chrome::registerPopupOpeningObserver(PopupOpeningObserver* observer)
404 m_popupOpeningObservers.append(observer);
407 void Chrome::unregisterPopupOpeningObserver(PopupOpeningObserver* observer)
409 size_t index = m_popupOpeningObservers.find(observer);
410 ASSERT(index != kNotFound);
411 m_popupOpeningObservers.remove(index);
414 void Chrome::notifyPopupOpeningObservers() const
416 const Vector<PopupOpeningObserver*> observers(m_popupOpeningObservers);
417 for (size_t i = 0; i < observers.size(); ++i)
418 observers[i]->willOpenPopup();
421 } // namespace WebCore