Commit 0a2e2317 authored by dgozman's avatar dgozman Committed by Commit Bot

[inspector] Inline InjectedScriptNative into InjectedScript

This is just a cleanup patch.

BUG=none

Review-Url: https://codereview.chromium.org/2921623006
Cr-Commit-Position: refs/heads/master@{#45669}
parent 631aba17
......@@ -132,8 +132,6 @@ v8_source_set("inspector") {
sources += get_target_outputs(":inspector_injected_script")
sources += get_target_outputs(":inspector_debugger_script")
sources += [
"injected-script-native.cc",
"injected-script-native.h",
"injected-script.cc",
"injected-script.h",
"inspected-context.cc",
......
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/inspector/injected-script-native.h"
namespace v8_inspector {
InjectedScriptNative::InjectedScriptNative(v8::Isolate* isolate)
: m_lastBoundObjectId(1), m_isolate(isolate) {}
static const char privateKeyName[] = "v8-inspector#injectedScript";
InjectedScriptNative::~InjectedScriptNative() {}
void InjectedScriptNative::setOnInjectedScriptHost(
v8::Local<v8::Object> injectedScriptHost) {
v8::HandleScope handleScope(m_isolate);
v8::Local<v8::External> external = v8::External::New(m_isolate, this);
v8::Local<v8::Private> privateKey = v8::Private::ForApi(
m_isolate, v8::String::NewFromUtf8(m_isolate, privateKeyName,
v8::NewStringType::kInternalized)
.ToLocalChecked());
injectedScriptHost->SetPrivate(m_isolate->GetCurrentContext(), privateKey,
external);
}
InjectedScriptNative* InjectedScriptNative::fromInjectedScriptHost(
v8::Isolate* isolate, v8::Local<v8::Object> injectedScriptObject) {
v8::HandleScope handleScope(isolate);
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Private> privateKey = v8::Private::ForApi(
isolate, v8::String::NewFromUtf8(isolate, privateKeyName,
v8::NewStringType::kInternalized)
.ToLocalChecked());
v8::Local<v8::Value> value =
injectedScriptObject->GetPrivate(context, privateKey).ToLocalChecked();
DCHECK(value->IsExternal());
v8::Local<v8::External> external = value.As<v8::External>();
return static_cast<InjectedScriptNative*>(external->Value());
}
int InjectedScriptNative::bind(v8::Local<v8::Value> value,
const String16& groupName) {
if (m_lastBoundObjectId <= 0) m_lastBoundObjectId = 1;
int id = m_lastBoundObjectId++;
m_idToWrappedObject.insert(
std::make_pair(id, v8::Global<v8::Value>(m_isolate, value)));
addObjectToGroup(id, groupName);
return id;
}
void InjectedScriptNative::unbind(int id) {
m_idToWrappedObject.erase(id);
m_idToObjectGroupName.erase(id);
}
v8::Local<v8::Value> InjectedScriptNative::objectForId(int id) {
auto iter = m_idToWrappedObject.find(id);
return iter != m_idToWrappedObject.end() ? iter->second.Get(m_isolate)
: v8::Local<v8::Value>();
}
void InjectedScriptNative::addObjectToGroup(int objectId,
const String16& groupName) {
if (groupName.isEmpty()) return;
if (objectId <= 0) return;
m_idToObjectGroupName[objectId] = groupName;
m_nameToObjectGroup[groupName].push_back(
objectId); // Creates an empty vector if key is not there
}
void InjectedScriptNative::releaseObjectGroup(const String16& groupName) {
if (groupName.isEmpty()) return;
NameToObjectGroup::iterator groupIt = m_nameToObjectGroup.find(groupName);
if (groupIt == m_nameToObjectGroup.end()) return;
for (int id : groupIt->second) unbind(id);
m_nameToObjectGroup.erase(groupIt);
}
String16 InjectedScriptNative::groupName(int objectId) const {
if (objectId <= 0) return String16();
IdToObjectGroupName::const_iterator iterator =
m_idToObjectGroupName.find(objectId);
return iterator != m_idToObjectGroupName.end() ? iterator->second
: String16();
}
} // namespace v8_inspector
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_INSPECTOR_INJECTEDSCRIPTNATIVE_H_
#define V8_INSPECTOR_INJECTEDSCRIPTNATIVE_H_
#include <vector>
#include "src/inspector/protocol/Protocol.h"
#include "include/v8.h"
namespace v8_inspector {
class InjectedScriptNative final {
public:
explicit InjectedScriptNative(v8::Isolate*);
~InjectedScriptNative();
void setOnInjectedScriptHost(v8::Local<v8::Object>);
static InjectedScriptNative* fromInjectedScriptHost(v8::Isolate* isolate,
v8::Local<v8::Object>);
int bind(v8::Local<v8::Value>, const String16& groupName);
void unbind(int id);
v8::Local<v8::Value> objectForId(int id);
void releaseObjectGroup(const String16& groupName);
String16 groupName(int objectId) const;
private:
void addObjectToGroup(int objectId, const String16& groupName);
int m_lastBoundObjectId;
v8::Isolate* m_isolate;
protocol::HashMap<int, v8::Global<v8::Value>> m_idToWrappedObject;
typedef protocol::HashMap<int, String16> IdToObjectGroupName;
IdToObjectGroupName m_idToObjectGroupName;
typedef protocol::HashMap<String16, std::vector<int>> NameToObjectGroup;
NameToObjectGroup m_nameToObjectGroup;
};
} // namespace v8_inspector
#endif // V8_INSPECTOR_INJECTEDSCRIPTNATIVE_H_
......@@ -30,7 +30,6 @@
#include "src/inspector/injected-script.h"
#include "src/inspector/injected-script-native.h"
#include "src/inspector/injected-script-source.h"
#include "src/inspector/inspected-context.h"
#include "src/inspector/protocol/Protocol.h"
......@@ -48,6 +47,10 @@
namespace v8_inspector {
namespace {
static const char privateKeyName[] = "v8-inspector#injectedScript";
}
using protocol::Array;
using protocol::Runtime::PropertyDescriptor;
using protocol::Runtime::InternalPropertyDescriptor;
......@@ -61,12 +64,6 @@ std::unique_ptr<InjectedScript> InjectedScript::create(
v8::Local<v8::Context> context = inspectedContext->context();
v8::Context::Scope scope(context);
std::unique_ptr<InjectedScriptNative> injectedScriptNative(
new InjectedScriptNative(isolate));
v8::Local<v8::Object> scriptHostWrapper =
V8InjectedScriptHost::create(context, inspectedContext->inspector());
injectedScriptNative->setOnInjectedScriptHost(scriptHostWrapper);
// Inject javascript into the context. The compiled script is supposed to
// evaluate into
// a single anonymous function(it's anonymous to avoid cluttering the global
......@@ -87,6 +84,8 @@ std::unique_ptr<InjectedScript> InjectedScript::create(
.ToLocal(&value))
return nullptr;
DCHECK(value->IsFunction());
v8::Local<v8::Object> scriptHostWrapper =
V8InjectedScriptHost::create(context, inspectedContext->inspector());
v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value);
v8::Local<v8::Object> windowGlobal = context->Global();
v8::Local<v8::Value> info[] = {
......@@ -105,17 +104,21 @@ std::unique_ptr<InjectedScript> InjectedScript::create(
if (inspector->getContext(contextGroupId, contextId) != inspectedContext)
return nullptr;
if (!injectedScriptValue->IsObject()) return nullptr;
return std::unique_ptr<InjectedScript>(
new InjectedScript(inspectedContext, injectedScriptValue.As<v8::Object>(),
std::move(injectedScriptNative)));
std::unique_ptr<InjectedScript> injectedScript(new InjectedScript(
inspectedContext, injectedScriptValue.As<v8::Object>()));
v8::Local<v8::Private> privateKey = v8::Private::ForApi(
isolate, v8::String::NewFromUtf8(isolate, privateKeyName,
v8::NewStringType::kInternalized)
.ToLocalChecked());
scriptHostWrapper->SetPrivate(
context, privateKey, v8::External::New(isolate, injectedScript.get()));
return injectedScript;
}
InjectedScript::InjectedScript(
InspectedContext* context, v8::Local<v8::Object> object,
std::unique_ptr<InjectedScriptNative> injectedScriptNative)
: m_context(context),
m_value(context->isolate(), object),
m_native(std::move(injectedScriptNative)) {}
InjectedScript::InjectedScript(InspectedContext* context,
v8::Local<v8::Object> object)
: m_context(context), m_value(context->isolate(), object) {}
InjectedScript::~InjectedScript() {}
......@@ -165,7 +168,7 @@ void InjectedScript::releaseObject(const String16& objectId) {
if (!object) return;
int boundId = 0;
if (!object->getInteger("id", &boundId)) return;
m_native->unbind(boundId);
unbindObject(boundId);
}
Response InjectedScript::wrapObject(
......@@ -266,19 +269,26 @@ std::unique_ptr<protocol::Runtime::RemoteObject> InjectedScript::wrapTable(
Response InjectedScript::findObject(const RemoteObjectId& objectId,
v8::Local<v8::Value>* outObject) const {
*outObject = m_native->objectForId(objectId.id());
if (outObject->IsEmpty())
auto it = m_idToWrappedObject.find(objectId.id());
if (it == m_idToWrappedObject.end())
return Response::Error("Could not find object with given id");
*outObject = it->second.Get(m_context->isolate());
return Response::OK();
}
String16 InjectedScript::objectGroupName(const RemoteObjectId& objectId) const {
return m_native->groupName(objectId.id());
if (objectId.id() <= 0) return String16();
auto it = m_idToObjectGroupName.find(objectId.id());
return it != m_idToObjectGroupName.end() ? it->second : String16();
}
void InjectedScript::releaseObjectGroup(const String16& objectGroup) {
m_native->releaseObjectGroup(objectGroup);
if (objectGroup == "console") m_lastEvaluationResult.Reset();
if (objectGroup.isEmpty()) return;
auto it = m_nameToObjectGroup.find(objectGroup);
if (it == m_nameToObjectGroup.end()) return;
for (int id : it->second) unbindObject(id);
m_nameToObjectGroup.erase(it);
}
void InjectedScript::setCustomObjectFormatterEnabled(bool enabled) {
......@@ -536,4 +546,37 @@ Response InjectedScript::CallFrameScope::findInjectedScript(
return session->findInjectedScript(remoteId.get(), m_injectedScript);
}
InjectedScript* InjectedScript::fromInjectedScriptHost(
v8::Isolate* isolate, v8::Local<v8::Object> injectedScriptObject) {
v8::HandleScope handleScope(isolate);
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Private> privateKey = v8::Private::ForApi(
isolate, v8::String::NewFromUtf8(isolate, privateKeyName,
v8::NewStringType::kInternalized)
.ToLocalChecked());
v8::Local<v8::Value> value =
injectedScriptObject->GetPrivate(context, privateKey).ToLocalChecked();
DCHECK(value->IsExternal());
v8::Local<v8::External> external = value.As<v8::External>();
return static_cast<InjectedScript*>(external->Value());
}
int InjectedScript::bindObject(v8::Local<v8::Value> value,
const String16& groupName) {
if (m_lastBoundObjectId <= 0) m_lastBoundObjectId = 1;
int id = m_lastBoundObjectId++;
m_idToWrappedObject[id].Reset(m_context->isolate(), value);
if (!groupName.isEmpty() && id > 0) {
m_idToObjectGroupName[id] = groupName;
m_nameToObjectGroup[groupName].push_back(id);
}
return id;
}
void InjectedScript::unbindObject(int id) {
m_idToWrappedObject.erase(id);
m_idToObjectGroupName.erase(id);
}
} // namespace v8_inspector
......@@ -31,8 +31,9 @@
#ifndef V8_INSPECTOR_INJECTEDSCRIPT_H_
#define V8_INSPECTOR_INJECTEDSCRIPT_H_
#include <unordered_map>
#include "src/base/macros.h"
#include "src/inspector/injected-script-native.h"
#include "src/inspector/inspected-context.h"
#include "src/inspector/protocol/Forward.h"
#include "src/inspector/protocol/Runtime.h"
......@@ -55,6 +56,8 @@ class InjectedScript final {
public:
static std::unique_ptr<InjectedScript> create(InspectedContext*);
~InjectedScript();
static InjectedScript* fromInjectedScriptHost(v8::Isolate* isolate,
v8::Local<v8::Object>);
InspectedContext* context() const { return m_context; }
......@@ -99,6 +102,8 @@ class InjectedScript final {
Maybe<protocol::Runtime::ExceptionDetails>*);
v8::Local<v8::Value> lastEvaluationResult() const;
int bindObject(v8::Local<v8::Value>, const String16& groupName);
class Scope {
public:
Response initialize();
......@@ -176,19 +181,22 @@ class InjectedScript final {
};
private:
InjectedScript(InspectedContext*, v8::Local<v8::Object>,
std::unique_ptr<InjectedScriptNative>);
InjectedScript(InspectedContext*, v8::Local<v8::Object>);
v8::Local<v8::Value> v8Value() const;
Response wrapValue(v8::Local<v8::Value>, const String16& groupName,
bool forceValueType, bool generatePreview,
v8::Local<v8::Value>* result) const;
v8::Local<v8::Object> commandLineAPI();
void unbindObject(int id);
InspectedContext* m_context;
v8::Global<v8::Value> m_value;
v8::Global<v8::Value> m_lastEvaluationResult;
std::unique_ptr<InjectedScriptNative> m_native;
v8::Global<v8::Object> m_commandLineAPI;
int m_lastBoundObjectId = 1;
std::unordered_map<int, v8::Global<v8::Value>> m_idToWrappedObject;
std::unordered_map<int, String16> m_idToObjectGroupName;
std::unordered_map<String16, std::vector<int>> m_nameToObjectGroup;
DISALLOW_COPY_AND_ASSIGN(InjectedScript);
};
......
......@@ -38,8 +38,6 @@
'../../include/v8-inspector-protocol.h',
'inspector/injected-script.cc',
'inspector/injected-script.h',
'inspector/injected-script-native.cc',
'inspector/injected-script-native.h',
'inspector/inspected-context.cc',
'inspector/inspected-context.h',
'inspector/java-script-call-frame.cc',
......
......@@ -5,7 +5,7 @@
#include "src/inspector/v8-injected-script-host.h"
#include "src/base/macros.h"
#include "src/inspector/injected-script-native.h"
#include "src/inspector/injected-script.h"
#include "src/inspector/string-util.h"
#include "src/inspector/v8-debugger.h"
#include "src/inspector/v8-inspector-impl.h"
......@@ -309,16 +309,15 @@ void V8InjectedScriptHost::objectHasOwnPropertyCallback(
void V8InjectedScriptHost::bindCallback(
const v8::FunctionCallbackInfo<v8::Value>& info) {
if (info.Length() < 2 || !info[1]->IsString()) return;
InjectedScriptNative* injectedScriptNative =
InjectedScriptNative::fromInjectedScriptHost(info.GetIsolate(),
info.Holder());
if (!injectedScriptNative) return;
InjectedScript* injectedScript =
InjectedScript::fromInjectedScriptHost(info.GetIsolate(), info.Holder());
if (!injectedScript) return;
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
v8::Local<v8::String> v8groupName =
info[1]->ToString(context).ToLocalChecked();
String16 groupName = toProtocolStringWithTypeCheck(v8groupName);
int id = injectedScriptNative->bind(info[0], groupName);
int id = injectedScript->bindObject(info[0], groupName);
info.GetReturnValue().Set(id);
}
......
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