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

Side by Side Diff: third_party/mojo/src/mojo/public/go/application/connection.go

Issue 1058033002: NOT TO COMMITTED: Update mojo sdk to rev 0018e6a91b75bcb941c867f8a7aa1753c3b53a13 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 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
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package application
6
7 import (
8 "log"
9
10 "mojo/public/go/bindings"
11 "mojo/public/go/system"
12
13 sp "mojo/public/interfaces/application/service_provider"
14 )
15
16 type connectionInfo struct {
17 requestorURL string
18 connectionURL string
19 }
20
21 // RequestorURL returns the URL of application that established the connection.
22 func (c *connectionInfo) RequestorURL() string {
23 return c.requestorURL
24 }
25
26 // ConnectionURL returns the URL that was used by the source application to
27 // establish a connection to the destination application.
28 func (c *connectionInfo) ConnectionURL() string {
29 return c.connectionURL
30 }
31
32 // ServiceRequest is an interface request for a specified mojo service.
33 type ServiceRequest interface {
34 // Name returns the name of requested mojo service.
35 Name() string
36
37 // PassMessagePipe passes ownership of the underlying message pipe
38 // handle to the newly created handle object, invalidating the
39 // underlying handle object in the process.
40 PassMessagePipe() system.MessagePipeHandle
41 }
42
43 // ServiceFactory provides implementation of a mojo service.
44 type ServiceFactory interface {
45 // Name returns the name of provided mojo service.
46 Name() string
47
48 // Create binds an implementation of mojo service to the provided
49 // message pipe and runs it.
50 Create(pipe system.MessagePipeHandle)
51 }
52
53 // Connection represents a connection to another application. An instance of
54 // this struct is passed to Delegate's AcceptConnection() function each time a
55 // connection is made to this application.
56 type Connection struct {
57 connectionInfo
58 // Request for local services. Is valid until ProvideServices is called.
59 servicesRequest *sp.ServiceProviderRequest
60 // Indicates that ProvideServices function was already called.
61 servicesProvided bool
62 localServices *bindings.Stub
63 outgoingConnection *OutgoingConnection
64 isClosed bool
65 }
66
67 func newConnection(requestorURL string, services *sp.ServiceProviderRequest, exp osedServices *sp.ServiceProviderPointer, resolvedURL string) *Connection {
68 info := connectionInfo{
69 requestorURL,
70 resolvedURL,
71 }
72 var remoteServices *sp.ServiceProviderProxy
73 if exposedServices != nil {
74 remoteServices = sp.NewServiceProviderProxy(*exposedServices, bi ndings.GetAsyncWaiter())
75 }
76 return &Connection{
77 connectionInfo: info,
78 servicesRequest: services,
79 outgoingConnection: &OutgoingConnection{
80 info,
81 remoteServices,
82 },
83 }
84 }
85
86 // ProvideServices starts a service provider on a separate goroutine that
87 // provides given services to the remote application. Returns a pointer to
88 // outgoing connection that can be used to connect to services provided by
89 // remote application.
90 // Panics if called more than once.
91 func (c *Connection) ProvideServices(services ...ServiceFactory) *OutgoingConnec tion {
92 if c.servicesProvided {
93 panic("ProvideServices can be called only once")
94 }
95 c.servicesProvided = true
96 if c.servicesRequest == nil {
97 return c.outgoingConnection
98 }
99 if len(services) == 0 {
100 c.servicesRequest.PassMessagePipe().Close()
101 return c.outgoingConnection
102 }
103
104 provider := &serviceProviderImpl{
105 make(map[string]ServiceFactory),
106 }
107 for _, service := range services {
108 provider.AddService(service)
109 }
110 c.localServices = sp.NewServiceProviderStub(*c.servicesRequest, provider , bindings.GetAsyncWaiter())
111 go func() {
112 for {
113 if err := c.localServices.ServeRequest(); err != nil {
114 // TODO(rogulenko): don't log in case message pi pe was closed
115 log.Println(err)
116 break
117 }
118 }
119 }()
120 return c.outgoingConnection
121 }
122
123 // Close closes both incoming and outgoing parts of the connection.
124 func (c *Connection) Close() {
125 if c.servicesRequest != nil {
126 c.servicesRequest.Close()
127 }
128 if c.localServices != nil {
129 c.localServices.Close()
130 }
131 if c.outgoingConnection.remoteServices != nil {
132 c.outgoingConnection.remoteServices.Close_proxy()
133 }
134 c.isClosed = true
135 }
136
137 // OutgoingConnection represents outgoing part of connection to another
138 // application. In order to close it close the |Connection| object that returned
139 // this |OutgoingConnection|.
140 type OutgoingConnection struct {
141 connectionInfo
142 remoteServices *sp.ServiceProviderProxy
143 }
144
145 // ConnectToService asks remote application to provide a service through the
146 // message pipe endpoint supplied by the caller.
147 func (c *OutgoingConnection) ConnectToService(request ServiceRequest) {
148 pipe := request.PassMessagePipe()
149 if c.remoteServices == nil {
150 pipe.Close()
151 return
152 }
153 c.remoteServices.ConnectToService(request.Name(), pipe)
154 }
155
156 // serviceProviderImpl is an implementation of mojo ServiceProvider interface.
157 type serviceProviderImpl struct {
158 factories map[string]ServiceFactory
159 }
160
161 // Mojo ServiceProvider implementation.
162 func (sp *serviceProviderImpl) ConnectToService(name string, messagePipe system. MessagePipeHandle) error {
163 factory, ok := sp.factories[name]
164 if !ok {
165 messagePipe.Close()
166 return nil
167 }
168 factory.Create(messagePipe)
169 return nil
170 }
171
172 func (sp *serviceProviderImpl) AddService(factory ServiceFactory) {
173 sp.factories[factory.Name()] = factory
174 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698