OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:isolate'; | 5 import 'dart:isolate'; |
6 | 6 |
7 patch bool debugger({bool when: true, | 7 patch bool debugger({bool when: true, |
8 String message}) native "Developer_debugger"; | 8 String message}) native "Developer_debugger"; |
9 | 9 |
10 patch Object inspect(Object object) native "Developer_inspect"; | 10 patch Object inspect(Object object) native "Developer_inspect"; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 60 |
61 patch _registerExtension(String method, ServiceExtensionHandler handler) | 61 patch _registerExtension(String method, ServiceExtensionHandler handler) |
62 native "Developer_registerExtension"; | 62 native "Developer_registerExtension"; |
63 | 63 |
64 // This code is only invoked when there is no other Dart code on the stack. | 64 // This code is only invoked when there is no other Dart code on the stack. |
65 _runExtension(ServiceExtensionHandler handler, | 65 _runExtension(ServiceExtensionHandler handler, |
66 String method, | 66 String method, |
67 List<String> parameterKeys, | 67 List<String> parameterKeys, |
68 List<String> parameterValues, | 68 List<String> parameterValues, |
69 SendPort replyPort, | 69 SendPort replyPort, |
70 Object id) { | 70 Object id, |
| 71 bool trace_service) { |
71 var parameters = {}; | 72 var parameters = {}; |
72 for (var i = 0; i < parameterKeys.length; i++) { | 73 for (var i = 0; i < parameterKeys.length; i++) { |
73 parameters[parameterKeys[i]] = parameterValues[i]; | 74 parameters[parameterKeys[i]] = parameterValues[i]; |
74 } | 75 } |
75 var response; | 76 var response; |
76 try { | 77 try { |
77 response = handler(method, parameters); | 78 response = handler(method, parameters); |
78 } catch (e, st) { | 79 } catch (e, st) { |
79 var errorDetails = (st == null) ? '$e' : '$e\n$st'; | 80 var errorDetails = (st == null) ? '$e' : '$e\n$st'; |
80 response = new ServiceExtensionResponse.error( | 81 response = new ServiceExtensionResponse.error( |
81 ServiceExtensionResponse.kExtensionError, | 82 ServiceExtensionResponse.kExtensionError, |
82 errorDetails); | 83 errorDetails); |
83 _postResponse(replyPort, id, response); | 84 _postResponse(replyPort, id, response, trace_service); |
84 return; | 85 return; |
85 } | 86 } |
86 if (response is! Future) { | 87 if (response is! Future) { |
87 response = new ServiceExtensionResponse.error( | 88 response = new ServiceExtensionResponse.error( |
88 ServiceExtensionResponse.kExtensionError, | 89 ServiceExtensionResponse.kExtensionError, |
89 "Extension handler must return a Future"); | 90 "Extension handler must return a Future"); |
90 _postResponse(replyPort, id, response); | 91 _postResponse(replyPort, id, response, trace_service); |
91 return; | 92 return; |
92 } | 93 } |
93 response.catchError((e, st) { | 94 response.catchError((e, st) { |
94 // Catch any errors eagerly and wrap them in a ServiceExtensionResponse. | 95 // Catch any errors eagerly and wrap them in a ServiceExtensionResponse. |
95 var errorDetails = (st == null) ? '$e' : '$e\n$st'; | 96 var errorDetails = (st == null) ? '$e' : '$e\n$st'; |
96 return new ServiceExtensionResponse.error( | 97 return new ServiceExtensionResponse.error( |
97 ServiceExtensionResponse.kExtensionError, | 98 ServiceExtensionResponse.kExtensionError, |
98 errorDetails); | 99 errorDetails); |
99 }).then((response) { | 100 }).then((response) { |
100 // Post the valid response or the wrapped error after verifying that | 101 // Post the valid response or the wrapped error after verifying that |
101 // the response is a ServiceExtensionResponse. | 102 // the response is a ServiceExtensionResponse. |
102 if (response is! ServiceExtensionResponse) { | 103 if (response is! ServiceExtensionResponse) { |
103 response = new ServiceExtensionResponse.error( | 104 response = new ServiceExtensionResponse.error( |
104 ServiceExtensionResponse.kExtensionError, | 105 ServiceExtensionResponse.kExtensionError, |
105 "Extension handler must complete to a ServiceExtensionResponse"); | 106 "Extension handler must complete to a ServiceExtensionResponse"); |
106 } | 107 } |
107 _postResponse(replyPort, id, response); | 108 _postResponse(replyPort, id, response, trace_service); |
108 }).catchError((e, st) { | 109 }).catchError((e, st) { |
109 // We do not expect any errors to occur in the .then or .catchError blocks | 110 // We do not expect any errors to occur in the .then or .catchError blocks |
110 // but, suppress them just in case. | 111 // but, suppress them just in case. |
111 }); | 112 }); |
112 } | 113 } |
113 | 114 |
114 // This code is only invoked by _runExtension. | 115 // This code is only invoked by _runExtension. |
115 _postResponse(SendPort replyPort, | 116 _postResponse(SendPort replyPort, |
116 Object id, | 117 Object id, |
117 ServiceExtensionResponse response) { | 118 ServiceExtensionResponse response, |
| 119 bool trace_service) { |
118 assert(replyPort != null); | 120 assert(replyPort != null); |
119 if (id == null) { | 121 if (id == null) { |
| 122 if (trace_service) { |
| 123 print("vm-service: posting no response for request"); |
| 124 } |
120 // No id -> no response. | 125 // No id -> no response. |
121 replyPort.send(null); | 126 replyPort.send(null); |
122 return; | 127 return; |
123 } | 128 } |
124 assert(id != null); | 129 assert(id != null); |
125 StringBuffer sb = new StringBuffer(); | 130 StringBuffer sb = new StringBuffer(); |
126 sb.write('{"jsonrpc":"2.0",'); | 131 sb.write('{"jsonrpc":"2.0",'); |
127 if (response._isError()) { | 132 if (response._isError()) { |
| 133 if (trace_service) { |
| 134 print("vm-service: posting error response for request $id"); |
| 135 } |
128 sb.write('"error":'); | 136 sb.write('"error":'); |
129 } else { | 137 } else { |
| 138 if (trace_service) { |
| 139 print("vm-service: posting response for request $id"); |
| 140 } |
130 sb.write('"result":'); | 141 sb.write('"result":'); |
131 } | 142 } |
132 sb.write('${response._toString()},'); | 143 sb.write('${response._toString()},'); |
133 if (id is String) { | 144 if (id is String) { |
134 sb.write('"id":"$id"}'); | 145 sb.write('"id":"$id"}'); |
135 } else { | 146 } else { |
136 sb.write('"id":$id}'); | 147 sb.write('"id":$id}'); |
137 } | 148 } |
138 replyPort.send(sb.toString()); | 149 replyPort.send(sb.toString()); |
139 } | 150 } |
OLD | NEW |