2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "DOMWindow.h"
30 #include "BeforeUnloadEvent.h"
31 #include "CSSComputedStyleDeclaration.h"
32 #include "CSSRuleList.h"
33 #include "CSSStyleSelector.h"
38 #include "DOMApplicationCache.h"
39 #include "DOMSelection.h"
41 #include "PageTransitionEvent.h"
44 #include "EventException.h"
45 #include "EventListener.h"
46 #include "EventNames.h"
47 #include "ExceptionCode.h"
48 #include "FloatRect.h"
50 #include "FrameLoader.h"
51 #include "FrameTree.h"
52 #include "FrameView.h"
53 #include "HTMLFrameOwnerElement.h"
55 #include "InspectorController.h"
56 #include "InspectorTimelineAgent.h"
59 #include "MessageEvent.h"
60 #include "Navigator.h"
61 #include "NotificationCenter.h"
63 #include "PageGroup.h"
64 #include "PlatformScreen.h"
65 #include "PlatformString.h"
67 #include "SecurityOrigin.h"
68 #include "SerializedScriptValue.h"
71 #include "StorageArea.h"
72 #include "StorageNamespace.h"
73 #include "SuddenTermination.h"
74 #include "WebKitPoint.h"
76 #include <wtf/MathExtras.h>
83 class PostMessageTimer : public TimerBase {
85 PostMessageTimer(DOMWindow* window, PassRefPtr<SerializedScriptValue> message, const String& sourceOrigin, PassRefPtr<DOMWindow> source, PassOwnPtr<MessagePortChannelArray> channels, SecurityOrigin* targetOrigin)
88 , m_origin(sourceOrigin)
90 , m_channels(channels)
91 , m_targetOrigin(targetOrigin)
95 PassRefPtr<MessageEvent> event(ScriptExecutionContext* context)
97 OwnPtr<MessagePortArray> messagePorts = MessagePort::entanglePorts(*context, m_channels.release());
98 return MessageEvent::create(messagePorts.release(), m_message, m_origin, "", m_source);
100 SecurityOrigin* targetOrigin() const { return m_targetOrigin.get(); }
105 m_window->postMessageTimerFired(this);
108 RefPtr<DOMWindow> m_window;
109 RefPtr<SerializedScriptValue> m_message;
111 RefPtr<DOMWindow> m_source;
112 OwnPtr<MessagePortChannelArray> m_channels;
113 RefPtr<SecurityOrigin> m_targetOrigin;
116 typedef HashCountedSet<DOMWindow*> DOMWindowSet;
118 static DOMWindowSet& windowsWithUnloadEventListeners()
120 DEFINE_STATIC_LOCAL(DOMWindowSet, windowsWithUnloadEventListeners, ());
121 return windowsWithUnloadEventListeners;
124 static DOMWindowSet& windowsWithBeforeUnloadEventListeners()
126 DEFINE_STATIC_LOCAL(DOMWindowSet, windowsWithBeforeUnloadEventListeners, ());
127 return windowsWithBeforeUnloadEventListeners;
130 static void addUnloadEventListener(DOMWindow* domWindow)
132 DOMWindowSet& set = windowsWithUnloadEventListeners();
134 disableSuddenTermination();
138 static void removeUnloadEventListener(DOMWindow* domWindow)
140 DOMWindowSet& set = windowsWithUnloadEventListeners();
141 DOMWindowSet::iterator it = set.find(domWindow);
146 enableSuddenTermination();
149 static void removeAllUnloadEventListeners(DOMWindow* domWindow)
151 DOMWindowSet& set = windowsWithUnloadEventListeners();
152 DOMWindowSet::iterator it = set.find(domWindow);
157 enableSuddenTermination();
160 static void addBeforeUnloadEventListener(DOMWindow* domWindow)
162 DOMWindowSet& set = windowsWithBeforeUnloadEventListeners();
164 disableSuddenTermination();
168 static void removeBeforeUnloadEventListener(DOMWindow* domWindow)
170 DOMWindowSet& set = windowsWithBeforeUnloadEventListeners();
171 DOMWindowSet::iterator it = set.find(domWindow);
176 enableSuddenTermination();
179 static void removeAllBeforeUnloadEventListeners(DOMWindow* domWindow)
181 DOMWindowSet& set = windowsWithBeforeUnloadEventListeners();
182 DOMWindowSet::iterator it = set.find(domWindow);
187 enableSuddenTermination();
190 static bool allowsBeforeUnloadListeners(DOMWindow* window)
192 ASSERT_ARG(window, window);
193 Frame* frame = window->frame();
196 Page* page = frame->page();
199 return frame == page->mainFrame();
202 bool DOMWindow::dispatchAllPendingBeforeUnloadEvents()
204 DOMWindowSet& set = windowsWithBeforeUnloadEventListeners();
208 static bool alreadyDispatched = false;
209 ASSERT(!alreadyDispatched);
210 if (alreadyDispatched)
213 Vector<RefPtr<DOMWindow> > windows;
214 DOMWindowSet::iterator end = set.end();
215 for (DOMWindowSet::iterator it = set.begin(); it != end; ++it)
216 windows.append(it->first);
218 size_t size = windows.size();
219 for (size_t i = 0; i < size; ++i) {
220 DOMWindow* window = windows[i].get();
221 if (!set.contains(window))
224 Frame* frame = window->frame();
228 if (!frame->shouldClose())
232 enableSuddenTermination();
234 alreadyDispatched = true;
239 unsigned DOMWindow::pendingUnloadEventListeners() const
241 return windowsWithUnloadEventListeners().count(const_cast<DOMWindow*>(this));
244 void DOMWindow::dispatchAllPendingUnloadEvents()
246 DOMWindowSet& set = windowsWithUnloadEventListeners();
250 static bool alreadyDispatched = false;
251 ASSERT(!alreadyDispatched);
252 if (alreadyDispatched)
255 Vector<RefPtr<DOMWindow> > windows;
256 DOMWindowSet::iterator end = set.end();
257 for (DOMWindowSet::iterator it = set.begin(); it != end; ++it)
258 windows.append(it->first);
260 size_t size = windows.size();
261 for (size_t i = 0; i < size; ++i) {
262 DOMWindow* window = windows[i].get();
263 if (!set.contains(window))
266 window->dispatchEvent(PageTransitionEvent::create(eventNames().pagehideEvent, false), window->document());
267 window->dispatchEvent(Event::create(eventNames().unloadEvent, false, false), window->document());
270 enableSuddenTermination();
272 alreadyDispatched = true;
276 // 1) Validates the pending changes are not changing to NaN
277 // 2) Constrains the window rect to no smaller than 100 in each dimension and no
278 // bigger than the the float rect's dimensions.
279 // 3) Constrain window rect to within the top and left boundaries of the screen rect
280 // 4) Constraint the window rect to within the bottom and right boundaries of the
282 // 5) Translate the window rect coordinates to be within the coordinate space of
284 void DOMWindow::adjustWindowRect(const FloatRect& screen, FloatRect& window, const FloatRect& pendingChanges)
286 // Make sure we're in a valid state before adjusting dimensions.
287 ASSERT(isfinite(screen.x()));
288 ASSERT(isfinite(screen.y()));
289 ASSERT(isfinite(screen.width()));
290 ASSERT(isfinite(screen.height()));
291 ASSERT(isfinite(window.x()));
292 ASSERT(isfinite(window.y()));
293 ASSERT(isfinite(window.width()));
294 ASSERT(isfinite(window.height()));
296 // Update window values if new requested values are not NaN.
297 if (!isnan(pendingChanges.x()))
298 window.setX(pendingChanges.x());
299 if (!isnan(pendingChanges.y()))
300 window.setY(pendingChanges.y());
301 if (!isnan(pendingChanges.width()))
302 window.setWidth(pendingChanges.width());
303 if (!isnan(pendingChanges.height()))
304 window.setHeight(pendingChanges.height());
306 // Resize the window to between 100 and the screen width and height.
307 window.setWidth(min(max(100.0f, window.width()), screen.width()));
308 window.setHeight(min(max(100.0f, window.height()), screen.height()));
310 // Constrain the window position to the screen.
311 window.setX(max(screen.x(), min(window.x(), screen.right() - window.width())));
312 window.setY(max(screen.y(), min(window.y(), screen.bottom() - window.height())));
315 void DOMWindow::parseModalDialogFeatures(const String& featuresArg, HashMap<String, String>& map)
317 Vector<String> features;
318 featuresArg.split(';', features);
319 Vector<String>::const_iterator end = features.end();
320 for (Vector<String>::const_iterator it = features.begin(); it != end; ++it) {
322 int pos = s.find('=');
323 int colonPos = s.find(':');
324 if (pos >= 0 && colonPos >= 0)
325 continue; // ignore any strings that have both = and :
329 // null string for value means key without value
330 map.set(s.stripWhiteSpace().lower(), String());
332 String key = s.left(pos).stripWhiteSpace().lower();
333 String val = s.substring(pos + 1).stripWhiteSpace().lower();
334 int spacePos = val.find(' ');
336 val = val.left(spacePos);
342 bool DOMWindow::allowPopUp(Frame* activeFrame)
345 if (activeFrame->script()->processingUserGesture())
347 Settings* settings = activeFrame->settings();
348 return settings && settings->javaScriptCanOpenWindowsAutomatically();
351 bool DOMWindow::canShowModalDialog(const Frame* frame)
355 Page* page = frame->page();
358 return page->chrome()->canRunModal();
361 bool DOMWindow::canShowModalDialogNow(const Frame* frame)
365 Page* page = frame->page();
368 return page->chrome()->canRunModalNow();
371 DOMWindow::DOMWindow(Frame* frame)
376 DOMWindow::~DOMWindow()
379 m_frame->clearFormerDOMWindow(this);
381 removeAllUnloadEventListeners(this);
382 removeAllBeforeUnloadEventListeners(this);
385 ScriptExecutionContext* DOMWindow::scriptExecutionContext() const
390 void DOMWindow::disconnectFrame()
396 void DOMWindow::clear()
399 m_screen->disconnectFrame();
403 m_selection->disconnectFrame();
407 m_history->disconnectFrame();
411 m_locationbar->disconnectFrame();
415 m_menubar->disconnectFrame();
419 m_personalbar->disconnectFrame();
423 m_scrollbars->disconnectFrame();
427 m_statusbar->disconnectFrame();
431 m_toolbar->disconnectFrame();
435 m_console->disconnectFrame();
439 m_navigator->disconnectFrame();
443 m_location->disconnectFrame();
446 #if ENABLE(DOM_STORAGE)
447 if (m_sessionStorage)
448 m_sessionStorage->disconnectFrame();
449 m_sessionStorage = 0;
452 m_localStorage->disconnectFrame();
456 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
457 if (m_applicationCache)
458 m_applicationCache->disconnectFrame();
459 m_applicationCache = 0;
462 #if ENABLE(NOTIFICATIONS)
467 #if ENABLE(ORIENTATION_EVENTS)
468 int DOMWindow::orientation() const
473 return m_frame->orientation();
477 Screen* DOMWindow::screen() const
480 m_screen = Screen::create(m_frame);
481 return m_screen.get();
484 History* DOMWindow::history() const
487 m_history = History::create(m_frame);
488 return m_history.get();
491 BarInfo* DOMWindow::locationbar() const
494 m_locationbar = BarInfo::create(m_frame, BarInfo::Locationbar);
495 return m_locationbar.get();
498 BarInfo* DOMWindow::menubar() const
501 m_menubar = BarInfo::create(m_frame, BarInfo::Menubar);
502 return m_menubar.get();
505 BarInfo* DOMWindow::personalbar() const
508 m_personalbar = BarInfo::create(m_frame, BarInfo::Personalbar);
509 return m_personalbar.get();
512 BarInfo* DOMWindow::scrollbars() const
515 m_scrollbars = BarInfo::create(m_frame, BarInfo::Scrollbars);
516 return m_scrollbars.get();
519 BarInfo* DOMWindow::statusbar() const
522 m_statusbar = BarInfo::create(m_frame, BarInfo::Statusbar);
523 return m_statusbar.get();
526 BarInfo* DOMWindow::toolbar() const
529 m_toolbar = BarInfo::create(m_frame, BarInfo::Toolbar);
530 return m_toolbar.get();
533 Console* DOMWindow::console() const
536 m_console = Console::create(m_frame);
537 return m_console.get();
540 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
541 DOMApplicationCache* DOMWindow::applicationCache() const
543 if (!m_applicationCache)
544 m_applicationCache = DOMApplicationCache::create(m_frame);
545 return m_applicationCache.get();
549 Navigator* DOMWindow::navigator() const
552 m_navigator = Navigator::create(m_frame);
553 return m_navigator.get();
556 Location* DOMWindow::location() const
559 m_location = Location::create(m_frame);
560 return m_location.get();
563 #if ENABLE(DOM_STORAGE)
564 Storage* DOMWindow::sessionStorage() const
566 if (m_sessionStorage)
567 return m_sessionStorage.get();
569 Document* document = this->document();
573 Page* page = document->page();
577 RefPtr<StorageArea> storageArea = page->sessionStorage()->storageArea(document->securityOrigin());
578 #if ENABLE(INSPECTOR)
579 page->inspectorController()->didUseDOMStorage(storageArea.get(), false, m_frame);
582 m_sessionStorage = Storage::create(m_frame, storageArea.release());
583 return m_sessionStorage.get();
586 Storage* DOMWindow::localStorage() const
589 return m_localStorage.get();
591 Document* document = this->document();
595 Page* page = document->page();
599 if (!page->settings()->localStorageEnabled())
602 RefPtr<StorageArea> storageArea = page->group().localStorage()->storageArea(document->securityOrigin());
603 #if ENABLE(INSPECTOR)
604 page->inspectorController()->didUseDOMStorage(storageArea.get(), true, m_frame);
607 m_localStorage = Storage::create(m_frame, storageArea.release());
608 return m_localStorage.get();
612 #if ENABLE(NOTIFICATIONS)
613 NotificationCenter* DOMWindow::webkitNotifications() const
616 return m_notifications.get();
618 Document* document = this->document();
622 Page* page = document->page();
626 NotificationPresenter* provider = page->chrome()->notificationPresenter();
628 m_notifications = NotificationCenter::create(document, provider);
630 return m_notifications.get();
634 void DOMWindow::postMessage(PassRefPtr<SerializedScriptValue> message, MessagePort* port, const String& targetOrigin, DOMWindow* source, ExceptionCode& ec)
636 MessagePortArray ports;
639 postMessage(message, &ports, targetOrigin, source, ec);
642 void DOMWindow::postMessage(PassRefPtr<SerializedScriptValue> message, const MessagePortArray* ports, const String& targetOrigin, DOMWindow* source, ExceptionCode& ec)
647 // Compute the target origin. We need to do this synchronously in order
648 // to generate the SYNTAX_ERR exception correctly.
649 RefPtr<SecurityOrigin> target;
650 if (targetOrigin != "*") {
651 target = SecurityOrigin::createFromString(targetOrigin);
652 if (target->isEmpty()) {
658 OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(ports, ec);
662 // Capture the source of the message. We need to do this synchronously
663 // in order to capture the source of the message correctly.
664 Document* sourceDocument = source->document();
667 String sourceOrigin = sourceDocument->securityOrigin()->toString();
669 // Schedule the message.
670 PostMessageTimer* timer = new PostMessageTimer(this, message, sourceOrigin, source, channels.release(), target.get());
671 timer->startOneShot(0);
674 void DOMWindow::postMessageTimerFired(PostMessageTimer* t)
676 OwnPtr<PostMessageTimer> timer(t);
681 if (timer->targetOrigin()) {
682 // Check target origin now since the target document may have changed since the simer was scheduled.
683 if (!timer->targetOrigin()->isSameSchemeHostPort(document()->securityOrigin())) {
684 String message = String::format("Unable to post message to %s. Recipient has origin %s.\n",
685 timer->targetOrigin()->toString().utf8().data(), document()->securityOrigin()->toString().utf8().data());
686 console()->addMessage(JSMessageSource, LogMessageType, ErrorMessageLevel, message, 0, String());
691 dispatchEvent(timer->event(document()));
694 DOMSelection* DOMWindow::getSelection()
697 m_selection = DOMSelection::create(m_frame);
698 return m_selection.get();
701 Element* DOMWindow::frameElement() const
706 return m_frame->ownerElement();
709 void DOMWindow::focus()
714 m_frame->focusWindow();
717 void DOMWindow::blur()
722 m_frame->unfocusWindow();
725 void DOMWindow::close()
730 Page* page = m_frame->page();
734 if (m_frame != page->mainFrame())
737 Settings* settings = m_frame->settings();
738 bool allowScriptsToCloseWindows = settings && settings->allowScriptsToCloseWindows();
740 if (page->openedByDOM() || page->getHistoryLength() <= 1 || allowScriptsToCloseWindows)
741 m_frame->scheduleClose();
744 void DOMWindow::print()
749 Page* page = m_frame->page();
753 page->chrome()->print(m_frame);
756 void DOMWindow::stop()
761 // We must check whether the load is complete asynchronously, because we might still be parsing
762 // the document until the callstack unwinds.
763 m_frame->loader()->stopForUserCancel(true);
766 void DOMWindow::alert(const String& message)
771 m_frame->document()->updateStyleIfNeeded();
773 Page* page = m_frame->page();
777 page->chrome()->runJavaScriptAlert(m_frame, message);
780 bool DOMWindow::confirm(const String& message)
785 m_frame->document()->updateStyleIfNeeded();
787 Page* page = m_frame->page();
791 return page->chrome()->runJavaScriptConfirm(m_frame, message);
794 String DOMWindow::prompt(const String& message, const String& defaultValue)
799 m_frame->document()->updateStyleIfNeeded();
801 Page* page = m_frame->page();
806 if (page->chrome()->runJavaScriptPrompt(m_frame, message, defaultValue, returnValue))
812 bool DOMWindow::find(const String& string, bool caseSensitive, bool backwards, bool wrap, bool /*wholeWord*/, bool /*searchInFrames*/, bool /*showDialog*/) const
817 // FIXME (13016): Support wholeWord, searchInFrames and showDialog
818 return m_frame->findString(string, !backwards, caseSensitive, wrap, false);
821 bool DOMWindow::offscreenBuffering() const
826 int DOMWindow::outerHeight() const
831 Page* page = m_frame->page();
835 return static_cast<int>(page->chrome()->windowRect().height());
838 int DOMWindow::outerWidth() const
843 Page* page = m_frame->page();
847 return static_cast<int>(page->chrome()->windowRect().width());
850 int DOMWindow::innerHeight() const
855 FrameView* view = m_frame->view();
859 return static_cast<int>(view->height() / m_frame->pageZoomFactor());
862 int DOMWindow::innerWidth() const
867 FrameView* view = m_frame->view();
871 return static_cast<int>(view->width() / m_frame->pageZoomFactor());
874 int DOMWindow::screenX() const
879 Page* page = m_frame->page();
883 return static_cast<int>(page->chrome()->windowRect().x());
886 int DOMWindow::screenY() const
891 Page* page = m_frame->page();
895 return static_cast<int>(page->chrome()->windowRect().y());
898 int DOMWindow::scrollX() const
903 FrameView* view = m_frame->view();
907 m_frame->document()->updateLayoutIgnorePendingStylesheets();
909 return static_cast<int>(view->scrollX() / m_frame->pageZoomFactor());
912 int DOMWindow::scrollY() const
917 FrameView* view = m_frame->view();
921 m_frame->document()->updateLayoutIgnorePendingStylesheets();
923 return static_cast<int>(view->scrollY() / m_frame->pageZoomFactor());
926 bool DOMWindow::closed() const
931 unsigned DOMWindow::length() const
936 return m_frame->tree()->childCount();
939 String DOMWindow::name() const
944 return m_frame->tree()->name();
947 void DOMWindow::setName(const String& string)
952 m_frame->tree()->setName(string);
955 String DOMWindow::status() const
960 return m_frame->jsStatusBarText();
963 void DOMWindow::setStatus(const String& string)
968 m_frame->setJSStatusBarText(string);
971 String DOMWindow::defaultStatus() const
976 return m_frame->jsDefaultStatusBarText();
979 void DOMWindow::setDefaultStatus(const String& string)
984 m_frame->setJSDefaultStatusBarText(string);
987 DOMWindow* DOMWindow::self() const
992 return m_frame->domWindow();
995 DOMWindow* DOMWindow::opener() const
1000 Frame* opener = m_frame->loader()->opener();
1004 return opener->domWindow();
1007 DOMWindow* DOMWindow::parent() const
1012 Frame* parent = m_frame->tree()->parent(true);
1014 return parent->domWindow();
1016 return m_frame->domWindow();
1019 DOMWindow* DOMWindow::top() const
1024 Page* page = m_frame->page();
1028 return m_frame->tree()->top(true)->domWindow();
1031 Document* DOMWindow::document() const
1033 // FIXME: This function shouldn't need a frame to work.
1037 // The m_frame pointer is not zeroed out when the window is put into b/f cache, so it can hold an unrelated document/window pair.
1038 // FIXME: We should always zero out the frame pointer on navigation to avoid accidentally accessing the new frame content.
1039 if (m_frame->domWindow() != this)
1042 ASSERT(m_frame->document());
1043 return m_frame->document();
1046 PassRefPtr<Media> DOMWindow::media() const
1048 return Media::create(const_cast<DOMWindow*>(this));
1051 PassRefPtr<CSSStyleDeclaration> DOMWindow::getComputedStyle(Element* elt, const String&) const
1056 // FIXME: This needs take pseudo elements into account.
1057 return computedStyle(elt);
1060 PassRefPtr<CSSRuleList> DOMWindow::getMatchedCSSRules(Element* elt, const String& pseudoElt, bool authorOnly) const
1065 Document* doc = m_frame->document();
1067 if (!pseudoElt.isEmpty())
1068 return doc->styleSelector()->pseudoStyleRulesForElement(elt, pseudoElt, authorOnly);
1069 return doc->styleSelector()->styleRulesForElement(elt, authorOnly);
1072 PassRefPtr<WebKitPoint> DOMWindow::webkitConvertPointFromNodeToPage(Node* node, const WebKitPoint* p) const
1077 FloatPoint pagePoint(p->x(), p->y());
1078 pagePoint = node->convertToPage(pagePoint);
1079 return WebKitPoint::create(pagePoint.x(), pagePoint.y());
1082 PassRefPtr<WebKitPoint> DOMWindow::webkitConvertPointFromPageToNode(Node* node, const WebKitPoint* p) const
1087 FloatPoint nodePoint(p->x(), p->y());
1088 nodePoint = node->convertFromPage(nodePoint);
1089 return WebKitPoint::create(nodePoint.x(), nodePoint.y());
1092 double DOMWindow::devicePixelRatio() const
1097 Page* page = m_frame->page();
1101 return page->chrome()->scaleFactor();
1104 #if ENABLE(DATABASE)
1105 PassRefPtr<Database> DOMWindow::openDatabase(const String& name, const String& version, const String& displayName, unsigned long estimatedSize, ExceptionCode& ec)
1110 Document* doc = m_frame->document();
1112 Settings* settings = m_frame->settings();
1113 if (!settings || !settings->databasesEnabled())
1116 return Database::openDatabase(doc, name, version, displayName, estimatedSize, ec);
1120 void DOMWindow::scrollBy(int x, int y) const
1125 m_frame->document()->updateLayoutIgnorePendingStylesheets();
1127 FrameView* view = m_frame->view();
1131 view->scrollBy(IntSize(x, y));
1134 void DOMWindow::scrollTo(int x, int y) const
1139 m_frame->document()->updateLayoutIgnorePendingStylesheets();
1141 FrameView* view = m_frame->view();
1145 int zoomedX = static_cast<int>(x * m_frame->pageZoomFactor());
1146 int zoomedY = static_cast<int>(y * m_frame->pageZoomFactor());
1147 view->setScrollPosition(IntPoint(zoomedX, zoomedY));
1150 void DOMWindow::moveBy(float x, float y) const
1155 Page* page = m_frame->page();
1159 if (m_frame != page->mainFrame())
1162 FloatRect fr = page->chrome()->windowRect();
1163 FloatRect update = fr;
1165 // Security check (the spec talks about UniversalBrowserWrite to disable this check...)
1166 adjustWindowRect(screenAvailableRect(page->mainFrame()->view()), fr, update);
1167 page->chrome()->setWindowRect(fr);
1170 void DOMWindow::moveTo(float x, float y) const
1175 Page* page = m_frame->page();
1179 if (m_frame != page->mainFrame())
1182 FloatRect fr = page->chrome()->windowRect();
1183 FloatRect sr = screenAvailableRect(page->mainFrame()->view());
1184 fr.setLocation(sr.location());
1185 FloatRect update = fr;
1187 // Security check (the spec talks about UniversalBrowserWrite to disable this check...)
1188 adjustWindowRect(sr, fr, update);
1189 page->chrome()->setWindowRect(fr);
1192 void DOMWindow::resizeBy(float x, float y) const
1197 Page* page = m_frame->page();
1201 if (m_frame != page->mainFrame())
1204 FloatRect fr = page->chrome()->windowRect();
1205 FloatSize dest = fr.size() + FloatSize(x, y);
1206 FloatRect update(fr.location(), dest);
1207 adjustWindowRect(screenAvailableRect(page->mainFrame()->view()), fr, update);
1208 page->chrome()->setWindowRect(fr);
1211 void DOMWindow::resizeTo(float width, float height) const
1216 Page* page = m_frame->page();
1220 if (m_frame != page->mainFrame())
1223 FloatRect fr = page->chrome()->windowRect();
1224 FloatSize dest = FloatSize(width, height);
1225 FloatRect update(fr.location(), dest);
1226 adjustWindowRect(screenAvailableRect(page->mainFrame()->view()), fr, update);
1227 page->chrome()->setWindowRect(fr);
1230 int DOMWindow::setTimeout(ScheduledAction* action, int timeout)
1232 return DOMTimer::install(scriptExecutionContext(), action, timeout, true);
1235 void DOMWindow::clearTimeout(int timeoutId)
1237 DOMTimer::removeById(scriptExecutionContext(), timeoutId);
1240 int DOMWindow::setInterval(ScheduledAction* action, int timeout)
1242 return DOMTimer::install(scriptExecutionContext(), action, timeout, false);
1245 void DOMWindow::clearInterval(int timeoutId)
1247 DOMTimer::removeById(scriptExecutionContext(), timeoutId);
1250 bool DOMWindow::addEventListener(const AtomicString& eventType, PassRefPtr<EventListener> listener, bool useCapture)
1252 if (!EventTarget::addEventListener(eventType, listener, useCapture))
1255 if (Document* document = this->document())
1256 document->addListenerTypeIfNeeded(eventType);
1258 if (eventType == eventNames().unloadEvent)
1259 addUnloadEventListener(this);
1260 else if (eventType == eventNames().beforeunloadEvent && allowsBeforeUnloadListeners(this))
1261 addBeforeUnloadEventListener(this);
1266 bool DOMWindow::removeEventListener(const AtomicString& eventType, EventListener* listener, bool useCapture)
1268 if (!EventTarget::removeEventListener(eventType, listener, useCapture))
1271 if (eventType == eventNames().unloadEvent)
1272 removeUnloadEventListener(this);
1273 else if (eventType == eventNames().beforeunloadEvent && allowsBeforeUnloadListeners(this))
1274 removeBeforeUnloadEventListener(this);
1279 void DOMWindow::dispatchLoadEvent()
1281 dispatchEvent(Event::create(eventNames().loadEvent, false, false), document());
1283 // For load events, send a separate load event to the enclosing frame only.
1284 // This is a DOM extension and is independent of bubbling/capturing rules of
1286 Element* ownerElement = document()->ownerElement();
1288 RefPtr<Event> ownerEvent = Event::create(eventNames().loadEvent, false, false);
1289 ownerEvent->setTarget(ownerElement);
1290 ownerElement->dispatchGenericEvent(ownerEvent.release());
1293 #if ENABLE(INSPECTOR)
1294 if (!frame() || !frame()->page())
1297 if (InspectorController* controller = frame()->page()->inspectorController())
1298 controller->mainResourceFiredLoadEvent(frame()->loader()->documentLoader(), url());
1302 InspectorTimelineAgent* DOMWindow::inspectorTimelineAgent()
1304 if (frame() && frame()->page())
1305 return frame()->page()->inspectorTimelineAgent();
1309 bool DOMWindow::dispatchEvent(PassRefPtr<Event> prpEvent, PassRefPtr<EventTarget> prpTarget)
1311 RefPtr<EventTarget> protect = this;
1312 RefPtr<Event> event = prpEvent;
1314 event->setTarget(prpTarget ? prpTarget : this);
1315 event->setCurrentTarget(this);
1316 event->setEventPhase(Event::AT_TARGET);
1318 #if ENABLE(INSPECTOR)
1319 InspectorTimelineAgent* timelineAgent = inspectorTimelineAgent();
1320 bool timelineAgentIsActive = timelineAgent && hasEventListeners(event->type());
1321 if (timelineAgentIsActive)
1322 timelineAgent->willDispatchEvent(*event);
1325 bool result = fireEventListeners(event.get());
1327 #if ENABLE(INSPECTOR)
1328 if (timelineAgentIsActive) {
1329 timelineAgent = inspectorTimelineAgent();
1331 timelineAgent->didDispatchEvent();
1338 void DOMWindow::removeAllEventListeners()
1340 EventTarget::removeAllEventListeners();
1342 removeAllUnloadEventListeners(this);
1343 removeAllBeforeUnloadEventListeners(this);
1346 void DOMWindow::captureEvents()
1351 void DOMWindow::releaseEvents()
1356 EventTargetData* DOMWindow::eventTargetData()
1358 return &m_eventTargetData;
1361 EventTargetData* DOMWindow::ensureEventTargetData()
1363 return &m_eventTargetData;
1366 } // namespace WebCore