Chromium Code Reviews| Index: Source/core/inspector/PromiseTracker.cpp |
| diff --git a/Source/core/inspector/PromiseTracker.cpp b/Source/core/inspector/PromiseTracker.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5630e2ba515e16823bfef2a8310c88dd2704ec71 |
| --- /dev/null |
| +++ b/Source/core/inspector/PromiseTracker.cpp |
| @@ -0,0 +1,162 @@ |
| +/* |
| + * Copyright (C) 2014 Google Inc. All rights reserved. |
|
aandrey
2014/08/01 08:28:12
use new
Alexandra Mikhaylova
2014/08/04 14:25:06
Done.
|
| + * |
| + * 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/PromiseTracker.h" |
| + |
| +#include "bindings/core/v8/ScriptCallStackFactory.h" |
| +#include "bindings/core/v8/V8Binding.h" |
| + |
| +namespace blink { |
| + |
| +PromiseTracker::PromiseData::PromiseData(v8::Handle<v8::Object> promise, v8::Handle<v8::Object> parentPromise, int status, v8::Handle<v8::Value> value, v8::Isolate* isolate, PromiseTracker* tracker, bool getStack) |
| + : m_isolate(isolate) |
| + , m_promiseTracker(tracker) |
| + , m_promise(m_isolate, promise) |
| + , m_functionDetails(*(new ScriptCallFrame("", "", "", 0, 0))) |
|
aandrey
2014/08/01 08:28:12
memory leak. just remove
Alexandra Mikhaylova
2014/08/04 14:25:06
Done.
|
| + , m_parentPromise(m_isolate, parentPromise) |
| + , m_status(status) |
| + , m_value(m_isolate, value) |
| + , m_weakPtrFactory(this) |
| +{ |
| + if (getStack) { |
|
aandrey
2014/08/01 08:28:12
captureStack
Alexandra Mikhaylova
2014/08/04 14:25:06
Done.
|
| + RefPtrWillBeRawPtr<ScriptCallStack> stack = createScriptCallStack(1, true); |
|
aandrey
2014/08/01 08:28:12
should create stack on the given isolate
Alexandra Mikhaylova
2014/08/04 14:25:06
Done.
|
| + if (stack->size()) |
| + m_functionDetails = stack->at(0); |
| + } |
| + PromiseDataWrapper* wrapper = new PromiseDataWrapper(m_weakPtrFactory.createWeakPtr()); |
| + m_promise.setWeak(wrapper, &PromiseTracker::PromiseDataWrapper::didRemovePromise); |
| +} |
| + |
| +void PromiseTracker::enable() |
| +{ |
| + m_isEnabled = true; |
| +} |
| + |
| +void PromiseTracker::disable() |
| +{ |
| + m_isEnabled = false; |
| + clear(); |
| +} |
| + |
| +void PromiseTracker::clear() |
| +{ |
| + m_promiseDataMap.clear(); |
| +} |
| + |
| +PromiseTracker::PromiseDataVector* PromiseTracker::getPromiseVector(v8::Handle<v8::Object> promise) |
| +{ |
| + ASSERT(isEnabled()); |
| + |
| + int promiseHash = promise->GetIdentityHash(); |
| + PromiseDataMap::iterator it = m_promiseDataMap.find(promiseHash); |
| + if (it != m_promiseDataMap.end()) |
| + return &it->value; |
| + return &m_promiseDataMap.add(promiseHash, PromiseDataVector()).storedValue->value; |
| +} |
| + |
| +void PromiseTracker::didCreatePromise(ExecutionContext* context, v8::Handle<v8::Object> promise) |
| +{ |
| + ASSERT(isEnabled()); |
| + |
| + PromiseDataVector* vector = getPromiseVector(promise); |
| + v8::Isolate* isolate = toIsolate(context); |
| + vector->append(adoptRef(new PromiseData(promise, v8::Handle<v8::Object>(), 0, v8::Undefined(isolate), isolate, this, true))); |
| +} |
| + |
| +void PromiseTracker::didUpdatePromiseParent(ExecutionContext* context, v8::Handle<v8::Object> promise, v8::Handle<v8::Object> parentPromise) |
| +{ |
| + ASSERT(isEnabled()); |
| + |
| + PromiseDataVector* vector = getPromiseVector(promise); |
| + bool found = false; |
| + for (size_t index = 0; index < vector->size(); ++index) { |
| + RefPtr<PromiseData> data = (*vector)[index]; |
| + if (data->m_promise == promise) { |
| + found = true; |
| + data->m_parentPromise.set(data->m_isolate, parentPromise); |
| + break; |
| + } |
| + } |
| + if (!found) { |
| + v8::Isolate* isolate = toIsolate(context); |
| + vector->append(adoptRef(new PromiseData(promise, parentPromise, 0, v8::Undefined(isolate), isolate, this))); |
| + } |
| +} |
| + |
| +void PromiseTracker::didUpdatePromiseStatus(ExecutionContext* context, v8::Handle<v8::Object> promise, int status, v8::Handle<v8::Value> value) |
| +{ |
| + ASSERT(isEnabled()); |
| + |
| + PromiseDataVector* vector = getPromiseVector(promise); |
| + bool found = false; |
| + for (size_t index = 0; index < vector->size(); ++index) { |
| + RefPtr<PromiseData> data = (*vector)[index]; |
| + if (data->m_promise == promise) { |
| + found = true; |
| + data->m_status = status; |
| + data->m_value.set(data->m_isolate, value); |
| + break; |
| + } |
| + } |
| + if (!found) |
| + vector->append(adoptRef(new PromiseData(promise, v8::Handle<v8::Object>(), status, value, toIsolate(context), this))); |
| +} |
| + |
| +void PromiseTracker::didRemovePromise(v8::Handle<v8::Object> promise) |
| +{ |
| + ASSERT(isEnabled()); |
| + |
| + PromiseDataVector* vector = getPromiseVector(promise); |
| + ASSERT(vector->size() >= 1); |
| + bool found = false; |
| + for (size_t index = 0; index < vector->size(); ++index) { |
| + RefPtr<PromiseData> data = (*vector)[index]; |
| + if (data->m_promise == promise) { |
| + found = true; |
| + vector->remove(index); |
| + break; |
| + } |
| + } |
| + if (vector->size() == 0) |
| + m_promiseDataMap.remove(promise->GetIdentityHash()); |
| +} |
| + |
| +void PromiseTracker::PromiseDataWrapper::didRemovePromise(const v8::WeakCallbackData<v8::Object, PromiseDataWrapper>& data) |
| +{ |
|
aandrey
2014/08/01 08:28:12
memory leak for PromiseDataWrapper. use OwnPtr
Alexandra Mikhaylova
2014/08/04 14:25:06
Thanks, done.
|
| + |
| + PromiseData* promiseData = data.GetParameter()->m_data.get(); |
|
aandrey
2014/08/01 08:28:12
use WeakPtr<PromiseData>
Alexandra Mikhaylova
2014/08/04 14:25:06
Done.
|
| + if (!(promiseData && promiseData->m_promiseTracker->isEnabled())) |
| + return; |
| + |
| + promiseData->m_promiseTracker->didRemovePromise(promiseData->m_promise.newLocal(promiseData->m_isolate)); |
| +} |
| + |
| +} // namespace blink |