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

Side by Side Diff: runtime/vm/service.cc

Issue 1270103002: Allow Dart code to register service protocol handlers (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 4 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 (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/service.h" 5 #include "vm/service.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "include/dart_native_api.h" 8 #include "include/dart_native_api.h"
9 #include "platform/globals.h" 9 #include "platform/globals.h"
10 10
(...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 if (handler == NULL) { 578 if (handler == NULL) {
579 handler = FindRootEmbedderHandler(c_method_name); 579 handler = FindRootEmbedderHandler(c_method_name);
580 } 580 }
581 581
582 if (handler != NULL) { 582 if (handler != NULL) {
583 EmbedderHandleMessage(handler, &js); 583 EmbedderHandleMessage(handler, &js);
584 js.PostReply(); 584 js.PostReply();
585 return; 585 return;
586 } 586 }
587 587
588 if (ExtensionHandlerExists(method_name)) {
589 const Array& response =
590 Array::Handle(Array::New(3));
591 InvokeExtensionHandler(method_name, param_keys, param_values, response);
592 PostExtensionResponse(response, &js);
593 js.PostReply();
594 return;
595 }
596
588 PrintUnrecognizedMethodError(&js); 597 PrintUnrecognizedMethodError(&js);
589 js.PostReply(); 598 js.PostReply();
590 return; 599 return;
591 } 600 }
592 } 601 }
593 602
594 603
595 void Service::HandleRootMessage(const Array& msg_instance) { 604 void Service::HandleRootMessage(const Array& msg_instance) {
596 Isolate* isolate = Isolate::Current(); 605 Isolate* isolate = Isolate::Current();
597 InvokeMethod(isolate, msg_instance); 606 InvokeMethod(isolate, msg_instance);
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
862 while (current != NULL) { 871 while (current != NULL) {
863 if (strcmp(name, current->name()) == 0) { 872 if (strcmp(name, current->name()) == 0) {
864 return current; 873 return current;
865 } 874 }
866 current = current->next(); 875 current = current->next();
867 } 876 }
868 return NULL; 877 return NULL;
869 } 878 }
870 879
871 880
881 #define EXTENSION_HANDLER_RESPONSE_LENGTH 3
882
883 bool Service::ExtensionHandlerExists(const String& method) {
884 ASSERT(!method.IsNull());
885 const Library& developer_lib = Library::Handle(Library::DeveloperLibrary());
886 ASSERT(!developer_lib.IsNull());
887 const Function& extension_exists = Function::Handle(
888 developer_lib.LookupLocalFunction(Symbols::_extensionExists()));
889 ASSERT(!extension_exists.IsNull());
890 const Array& arguments = Array::Handle(Array::New(1));
891 ASSERT(!arguments.IsNull());
892 arguments.SetAt(0, method);
893 return (DartEntry::InvokeFunction(extension_exists, arguments) ==
894 Object::bool_true().raw());
895 }
896
897
898 bool Service::InvokeExtensionHandler(const String& method_name,
899 const Array& parameter_keys,
900 const Array& parameter_values,
901 const Array& response) {
902 ASSERT(!method_name.IsNull());
903 ASSERT(!parameter_keys.IsNull());
904 ASSERT(!parameter_values.IsNull());
905 ASSERT(!response.IsNull());
906 ASSERT(response.Length() == EXTENSION_HANDLER_RESPONSE_LENGTH);
907 const Library& developer_lib = Library::Handle(Library::DeveloperLibrary());
908 ASSERT(!developer_lib.IsNull());
909 const Function& invoke_extension = Function::Handle(
910 developer_lib.LookupLocalFunction(Symbols::_invokeExtension()));
911 ASSERT(!invoke_extension.IsNull());
912 const Array& arguments = Array::Handle(Array::New(4));
913 arguments.SetAt(0, method_name);
914 arguments.SetAt(1, parameter_keys);
915 arguments.SetAt(2, parameter_values);
916 arguments.SetAt(3, response);
917 return (DartEntry::InvokeFunction(invoke_extension, arguments) ==
918 Object::bool_true().raw());
919 }
920
921
922 void Service::PostExtensionResponse(const Array& response, JSONStream* js) {
923 ASSERT(!response.IsNull());
924 ASSERT(response.Length() == EXTENSION_HANDLER_RESPONSE_LENGTH);
925 const Object& result = Object::Handle(response.At(0));
926 const Object& error_code = Object::Handle(response.At(1));
927 const Object& error_message = Object::Handle(response.At(2));
928 if (!result.IsNull()) {
929 if (!result.IsString()) {
930 js->PrintError(kInternalError,
931 "Extension error: response[0] must be a String.");
932 return;
933 }
934 TextBuffer* buffer = js->buffer();
935 buffer->AddString(String::Cast(result).ToCString());
936 } else {
937 if (!error_code.IsInteger()) {
938 js->PrintError(kInternalError,
939 "Extension error: response[1] must be an int.");
940 return;
941 }
942 if (!error_message.IsString()) {
943 js->PrintError(kInternalError,
944 "Extension error: response[2] must be a String.");
945 return;
946 }
947 js->PrintError(Integer::Cast(error_code).AsInt64Value(),
948 String::Cast(error_message).ToCString());
949 }
950 }
951
952
872 static const MethodParameter* get_isolate_params[] = { 953 static const MethodParameter* get_isolate_params[] = {
873 ISOLATE_PARAMETER, 954 ISOLATE_PARAMETER,
874 NULL, 955 NULL,
875 }; 956 };
876 957
877 958
878 static bool GetIsolate(Isolate* isolate, JSONStream* js) { 959 static bool GetIsolate(Isolate* isolate, JSONStream* js) {
879 isolate->PrintJSON(js, false); 960 isolate->PrintJSON(js, false);
880 return true; 961 return true;
881 } 962 }
(...skipping 2197 matching lines...) Expand 10 before | Expand all | Expand 10 after
3079 ServiceMethodDescriptor& method = service_methods_[i]; 3160 ServiceMethodDescriptor& method = service_methods_[i];
3080 if (strcmp(method_name, method.name) == 0) { 3161 if (strcmp(method_name, method.name) == 0) {
3081 return &method; 3162 return &method;
3082 } 3163 }
3083 } 3164 }
3084 return NULL; 3165 return NULL;
3085 } 3166 }
3086 3167
3087 3168
3088 } // namespace dart 3169 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/service.h ('k') | runtime/vm/symbols.h » ('j') | sdk/lib/developer/extension.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698