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/bindings/error_handler.h" | 10 #include "mojo/public/bindings/error_handler.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 // Specialization of ServiceFactory. | 56 // Specialization of ServiceFactory. |
57 // ServiceImpl: Implementation of Service interface. | 57 // ServiceImpl: Implementation of Service interface. |
58 // Context: Optional type of shared context.v | 58 // Context: Optional type of shared context.v |
59 // | 59 // |
60 // | 60 // |
61 namespace mojo { | 61 namespace mojo { |
62 | 62 |
63 namespace internal { | 63 namespace internal { |
64 class ServiceFactoryBase { | 64 class ServiceFactoryBase { |
65 public: | 65 public: |
66 class Owner { | 66 class Owner : public ShellClient { |
67 public: | 67 public: |
68 virtual Shell* GetShell() = 0; | 68 Owner(ScopedShellHandle shell_handle); |
| 69 ~Owner(); |
| 70 Shell* shell() { return shell_.get(); } |
69 virtual void AddServiceFactory( | 71 virtual void AddServiceFactory( |
70 internal::ServiceFactoryBase* service_factory) = 0; | 72 internal::ServiceFactoryBase* service_factory) = 0; |
71 virtual void RemoveServiceFactory( | 73 virtual void RemoveServiceFactory( |
72 internal::ServiceFactoryBase* service_factory) = 0; | 74 internal::ServiceFactoryBase* service_factory) = 0; |
73 | 75 |
74 protected: | 76 protected: |
75 void set_service_factory_owner(ServiceFactoryBase* service_factory, | 77 void set_service_factory_owner(ServiceFactoryBase* service_factory, |
76 Owner* owner) { | 78 Owner* owner) { |
77 service_factory->owner_ = owner; | 79 service_factory->owner_ = owner; |
78 } | 80 } |
| 81 RemotePtr<Shell> shell_; |
79 }; | 82 }; |
80 ServiceFactoryBase() : owner_(NULL) {} | 83 ServiceFactoryBase() : owner_(NULL) {} |
81 virtual ~ServiceFactoryBase(); | 84 virtual ~ServiceFactoryBase(); |
82 Shell* GetShell() { return owner_->GetShell(); } | 85 Shell* shell() { return owner_->shell(); } |
83 virtual void AcceptConnection(const std::string& url, | 86 virtual void AcceptConnection(const std::string& url, |
84 ScopedMessagePipeHandle client_handle) = 0; | 87 ScopedMessagePipeHandle client_handle) = 0; |
85 | 88 |
86 protected: | 89 protected: |
87 Owner* owner_; | 90 Owner* owner_; |
88 }; | 91 }; |
89 } // namespace internal | 92 } // namespace internal |
90 | 93 |
91 template <class ServiceImpl, typename Context=void> | 94 template <class ServiceImpl, typename Context=void> |
92 class ServiceFactory : public internal::ServiceFactoryBase { | 95 class ServiceFactory : public internal::ServiceFactoryBase { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 ScopedMessagePipeHandle client_handle) { | 148 ScopedMessagePipeHandle client_handle) { |
146 service_factory_ = service_factory; | 149 service_factory_ = service_factory; |
147 client_.reset( | 150 client_.reset( |
148 MakeScopedHandle( | 151 MakeScopedHandle( |
149 InterfaceHandle<typename ServiceInterface::_Peer>( | 152 InterfaceHandle<typename ServiceInterface::_Peer>( |
150 client_handle.release().value())).Pass(), | 153 client_handle.release().value())).Pass(), |
151 this, | 154 this, |
152 &reaper_); | 155 &reaper_); |
153 } | 156 } |
154 | 157 |
| 158 Shell* shell() { return service_factory_->shell(); } |
155 Context* context() const { return service_factory_->context(); } | 159 Context* context() const { return service_factory_->context(); } |
156 typename ServiceInterface::_Peer* client() { return client_.get(); } | 160 typename ServiceInterface::_Peer* client() { return client_.get(); } |
157 | 161 |
158 private: | 162 private: |
159 // The Reaper class allows us to handle errors on the client proxy without | 163 // The Reaper class allows us to handle errors on the client proxy without |
160 // polluting the name space of the Service<> class. | 164 // polluting the name space of the Service<> class. |
161 class Reaper : public ErrorHandler { | 165 class Reaper : public ErrorHandler { |
162 public: | 166 public: |
163 Reaper(Service<ServiceInterface, ServiceImpl, Context>* service) | 167 Reaper(Service<ServiceInterface, ServiceImpl, Context>* service) |
164 : service_(service) {} | 168 : service_(service) {} |
165 virtual void OnError() { | 169 virtual void OnError() { |
166 service_->service_factory_->RemoveService( | 170 service_->service_factory_->RemoveService( |
167 static_cast<ServiceImpl*>(service_)); | 171 static_cast<ServiceImpl*>(service_)); |
168 } | 172 } |
169 private: | 173 private: |
170 Service<ServiceInterface, ServiceImpl, Context>* service_; | 174 Service<ServiceInterface, ServiceImpl, Context>* service_; |
171 }; | 175 }; |
172 friend class ServiceFactory<ServiceImpl, Context>; | 176 friend class ServiceFactory<ServiceImpl, Context>; |
173 Reaper reaper_; | 177 Reaper reaper_; |
174 ServiceFactory<ServiceImpl, Context>* service_factory_; | 178 ServiceFactory<ServiceImpl, Context>* service_factory_; |
175 RemotePtr<typename ServiceInterface::_Peer> client_; | 179 RemotePtr<typename ServiceInterface::_Peer> client_; |
176 }; | 180 }; |
177 | 181 |
178 } // namespace mojo | 182 } // namespace mojo |
179 | 183 |
180 #endif // MOJO_PUBLIC_SHELL_SERVICE_H_ | 184 #endif // MOJO_PUBLIC_SHELL_SERVICE_H_ |
OLD | NEW |