| OLD | NEW |
| (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 "fmt" | |
| 9 "log" | |
| 10 | |
| 11 "mojo/public/go/bindings" | |
| 12 | |
| 13 "mojo/public/interfaces/bindings/mojom_types" | |
| 14 "mojo/public/interfaces/bindings/service_describer" | |
| 15 ) | |
| 16 | |
| 17 // ServiceDescriberFactory implements ServiceFactory for ServiceDescriber. | |
| 18 // For cleanup purposes, it is also the implementation of a ServiceDescriber. | |
| 19 type ServiceDescriberFactory struct { | |
| 20 // mapping holds the map from interface names to ServiceDescription's. | |
| 21 mapping map[string]service_describer.ServiceDescription | |
| 22 // stubs stores the stubs for connections opened to this factory. | |
| 23 stubs []*bindings.Stub | |
| 24 // descriptionFactories maps interface names to ServiceDescriptionFactor
y's. | |
| 25 descriptionFactories map[string]*ServiceDescriptionFactory | |
| 26 } | |
| 27 | |
| 28 func newServiceDescriberFactory(mapping map[string]service_describer.ServiceDesc
ription) *ServiceDescriberFactory { | |
| 29 return &ServiceDescriberFactory{ | |
| 30 mapping: mapping, | |
| 31 descriptionFactories: make(map[string]*ServiceDescriptionFactory
), | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 func (sd *ServiceDescriberFactory) Create(request service_describer.ServiceDescr
iber_Request) { | |
| 36 stub := service_describer.NewServiceDescriberStub(request, sd, bindings.
GetAsyncWaiter()) | |
| 37 sd.stubs = append(sd.stubs, stub) | |
| 38 go func() { | |
| 39 for { | |
| 40 if err := stub.ServeRequest(); err != nil { | |
| 41 connectionError, ok := err.(*bindings.Connection
Error) | |
| 42 if !ok || !connectionError.Closed() { | |
| 43 log.Println(err) | |
| 44 } | |
| 45 break | |
| 46 } | |
| 47 } | |
| 48 }() | |
| 49 } | |
| 50 | |
| 51 func (sd *ServiceDescriberFactory) Close() { | |
| 52 for _, stub := range sd.stubs { | |
| 53 stub.Close() | |
| 54 } | |
| 55 for _, factory := range sd.descriptionFactories { | |
| 56 for _, stub := range factory.stubs { | |
| 57 stub.Close() | |
| 58 } | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 // Helper method for DescribeService | |
| 63 func (sd *ServiceDescriberFactory) getServiceDescriptionFactory(inInterfaceName
string) *ServiceDescriptionFactory { | |
| 64 // Assumes the interface name is in the mapping. | |
| 65 if desc, ok := sd.descriptionFactories[inInterfaceName]; ok { | |
| 66 return desc | |
| 67 } | |
| 68 sd.descriptionFactories[inInterfaceName] = &ServiceDescriptionFactory{ | |
| 69 impl: sd.mapping[inInterfaceName], | |
| 70 } | |
| 71 return sd.descriptionFactories[inInterfaceName] | |
| 72 } | |
| 73 | |
| 74 func (sd *ServiceDescriberFactory) DescribeService(inInterfaceName string, inDes
criptionRequest service_describer.ServiceDescription_Request) (err error) { | |
| 75 if _, ok := sd.mapping[inInterfaceName]; ok { | |
| 76 sd.getServiceDescriptionFactory(inInterfaceName).Create(inDescri
ptionRequest) | |
| 77 return nil | |
| 78 } | |
| 79 return fmt.Errorf("The interface %s is unknown by this application", inI
nterfaceName) | |
| 80 } | |
| 81 | |
| 82 // ServiceDescriptionFactory implements ServiceFactory for ServiceDescription. | |
| 83 type ServiceDescriptionFactory struct { | |
| 84 // stubs stores the stubs for connections opened to this factory. | |
| 85 stubs []*bindings.Stub | |
| 86 // impl is the ServiceDescription implementation served by this factory. | |
| 87 impl service_describer.ServiceDescription | |
| 88 } | |
| 89 | |
| 90 func (serviceDescriptionFactory *ServiceDescriptionFactory) Create(request servi
ce_describer.ServiceDescription_Request) { | |
| 91 stub := service_describer.NewServiceDescriptionStub(request, serviceDesc
riptionFactory.impl, bindings.GetAsyncWaiter()) | |
| 92 serviceDescriptionFactory.stubs = append(serviceDescriptionFactory.stubs
, stub) | |
| 93 go func() { | |
| 94 for { | |
| 95 if err := stub.ServeRequest(); err != nil { | |
| 96 connectionError, ok := err.(*bindings.Connection
Error) | |
| 97 if !ok || !connectionError.Closed() { | |
| 98 log.Println(err) | |
| 99 } | |
| 100 break | |
| 101 } | |
| 102 } | |
| 103 }() | |
| 104 } | |
| 105 | |
| 106 // ObjectWithMojomTypeSupport is an interface implemented by pointers to | |
| 107 // Mojo structs, enums, interface requests and union variants, but only if the | |
| 108 // support of runtime mojom type information was enabled at build time. | |
| 109 type ObjectWithMojomTypeSupport interface { | |
| 110 // MojomType returns the UserDefinedType that describes the Mojom | |
| 111 // type of this object. To obtain the UserDefinedType for Mojom types re
cursively | |
| 112 // contained in the returned UserDefinedType, look in the map returned | |
| 113 // by the function AllMojomTypes(). | |
| 114 MojomType() mojom_types.UserDefinedType | |
| 115 | |
| 116 // AllMojomTypes returns a map that contains the UserDefinedType for | |
| 117 // all Mojom types in the complete type graph of the Mojom type of this
object. | |
| 118 AllMojomTypes() map[string]mojom_types.UserDefinedType | |
| 119 } | |
| OLD | NEW |