Commit 71781f31 authored by kozyatinskiy's avatar kozyatinskiy Committed by Commit bot

[inspector] handle context destroyed after console.log

BUG=chromium:642496
R=dgozman@chromium.org

Review-Url: https://codereview.chromium.org/2323173004
Cr-Commit-Position: refs/heads/master@{#39322}
parent 8d8cae5f
......@@ -126,6 +126,7 @@ void InjectedScript::getProperties(
std::unique_ptr<Array<PropertyDescriptor>>* properties,
Maybe<protocol::Runtime::ExceptionDetails>* exceptionDetails) {
v8::HandleScope handles(m_context->isolate());
v8::Local<v8::Context> context = m_context->context();
V8FunctionCall function(m_context->inspector(), m_context->context(),
v8Value(), "getProperties");
function.appendArgument(object);
......@@ -145,7 +146,7 @@ void InjectedScript::getProperties(
}
std::unique_ptr<protocol::Value> protocolValue =
toProtocolValue(m_context->context(), resultValue);
toProtocolValue(context, resultValue);
if (hasInternalError(errorString, !protocolValue)) return;
protocol::ErrorSupport errors(errorString);
std::unique_ptr<Array<PropertyDescriptor>> result =
......@@ -172,13 +173,14 @@ std::unique_ptr<protocol::Runtime::RemoteObject> InjectedScript::wrapObject(
bool generatePreview) const {
v8::HandleScope handles(m_context->isolate());
v8::Local<v8::Value> wrappedObject;
v8::Local<v8::Context> context = m_context->context();
if (!wrapValue(errorString, value, groupName, forceValueType, generatePreview)
.ToLocal(&wrappedObject))
return nullptr;
protocol::ErrorSupport errors;
std::unique_ptr<protocol::Runtime::RemoteObject> remoteObject =
protocol::Runtime::RemoteObject::parse(
toProtocolValue(m_context->context(), wrappedObject).get(), &errors);
toProtocolValue(context, wrappedObject).get(), &errors);
if (!remoteObject) *errorString = "Object has too long reference chain";
return remoteObject;
}
......@@ -190,9 +192,9 @@ bool InjectedScript::wrapObjectProperty(ErrorString* errorString,
bool forceValueType,
bool generatePreview) const {
v8::Local<v8::Value> property;
if (hasInternalError(
errorString,
!object->Get(m_context->context(), key).ToLocal(&property)))
v8::Local<v8::Context> context = m_context->context();
if (hasInternalError(errorString,
!object->Get(context, key).ToLocal(&property)))
return false;
v8::Local<v8::Value> wrappedProperty;
if (!wrapValue(errorString, property, groupName, forceValueType,
......@@ -200,7 +202,7 @@ bool InjectedScript::wrapObjectProperty(ErrorString* errorString,
.ToLocal(&wrappedProperty))
return false;
v8::Maybe<bool> success =
createDataProperty(m_context->context(), object, key, wrappedProperty);
createDataProperty(context, object, key, wrappedProperty);
if (hasInternalError(errorString, success.IsNothing() || !success.FromJust()))
return false;
return true;
......@@ -260,8 +262,9 @@ v8::MaybeLocal<v8::Value> InjectedScript::wrapValue(
std::unique_ptr<protocol::Runtime::RemoteObject> InjectedScript::wrapTable(
v8::Local<v8::Value> table, v8::Local<v8::Value> columns) const {
v8::HandleScope handles(m_context->isolate());
V8FunctionCall function(m_context->inspector(), m_context->context(),
v8Value(), "wrapTable");
v8::Local<v8::Context> context = m_context->context();
V8FunctionCall function(m_context->inspector(), context, v8Value(),
"wrapTable");
function.appendArgument(table);
if (columns.IsEmpty())
function.appendArgument(false);
......@@ -272,7 +275,7 @@ std::unique_ptr<protocol::Runtime::RemoteObject> InjectedScript::wrapTable(
if (hadException) return nullptr;
protocol::ErrorSupport errors;
return protocol::Runtime::RemoteObject::parse(
toProtocolValue(m_context->context(), r).get(), &errors);
toProtocolValue(context, r).get(), &errors);
}
bool InjectedScript::findObject(ErrorString* errorString,
......
......@@ -61,16 +61,19 @@ void V8ConsoleAgentImpl::reportAllMessages() {
m_session->inspector()->ensureConsoleMessageStorage(
m_session->contextGroupId());
for (const auto& message : storage->messages()) {
if (message->origin() == V8MessageOrigin::kConsole)
reportMessage(message.get(), false);
if (message->origin() == V8MessageOrigin::kConsole) {
if (!reportMessage(message.get(), false)) return;
}
}
}
void V8ConsoleAgentImpl::reportMessage(V8ConsoleMessage* message,
bool V8ConsoleAgentImpl::reportMessage(V8ConsoleMessage* message,
bool generatePreview) {
DCHECK(message->origin() == V8MessageOrigin::kConsole);
message->reportToFrontend(&m_frontend);
m_frontend.flush();
return m_session->inspector()->hasConsoleMessageStorage(
m_session->contextGroupId());
}
} // namespace v8_inspector
......@@ -35,7 +35,7 @@ class V8ConsoleAgentImpl : public protocol::Console::Backend {
private:
void reportAllMessages();
void reportMessage(V8ConsoleMessage*, bool generatePreview);
bool reportMessage(V8ConsoleMessage*, bool generatePreview);
V8InspectorSessionImpl* m_session;
protocol::DictionaryValue* m_state;
......
......@@ -337,6 +337,11 @@ std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForConsoleAPI(
double timestamp, ConsoleAPIType type,
const std::vector<v8::Local<v8::Value>>& arguments,
std::unique_ptr<V8StackTraceImpl> stackTrace, InspectedContext* context) {
v8::Isolate* isolate = context->isolate();
int contextId = context->contextId();
int contextGroupId = context->contextGroupId();
V8InspectorImpl* inspector = context->inspector();
std::unique_ptr<V8ConsoleMessage> message = wrapUnique(
new V8ConsoleMessage(V8MessageOrigin::kConsole, timestamp, String16()));
if (stackTrace && !stackTrace->isEmpty()) {
......@@ -346,13 +351,12 @@ std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForConsoleAPI(
}
message->m_stackTrace = std::move(stackTrace);
message->m_type = type;
message->m_contextId = context->contextId();
message->m_contextId = contextId;
for (size_t i = 0; i < arguments.size(); ++i)
message->m_arguments.push_back(wrapUnique(
new v8::Global<v8::Value>(context->isolate(), arguments.at(i))));
message->m_arguments.push_back(
wrapUnique(new v8::Global<v8::Value>(isolate, arguments.at(i))));
if (arguments.size())
message->m_message =
V8ValueStringBuilder::toString(arguments[0], context->isolate());
message->m_message = V8ValueStringBuilder::toString(arguments[0], isolate);
V8ConsoleAPIType clientType = V8ConsoleAPIType::kLog;
if (type == ConsoleAPIType::kDebug || type == ConsoleAPIType::kCount ||
......@@ -366,8 +370,8 @@ std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForConsoleAPI(
clientType = V8ConsoleAPIType::kInfo;
else if (type == ConsoleAPIType::kClear)
clientType = V8ConsoleAPIType::kClear;
context->inspector()->client()->consoleAPIMessage(
context->contextGroupId(), clientType, toStringView(message->m_message),
inspector->client()->consoleAPIMessage(
contextGroupId, clientType, toStringView(message->m_message),
toStringView(message->m_url), message->m_lineNumber,
message->m_columnNumber, message->m_stackTrace.get());
......@@ -425,15 +429,18 @@ V8ConsoleMessageStorage::~V8ConsoleMessageStorage() { clear(); }
void V8ConsoleMessageStorage::addMessage(
std::unique_ptr<V8ConsoleMessage> message) {
int contextGroupId = m_contextGroupId;
V8InspectorImpl* inspector = m_inspector;
if (message->type() == ConsoleAPIType::kClear) clear();
V8InspectorSessionImpl* session =
m_inspector->sessionForContextGroup(m_contextGroupId);
inspector->sessionForContextGroup(contextGroupId);
if (session) {
if (message->origin() == V8MessageOrigin::kConsole)
session->consoleAgent()->messageAdded(message.get());
session->runtimeAgent()->messageAdded(message.get());
}
if (!inspector->hasConsoleMessageStorage(contextGroupId)) return;
DCHECK(m_messages.size() <= maxConsoleMessageCount);
if (m_messages.size() == maxConsoleMessageCount) {
......
......@@ -171,6 +171,12 @@ V8ConsoleMessageStorage* V8InspectorImpl::ensureConsoleMessageStorage(
return storageIt->second.get();
}
bool V8InspectorImpl::hasConsoleMessageStorage(int contextGroupId) {
ConsoleStorageMap::iterator storageIt =
m_consoleStorageMap.find(contextGroupId);
return storageIt != m_consoleStorageMap.end();
}
std::unique_ptr<V8StackTrace> V8InspectorImpl::createStackTrace(
v8::Local<v8::StackTrace> stackTrace) {
return m_debugger->createStackTrace(stackTrace);
......
......@@ -109,6 +109,7 @@ class V8InspectorImpl : public V8Inspector {
void muteExceptions(int contextGroupId);
void unmuteExceptions(int contextGroupId);
V8ConsoleMessageStorage* ensureConsoleMessageStorage(int contextGroupId);
bool hasConsoleMessageStorage(int contextGroupId);
using ContextByIdMap =
protocol::HashMap<int, std::unique_ptr<InspectedContext>>;
void discardInspectedContext(int contextGroupId, int contextId);
......
......@@ -658,8 +658,9 @@ void V8RuntimeAgentImpl::enable(ErrorString* errorString) {
m_session->reportAllContexts(this);
V8ConsoleMessageStorage* storage =
m_inspector->ensureConsoleMessageStorage(m_session->contextGroupId());
for (const auto& message : storage->messages())
reportMessage(message.get(), false);
for (const auto& message : storage->messages()) {
if (!reportMessage(message.get(), false)) return;
}
}
void V8RuntimeAgentImpl::disable(ErrorString* errorString) {
......@@ -719,10 +720,11 @@ void V8RuntimeAgentImpl::messageAdded(V8ConsoleMessage* message) {
if (m_enabled) reportMessage(message, true);
}
void V8RuntimeAgentImpl::reportMessage(V8ConsoleMessage* message,
bool V8RuntimeAgentImpl::reportMessage(V8ConsoleMessage* message,
bool generatePreview) {
message->reportToFrontend(&m_frontend, m_session, generatePreview);
m_frontend.flush();
return m_inspector->hasConsoleMessageStorage(m_session->contextGroupId());
}
} // namespace v8_inspector
......@@ -115,7 +115,7 @@ class V8RuntimeAgentImpl : public protocol::Runtime::Backend {
bool enabled() const { return m_enabled; }
private:
void reportMessage(V8ConsoleMessage*, bool generatePreview);
bool reportMessage(V8ConsoleMessage*, bool generatePreview);
V8InspectorSessionImpl* m_session;
protocol::DictionaryValue* m_state;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment