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/cpp/shell.h" |
21 #include "mojo/shell/public/interfaces/application.mojom.h" | 22 #include "mojo/shell/public/interfaces/application.mojom.h" |
22 #include "mojo/shell/public/interfaces/shell.mojom.h" | 23 #include "mojo/shell/public/interfaces/shell.mojom.h" |
23 | 24 |
24 namespace mojo { | 25 namespace mojo { |
25 | 26 |
26 shell::mojom::CapabilityFilterPtr CreatePermissiveCapabilityFilter(); | |
27 | |
28 using ApplicationRequest = InterfaceRequest<shell::mojom::Application>; | |
29 | |
30 // TODO(beng): This comment is hilariously out of date. | 27 // TODO(beng): This comment is hilariously out of date. |
31 // Utility class for communicating with the Shell, and providing Services | 28 // Utility class for communicating with the Shell, and providing Services |
32 // to clients. | 29 // to clients. |
33 // | 30 // |
34 // To use define a class that implements your specific server api, e.g. FooImpl | 31 // To use define a class that implements your specific server api, e.g. FooImpl |
35 // to implement a service named Foo. | 32 // to implement a service named Foo. |
36 // That class must subclass an InterfaceImpl specialization. | 33 // That class must subclass an InterfaceImpl specialization. |
37 // | 34 // |
38 // If there is context that is to be shared amongst all instances, define a | 35 // If there is context that is to be shared amongst all instances, define a |
39 // constructor with that class as its only argument, otherwise define an empty | 36 // constructor with that class as its only argument, otherwise define an empty |
(...skipping 14 matching lines...) Expand all Loading... |
54 // | 51 // |
55 // Create an ApplicationImpl instance that collects any service implementations. | 52 // Create an ApplicationImpl instance that collects any service implementations. |
56 // | 53 // |
57 // ApplicationImpl app(service_provider_handle); | 54 // ApplicationImpl app(service_provider_handle); |
58 // app.AddService<FooImpl>(); | 55 // app.AddService<FooImpl>(); |
59 // | 56 // |
60 // BarContext context; | 57 // BarContext context; |
61 // app.AddService<BarImpl>(&context); | 58 // app.AddService<BarImpl>(&context); |
62 // | 59 // |
63 // | 60 // |
64 class ApplicationImpl : public shell::mojom::Application { | 61 class ApplicationImpl : public Shell, public shell::mojom::Application { |
65 public: | 62 public: |
66 class ConnectParams { | |
67 public: | |
68 explicit ConnectParams(const std::string& url); | |
69 explicit ConnectParams(URLRequestPtr request); | |
70 ~ConnectParams(); | |
71 | |
72 URLRequestPtr TakeRequest() { return std::move(request_); } | |
73 shell::mojom::CapabilityFilterPtr TakeFilter() { | |
74 return std::move(filter_); | |
75 } | |
76 void set_filter(shell::mojom::CapabilityFilterPtr filter) { | |
77 filter_ = std::move(filter); | |
78 } | |
79 | |
80 private: | |
81 URLRequestPtr request_; | |
82 shell::mojom::CapabilityFilterPtr filter_; | |
83 | |
84 DISALLOW_COPY_AND_ASSIGN(ConnectParams); | |
85 }; | |
86 | |
87 class TestApi { | 63 class TestApi { |
88 public: | 64 public: |
89 explicit TestApi(ApplicationImpl* application) | 65 explicit TestApi(ApplicationImpl* application) |
90 : application_(application) {} | 66 : application_(application) {} |
91 | 67 |
92 void UnbindConnections( | 68 void UnbindConnections( |
93 InterfaceRequest<shell::mojom::Application>* application_request, | 69 InterfaceRequest<shell::mojom::Application>* application_request, |
94 shell::mojom::ShellPtr* shell) { | 70 shell::mojom::ShellPtr* shell) { |
95 application_->UnbindConnections(application_request, shell); | 71 application_->UnbindConnections(application_request, shell); |
96 } | 72 } |
(...skipping 12 matching lines...) Expand all Loading... |
109 ApplicationImpl(ApplicationDelegate* delegate, | 85 ApplicationImpl(ApplicationDelegate* delegate, |
110 InterfaceRequest<shell::mojom::Application> request, | 86 InterfaceRequest<shell::mojom::Application> request, |
111 const Closure& termination_closure); | 87 const Closure& termination_closure); |
112 ~ApplicationImpl() override; | 88 ~ApplicationImpl() override; |
113 | 89 |
114 // The Mojo shell. This will return a valid pointer after Initialize() has | 90 // The Mojo shell. This will return a valid pointer after Initialize() has |
115 // been invoked. It will remain valid until UnbindConnections() is invoked or | 91 // been invoked. It will remain valid until UnbindConnections() is invoked or |
116 // the ApplicationImpl is destroyed. | 92 // the ApplicationImpl is destroyed. |
117 shell::mojom::Shell* shell() const { return shell_.get(); } | 93 shell::mojom::Shell* shell() const { return shell_.get(); } |
118 | 94 |
119 const std::string& url() const { return url_; } | |
120 uint32_t id() const { return id_; } | |
121 | |
122 AppLifetimeHelper* app_lifetime_helper() { return &app_lifetime_helper_; } | |
123 | |
124 // Requests a new connection to an application. Returns a pointer to the | |
125 // connection if the connection is permitted by this application's delegate, | |
126 // or nullptr otherwise. Caller takes ownership. | |
127 scoped_ptr<ApplicationConnection> ConnectToApplication( | |
128 const std::string& url); | |
129 scoped_ptr<ApplicationConnection> ConnectToApplication(ConnectParams* params); | |
130 | |
131 // Connect to application identified by |request->url| and connect to the | |
132 // service implementation of the interface identified by |Interface|. | |
133 template <typename Interface> | |
134 void ConnectToService(ConnectParams* params, InterfacePtr<Interface>* ptr) { | |
135 scoped_ptr<ApplicationConnection> connection = ConnectToApplication(params); | |
136 if (!connection.get()) | |
137 return; | |
138 connection->ConnectToService(ptr); | |
139 } | |
140 template <typename Interface> | |
141 void ConnectToService(const std::string& url, InterfacePtr<Interface>* ptr) { | |
142 ConnectParams params(url); | |
143 params.set_filter(CreatePermissiveCapabilityFilter()); | |
144 return ConnectToService(¶ms, ptr); | |
145 } | |
146 | |
147 // Block the calling thread until the Initialize() method is called by the | 95 // Block the calling thread until the Initialize() method is called by the |
148 // shell. | 96 // shell. |
149 void WaitForInitialize(); | 97 void WaitForInitialize(); |
150 | 98 |
151 // Initiate shutdown of this application. This may involve a round trip to the | 99 // Shell. |
152 // Shell to ensure there are no inbound service requests. | 100 scoped_ptr<ApplicationConnection> ConnectToApplication( |
153 void Quit(); | 101 const std::string& url) override; |
| 102 scoped_ptr<ApplicationConnection> ConnectToApplication( |
| 103 ConnectParams* params) override; |
| 104 void Quit() override; |
| 105 scoped_ptr<AppRefCount> CreateAppRefCount() override; |
154 | 106 |
155 private: | 107 private: |
156 // shell::mojom::Application implementation. | 108 // shell::mojom::Application implementation. |
157 void Initialize(shell::mojom::ShellPtr shell, | 109 void Initialize(shell::mojom::ShellPtr shell, |
158 const mojo::String& url, | 110 const mojo::String& url, |
159 uint32_t id) override; | 111 uint32_t id) override; |
160 void AcceptConnection(const String& requestor_url, | 112 void AcceptConnection(const String& requestor_url, |
161 uint32_t requestor_id, | 113 uint32_t requestor_id, |
162 InterfaceRequest<ServiceProvider> services, | 114 InterfaceRequest<ServiceProvider> services, |
163 ServiceProviderPtr exposed_services, | 115 ServiceProviderPtr exposed_services, |
(...skipping 13 matching lines...) Expand all Loading... |
177 void UnbindConnections( | 129 void UnbindConnections( |
178 InterfaceRequest<shell::mojom::Application>* application_request, | 130 InterfaceRequest<shell::mojom::Application>* application_request, |
179 shell::mojom::ShellPtr* shell); | 131 shell::mojom::ShellPtr* shell); |
180 | 132 |
181 // We track the lifetime of incoming connection registries as it more | 133 // We track the lifetime of incoming connection registries as it more |
182 // convenient for the client. | 134 // convenient for the client. |
183 ScopedVector<ApplicationConnection> incoming_connections_; | 135 ScopedVector<ApplicationConnection> incoming_connections_; |
184 ApplicationDelegate* delegate_; | 136 ApplicationDelegate* delegate_; |
185 Binding<shell::mojom::Application> binding_; | 137 Binding<shell::mojom::Application> binding_; |
186 shell::mojom::ShellPtr shell_; | 138 shell::mojom::ShellPtr shell_; |
187 std::string url_; | |
188 uint32_t id_; | |
189 Closure termination_closure_; | 139 Closure termination_closure_; |
190 AppLifetimeHelper app_lifetime_helper_; | 140 AppLifetimeHelper app_lifetime_helper_; |
191 bool quit_requested_; | 141 bool quit_requested_; |
192 base::WeakPtrFactory<ApplicationImpl> weak_factory_; | 142 base::WeakPtrFactory<ApplicationImpl> weak_factory_; |
193 | 143 |
194 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); | 144 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); |
195 }; | 145 }; |
196 | 146 |
197 } // namespace mojo | 147 } // namespace mojo |
198 | 148 |
199 #endif // MOJO_SHELL_PUBLIC_CPP_APPLICATION_IMPL_H_ | 149 #endif // MOJO_SHELL_PUBLIC_CPP_APPLICATION_IMPL_H_ |
OLD | NEW |