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

Unified Diff: mojo/dart/packages/mojo/lib/src/stub.dart

Issue 1983453002: Dart: Refactor Stubs (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Merge Created 4 years, 7 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
Index: mojo/dart/packages/mojo/lib/src/stub.dart
diff --git a/mojo/dart/packages/mojo/lib/src/stub.dart b/mojo/dart/packages/mojo/lib/src/stub.dart
index 2b95ea777ee2a5af60721d3833e2a60658e2b77f..a076533667d15204959653b14a2e9a07b44010f3 100644
--- a/mojo/dart/packages/mojo/lib/src/stub.dart
+++ b/mojo/dart/packages/mojo/lib/src/stub.dart
@@ -4,22 +4,61 @@
part of bindings;
-abstract class Stub extends core.MojoEventHandler {
+/// Generated StubControl classes implement this interface.
+/// StubControl objects are accessible through the [ctrl] field on Stubs.
+abstract class StubControl<T> implements StubMessageHandler {
+ T impl;
+}
+
+/// Generated Stub classes extend this base class.
+class Stub<T> {
+ // In general it's probalby better to avoid adding fields and methods to this
+ // class. Names added to this class have to be mangled by Mojo bindings
+ // generation to avoid name conflicts.
+
+ /// Proxies control the StubMessageHandler by way of this [StubControl]
+ /// object.
+ final StubControl<T> ctrl;
+
+ Stub(this.ctrl);
+
+ /// This is a convenience method that simply forwards to ctrl.close().
+ /// If a Mojo interface has a method 'close', its name will be mangled to be
+ /// 'close_'.
+ Future close({bool immediate: false}) => ctrl.close(immediate: immediate);
+
+ /// This getter and setter pair is for convenience and simply forwards to
+ /// ctrl.impl. If a Mojo interface has a method 'close', its name will be
+ /// mangled to be 'impl_'.
+ T get impl => ctrl.impl;
+ set impl(T impl) {
+ ctrl.impl = impl;
+ }
+}
+
+abstract class StubMessageHandler extends core.MojoEventHandler {
int _outstandingResponseFutures = 0;
bool _isClosing = false;
Completer _closeCompleter;
- Stub.fromEndpoint(core.MojoMessagePipeEndpoint endpoint,
- {bool autoBegin: true})
+ StubMessageHandler.fromEndpoint(core.MojoMessagePipeEndpoint endpoint,
+ {bool autoBegin: true})
: super.fromEndpoint(endpoint, autoBegin: autoBegin);
- Stub.fromHandle(core.MojoHandle handle, {bool autoBegin: true})
+ StubMessageHandler.fromHandle(core.MojoHandle handle, {bool autoBegin: true})
: super.fromHandle(handle, autoBegin: autoBegin);
- Stub.unbound() : super.unbound();
+ StubMessageHandler.unbound() : super.unbound();
+ /// Generated StubControl classes implement this method to route messages to
+ /// the correct implementation method.
dynamic handleMessage(ServiceMessage message);
+ /// Generated StubControl classes implement this getter to return the version
+ /// of the mojom interface for which the bindings are generated.
+ int get version;
+
+ @override
void handleRead() {
var result = endpoint.queryAndRead();
if ((result.data == null) || (result.dataLength == 0)) {
@@ -64,29 +103,7 @@ abstract class Stub extends core.MojoEventHandler {
}
}
- void _sendResponse(Message response) {
- if (isOpen) {
- endpoint.write(
- response.buffer, response.buffer.lengthInBytes, response.handles);
- // FailedPrecondition is only used to indicate that the other end of
- // the pipe has been closed. We can ignore the close here and wait for
- // the PeerClosed signal on the event stream.
- assert((endpoint.status == core.MojoResult.kOk) ||
- (endpoint.status == core.MojoResult.kFailedPrecondition));
- if (_isClosing && (_outstandingResponseFutures == 0)) {
- // This was the final response future for which we needed to send
- // a response. It is safe to close.
- super.close().then((_) {
- if (_isClosing) {
- _isClosing = false;
- _closeCompleter.complete(null);
- _closeCompleter = null;
- }
- });
- }
- }
- }
-
+ @override
void handleWrite() {
throw 'Unexpected write signal in client.';
}
@@ -128,15 +145,37 @@ abstract class Stub extends core.MojoEventHandler {
return response.serializeWithHeader(header);
}
+ @override
String toString() {
var superString = super.toString();
- return "Stub(${superString})";
+ return "StubMessageHandler(${superString})";
}
- int get version;
-
/// Returns a service description, which exposes the mojom type information
/// of the service being stubbed.
/// Note: The description is null or incomplete if type info is unavailable.
service_describer.ServiceDescription get description => null;
+
+ void _sendResponse(Message response) {
+ if (isOpen) {
+ endpoint.write(
+ response.buffer, response.buffer.lengthInBytes, response.handles);
+ // FailedPrecondition is only used to indicate that the other end of
+ // the pipe has been closed. We can ignore the close here and wait for
+ // the PeerClosed signal on the event stream.
+ assert((endpoint.status == core.MojoResult.kOk) ||
+ (endpoint.status == core.MojoResult.kFailedPrecondition));
+ if (_isClosing && (_outstandingResponseFutures == 0)) {
+ // This was the final response future for which we needed to send
+ // a response. It is safe to close.
+ super.close().then((_) {
+ if (_isClosing) {
+ _isClosing = false;
+ _closeCompleter.complete(null);
+ _closeCompleter = null;
+ }
+ });
+ }
+ }
+ }
}
« no previous file with comments | « mojo/dart/packages/mojo/lib/src/proxy.dart ('k') | mojo/dart/packages/mojo_services/lib/activity/activity.mojom.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698