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 #ifndef MOJO_PUBLIC_CPP_APPLICATION_LIB_SERVICE_CONNECTOR_H_ | 5 #ifndef MOJO_PUBLIC_CPP_APPLICATION_LIB_SERVICE_CONNECTOR_H_ |
6 #define MOJO_PUBLIC_CPP_APPLICATION_LIB_SERVICE_CONNECTOR_H_ | 6 #define MOJO_PUBLIC_CPP_APPLICATION_LIB_SERVICE_CONNECTOR_H_ |
7 | 7 |
8 #include <assert.h> | 8 #include "mojo/public/cpp/application/interface_factory.h" |
9 | 9 #include "mojo/public/cpp/bindings/interface_request.h" |
10 #include <vector> | |
11 | |
12 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" | 10 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" |
13 | 11 |
14 namespace mojo { | 12 namespace mojo { |
15 class ApplicationConnection; | 13 class ApplicationConnection; |
16 | 14 |
17 namespace internal { | 15 namespace internal { |
18 | 16 |
19 template <class ServiceImpl, typename Context> | |
20 class ServiceConnector; | |
21 | |
22 // Specialization of ServiceConnection. | |
23 // ServiceImpl: Subclass of InterfaceImpl<...>. | |
24 // Context: Type of shared context. | |
25 template <class ServiceImpl, typename Context> | |
26 class ServiceConnection : public ServiceImpl { | |
27 public: | |
28 explicit ServiceConnection(ApplicationConnection* connection) | |
29 : ServiceImpl(connection) {} | |
30 ServiceConnection(ApplicationConnection* connection, | |
31 Context* context) : ServiceImpl(connection, context) {} | |
32 | |
33 virtual void OnConnectionError() MOJO_OVERRIDE { | |
34 service_connector_->RemoveConnection(static_cast<ServiceImpl*>(this)); | |
35 ServiceImpl::OnConnectionError(); | |
36 } | |
37 | |
38 private: | |
39 friend class ServiceConnector<ServiceImpl, Context>; | |
40 | |
41 // Called shortly after this class is instantiated. | |
42 void set_service_connector( | |
43 ServiceConnector<ServiceImpl, Context>* connector) { | |
44 service_connector_ = connector; | |
45 } | |
46 | |
47 ServiceConnector<ServiceImpl, Context>* service_connector_; | |
48 | |
49 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceConnection); | |
50 }; | |
51 | |
52 template <typename ServiceImpl, typename Context> | |
53 struct ServiceConstructor { | |
54 static ServiceConnection<ServiceImpl, Context>* New( | |
55 ApplicationConnection* connection, | |
56 Context* context) { | |
57 return new ServiceConnection<ServiceImpl, Context>( | |
58 connection, context); | |
59 } | |
60 }; | |
61 | |
62 template <typename ServiceImpl> | |
63 struct ServiceConstructor<ServiceImpl, void> { | |
64 public: | |
65 static ServiceConnection<ServiceImpl, void>* New( | |
66 ApplicationConnection* connection, | |
67 void* context) { | |
68 return new ServiceConnection<ServiceImpl, void>(connection); | |
69 } | |
70 }; | |
71 | |
72 class ServiceConnectorBase { | 17 class ServiceConnectorBase { |
73 public: | 18 public: |
74 ServiceConnectorBase(const std::string& name); | 19 ServiceConnectorBase(const std::string& name); |
75 virtual ~ServiceConnectorBase(); | 20 virtual ~ServiceConnectorBase(); |
76 virtual void ConnectToService(const std::string& name, | 21 virtual void ConnectToService(const std::string& name, |
77 ScopedMessagePipeHandle client_handle) = 0; | 22 ScopedMessagePipeHandle client_handle) = 0; |
78 std::string name() const { return name_; } | 23 std::string name() const { return name_; } |
79 void set_application_connection(ApplicationConnection* connection) { | 24 void set_application_connection(ApplicationConnection* connection) { |
80 application_connection_ = connection; } | 25 application_connection_ = connection; } |
81 | 26 |
82 protected: | 27 protected: |
83 std::string name_; | 28 std::string name_; |
84 ApplicationConnection* application_connection_; | 29 ApplicationConnection* application_connection_; |
85 | 30 |
86 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceConnectorBase); | 31 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceConnectorBase); |
87 }; | 32 }; |
88 | 33 |
89 template <class ServiceImpl, typename Context=void> | 34 template <typename Interface> |
90 class ServiceConnector : public internal::ServiceConnectorBase { | 35 class InterfaceFactoryConnector : public ServiceConnectorBase { |
91 public: | 36 public: |
92 ServiceConnector(const std::string& name, Context* context = NULL) | 37 explicit InterfaceFactoryConnector(InterfaceFactory<Interface>* factory) |
93 : ServiceConnectorBase(name), context_(context) {} | 38 : ServiceConnectorBase(Interface::Name_), factory_(factory) {} |
| 39 virtual ~InterfaceFactoryConnector() {} |
94 | 40 |
95 virtual ~ServiceConnector() { | 41 virtual void ConnectToService(const std::string& name, |
96 ConnectionList doomed; | 42 ScopedMessagePipeHandle client_handle) { |
97 doomed.swap(connections_); | 43 factory_->Create(application_connection_, |
98 for (typename ConnectionList::iterator it = doomed.begin(); | 44 MakeRequest<Interface>(client_handle.Pass())); |
99 it != doomed.end(); ++it) { | |
100 delete *it; | |
101 } | |
102 assert(connections_.empty()); // No one should have added more! | |
103 } | 45 } |
104 | 46 |
105 virtual void ConnectToService(const std::string& name, | |
106 ScopedMessagePipeHandle handle) MOJO_OVERRIDE { | |
107 ServiceConnection<ServiceImpl, Context>* impl = | |
108 ServiceConstructor<ServiceImpl, Context>::New(application_connection_, | |
109 context_); | |
110 impl->set_service_connector(this); | |
111 BindToPipe(impl, handle.Pass()); | |
112 | |
113 connections_.push_back(impl); | |
114 } | |
115 | |
116 void RemoveConnection(ServiceImpl* impl) { | |
117 // Called from ~ServiceImpl, in response to a connection error. | |
118 for (typename ConnectionList::iterator it = connections_.begin(); | |
119 it != connections_.end(); ++it) { | |
120 if (*it == impl) { | |
121 delete impl; | |
122 connections_.erase(it); | |
123 return; | |
124 } | |
125 } | |
126 } | |
127 | |
128 Context* context() const { return context_; } | |
129 | |
130 private: | 47 private: |
131 typedef std::vector<ServiceImpl*> ConnectionList; | 48 InterfaceFactory<Interface>* factory_; |
132 ConnectionList connections_; | 49 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceFactoryConnector); |
133 Context* context_; | |
134 | |
135 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceConnector); | |
136 }; | 50 }; |
137 | 51 |
138 } // namespace internal | 52 } // namespace internal |
139 } // namespace mojo | 53 } // namespace mojo |
140 | 54 |
141 #endif // MOJO_PUBLIC_CPP_APPLICATION_LIB_SERVICE_CONNECTOR_H_ | 55 #endif // MOJO_PUBLIC_CPP_APPLICATION_LIB_SERVICE_CONNECTOR_H_ |
OLD | NEW |