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