Index: Source/core/inspector/AsyncCallStackTracker.cpp |
diff --git a/Source/core/inspector/AsyncCallStackTracker.cpp b/Source/core/inspector/AsyncCallStackTracker.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9ccc506e38e54c56ffc9d4e01decd3eb15bcdd4b |
--- /dev/null |
+++ b/Source/core/inspector/AsyncCallStackTracker.cpp |
@@ -0,0 +1,211 @@ |
+/* |
+ * Copyright (C) 2013 Google Inc. All rights reserved. |
+ * |
+ * Redistribution and use in source and binary forms, with or without |
+ * modification, are permitted provided that the following conditions are |
+ * met: |
+ * |
+ * * Redistributions of source code must retain the above copyright |
+ * notice, this list of conditions and the following disclaimer. |
+ * * Redistributions in binary form must reproduce the above |
+ * copyright notice, this list of conditions and the following disclaimer |
+ * in the documentation and/or other materials provided with the |
+ * distribution. |
+ * * Neither the name of Google Inc. nor the names of its |
+ * contributors may be used to endorse or promote products derived from |
+ * this software without specific prior written permission. |
+ * |
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ */ |
+ |
+#include "config.h" |
+#include "core/inspector/AsyncCallStackTracker.h" |
+ |
+namespace WebCore { |
+ |
+AsyncCallStackTracker::AsyncCallStack::AsyncCallStack(AsyncCallStack* next) |
+ : m_callFrames(ScriptValue()) |
+ , m_next(next) |
+ , m_pathCount(0) |
+{ |
+} |
+ |
+AsyncCallStackTracker::AsyncCallStackIterator::AsyncCallStackIterator(AsyncCallStack* head, AsyncCallStack* tail) |
+ : m_head(head) |
+ , m_tail(tail) |
+{ |
+} |
+ |
+bool AsyncCallStackTracker::AsyncCallStackIterator::hasNext() const |
+{ |
+ return m_head && m_head != m_tail && !m_head->m_callFrames.hasNoValue(); |
+} |
+ |
+ScriptValue AsyncCallStackTracker::AsyncCallStackIterator::next() |
+{ |
+ ASSERT(hasNext()); |
+ ScriptValue result = m_head->m_callFrames; |
+ m_head = m_head->m_next; |
+ if (m_head == m_tail) |
+ m_head = m_tail = 0; |
+ return result; |
+} |
+ |
+AsyncCallStackTracker::AsyncCallStackTracker() |
+ : m_maxAsyncCallStackDepth(0) |
+ , m_currentAsyncCallStackDelta(0) |
+{ |
+} |
+ |
+void AsyncCallStackTracker::setAsyncCallStackDepth(int depth) |
+{ |
+ if (depth <= 0) { |
+ m_maxAsyncCallStackDepth = 0; |
+ clear(); |
+ } else { |
+ m_maxAsyncCallStackDepth = depth; |
+ } |
+} |
+ |
+AsyncCallStackTracker::AsyncCallStackIterator AsyncCallStackTracker::currentAsyncCallStack() |
+{ |
+ RefPtr<AsyncCallStack> head = m_currentAsyncCallStack; |
+ RefPtr<AsyncCallStack> tail = m_currentAsyncCallStack; |
+ for (int i = 0; tail && i < m_maxAsyncCallStackDepth; ++i) |
+ tail = tail->m_next; |
+ return AsyncCallStackTracker::AsyncCallStackIterator(head.get(), tail.get()); |
+} |
+ |
+void AsyncCallStackTracker::didInstallTimer(int timerId, bool singleShot) |
+{ |
+ if (!isEnabled()) |
+ return; |
+ ASSERT(timerId > 0); |
+ m_timerCallStacks.add(timerId, requestAsyncCallStack()); |
+ if (!singleShot) |
+ m_intervalTimerIds.add(timerId); |
+} |
+ |
+void AsyncCallStackTracker::didRemoveTimer(int timerId) |
+{ |
+ if (!isEnabled() || timerId <= 0) |
+ return; |
+ m_intervalTimerIds.remove(timerId); |
+ modifyPathCount(m_timerCallStacks.take(timerId), -1); |
+} |
+ |
+void AsyncCallStackTracker::willFireTimer(int timerId) |
+{ |
+ if (!isEnabled()) |
+ return; |
+ ASSERT(timerId > 0); |
+ ASSERT(!m_currentAsyncCallStack); |
+ if (m_intervalTimerIds.contains(timerId)) |
+ setCurrentAsyncCallStack(m_timerCallStacks.get(timerId), 0); |
+ else |
+ setCurrentAsyncCallStack(m_timerCallStacks.take(timerId), -1); |
+} |
+ |
+void AsyncCallStackTracker::didRequestAnimationFrame(int callbackId) |
+{ |
+ if (!isEnabled()) |
+ return; |
+ ASSERT(callbackId > 0); |
+ m_animationFrameCallStacks.add(callbackId, requestAsyncCallStack()); |
+} |
+ |
+void AsyncCallStackTracker::didCancelAnimationFrame(int callbackId) |
+{ |
+ if (!isEnabled() || callbackId <= 0) |
+ return; |
+ modifyPathCount(m_animationFrameCallStacks.take(callbackId), -1); |
+} |
+ |
+void AsyncCallStackTracker::willFireAnimationFrame(int callbackId) |
+{ |
+ if (!isEnabled()) |
+ return; |
+ ASSERT(callbackId > 0); |
+ ASSERT(!m_currentAsyncCallStack); |
+ setCurrentAsyncCallStack(m_animationFrameCallStacks.take(callbackId), -1); |
+} |
+ |
+void AsyncCallStackTracker::didAsyncCall() |
+{ |
+ if (!isEnabled()) |
+ return; |
+ setCurrentAsyncCallStack(0, 0); |
+} |
+ |
+void AsyncCallStackTracker::didRequestAsyncCallFrames(ScriptValue callFrames) |
+{ |
+ if (!isEnabled()) |
+ return; |
+ ASSERT(m_requestedAsyncCallStack); |
+ if (!m_requestedAsyncCallStack) |
+ return; |
+ m_requestedAsyncCallStack->m_callFrames = callFrames; |
+ m_requestedAsyncCallStack = 0; |
+} |
+ |
+AsyncCallStackTracker::AsyncCallStack* AsyncCallStackTracker::requestAsyncCallStack() |
+{ |
+ ASSERT(isEnabled()); |
+ ASSERT(!m_requestedAsyncCallStack); |
+ m_requestedAsyncCallStack = adoptRef(new AsyncCallStackTracker::AsyncCallStack(m_currentAsyncCallStack.get())); |
+ modifyPathCount(m_requestedAsyncCallStack, 1); |
+ return m_requestedAsyncCallStack.get(); |
+} |
+ |
+void AsyncCallStackTracker::setCurrentAsyncCallStack(PassRefPtr<AsyncCallStack> callStack, int delta) |
+{ |
+ if (m_currentAsyncCallStack) |
+ modifyPathCount(m_currentAsyncCallStack.release(), m_currentAsyncCallStackDelta); |
+ m_currentAsyncCallStack = callStack; |
+ m_currentAsyncCallStackDelta = delta; |
+} |
+ |
+void AsyncCallStackTracker::modifyPathCount(PassRefPtr<AsyncCallStack> head, int delta) |
+{ |
+ if (!head || !delta) |
+ return; |
+ if (head == m_currentAsyncCallStack) { |
+ // Defer modifying the path count. |
yurys
2013/11/28 09:18:16
Why do you need to defer here, can't we simply che
|
+ m_currentAsyncCallStackDelta += delta; |
+ return; |
+ } |
+ int depth = 0; |
+ RefPtr<AsyncCallStack> current = head; |
+ while (current && depth < m_maxAsyncCallStackDepth) { |
+ ++depth; |
+ current->m_pathCount += delta; |
+ if (current->m_pathCount <= 0) { |
yurys
2013/11/28 09:18:16
If m_pathCount == 0 we should dispose of current,
|
+ ASSERT(!current->m_pathCount); |
+ current = current->m_next.release(); |
+ } else { |
+ current = current->m_next; |
+ } |
+ } |
+} |
+ |
+void AsyncCallStackTracker::clear() |
+{ |
+ m_currentAsyncCallStackDelta = 0; |
+ m_currentAsyncCallStack = 0; |
+ m_requestedAsyncCallStack = 0; |
+ m_intervalTimerIds.clear(); |
+ m_timerCallStacks.clear(); |
+ m_animationFrameCallStacks.clear(); |
+} |
+ |
+} // namespace WebCore |