| 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_APPLICATION_H_ | 5 #ifndef MOJO_PUBLIC_APPLICATION_APPLICATION_H_ |
| 6 #define MOJO_PUBLIC_SHELL_APPLICATION_H_ | 6 #define MOJO_PUBLIC_APPLICATION_APPLICATION_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "mojo/public/cpp/shell/connect.h" | 10 #include "mojo/public/cpp/application/connect.h" |
| 11 #include "mojo/public/cpp/shell/lib/service_connector.h" | 11 #include "mojo/public/cpp/application/lib/service_connector.h" |
| 12 #include "mojo/public/cpp/system/core.h" | 12 #include "mojo/public/cpp/system/core.h" |
| 13 #include "mojo/public/interfaces/shell/shell.mojom.h" | 13 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" |
| 14 | 14 |
| 15 namespace mojo { | 15 namespace mojo { |
| 16 | 16 |
| 17 // Utility class for creating ShellClients that vend service instances. | 17 // Utility class for creating ServiceProviders that vend service instances. |
| 18 // To use define a class that implements your specific server api, e.g. FooImpl | 18 // To use define a class that implements your specific server api, e.g. FooImpl |
| 19 // to implement a service named Foo. | 19 // to implement a service named Foo. |
| 20 // That class must subclass an InterfaceImpl specialization. | 20 // That class must subclass an InterfaceImpl specialization. |
| 21 // | 21 // |
| 22 // If there is context that is to be shared amongst all instances, define a | 22 // If there is context that is to be shared amongst all instances, define a |
| 23 // constructor with that class as its only argument, otherwise define an empty | 23 // constructor with that class as its only argument, otherwise define an empty |
| 24 // constructor. | 24 // constructor. |
| 25 // | 25 // |
| 26 // class FooImpl : public InterfaceImpl<Foo> { | 26 // class FooImpl : public InterfaceImpl<Foo> { |
| 27 // public: | 27 // public: |
| 28 // FooImpl() {} | 28 // FooImpl() {} |
| 29 // }; | 29 // }; |
| 30 // | 30 // |
| 31 // or | 31 // or |
| 32 // | 32 // |
| 33 // class BarImpl : public InterfaceImpl<Bar> { | 33 // class BarImpl : public InterfaceImpl<Bar> { |
| 34 // public: | 34 // public: |
| 35 // // context will remain valid for the lifetime of BarImpl. | 35 // // context will remain valid for the lifetime of BarImpl. |
| 36 // BarImpl(BarContext* context) : context_(context) {} | 36 // BarImpl(BarContext* context) : context_(context) {} |
| 37 // private: | 37 // private: |
| 38 // BarContext* context; | 38 // BarContext* context; |
| 39 // }; | 39 // }; |
| 40 // | 40 // |
| 41 // Create an Application instance that collects any service implementations. | 41 // Create an Application instance that collects any service implementations. |
| 42 // | 42 // |
| 43 // Application app(shell_handle); | 43 // Application app(service_provider_handle); |
| 44 // app.AddService<FooImpl>(); | 44 // app.AddService<FooImpl>(); |
| 45 // | 45 // |
| 46 // BarContext context; | 46 // BarContext context; |
| 47 // app.AddService<BarImpl>(&context); | 47 // app.AddService<BarImpl>(&context); |
| 48 // | 48 // |
| 49 // | 49 // |
| 50 class Application : public internal::ServiceConnectorBase::Owner { | 50 class Application : public internal::ServiceConnectorBase::Owner { |
| 51 public: | 51 public: |
| 52 explicit Application(ScopedMessagePipeHandle shell_handle); | 52 explicit Application(ScopedMessagePipeHandle service_provider_handle); |
| 53 explicit Application(MojoHandle shell_handle); | 53 explicit Application(MojoHandle service_provider_handle); |
| 54 virtual ~Application(); | 54 virtual ~Application(); |
| 55 | 55 |
| 56 template <typename Impl, typename Context> | 56 template <typename Impl, typename Context> |
| 57 void AddService(Context* context) { | 57 void AddService(Context* context) { |
| 58 AddServiceConnector(new internal::ServiceConnector<Impl, Context>(context)); | 58 AddServiceConnector(new internal::ServiceConnector<Impl, Context>(context)); |
| 59 } | 59 } |
| 60 | 60 |
| 61 template <typename Impl> | 61 template <typename Impl> |
| 62 void AddService() { | 62 void AddService() { |
| 63 AddServiceConnector(new internal::ServiceConnector<Impl, void>(NULL)); | 63 AddServiceConnector(new internal::ServiceConnector<Impl, void>(NULL)); |
| 64 } | 64 } |
| 65 | 65 |
| 66 template <typename Interface> | 66 template <typename Interface> |
| 67 void ConnectTo(const std::string& url, InterfacePtr<Interface>* ptr) { | 67 void ConnectTo(const std::string& url, |
| 68 mojo::ConnectTo(shell(), url, ptr); | 68 InterfacePtr<Interface>* ptr) { |
| 69 mojo::ConnectToService(service_provider(), url, ptr); |
| 69 } | 70 } |
| 70 | 71 |
| 71 protected: | 72 protected: |
| 72 // ShellClient methods. | 73 // ServiceProvider methods. |
| 73 // Override this to dispatch to correct service when there's more than one. | 74 // Override this to dispatch to correct service when there's more than one. |
| 74 // TODO(davemoore): Augment this with name registration. | 75 // TODO(davemoore): Augment this with name registration. |
| 75 virtual void AcceptConnection(const mojo::String& url, | 76 virtual void ConnectToService(const mojo::String& url, |
| 76 ScopedMessagePipeHandle client_handle) | 77 ScopedMessagePipeHandle client_handle) |
| 77 MOJO_OVERRIDE; | 78 MOJO_OVERRIDE; |
| 78 | 79 |
| 79 private: | 80 private: |
| 80 // internal::ServiceConnectorBase::Owner methods. | 81 // internal::ServiceConnectorBase::Owner methods. |
| 81 // Takes ownership of |service_connector|. | 82 // Takes ownership of |service_connector|. |
| 82 virtual void AddServiceConnector( | 83 virtual void AddServiceConnector( |
| 83 internal::ServiceConnectorBase* service_connector) MOJO_OVERRIDE; | 84 internal::ServiceConnectorBase* service_connector) MOJO_OVERRIDE; |
| 84 virtual void RemoveServiceConnector( | 85 virtual void RemoveServiceConnector( |
| 85 internal::ServiceConnectorBase* service_connector) MOJO_OVERRIDE; | 86 internal::ServiceConnectorBase* service_connector) MOJO_OVERRIDE; |
| 86 | 87 |
| 87 typedef std::vector<internal::ServiceConnectorBase*> ServiceConnectorList; | 88 typedef std::vector<internal::ServiceConnectorBase*> ServiceConnectorList; |
| 88 ServiceConnectorList service_connectors_; | 89 ServiceConnectorList service_connectors_; |
| 89 }; | 90 }; |
| 90 | 91 |
| 91 } // namespace mojo | 92 } // namespace mojo |
| 92 | 93 |
| 93 #endif // MOJO_PUBLIC_SHELL_APPLICATION_H_ | 94 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_H_ |
| OLD | NEW |