Home | History | Annotate | Download | only in page
      1 /*
      2  * Copyright (C) 2006, 2007, 2009 Apple Inc.  All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      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.
     12  *
     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.
     24  */
     25 
     26 #ifndef DOMWindow_h
     27 #define DOMWindow_h
     28 
     29 #include "EventTarget.h"
     30 #include "KURL.h"
     31 #include "MessagePort.h"
     32 #include "PlatformString.h"
     33 #include "RegisteredEventListener.h"
     34 #include "SecurityOrigin.h"
     35 #include <wtf/Forward.h>
     36 #include <wtf/RefCounted.h>
     37 #include <wtf/RefPtr.h>
     38 
     39 namespace WebCore {
     40 
     41     class BarInfo;
     42     class BeforeUnloadEvent;
     43     class CSSRuleList;
     44     class CSSStyleDeclaration;
     45     class Console;
     46     class DOMSelection;
     47     class Database;
     48     class Document;
     49     class Element;
     50     class Event;
     51     class EventListener;
     52     class FloatRect;
     53     class Frame;
     54     class History;
     55     class IndexedDatabaseRequest;
     56     class InspectorTimelineAgent;
     57     class Location;
     58     class Media;
     59     class Navigator;
     60     class Node;
     61     class NotificationCenter;
     62     class PostMessageTimer;
     63     class ScheduledAction;
     64     class SerializedScriptValue;
     65     class Screen;
     66     class WebKitPoint;
     67 
     68 #if ENABLE(DOM_STORAGE)
     69     class Storage;
     70 #endif
     71 
     72 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
     73     class DOMApplicationCache;
     74 #endif
     75 
     76     typedef int ExceptionCode;
     77 
     78     class DOMWindow : public RefCounted<DOMWindow>, public EventTarget {
     79     public:
     80         static PassRefPtr<DOMWindow> create(Frame* frame) { return adoptRef(new DOMWindow(frame)); }
     81         virtual ~DOMWindow();
     82 
     83         virtual DOMWindow* toDOMWindow() { return this; }
     84         virtual ScriptExecutionContext* scriptExecutionContext() const;
     85 
     86         Frame* frame() const { return m_frame; }
     87         void disconnectFrame();
     88 
     89         void clear();
     90 
     91 #if ENABLE(ORIENTATION_EVENTS)
     92         // This is the interface orientation in degrees. Some examples are:
     93         //  0 is straight up; -90 is when the device is rotated 90 clockwise;
     94         //  90 is when rotated counter clockwise.
     95         int orientation() const;
     96 #endif
     97 
     98         void setSecurityOrigin(SecurityOrigin* securityOrigin) { m_securityOrigin = securityOrigin; }
     99         SecurityOrigin* securityOrigin() const { return m_securityOrigin.get(); }
    100 
    101         void setURL(const KURL& url) { m_url = url; }
    102         KURL url() const { return m_url; }
    103 
    104         unsigned pendingUnloadEventListeners() const;
    105 
    106         static bool dispatchAllPendingBeforeUnloadEvents();
    107         static void dispatchAllPendingUnloadEvents();
    108 
    109         static void adjustWindowRect(const FloatRect& screen, FloatRect& window, const FloatRect& pendingChanges);
    110         static void parseModalDialogFeatures(const String& featuresArg, HashMap<String, String>& map);
    111 
    112         static bool allowPopUp(Frame* activeFrame);
    113         static bool canShowModalDialog(const Frame*);
    114         static bool canShowModalDialogNow(const Frame*);
    115 
    116         // DOM Level 0
    117         Screen* screen() const;
    118         History* history() const;
    119         BarInfo* locationbar() const;
    120         BarInfo* menubar() const;
    121         BarInfo* personalbar() const;
    122         BarInfo* scrollbars() const;
    123         BarInfo* statusbar() const;
    124         BarInfo* toolbar() const;
    125         Navigator* navigator() const;
    126         Navigator* clientInformation() const { return navigator(); }
    127         Location* location() const;
    128 
    129         DOMSelection* getSelection();
    130 
    131         Element* frameElement() const;
    132 
    133         void focus();
    134         void blur();
    135         void close();
    136         void print();
    137         void stop();
    138 
    139         void alert(const String& message);
    140         bool confirm(const String& message);
    141         String prompt(const String& message, const String& defaultValue);
    142 
    143         bool find(const String&, bool caseSensitive, bool backwards, bool wrap, bool wholeWord, bool searchInFrames, bool showDialog) const;
    144 
    145         bool offscreenBuffering() const;
    146 
    147         int outerHeight() const;
    148         int outerWidth() const;
    149         int innerHeight() const;
    150         int innerWidth() const;
    151         int screenX() const;
    152         int screenY() const;
    153         int screenLeft() const { return screenX(); }
    154         int screenTop() const { return screenY(); }
    155         int scrollX() const;
    156         int scrollY() const;
    157         int pageXOffset() const { return scrollX(); }
    158         int pageYOffset() const { return scrollY(); }
    159 
    160         bool closed() const;
    161 
    162         unsigned length() const;
    163 
    164         String name() const;
    165         void setName(const String&);
    166 
    167         String status() const;
    168         void setStatus(const String&);
    169         String defaultStatus() const;
    170         void setDefaultStatus(const String&);
    171         // This attribute is an alias of defaultStatus and is necessary for legacy uses.
    172         String defaultstatus() const { return defaultStatus(); }
    173         void setDefaultstatus(const String& status) { setDefaultStatus(status); }
    174 
    175         // Self referential attributes
    176         DOMWindow* self() const;
    177         DOMWindow* window() const { return self(); }
    178         DOMWindow* frames() const { return self(); }
    179 
    180         DOMWindow* opener() const;
    181         DOMWindow* parent() const;
    182         DOMWindow* top() const;
    183 
    184         // DOM Level 2 AbstractView Interface
    185         Document* document() const;
    186         // CSSOM View Module
    187         PassRefPtr<Media> media() const;
    188 
    189         // DOM Level 2 Style Interface
    190         PassRefPtr<CSSStyleDeclaration> getComputedStyle(Element*, const String& pseudoElt) const;
    191 
    192         // WebKit extensions
    193         PassRefPtr<CSSRuleList> getMatchedCSSRules(Element*, const String& pseudoElt, bool authorOnly = true) const;
    194         double devicePixelRatio() const;
    195 
    196         PassRefPtr<WebKitPoint> webkitConvertPointFromPageToNode(Node* node, const WebKitPoint* p) const;
    197         PassRefPtr<WebKitPoint> webkitConvertPointFromNodeToPage(Node* node, const WebKitPoint* p) const;
    198 
    199 #if ENABLE(DATABASE)
    200         // HTML 5 client-side database
    201         PassRefPtr<Database> openDatabase(const String& name, const String& version, const String& displayName, unsigned long estimatedSize, ExceptionCode&);
    202 #endif
    203 
    204 #if ENABLE(DOM_STORAGE)
    205         // HTML 5 key/value storage
    206         Storage* sessionStorage() const;
    207         Storage* localStorage() const;
    208 #endif
    209 
    210         Console* console() const;
    211 
    212 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
    213         DOMApplicationCache* applicationCache() const;
    214 #endif
    215 
    216 #if ENABLE(NOTIFICATIONS)
    217         NotificationCenter* webkitNotifications() const;
    218 #endif
    219 
    220 #if ENABLE(INDEXED_DATABASE)
    221         IndexedDatabaseRequest* indexedDB() const;
    222 #endif
    223 
    224         void postMessage(PassRefPtr<SerializedScriptValue> message, const MessagePortArray*, const String& targetOrigin, DOMWindow* source, ExceptionCode&);
    225         // FIXME: remove this when we update the ObjC bindings (bug #28774).
    226         void postMessage(PassRefPtr<SerializedScriptValue> message, MessagePort*, const String& targetOrigin, DOMWindow* source, ExceptionCode&);
    227         void postMessageTimerFired(PostMessageTimer*);
    228 
    229         void scrollBy(int x, int y) const;
    230         void scrollTo(int x, int y) const;
    231         void scroll(int x, int y) const { scrollTo(x, y); }
    232 
    233         void moveBy(float x, float y) const;
    234         void moveTo(float x, float y) const;
    235 
    236         void resizeBy(float x, float y) const;
    237         void resizeTo(float width, float height) const;
    238 
    239         // Timers
    240         int setTimeout(ScheduledAction*, int timeout, ExceptionCode&);
    241         void clearTimeout(int timeoutId);
    242         int setInterval(ScheduledAction*, int timeout, ExceptionCode&);
    243         void clearInterval(int timeoutId);
    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         void dispatchLoadEvent();
    254 
    255         DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
    256         DEFINE_ATTRIBUTE_EVENT_LISTENER(beforeunload);
    257         DEFINE_ATTRIBUTE_EVENT_LISTENER(blur);
    258         DEFINE_ATTRIBUTE_EVENT_LISTENER(canplay);
    259         DEFINE_ATTRIBUTE_EVENT_LISTENER(canplaythrough);
    260         DEFINE_ATTRIBUTE_EVENT_LISTENER(change);
    261         DEFINE_ATTRIBUTE_EVENT_LISTENER(click);
    262         DEFINE_ATTRIBUTE_EVENT_LISTENER(contextmenu);
    263         DEFINE_ATTRIBUTE_EVENT_LISTENER(dblclick);
    264         DEFINE_ATTRIBUTE_EVENT_LISTENER(drag);
    265         DEFINE_ATTRIBUTE_EVENT_LISTENER(dragend);
    266         DEFINE_ATTRIBUTE_EVENT_LISTENER(dragenter);
    267         DEFINE_ATTRIBUTE_EVENT_LISTENER(dragleave);
    268         DEFINE_ATTRIBUTE_EVENT_LISTENER(dragover);
    269         DEFINE_ATTRIBUTE_EVENT_LISTENER(dragstart);
    270         DEFINE_ATTRIBUTE_EVENT_LISTENER(drop);
    271         DEFINE_ATTRIBUTE_EVENT_LISTENER(durationchange);
    272         DEFINE_ATTRIBUTE_EVENT_LISTENER(emptied);
    273         DEFINE_ATTRIBUTE_EVENT_LISTENER(ended);
    274         DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
    275         DEFINE_ATTRIBUTE_EVENT_LISTENER(focus);
    276         DEFINE_ATTRIBUTE_EVENT_LISTENER(hashchange);
    277         DEFINE_ATTRIBUTE_EVENT_LISTENER(input);
    278         DEFINE_ATTRIBUTE_EVENT_LISTENER(invalid);
    279         DEFINE_ATTRIBUTE_EVENT_LISTENER(keydown);
    280         DEFINE_ATTRIBUTE_EVENT_LISTENER(keypress);
    281         DEFINE_ATTRIBUTE_EVENT_LISTENER(keyup);
    282         DEFINE_ATTRIBUTE_EVENT_LISTENER(load);
    283         DEFINE_ATTRIBUTE_EVENT_LISTENER(loadeddata);
    284         DEFINE_ATTRIBUTE_EVENT_LISTENER(loadedmetadata);
    285         DEFINE_ATTRIBUTE_EVENT_LISTENER(loadstart);
    286         DEFINE_ATTRIBUTE_EVENT_LISTENER(message);
    287         DEFINE_ATTRIBUTE_EVENT_LISTENER(mousedown);
    288         DEFINE_ATTRIBUTE_EVENT_LISTENER(mousemove);
    289         DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseout);
    290         DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseover);
    291         DEFINE_ATTRIBUTE_EVENT_LISTENER(mouseup);
    292         DEFINE_ATTRIBUTE_EVENT_LISTENER(mousewheel);
    293         DEFINE_ATTRIBUTE_EVENT_LISTENER(offline);
    294         DEFINE_ATTRIBUTE_EVENT_LISTENER(online);
    295         DEFINE_ATTRIBUTE_EVENT_LISTENER(pagehide);
    296         DEFINE_ATTRIBUTE_EVENT_LISTENER(pageshow);
    297         DEFINE_ATTRIBUTE_EVENT_LISTENER(pause);
    298         DEFINE_ATTRIBUTE_EVENT_LISTENER(play);
    299         DEFINE_ATTRIBUTE_EVENT_LISTENER(playing);
    300         DEFINE_ATTRIBUTE_EVENT_LISTENER(popstate);
    301         DEFINE_ATTRIBUTE_EVENT_LISTENER(progress);
    302         DEFINE_ATTRIBUTE_EVENT_LISTENER(ratechange);
    303         DEFINE_ATTRIBUTE_EVENT_LISTENER(reset);
    304         DEFINE_ATTRIBUTE_EVENT_LISTENER(resize);
    305         DEFINE_ATTRIBUTE_EVENT_LISTENER(scroll);
    306         DEFINE_ATTRIBUTE_EVENT_LISTENER(search);
    307         DEFINE_ATTRIBUTE_EVENT_LISTENER(seeked);
    308         DEFINE_ATTRIBUTE_EVENT_LISTENER(seeking);
    309         DEFINE_ATTRIBUTE_EVENT_LISTENER(select);
    310         DEFINE_ATTRIBUTE_EVENT_LISTENER(stalled);
    311         DEFINE_ATTRIBUTE_EVENT_LISTENER(storage);
    312         DEFINE_ATTRIBUTE_EVENT_LISTENER(submit);
    313         DEFINE_ATTRIBUTE_EVENT_LISTENER(suspend);
    314         DEFINE_ATTRIBUTE_EVENT_LISTENER(timeupdate);
    315         DEFINE_ATTRIBUTE_EVENT_LISTENER(unload);
    316         DEFINE_ATTRIBUTE_EVENT_LISTENER(volumechange);
    317         DEFINE_ATTRIBUTE_EVENT_LISTENER(waiting);
    318         DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitbeginfullscreen);
    319         DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitendfullscreen);
    320 
    321 #if ENABLE(ORIENTATION_EVENTS)
    322         DEFINE_ATTRIBUTE_EVENT_LISTENER(orientationchange);
    323 #endif
    324 
    325         DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(webkitanimationstart, webkitAnimationStart);
    326         DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(webkitanimationiteration, webkitAnimationIteration);
    327         DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(webkitanimationend, webkitAnimationEnd);
    328         DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(webkittransitionend, webkitTransitionEnd);
    329 
    330 #if ENABLE(TOUCH_EVENTS)
    331         DEFINE_ATTRIBUTE_EVENT_LISTENER(touchstart);
    332         DEFINE_ATTRIBUTE_EVENT_LISTENER(touchmove);
    333         DEFINE_ATTRIBUTE_EVENT_LISTENER(touchend);
    334         DEFINE_ATTRIBUTE_EVENT_LISTENER(touchcancel);
    335 #endif
    336         void captureEvents();
    337         void releaseEvents();
    338 
    339         // These methods are used for GC marking. See JSDOMWindow::markChildren(MarkStack&) in
    340         // JSDOMWindowCustom.cpp.
    341         Screen* optionalScreen() const { return m_screen.get(); }
    342         DOMSelection* optionalSelection() const { return m_selection.get(); }
    343         History* optionalHistory() const { return m_history.get(); }
    344         BarInfo* optionalLocationbar() const { return m_locationbar.get(); }
    345         BarInfo* optionalMenubar() const { return m_menubar.get(); }
    346         BarInfo* optionalPersonalbar() const { return m_personalbar.get(); }
    347         BarInfo* optionalScrollbars() const { return m_scrollbars.get(); }
    348         BarInfo* optionalStatusbar() const { return m_statusbar.get(); }
    349         BarInfo* optionalToolbar() const { return m_toolbar.get(); }
    350         Console* optionalConsole() const { return m_console.get(); }
    351         Navigator* optionalNavigator() const { return m_navigator.get(); }
    352         Location* optionalLocation() const { return m_location.get(); }
    353         Media* optionalMedia() const { return m_media.get(); }
    354 #if ENABLE(DOM_STORAGE)
    355         Storage* optionalSessionStorage() const { return m_sessionStorage.get(); }
    356         Storage* optionalLocalStorage() const { return m_localStorage.get(); }
    357 #endif
    358 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
    359         DOMApplicationCache* optionalApplicationCache() const { return m_applicationCache.get(); }
    360 #endif
    361 
    362         using RefCounted<DOMWindow>::ref;
    363         using RefCounted<DOMWindow>::deref;
    364 
    365     private:
    366         DOMWindow(Frame*);
    367 
    368         virtual void refEventTarget() { ref(); }
    369         virtual void derefEventTarget() { deref(); }
    370         virtual EventTargetData* eventTargetData();
    371         virtual EventTargetData* ensureEventTargetData();
    372         InspectorTimelineAgent* inspectorTimelineAgent();
    373 
    374         RefPtr<SecurityOrigin> m_securityOrigin;
    375         KURL m_url;
    376 
    377         Frame* m_frame;
    378         mutable RefPtr<Screen> m_screen;
    379         mutable RefPtr<DOMSelection> m_selection;
    380         mutable RefPtr<History> m_history;
    381         mutable RefPtr<BarInfo> m_locationbar;
    382         mutable RefPtr<BarInfo> m_menubar;
    383         mutable RefPtr<BarInfo> m_personalbar;
    384         mutable RefPtr<BarInfo> m_scrollbars;
    385         mutable RefPtr<BarInfo> m_statusbar;
    386         mutable RefPtr<BarInfo> m_toolbar;
    387         mutable RefPtr<Console> m_console;
    388         mutable RefPtr<Navigator> m_navigator;
    389         mutable RefPtr<Location> m_location;
    390         mutable RefPtr<Media> m_media;
    391 #if ENABLE(DOM_STORAGE)
    392         mutable RefPtr<Storage> m_sessionStorage;
    393         mutable RefPtr<Storage> m_localStorage;
    394 #endif
    395 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
    396         mutable RefPtr<DOMApplicationCache> m_applicationCache;
    397 #endif
    398 #if ENABLE(NOTIFICATIONS)
    399         mutable RefPtr<NotificationCenter> m_notifications;
    400 #endif
    401 
    402         EventTargetData m_eventTargetData;
    403     };
    404 
    405 } // namespace WebCore
    406 
    407 #endif // DOMWindow_h
    408