Home | History | Annotate | Download | only in frame
      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/events/EventTarget.h"
     32 #include "core/frame/FrameDestructionObserver.h"
     33 #include "platform/LifecycleContext.h"
     34 #include "platform/Supplementable.h"
     35 
     36 #include "wtf/Forward.h"
     37 
     38 namespace WebCore {
     39     class ApplicationCache;
     40     class BarProp;
     41     class CSSRuleList;
     42     class CSSStyleDeclaration;
     43     class Console;
     44     class DOMPoint;
     45     class DOMSelection;
     46     class DOMURL;
     47     class DOMWindowProperty;
     48     class Database;
     49     class DatabaseCallback;
     50     class Document;
     51     class DocumentInit;
     52     class DOMWindowEventQueue;
     53     class DOMWindowLifecycleNotifier;
     54     class Element;
     55     class EventListener;
     56     class EventQueue;
     57     class ExceptionState;
     58     class FloatRect;
     59     class Frame;
     60     class History;
     61     class IDBFactory;
     62     class Location;
     63     class MediaQueryList;
     64     class MessageEvent;
     65     class Navigator;
     66     class Node;
     67     class Page;
     68     class PageConsole;
     69     class Performance;
     70     class PostMessageTimer;
     71     class RequestAnimationFrameCallback;
     72     class ScheduledAction;
     73     class Screen;
     74     class ScriptCallStack;
     75     class SecurityOrigin;
     76     class SerializedScriptValue;
     77     class Storage;
     78     class StyleMedia;
     79     class DOMWindowCSS;
     80 
     81     struct WindowFeatures;
     82 
     83     typedef Vector<RefPtr<MessagePort>, 1> MessagePortArray;
     84 
     85 enum PageshowEventPersistence {
     86     PageshowEventNotPersisted = 0,
     87     PageshowEventPersisted = 1
     88 };
     89 
     90     enum SetLocationLocking { LockHistoryBasedOnGestureState, LockHistoryAndBackForwardList };
     91 
     92     class DOMWindow : public RefCounted<DOMWindow>, public ScriptWrappable, public EventTargetWithInlineData, public FrameDestructionObserver, public Supplementable<DOMWindow>, public LifecycleContext<DOMWindow> {
     93         REFCOUNTED_EVENT_TARGET(DOMWindow);
     94     public:
     95         static PassRefPtr<Document> createDocument(const String& mimeType, const DocumentInit&, bool forceXHTML);
     96         static PassRefPtr<DOMWindow> create(Frame* frame) { return adoptRef(new DOMWindow(frame)); }
     97         virtual ~DOMWindow();
     98 
     99         PassRefPtr<Document> installNewDocument(const String& mimeType, const DocumentInit&, bool forceXHTML = false);
    100 
    101         virtual const AtomicString& interfaceName() const OVERRIDE;
    102         virtual ExecutionContext* executionContext() const OVERRIDE;
    103 
    104         virtual DOMWindow* toDOMWindow();
    105 
    106         void registerProperty(DOMWindowProperty*);
    107         void unregisterProperty(DOMWindowProperty*);
    108 
    109         void reset();
    110 
    111         PassRefPtr<MediaQueryList> matchMedia(const String&);
    112 
    113         unsigned pendingUnloadEventListeners() const;
    114 
    115         static FloatRect adjustWindowRect(Page*, const FloatRect& pendingChanges);
    116 
    117         bool allowPopUp(); // Call on first window, not target window.
    118         static bool allowPopUp(Frame* firstFrame);
    119         static bool canShowModalDialog(const Frame*);
    120         static bool canShowModalDialogNow(const Frame*);
    121 
    122         // DOM Level 0
    123 
    124         Screen* screen() const;
    125         History* history() const;
    126         BarProp* locationbar() const;
    127         BarProp* menubar() const;
    128         BarProp* personalbar() const;
    129         BarProp* scrollbars() const;
    130         BarProp* statusbar() const;
    131         BarProp* toolbar() const;
    132         Navigator* navigator() const;
    133         Navigator* clientInformation() const { return navigator(); }
    134 
    135         Location* location() const;
    136         void setLocation(const String& location, DOMWindow* activeWindow, DOMWindow* firstWindow,
    137             SetLocationLocking = LockHistoryBasedOnGestureState);
    138 
    139         DOMSelection* getSelection();
    140 
    141         Element* frameElement() const;
    142 
    143         void focus(ExecutionContext* = 0);
    144         void blur();
    145         void close(ExecutionContext* = 0);
    146         void print();
    147         void stop();
    148 
    149         PassRefPtr<DOMWindow> open(const String& urlString, const AtomicString& frameName, const String& windowFeaturesString,
    150             DOMWindow* activeWindow, DOMWindow* firstWindow);
    151 
    152         typedef void (*PrepareDialogFunction)(DOMWindow*, void* context);
    153         void showModalDialog(const String& urlString, const String& dialogFeaturesString,
    154             DOMWindow* activeWindow, DOMWindow* firstWindow, PrepareDialogFunction, void* functionContext);
    155 
    156         void alert(const String& message);
    157         bool confirm(const String& message);
    158         String prompt(const String& message, const String& defaultValue);
    159 
    160         bool find(const String&, bool caseSensitive, bool backwards, bool wrap, bool wholeWord, bool searchInFrames, bool showDialog) const;
    161 
    162         bool offscreenBuffering() const;
    163 
    164         int outerHeight() const;
    165         int outerWidth() const;
    166         int innerHeight() const;
    167         int innerWidth() const;
    168         int screenX() const;
    169         int screenY() const;
    170         int screenLeft() const { return screenX(); }
    171         int screenTop() const { return screenY(); }
    172         int scrollX() const;
    173         int scrollY() const;
    174         int pageXOffset() const { return scrollX(); }
    175         int pageYOffset() const { return scrollY(); }
    176 
    177         bool closed() const;
    178 
    179         unsigned length() const;
    180 
    181         const AtomicString& name() const;
    182         void setName(const AtomicString&);
    183 
    184         String status() const;
    185         void setStatus(const String&);
    186         String defaultStatus() const;
    187         void setDefaultStatus(const String&);
    188 
    189         // Self-referential attributes
    190 
    191         DOMWindow* self() const;
    192         DOMWindow* window() const { return self(); }
    193         DOMWindow* frames() const { return self(); }
    194 
    195         DOMWindow* opener() const;
    196         DOMWindow* parent() const;
    197         DOMWindow* top() const;
    198 
    199         // DOM Level 2 AbstractView Interface
    200 
    201         Document* document() const;
    202 
    203         // CSSOM View Module
    204 
    205         PassRefPtr<StyleMedia> styleMedia() const;
    206 
    207         // DOM Level 2 Style Interface
    208 
    209         PassRefPtr<CSSStyleDeclaration> getComputedStyle(Element*, const String& pseudoElt) const;
    210 
    211         // WebKit extensions
    212 
    213         PassRefPtr<CSSRuleList> getMatchedCSSRules(Element*, const String& pseudoElt, bool authorOnly = true) const;
    214         double devicePixelRatio() const;
    215 
    216         PassRefPtr<DOMPoint> webkitConvertPointFromPageToNode(Node*, const DOMPoint*) const;
    217         PassRefPtr<DOMPoint> webkitConvertPointFromNodeToPage(Node*, const DOMPoint*) const;
    218 
    219         Console* console() const;
    220         PageConsole* pageConsole() const;
    221 
    222         void printErrorMessage(const String&);
    223         String crossDomainAccessErrorMessage(DOMWindow* activeWindow);
    224         String sanitizedCrossDomainAccessErrorMessage(DOMWindow* activeWindow);
    225 
    226         void postMessage(PassRefPtr<SerializedScriptValue> message, const MessagePortArray*, const String& targetOrigin, DOMWindow* source, ExceptionState&);
    227         void postMessageTimerFired(PassOwnPtr<PostMessageTimer>);
    228         void dispatchMessageEventWithOriginCheck(SecurityOrigin* intendedTargetOrigin, PassRefPtr<Event>, PassRefPtr<ScriptCallStack>);
    229 
    230         void scrollBy(int x, int y) const;
    231         void scrollTo(int x, int y) const;
    232         void scroll(int x, int y) const { scrollTo(x, y); }
    233 
    234         void moveBy(float x, float y) const;
    235         void moveTo(float x, float y) const;
    236 
    237         void resizeBy(float x, float y) const;
    238         void resizeTo(float width, float height) const;
    239 
    240         // WebKit animation extensions
    241         int requestAnimationFrame(PassOwnPtr<RequestAnimationFrameCallback>);
    242         int webkitRequestAnimationFrame(PassOwnPtr<RequestAnimationFrameCallback>);
    243         void cancelAnimationFrame(int id);
    244 
    245         DOMWindowCSS* css();
    246 
    247         // Events
    248         // EventTarget API
    249         virtual bool addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture) OVERRIDE;
    250         virtual bool removeEventListener(const AtomicString& eventType, EventListener*, bool useCapture) OVERRIDE;
    251         virtual void removeAllEventListeners() OVERRIDE;
    252 
    253         using EventTarget::dispatchEvent;
    254         bool dispatchEvent(PassRefPtr<Event> prpEvent, PassRefPtr<EventTarget> prpTarget);
    255 
    256         void dispatchLoadEvent();
    257 
    258         DEFINE_ATTRIBUTE_EVENT_LISTENER(animationend);
    259         DEFINE_ATTRIBUTE_EVENT_LISTENER(animationiteration);
    260         DEFINE_ATTRIBUTE_EVENT_LISTENER(animationstart);
    261         DEFINE_ATTRIBUTE_EVENT_LISTENER(search);
    262         DEFINE_ATTRIBUTE_EVENT_LISTENER(transitionend);
    263         DEFINE_ATTRIBUTE_EVENT_LISTENER(wheel);
    264 
    265         DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(webkitanimationstart, webkitAnimationStart);
    266         DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(webkitanimationiteration, webkitAnimationIteration);
    267         DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(webkitanimationend, webkitAnimationEnd);
    268         DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(webkittransitionend, webkitTransitionEnd);
    269 
    270         void captureEvents() { }
    271         void releaseEvents() { }
    272 
    273         void finishedLoading();
    274 
    275         DEFINE_ATTRIBUTE_EVENT_LISTENER(devicemotion);
    276         DEFINE_ATTRIBUTE_EVENT_LISTENER(deviceorientation);
    277 
    278         // HTML 5 key/value storage
    279         Storage* sessionStorage(ExceptionState&) const;
    280         Storage* localStorage(ExceptionState&) const;
    281         Storage* optionalSessionStorage() const { return m_sessionStorage.get(); }
    282         Storage* optionalLocalStorage() const { return m_localStorage.get(); }
    283 
    284         ApplicationCache* applicationCache() const;
    285         ApplicationCache* optionalApplicationCache() const { return m_applicationCache.get(); }
    286 
    287 #if ENABLE(ORIENTATION_EVENTS)
    288         // This is the interface orientation in degrees. Some examples are:
    289         //  0 is straight up; -90 is when the device is rotated 90 clockwise;
    290         //  90 is when rotated counter clockwise.
    291         int orientation() const;
    292 
    293         DEFINE_ATTRIBUTE_EVENT_LISTENER(orientationchange);
    294 #endif
    295 
    296         DEFINE_ATTRIBUTE_EVENT_LISTENER(touchstart);
    297         DEFINE_ATTRIBUTE_EVENT_LISTENER(touchmove);
    298         DEFINE_ATTRIBUTE_EVENT_LISTENER(touchend);
    299         DEFINE_ATTRIBUTE_EVENT_LISTENER(touchcancel);
    300 
    301         Performance* performance() const;
    302 
    303         // FIXME: When this DOMWindow is no longer the active DOMWindow (i.e.,
    304         // when its document is no longer the document that is displayed in its
    305         // frame), we would like to zero out m_frame to avoid being confused
    306         // by the document that is currently active in m_frame.
    307         bool isCurrentlyDisplayedInFrame() const;
    308 
    309         void willDetachDocumentFromFrame();
    310         DOMWindow* anonymousIndexedGetter(uint32_t);
    311 
    312         bool isInsecureScriptAccess(DOMWindow* activeWindow, const String& urlString);
    313 
    314         PassOwnPtr<LifecycleNotifier<DOMWindow> > createLifecycleNotifier();
    315 
    316         EventQueue* eventQueue() const;
    317         void enqueueWindowEvent(PassRefPtr<Event>);
    318         void enqueueDocumentEvent(PassRefPtr<Event>);
    319         void enqueuePageshowEvent(PageshowEventPersistence);
    320         void enqueueHashchangeEvent(const String& oldURL, const String& newURL);
    321         void enqueuePopstateEvent(PassRefPtr<SerializedScriptValue>);
    322         void dispatchWindowLoadEvent();
    323         void documentWasClosed();
    324         void statePopped(PassRefPtr<SerializedScriptValue>);
    325 
    326         // FIXME: This shouldn't be public once DOMWindow becomes ExecutionContext.
    327         void clearEventQueue();
    328 
    329     protected:
    330         DOMWindowLifecycleNotifier& lifecycleNotifier();
    331 
    332     private:
    333         explicit DOMWindow(Frame*);
    334 
    335         Page* page();
    336 
    337         virtual void frameDestroyed() OVERRIDE;
    338         virtual void willDetachPage() OVERRIDE;
    339 
    340         void clearDocument();
    341         void resetDOMWindowProperties();
    342         void willDestroyDocumentInFrame();
    343 
    344         RefPtr<Document> m_document;
    345 
    346         bool m_shouldPrintWhenFinishedLoading;
    347 
    348         HashSet<DOMWindowProperty*> m_properties;
    349 
    350         mutable RefPtr<Screen> m_screen;
    351         mutable RefPtr<History> m_history;
    352         mutable RefPtr<BarProp> m_locationbar;
    353         mutable RefPtr<BarProp> m_menubar;
    354         mutable RefPtr<BarProp> m_personalbar;
    355         mutable RefPtr<BarProp> m_scrollbars;
    356         mutable RefPtr<BarProp> m_statusbar;
    357         mutable RefPtr<BarProp> m_toolbar;
    358         mutable RefPtr<Console> m_console;
    359         mutable RefPtr<Navigator> m_navigator;
    360         mutable RefPtr<Location> m_location;
    361         mutable RefPtr<StyleMedia> m_media;
    362 
    363         String m_status;
    364         String m_defaultStatus;
    365 
    366         mutable RefPtr<Storage> m_sessionStorage;
    367         mutable RefPtr<Storage> m_localStorage;
    368         mutable RefPtr<ApplicationCache> m_applicationCache;
    369 
    370         mutable RefPtr<Performance> m_performance;
    371 
    372         mutable RefPtr<DOMWindowCSS> m_css;
    373 
    374         RefPtr<DOMWindowEventQueue> m_eventQueue;
    375         RefPtr<SerializedScriptValue> m_pendingStateObject;
    376     };
    377 
    378     inline String DOMWindow::status() const
    379     {
    380         return m_status;
    381     }
    382 
    383     inline String DOMWindow::defaultStatus() const
    384     {
    385         return m_defaultStatus;
    386     }
    387 
    388 } // namespace WebCore
    389 
    390 #endif // DOMWindow_h
    391