OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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:async'; | 5 import 'dart:async'; |
6 import 'dart:convert'; | 6 import 'dart:convert'; |
7 import 'dart:isolate'; | 7 import 'dart:isolate'; |
8 | 8 |
9 import 'package:analyzer/instrumentation/instrumentation.dart'; | 9 import 'package:analyzer/instrumentation/instrumentation.dart'; |
10 import 'package:analyzer_plugin/channel/channel.dart'; | 10 import 'package:analyzer_plugin/channel/channel.dart'; |
11 import 'package:analyzer_plugin/protocol/protocol.dart'; | 11 import 'package:analyzer_plugin/protocol/protocol.dart'; |
| 12 import 'package:analyzer_plugin/protocol/protocol_generated.dart'; |
12 | 13 |
13 /** | 14 /** |
14 * The object that allows a [ServerPlugin] to receive [Request]s and to return | 15 * The object that allows a [ServerPlugin] to receive [Request]s and to return |
15 * both [Response]s and [Notification]s. It communicates with the analysis | 16 * both [Response]s and [Notification]s. It communicates with the analysis |
16 * server by passing data to the server's main isolate. | 17 * server by passing data to the server's main isolate. |
17 */ | 18 */ |
18 class PluginIsolateChannel implements PluginCommunicationChannel { | 19 class PluginIsolateChannel implements PluginCommunicationChannel { |
19 /** | 20 /** |
20 * The port used to send notifications and responses to the server. | 21 * The port used to send notifications and responses to the server. |
21 */ | 22 */ |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 _errorPort.listen((error) { | 158 _errorPort.listen((error) { |
158 onError(error); | 159 onError(error); |
159 }); | 160 }); |
160 } | 161 } |
161 if (onDone != null) { | 162 if (onDone != null) { |
162 _exitPort = new ReceivePort(); | 163 _exitPort = new ReceivePort(); |
163 _exitPort.listen((_) { | 164 _exitPort.listen((_) { |
164 onDone(); | 165 onDone(); |
165 }); | 166 }); |
166 } | 167 } |
167 _isolate = await Isolate.spawnUri( | 168 try { |
168 pluginUri, <String>[], _receivePort.sendPort, | 169 _isolate = await Isolate.spawnUri( |
169 onError: _errorPort?.sendPort, | 170 pluginUri, <String>[], _receivePort.sendPort, |
170 onExit: _exitPort?.sendPort, | 171 onError: _errorPort?.sendPort, |
171 packageConfig: packagesUri); | 172 onExit: _exitPort?.sendPort, |
| 173 packageConfig: packagesUri); |
| 174 } catch (exception, stackTrace) { |
| 175 instrumentationService.logPluginError( |
| 176 new PluginData(pluginUri.toString(), null, null), |
| 177 RequestErrorCode.PLUGIN_ERROR.toString(), |
| 178 exception.toString(), |
| 179 stackTrace.toString()); |
| 180 if (onDone != null) { |
| 181 onDone(); |
| 182 } |
| 183 close(); |
| 184 return null; |
| 185 } |
172 Completer<Null> channelReady = new Completer<Null>(); | 186 Completer<Null> channelReady = new Completer<Null>(); |
173 _receivePort.listen((dynamic input) { | 187 _receivePort.listen((dynamic input) { |
174 if (input is SendPort) { | 188 if (input is SendPort) { |
175 // print('[server] Received send port'); | |
176 _sendPort = input; | 189 _sendPort = input; |
177 channelReady.complete(null); | 190 channelReady.complete(null); |
178 } else if (input is Map) { | 191 } else if (input is Map) { |
179 if (input.containsKey('id') != null) { | 192 if (input.containsKey('id')) { |
180 String encodedInput = JSON.encode(input); | 193 String encodedInput = JSON.encode(input); |
181 // print('[server] Received response: $encodedInput'); | |
182 instrumentationService.logPluginResponse(pluginUri, encodedInput); | 194 instrumentationService.logPluginResponse(pluginUri, encodedInput); |
183 onResponse(new Response.fromJson(input)); | 195 onResponse(new Response.fromJson(input)); |
184 } else if (input.containsKey('event')) { | 196 } else if (input.containsKey('event')) { |
185 String encodedInput = JSON.encode(input); | 197 String encodedInput = JSON.encode(input); |
186 // print('[server] Received notification: $encodedInput'); | |
187 instrumentationService.logPluginNotification(pluginUri, encodedInput); | 198 instrumentationService.logPluginNotification(pluginUri, encodedInput); |
188 onNotification(new Notification.fromJson(input)); | 199 onNotification(new Notification.fromJson(input)); |
189 } | 200 } |
190 } | 201 } |
191 }); | 202 }); |
192 return channelReady.future; | 203 return channelReady.future; |
193 } | 204 } |
194 | 205 |
195 @override | 206 @override |
196 void sendRequest(Request request) { | 207 void sendRequest(Request request) { |
197 Map<String, Object> json = request.toJson(); | 208 Map<String, Object> json = request.toJson(); |
198 String encodedRequest = JSON.encode(json); | 209 String encodedRequest = JSON.encode(json); |
199 // print('[server] Send request: $encodedRequest'); | 210 // print('[server] Send request: $encodedRequest'); |
200 instrumentationService.logPluginRequest(pluginUri, encodedRequest); | 211 instrumentationService.logPluginRequest(pluginUri, encodedRequest); |
201 _sendPort.send(json); | 212 _sendPort.send(json); |
202 } | 213 } |
203 } | 214 } |
OLD | NEW |