Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(440)

Side by Side Diff: content/browser/shared_worker/shared_worker_service_impl_unittest.cc

Issue 2600113003: (SUSPENDED) SharedWorker: Mojofy Renderer(Document)->Browser communication for SharedWorker
Patch Set: address review comments Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "content/browser/shared_worker/shared_worker_service_impl.h" 5 #include "content/browser/shared_worker/shared_worker_service_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <map> 9 #include <map>
10 #include <memory> 10 #include <memory>
(...skipping 15 matching lines...) Expand all
26 #include "content/common/worker_messages.h" 26 #include "content/common/worker_messages.h"
27 #include "content/public/browser/storage_partition.h" 27 #include "content/public/browser/storage_partition.h"
28 #include "content/public/test/test_browser_context.h" 28 #include "content/public/test/test_browser_context.h"
29 #include "content/public/test/test_browser_thread_bundle.h" 29 #include "content/public/test/test_browser_thread_bundle.h"
30 #include "content/public/test/test_utils.h" 30 #include "content/public/test/test_utils.h"
31 #include "ipc/ipc_sync_message.h" 31 #include "ipc/ipc_sync_message.h"
32 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
33 33
34 namespace content { 34 namespace content {
35 35
36 namespace {
37
38 void CreateWorkerCallback(int* route_id_out,
39 blink::WebWorkerCreationError* creation_error_out,
40 int route_id,
41 blink::WebWorkerCreationError creation_error) {
42 *route_id_out = route_id;
43 *creation_error_out = creation_error;
44 }
45
46 } // namespace
47
36 class SharedWorkerServiceImplTest : public testing::Test { 48 class SharedWorkerServiceImplTest : public testing::Test {
37 public: 49 public:
38 static void RegisterRunningProcessID(int process_id) { 50 static void RegisterRunningProcessID(int process_id) {
39 base::AutoLock lock(s_lock_); 51 base::AutoLock lock(s_lock_);
40 s_running_process_id_set_.insert(process_id); 52 s_running_process_id_set_.insert(process_id);
41 } 53 }
42 static void UnregisterRunningProcessID(int process_id) { 54 static void UnregisterRunningProcessID(int process_id) {
43 base::AutoLock lock(s_lock_); 55 base::AutoLock lock(s_lock_);
44 s_running_process_id_set_.erase(process_id); 56 s_running_process_id_set_.erase(process_id);
45 } 57 }
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 queued_messages_.pop_back(); 216 queued_messages_.pop_back();
205 IPC::SyncMessage* sync_msg = static_cast<IPC::SyncMessage*>(message); 217 IPC::SyncMessage* sync_msg = static_cast<IPC::SyncMessage*>(message);
206 std::unique_ptr<IPC::MessageReplyDeserializer> reply_serializer( 218 std::unique_ptr<IPC::MessageReplyDeserializer> reply_serializer(
207 sync_msg->GetReplyDeserializer()); 219 sync_msg->GetReplyDeserializer());
208 bool result = reply_serializer->SerializeOutputParameters(*response_msg); 220 bool result = reply_serializer->SerializeOutputParameters(*response_msg);
209 CHECK(result); 221 CHECK(result);
210 } 222 }
211 return ret; 223 return ret;
212 } 224 }
213 225
226 // Emulates mojo RPC calls (Renderer(Document)->Browser).
227 void OnCreateWorker(mojom::SharedWorkerCreateParamsPtr params,
228 int* route_id_out,
229 blink::WebWorkerCreationError* creation_error_out) {
230 worker_filter_->OnCreateWorker(
231 std::move(params),
232 base::Bind(&CreateWorkerCallback, route_id_out, creation_error_out));
233 }
234
235 void OnConnectToWorker(int route_id, int message_port_id) {
236 worker_filter_->OnConnectToWorker(route_id, message_port_id);
237 }
238
214 size_t QueuedMessageCount() const { return queued_messages_.size(); } 239 size_t QueuedMessageCount() const { return queued_messages_.size(); }
215 240
216 std::unique_ptr<IPC::Message> PopMessage() { 241 std::unique_ptr<IPC::Message> PopMessage() {
217 CHECK(queued_messages_.size()); 242 CHECK(queued_messages_.size());
218 std::unique_ptr<IPC::Message> msg(queued_messages_.begin()->release()); 243 std::unique_ptr<IPC::Message> msg(queued_messages_.begin()->release());
219 queued_messages_.erase(queued_messages_.begin()); 244 queued_messages_.erase(queued_messages_.begin());
220 return msg; 245 return msg;
221 } 246 }
222 247
223 void FastShutdownIfPossible() { 248 void FastShutdownIfPossible() {
(...skipping 21 matching lines...) Expand all
245 new MessagePortHostMsg_Entangle(*port_1, *port_2))); 270 new MessagePortHostMsg_Entangle(*port_1, *port_2)));
246 EXPECT_TRUE(renderer->OnMessageReceived( 271 EXPECT_TRUE(renderer->OnMessageReceived(
247 new MessagePortHostMsg_Entangle(*port_2, *port_1))); 272 new MessagePortHostMsg_Entangle(*port_2, *port_1)));
248 } 273 }
249 274
250 void PostCreateWorker(MockRendererProcessHost* renderer, 275 void PostCreateWorker(MockRendererProcessHost* renderer,
251 const std::string& url, 276 const std::string& url,
252 const std::string& name, 277 const std::string& name,
253 unsigned long long document_id, 278 unsigned long long document_id,
254 int render_frame_route_id, 279 int render_frame_route_id,
255 ViewHostMsg_CreateWorker_Reply* reply) { 280 int* route_id_out,
256 ViewHostMsg_CreateWorker_Params params; 281 blink::WebWorkerCreationError* creation_error_out) {
257 params.url = GURL(url); 282 mojom::SharedWorkerCreateParamsPtr params =
258 params.name = base::ASCIIToUTF16(name); 283 mojom::SharedWorkerCreateParams::New();
259 params.content_security_policy = base::string16(); 284 params->url = GURL(url);
260 params.security_policy_type = blink::WebContentSecurityPolicyTypeReport; 285 params->name = base::ASCIIToUTF16(name);
261 params.document_id = document_id; 286 params->content_security_policy = base::string16();
262 params.render_frame_route_id = render_frame_route_id; 287 params->security_policy_type = blink::WebContentSecurityPolicyTypeReport;
263 params.creation_context_type = 288 params->document_id = document_id;
289 params->render_frame_route_id = render_frame_route_id;
290 params->creation_context_type =
264 blink::WebSharedWorkerCreationContextTypeSecure; 291 blink::WebSharedWorkerCreationContextTypeSecure;
265 EXPECT_TRUE( 292 renderer->OnCreateWorker(std::move(params), route_id_out, creation_error_out);
266 renderer->OnMessageReceived(new ViewHostMsg_CreateWorker(params, reply)));
267 } 293 }
268 294
269 class MockSharedWorkerConnector { 295 class MockSharedWorkerConnector {
270 public: 296 public:
271 MockSharedWorkerConnector(MockRendererProcessHost* renderer_host) 297 MockSharedWorkerConnector(MockRendererProcessHost* renderer_host)
272 : renderer_host_(renderer_host), 298 : renderer_host_(renderer_host),
273 temporary_remote_port_route_id_(0), 299 temporary_remote_port_route_id_(0),
274 remote_port_id_(0), 300 remote_port_id_(0),
275 local_port_route_id_(0), 301 local_port_route_id_(0),
276 local_port_id_(0) {} 302 local_port_id_(0) {}
277 void Create(const std::string& url, 303 void Create(const std::string& url,
278 const std::string& name, 304 const std::string& name,
279 unsigned long long document_id, 305 unsigned long long document_id,
280 int render_frame_route_id) { 306 int render_frame_route_id) {
281 CreateMessagePortPair(renderer_host_, 307 CreateMessagePortPair(renderer_host_,
282 &temporary_remote_port_route_id_, 308 &temporary_remote_port_route_id_,
283 &remote_port_id_, 309 &remote_port_id_,
284 &local_port_route_id_, 310 &local_port_route_id_,
285 &local_port_id_); 311 &local_port_id_);
286 PostCreateWorker(renderer_host_, url, name, document_id, 312 PostCreateWorker(renderer_host_, url, name, document_id,
287 render_frame_route_id, &create_worker_reply_); 313 render_frame_route_id, &route_id_, &creation_error_);
288 } 314 }
289 void SendQueueMessages() { 315 void SendQueueMessages() {
290 EXPECT_TRUE(renderer_host_->OnMessageReceived( 316 EXPECT_TRUE(renderer_host_->OnMessageReceived(
291 new MessagePortHostMsg_QueueMessages(remote_port_id_))); 317 new MessagePortHostMsg_QueueMessages(remote_port_id_)));
292 } 318 }
293 void SendPostMessage(const std::string& data) { 319 void SendPostMessage(const std::string& data) {
294 const std::vector<int> empty_ports; 320 const std::vector<int> empty_ports;
295 EXPECT_TRUE( 321 EXPECT_TRUE(
296 renderer_host_->OnMessageReceived(new MessagePortHostMsg_PostMessage( 322 renderer_host_->OnMessageReceived(new MessagePortHostMsg_PostMessage(
297 local_port_id_, base::ASCIIToUTF16(data), empty_ports))); 323 local_port_id_, base::ASCIIToUTF16(data), empty_ports)));
298 } 324 }
299 void SendConnect() { 325 void SendConnect() {
300 EXPECT_TRUE( 326 renderer_host_->OnConnectToWorker(route_id_, remote_port_id_);
301 renderer_host_->OnMessageReceived(new ViewHostMsg_ConnectToWorker(
302 create_worker_reply_.route_id, remote_port_id_)));
303 } 327 }
304 void SendSendQueuedMessages( 328 void SendSendQueuedMessages(
305 const std::vector<QueuedMessage>& queued_messages) { 329 const std::vector<QueuedMessage>& queued_messages) {
306 EXPECT_TRUE(renderer_host_->OnMessageReceived( 330 EXPECT_TRUE(renderer_host_->OnMessageReceived(
307 new MessagePortHostMsg_SendQueuedMessages(remote_port_id_, 331 new MessagePortHostMsg_SendQueuedMessages(remote_port_id_,
308 queued_messages))); 332 queued_messages)));
309 } 333 }
310 int temporary_remote_port_route_id() { 334 int temporary_remote_port_route_id() {
311 return temporary_remote_port_route_id_; 335 return temporary_remote_port_route_id_;
312 } 336 }
313 int remote_port_id() { return remote_port_id_; } 337 int remote_port_id() { return remote_port_id_; }
314 int local_port_route_id() { return local_port_route_id_; } 338 int local_port_route_id() { return local_port_route_id_; }
315 int local_port_id() { return local_port_id_; } 339 int local_port_id() { return local_port_id_; }
316 int route_id() { return create_worker_reply_.route_id; } 340 int route_id() { return route_id_; }
317 blink::WebWorkerCreationError creation_error() { 341 blink::WebWorkerCreationError creation_error() { return creation_error_; }
318 return create_worker_reply_.error;
319 }
320 342
321 private: 343 private:
322 MockRendererProcessHost* renderer_host_; 344 MockRendererProcessHost* renderer_host_;
323 int temporary_remote_port_route_id_; 345 int temporary_remote_port_route_id_;
324 int remote_port_id_; 346 int remote_port_id_;
325 int local_port_route_id_; 347 int local_port_route_id_;
326 int local_port_id_; 348 int local_port_id_;
327 ViewHostMsg_CreateWorker_Reply create_worker_reply_; 349 int route_id_;
350 blink::WebWorkerCreationError creation_error_;
328 }; 351 };
329 352
330 void CheckWorkerProcessMsgCreateWorker( 353 void CheckWorkerProcessMsgCreateWorker(
331 MockRendererProcessHost* renderer_host, 354 MockRendererProcessHost* renderer_host,
332 const std::string& expected_url, 355 const std::string& expected_url,
333 const std::string& expected_name, 356 const std::string& expected_name,
334 blink::WebContentSecurityPolicyType expected_security_policy_type, 357 blink::WebContentSecurityPolicyType expected_security_policy_type,
335 int* route_id) { 358 int* route_id) {
336 std::unique_ptr<IPC::Message> msg(renderer_host->PopMessage()); 359 std::unique_ptr<IPC::Message> msg(renderer_host->PopMessage());
337 EXPECT_EQ(WorkerProcessMsg_CreateWorker::ID, msg->type()); 360 EXPECT_EQ(WorkerProcessMsg_CreateWorker::ID, msg->type());
(...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after
977 kDocumentIDs[2], 1000 kDocumentIDs[2],
978 kRenderFrameRouteIDs[2]); 1001 kRenderFrameRouteIDs[2]);
979 EXPECT_NE(MSG_ROUTING_NONE, connector2->route_id()); 1002 EXPECT_NE(MSG_ROUTING_NONE, connector2->route_id());
980 EXPECT_EQ(0U, renderer_host2->QueuedMessageCount()); 1003 EXPECT_EQ(0U, renderer_host2->QueuedMessageCount());
981 RunAllPendingInMessageLoop(); 1004 RunAllPendingInMessageLoop();
982 EXPECT_EQ(1U, renderer_host2->QueuedMessageCount()); 1005 EXPECT_EQ(1U, renderer_host2->QueuedMessageCount());
983 CheckViewMsgWorkerCreated(renderer_host2.get(), connector2.get()); 1006 CheckViewMsgWorkerCreated(renderer_host2.get(), connector2.get());
984 } 1007 }
985 1008
986 } // namespace content 1009 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698