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