Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(992)

Unified Diff: Source/core/inspector/AsyncCallStackTracker.cpp

Issue 74063002: DevTools: Support asynchronous call stacks on backend. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: addressed Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..724771b63aae039b15aef02b36e6c8d9bad506da
--- /dev/null
+++ b/Source/core/inspector/AsyncCallStackTracker.cpp
@@ -0,0 +1,191 @@
+/*
+ * 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"
+
+#define DUMP_ASYNC_CALLS_TRACKER_STATS 0
+#if DUMP_ASYNC_CALLS_TRACKER_STATS
+#include "wtf/DataLog.h"
+#endif
+
+namespace WebCore {
+
+typedef AsyncCallStackTracker::AsyncCallTraceIterator AsyncCallTraceIterator;
+
+#if DUMP_ASYNC_CALLS_TRACKER_STATS
+namespace {
+unsigned totalAsyncCallStacks = 0;
+}
+#endif
+
+AsyncCallStackTracker::AsyncCallStack::AsyncCallStack()
+{
+#if DUMP_ASYNC_CALLS_TRACKER_STATS
+ dataLogF("AsyncCallStack::AsyncCallStack() %u\n", ++totalAsyncCallStacks);
+#endif
+}
+
+AsyncCallStackTracker::AsyncCallStack::~AsyncCallStack()
+{
+#if DUMP_ASYNC_CALLS_TRACKER_STATS
+ dataLogF("AsyncCallStack::~AsyncCallStack() %u\n", --totalAsyncCallStacks);
+#endif
+}
+
+bool AsyncCallTraceIterator::hasNext() const
+{
+ return !m_callStacks.isEmpty() && !m_callStacks.first()->m_callFrames.hasNoValue();
+}
+
+ScriptValue AsyncCallTraceIterator::next()
+{
+ ASSERT(hasNext());
+ return m_callStacks.takeFirst()->m_callFrames;
+}
+
+AsyncCallStackTracker::AsyncCallStackTracker()
+ : m_maxAsyncCallStackDepth(DUMP_ASYNC_CALLS_TRACKER_STATS ? 4 : 0)
+{
+}
+
+void AsyncCallStackTracker::setAsyncCallStackDepth(int depth)
+{
+ if (depth <= 0) {
+ m_maxAsyncCallStackDepth = 0;
+ clear();
+ } else {
+ m_maxAsyncCallStackDepth = depth;
+ }
+}
+
+AsyncCallTraceIterator AsyncCallStackTracker::currentAsyncCallStack()
+{
+ return m_currentAsyncCallTrace ? AsyncCallTraceIterator(*m_currentAsyncCallTrace) : AsyncCallTraceIterator();
+}
+
+void AsyncCallStackTracker::didInstallTimer(int timerId, bool singleShot)
+{
+ if (!isEnabled())
+ return;
+ ASSERT(timerId > 0);
+ m_timerCallTraces.set(timerId, createAsyncCallTrace());
+ if (!singleShot)
+ m_intervalTimerIds.add(timerId);
+}
+
+void AsyncCallStackTracker::didRemoveTimer(int timerId)
+{
+ if (!isEnabled() || timerId <= 0)
+ return;
+ m_intervalTimerIds.remove(timerId);
+ m_timerCallTraces.remove(timerId);
+}
+
+void AsyncCallStackTracker::willFireTimer(int timerId)
+{
+ if (!isEnabled())
+ return;
+ ASSERT(timerId > 0);
+ ASSERT(!m_currentAsyncCallTrace);
+ if (m_intervalTimerIds.contains(timerId))
+ m_currentAsyncCallTrace = m_timerCallTraces.get(timerId);
+ else
+ m_currentAsyncCallTrace = m_timerCallTraces.take(timerId);
+}
+
+void AsyncCallStackTracker::didRequestAnimationFrame(int callbackId)
+{
+ if (!isEnabled())
+ return;
+ ASSERT(callbackId > 0);
+ m_animationFrameCallTraces.set(callbackId, createAsyncCallTrace());
+}
+
+void AsyncCallStackTracker::didCancelAnimationFrame(int callbackId)
+{
+ if (!isEnabled() || callbackId <= 0)
+ return;
+ m_animationFrameCallTraces.remove(callbackId);
+}
+
+void AsyncCallStackTracker::willFireAnimationFrame(int callbackId)
+{
+ if (!isEnabled())
+ return;
+ ASSERT(callbackId > 0);
+ ASSERT(!m_currentAsyncCallTrace);
+ m_currentAsyncCallTrace = m_animationFrameCallTraces.take(callbackId);
+}
+
+void AsyncCallStackTracker::didAsyncCall()
+{
+ m_currentAsyncCallTrace = 0;
+}
+
+void AsyncCallStackTracker::didRequestAsyncCallFrames(ScriptValue callFrames)
+{
+ if (!isEnabled())
+ return;
+ ASSERT(m_requestedAsyncCallStack);
+ if (!m_requestedAsyncCallStack)
+ return;
+ m_requestedAsyncCallStack->m_callFrames = callFrames;
+ // FIXME: clean up if callFrames has no value.
+ m_requestedAsyncCallStack = 0;
+}
+
+PassRefPtr<AsyncCallStackTracker::AsyncCallTrace> AsyncCallStackTracker::createAsyncCallTrace()
+{
+ ASSERT(isEnabled());
+ ASSERT(!m_requestedAsyncCallStack);
+ RefPtr<AsyncCallTrace> trace = adoptRef(m_currentAsyncCallTrace ? new AsyncCallStackTracker::AsyncCallTrace(*m_currentAsyncCallTrace) : new AsyncCallStackTracker::AsyncCallTrace());
+ ensureMaxAsyncCallTraceDepth(trace.get(), m_maxAsyncCallStackDepth - 1);
+ m_requestedAsyncCallStack = adoptRef(new AsyncCallStackTracker::AsyncCallStack());
+ trace->m_callStacks.prepend(m_requestedAsyncCallStack);
+ return trace.release();
+}
+
+void AsyncCallStackTracker::ensureMaxAsyncCallTraceDepth(AsyncCallTrace* trace, unsigned maxDepth)
+{
+ while (trace->m_callStacks.size() > maxDepth)
yurys 2013/12/03 13:41:08 Isn't it just 'if'?
aandrey 2013/12/04 12:45:47 The max depth limit can be changed at any time via
+ trace->m_callStacks.removeLast();
+}
+
+void AsyncCallStackTracker::clear()
+{
+ m_requestedAsyncCallStack = 0;
+ m_currentAsyncCallTrace = 0;
+ m_intervalTimerIds.clear();
+ m_timerCallTraces.clear();
+ m_animationFrameCallTraces.clear();
+}
+
+} // namespace WebCore

Powered by Google App Engine
This is Rietveld 408576698