2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef AsyncCallStackTracker_h
32 #define AsyncCallStackTracker_h
34 #include "bindings/core/v8/ScriptValue.h"
35 #include "core/dom/ContextLifecycleObserver.h"
36 #include "wtf/Deque.h"
37 #include "wtf/HashMap.h"
38 #include "wtf/HashSet.h"
39 #include "wtf/Noncopyable.h"
40 #include "wtf/PassRefPtr.h"
41 #include "wtf/RefPtr.h"
48 class ExecutionContext;
49 class ExecutionContextTask;
50 class MutationObserver;
53 class AsyncCallStackTracker final : public NoBaseWillBeGarbageCollectedFinalized<AsyncCallStackTracker> {
54 WTF_MAKE_NONCOPYABLE(AsyncCallStackTracker);
56 class AsyncCallStack final : public RefCountedWillBeGarbageCollectedFinalized<AsyncCallStack> {
58 AsyncCallStack(const String&, const ScriptValue&);
60 void trace(Visitor*) { }
61 String description() const { return m_description; }
62 ScriptValue callFrames() const { return m_callFrames; }
65 ScriptValue m_callFrames;
68 typedef WillBeHeapDeque<RefPtrWillBeMember<AsyncCallStack>, 4> AsyncCallStackVector;
70 class AsyncCallChain final : public RefCountedWillBeGarbageCollected<AsyncCallChain> {
73 AsyncCallChain(const AsyncCallChain& t) : m_callStacks(t.m_callStacks) { }
74 AsyncCallStackVector callStacks() const { return m_callStacks; }
77 friend class AsyncCallStackTracker;
78 AsyncCallStackVector m_callStacks;
81 class ExecutionContextData final : public NoBaseWillBeGarbageCollectedFinalized<ExecutionContextData>, public ContextLifecycleObserver {
82 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
84 ExecutionContextData(AsyncCallStackTracker* tracker, ExecutionContext* executionContext)
85 : ContextLifecycleObserver(executionContext)
86 , m_circularSequentialID(0)
91 virtual void contextDestroyed() override;
93 int circularSequentialID();
98 int m_circularSequentialID;
101 RawPtrWillBeMember<AsyncCallStackTracker> m_tracker;
102 HashSet<int> m_intervalTimerIds;
103 WillBeHeapHashMap<int, RefPtrWillBeMember<AsyncCallChain> > m_timerCallChains;
104 WillBeHeapHashMap<int, RefPtrWillBeMember<AsyncCallChain> > m_animationFrameCallChains;
105 WillBeHeapHashMap<RawPtrWillBeMember<Event>, RefPtrWillBeMember<AsyncCallChain> > m_eventCallChains;
106 WillBeHeapHashMap<RawPtrWillBeMember<EventTarget>, RefPtrWillBeMember<AsyncCallChain> > m_xhrCallChains;
107 WillBeHeapHashMap<RawPtrWillBeMember<MutationObserver>, RefPtrWillBeMember<AsyncCallChain> > m_mutationObserverCallChains;
108 WillBeHeapHashMap<ExecutionContextTask*, RefPtrWillBeMember<AsyncCallChain> > m_executionContextTaskCallChains;
109 WillBeHeapHashMap<String, RefPtrWillBeMember<AsyncCallChain> > m_v8AsyncTaskCallChains;
110 WillBeHeapHashMap<int, RefPtrWillBeMember<AsyncCallChain> > m_asyncOperationCallChains;
113 AsyncCallStackTracker();
115 bool isEnabled() const { return m_maxAsyncCallStackDepth; }
116 void setAsyncCallStackDepth(int);
117 const AsyncCallChain* currentAsyncCallChain() const;
119 void didInstallTimer(ExecutionContext*, int timerId, bool singleShot, const ScriptValue& callFrames);
120 void didRemoveTimer(ExecutionContext*, int timerId);
121 void willFireTimer(ExecutionContext*, int timerId);
123 void didRequestAnimationFrame(ExecutionContext*, int callbackId, const ScriptValue& callFrames);
124 void didCancelAnimationFrame(ExecutionContext*, int callbackId);
125 void willFireAnimationFrame(ExecutionContext*, int callbackId);
127 void didEnqueueEvent(EventTarget*, Event*, const ScriptValue& callFrames);
128 void didRemoveEvent(EventTarget*, Event*);
129 void willHandleEvent(EventTarget*, Event*, EventListener*, bool useCapture);
131 void willLoadXHR(XMLHttpRequest*, const ScriptValue& callFrames);
132 void didLoadXHR(XMLHttpRequest*);
134 void didEnqueueMutationRecord(ExecutionContext*, MutationObserver*, const ScriptValue& callFrames);
135 bool hasEnqueuedMutationRecord(ExecutionContext*, MutationObserver*);
136 void didClearAllMutationRecords(ExecutionContext*, MutationObserver*);
137 void willDeliverMutationRecords(ExecutionContext*, MutationObserver*);
139 void didPostExecutionContextTask(ExecutionContext*, ExecutionContextTask*, const ScriptValue& callFrames);
140 void didKillAllExecutionContextTasks(ExecutionContext*);
141 void willPerformExecutionContextTask(ExecutionContext*, ExecutionContextTask*);
143 void didEnqueueV8AsyncTask(ExecutionContext*, const String& eventName, int id, const ScriptValue& callFrames);
144 void willHandleV8AsyncTask(ExecutionContext*, const String& eventName, int id);
146 int traceAsyncOperationStarting(ExecutionContext*, const String& operationName, const ScriptValue& callFrames);
147 void traceAsyncOperationCompleted(ExecutionContext*, int operationId);
148 void traceAsyncCallbackStarting(ExecutionContext*, int operationId);
150 void didFireAsyncCall();
153 void trace(Visitor*);
156 void willHandleXHREvent(XMLHttpRequest*, Event*);
158 PassRefPtrWillBeRawPtr<AsyncCallChain> createAsyncCallChain(const String& description, const ScriptValue& callFrames);
159 void setCurrentAsyncCallChain(ExecutionContext*, PassRefPtrWillBeRawPtr<AsyncCallChain>);
160 void clearCurrentAsyncCallChain();
161 static void ensureMaxAsyncCallChainDepth(AsyncCallChain*, unsigned);
162 bool validateCallFrames(const ScriptValue& callFrames);
164 ExecutionContextData* createContextDataIfNeeded(ExecutionContext*);
166 unsigned m_maxAsyncCallStackDepth;
167 RefPtrWillBeMember<AsyncCallChain> m_currentAsyncCallChain;
168 unsigned m_nestedAsyncCallCount;
169 typedef WillBeHeapHashMap<RawPtrWillBeMember<ExecutionContext>, OwnPtrWillBeMember<ExecutionContextData> > ExecutionContextDataMap;
170 ExecutionContextDataMap m_executionContextDataMap;
175 #endif // !defined(AsyncCallStackTracker_h)