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

Side by Side Diff: third_party/WebKit/Source/platform/v8_inspector/V8Console.cpp

Issue 1921143002: [DevTools] Align Console implementation with The JSey way (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@move-part-command-line-api-to-native
Patch Set: Created 4 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 unified diff | Download patch
OLDNEW
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/V8DebuggerAgentImpl.h" 10 #include "platform/v8_inspector/V8DebuggerAgentImpl.h"
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 if (flagValue->IsBoolean()) { 232 if (flagValue->IsBoolean()) {
233 ASSERT(flagValue.As<v8::Boolean>()->Value()); 233 ASSERT(flagValue.As<v8::Boolean>()->Value());
234 return true; 234 return true;
235 } 235 }
236 if (!console->SetPrivate(m_context, key, v8::True(m_isolate)).FromMaybe( false)) 236 if (!console->SetPrivate(m_context, key, v8::True(m_isolate)).FromMaybe( false))
237 return defaultValue; 237 return defaultValue;
238 return false; 238 return false;
239 } 239 }
240 }; 240 };
241 241
242 v8::MaybeLocal<v8::Object> createObjectWithClassName(V8DebuggerImpl* debugger, v 8::Local<v8::Context> context, const char* className) 242 void createBoundFunctionProperty(v8::Local<v8::Context> context, v8::Local<v8::O bject> console, const char* name, v8::FunctionCallback callback)
243 {
244 v8::Isolate* isolate = debugger->isolate();
245 v8::Local<v8::FunctionTemplate> functionTemplate;
246 String16 functionTemplateName = String16("V8Console#") + className;
247 if (!debugger->functionTemplate(functionTemplateName).ToLocal(&functionTempl ate)) {
248 functionTemplate = v8::FunctionTemplate::New(isolate);
249 functionTemplate->SetClassName(toV8StringInternalized(isolate, className ));
250 debugger->setFunctionTemplate(functionTemplateName, functionTemplate);
251 }
252 v8::Local<v8::Function> constructor;
253 if (!functionTemplate->GetFunction(context).ToLocal(&constructor))
254 return v8::MaybeLocal<v8::Object>();
255 return constructor->NewInstance(context, 0, nullptr);
256 }
257
258 void createBoundFunctionProperty(v8::Local<v8::Context> context, v8::Local<v8::O bject> obj, v8::Local<v8::Object> prototype, const char* name, v8::FunctionCallb ack callback)
259 { 243 {
260 v8::Local<v8::String> funcName = toV8StringInternalized(context->GetIsolate( ), name); 244 v8::Local<v8::String> funcName = toV8StringInternalized(context->GetIsolate( ), name);
261 v8::Local<v8::Function> func; 245 v8::Local<v8::Function> func;
262 if (!v8::Function::New(context, callback, obj).ToLocal(&func)) 246 if (!v8::Function::New(context, callback, console).ToLocal(&func))
263 return; 247 return;
264 func->SetName(funcName); 248 func->SetName(funcName);
265 if (!prototype->Set(context, funcName, func).FromMaybe(false)) 249 if (!console->Set(context, funcName, func).FromMaybe(false))
266 return; 250 return;
267 } 251 }
268 252
269 } // namespace 253 } // namespace
270 254
271 void V8Console::debugCallback(const v8::FunctionCallbackInfo<v8::Value>& info) 255 void V8Console::debugCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
272 { 256 {
273 ConsoleHelper(info).addMessage(LogMessageType, DebugMessageLevel, false, 0); 257 ConsoleHelper(info).addMessage(LogMessageType, DebugMessageLevel, false, 0);
274 } 258 }
275 259
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
625 if (V8InspectorSessionImpl* session = helper.currentSession()) { 609 if (V8InspectorSessionImpl* session = helper.currentSession()) {
626 V8InspectorSession::Inspectable* object = session->inspectedObject(num); 610 V8InspectorSession::Inspectable* object = session->inspectedObject(num);
627 v8::Isolate* isolate = info.GetIsolate(); 611 v8::Isolate* isolate = info.GetIsolate();
628 if (object) 612 if (object)
629 info.GetReturnValue().Set(object->get(isolate->GetCurrentContext())) ; 613 info.GetReturnValue().Set(object->get(isolate->GetCurrentContext())) ;
630 else 614 else
631 info.GetReturnValue().Set(v8::Undefined(isolate)); 615 info.GetReturnValue().Set(v8::Undefined(isolate));
632 } 616 }
633 } 617 }
634 618
635 v8::MaybeLocal<v8::Object> V8Console::createConsole(InspectedContext* inspectedC ontext, bool hasMemoryAttribute) 619 v8::Local<v8::Object> V8Console::createConsole(InspectedContext* inspectedContex t, bool hasMemoryAttribute)
636 { 620 {
637 v8::Local<v8::Context> context = inspectedContext->context(); 621 v8::Local<v8::Context> context = inspectedContext->context();
638 v8::Isolate* isolate = context->GetIsolate(); 622 v8::Isolate* isolate = context->GetIsolate();
639 v8::MicrotasksScope microtasksScope(isolate, v8::MicrotasksScope::kDoNotRunM icrotasks); 623 v8::MicrotasksScope microtasksScope(isolate, v8::MicrotasksScope::kDoNotRunM icrotasks);
640 624
641 v8::Local<v8::Object> console; 625 v8::Local<v8::Object> console = v8::Object::New(isolate);
642 if (!createObjectWithClassName(inspectedContext->debugger(), context, "Conso le").ToLocal(&console))
643 return v8::MaybeLocal<v8::Object>();
644 626
645 v8::Local<v8::Object> prototype; 627 createBoundFunctionProperty(context, console, "debug", V8Console::debugCallb ack);
646 if (!createObjectWithClassName(inspectedContext->debugger(), context, "Conso lePrototype").ToLocal(&prototype)) 628 createBoundFunctionProperty(context, console, "error", V8Console::errorCallb ack);
647 return v8::MaybeLocal<v8::Object>(); 629 createBoundFunctionProperty(context, console, "info", V8Console::infoCallbac k);
648 630 createBoundFunctionProperty(context, console, "log", V8Console::logCallback) ;
649 createBoundFunctionProperty(context, console, prototype, "debug", V8Console: :debugCallback); 631 createBoundFunctionProperty(context, console, "warn", V8Console::warnCallbac k);
650 createBoundFunctionProperty(context, console, prototype, "error", V8Console: :errorCallback); 632 createBoundFunctionProperty(context, console, "dir", V8Console::dirCallback) ;
651 createBoundFunctionProperty(context, console, prototype, "info", V8Console:: infoCallback); 633 createBoundFunctionProperty(context, console, "dirxml", V8Console::dirxmlCal lback);
652 createBoundFunctionProperty(context, console, prototype, "log", V8Console::l ogCallback); 634 createBoundFunctionProperty(context, console, "table", V8Console::tableCallb ack);
653 createBoundFunctionProperty(context, console, prototype, "warn", V8Console:: warnCallback); 635 createBoundFunctionProperty(context, console, "trace", V8Console::traceCallb ack);
654 createBoundFunctionProperty(context, console, prototype, "dir", V8Console::d irCallback); 636 createBoundFunctionProperty(context, console, "group", V8Console::groupCallb ack);
655 createBoundFunctionProperty(context, console, prototype, "dirxml", V8Console ::dirxmlCallback); 637 createBoundFunctionProperty(context, console, "groupCollapsed", V8Console::g roupCollapsedCallback);
656 createBoundFunctionProperty(context, console, prototype, "table", V8Console: :tableCallback); 638 createBoundFunctionProperty(context, console, "groupEnd", V8Console::groupEn dCallback);
657 createBoundFunctionProperty(context, console, prototype, "trace", V8Console: :traceCallback); 639 createBoundFunctionProperty(context, console, "clear", V8Console::clearCallb ack);
658 createBoundFunctionProperty(context, console, prototype, "group", V8Console: :groupCallback); 640 createBoundFunctionProperty(context, console, "count", V8Console::countCallb ack);
659 createBoundFunctionProperty(context, console, prototype, "groupCollapsed", V 8Console::groupCollapsedCallback); 641 createBoundFunctionProperty(context, console, "assert", V8Console::assertCal lback);
660 createBoundFunctionProperty(context, console, prototype, "groupEnd", V8Conso le::groupEndCallback); 642 createBoundFunctionProperty(context, console, "markTimeline", V8Console::mar kTimelineCallback);
661 createBoundFunctionProperty(context, console, prototype, "clear", V8Console: :clearCallback); 643 createBoundFunctionProperty(context, console, "profile", V8Console::profileC allback);
662 createBoundFunctionProperty(context, console, prototype, "count", V8Console: :countCallback); 644 createBoundFunctionProperty(context, console, "profileEnd", V8Console::profi leEndCallback);
663 createBoundFunctionProperty(context, console, prototype, "assert", V8Console ::assertCallback); 645 createBoundFunctionProperty(context, console, "timeline", V8Console::timelin eCallback);
664 createBoundFunctionProperty(context, console, prototype, "markTimeline", V8C onsole::markTimelineCallback); 646 createBoundFunctionProperty(context, console, "timelineEnd", V8Console::time lineEndCallback);
665 createBoundFunctionProperty(context, console, prototype, "profile", V8Consol e::profileCallback); 647 createBoundFunctionProperty(context, console, "time", V8Console::timeCallbac k);
666 createBoundFunctionProperty(context, console, prototype, "profileEnd", V8Con sole::profileEndCallback); 648 createBoundFunctionProperty(context, console, "timeEnd", V8Console::timeEndC allback);
667 createBoundFunctionProperty(context, console, prototype, "timeline", V8Conso le::timelineCallback); 649 createBoundFunctionProperty(context, console, "timeStamp", V8Console::timeSt ampCallback);
668 createBoundFunctionProperty(context, console, prototype, "timelineEnd", V8Co nsole::timelineEndCallback);
669 createBoundFunctionProperty(context, console, prototype, "time", V8Console:: timeCallback);
670 createBoundFunctionProperty(context, console, prototype, "timeEnd", V8Consol e::timeEndCallback);
671 createBoundFunctionProperty(context, console, prototype, "timeStamp", V8Cons ole::timeStampCallback);
672
673 if (!console->SetPrototype(context, prototype).FromMaybe(false))
674 return v8::MaybeLocal<v8::Object>();
675 650
676 if (hasMemoryAttribute) 651 if (hasMemoryAttribute)
677 console->SetAccessorProperty(toV8StringInternalized(isolate, "memory"), v8::Function::New(isolate, V8Console::memoryGetterCallback, console), v8::Functi on::New(isolate, V8Console::memorySetterCallback), static_cast<v8::PropertyAttri bute>(v8::None), v8::DEFAULT); 652 console->SetAccessorProperty(toV8StringInternalized(isolate, "memory"), v8::Function::New(isolate, V8Console::memoryGetterCallback, console), v8::Functi on::New(isolate, V8Console::memorySetterCallback), static_cast<v8::PropertyAttri bute>(v8::None), v8::DEFAULT);
678 653
679 console->SetPrivate(context, inspectedContextPrivateKey(isolate), v8::Extern al::New(isolate, inspectedContext)); 654 console->SetPrivate(context, inspectedContextPrivateKey(isolate), v8::Extern al::New(isolate, inspectedContext));
680 return console; 655 return console;
681 } 656 }
682 657
683 v8::Local<v8::Object> V8Console::createCommandLineAPI(InspectedContext* inspecte dContext) 658 v8::Local<v8::Object> V8Console::createCommandLineAPI(InspectedContext* inspecte dContext)
684 { 659 {
685 v8::Local<v8::Context> context = inspectedContext->context(); 660 v8::Local<v8::Context> context = inspectedContext->context();
686 v8::Isolate* isolate = context->GetIsolate(); 661 v8::Isolate* isolate = context->GetIsolate();
687 v8::MicrotasksScope microtasksScope(isolate, v8::MicrotasksScope::kDoNotRunM icrotasks); 662 v8::MicrotasksScope microtasksScope(isolate, v8::MicrotasksScope::kDoNotRunM icrotasks);
688 663
689 v8::Local<v8::Object> commandLineAPI = v8::Object::New(isolate); 664 v8::Local<v8::Object> commandLineAPI = v8::Object::New(isolate);
690 665
691 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "dir", V8Console::dirCallback); 666 createBoundFunctionProperty(context, commandLineAPI, "dir", V8Console::dirCa llback);
692 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "dirxml ", V8Console::dirxmlCallback); 667 createBoundFunctionProperty(context, commandLineAPI, "dirxml", V8Console::di rxmlCallback);
693 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "profil e", V8Console::profileCallback); 668 createBoundFunctionProperty(context, commandLineAPI, "profile", V8Console::p rofileCallback);
694 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "profil eEnd", V8Console::profileEndCallback); 669 createBoundFunctionProperty(context, commandLineAPI, "profileEnd", V8Console ::profileEndCallback);
695 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "clear" , V8Console::clearCallback); 670 createBoundFunctionProperty(context, commandLineAPI, "clear", V8Console::cle arCallback);
696 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "table" , V8Console::tableCallback); 671 createBoundFunctionProperty(context, commandLineAPI, "table", V8Console::tab leCallback);
697 672
698 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "keys", V8Console::keysCallback); 673 createBoundFunctionProperty(context, commandLineAPI, "keys", V8Console::keys Callback);
699 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "values ", V8Console::valuesCallback); 674 createBoundFunctionProperty(context, commandLineAPI, "values", V8Console::va luesCallback);
700 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "debug" , V8Console::debugFunctionCallback); 675 createBoundFunctionProperty(context, commandLineAPI, "debug", V8Console::deb ugFunctionCallback);
701 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "undebu g", V8Console::undebugFunctionCallback); 676 createBoundFunctionProperty(context, commandLineAPI, "undebug", V8Console::u ndebugFunctionCallback);
702 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "monito r", V8Console::monitorFunctionCallback); 677 createBoundFunctionProperty(context, commandLineAPI, "monitor", V8Console::m onitorFunctionCallback);
703 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "unmoni tor", V8Console::unmonitorFunctionCallback); 678 createBoundFunctionProperty(context, commandLineAPI, "unmonitor", V8Console: :unmonitorFunctionCallback);
704 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "inspec t", V8Console::inspectCallback); 679 createBoundFunctionProperty(context, commandLineAPI, "inspect", V8Console::i nspectCallback);
705 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "copy", V8Console::copyCallback); 680 createBoundFunctionProperty(context, commandLineAPI, "copy", V8Console::copy Callback);
706 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "$_", V 8Console::lastEvaluationResultCallback); 681 createBoundFunctionProperty(context, commandLineAPI, "$_", V8Console::lastEv aluationResultCallback);
707 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "$0", V 8Console::inspectedObject0); 682 createBoundFunctionProperty(context, commandLineAPI, "$0", V8Console::inspec tedObject0);
708 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "$1", V 8Console::inspectedObject1); 683 createBoundFunctionProperty(context, commandLineAPI, "$1", V8Console::inspec tedObject1);
709 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "$2", V 8Console::inspectedObject2); 684 createBoundFunctionProperty(context, commandLineAPI, "$2", V8Console::inspec tedObject2);
710 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "$3", V 8Console::inspectedObject3); 685 createBoundFunctionProperty(context, commandLineAPI, "$3", V8Console::inspec tedObject3);
711 createBoundFunctionProperty(context, commandLineAPI, commandLineAPI, "$4", V 8Console::inspectedObject4); 686 createBoundFunctionProperty(context, commandLineAPI, "$4", V8Console::inspec tedObject4);
712 687
713 commandLineAPI->SetPrivate(context, inspectedContextPrivateKey(isolate), v8: :External::New(isolate, inspectedContext)); 688 commandLineAPI->SetPrivate(context, inspectedContextPrivateKey(isolate), v8: :External::New(isolate, inspectedContext));
714 return commandLineAPI; 689 return commandLineAPI;
715 } 690 }
716 691
717 void V8Console::clearInspectedContextIfNeeded(v8::Local<v8::Context> context, v8 ::Local<v8::Object> console) 692 void V8Console::clearInspectedContextIfNeeded(v8::Local<v8::Context> context, v8 ::Local<v8::Object> console)
718 { 693 {
719 v8::Isolate* isolate = context->GetIsolate(); 694 v8::Isolate* isolate = context->GetIsolate();
720 console->SetPrivate(context, inspectedContextPrivateKey(isolate), v8::Extern al::New(isolate, nullptr)); 695 console->SetPrivate(context, inspectedContextPrivateKey(isolate), v8::Extern al::New(isolate, nullptr));
721 } 696 }
(...skipping 16 matching lines...) Expand all
738 DEFINE_STATIC_LOCAL(protocol::HashSet<String16>, getters, ()); 713 DEFINE_STATIC_LOCAL(protocol::HashSet<String16>, getters, ());
739 if (getters.size() == 0) { 714 if (getters.size() == 0) {
740 const char* members[] = { "$0", "$1", "$2", "$3", "$4", "$_" }; 715 const char* members[] = { "$0", "$1", "$2", "$3", "$4", "$_" };
741 for (size_t i = 0; i < WTF_ARRAY_LENGTH(members); ++i) 716 for (size_t i = 0; i < WTF_ARRAY_LENGTH(members); ++i)
742 getters.add(members[i]); 717 getters.add(members[i]);
743 } 718 }
744 return getters.find(name) != getters.end(); 719 return getters.find(name) != getters.end();
745 } 720 }
746 721
747 } // namespace blink 722 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698