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 #include "base/files/file_path.h" | 5 #include "base/files/file_path.h" |
6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
11 #include "mojo/common/common_type_converters.h" | 11 #include "mojo/common/common_type_converters.h" |
| 12 #include "mojo/public/cpp/application/application_delegate.h" |
| 13 #include "mojo/public/cpp/application/application_impl.h" |
| 14 #include "mojo/public/cpp/bindings/strong_binding.h" |
12 #include "mojo/public/interfaces/application/application.mojom.h" | 15 #include "mojo/public/interfaces/application/application.mojom.h" |
13 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 16 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
14 #include "mojo/public/interfaces/application/shell.mojom.h" | 17 #include "mojo/public/interfaces/application/shell.mojom.h" |
15 #include "shell/application_manager/application_loader.h" | 18 #include "shell/application_manager/application_loader.h" |
16 #include "shell/application_manager/application_manager.h" | 19 #include "shell/application_manager/application_manager.h" |
17 #include "shell/domain_socket/net_errors.h" | 20 #include "shell/domain_socket/net_errors.h" |
18 #include "shell/domain_socket/test_completion_callback.h" | 21 #include "shell/domain_socket/test_completion_callback.h" |
19 #include "shell/domain_socket/unix_domain_client_socket_posix.h" | 22 #include "shell/domain_socket/unix_domain_client_socket_posix.h" |
20 #include "shell/external_application_listener.h" | 23 #include "shell/external_application_listener.h" |
21 #include "shell/external_application_registrar.mojom.h" | 24 #include "shell/external_application_registrar.mojom.h" |
22 #include "shell/external_application_registrar_connection.h" | 25 #include "shell/external_application_registrar_connection.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "url/gurl.h" | 27 #include "url/gurl.h" |
25 | 28 |
26 namespace mojo { | 29 namespace mojo { |
27 namespace shell { | 30 namespace shell { |
28 | 31 |
29 class NotAnApplicationLoader : public ApplicationLoader { | 32 class NotAnApplicationLoader : public ApplicationLoader { |
30 public: | 33 public: |
31 NotAnApplicationLoader() {} | 34 NotAnApplicationLoader() {} |
32 ~NotAnApplicationLoader() override {} | 35 ~NotAnApplicationLoader() override {} |
33 | 36 |
34 void Load(ApplicationManager* application_manager, | 37 void Load(ApplicationManager* application_manager, |
35 const GURL& url, | 38 const GURL& url, |
36 ShellPtr shell, | 39 InterfaceRequest<Application> application_request, |
37 LoadCallback callback) override { | 40 LoadCallback callback) override { |
38 NOTREACHED(); | 41 NOTREACHED(); |
39 } | 42 } |
40 | 43 |
41 void OnApplicationError(ApplicationManager* manager, | 44 void OnApplicationError(ApplicationManager* manager, |
42 const GURL& url) override { | 45 const GURL& url) override { |
43 NOTREACHED(); | 46 NOTREACHED(); |
44 } | 47 } |
45 }; | 48 }; |
46 | 49 |
(...skipping 18 matching lines...) Expand all Loading... |
65 base::RunLoop run_loop_; | 68 base::RunLoop run_loop_; |
66 base::Thread io_thread_; | 69 base::Thread io_thread_; |
67 | 70 |
68 base::ScopedTempDir temp_dir_; | 71 base::ScopedTempDir temp_dir_; |
69 base::FilePath socket_path_; | 72 base::FilePath socket_path_; |
70 scoped_ptr<ExternalApplicationListener> listener_; | 73 scoped_ptr<ExternalApplicationListener> listener_; |
71 }; | 74 }; |
72 | 75 |
73 namespace { | 76 namespace { |
74 | 77 |
75 class StubShellImpl : public InterfaceImpl<Shell> { | 78 class StubShellImpl : public Shell { |
| 79 public: |
| 80 StubShellImpl(ApplicationPtr application) |
| 81 : application_(application.Pass()), binding_(this) { |
| 82 ShellPtr shell; |
| 83 binding_.Bind(GetProxy(&shell)); |
| 84 application_->Initialize(shell.Pass(), Array<String>()); |
| 85 } |
| 86 ~StubShellImpl() override {} |
| 87 |
76 private: | 88 private: |
77 void ConnectToApplication(const String& requestor_url, | 89 void ConnectToApplication(const String& requestor_url, |
78 InterfaceRequest<ServiceProvider> services, | 90 InterfaceRequest<ServiceProvider> services, |
79 ServiceProviderPtr exposed_services) override { | 91 ServiceProviderPtr exposed_services) override { |
80 client()->AcceptConnection(requestor_url, services.Pass(), | 92 application_->AcceptConnection(requestor_url, services.Pass(), |
81 exposed_services.Pass()); | 93 exposed_services.Pass()); |
82 } | 94 } |
| 95 |
| 96 ApplicationPtr application_; |
| 97 StrongBinding<Shell> binding_; |
83 }; | 98 }; |
84 | 99 |
85 void DoLocalRegister(const GURL& app_url, | 100 void DoLocalRegister(const GURL& app_url, |
86 const std::vector<std::string>& args, | 101 const std::vector<std::string>& args, |
87 ScopedMessagePipeHandle shell) { | 102 ApplicationPtr application) { |
88 BindToPipe(new StubShellImpl, shell.Pass()); | 103 new StubShellImpl(application.Pass()); |
89 } | 104 } |
90 | 105 |
91 void ConnectOnIOThread(const base::FilePath& socket_path, | 106 void ConnectOnIOThread(const base::FilePath& socket_path, |
92 scoped_refptr<base::TaskRunner> to_quit, | 107 scoped_refptr<base::TaskRunner> to_quit, |
93 base::Closure quit_callback) { | 108 base::Closure quit_callback) { |
94 ExternalApplicationRegistrarConnection connection(socket_path); | 109 ExternalApplicationRegistrarConnection connection(socket_path); |
95 EXPECT_TRUE(connection.Connect()); | 110 EXPECT_TRUE(connection.Connect()); |
96 to_quit->PostTask(FROM_HERE, quit_callback); | 111 to_quit->PostTask(FROM_HERE, quit_callback); |
97 } | 112 } |
98 | 113 |
99 } // namespace | 114 } // namespace |
100 | 115 |
101 TEST_F(ExternalApplicationListenerTest, ConnectConnection) { | 116 TEST_F(ExternalApplicationListenerTest, ConnectConnection) { |
102 listener_->ListenInBackground(socket_path_, base::Bind(&DoLocalRegister)); | 117 listener_->ListenInBackground(socket_path_, base::Bind(&DoLocalRegister)); |
103 listener_->WaitForListening(); | 118 listener_->WaitForListening(); |
104 io_thread_.task_runner()->PostTask( | 119 io_thread_.task_runner()->PostTask( |
105 FROM_HERE, base::Bind(&ConnectOnIOThread, socket_path_, | 120 FROM_HERE, base::Bind(&ConnectOnIOThread, socket_path_, |
106 loop_.task_runner(), run_loop_.QuitClosure())); | 121 loop_.task_runner(), run_loop_.QuitClosure())); |
107 run_loop_.Run(); | 122 run_loop_.Run(); |
108 } | 123 } |
109 | 124 |
110 namespace { | 125 namespace { |
111 | 126 |
112 class QuitLoopOnConnectApplicationImpl : public InterfaceImpl<Application> { | 127 class QuitLoopOnConnectApp : public ApplicationDelegate { |
113 public: | 128 public: |
114 QuitLoopOnConnectApplicationImpl(const std::string& url, | 129 QuitLoopOnConnectApp(const std::string& url, |
115 scoped_refptr<base::TaskRunner> loop, | 130 scoped_refptr<base::TaskRunner> loop, |
116 base::Closure quit_callback) | 131 base::Closure quit_callback) |
117 : url_(url), to_quit_(loop), quit_callback_(quit_callback) {} | 132 : url_(url), to_quit_(loop), quit_callback_(quit_callback) {} |
118 | 133 |
119 private: | 134 private: |
120 void Initialize(Array<String> args) override {} | 135 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { |
121 | 136 DVLOG(1) << url_ << " accepting connection from " |
122 void AcceptConnection(const String& requestor_url, | 137 << connection->GetRemoteApplicationURL(); |
123 InterfaceRequest<ServiceProvider> services, | |
124 ServiceProviderPtr exposed_services) override { | |
125 DVLOG(1) << url_ << " accepting connection from " << requestor_url; | |
126 to_quit_->PostTask(FROM_HERE, quit_callback_); | 138 to_quit_->PostTask(FROM_HERE, quit_callback_); |
127 } | 139 return true; |
128 | |
129 void RequestQuit() override { | |
130 } | 140 } |
131 | 141 |
132 const std::string url_; | 142 const std::string url_; |
133 scoped_refptr<base::TaskRunner> to_quit_; | 143 scoped_refptr<base::TaskRunner> to_quit_; |
134 base::Closure quit_callback_; | 144 base::Closure quit_callback_; |
135 }; | 145 }; |
136 | 146 |
137 class FakeExternalApplication { | 147 class FakeExternalApplication { |
138 public: | 148 public: |
139 FakeExternalApplication(const std::string& url) : url_(url) {} | 149 explicit FakeExternalApplication(const std::string& url) : url_(url) {} |
140 | 150 |
141 void ConnectSynchronously(const base::FilePath& socket_path) { | 151 void ConnectSynchronously(const base::FilePath& socket_path) { |
142 connection_.reset(new ExternalApplicationRegistrarConnection(socket_path)); | 152 connection_.reset(new ExternalApplicationRegistrarConnection(socket_path)); |
143 EXPECT_TRUE(connection_->Connect()); | 153 EXPECT_TRUE(connection_->Connect()); |
144 } | 154 } |
145 | 155 |
146 // application_impl is the the actual implementation to be registered. | 156 // application_delegate is the the actual implementation to be registered. |
147 void Register(scoped_ptr<InterfaceImpl<Application>> application_impl, | 157 void Register(scoped_ptr<ApplicationDelegate> application_delegate, |
148 base::Closure register_complete_callback) { | 158 const base::Closure& register_complete_callback) { |
149 connection_->Register( | 159 connection_->Register( |
150 GURL(url_), std::vector<std::string>(), | 160 GURL(url_), std::vector<std::string>(), |
151 base::Bind(&FakeExternalApplication::OnRegister, base::Unretained(this), | 161 base::Bind(&FakeExternalApplication::OnRegister, base::Unretained(this), |
152 register_complete_callback)); | 162 register_complete_callback)); |
153 application_impl_ = application_impl.Pass(); | 163 application_delegate_ = application_delegate.Pass(); |
154 } | 164 } |
155 | 165 |
156 void ConnectToAppByUrl(std::string app_url) { | 166 void ConnectToAppByUrl(std::string app_url) { |
157 ServiceProviderPtr sp; | 167 ServiceProviderPtr sp; |
158 ptr_->ConnectToApplication(app_url, GetProxy(&sp), nullptr); | 168 application_impl_->WaitForInitialize(); |
| 169 application_impl_->shell()->ConnectToApplication(app_url, GetProxy(&sp), |
| 170 nullptr); |
159 } | 171 } |
160 | 172 |
161 const std::string& url() { return url_; } | 173 const std::string& url() { return url_; } |
162 | 174 |
163 private: | 175 private: |
164 void OnRegister(base::Closure complete_callback, ShellPtr shell) { | 176 void OnRegister(const base::Closure& callback, |
165 ptr_ = shell.Pass(); | 177 InterfaceRequest<Application> application_request) { |
166 ptr_.set_client(application_impl_.get()); | 178 application_impl_.reset(new ApplicationImpl(application_delegate_.get(), |
167 complete_callback.Run(); | 179 application_request.Pass())); |
| 180 callback.Run(); |
168 } | 181 } |
169 | 182 |
170 const std::string url_; | 183 const std::string url_; |
171 scoped_ptr<InterfaceImpl<Application>> application_impl_; | 184 scoped_ptr<ApplicationDelegate> application_delegate_; |
172 ShellPtr ptr_; | 185 scoped_ptr<ApplicationImpl> application_impl_; |
173 | 186 |
174 scoped_ptr<ExternalApplicationRegistrarConnection> connection_; | 187 scoped_ptr<ExternalApplicationRegistrarConnection> connection_; |
175 }; | 188 }; |
176 | 189 |
177 void ConnectToApp(FakeExternalApplication* connector, | 190 void ConnectToApp(FakeExternalApplication* connector, |
178 FakeExternalApplication* connectee) { | 191 FakeExternalApplication* connectee) { |
179 connector->ConnectToAppByUrl(connectee->url()); | 192 connector->ConnectToAppByUrl(connectee->url()); |
180 } | 193 } |
181 | 194 |
182 void NoOp() { | |
183 } | |
184 | |
185 void ConnectAndRegisterOnIOThread(const base::FilePath& socket_path, | 195 void ConnectAndRegisterOnIOThread(const base::FilePath& socket_path, |
186 scoped_refptr<base::TaskRunner> loop, | 196 scoped_refptr<base::TaskRunner> loop, |
187 base::Closure quit_callback, | 197 base::Closure quit_callback, |
188 FakeExternalApplication* connector, | 198 FakeExternalApplication* connector, |
189 FakeExternalApplication* connectee) { | 199 FakeExternalApplication* connectee) { |
190 // Connect the first app to the registrar. | 200 // Connect the first app to the registrar. |
191 connector->ConnectSynchronously(socket_path); | 201 connector->ConnectSynchronously(socket_path); |
192 // connector will use this implementation of the Mojo Application interface | 202 // connector will use this implementation of the Mojo Application interface |
193 // once registration complete. | 203 // once registration complete. |
194 scoped_ptr<QuitLoopOnConnectApplicationImpl> connector_app_impl( | 204 scoped_ptr<QuitLoopOnConnectApp> connector_app( |
195 new QuitLoopOnConnectApplicationImpl(connector->url(), loop, | 205 new QuitLoopOnConnectApp(connector->url(), loop, quit_callback)); |
196 quit_callback)); | 206 |
197 // Since connectee won't be ready when connector is done registering, pass | 207 // Since connectee won't be ready when connector is done registering, pass |
198 // in a do-nothing callback. | 208 // in a do-nothing callback. |
199 connector->Register(connector_app_impl.Pass(), base::Bind(&NoOp)); | 209 connector->Register(connector_app.Pass(), base::Bind(&base::DoNothing)); |
200 | 210 |
201 // Connect the second app to the registrar. | 211 // Connect the second app to the registrar. |
202 connectee->ConnectSynchronously(socket_path); | 212 connectee->ConnectSynchronously(socket_path); |
203 scoped_ptr<QuitLoopOnConnectApplicationImpl> connectee_app_impl( | 213 |
204 new QuitLoopOnConnectApplicationImpl(connectee->url(), loop, | 214 scoped_ptr<QuitLoopOnConnectApp> connectee_app( |
205 quit_callback)); | 215 new QuitLoopOnConnectApp(connectee->url(), loop, quit_callback)); |
206 // After connectee is successfully registered, connector should be | 216 // After connectee is successfully registered, connector should be |
207 // able to connect to is by URL. Pass in a callback to attempt the | 217 // able to connect to is by URL. Pass in a callback to attempt the |
208 // app -> app connection. | 218 // app -> app connection. |
209 connectee->Register(connectee_app_impl.Pass(), | 219 connectee->Register(connectee_app.Pass(), |
210 base::Bind(&ConnectToApp, connector, connectee)); | 220 base::Bind(&ConnectToApp, connector, connectee)); |
211 } | 221 } |
212 | 222 |
213 void DestroyOnIOThread(scoped_ptr<FakeExternalApplication> doomed1, | 223 void DestroyOnIOThread(scoped_ptr<FakeExternalApplication> doomed1, |
214 scoped_ptr<FakeExternalApplication> doomed2) { | 224 scoped_ptr<FakeExternalApplication> doomed2) { |
215 } | 225 } |
216 | 226 |
217 } // namespace | 227 } // namespace |
218 | 228 |
219 // Create two external applications, have them discover and connect to | 229 // Create two external applications, have them discover and connect to |
(...skipping 23 matching lines...) Expand all Loading... |
243 run_loop_.Run(); | 253 run_loop_.Run(); |
244 | 254 |
245 // The apps need to be destroyed on the thread where they did socket stuff. | 255 // The apps need to be destroyed on the thread where they did socket stuff. |
246 io_thread_.task_runner()->PostTask( | 256 io_thread_.task_runner()->PostTask( |
247 FROM_HERE, base::Bind(&DestroyOnIOThread, base::Passed(&supersweet_app), | 257 FROM_HERE, base::Bind(&DestroyOnIOThread, base::Passed(&supersweet_app), |
248 base::Passed(&awesome_app))); | 258 base::Passed(&awesome_app))); |
249 } | 259 } |
250 | 260 |
251 } // namespace shell | 261 } // namespace shell |
252 } // namespace mojo | 262 } // namespace mojo |
OLD | NEW |