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