Index: third_party/WebKit/Source/platform/v8_inspector/V8Console.cpp |
diff --git a/third_party/WebKit/Source/platform/v8_inspector/V8Console.cpp b/third_party/WebKit/Source/platform/v8_inspector/V8Console.cpp |
index f94aab113f369a78628a35241ffa83f5217f8208..d10b326d151f67c8b24f229a19c8a2f28911dad6 100644 |
--- a/third_party/WebKit/Source/platform/v8_inspector/V8Console.cpp |
+++ b/third_party/WebKit/Source/platform/v8_inspector/V8Console.cpp |
@@ -667,6 +667,12 @@ v8::Local<v8::Object> V8Console::createConsole(InspectedContext* inspectedContex |
return console; |
} |
+void V8Console::clearInspectedContextIfNeeded(v8::Local<v8::Context> context, v8::Local<v8::Object> console) |
+{ |
+ v8::Isolate* isolate = context->GetIsolate(); |
+ console->SetPrivate(context, inspectedContextPrivateKey(isolate), v8::External::New(isolate, nullptr)); |
+} |
+ |
v8::Local<v8::Object> V8Console::createCommandLineAPI(InspectedContext* inspectedContext) |
{ |
v8::Local<v8::Context> context = inspectedContext->context(); |
@@ -703,33 +709,95 @@ v8::Local<v8::Object> V8Console::createCommandLineAPI(InspectedContext* inspecte |
return commandLineAPI; |
} |
-void V8Console::clearInspectedContextIfNeeded(v8::Local<v8::Context> context, v8::Local<v8::Object> console) |
+static bool isCommandLineAPIGetter(const String16& name) |
{ |
- v8::Isolate* isolate = context->GetIsolate(); |
- console->SetPrivate(context, inspectedContextPrivateKey(isolate), v8::External::New(isolate, nullptr)); |
+ if (name.length() != 2) |
+ return false; |
+ // $0 ... $4, $_ |
+ return name[0] == '$' && ((name[1] >= '0' && name[1] <= '4') || name[1] == '_'); |
+} |
+ |
+void V8Console::CommandLineAPIScope::accessorGetterCallback(v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) |
+{ |
+ CommandLineAPIScope* scope = static_cast<CommandLineAPIScope*>(info.Data().As<v8::External>()->Value()); |
+ DCHECK(scope); |
+ |
+ v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext(); |
+ if (scope->m_cleanup) { |
+ bool removed = info.Holder()->Delete(context, name).FromMaybe(false); |
+ DCHECK(removed); |
+ return; |
+ } |
+ v8::Local<v8::Object> commandLineAPI = scope->m_commandLineAPI; |
+ |
+ v8::Local<v8::Value> value; |
+ if (!commandLineAPI->Get(context, name).ToLocal(&value)) |
+ return; |
+ if (isCommandLineAPIGetter(toProtocolStringWithTypeCheck(name))) { |
+ DCHECK(value->IsFunction()); |
+ v8::MicrotasksScope microtasks(info.GetIsolate(), v8::MicrotasksScope::kDoNotRunMicrotasks); |
+ if (value.As<v8::Function>()->Call(context, commandLineAPI, 0, nullptr).ToLocal(&value)) |
+ info.GetReturnValue().Set(value); |
+ } else { |
+ info.GetReturnValue().Set(value); |
+ } |
} |
-bool V8Debugger::isCommandLineAPIMethod(const String16& name) |
+void V8Console::CommandLineAPIScope::accessorSetterCallback(v8::Local<v8::Name> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info) |
{ |
- DEFINE_STATIC_LOCAL(protocol::HashSet<String16>, methods, ()); |
- if (methods.size() == 0) { |
- const char* members[] = { "dir", "dirxml", "keys", "values", "profile", "profileEnd", "inspect", |
- "copy", "clear", "debug", "undebug", "monitor", "unmonitor", "table" }; |
- for (size_t i = 0; i < PROTOCOL_ARRAY_LENGTH(members); ++i) |
- methods.add(members[i]); |
+ CommandLineAPIScope* scope = static_cast<CommandLineAPIScope*>(info.Data().As<v8::External>()->Value()); |
+ v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext(); |
+ if (!info.Holder()->Delete(context, name).FromMaybe(false)) |
+ return; |
+ if (!info.Holder()->CreateDataProperty(context, name, value).FromMaybe(false)) |
+ return; |
+ bool removed = scope->m_installedMethods->Delete(context, name).FromMaybe(false); |
+ DCHECK(removed); |
+} |
+ |
+V8Console::CommandLineAPIScope::CommandLineAPIScope(v8::Local<v8::Context> context, v8::Local<v8::Object> commandLineAPI, v8::Local<v8::Object> global) |
+ : m_context(context) |
+ , m_commandLineAPI(commandLineAPI) |
+ , m_global(global) |
+ , m_installedMethods(v8::Set::New(context->GetIsolate())) |
+ , m_cleanup(false) |
+{ |
+ v8::Local<v8::Array> names; |
+ if (!m_commandLineAPI->GetOwnPropertyNames(context).ToLocal(&names)) |
+ return; |
+ v8::Local<v8::External> externalThis = v8::External::New(context->GetIsolate(), this); |
+ for (size_t i = 0; i < names->Length(); ++i) { |
+ v8::Local<v8::Value> name; |
+ if (!names->Get(context, i).ToLocal(&name) || !name->IsName()) |
+ continue; |
+ if (m_global->Has(context, name).FromMaybe(true)) |
+ continue; |
+ if (!m_installedMethods->Add(context, name).ToLocal(&m_installedMethods)) |
+ continue; |
+ if (!m_global->SetAccessor(context, v8::Local<v8::Name>::Cast(name), CommandLineAPIScope::accessorGetterCallback, |
+ CommandLineAPIScope::accessorSetterCallback, externalThis, |
+ v8::DEFAULT, v8::DontEnum).FromMaybe(false)) { |
+ bool removed = m_installedMethods->Delete(context, name).FromMaybe(false); |
+ DCHECK(removed); |
+ continue; |
+ } |
} |
- return methods.find(name) != methods.end(); |
} |
-bool V8Debugger::isCommandLineAPIGetter(const String16& name) |
+V8Console::CommandLineAPIScope::~CommandLineAPIScope() |
{ |
- DEFINE_STATIC_LOCAL(protocol::HashSet<String16>, getters, ()); |
- if (getters.size() == 0) { |
- const char* members[] = { "$0", "$1", "$2", "$3", "$4", "$_" }; |
- for (size_t i = 0; i < PROTOCOL_ARRAY_LENGTH(members); ++i) |
- getters.add(members[i]); |
+ m_cleanup = true; |
+ v8::Local<v8::Array> names = m_installedMethods->AsArray(); |
+ for (size_t i = 0; i < names->Length(); ++i) { |
+ v8::Local<v8::Value> name; |
+ if (!names->Get(m_context, i).ToLocal(&name) || !name->IsName()) |
+ continue; |
+ if (name->IsString()) { |
+ v8::Local<v8::Value> descriptor; |
+ bool success = m_global->GetOwnPropertyDescriptor(m_context, v8::Local<v8::String>::Cast(name)).ToLocal(&descriptor); |
+ DCHECK(success); |
+ } |
} |
- return getters.find(name) != getters.end(); |
} |
} // namespace blink |