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

Side by Side Diff: mojo/public/cpp/application/lib/service_connector.h

Issue 380413003: Mojo: Use InterfaceFactory<Interface> for service registration (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review feedback Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
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 <assert.h>
9 9
10 #include <vector> 10 #include <vector>
11 11
12 #include "mojo/public/cpp/application/interface_factory.h"
12 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" 13 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h"
13 14
14 namespace mojo { 15 namespace mojo {
15 class ApplicationConnection; 16 class ApplicationConnection;
16 17
17 namespace internal { 18 namespace internal {
18 19
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 { 20 class ServiceConnectorBase {
73 public: 21 public:
74 ServiceConnectorBase(const std::string& name); 22 ServiceConnectorBase(const std::string& name);
75 virtual ~ServiceConnectorBase(); 23 virtual ~ServiceConnectorBase();
tim (not reviewing) 2014/07/16 17:57:09 Are these defined anywhere?
jamesr 2014/07/17 22:22:50 Yes, in service_connector_base.cc
76 virtual void ConnectToService(const std::string& name, 24 virtual void ConnectToService(const std::string& name,
77 ScopedMessagePipeHandle client_handle) = 0; 25 ScopedMessagePipeHandle client_handle) = 0;
78 std::string name() const { return name_; } 26 std::string name() const { return name_; }
79 void set_application_connection(ApplicationConnection* connection) { 27 void set_application_connection(ApplicationConnection* connection) {
80 application_connection_ = connection; } 28 application_connection_ = connection; }
81 29
82 protected: 30 protected:
83 std::string name_; 31 std::string name_;
84 ApplicationConnection* application_connection_; 32 ApplicationConnection* application_connection_;
85 33
86 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceConnectorBase); 34 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceConnectorBase);
87 }; 35 };
88 36
89 template <class ServiceImpl, typename Context=void> 37 template <typename Interface>
90 class ServiceConnector : public internal::ServiceConnectorBase { 38 class InterfaceFactoryConnector : public ServiceConnectorBase {
91 public: 39 public:
92 ServiceConnector(const std::string& name, Context* context = NULL) 40 InterfaceFactoryConnector(InterfaceFactory<Interface>* provider)
93 : ServiceConnectorBase(name), context_(context) {} 41 : ServiceConnectorBase(Interface::Name_), factory_(provider) {}
42 virtual ~InterfaceFactoryConnector() {}
94 43
95 virtual ~ServiceConnector() { 44 virtual void ConnectToService(const std::string& name,
96 ConnectionList doomed; 45 ScopedMessagePipeHandle client_handle) {
97 doomed.swap(connections_); 46 factory_->Create(application_connection_,
98 for (typename ConnectionList::iterator it = doomed.begin(); 47 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 } 48 }
104 49
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: 50 private:
131 typedef std::vector<ServiceImpl*> ConnectionList; 51 InterfaceFactory<Interface>* factory_;
132 ConnectionList connections_; 52 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceFactoryConnector);
133 Context* context_;
134
135 MOJO_DISALLOW_COPY_AND_ASSIGN(ServiceConnector);
136 }; 53 };
137 54
138 } // namespace internal 55 } // namespace internal
139 } // namespace mojo 56 } // namespace mojo
140 57
141 #endif // MOJO_PUBLIC_CPP_APPLICATION_LIB_SERVICE_CONNECTOR_H_ 58 #endif // MOJO_PUBLIC_CPP_APPLICATION_LIB_SERVICE_CONNECTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698