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

Unified Diff: src/inspector/injected-script.cc

Issue 2921623006: [inspector] Inline InjectedScriptNative into InjectedScript (Closed)
Patch Set: Created 3 years, 7 months 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
« no previous file with comments | « src/inspector/injected-script.h ('k') | src/inspector/injected-script-native.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/inspector/injected-script.cc
diff --git a/src/inspector/injected-script.cc b/src/inspector/injected-script.cc
index e46fbc3872322dcac8552d83d3b768281fa69857..bf267075f04b545da12305b5850d3f65ff2699ae 100644
--- a/src/inspector/injected-script.cc
+++ b/src/inspector/injected-script.cc
@@ -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
« no previous file with comments | « src/inspector/injected-script.h ('k') | src/inspector/injected-script-native.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698