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