- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / ExecutionContext.h
1 /*
2  * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3  * Copyright (C) 2012 Google Inc. All Rights Reserved.
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
28 #ifndef ExecutionContext_h
29 #define ExecutionContext_h
30
31 #include "core/dom/ActiveDOMObject.h"
32 #include "core/dom/ExecutionContextClient.h"
33 #include "core/dom/SandboxFlags.h"
34 #include "core/dom/SecurityContext.h"
35 #include "core/events/ErrorEvent.h"
36 #include "core/fetch/CrossOriginAccessControl.h"
37 #include "core/frame/ConsoleTypes.h"
38 #include "core/frame/DOMTimer.h"
39 #include "platform/LifecycleContext.h"
40 #include "weborigin/KURL.h"
41 #include "wtf/HashSet.h"
42 #include "wtf/OwnPtr.h"
43 #include "wtf/PassOwnPtr.h"
44
45 namespace WTF {
46 class OrdinalNumber;
47 }
48
49 namespace WebCore {
50
51 class ContextLifecycleNotifier;
52 class DOMWindow;
53 class EventListener;
54 class EventQueue;
55 class EventTarget;
56 class ExecutionContextTask;
57 class MessagePort;
58 class PublicURLManager;
59 class SecurityOrigin;
60 class ScriptCallStack;
61 class ScriptState;
62
63 class ExecutionContext : public LifecycleContext<ExecutionContext> {
64 public:
65     ExecutionContext();
66     virtual ~ExecutionContext();
67
68     // Delegating to ExecutionContextClient
69     void setClient(ExecutionContextClient* client) { m_client = client; }
70     bool isDocument() const { return m_client && m_client->isDocument(); }
71     bool isWorkerGlobalScope() { return m_client && m_client->isWorkerGlobalScope(); }
72     bool isJSExecutionForbidden() { return m_client && m_client->isJSExecutionForbidden(); }
73     SecurityOrigin* securityOrigin() const;
74     ContentSecurityPolicy* contentSecurityPolicy() const;
75     const KURL& url() const;
76     KURL completeURL(const String& url) const;
77     void userEventWasHandled();
78     void disableEval(const String& errorMessage);
79     DOMWindow* executingWindow() const;
80     String userAgent(const KURL&) const;
81     void postTask(PassOwnPtr<ExecutionContextTask>);
82     double timerAlignmentInterval() const;
83
84     bool shouldSanitizeScriptError(const String& sourceURL, AccessControlStatus);
85     void reportException(PassRefPtr<ErrorEvent>, PassRefPtr<ScriptCallStack>, AccessControlStatus);
86
87     void addConsoleMessage(MessageSource, MessageLevel, const String& message, const String& sourceURL, unsigned lineNumber);
88     void addConsoleMessage(MessageSource, MessageLevel, const String& message, ScriptState* = 0);
89
90     PublicURLManager& publicURLManager();
91
92     // Active objects are not garbage collected even if inaccessible, e.g. because their activity may result in callbacks being invoked.
93     bool hasPendingActivity();
94
95     void suspendActiveDOMObjects();
96     void resumeActiveDOMObjects();
97     void stopActiveDOMObjects();
98
99     bool activeDOMObjectsAreSuspended() const { return m_activeDOMObjectsAreSuspended; }
100     bool activeDOMObjectsAreStopped() const { return m_activeDOMObjectsAreStopped; }
101     bool isIteratingOverObservers() const;
102
103     // Called after the construction of an ActiveDOMObject to synchronize suspend state.
104     void suspendActiveDOMObjectIfNeeded(ActiveDOMObject*);
105
106     // MessagePort is conceptually a kind of ActiveDOMObject, but it needs to be tracked separately for message dispatch.
107     void processMessagePortMessagesSoon();
108     void dispatchMessagePortEvents();
109     void createdMessagePort(MessagePort*);
110     void destroyedMessagePort(MessagePort*);
111     const HashSet<MessagePort*>& messagePorts() const { return m_messagePorts; }
112
113     void ref() { refExecutionContext(); }
114     void deref() { derefExecutionContext(); }
115
116     // Gets the next id in a circular sequence from 1 to 2^31-1.
117     int circularSequentialID();
118
119     void didChangeTimerAlignmentInterval();
120
121     SandboxFlags sandboxFlags() const { return m_sandboxFlags; }
122     bool isSandboxed(SandboxFlags mask) const { return m_sandboxFlags & mask; }
123     void enforceSandboxFlags(SandboxFlags mask);
124
125     PassOwnPtr<LifecycleNotifier<ExecutionContext> > createLifecycleNotifier();
126
127     virtual EventQueue* eventQueue() const = 0;
128
129 protected:
130
131     ContextLifecycleNotifier& lifecycleNotifier();
132
133 private:
134     friend class DOMTimer; // For installNewTimeout() and removeTimeoutByID() below.
135
136     bool dispatchErrorEvent(PassRefPtr<ErrorEvent>, AccessControlStatus);
137
138     void closeMessagePorts();
139
140     virtual void refExecutionContext() = 0;
141     virtual void derefExecutionContext() = 0;
142     // LifecycleContext implementation.
143
144     // Implementation details for DOMTimer. No other classes should call these functions.
145     int installNewTimeout(PassOwnPtr<ScheduledAction>, int timeout, bool singleShot);
146     void removeTimeoutByID(int timeoutID); // This makes underlying DOMTimer instance destructed.
147
148     ExecutionContextClient* m_client;
149     SandboxFlags m_sandboxFlags;
150     HashSet<MessagePort*> m_messagePorts;
151
152     int m_circularSequentialID;
153     typedef HashMap<int, OwnPtr<DOMTimer> > TimeoutMap;
154     TimeoutMap m_timeouts;
155
156     bool m_inDispatchErrorEvent;
157     class PendingException;
158     OwnPtr<Vector<OwnPtr<PendingException> > > m_pendingExceptions;
159
160     bool m_activeDOMObjectsAreSuspended;
161     bool m_activeDOMObjectsAreStopped;
162
163     OwnPtr<PublicURLManager> m_publicURLManager;
164
165     // The location of this member is important; to make sure contextDestroyed() notification on
166     // ExecutionContext's members (notably m_timeouts) is called before they are destructed,
167     // m_lifecycleNotifer should be placed *after* such members.
168     OwnPtr<ContextLifecycleNotifier> m_lifecycleNotifier;
169 };
170
171 } // namespace WebCore
172
173 #endif // ExecutionContext_h