DOMWindow.h revision 65f03d4f644ce73618e5f4f50dd694b26f55ae12
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 "KURL.h" 31#include "MessagePort.h" 32#include "SecurityOrigin.h" 33 34namespace WebCore { 35 36 class BarInfo; 37 class CSSRuleList; 38 class CSSStyleDeclaration; 39 class Console; 40 class DOMApplicationCache; 41 class DOMSelection; 42 class DOMURL; 43 class Database; 44 class DatabaseCallback; 45 class Document; 46 class Element; 47 class ErrorCallback; 48 class EventListener; 49 class FileSystemCallback; 50 class FloatRect; 51 class History; 52 class IDBFactory; 53 class Location; 54 class MediaQueryList; 55 class Navigator; 56 class Node; 57 class NotificationCenter; 58 class Performance; 59 class PostMessageTimer; 60 class ScheduledAction; 61 class Screen; 62 class SerializedScriptValue; 63 class Storage; 64 class StyleMedia; 65 class WebKitPoint; 66 67 struct WindowFeatures; 68 69 typedef int ExceptionCode; 70 71 enum SetLocationLocking { LockHistoryBasedOnGestureState, LockHistoryAndBackForwardList }; 72 73 class DOMWindow : public RefCounted<DOMWindow>, public EventTarget { 74 public: 75 static PassRefPtr<DOMWindow> create(Frame* frame) { return adoptRef(new DOMWindow(frame)); } 76 virtual ~DOMWindow(); 77 78 virtual DOMWindow* toDOMWindow() { return this; } 79 virtual ScriptExecutionContext* scriptExecutionContext() const; 80 81 Frame* frame() const { return m_frame; } 82 void disconnectFrame(); 83 84 void clear(); 85 86 PassRefPtr<MediaQueryList> matchMedia(const String&); 87 88 void setSecurityOrigin(SecurityOrigin* securityOrigin) { m_securityOrigin = securityOrigin; } 89 SecurityOrigin* securityOrigin() const { return m_securityOrigin.get(); } 90 91 void setURL(const KURL& url) { m_url = url; } 92 KURL url() const { return m_url; } 93 94 unsigned pendingUnloadEventListeners() const; 95 96 static bool dispatchAllPendingBeforeUnloadEvents(); 97 static void dispatchAllPendingUnloadEvents(); 98 99 static void adjustWindowRect(const FloatRect& screen, FloatRect& window, const FloatRect& pendingChanges); 100 101 // FIXME: We can remove this function once V8 showModalDialog is changed to use DOMWindow. 102 static void parseModalDialogFeatures(const String&, HashMap<String, String>&); 103 104 bool allowPopUp(); // Call on first window, not target window. 105 static bool allowPopUp(Frame* firstFrame); 106 static bool canShowModalDialog(const Frame*); 107 static bool canShowModalDialogNow(const Frame*); 108 109 // DOM Level 0 110 111 Screen* screen() const; 112 History* history() const; 113 BarInfo* locationbar() const; 114 BarInfo* menubar() const; 115 BarInfo* personalbar() const; 116 BarInfo* scrollbars() const; 117 BarInfo* statusbar() const; 118 BarInfo* toolbar() const; 119 Navigator* navigator() const; 120 Navigator* clientInformation() const { return navigator(); } 121 122 Location* location() const; 123 void setLocation(const String& location, DOMWindow* activeWindow, DOMWindow* firstWindow, 124 SetLocationLocking = LockHistoryBasedOnGestureState); 125 126 DOMSelection* getSelection(); 127 128 Element* frameElement() const; 129 130 void focus(); 131 void blur(); 132 void close(ScriptExecutionContext* = 0); 133 void print(); 134 void stop(); 135 136 PassRefPtr<DOMWindow> open(const String& urlString, const AtomicString& frameName, const String& windowFeaturesString, 137 DOMWindow* activeWindow, DOMWindow* firstWindow); 138 139 typedef void (*PrepareDialogFunction)(DOMWindow*, void* context); 140 void showModalDialog(const String& urlString, const String& dialogFeaturesString, 141 DOMWindow* activeWindow, DOMWindow* firstWindow, PrepareDialogFunction, void* functionContext); 142 143 void alert(const String& message); 144 bool confirm(const String& message); 145 String prompt(const String& message, const String& defaultValue); 146 String btoa(const String& stringToEncode, ExceptionCode&); 147 String atob(const String& encodedString, ExceptionCode&); 148 149 bool find(const String&, bool caseSensitive, bool backwards, bool wrap, bool wholeWord, bool searchInFrames, bool showDialog) const; 150 151 bool offscreenBuffering() const; 152 153 int outerHeight() const; 154 int outerWidth() const; 155 int innerHeight() const; 156 int innerWidth() const; 157 int screenX() const; 158 int screenY() const; 159 int screenLeft() const { return screenX(); } 160 int screenTop() const { return screenY(); } 161 int scrollX() const; 162 int scrollY() const; 163 int pageXOffset() const { return scrollX(); } 164 int pageYOffset() const { return scrollY(); } 165 166 bool closed() const; 167 168 unsigned length() const; 169 170 String name() const; 171 void setName(const String&); 172 173 String status() const; 174 void setStatus(const String&); 175 String defaultStatus() const; 176 void setDefaultStatus(const String&); 177 178 // This attribute is an alias of defaultStatus and is necessary for legacy uses. 179 String defaultstatus() const { return defaultStatus(); } 180 void setDefaultstatus(const String& status) { setDefaultStatus(status); } 181 182 // Self-referential attributes 183 184 DOMWindow* self() const; 185 DOMWindow* window() const { return self(); } 186 DOMWindow* frames() const { return self(); } 187 188 DOMWindow* opener() const; 189 DOMWindow* parent() const; 190 DOMWindow* top() const; 191 192 // DOM Level 2 AbstractView Interface 193 194 Document* document() const; 195 196 // CSSOM View Module 197 198 PassRefPtr<StyleMedia> styleMedia() const; 199 200 // DOM Level 2 Style Interface 201 202 PassRefPtr<CSSStyleDeclaration> getComputedStyle(Element*, const String& pseudoElt) const; 203 204 // WebKit extensions 205 206 PassRefPtr<CSSRuleList> getMatchedCSSRules(Element*, const String& pseudoElt, bool authorOnly = true) const; 207 double devicePixelRatio() const; 208 209 PassRefPtr<WebKitPoint> webkitConvertPointFromPageToNode(Node*, const WebKitPoint*) const; 210 PassRefPtr<WebKitPoint> webkitConvertPointFromNodeToPage(Node*, const WebKitPoint*) const; 211 212 Console* console() const; 213 214 void printErrorMessage(const String&); 215 String crossDomainAccessErrorMessage(DOMWindow* activeWindow); 216 217 void pageDestroyed(); 218 219 void postMessage(PassRefPtr<SerializedScriptValue> message, const MessagePortArray*, const String& targetOrigin, DOMWindow* source, ExceptionCode&); 220 // FIXME: remove this when we update the ObjC bindings (bug #28774). 221 void postMessage(PassRefPtr<SerializedScriptValue> message, MessagePort*, const String& targetOrigin, DOMWindow* source, ExceptionCode&); 222 void postMessageTimerFired(PostMessageTimer*); 223 224 void scrollBy(int x, int y) const; 225 void scrollTo(int x, int y) const; 226 void scroll(int x, int y) const { scrollTo(x, y); } 227 228 void moveBy(float x, float y) const; 229 void moveTo(float x, float y) const; 230 231 void resizeBy(float x, float y) const; 232 void resizeTo(float width, float height) const; 233 234 // Timers 235 int setTimeout(PassOwnPtr<ScheduledAction>, int timeout, ExceptionCode&); 236 void clearTimeout(int timeoutId); 237 int setInterval(PassOwnPtr<ScheduledAction>, int timeout, ExceptionCode&); 238 void clearInterval(int timeoutId); 239 240 // Events 241 // EventTarget API 242 virtual bool addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture); 243 virtual bool removeEventListener(const AtomicString& eventType, EventListener*, bool useCapture); 244 virtual void removeAllEventListeners(); 245 246 using EventTarget::dispatchEvent; 247 bool dispatchEvent(PassRefPtr<Event> prpEvent, PassRefPtr<EventTarget> prpTarget); 248 void dispatchLoadEvent(); 249 void dispatchTimedEvent(PassRefPtr<Event> event, Document* target, double* startTime, double* endTime); 250 251 DEFINE_ATTRIBUTE_EVENT_LISTENER(abort); 252 DEFINE_ATTRIBUTE_EVENT_LISTENER(beforeunload); 253 DEFINE_ATTRIBUTE_EVENT_LISTENER(blur); 254 DEFINE_ATTRIBUTE_EVENT_LISTENER(canplay); 255 DEFINE_ATTRIBUTE_EVENT_LISTENER(canplaythrough); 256 DEFINE_ATTRIBUTE_EVENT_LISTENER(change); 257 DEFINE_ATTRIBUTE_EVENT_LISTENER(click); 258 DEFINE_ATTRIBUTE_EVENT_LISTENER(contextmenu); 259 DEFINE_ATTRIBUTE_EVENT_LISTENER(dblclick); 260 DEFINE_ATTRIBUTE_EVENT_LISTENER(drag); 261 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragend); 262 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragenter); 263 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragleave); 264 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragover); 265 DEFINE_ATTRIBUTE_EVENT_LISTENER(dragstart); 266 DEFINE_ATTRIBUTE_EVENT_LISTENER(drop); 267 DEFINE_ATTRIBUTE_EVENT_LISTENER(durationchange); 268 DEFINE_ATTRIBUTE_EVENT_LISTENER(emptied); 269 DEFINE_ATTRIBUTE_EVENT_LISTENER(ended); 270 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); 271 DEFINE_ATTRIBUTE_EVENT_LISTENER(focus); 272 DEFINE_ATTRIBUTE_EVENT_LISTENER(hashchange); 273 DEFINE_ATTRIBUTE_EVENT_LISTENER(input); 274 DEFINE_ATTRIBUTE_EVENT_LISTENER(invalid); 275 DEFINE_ATTRIBUTE_EVENT_LISTENER(keydown); 276 DEFINE_ATTRIBUTE_EVENT_LISTENER(keypress); 277 DEFINE_ATTRIBUTE_EVENT_LISTENER(keyup); 278 DEFINE_ATTRIBUTE_EVENT_LISTENER(load); 279 DEFINE_ATTRIBUTE_EVENT_LISTENER(loadeddata); 280 DEFINE_ATTRIBUTE_EVENT_LISTENER(loadedmetadata); 281 DEFINE_ATTRIBUTE_EVENT_LISTENER(loadstart); 282 DEFINE_ATTRIBUTE_EVENT_LISTENER(message); 283 DEFINE_ATTRIBUTE_EVENT_LISTENER(mousedown); 284 DEFINE_ATTRIBUTE_EVENT_LISTENER(mousemove); 285 DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseout); 286 DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseover); 287 DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseup); 288 DEFINE_ATTRIBUTE_EVENT_LISTENER(mousewheel); 289 DEFINE_ATTRIBUTE_EVENT_LISTENER(offline); 290 DEFINE_ATTRIBUTE_EVENT_LISTENER(online); 291 DEFINE_ATTRIBUTE_EVENT_LISTENER(pagehide); 292 DEFINE_ATTRIBUTE_EVENT_LISTENER(pageshow); 293 DEFINE_ATTRIBUTE_EVENT_LISTENER(pause); 294 DEFINE_ATTRIBUTE_EVENT_LISTENER(play); 295 DEFINE_ATTRIBUTE_EVENT_LISTENER(playing); 296 DEFINE_ATTRIBUTE_EVENT_LISTENER(popstate); 297 DEFINE_ATTRIBUTE_EVENT_LISTENER(progress); 298 DEFINE_ATTRIBUTE_EVENT_LISTENER(ratechange); 299 DEFINE_ATTRIBUTE_EVENT_LISTENER(reset); 300 DEFINE_ATTRIBUTE_EVENT_LISTENER(resize); 301 DEFINE_ATTRIBUTE_EVENT_LISTENER(scroll); 302 DEFINE_ATTRIBUTE_EVENT_LISTENER(search); 303 DEFINE_ATTRIBUTE_EVENT_LISTENER(seeked); 304 DEFINE_ATTRIBUTE_EVENT_LISTENER(seeking); 305 DEFINE_ATTRIBUTE_EVENT_LISTENER(select); 306 DEFINE_ATTRIBUTE_EVENT_LISTENER(stalled); 307 DEFINE_ATTRIBUTE_EVENT_LISTENER(storage); 308 DEFINE_ATTRIBUTE_EVENT_LISTENER(submit); 309 DEFINE_ATTRIBUTE_EVENT_LISTENER(suspend); 310 DEFINE_ATTRIBUTE_EVENT_LISTENER(timeupdate); 311 DEFINE_ATTRIBUTE_EVENT_LISTENER(unload); 312 DEFINE_ATTRIBUTE_EVENT_LISTENER(volumechange); 313 DEFINE_ATTRIBUTE_EVENT_LISTENER(waiting); 314 DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitbeginfullscreen); 315 DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitendfullscreen); 316 317 DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(webkitanimationstart, webkitAnimationStart); 318 DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(webkitanimationiteration, webkitAnimationIteration); 319 DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(webkitanimationend, webkitAnimationEnd); 320 DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(webkittransitionend, webkitTransitionEnd); 321 322 void captureEvents(); 323 void releaseEvents(); 324 325 void finishedLoading(); 326 327 // These functions are used for GC marking. See JSDOMWindow::markChildren(MarkStack&) in JSDOMWindowCustom.cpp. 328 Screen* optionalScreen() const { return m_screen.get(); } 329 DOMSelection* optionalSelection() const { return m_selection.get(); } 330 History* optionalHistory() const { return m_history.get(); } 331 BarInfo* optionalLocationbar() const { return m_locationbar.get(); } 332 BarInfo* optionalMenubar() const { return m_menubar.get(); } 333 BarInfo* optionalPersonalbar() const { return m_personalbar.get(); } 334 BarInfo* optionalScrollbars() const { return m_scrollbars.get(); } 335 BarInfo* optionalStatusbar() const { return m_statusbar.get(); } 336 BarInfo* optionalToolbar() const { return m_toolbar.get(); } 337 Console* optionalConsole() const { return m_console.get(); } 338 Navigator* optionalNavigator() const { return m_navigator.get(); } 339 Location* optionalLocation() const { return m_location.get(); } 340 StyleMedia* optionalMedia() const { return m_media.get(); } 341 342 using RefCounted<DOMWindow>::ref; 343 using RefCounted<DOMWindow>::deref; 344 345#if ENABLE(BLOB) 346 DOMURL* webkitURL() const; 347#endif 348 349#if ENABLE(DATABASE) 350 // HTML 5 client-side database 351 PassRefPtr<Database> openDatabase(const String& name, const String& version, const String& displayName, unsigned long estimatedSize, PassRefPtr<DatabaseCallback> creationCallback, ExceptionCode&); 352#endif 353 354#if ENABLE(DEVICE_ORIENTATION) 355 DEFINE_ATTRIBUTE_EVENT_LISTENER(devicemotion); 356 DEFINE_ATTRIBUTE_EVENT_LISTENER(deviceorientation); 357#endif 358 359#if ENABLE(DOM_STORAGE) 360 // HTML 5 key/value storage 361 Storage* sessionStorage(ExceptionCode&) const; 362 Storage* localStorage(ExceptionCode&) const; 363 Storage* optionalSessionStorage() const { return m_sessionStorage.get(); } 364 Storage* optionalLocalStorage() const { return m_localStorage.get(); } 365#ifdef ANDROID 366 void clearDOMStorage(); 367#endif 368#endif 369 370#if ENABLE(FILE_SYSTEM) 371 // They are placed here and in all capital letters so they can be checked against the constants in the 372 // IDL at compile time. 373 enum FileSystemType { 374 TEMPORARY, 375 PERSISTENT, 376 }; 377 void requestFileSystem(int type, long long size, PassRefPtr<FileSystemCallback>, PassRefPtr<ErrorCallback>); 378#endif 379 380#if ENABLE(INDEXED_DATABASE) 381 IDBFactory* webkitIndexedDB() const; 382#endif 383 384#if ENABLE(NOTIFICATIONS) 385 NotificationCenter* webkitNotifications() const; 386#endif 387 388#if ENABLE(OFFLINE_WEB_APPLICATIONS) 389 DOMApplicationCache* applicationCache() const; 390 DOMApplicationCache* optionalApplicationCache() const { return m_applicationCache.get(); } 391#endif 392 393#if ENABLE(ORIENTATION_EVENTS) 394 // This is the interface orientation in degrees. Some examples are: 395 // 0 is straight up; -90 is when the device is rotated 90 clockwise; 396 // 90 is when rotated counter clockwise. 397 int orientation() const; 398 399 DEFINE_ATTRIBUTE_EVENT_LISTENER(orientationchange); 400#endif 401 402#if ENABLE(TOUCH_EVENTS) 403 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchstart); 404 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchmove); 405 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchend); 406 DEFINE_ATTRIBUTE_EVENT_LISTENER(touchcancel); 407#endif 408 409#if ENABLE(WEB_TIMING) 410 Performance* performance() const; 411 Performance* optionalPerformance() const { return m_performance.get(); } 412#endif 413 414 private: 415 DOMWindow(Frame*); 416 417 virtual void refEventTarget() { ref(); } 418 virtual void derefEventTarget() { deref(); } 419 virtual EventTargetData* eventTargetData(); 420 virtual EventTargetData* ensureEventTargetData(); 421 422 static Frame* createWindow(const String& urlString, const AtomicString& frameName, const WindowFeatures&, 423 DOMWindow* activeWindow, Frame* firstFrame, Frame* openerFrame, 424 PrepareDialogFunction = 0, void* functionContext = 0); 425 bool isInsecureScriptAccess(DOMWindow* activeWindow, const String& urlString); 426 427 RefPtr<SecurityOrigin> m_securityOrigin; 428 KURL m_url; 429 430 bool m_shouldPrintWhenFinishedLoading; 431 Frame* m_frame; 432 mutable RefPtr<Screen> m_screen; 433 mutable RefPtr<DOMSelection> m_selection; 434 mutable RefPtr<History> m_history; 435 mutable RefPtr<BarInfo> m_locationbar; 436 mutable RefPtr<BarInfo> m_menubar; 437 mutable RefPtr<BarInfo> m_personalbar; 438 mutable RefPtr<BarInfo> m_scrollbars; 439 mutable RefPtr<BarInfo> m_statusbar; 440 mutable RefPtr<BarInfo> m_toolbar; 441 mutable RefPtr<Console> m_console; 442 mutable RefPtr<Navigator> m_navigator; 443 mutable RefPtr<Location> m_location; 444 mutable RefPtr<StyleMedia> m_media; 445 446 EventTargetData m_eventTargetData; 447 448 String m_status; 449 String m_defaultStatus; 450 451#if ENABLE(DOM_STORAGE) 452 mutable RefPtr<Storage> m_sessionStorage; 453 mutable RefPtr<Storage> m_localStorage; 454#endif 455 456#if ENABLE(INDEXED_DATABASE) 457 mutable RefPtr<IDBFactory> m_idbFactory; 458#endif 459 460#if ENABLE(OFFLINE_WEB_APPLICATIONS) 461 mutable RefPtr<DOMApplicationCache> m_applicationCache; 462#endif 463 464#if ENABLE(NOTIFICATIONS) 465 mutable RefPtr<NotificationCenter> m_notifications; 466#endif 467 468#if ENABLE(WEB_TIMING) 469 mutable RefPtr<Performance> m_performance; 470#endif 471 472#if ENABLE(BLOB) 473 mutable RefPtr<DOMURL> m_domURL; 474#endif 475 }; 476 477 inline String DOMWindow::status() const 478 { 479 return m_status; 480 } 481 482 inline String DOMWindow::defaultStatus() const 483 { 484 return m_defaultStatus; 485 } 486 487} // namespace WebCore 488 489#endif // DOMWindow_h 490