1 /* 2 * Copyright (C) 2006, 2007, 2009, 2010 Apple Inc. All rights reserved. 3 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #ifndef DOMWindow_h 28 #define DOMWindow_h 29 30 #include "bindings/v8/ScriptWrappable.h" 31 #include "core/dom/EventTarget.h" 32 #include "core/page/FrameDestructionObserver.h" 33 #include "core/platform/Supplementable.h" 34 35 #include "wtf/Forward.h" 36 37 namespace WebCore { 38 class BarProp; 39 class CSSRuleList; 40 class CSSStyleDeclaration; 41 class Console; 42 class DOMApplicationCache; 43 class DOMPoint; 44 class DOMSelection; 45 class DOMURL; 46 class DOMWindowProperty; 47 class Database; 48 class DatabaseCallback; 49 class Document; 50 class Element; 51 class EventListener; 52 class ExceptionState; 53 class FloatRect; 54 class Frame; 55 class History; 56 class IDBFactory; 57 class Location; 58 class MediaQueryList; 59 class MessageEvent; 60 class Navigator; 61 class Node; 62 class Page; 63 class PageConsole; 64 class Performance; 65 class PostMessageTimer; 66 class RequestAnimationFrameCallback; 67 class ScheduledAction; 68 class Screen; 69 class ScriptCallStack; 70 class SecurityOrigin; 71 class SerializedScriptValue; 72 class Storage; 73 class StyleMedia; 74 class DOMWindowCSS; 75 76 struct WindowFeatures; 77 78 typedef Vector<RefPtr<MessagePort>, 1> MessagePortArray; 79 80 enum SetLocationLocking { LockHistoryBasedOnGestureState, LockHistoryAndBackForwardList }; 81 82 class DOMWindow : public RefCounted<DOMWindow>, public ScriptWrappable, public EventTarget, public FrameDestructionObserver, public Supplementable<DOMWindow> { 83 public: 84 static PassRefPtr<DOMWindow> create(Frame* frame) { return adoptRef(new DOMWindow(frame)); } 85 virtual ~DOMWindow(); 86 87 // In some rare cases, we'll re-used a DOMWindow for a new Document. For example, 88 // when a script calls window.open("..."), the browser gives JavaScript a window 89 // synchronously but kicks off the load in the window asynchronously. Web sites 90 // expect that modifications that they make to the window object synchronously 91 // won't be blown away when the network load commits. To make that happen, we 92 // "securely transition" the existing DOMWindow to the Document that results from 93 // the network load. See also SecurityContext::isSecureTransitionTo. 94 void setDocument(PassRefPtr<Document>); 95 96 virtual const AtomicString& interfaceName() const; 97 virtual ScriptExecutionContext* scriptExecutionContext() const; 98 99 virtual DOMWindow* toDOMWindow(); 100 101 void registerProperty(DOMWindowProperty*); 102 void unregisterProperty(DOMWindowProperty*); 103 104 void reset(); 105 106 PassRefPtr<MediaQueryList> matchMedia(const String&); 107 108 unsigned pendingUnloadEventListeners() const; 109 110 static FloatRect adjustWindowRect(Page*, const FloatRect& pendingChanges); 111 112 bool allowPopUp(); // Call on first window, not target window. 113 static bool allowPopUp(Frame* firstFrame); 114 static bool canShowModalDialog(const Frame*); 115 static bool canShowModalDialogNow(const Frame*); 116 117 // DOM Level 0 118 119 Screen* screen() const; 120 History* history() const; 121 BarProp* locationbar() const; 122 BarProp* menubar() const; 123 BarProp* personalbar() const; 124 BarProp* scrollbars() const; 125 BarProp* statusbar() const; 126 BarProp* toolbar() const; 127 Navigator* navigator() const; 128 Navigator* clientInformation() const { return navigator(); } 129 130 Location* location() const; 131 void setLocation(const String& location, DOMWindow* activeWindow, DOMWindow* firstWindow, 132 SetLocationLocking = LockHistoryBasedOnGestureState); 133 134 DOMSelection* getSelection(); 135 136 Element* frameElement() const; 137 138 void focus(ScriptExecutionContext* = 0); 139 void blur(); 140 void close(ScriptExecutionContext* = 0); 141 void print(); 142 void stop(); 143 144 PassRefPtr<DOMWindow> open(const String& urlString, const AtomicString& frameName, const String& windowFeaturesString, 145 DOMWindow* activeWindow, DOMWindow* firstWindow); 146 147 typedef void (*PrepareDialogFunction)(DOMWindow*, void* context); 148 void showModalDialog(const String& urlString, const String& dialogFeaturesString, 149 DOMWindow* activeWindow, DOMWindow* firstWindow, PrepareDialogFunction, void* functionContext); 150 151 void alert(const String& message); 152 bool confirm(const String& message); 153 String prompt(const String& message, const String& defaultValue); 154 155 bool find(const String&, bool caseSensitive, bool backwards, bool wrap, bool wholeWord, bool searchInFrames, bool showDialog) const; 156 157 bool offscreenBuffering() const; 158 159 int outerHeight() const; 160 int outerWidth() const; 161 int innerHeight() const; 162 int innerWidth() const; 163 int screenX() const; 164 int screenY() const; 165 int screenLeft() const { return screenX(); } 166 int screenTop() const { return screenY(); } 167 int scrollX() const; 168 int scrollY() const; 169 int pageXOffset() const { return scrollX(); } 170 int pageYOffset() const { return scrollY(); } 171 172 bool closed() const; 173 174 unsigned length() const; 175 176 String name() const; 177 void setName(const String&); 178 179 String status() const; 180 void setStatus(const String&); 181 String defaultStatus() const; 182 void setDefaultStatus(const String&); 183 184 // This attribute is an alias of defaultStatus and is necessary for legacy uses. 185 String defaultstatus() const { return defaultStatus(); } 186 void setDefaultstatus(const String& status) { setDefaultStatus(status); } 187 188 // Self-referential attributes 189 190 DOMWindow* self() const; 191 DOMWindow* window() const { return self(); } 192 DOMWindow* frames() const { return self(); } 193 194 DOMWindow* opener() const; 195 DOMWindow* parent() const; 196 DOMWindow* top() const; 197 198 // DOM Level 2 AbstractView Interface 199 200 Document* document() const; 201 202 // CSSOM View Module 203 204 PassRefPtr<StyleMedia> styleMedia() const; 205 206 // DOM Level 2 Style Interface 207 208 PassRefPtr<CSSStyleDeclaration> getComputedStyle(Element*, const String& pseudoElt) const; 209 210 // WebKit extensions 211 212 PassRefPtr<CSSRuleList> getMatchedCSSRules(Element*, const String& pseudoElt, bool authorOnly = true) const; 213 double devicePixelRatio() const; 214 215 PassRefPtr<DOMPoint> webkitConvertPointFromPageToNode(Node*, const DOMPoint*) const; 216 PassRefPtr<DOMPoint> webkitConvertPointFromNodeToPage(Node*, const DOMPoint*) const; 217 218 Console* console() const; 219 PageConsole* pageConsole() const; 220 221 void printErrorMessage(const String&); 222 String crossDomainAccessErrorMessage(DOMWindow* activeWindow); 223 224 void postMessage(PassRefPtr<SerializedScriptValue> message, const MessagePortArray*, const String& targetOrigin, DOMWindow* source, ExceptionState&); 225 void postMessageTimerFired(PassOwnPtr<PostMessageTimer>); 226 void dispatchMessageEventWithOriginCheck(SecurityOrigin* intendedTargetOrigin, PassRefPtr<Event>, PassRefPtr<ScriptCallStack>); 227 228 void scrollBy(int x, int y) const; 229 void scrollTo(int x, int y) const; 230 void scroll(int x, int y) const { scrollTo(x, y); } 231 232 void moveBy(float x, float y) const; 233 void moveTo(float x, float y) const; 234 235 void resizeBy(float x, float y) const; 236 void resizeTo(float width, float height) const; 237 238 // WebKit animation extensions 239 int requestAnimationFrame(PassRefPtr<RequestAnimationFrameCallback>); 240 int webkitRequestAnimationFrame(PassRefPtr<RequestAnimationFrameCallback>); 241 void cancelAnimationFrame(int id); 242 243 DOMWindowCSS* css(); 244 245 // Events 246 // EventTarget API 247 virtual bool addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture); 248 virtual bool removeEventListener(const AtomicString& eventType, EventListener*, bool useCapture); 249 virtual void removeAllEventListeners(); 250 251 using EventTarget::dispatchEvent; 252 bool dispatchEvent(PassRefPtr<Event> prpEvent, PassRefPtr<EventTarget> prpTarget); 253 254 void dispatchLoadEvent(); 255 256 DEFINE_ATTRIBUTE_EVENT_LISTENER(abort); 257 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforeunload); 258 DEFINE_ATTRIBUTE_EVENT_LISTENER(blur); 259 DEFINE_ATTRIBUTE_EVENT_LISTENER(canplay); 260 DEFINE_ATTRIBUTE_EVENT_LISTENER(canplaythrough); 261 DEFINE_ATTRIBUTE_EVENT_LISTENER(change); 262 DEFINE_ATTRIBUTE_EVENT_LISTENER(click); 263 DEFINE_ATTRIBUTE_EVENT_LISTENER(contextmenu); 264 DEFINE_ATTRIBUTE_EVENT_LISTENER(dblclick); 265 DEFINE_ATTRIBUTE_EVENT_LISTENER(drag); 266 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragend); 267 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragenter); 268 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragleave); 269 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragover); 270 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragstart); 271 DEFINE_ATTRIBUTE_EVENT_LISTENER(drop); 272 DEFINE_ATTRIBUTE_EVENT_LISTENER(durationchange); 273 DEFINE_ATTRIBUTE_EVENT_LISTENER(emptied); 274 DEFINE_ATTRIBUTE_EVENT_LISTENER(ended); 275 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); 276 DEFINE_ATTRIBUTE_EVENT_LISTENER(focus); 277 DEFINE_ATTRIBUTE_EVENT_LISTENER(hashchange); 278 DEFINE_ATTRIBUTE_EVENT_LISTENER(input); 279 DEFINE_ATTRIBUTE_EVENT_LISTENER(invalid); 280 DEFINE_ATTRIBUTE_EVENT_LISTENER(keydown); 281 DEFINE_ATTRIBUTE_EVENT_LISTENER(keypress); 282 DEFINE_ATTRIBUTE_EVENT_LISTENER(keyup); 283 DEFINE_ATTRIBUTE_EVENT_LISTENER(load); 284 DEFINE_ATTRIBUTE_EVENT_LISTENER(loadeddata); 285 DEFINE_ATTRIBUTE_EVENT_LISTENER(loadedmetadata); 286 DEFINE_ATTRIBUTE_EVENT_LISTENER(loadstart); 287 DEFINE_ATTRIBUTE_EVENT_LISTENER(message); 288 DEFINE_ATTRIBUTE_EVENT_LISTENER(mousedown); 289 DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseenter); 290 DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseleave); 291 DEFINE_ATTRIBUTE_EVENT_LISTENER(mousemove); 292 DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseout); 293 DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseover); 294 DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseup); 295 DEFINE_ATTRIBUTE_EVENT_LISTENER(mousewheel); 296 DEFINE_ATTRIBUTE_EVENT_LISTENER(offline); 297 DEFINE_ATTRIBUTE_EVENT_LISTENER(online); 298 DEFINE_ATTRIBUTE_EVENT_LISTENER(pagehide); 299 DEFINE_ATTRIBUTE_EVENT_LISTENER(pageshow); 300 DEFINE_ATTRIBUTE_EVENT_LISTENER(pause); 301 DEFINE_ATTRIBUTE_EVENT_LISTENER(play); 302 DEFINE_ATTRIBUTE_EVENT_LISTENER(playing); 303 DEFINE_ATTRIBUTE_EVENT_LISTENER(popstate); 304 DEFINE_ATTRIBUTE_EVENT_LISTENER(progress); 305 DEFINE_ATTRIBUTE_EVENT_LISTENER(ratechange); 306 DEFINE_ATTRIBUTE_EVENT_LISTENER(reset); 307 DEFINE_ATTRIBUTE_EVENT_LISTENER(resize); 308 DEFINE_ATTRIBUTE_EVENT_LISTENER(scroll); 309 DEFINE_ATTRIBUTE_EVENT_LISTENER(search); 310 DEFINE_ATTRIBUTE_EVENT_LISTENER(seeked); 311 DEFINE_ATTRIBUTE_EVENT_LISTENER(seeking); 312 DEFINE_ATTRIBUTE_EVENT_LISTENER(select); 313 DEFINE_ATTRIBUTE_EVENT_LISTENER(stalled); 314 DEFINE_ATTRIBUTE_EVENT_LISTENER(storage); 315 DEFINE_ATTRIBUTE_EVENT_LISTENER(submit); 316 DEFINE_ATTRIBUTE_EVENT_LISTENER(suspend); 317 DEFINE_ATTRIBUTE_EVENT_LISTENER(timeupdate); 318 DEFINE_ATTRIBUTE_EVENT_LISTENER(unload); 319 DEFINE_ATTRIBUTE_EVENT_LISTENER(volumechange); 320 DEFINE_ATTRIBUTE_EVENT_LISTENER(waiting); 321 322 DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(webkitanimationstart, webkitAnimationStart); 323 DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(webkitanimationiteration, webkitAnimationIteration); 324 DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(webkitanimationend, webkitAnimationEnd); 325 DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(webkittransitionend, webkitTransitionEnd); 326 DEFINE_ATTRIBUTE_EVENT_LISTENER(transitionend); 327 328 void captureEvents() { } 329 void releaseEvents() { } 330 331 void finishedLoading(); 332 333 using RefCounted<DOMWindow>::ref; 334 using RefCounted<DOMWindow>::deref; 335 336 DEFINE_ATTRIBUTE_EVENT_LISTENER(devicemotion); 337 DEFINE_ATTRIBUTE_EVENT_LISTENER(deviceorientation); 338 339 // HTML 5 key/value storage 340 Storage* sessionStorage(ExceptionState&) const; 341 Storage* localStorage(ExceptionState&) const; 342 Storage* optionalSessionStorage() const { return m_sessionStorage.get(); } 343 Storage* optionalLocalStorage() const { return m_localStorage.get(); } 344 345 DOMApplicationCache* applicationCache() const; 346 DOMApplicationCache* optionalApplicationCache() const { return m_applicationCache.get(); } 347 348 #if ENABLE(ORIENTATION_EVENTS) 349 // This is the interface orientation in degrees. Some examples are: 350 // 0 is straight up; -90 is when the device is rotated 90 clockwise; 351 // 90 is when rotated counter clockwise. 352 int orientation() const; 353 354 DEFINE_ATTRIBUTE_EVENT_LISTENER(orientationchange); 355 #endif 356 357 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchstart); 358 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchmove); 359 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchend); 360 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchcancel); 361 362 Performance* performance() const; 363 364 // FIXME: When this DOMWindow is no longer the active DOMWindow (i.e., 365 // when its document is no longer the document that is displayed in its 366 // frame), we would like to zero out m_frame to avoid being confused 367 // by the document that is currently active in m_frame. 368 bool isCurrentlyDisplayedInFrame() const; 369 370 void willDetachDocumentFromFrame(); 371 DOMWindow* anonymousIndexedGetter(uint32_t); 372 373 bool isInsecureScriptAccess(DOMWindow* activeWindow, const String& urlString); 374 375 private: 376 explicit DOMWindow(Frame*); 377 378 Page* page(); 379 380 virtual void frameDestroyed() OVERRIDE; 381 virtual void willDetachPage() OVERRIDE; 382 383 virtual void refEventTarget() { ref(); } 384 virtual void derefEventTarget() { deref(); } 385 virtual EventTargetData* eventTargetData(); 386 virtual EventTargetData* ensureEventTargetData(); 387 388 void resetDOMWindowProperties(); 389 void willDestroyDocumentInFrame(); 390 391 RefPtr<Document> m_document; 392 393 bool m_shouldPrintWhenFinishedLoading; 394 395 HashSet<DOMWindowProperty*> m_properties; 396 397 mutable RefPtr<Screen> m_screen; 398 mutable RefPtr<History> m_history; 399 mutable RefPtr<BarProp> m_locationbar; 400 mutable RefPtr<BarProp> m_menubar; 401 mutable RefPtr<BarProp> m_personalbar; 402 mutable RefPtr<BarProp> m_scrollbars; 403 mutable RefPtr<BarProp> m_statusbar; 404 mutable RefPtr<BarProp> m_toolbar; 405 mutable RefPtr<Console> m_console; 406 mutable RefPtr<Navigator> m_navigator; 407 mutable RefPtr<Location> m_location; 408 mutable RefPtr<StyleMedia> m_media; 409 410 EventTargetData m_eventTargetData; 411 412 String m_status; 413 String m_defaultStatus; 414 415 mutable RefPtr<Storage> m_sessionStorage; 416 mutable RefPtr<Storage> m_localStorage; 417 mutable RefPtr<DOMApplicationCache> m_applicationCache; 418 419 mutable RefPtr<Performance> m_performance; 420 421 mutable RefPtr<DOMWindowCSS> m_css; 422 }; 423 424 inline String DOMWindow::status() const 425 { 426 return m_status; 427 } 428 429 inline String DOMWindow::defaultStatus() const 430 { 431 return m_defaultStatus; 432 } 433 434 } // namespace WebCore 435 436 #endif // DOMWindow_h 437