| OLD | NEW |
| (Empty) |
| 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_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_ | |
| 6 #define MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_ | |
| 7 | |
| 8 #include <utility> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/macros.h" | |
| 12 #include "base/memory/scoped_vector.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "mojo/application/public/cpp/app_lifetime_helper.h" | |
| 15 #include "mojo/application/public/cpp/application_connection.h" | |
| 16 #include "mojo/application/public/cpp/application_delegate.h" | |
| 17 #include "mojo/application/public/cpp/lib/service_registry.h" | |
| 18 #include "mojo/application/public/interfaces/application.mojom.h" | |
| 19 #include "mojo/application/public/interfaces/shell.mojom.h" | |
| 20 #include "mojo/public/cpp/bindings/binding.h" | |
| 21 #include "mojo/public/cpp/bindings/callback.h" | |
| 22 #include "mojo/public/cpp/system/core.h" | |
| 23 | |
| 24 namespace mojo { | |
| 25 | |
| 26 CapabilityFilterPtr CreatePermissiveCapabilityFilter(); | |
| 27 | |
| 28 // TODO(beng): This comment is hilariously out of date. | |
| 29 // Utility class for communicating with the Shell, and providing Services | |
| 30 // to clients. | |
| 31 // | |
| 32 // To use define a class that implements your specific server api, e.g. FooImpl | |
| 33 // to implement a service named Foo. | |
| 34 // That class must subclass an InterfaceImpl specialization. | |
| 35 // | |
| 36 // If there is context that is to be shared amongst all instances, define a | |
| 37 // constructor with that class as its only argument, otherwise define an empty | |
| 38 // constructor. | |
| 39 // | |
| 40 // class FooImpl : public InterfaceImpl<Foo> { | |
| 41 // public: | |
| 42 // FooImpl(ApplicationContext* app_context) {} | |
| 43 // }; | |
| 44 // | |
| 45 // or | |
| 46 // | |
| 47 // class BarImpl : public InterfaceImpl<Bar> { | |
| 48 // public: | |
| 49 // // contexts will remain valid for the lifetime of BarImpl. | |
| 50 // BarImpl(ApplicationContext* app_context, BarContext* service_context) | |
| 51 // : app_context_(app_context), servicecontext_(context) {} | |
| 52 // | |
| 53 // Create an ApplicationImpl instance that collects any service implementations. | |
| 54 // | |
| 55 // ApplicationImpl app(service_provider_handle); | |
| 56 // app.AddService<FooImpl>(); | |
| 57 // | |
| 58 // BarContext context; | |
| 59 // app.AddService<BarImpl>(&context); | |
| 60 // | |
| 61 // | |
| 62 class ApplicationImpl : public Application { | |
| 63 public: | |
| 64 class ConnectParams { | |
| 65 public: | |
| 66 explicit ConnectParams(const std::string& url); | |
| 67 explicit ConnectParams(URLRequestPtr request); | |
| 68 ~ConnectParams(); | |
| 69 | |
| 70 URLRequestPtr TakeRequest() { return std::move(request_); } | |
| 71 CapabilityFilterPtr TakeFilter() { return std::move(filter_); } | |
| 72 void set_filter(CapabilityFilterPtr filter) { filter_ = std::move(filter); } | |
| 73 | |
| 74 private: | |
| 75 URLRequestPtr request_; | |
| 76 CapabilityFilterPtr filter_; | |
| 77 | |
| 78 DISALLOW_COPY_AND_ASSIGN(ConnectParams); | |
| 79 }; | |
| 80 | |
| 81 class TestApi { | |
| 82 public: | |
| 83 explicit TestApi(ApplicationImpl* application) | |
| 84 : application_(application) {} | |
| 85 | |
| 86 void UnbindConnections(InterfaceRequest<Application>* application_request, | |
| 87 ShellPtr* shell) { | |
| 88 application_->UnbindConnections(application_request, shell); | |
| 89 } | |
| 90 | |
| 91 private: | |
| 92 ApplicationImpl* application_; | |
| 93 }; | |
| 94 | |
| 95 // Does not take ownership of |delegate|, which must remain valid for the | |
| 96 // lifetime of ApplicationImpl. | |
| 97 ApplicationImpl(ApplicationDelegate* delegate, | |
| 98 InterfaceRequest<Application> request); | |
| 99 // Constructs an ApplicationImpl with a custom termination closure. This | |
| 100 // closure is invoked on Quit() instead of the default behavior of quitting | |
| 101 // the current base::MessageLoop. | |
| 102 ApplicationImpl(ApplicationDelegate* delegate, | |
| 103 InterfaceRequest<Application> request, | |
| 104 const Closure& termination_closure); | |
| 105 ~ApplicationImpl() override; | |
| 106 | |
| 107 // The Mojo shell. This will return a valid pointer after Initialize() has | |
| 108 // been invoked. It will remain valid until UnbindConnections() is invoked or | |
| 109 // the ApplicationImpl is destroyed. | |
| 110 Shell* shell() const { return shell_.get(); } | |
| 111 | |
| 112 const std::string& url() const { return url_; } | |
| 113 | |
| 114 AppLifetimeHelper* app_lifetime_helper() { return &app_lifetime_helper_; } | |
| 115 | |
| 116 // Requests a new connection to an application. Returns a pointer to the | |
| 117 // connection if the connection is permitted by this application's delegate, | |
| 118 // or nullptr otherwise. Caller takes ownership. | |
| 119 scoped_ptr<ApplicationConnection> ConnectToApplication( | |
| 120 const std::string& url); | |
| 121 scoped_ptr<ApplicationConnection> ConnectToApplication(ConnectParams* params); | |
| 122 | |
| 123 // Connect to application identified by |request->url| and connect to the | |
| 124 // service implementation of the interface identified by |Interface|. | |
| 125 template <typename Interface> | |
| 126 void ConnectToService(ConnectParams* params, InterfacePtr<Interface>* ptr) { | |
| 127 scoped_ptr<ApplicationConnection> connection = ConnectToApplication(params); | |
| 128 if (!connection.get()) | |
| 129 return; | |
| 130 connection->ConnectToService(ptr); | |
| 131 } | |
| 132 template <typename Interface> | |
| 133 void ConnectToService(const std::string& url, InterfacePtr<Interface>* ptr) { | |
| 134 ConnectParams params(url); | |
| 135 params.set_filter(CreatePermissiveCapabilityFilter()); | |
| 136 return ConnectToService(¶ms, ptr); | |
| 137 } | |
| 138 | |
| 139 // Block the calling thread until the Initialize() method is called by the | |
| 140 // shell. | |
| 141 void WaitForInitialize(); | |
| 142 | |
| 143 // Initiate shutdown of this application. This may involve a round trip to the | |
| 144 // Shell to ensure there are no inbound service requests. | |
| 145 void Quit(); | |
| 146 | |
| 147 private: | |
| 148 // Application implementation. | |
| 149 void Initialize(ShellPtr shell, const mojo::String& url) override; | |
| 150 void AcceptConnection(const String& requestor_url, | |
| 151 InterfaceRequest<ServiceProvider> services, | |
| 152 ServiceProviderPtr exposed_services, | |
| 153 Array<String> allowed_interfaces, | |
| 154 const String& url) override; | |
| 155 void OnQuitRequested(const Callback<void(bool)>& callback) override; | |
| 156 | |
| 157 void OnConnectionError(); | |
| 158 | |
| 159 // Called from Quit() when there is no Shell connection, or asynchronously | |
| 160 // from Quit() once the Shell has OK'ed shutdown. | |
| 161 void QuitNow(); | |
| 162 | |
| 163 // Unbinds the Shell and Application connections. Can be used to re-bind the | |
| 164 // handles to another implementation of ApplicationImpl, for instance when | |
| 165 // running apptests. | |
| 166 void UnbindConnections(InterfaceRequest<Application>* application_request, | |
| 167 ShellPtr* shell); | |
| 168 | |
| 169 // We track the lifetime of incoming connection registries as it more | |
| 170 // convenient for the client. | |
| 171 ScopedVector<ApplicationConnection> incoming_connections_; | |
| 172 ApplicationDelegate* delegate_; | |
| 173 Binding<Application> binding_; | |
| 174 ShellPtr shell_; | |
| 175 std::string url_; | |
| 176 Closure termination_closure_; | |
| 177 AppLifetimeHelper app_lifetime_helper_; | |
| 178 bool quit_requested_; | |
| 179 base::WeakPtrFactory<ApplicationImpl> weak_factory_; | |
| 180 | |
| 181 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); | |
| 182 }; | |
| 183 | |
| 184 } // namespace mojo | |
| 185 | |
| 186 #endif // MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_ | |
| OLD | NEW |