| 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_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_ | 5 #ifndef MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_ |
| 6 #define MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_ | 6 #define MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "mojo/application/public/cpp/app_lifetime_helper.h" | 11 #include "mojo/application/public/cpp/app_lifetime_helper.h" |
| 12 #include "mojo/application/public/cpp/application_connection.h" | 12 #include "mojo/application/public/cpp/application_connection.h" |
| 13 #include "mojo/application/public/cpp/application_delegate.h" | 13 #include "mojo/application/public/cpp/application_delegate.h" |
| 14 #include "mojo/application/public/cpp/lib/service_registry.h" | 14 #include "mojo/application/public/cpp/lib/service_registry.h" |
| 15 #include "mojo/application/public/interfaces/application.mojom.h" | 15 #include "mojo/application/public/interfaces/application.mojom.h" |
| 16 #include "mojo/application/public/interfaces/shell.mojom.h" | 16 #include "mojo/application/public/interfaces/shell.mojom.h" |
| 17 #include "mojo/public/cpp/bindings/callback.h" | 17 #include "mojo/public/cpp/bindings/callback.h" |
| 18 #include "mojo/public/cpp/system/core.h" | 18 #include "mojo/public/cpp/system/core.h" |
| 19 | 19 |
| 20 namespace mojo { | 20 namespace mojo { |
| 21 | 21 |
| 22 // TODO(beng): this comment is hilariously out of date. |
| 22 // Utility class for communicating with the Shell, and providing Services | 23 // Utility class for communicating with the Shell, and providing Services |
| 23 // to clients. | 24 // to clients. |
| 24 // | 25 // |
| 25 // To use define a class that implements your specific server api, e.g. FooImpl | 26 // To use define a class that implements your specific server api, e.g. FooImpl |
| 26 // to implement a service named Foo. | 27 // to implement a service named Foo. |
| 27 // That class must subclass an InterfaceImpl specialization. | 28 // That class must subclass an InterfaceImpl specialization. |
| 28 // | 29 // |
| 29 // If there is context that is to be shared amongst all instances, define a | 30 // If there is context that is to be shared amongst all instances, define a |
| 30 // constructor with that class as its only argument, otherwise define an empty | 31 // constructor with that class as its only argument, otherwise define an empty |
| 31 // constructor. | 32 // constructor. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 49 // app.AddService<FooImpl>(); | 50 // app.AddService<FooImpl>(); |
| 50 // | 51 // |
| 51 // BarContext context; | 52 // BarContext context; |
| 52 // app.AddService<BarImpl>(&context); | 53 // app.AddService<BarImpl>(&context); |
| 53 // | 54 // |
| 54 // | 55 // |
| 55 class ApplicationImpl : public Application { | 56 class ApplicationImpl : public Application { |
| 56 public: | 57 public: |
| 57 // Does not take ownership of |delegate|, which must remain valid for the | 58 // Does not take ownership of |delegate|, which must remain valid for the |
| 58 // lifetime of ApplicationImpl. | 59 // lifetime of ApplicationImpl. |
| 59 ApplicationImpl(ApplicationDelegate* delegate, | 60 // |termination_closure| is called after the connection with the shell is |
| 60 InterfaceRequest<Application> request); | 61 // broken. |
| 61 // Constructs an ApplicationImpl with a custom termination closure. This | |
| 62 // closure is invoked on Terminate() instead of the default behavior of | |
| 63 // quitting the current MessageLoop. | |
| 64 ApplicationImpl(ApplicationDelegate* delegate, | 62 ApplicationImpl(ApplicationDelegate* delegate, |
| 65 InterfaceRequest<Application> request, | 63 InterfaceRequest<Application> request, |
| 66 const Closure& termination_closure); | 64 const Closure& termination_closure); |
| 67 ~ApplicationImpl() override; | 65 ~ApplicationImpl() override; |
| 68 | 66 |
| 69 // The Mojo shell. This will return a valid pointer after Initialize() has | 67 // The Mojo shell. This will return a valid pointer after Initialize() has |
| 70 // been invoked. It will remain valid until UnbindConnections() is invoked or | 68 // been invoked. It will remain valid until UnbindConnections() is invoked or |
| 71 // the ApplicationImpl is destroyed. | 69 // the ApplicationImpl is destroyed. |
| 72 Shell* shell() const { return shell_.get(); } | 70 Shell* shell() const { return shell_.get(); } |
| 73 | 71 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 | 104 |
| 107 // Block until the Application is initialized, if it is not already. | 105 // Block until the Application is initialized, if it is not already. |
| 108 void WaitForInitialize(); | 106 void WaitForInitialize(); |
| 109 | 107 |
| 110 // Unbinds the Shell and Application connections. Can be used to re-bind the | 108 // Unbinds the Shell and Application connections. Can be used to re-bind the |
| 111 // handles to another implementation of ApplicationImpl, for instance when | 109 // handles to another implementation of ApplicationImpl, for instance when |
| 112 // running apptests. | 110 // running apptests. |
| 113 void UnbindConnections(InterfaceRequest<Application>* application_request, | 111 void UnbindConnections(InterfaceRequest<Application>* application_request, |
| 114 ShellPtr* shell); | 112 ShellPtr* shell); |
| 115 | 113 |
| 116 // Quits the main run loop for this application. It first checks with the | 114 // Initiate shutdown of this application. This may involve a round trip to the |
| 117 // shell to ensure there are no outstanding service requests. | 115 // Shell to ensure there are no inbound service requests. |
| 118 void Terminate(); | 116 void Quit(); |
| 119 | |
| 120 // Quits without waiting to check with the shell. | |
| 121 void QuitNow(); | |
| 122 | 117 |
| 123 private: | 118 private: |
| 124 // Application implementation. | 119 // Application implementation. |
| 125 void AcceptConnection(const String& requestor_url, | 120 void AcceptConnection(const String& requestor_url, |
| 126 InterfaceRequest<ServiceProvider> services, | 121 InterfaceRequest<ServiceProvider> services, |
| 127 ServiceProviderPtr exposed_services, | 122 ServiceProviderPtr exposed_services, |
| 128 Array<String> allowed_interfaces, | 123 Array<String> allowed_interfaces, |
| 129 const String& url) override; | 124 const String& url) override; |
| 130 void OnQuitRequested(const Callback<void(bool)>& callback) override; | 125 void OnQuitRequested(const Callback<void(bool)>& callback) override; |
| 131 | 126 |
| 132 void OnConnectionError(); | 127 void OnConnectionError(); |
| 133 | 128 |
| 134 void ClearConnections(); | 129 void ClearConnections(); |
| 135 | 130 |
| 131 // Called from Quit() when there is no Shell connection, or asynchronously |
| 132 // from Quit() once the Shell has OK'ed shutdown. |
| 133 void QuitNow(); |
| 134 |
| 136 typedef std::vector<internal::ServiceRegistry*> ServiceRegistryList; | 135 typedef std::vector<internal::ServiceRegistry*> ServiceRegistryList; |
| 137 | 136 |
| 138 ServiceRegistryList incoming_service_registries_; | 137 ServiceRegistryList incoming_service_registries_; |
| 139 ServiceRegistryList outgoing_service_registries_; | 138 ServiceRegistryList outgoing_service_registries_; |
| 140 ApplicationDelegate* delegate_; | 139 ApplicationDelegate* delegate_; |
| 141 Binding<Application> binding_; | 140 Binding<Application> binding_; |
| 142 ShellPtr shell_; | 141 ShellPtr shell_; |
| 143 std::string url_; | 142 std::string url_; |
| 144 Closure termination_closure_; | 143 Closure termination_closure_; |
| 145 AppLifetimeHelper app_lifetime_helper_; | 144 AppLifetimeHelper app_lifetime_helper_; |
| 146 bool quit_requested_; | 145 bool quit_requested_; |
| 147 bool in_destructor_; | 146 bool in_destructor_; |
| 148 base::WeakPtrFactory<ApplicationImpl> weak_factory_; | 147 base::WeakPtrFactory<ApplicationImpl> weak_factory_; |
| 149 | 148 |
| 150 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); | 149 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); |
| 151 }; | 150 }; |
| 152 | 151 |
| 153 } // namespace mojo | 152 } // namespace mojo |
| 154 | 153 |
| 155 #endif // MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_ | 154 #endif // MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_ |
| OLD | NEW |