Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/v8_inspector/V8Console.h" | 5 #include "platform/v8_inspector/V8Console.h" |
| 6 | 6 |
| 7 #include "platform/inspector_protocol/String16.h" | 7 #include "platform/inspector_protocol/String16.h" |
| 8 #include "platform/v8_inspector/InjectedScript.h" | 8 #include "platform/v8_inspector/InjectedScript.h" |
| 9 #include "platform/v8_inspector/InspectedContext.h" | 9 #include "platform/v8_inspector/InspectedContext.h" |
| 10 #include "platform/v8_inspector/V8Compat.h" | 10 #include "platform/v8_inspector/V8Compat.h" |
| (...skipping 684 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 695 createBoundFunctionProperty(context, commandLineAPI, "$2", V8Console::inspec tedObject2); | 695 createBoundFunctionProperty(context, commandLineAPI, "$2", V8Console::inspec tedObject2); |
| 696 createBoundFunctionProperty(context, commandLineAPI, "$3", V8Console::inspec tedObject3); | 696 createBoundFunctionProperty(context, commandLineAPI, "$3", V8Console::inspec tedObject3); |
| 697 createBoundFunctionProperty(context, commandLineAPI, "$4", V8Console::inspec tedObject4); | 697 createBoundFunctionProperty(context, commandLineAPI, "$4", V8Console::inspec tedObject4); |
| 698 | 698 |
| 699 inspectedContext->debugger()->client()->installAdditionalCommandLineAPI(cont ext, commandLineAPI); | 699 inspectedContext->debugger()->client()->installAdditionalCommandLineAPI(cont ext, commandLineAPI); |
| 700 | 700 |
| 701 commandLineAPI->SetPrivate(context, inspectedContextPrivateKey(isolate), v8: :External::New(isolate, inspectedContext)); | 701 commandLineAPI->SetPrivate(context, inspectedContextPrivateKey(isolate), v8: :External::New(isolate, inspectedContext)); |
| 702 return commandLineAPI; | 702 return commandLineAPI; |
| 703 } | 703 } |
| 704 | 704 |
| 705 void V8Console::clearInspectedContextIfNeeded(v8::Local<v8::Context> context, v8 ::Local<v8::Object> console) | 705 static void setterCallback(const v8::FunctionCallbackInfo<v8::Value>& info) |
|
dgozman
2016/06/01 01:25:40
Why do we need this indirection?
kozy
2016/06/01 18:49:22
We'd like to make CommandLineAPI methods replaceab
| |
| 706 { | 706 { |
| 707 v8::Isolate* isolate = context->GetIsolate(); | 707 if (!info.Holder()->CreateDataProperty(info.GetIsolate()->GetCurrentContext( ), v8::Local<v8::Name>::Cast(info.Data()), info[0]).FromMaybe(false)) |
| 708 console->SetPrivate(context, inspectedContextPrivateKey(isolate), v8::Extern al::New(isolate, nullptr)); | 708 return; |
| 709 } | 709 } |
| 710 | 710 |
| 711 bool V8Debugger::isCommandLineAPIMethod(const String16& name) | 711 static bool isCommandLineAPIGetter(const String16& name) |
| 712 { | |
| 713 DEFINE_STATIC_LOCAL(protocol::HashSet<String16>, methods, ()); | |
| 714 if (methods.size() == 0) { | |
| 715 const char* members[] = { "dir", "dirxml", "keys", "values", "profile", "profileEnd", "inspect", | |
| 716 "copy", "clear", "debug", "undebug", "monitor", "unmonitor", "table" }; | |
| 717 for (size_t i = 0; i < WTF_ARRAY_LENGTH(members); ++i) | |
| 718 methods.add(members[i]); | |
| 719 } | |
| 720 return methods.find(name) != methods.end(); | |
| 721 } | |
| 722 | |
| 723 bool V8Debugger::isCommandLineAPIGetter(const String16& name) | |
| 724 { | 712 { |
| 725 DEFINE_STATIC_LOCAL(protocol::HashSet<String16>, getters, ()); | 713 DEFINE_STATIC_LOCAL(protocol::HashSet<String16>, getters, ()); |
| 726 if (getters.size() == 0) { | 714 if (getters.size() == 0) { |
| 727 const char* members[] = { "$0", "$1", "$2", "$3", "$4", "$_" }; | 715 const char* members[] = { "$0", "$1", "$2", "$3", "$4", "$_" }; |
| 728 for (size_t i = 0; i < WTF_ARRAY_LENGTH(members); ++i) | 716 for (size_t i = 0; i < WTF_ARRAY_LENGTH(members); ++i) |
| 729 getters.add(members[i]); | 717 getters.add(members[i]); |
| 730 } | 718 } |
| 731 return getters.find(name) != getters.end(); | 719 return getters.find(name) != getters.end(); |
| 732 } | 720 } |
| 733 | 721 |
| 722 v8::Local<v8::Object> V8Console::installCommandLineAPI(v8::Local<v8::Context> co ntext, v8::Local<v8::Object> object, v8::Local<v8::Object> commandLineAPI) | |
| 723 { | |
| 724 v8::Local<v8::Object> installedMethods = v8::Object::New(context->GetIsolate ()); | |
| 725 v8::Local<v8::Array> commandLineAPIMethodNames; | |
| 726 if (!commandLineAPI->GetOwnPropertyNames(context).ToLocal(&commandLineAPIMet hodNames)) | |
|
dgozman
2016/06/01 01:25:40
Perhaps, it makes sense to migrate command line ap
kozy
2016/06/01 18:49:22
I'll make it in follow up.
| |
| 727 return installedMethods; | |
| 728 for (size_t i = 0; i < commandLineAPIMethodNames->Length(); ++i) { | |
| 729 v8::Local<v8::Value> methodName; | |
| 730 if (!commandLineAPIMethodNames->Get(context, i).ToLocal(&methodName) || !methodName->IsName()) | |
| 731 continue; | |
| 732 if (object->Has(context, methodName).FromMaybe(true)) | |
| 733 continue; | |
| 734 v8::Local<v8::Value> method; | |
| 735 if (!commandLineAPI->Get(context, methodName).ToLocal(&method) || !metho d->IsFunction()) | |
| 736 continue; | |
| 737 if (!installedMethods->Set(context, methodName, method).FromMaybe(false) ) | |
| 738 continue; | |
| 739 if (isCommandLineAPIGetter(toProtocolStringWithTypeCheck(methodName))) { | |
| 740 v8::Local<v8::Function> setter; | |
| 741 if (!v8::Function::New(context, setterCallback, methodName).ToLocal( &setter)) | |
| 742 continue; | |
| 743 object->SetAccessorProperty(v8::Local<v8::Name>::Cast(methodName), v 8::Local<v8::Function>::Cast(method), setter, v8::DontEnum); | |
| 744 } else if (!object->DefineOwnProperty(context, v8::Local<v8::Name>::Cast (methodName), method, v8::DontEnum).FromMaybe(false)) { | |
| 745 continue; | |
| 746 } | |
| 747 } | |
| 748 return installedMethods; | |
| 749 } | |
| 750 | |
| 751 void V8Console::clearCommandLineAPI(v8::Local<v8::Context> context, v8::Local<v8 ::Object> object, v8::Local<v8::Object> installedMethods) | |
| 752 { | |
| 753 v8::Local<v8::Array> installedMethodNames; | |
| 754 if (!installedMethods->GetOwnPropertyNames(context).ToLocal(&installedMethod Names)) | |
| 755 return; | |
| 756 for (size_t i = 0; i < installedMethodNames->Length(); ++i) { | |
| 757 v8::Local<v8::Value> methodName; | |
| 758 if (!installedMethodNames->Get(context, i).ToLocal(&methodName) || !meth odName->IsName()) | |
| 759 continue; | |
| 760 if (!object->Has(context, methodName).FromMaybe(false)) | |
| 761 continue; | |
| 762 v8::Local<v8::Value> commandLineAPIValue; | |
| 763 if (!installedMethods->Get(context, methodName).ToLocal(&commandLineAPIV alue)) | |
| 764 continue; | |
| 765 v8::Local<v8::Value> objectMethodValue; | |
| 766 if (isCommandLineAPIGetter(toProtocolStringWithTypeCheck(methodName))) { | |
| 767 v8::Local<v8::Value> propertyDescriptor; | |
| 768 if (!object->GetOwnPropertyDescriptor(context, v8::Local<v8::String> ::Cast(methodName)).ToLocal(&propertyDescriptor) || !propertyDescriptor->IsObjec t()) | |
| 769 continue; | |
| 770 if (!propertyDescriptor.As<v8::Object>()->Get(context, toV8StringInt ernalized(context->GetIsolate(), "get")).ToLocal(&objectMethodValue)) | |
| 771 continue; | |
| 772 } else { | |
| 773 if (!object->Get(context, methodName).ToLocal(&objectMethodValue)) | |
| 774 continue; | |
| 775 } | |
| 776 if (!commandLineAPIValue->Equals(context, objectMethodValue).FromMaybe(f alse)) | |
|
dgozman
2016/06/01 01:25:40
This is kind of scary. What about having a setter
kozy
2016/06/01 18:49:22
Done.
| |
| 777 continue; | |
| 778 if (!object->Delete(context, methodName).FromMaybe(false)) | |
| 779 continue; | |
| 780 } | |
| 781 } | |
| 782 | |
| 783 void V8Console::clearInspectedContextIfNeeded(v8::Local<v8::Context> context, v8 ::Local<v8::Object> console) | |
| 784 { | |
| 785 v8::Isolate* isolate = context->GetIsolate(); | |
| 786 console->SetPrivate(context, inspectedContextPrivateKey(isolate), v8::Extern al::New(isolate, nullptr)); | |
| 787 } | |
| 788 | |
| 734 } // namespace blink | 789 } // namespace blink |
| OLD | NEW |