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