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

Unified Diff: sdk/lib/developer/extension.dart

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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/developer/developer_sources.gypi ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/developer/extension.dart
diff --git a/sdk/lib/developer/extension.dart b/sdk/lib/developer/extension.dart
new file mode 100644
index 0000000000000000000000000000000000000000..e94e748e7a1770eae0915f77111a62cf6d75768b
--- /dev/null
+++ b/sdk/lib/developer/extension.dart
@@ -0,0 +1,60 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+part of dart.developer;
+
+/// Index of [response] that the result should be stored.
+const kResultIndex = 0;
+/// Index of [response] that the error code should be stored.
+const kErrorCodeIndex = 1;
+/// Index of [response] that the error message be stored.
+const kErrorMessageIndex = 2;
+
+/// A service protocol extension handler. Registered with [registerExtension].
+///
+/// Must modify [response] according to the following contract:
+/// On success, set index 0 to a JSON encoded result
+/// OR
+/// On failure, set index 1 to an error code and index 2 to a error message.
+///
+/// [method] - the method name.
+/// [parameters] - the parameters.
+/// [response] - a list of length three that the handler must set.
+typedef void ServiceExtensionHandler(String method,
+ Map paremeters,
+ List<String> response);
+
+final _extensions = new Map<String, ServiceExtensionHandler>();
+
+/// Register a [ServiceExtensionHandler] that will be invoked in this isolate
+/// for [method].
+void registerExtension(String method, ServiceExtensionHandler handler) {
+ if (_extensions[method] != null) {
+ throw new ArgumentError('Extension already registered: $method.');
+ }
+ if (handler is! ServiceExtensionHandler) {
+ throw new ArgumentError('handler must be a ServiceExtensionHandler.');
+ }
+ _extensions[method] = handler;
+}
+
+bool _extensionExists(String method) {
+ return _extensions[method] != null;
+}
+
+bool _invokeExtension(String method,
turnidge 2015/08/04 18:21:27 As discussed offline, let's make this async.
+ List<String> parameterKeys,
+ List<String> parameterValues,
+ List<String> response) {
+ ServiceExtensionHandler handler = _extensions[method];
+ if (handler == null) {
+ return false;
+ }
+ var parameters = {};
+ for (var i = 0; i < parameterKeys.length; i++) {
+ parameters[parameterKeys[i]] = parameterValues[i];
+ }
+ handler(method, parameters, response);
+ return true;
+}
« no previous file with comments | « sdk/lib/developer/developer_sources.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698