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

Side by Side Diff: sdk/lib/developer/extension.dart

Issue 1282883003: Move service extension handler execution to a timer (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
« no previous file with comments | « runtime/vm/symbols.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 part of dart.developer; 5 part of dart.developer;
6 6
7 class ServiceExtensionResponse { 7 class ServiceExtensionResponse {
8 final String _result; 8 final String _result;
9 final int _errorCode; 9 final int _errorCode;
10 final String _errorDetail; 10 final String _errorDetail;
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 throw new ArgumentError('Extension already registered: $method'); 95 throw new ArgumentError('Extension already registered: $method');
96 } 96 }
97 if (handler is! ServiceExtensionHandler) { 97 if (handler is! ServiceExtensionHandler) {
98 throw new ArgumentError.value(handler, 98 throw new ArgumentError.value(handler,
99 'handler', 99 'handler',
100 'Must be a ServiceExtensionHandler'); 100 'Must be a ServiceExtensionHandler');
101 } 101 }
102 _extensions[method] = handler; 102 _extensions[method] = handler;
103 } 103 }
104 104
105 bool _extensionExists(String method) { 105 bool _scheduleExtension(String method,
106 return _extensions[method] != null; 106 List<String> parameterKeys,
107 } 107 List<String> parameterValues,
108 108 SendPort replyPort,
109 bool _invokeExtension(String method, 109 Object id) {
110 List<String> parameterKeys,
111 List<String> parameterValues,
112 SendPort replyPort,
113 Object id) {
114 ServiceExtensionHandler handler = _extensions[method]; 110 ServiceExtensionHandler handler = _extensions[method];
115 assert(handler != null); 111 if (handler == null) {
116 var parameters = {}; 112 return false;
117 for (var i = 0; i < parameterKeys.length; i++) {
118 parameters[parameterKeys[i]] = parameterValues[i];
119 } 113 }
120 var response; 114 // Defer execution of handler until next event loop.
121 try { 115 Timer.run(() {
122 response = handler(method, parameters); 116 var parameters = {};
123 } catch (e, st) { 117 for (var i = 0; i < parameterKeys.length; i++) {
124 var errorDetails = (st == null) ? '$e' : '$e\n$st'; 118 parameters[parameterKeys[i]] = parameterValues[i];
125 response = new ServiceExtensionResponse.error( 119 }
126 ServiceExtensionResponse.kExtensionError, 120 var response;
127 errorDetails); 121 try {
128 _postResponse(replyPort, id, response); 122 response = handler(method, parameters);
129 return true; 123 } catch (e, st) {
130 } 124 var errorDetails = (st == null) ? '$e' : '$e\n$st';
131 if (response is! Future) {
132 response = new ServiceExtensionResponse.error(
133 ServiceExtensionResponse.kExtensionError,
134 "Extension handler must return a Future");
135 _postResponse(replyPort, id, response);
136 return true;
137 }
138 response.catchError((e, st) {
139 var errorDetails = (st == null) ? '$e' : '$e\n$st';
140 return new ServiceExtensionResponse.error(
141 ServiceExtensionResponse.kExtensionError,
142 errorDetails);
143 }).then((response) {
144 if (response == null) {
145 response = new ServiceExtensionResponse.error( 125 response = new ServiceExtensionResponse.error(
146 ServiceExtensionResponse.kExtensionError, 126 ServiceExtensionResponse.kExtensionError,
147 "Extension handler returned null"); 127 errorDetails);
128 _postResponse(replyPort, id, response);
129 return;
148 } 130 }
149 _postResponse(replyPort, id, response); 131 if (response is! Future) {
132 response = new ServiceExtensionResponse.error(
133 ServiceExtensionResponse.kExtensionError,
134 "Extension handler must return a Future");
135 _postResponse(replyPort, id, response);
136 return;
137 }
138 response.catchError((e, st) {
139 var errorDetails = (st == null) ? '$e' : '$e\n$st';
140 return new ServiceExtensionResponse.error(
141 ServiceExtensionResponse.kExtensionError,
142 errorDetails);
143 }).then((response) {
144 if (response == null) {
145 response = new ServiceExtensionResponse.error(
146 ServiceExtensionResponse.kExtensionError,
147 "Extension handler returned null");
148 }
149 _postResponse(replyPort, id, response);
150 });
150 }); 151 });
151 // Push an event on the event loop so that we invoke the scheduled microtasks.
152 Timer.run(() {});
153 return true; 152 return true;
154 } 153 }
155 154
156 _postResponse(SendPort replyPort, 155 _postResponse(SendPort replyPort,
157 Object id, 156 Object id,
158 ServiceExtensionResponse response) { 157 ServiceExtensionResponse response) {
159 assert(replyPort != null); 158 assert(replyPort != null);
160 if (id == null) { 159 if (id == null) {
161 // No id -> no response. 160 // No id -> no response.
162 // TODO(johnmccutchan): This code and the code in service.cc leave the 161 // TODO(johnmccutchan): This code and the code in service.cc leave the
(...skipping 10 matching lines...) Expand all
173 sb.write('"result":'); 172 sb.write('"result":');
174 } 173 }
175 sb.write('${response._toString()},'); 174 sb.write('${response._toString()},');
176 if (id is String) { 175 if (id is String) {
177 sb.write('"id":"$id"}'); 176 sb.write('"id":"$id"}');
178 } else { 177 } else {
179 sb.write('"id":$id}'); 178 sb.write('"id":$id}');
180 } 179 }
181 replyPort.send(sb.toString()); 180 replyPort.send(sb.toString());
182 } 181 }
OLDNEW
« no previous file with comments | « runtime/vm/symbols.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698