OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 part of application; | 5 part of application; |
6 | 6 |
7 typedef core.Listener ServiceFactory(core.MojoMessagePipeEndpoint endpoint); | 7 typedef Object ServiceFactory(core.MojoMessagePipeEndpoint endpoint); |
8 typedef void FallbackServiceFactory(String interfaceName, | 8 typedef void FallbackServiceFactory(String interfaceName, |
9 core.MojoMessagePipeEndpoint endpoint); | 9 core.MojoMessagePipeEndpoint endpoint); |
10 | 10 |
11 | 11 |
12 class LocalServiceProvider extends ServiceProvider { | 12 class LocalServiceProvider implements ServiceProvider { |
13 final ApplicationConnection connection; | 13 final ApplicationConnection connection; |
| 14 ServiceProviderStub _stub; |
14 | 15 |
15 LocalServiceProvider(ApplicationConnection this.connection, | 16 LocalServiceProvider(this.connection, this._stub) { |
16 ServiceProviderStub stub) | 17 _stub.delegate = this; |
17 : super.fromStub(stub) { | |
18 delegate = this; | |
19 } | 18 } |
20 | 19 |
| 20 listen() => _stub.listen(); |
| 21 |
| 22 void close({bool nodefer : false}) => _stub.close(nodefer: nodefer); |
| 23 |
21 void connectToService(String interfaceName, | 24 void connectToService(String interfaceName, |
22 core.MojoMessagePipeEndpoint pipe) { | 25 core.MojoMessagePipeEndpoint pipe) { |
23 if (connection._nameToServiceFactory.containsKey(interfaceName)) { | 26 if (connection._nameToServiceFactory.containsKey(interfaceName)) { |
24 var listener = connection._nameToServiceFactory[interfaceName](pipe); | 27 connection._nameToServiceFactory[interfaceName](pipe); |
25 if (listener != null) { | 28 return; |
26 listener.listen(); | |
27 return; | |
28 } | |
29 } | 29 } |
30 if (connection.fallbackServiceFactory != null) { | 30 if (connection.fallbackServiceFactory != null) { |
31 connection.fallbackServiceFactory(interfaceName, pipe); | 31 connection.fallbackServiceFactory(interfaceName, pipe); |
32 return; | 32 return; |
33 } | 33 } |
34 // The specified interface isn't provided. Close the pipe so the | 34 // The specified interface isn't provided. Close the pipe so the |
35 // remote endpoint sees that we don't support this interface. | 35 // remote endpoint sees that we don't support this interface. |
36 pipe.handle.close(); | 36 pipe.handle.close(); |
37 } | 37 } |
38 } | 38 } |
39 | 39 |
40 // Encapsulates a pair of ServiceProviders that enable services (interface | 40 // Encapsulates a pair of ServiceProviders that enable services (interface |
41 // implementations) to be provided to a remote application as well as exposing | 41 // implementations) to be provided to a remote application as well as exposing |
42 // services provided by a remote application. ApplicationConnection | 42 // services provided by a remote application. ApplicationConnection |
43 // objects are returned by the Application ConnectToApplication() method | 43 // objects are returned by the Application ConnectToApplication() method |
44 // and they're passed to the Application AcceptConnection() method. | 44 // and they're passed to the Application AcceptConnection() method. |
45 // | 45 // |
46 // To request a service from the remote application: | 46 // To request a service from the remote application: |
47 // var proxy = applicationConnection.requestService(ViewManagerClient.name); | 47 // var proxy = applicationConnection.requestService(ViewManagerClientName); |
48 // | 48 // |
49 // To provide a service to the remote application, specify a function that | 49 // To provide a service to the remote application, specify a function that |
50 // returns a service. For example: | 50 // returns a service. For example: |
51 // applicationConnection.provideService(ViewManagerClient.name, (pipe) => | 51 // applicationConnection.provideService(ViewManagerClientName, (pipe) => |
52 // new ViewManagerClientImpl(pipe)); | 52 // new ViewManagerClientImpl(pipe)); |
53 // | 53 // |
54 // To handle requests for any interface, set fallbackServiceFactory to a | 54 // To handle requests for any interface, set fallbackServiceFactory to a |
55 // function that takes ownership of the incoming message pipe endpoint. If the | 55 // function that takes ownership of the incoming message pipe endpoint. If the |
56 // fallbackServiceFactory function doesn't bind the pipe, it should close it. | 56 // fallbackServiceFactory function doesn't bind the pipe, it should close it. |
57 // | 57 // |
58 // The fallbackServiceFactory is only used if a service wasn't specified | 58 // The fallbackServiceFactory is only used if a service wasn't specified |
59 // with provideService(). | 59 // with provideService(). |
60 | 60 |
61 class ApplicationConnection { | 61 class ApplicationConnection { |
62 ServiceProviderProxy remoteServiceProvider; | 62 ServiceProviderProxy remoteServiceProvider; |
63 LocalServiceProvider _localServiceProvider; | 63 LocalServiceProvider _localServiceProvider; |
64 final _nameToServiceFactory = new Map<String, ServiceFactory>(); | 64 final _nameToServiceFactory = new Map<String, ServiceFactory>(); |
65 FallbackServiceFactory fallbackServiceFactory; | 65 FallbackServiceFactory _fallbackServiceFactory; |
66 | 66 |
67 ApplicationConnection(ServiceProviderStub stub, ServiceProviderProxy proxy) | 67 ApplicationConnection(ServiceProviderStub stub, ServiceProviderProxy proxy) |
68 : remoteServiceProvider = proxy { | 68 : remoteServiceProvider = proxy { |
69 if (stub != null) _localServiceProvider = | 69 if (stub != null) _localServiceProvider = |
70 new LocalServiceProvider(this, stub); | 70 new LocalServiceProvider(this, stub); |
71 } | 71 } |
72 | 72 |
73 bindings.Proxy requestService(bindings.Proxy proxy) { | 73 FallbackServiceFactory get fallbackServiceFactory => _fallbackServiceFactory; |
74 assert(!proxy.isBound && | 74 set fallbackServiceFactory(FallbackServiceFactory f) { |
| 75 assert(_localServiceProvider != null); |
| 76 _fallbackServiceFactory = f; |
| 77 } |
| 78 |
| 79 bindings.ProxyBase requestService(bindings.ProxyBase proxy) { |
| 80 assert(!proxy.impl.isBound && |
75 (remoteServiceProvider != null) && | 81 (remoteServiceProvider != null) && |
76 remoteServiceProvider.isBound); | 82 remoteServiceProvider.impl.isBound); |
77 var pipe = new core.MojoMessagePipe(); | 83 var pipe = new core.MojoMessagePipe(); |
78 proxy.bind(pipe.endpoints[0]); | 84 proxy.impl.bind(pipe.endpoints[0]); |
79 remoteServiceProvider.connectToService(proxy.name, pipe.endpoints[1]); | 85 remoteServiceProvider.ptr.connectToService( |
| 86 proxy.name, pipe.endpoints[1]); |
80 return proxy; | 87 return proxy; |
81 } | 88 } |
82 | 89 |
83 void provideService(String interfaceName, ServiceFactory factory) { | 90 void provideService(String interfaceName, ServiceFactory factory) { |
84 assert(_localServiceProvider != null); | 91 assert(_localServiceProvider != null); |
85 _nameToServiceFactory[interfaceName] = factory; | 92 _nameToServiceFactory[interfaceName] = factory; |
86 } | 93 } |
87 | 94 |
88 void listen() { | 95 void listen() { |
89 if (_localServiceProvider != null) _localServiceProvider.listen(); | 96 assert(_localServiceProvider != null); |
| 97 _localServiceProvider.listen(); |
90 } | 98 } |
91 | 99 |
92 void close({bool nodefer: false}) { | 100 void close({bool nodefer: false}) { |
93 if (remoteServiceProvider != null) { | 101 if (remoteServiceProvider != null) { |
94 remoteServiceProvider.close(); | 102 remoteServiceProvider.close(); |
95 remoteServiceProvider = null; | 103 remoteServiceProvider = null; |
96 } | 104 } |
97 if (_localServiceProvider != null) { | 105 if (_localServiceProvider != null) { |
98 _localServiceProvider.close(nodefer: nodefer); | 106 _localServiceProvider.close(nodefer: nodefer); |
99 _localServiceProvider = null; | 107 _localServiceProvider = null; |
100 } | 108 } |
101 | 109 |
102 } | 110 } |
103 } | 111 } |
OLD | NEW |