| 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_SHELL_SERVICE_H_ | 5 #ifndef MOJO_PUBLIC_SHELL_SERVICE_H_ |
| 6 #define MOJO_PUBLIC_SHELL_SERVICE_H_ | 6 #define MOJO_PUBLIC_SHELL_SERVICE_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "mojo/public/cpp/bindings/allocation_scope.h" |
| 10 #include "mojo/public/cpp/bindings/error_handler.h" | 11 #include "mojo/public/cpp/bindings/error_handler.h" |
| 11 #include "mojo/public/cpp/bindings/remote_ptr.h" | 12 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 12 #include "mojo/public/cpp/system/core.h" | 13 #include "mojo/public/cpp/system/core.h" |
| 13 #include "mojo/public/interfaces/shell/shell.mojom.h" | 14 #include "mojo/public/interfaces/shell/shell.mojom.h" |
| 14 | 15 |
| 15 // Utility classes for creating ShellClients that vend service instances. | 16 // Utility classes for creating ShellClients that vend service instances. |
| 16 // To use define a class that implements your specific server api, e.g. FooImpl | 17 // To use define a class that implements your specific server api, e.g. FooImpl |
| 17 // to implement a service named Foo. That class must define an empty constructor | 18 // to implement a service named Foo. That class must define an empty constructor |
| 18 // and the Initialize() method. | 19 // and the Initialize() method. |
| 19 // class FooImpl : public Foo { | 20 // class FooImpl : public Foo { |
| 20 // public: | 21 // public: |
| 21 // FooImpl(); | 22 // FooImpl(); |
| 22 // void Initialize(ServiceConnector<FooImpl>* service_connector, | 23 // void Initialize(); |
| 23 // ScopedMessagePipeHandle client_handle | |
| 24 // private: | 24 // private: |
| 25 // ServiceConnector<FooImpl>* service_connector_; | 25 // ServiceConnector<FooImpl>* service_connector_; |
| 26 // RemotePtr<FooPeer> client_; | |
| 27 // }; | 26 // }; |
| 28 // | 27 // |
| 29 // | 28 // |
| 30 // To simplify further FooImpl can use the ServiceConnection<> template. | 29 // To simplify further FooImpl can use the ServiceConnection<> template. |
| 31 // class FooImpl : public ServiceConnection<Foo, FooImpl> { | 30 // class FooImpl : public ServiceConnection<Foo, FooImpl> { |
| 32 // public: | 31 // public: |
| 33 // FooImpl(); | 32 // FooImpl(); |
| 34 // ... | 33 // ... |
| 35 // <Foo implementation> | 34 // <Foo implementation> |
| 36 // }; | 35 // }; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 59 // Context: Optional type of shared context.v | 58 // Context: Optional type of shared context.v |
| 60 // | 59 // |
| 61 // | 60 // |
| 62 namespace mojo { | 61 namespace mojo { |
| 63 | 62 |
| 64 namespace internal { | 63 namespace internal { |
| 65 class ServiceConnectorBase { | 64 class ServiceConnectorBase { |
| 66 public: | 65 public: |
| 67 class Owner : public ShellClient { | 66 class Owner : public ShellClient { |
| 68 public: | 67 public: |
| 69 Owner(ScopedShellHandle shell_handle); | 68 Owner(ScopedMessagePipeHandle shell_handle); |
| 70 ~Owner(); | 69 virtual ~Owner(); |
| 71 Shell* shell() { return shell_.get(); } | 70 Shell* shell() { return shell_.get(); } |
| 72 virtual void AddServiceConnector( | 71 virtual void AddServiceConnector( |
| 73 internal::ServiceConnectorBase* service_connector) = 0; | 72 internal::ServiceConnectorBase* service_connector) = 0; |
| 74 virtual void RemoveServiceConnector( | 73 virtual void RemoveServiceConnector( |
| 75 internal::ServiceConnectorBase* service_connector) = 0; | 74 internal::ServiceConnectorBase* service_connector) = 0; |
| 76 | 75 |
| 77 protected: | 76 protected: |
| 78 void set_service_connector_owner(ServiceConnectorBase* service_connector, | 77 void set_service_connector_owner(ServiceConnectorBase* service_connector, |
| 79 Owner* owner) { | 78 Owner* owner) { |
| 80 service_connector->owner_ = owner; | 79 service_connector->owner_ = owner; |
| 81 } | 80 } |
| 82 RemotePtr<Shell> shell_; | 81 ShellPtr shell_; |
| 83 }; | 82 }; |
| 84 ServiceConnectorBase() : owner_(NULL) {} | 83 ServiceConnectorBase() : owner_(NULL) {} |
| 85 virtual ~ServiceConnectorBase(); | 84 virtual ~ServiceConnectorBase(); |
| 86 Shell* shell() { return owner_->shell(); } | 85 Shell* shell() { return owner_->shell(); } |
| 87 virtual void AcceptConnection(const std::string& url, | 86 virtual void AcceptConnection(const std::string& url, |
| 88 ScopedMessagePipeHandle client_handle) = 0; | 87 ScopedMessagePipeHandle client_handle) = 0; |
| 89 | 88 |
| 90 protected: | 89 protected: |
| 91 Owner* owner_; | 90 Owner* owner_; |
| 92 }; | 91 }; |
| 93 } // namespace internal | 92 } // namespace internal |
| 94 | 93 |
| 95 template <class ServiceImpl, typename Context=void> | 94 template <class ConnectionImpl, typename Context=void> |
| 96 class ServiceConnector : public internal::ServiceConnectorBase { | 95 class ServiceConnector : public internal::ServiceConnectorBase { |
| 97 public: | 96 public: |
| 97 typedef typename ConnectionImpl::Interface Interface; |
| 98 |
| 98 ServiceConnector(Context* context = NULL) : context_(context) {} | 99 ServiceConnector(Context* context = NULL) : context_(context) {} |
| 99 | 100 |
| 100 virtual ~ServiceConnector() { | 101 virtual ~ServiceConnector() { |
| 101 for (typename ServiceList::iterator it = services_.begin(); | 102 for (typename ConnectionList::iterator it = connections_.begin(); |
| 102 it != services_.end(); ++it) { | 103 it != connections_.end(); ++it) { |
| 103 delete *it; | 104 delete *it; |
| 104 } | 105 } |
| 105 } | 106 } |
| 106 | 107 |
| 107 virtual void AcceptConnection(const std::string& url, | 108 virtual void AcceptConnection(const std::string& url, |
| 108 ScopedMessagePipeHandle client_handle) | 109 ScopedMessagePipeHandle handle) MOJO_OVERRIDE { |
| 109 MOJO_OVERRIDE { | 110 connections_.push_back(new ConnectionWrapper(this, handle.Pass())); |
| 110 ServiceImpl* service = new ServiceImpl(); | |
| 111 service->Initialize(this, client_handle.Pass()); | |
| 112 services_.push_back(service); | |
| 113 } | 111 } |
| 114 | 112 |
| 115 void RemoveService(ServiceImpl* service) { | 113 void RemoveConnection(Interface* impl) { |
| 116 for (typename ServiceList::iterator it = services_.begin(); | 114 for (typename ConnectionList::iterator it = connections_.begin(); |
| 117 it != services_.end(); ++it) { | 115 it != connections_.end(); ++it) { |
| 118 if (*it == service) { | 116 if ((*it)->get() == impl) { |
| 119 services_.erase(it); | 117 connections_.erase(it); |
| 120 delete service; | 118 if (connections_.empty()) |
| 121 if (services_.empty()) | |
| 122 owner_->RemoveServiceConnector(this); | 119 owner_->RemoveServiceConnector(this); |
| 123 return; | 120 return; |
| 124 } | 121 } |
| 125 } | 122 } |
| 126 } | 123 } |
| 127 | 124 |
| 128 Context* context() const { return context_; } | 125 Context* context() const { return context_; } |
| 129 | 126 |
| 130 private: | 127 private: |
| 131 typedef std::vector<ServiceImpl*> ServiceList; | 128 class ConnectionWrapper : public ErrorHandler { |
| 132 ServiceList services_; | 129 public: |
| 130 ConnectionWrapper(ServiceConnector<ConnectionImpl, Context>* connector, |
| 131 ScopedMessagePipeHandle handle) |
| 132 : connector_(connector) { |
| 133 ConnectionImpl* impl = new ConnectionImpl(); |
| 134 impl->set_connector(connector_); |
| 135 |
| 136 ptr_.Bind(impl); |
| 137 ptr_.ConfigureStub(handle.Pass()); |
| 138 |
| 139 impl->Initialize(); |
| 140 } |
| 141 |
| 142 Interface* get() { return ptr_.get(); } |
| 143 |
| 144 private: |
| 145 virtual void OnError() MOJO_OVERRIDE { |
| 146 connector_->RemoveConnection(ptr_.get()); |
| 147 } |
| 148 |
| 149 InterfacePtr<Interface> ptr_; |
| 150 ServiceConnector<ConnectionImpl, Context>* connector_; |
| 151 }; |
| 152 |
| 153 typedef std::vector<ConnectionWrapper*> ConnectionList; |
| 154 ConnectionList connections_; |
| 133 Context* context_; | 155 Context* context_; |
| 134 }; | 156 }; |
| 135 | 157 |
| 136 // Specialization of ServiceConnection. | 158 // Specialization of ServiceConnection. |
| 137 // ServiceInterface: Service interface. | 159 // ServiceInterface: Service interface. |
| 138 // ServiceImpl: Implementation of Service interface. | 160 // ServiceImpl: Implementation of Service interface. |
| 139 // Context: Optional type of shared context. | 161 // Context: Optional type of shared context. |
| 140 template <class ServiceInterface, class ServiceImpl, typename Context=void> | 162 template <class ServiceInterface, class ConnectionImpl, typename Context=void> |
| 141 class ServiceConnection : public ServiceInterface { | 163 class ServiceConnection : public ServiceInterface { |
| 142 public: | 164 public: |
| 143 virtual ~ServiceConnection() {} | 165 typedef ServiceInterface Interface; |
| 166 |
| 167 virtual ~ServiceConnection() { |
| 168 service_connector_->RemoveConnection(this); |
| 169 } |
| 144 | 170 |
| 145 protected: | 171 protected: |
| 146 ServiceConnection() : reaper_(this), service_connector_(NULL) {} | 172 ServiceConnection() : service_connector_(NULL) {} |
| 147 | 173 |
| 148 void Initialize(ServiceConnector<ServiceImpl, Context>* service_connector, | 174 // Shadow this method in ConnectionImpl to perform one-time initialization. |
| 149 ScopedMessagePipeHandle client_handle) { | 175 // At the time this is called, shell() and context() will be available. |
| 150 service_connector_ = service_connector; | 176 // NOTE: Do not call the base class Initialize. It will always be a no-op. |
| 151 client_.reset( | 177 void Initialize() {} |
| 152 MakeScopedHandle( | 178 |
| 153 InterfaceHandle<typename ServiceInterface::_Peer>( | 179 Shell* shell() { |
| 154 client_handle.release().value())).Pass(), | 180 return service_connector_->shell(); |
| 155 this, | |
| 156 &reaper_); | |
| 157 } | 181 } |
| 158 | 182 |
| 159 Shell* shell() { return service_connector_->shell(); } | 183 Context* context() const { |
| 160 Context* context() const { return service_connector_->context(); } | 184 return service_connector_->context(); |
| 161 typename ServiceInterface::_Peer* client() { return client_.get(); } | 185 } |
| 162 | 186 |
| 163 private: | 187 private: |
| 164 // The Reaper class allows us to handle errors on the client proxy without | 188 friend class ServiceConnector<ConnectionImpl, Context>; |
| 165 // polluting the name space of the ServiceConnection<> class. | 189 void set_connector(ServiceConnector<ConnectionImpl, Context>* connector) { |
| 166 class Reaper : public ErrorHandler { | 190 service_connector_ = connector; |
| 167 public: | 191 } |
| 168 Reaper(ServiceConnection<ServiceInterface, ServiceImpl, Context>* service) | 192 ServiceConnector<ConnectionImpl, Context>* service_connector_; |
| 169 : service_(service) {} | |
| 170 virtual void OnError() { | |
| 171 service_->service_connector_->RemoveService( | |
| 172 static_cast<ServiceImpl*>(service_)); | |
| 173 } | |
| 174 private: | |
| 175 ServiceConnection<ServiceInterface, ServiceImpl, Context>* service_; | |
| 176 }; | |
| 177 friend class ServiceConnector<ServiceImpl, Context>; | |
| 178 Reaper reaper_; | |
| 179 ServiceConnector<ServiceImpl, Context>* service_connector_; | |
| 180 RemotePtr<typename ServiceInterface::_Peer> client_; | |
| 181 }; | 193 }; |
| 182 | 194 |
| 195 template <typename InterfacePtr> |
| 196 inline void ConnectTo(Shell* shell, const std::string& url, InterfacePtr* ptr) { |
| 197 MessagePipe pipe; |
| 198 *ptr = MakeProxy<typename InterfacePtr::Interface>(pipe.handle0.Pass()); |
| 199 |
| 200 AllocationScope scope; |
| 201 shell->Connect(url, pipe.handle1.Pass()); |
| 202 } |
| 203 |
| 183 } // namespace mojo | 204 } // namespace mojo |
| 184 | 205 |
| 185 #endif // MOJO_PUBLIC_SHELL_SERVICE_H_ | 206 #endif // MOJO_PUBLIC_SHELL_SERVICE_H_ |
| OLD | NEW |