| 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 20 matching lines...) Expand all Loading... |
| 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 ApplicationImpl(ApplicationDelegate* delegate, |
| 60 InterfaceRequest<Application> request); | 61 InterfaceRequest<Application> request); |
| 61 // Constructs an ApplicationImpl with a custom termination closure. This | 62 // Constructs an ApplicationImpl with a custom termination closure. This |
| 62 // closure is invoked on Terminate() instead of the default behavior of | 63 // closure is invoked on Quit() instead of the default behavior of quitting |
| 63 // quitting the current MessageLoop. | 64 // the current base::MessageLoop. |
| 64 ApplicationImpl(ApplicationDelegate* delegate, | 65 ApplicationImpl(ApplicationDelegate* delegate, |
| 65 InterfaceRequest<Application> request, | 66 InterfaceRequest<Application> request, |
| 66 const Closure& termination_closure); | 67 const Closure& termination_closure); |
| 67 ~ApplicationImpl() override; | 68 ~ApplicationImpl() override; |
| 68 | 69 |
| 69 // The Mojo shell. This will return a valid pointer after Initialize() has | 70 // The Mojo shell. This will return a valid pointer after Initialize() has |
| 70 // been invoked. It will remain valid until UnbindConnections() is invoked or | 71 // been invoked. It will remain valid until UnbindConnections() is invoked or |
| 71 // the ApplicationImpl is destroyed. | 72 // the ApplicationImpl is destroyed. |
| 72 Shell* shell() const { return shell_.get(); } | 73 Shell* shell() const { return shell_.get(); } |
| 73 | 74 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 | 107 |
| 107 // Block until the Application is initialized, if it is not already. | 108 // Block until the Application is initialized, if it is not already. |
| 108 void WaitForInitialize(); | 109 void WaitForInitialize(); |
| 109 | 110 |
| 110 // Unbinds the Shell and Application connections. Can be used to re-bind the | 111 // Unbinds the Shell and Application connections. Can be used to re-bind the |
| 111 // handles to another implementation of ApplicationImpl, for instance when | 112 // handles to another implementation of ApplicationImpl, for instance when |
| 112 // running apptests. | 113 // running apptests. |
| 113 void UnbindConnections(InterfaceRequest<Application>* application_request, | 114 void UnbindConnections(InterfaceRequest<Application>* application_request, |
| 114 ShellPtr* shell); | 115 ShellPtr* shell); |
| 115 | 116 |
| 116 // Quits the main run loop for this application. It first checks with the | 117 // Initiate shutdown of this application. This may involve a round trip to the |
| 117 // shell to ensure there are no outstanding service requests. | 118 // Shell to ensure there are no inbound service requests. |
| 118 void Terminate(); | 119 void Quit(); |
| 119 | |
| 120 // Quits without waiting to check with the shell. | |
| 121 void QuitNow(); | |
| 122 | 120 |
| 123 private: | 121 private: |
| 124 // Application implementation. | 122 // Application implementation. |
| 125 void AcceptConnection(const String& requestor_url, | 123 void AcceptConnection(const String& requestor_url, |
| 126 InterfaceRequest<ServiceProvider> services, | 124 InterfaceRequest<ServiceProvider> services, |
| 127 ServiceProviderPtr exposed_services, | 125 ServiceProviderPtr exposed_services, |
| 128 Array<String> allowed_interfaces, | 126 Array<String> allowed_interfaces, |
| 129 const String& url) override; | 127 const String& url) override; |
| 130 void OnQuitRequested(const Callback<void(bool)>& callback) override; | 128 void OnQuitRequested(const Callback<void(bool)>& callback) override; |
| 131 | 129 |
| 132 void OnConnectionError(); | 130 void OnConnectionError(); |
| 133 | 131 |
| 134 void ClearConnections(); | 132 void ClearConnections(); |
| 135 | 133 |
| 134 // Called from Quit() when there is no Shell connection, or asynchronously |
| 135 // from Quit() once the Shell has OK'ed shutdown. |
| 136 void QuitNow(); |
| 137 |
| 136 typedef std::vector<internal::ServiceRegistry*> ServiceRegistryList; | 138 typedef std::vector<internal::ServiceRegistry*> ServiceRegistryList; |
| 137 | 139 |
| 138 ServiceRegistryList incoming_service_registries_; | 140 ServiceRegistryList incoming_service_registries_; |
| 139 ServiceRegistryList outgoing_service_registries_; | 141 ServiceRegistryList outgoing_service_registries_; |
| 140 ApplicationDelegate* delegate_; | 142 ApplicationDelegate* delegate_; |
| 141 Binding<Application> binding_; | 143 Binding<Application> binding_; |
| 142 ShellPtr shell_; | 144 ShellPtr shell_; |
| 143 std::string url_; | 145 std::string url_; |
| 144 Closure termination_closure_; | 146 Closure termination_closure_; |
| 145 AppLifetimeHelper app_lifetime_helper_; | 147 AppLifetimeHelper app_lifetime_helper_; |
| 146 bool quit_requested_; | 148 bool quit_requested_; |
| 147 bool in_destructor_; | 149 bool in_destructor_; |
| 148 base::WeakPtrFactory<ApplicationImpl> weak_factory_; | 150 base::WeakPtrFactory<ApplicationImpl> weak_factory_; |
| 149 | 151 |
| 150 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); | 152 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); |
| 151 }; | 153 }; |
| 152 | 154 |
| 153 } // namespace mojo | 155 } // namespace mojo |
| 154 | 156 |
| 155 #endif // MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_ | 157 #endif // MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_ |
| OLD | NEW |