davemoore@chromium.org | 15f4ce7 | 2014-06-26 06:57:02 +0900 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_ |
| 6 | #define MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_ |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "mojo/public/cpp/application/application_connection.h" |
| 10 | #include "mojo/public/cpp/application/lib/service_connector.h" |
| 11 | #include "mojo/public/cpp/application/lib/service_registry.h" |
| 12 | #include "mojo/public/cpp/system/core.h" |
darin@chromium.org | f5e5151 | 2014-07-31 17:58:42 +0900 | [diff] [blame] | 13 | #include "mojo/public/interfaces/application/application.mojom.h" |
| 14 | #include "mojo/public/interfaces/application/shell.mojom.h" |
davemoore@chromium.org | 15f4ce7 | 2014-06-26 06:57:02 +0900 | [diff] [blame] | 15 | |
davemoore@chromium.org | 15f4ce7 | 2014-06-26 06:57:02 +0900 | [diff] [blame] | 16 | namespace mojo { |
| 17 | |
| 18 | class ApplicationDelegate; |
| 19 | |
| 20 | // Utility class for communicating with the Shell, and providing Services |
| 21 | // to clients. |
| 22 | // |
| 23 | // To use define a class that implements your specific server api, e.g. FooImpl |
| 24 | // to implement a service named Foo. |
| 25 | // That class must subclass an InterfaceImpl specialization. |
| 26 | // |
| 27 | // If there is context that is to be shared amongst all instances, define a |
| 28 | // constructor with that class as its only argument, otherwise define an empty |
| 29 | // constructor. |
| 30 | // |
| 31 | // class FooImpl : public InterfaceImpl<Foo> { |
| 32 | // public: |
| 33 | // FooImpl(ApplicationContext* app_context) {} |
| 34 | // }; |
| 35 | // |
| 36 | // or |
| 37 | // |
| 38 | // class BarImpl : public InterfaceImpl<Bar> { |
| 39 | // public: |
| 40 | // // contexts will remain valid for the lifetime of BarImpl. |
| 41 | // BarImpl(ApplicationContext* app_context, BarContext* service_context) |
| 42 | // : app_context_(app_context), servicecontext_(context) {} |
| 43 | // |
tim@chromium.org | 82f179c | 2014-08-07 08:30:48 +0900 | [diff] [blame] | 44 | // Create an ApplicationImpl instance that collects any service implementations. |
davemoore@chromium.org | 15f4ce7 | 2014-06-26 06:57:02 +0900 | [diff] [blame] | 45 | // |
| 46 | // ApplicationImpl app(service_provider_handle); |
| 47 | // app.AddService<FooImpl>(); |
| 48 | // |
| 49 | // BarContext context; |
| 50 | // app.AddService<BarImpl>(&context); |
| 51 | // |
| 52 | // |
| 53 | class ApplicationImpl : public InterfaceImpl<Application> { |
| 54 | public: |
davemoore@chromium.org | 15f4ce7 | 2014-06-26 06:57:02 +0900 | [diff] [blame] | 55 | ApplicationImpl(ApplicationDelegate* delegate, |
| 56 | ScopedMessagePipeHandle shell_handle); |
| 57 | ApplicationImpl(ApplicationDelegate* delegate, |
| 58 | MojoHandle shell_handle); |
| 59 | virtual ~ApplicationImpl(); |
| 60 | |
aa | f7e3f68 | 2014-09-05 14:17:28 +0900 | [diff] [blame] | 61 | Shell* shell() const { return shell_.get(); } |
| 62 | |
davemoore | 4838f76 | 2014-09-20 08:55:33 +0900 | [diff] [blame] | 63 | // Returns any initial configuration arguments, passed by the Shell. |
| 64 | const Array<String>& args() { return args_; } |
| 65 | |
davemoore@chromium.org | 15f4ce7 | 2014-06-26 06:57:02 +0900 | [diff] [blame] | 66 | // Establishes a new connection to an application. Caller does not own. |
| 67 | ApplicationConnection* ConnectToApplication(const String& application_url); |
| 68 | |
darin@chromium.org | b1ebb4c | 2014-07-29 17:12:35 +0900 | [diff] [blame] | 69 | // Connect to application identified by |application_url| and connect to the |
| 70 | // service implementation of the interface identified by |Interface|. |
davemoore@chromium.org | 15f4ce7 | 2014-06-26 06:57:02 +0900 | [diff] [blame] | 71 | template <typename Interface> |
| 72 | void ConnectToService(const std::string& application_url, |
| 73 | InterfacePtr<Interface>* ptr) { |
| 74 | ConnectToApplication(application_url)->ConnectToService(ptr); |
| 75 | } |
| 76 | |
| 77 | private: |
davemoore | 9e945d7 | 2014-09-03 06:23:11 +0900 | [diff] [blame] | 78 | class ShellPtrWatcher; |
tim@chromium.org | 82f179c | 2014-08-07 08:30:48 +0900 | [diff] [blame] | 79 | |
davemoore@chromium.org | 15f4ce7 | 2014-06-26 06:57:02 +0900 | [diff] [blame] | 80 | void BindShell(ScopedMessagePipeHandle shell_handle); |
tim@chromium.org | 82f179c | 2014-08-07 08:30:48 +0900 | [diff] [blame] | 81 | void ClearConnections(); |
davemoore | 8048f09 | 2014-08-26 03:33:56 +0900 | [diff] [blame] | 82 | void OnShellError() { |
| 83 | ClearConnections(); |
| 84 | Terminate(); |
| 85 | }; |
tim@chromium.org | 82f179c | 2014-08-07 08:30:48 +0900 | [diff] [blame] | 86 | |
| 87 | // Quits the main run loop for this application. |
tim@chromium.org | 1e608da | 2014-08-21 19:46:21 +0900 | [diff] [blame] | 88 | static void Terminate(); |
davemoore@chromium.org | 15f4ce7 | 2014-06-26 06:57:02 +0900 | [diff] [blame] | 89 | |
| 90 | // Application implementation. |
Viet-Trung Luu | 7b62449 | 2014-09-30 05:50:24 +0900 | [diff] [blame^] | 91 | virtual void Initialize(Array<String> args) override; |
davemoore@chromium.org | 15f4ce7 | 2014-06-26 06:57:02 +0900 | [diff] [blame] | 92 | virtual void AcceptConnection(const String& requestor_url, |
Viet-Trung Luu | 7b62449 | 2014-09-30 05:50:24 +0900 | [diff] [blame^] | 93 | ServiceProviderPtr provider) override; |
davemoore@chromium.org | 15f4ce7 | 2014-06-26 06:57:02 +0900 | [diff] [blame] | 94 | |
| 95 | typedef std::vector<internal::ServiceRegistry*> ServiceRegistryList; |
davemoore | 4838f76 | 2014-09-20 08:55:33 +0900 | [diff] [blame] | 96 | |
| 97 | bool initialized_; |
davemoore@chromium.org | 15f4ce7 | 2014-06-26 06:57:02 +0900 | [diff] [blame] | 98 | ServiceRegistryList incoming_service_registries_; |
| 99 | ServiceRegistryList outgoing_service_registries_; |
| 100 | ApplicationDelegate* delegate_; |
| 101 | ShellPtr shell_; |
davemoore | 9e945d7 | 2014-09-03 06:23:11 +0900 | [diff] [blame] | 102 | ShellPtrWatcher* shell_watch_; |
davemoore | 4838f76 | 2014-09-20 08:55:33 +0900 | [diff] [blame] | 103 | Array<String> args_; |
davemoore@chromium.org | 15f4ce7 | 2014-06-26 06:57:02 +0900 | [diff] [blame] | 104 | |
| 105 | MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); |
| 106 | }; |
| 107 | |
| 108 | } // namespace mojo |
| 109 | |
| 110 | #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_ |