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.
9 #include "base/atomic_sequence_num.h"
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/synchronization/lock.h"
16 #include "content/browser/message_port_message_filter.h"
17 #include "content/browser/shared_worker/shared_worker_message_filter.h"
18 #include "content/browser/shared_worker/shared_worker_service_impl.h"
19 #include "content/browser/shared_worker/worker_storage_partition.h"
20 #include "content/common/message_port_messages.h"
21 #include "content/common/view_messages.h"
22 #include "content/common/worker_messages.h"
23 #include "content/public/test/test_browser_context.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "content/public/test/test_utils.h"
26 #include "ipc/ipc_sync_message.h"
27 #include "testing/gtest/include/gtest/gtest.h"
31 class SharedWorkerServiceImplTest
: public testing::Test
{
33 static void RegisterRunningProcessID(int process_id
) {
34 base::AutoLock
lock(s_lock_
);
35 s_running_process_id_set_
.insert(process_id
);
37 static void UnregisterRunningProcessID(int process_id
) {
38 base::AutoLock
lock(s_lock_
);
39 s_running_process_id_set_
.erase(process_id
);
43 SharedWorkerServiceImplTest()
44 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
),
45 browser_context_(new TestBrowserContext()),
47 new WorkerStoragePartition(browser_context_
->GetRequestContext(),
55 SharedWorkerServiceImpl::GetInstance()
56 ->ChangeUpdateWorkerDependencyFuncForTesting(
57 &SharedWorkerServiceImplTest::MockUpdateWorkerDependency
);
58 SharedWorkerServiceImpl::GetInstance()
59 ->ChangeTryIncrementWorkerRefCountFuncForTesting(
60 &SharedWorkerServiceImplTest::MockTryIncrementWorkerRefCount
);
63 void SetUp() override
{}
64 void TearDown() override
{
65 s_update_worker_dependency_call_count_
= 0;
66 s_worker_dependency_added_ids_
.clear();
67 s_worker_dependency_removed_ids_
.clear();
68 s_running_process_id_set_
.clear();
69 SharedWorkerServiceImpl::GetInstance()->ResetForTesting();
71 static void MockUpdateWorkerDependency(const std::vector
<int>& added_ids
,
72 const std::vector
<int>& removed_ids
) {
73 ++s_update_worker_dependency_call_count_
;
74 s_worker_dependency_added_ids_
= added_ids
;
75 s_worker_dependency_removed_ids_
= removed_ids
;
77 static bool MockTryIncrementWorkerRefCount(int worker_process_id
) {
78 base::AutoLock
lock(s_lock_
);
79 return s_running_process_id_set_
.find(worker_process_id
) !=
80 s_running_process_id_set_
.end();
83 TestBrowserThreadBundle browser_thread_bundle_
;
84 scoped_ptr
<TestBrowserContext
> browser_context_
;
85 scoped_ptr
<WorkerStoragePartition
> partition_
;
86 static int s_update_worker_dependency_call_count_
;
87 static std::vector
<int> s_worker_dependency_added_ids_
;
88 static std::vector
<int> s_worker_dependency_removed_ids_
;
89 static base::Lock s_lock_
;
90 static std::set
<int> s_running_process_id_set_
;
91 DISALLOW_COPY_AND_ASSIGN(SharedWorkerServiceImplTest
);
95 int SharedWorkerServiceImplTest::s_update_worker_dependency_call_count_
;
96 std::vector
<int> SharedWorkerServiceImplTest::s_worker_dependency_added_ids_
;
97 std::vector
<int> SharedWorkerServiceImplTest::s_worker_dependency_removed_ids_
;
98 base::Lock
SharedWorkerServiceImplTest::s_lock_
;
99 std::set
<int> SharedWorkerServiceImplTest::s_running_process_id_set_
;
103 static const int kProcessIDs
[] = {100, 101, 102};
104 static const unsigned long long kDocumentIDs
[] = {200, 201, 202};
105 static const int kRenderFrameRouteIDs
[] = {300, 301, 302};
107 class MockMessagePortMessageFilter
: public MessagePortMessageFilter
{
109 MockMessagePortMessageFilter(const NextRoutingIDCallback
& callback
,
110 ScopedVector
<IPC::Message
>* message_queue
)
111 : MessagePortMessageFilter(callback
), message_queue_(message_queue
) {}
113 bool Send(IPC::Message
* message
) override
{
114 if (!message_queue_
) {
118 message_queue_
->push_back(message
);
123 message_queue_
= NULL
;
128 ~MockMessagePortMessageFilter() override
{}
129 ScopedVector
<IPC::Message
>* message_queue_
;
132 class MockSharedWorkerMessageFilter
: public SharedWorkerMessageFilter
{
134 MockSharedWorkerMessageFilter(int render_process_id
,
135 ResourceContext
* resource_context
,
136 const WorkerStoragePartition
& partition
,
137 MessagePortMessageFilter
* message_port_filter
,
138 ScopedVector
<IPC::Message
>* message_queue
)
139 : SharedWorkerMessageFilter(render_process_id
,
142 message_port_filter
),
143 message_queue_(message_queue
) {}
145 bool Send(IPC::Message
* message
) override
{
146 if (!message_queue_
) {
150 message_queue_
->push_back(message
);
155 message_queue_
= NULL
;
160 ~MockSharedWorkerMessageFilter() override
{}
161 ScopedVector
<IPC::Message
>* message_queue_
;
164 class MockRendererProcessHost
{
166 MockRendererProcessHost(int process_id
,
167 ResourceContext
* resource_context
,
168 const WorkerStoragePartition
& partition
)
169 : process_id_(process_id
),
170 message_filter_(new MockMessagePortMessageFilter(
171 base::Bind(&base::AtomicSequenceNumber::GetNext
,
172 base::Unretained(&next_routing_id_
)),
174 worker_filter_(new MockSharedWorkerMessageFilter(process_id
,
177 message_filter_
.get(),
178 &queued_messages_
)) {
179 SharedWorkerServiceImplTest::RegisterRunningProcessID(process_id
);
182 ~MockRendererProcessHost() {
183 SharedWorkerServiceImplTest::UnregisterRunningProcessID(process_id_
);
184 message_filter_
->Close();
185 worker_filter_
->Close();
188 bool OnMessageReceived(IPC::Message
* message
) {
189 scoped_ptr
<IPC::Message
> msg(message
);
190 const bool ret
= message_filter_
->OnMessageReceived(*message
) ||
191 worker_filter_
->OnMessageReceived(*message
);
192 if (message
->is_sync()) {
193 CHECK(!queued_messages_
.empty());
194 const IPC::Message
* response_msg
= queued_messages_
.back();
195 IPC::SyncMessage
* sync_msg
= static_cast<IPC::SyncMessage
*>(message
);
196 scoped_ptr
<IPC::MessageReplyDeserializer
> reply_serializer(
197 sync_msg
->GetReplyDeserializer());
198 bool result
= reply_serializer
->SerializeOutputParameters(*response_msg
);
200 queued_messages_
.pop_back();
205 size_t QueuedMessageCount() const { return queued_messages_
.size(); }
207 scoped_ptr
<IPC::Message
> PopMessage() {
208 CHECK(queued_messages_
.size());
209 scoped_ptr
<IPC::Message
> msg(*queued_messages_
.begin());
210 queued_messages_
.weak_erase(queued_messages_
.begin());
214 void FastShutdownIfPossible() {
215 SharedWorkerServiceImplTest::UnregisterRunningProcessID(process_id_
);
219 const int process_id_
;
220 ScopedVector
<IPC::Message
> queued_messages_
;
221 base::AtomicSequenceNumber next_routing_id_
;
222 scoped_refptr
<MockMessagePortMessageFilter
> message_filter_
;
223 scoped_refptr
<MockSharedWorkerMessageFilter
> worker_filter_
;
226 void CreateMessagePortPair(MockRendererProcessHost
* renderer
,
231 EXPECT_TRUE(renderer
->OnMessageReceived(
232 new MessagePortHostMsg_CreateMessagePort(route_1
, port_1
)));
233 EXPECT_TRUE(renderer
->OnMessageReceived(
234 new MessagePortHostMsg_CreateMessagePort(route_2
, port_2
)));
235 EXPECT_TRUE(renderer
->OnMessageReceived(
236 new MessagePortHostMsg_Entangle(*port_1
, *port_2
)));
237 EXPECT_TRUE(renderer
->OnMessageReceived(
238 new MessagePortHostMsg_Entangle(*port_2
, *port_1
)));
241 void PostCreateWorker(MockRendererProcessHost
* renderer
,
242 const std::string
& url
,
243 const std::string
& name
,
244 unsigned long long document_id
,
245 int render_frame_route_id
,
246 int* connector_route_id
) {
247 ViewHostMsg_CreateWorker_Params params
;
248 params
.url
= GURL(url
);
249 params
.name
= base::ASCIIToUTF16(name
);
250 params
.content_security_policy
= base::string16();
251 params
.security_policy_type
= blink::WebContentSecurityPolicyTypeReport
;
252 params
.document_id
= document_id
;
253 params
.render_frame_route_id
= render_frame_route_id
;
254 EXPECT_TRUE(renderer
->OnMessageReceived(
255 new ViewHostMsg_CreateWorker(params
, connector_route_id
)));
258 class MockSharedWorkerConnector
{
260 MockSharedWorkerConnector(MockRendererProcessHost
* renderer_host
)
261 : renderer_host_(renderer_host
),
262 temporary_remote_port_route_id_(0),
264 local_port_route_id_(0),
267 void Create(const std::string
& url
,
268 const std::string
& name
,
269 unsigned long long document_id
,
270 int render_frame_route_id
) {
271 CreateMessagePortPair(renderer_host_
,
272 &temporary_remote_port_route_id_
,
274 &local_port_route_id_
,
276 PostCreateWorker(renderer_host_
,
280 render_frame_route_id
,
283 void SendQueueMessages() {
284 EXPECT_TRUE(renderer_host_
->OnMessageReceived(
285 new MessagePortHostMsg_QueueMessages(remote_port_id_
)));
287 void SendPostMessage(std::string data
) {
288 const std::vector
<TransferredMessagePort
> empty_ports
;
290 renderer_host_
->OnMessageReceived(new MessagePortHostMsg_PostMessage(
292 MessagePortMessage(base::ASCIIToUTF16(data
)), empty_ports
)));
296 renderer_host_
->OnMessageReceived(new ViewHostMsg_ForwardToWorker(
297 WorkerMsg_Connect(route_id_
, remote_port_id_
, MSG_ROUTING_NONE
))));
299 void SendSendQueuedMessages(
300 const std::vector
<QueuedMessage
>& queued_messages
) {
301 EXPECT_TRUE(renderer_host_
->OnMessageReceived(
302 new MessagePortHostMsg_SendQueuedMessages(remote_port_id_
,
305 int temporary_remote_port_route_id() {
306 return temporary_remote_port_route_id_
;
308 int remote_port_id() { return remote_port_id_
; }
309 int local_port_route_id() { return local_port_route_id_
; }
310 int local_port_id() { return local_port_id_
; }
311 int route_id() { return route_id_
; }
314 MockRendererProcessHost
* renderer_host_
;
315 int temporary_remote_port_route_id_
;
317 int local_port_route_id_
;
322 void CheckWorkerProcessMsgCreateWorker(
323 MockRendererProcessHost
* renderer_host
,
324 const std::string
& expected_url
,
325 const std::string
& expected_name
,
326 blink::WebContentSecurityPolicyType expected_security_policy_type
,
328 scoped_ptr
<IPC::Message
> msg(renderer_host
->PopMessage());
329 EXPECT_EQ(WorkerProcessMsg_CreateWorker::ID
, msg
->type());
330 Tuple
<WorkerProcessMsg_CreateWorker_Params
> param
;
331 EXPECT_TRUE(WorkerProcessMsg_CreateWorker::Read(msg
.get(), ¶m
));
332 EXPECT_EQ(GURL(expected_url
), get
<0>(param
).url
);
333 EXPECT_EQ(base::ASCIIToUTF16(expected_name
), get
<0>(param
).name
);
334 EXPECT_EQ(expected_security_policy_type
, get
<0>(param
).security_policy_type
);
335 *route_id
= get
<0>(param
).route_id
;
338 void CheckViewMsgWorkerCreated(MockRendererProcessHost
* renderer_host
,
339 MockSharedWorkerConnector
* connector
) {
340 scoped_ptr
<IPC::Message
> msg(renderer_host
->PopMessage());
341 EXPECT_EQ(ViewMsg_WorkerCreated::ID
, msg
->type());
342 EXPECT_EQ(connector
->route_id(), msg
->routing_id());
345 void CheckMessagePortMsgMessagesQueued(MockRendererProcessHost
* renderer_host
,
346 MockSharedWorkerConnector
* connector
) {
347 scoped_ptr
<IPC::Message
> msg(renderer_host
->PopMessage());
348 EXPECT_EQ(MessagePortMsg_MessagesQueued::ID
, msg
->type());
349 EXPECT_EQ(connector
->temporary_remote_port_route_id(), msg
->routing_id());
352 void CheckWorkerMsgConnect(MockRendererProcessHost
* renderer_host
,
353 int expected_msg_route_id
,
354 int expected_sent_message_port_id
,
356 scoped_ptr
<IPC::Message
> msg(renderer_host
->PopMessage());
357 EXPECT_EQ(WorkerMsg_Connect::ID
, msg
->type());
358 EXPECT_EQ(expected_msg_route_id
, msg
->routing_id());
359 WorkerMsg_Connect::Param params
;
360 EXPECT_TRUE(WorkerMsg_Connect::Read(msg
.get(), ¶ms
));
361 int port_id
= get
<0>(params
);
362 *routing_id
= get
<1>(params
);
363 EXPECT_EQ(expected_sent_message_port_id
, port_id
);
366 void CheckMessagePortMsgMessage(MockRendererProcessHost
* renderer_host
,
367 int expected_msg_route_id
,
368 std::string expected_data
) {
369 scoped_ptr
<IPC::Message
> msg(renderer_host
->PopMessage());
370 EXPECT_EQ(MessagePortMsg_Message::ID
, msg
->type());
371 EXPECT_EQ(expected_msg_route_id
, msg
->routing_id());
372 MessagePortMsg_Message::Param params
;
373 EXPECT_TRUE(MessagePortMsg_Message::Read(msg
.get(), ¶ms
));
374 base::string16 data
= get
<0>(params
).message_as_string
;
375 EXPECT_EQ(base::ASCIIToUTF16(expected_data
), data
);
378 void CheckViewMsgWorkerConnected(MockRendererProcessHost
* renderer_host
,
379 MockSharedWorkerConnector
* connector
) {
380 scoped_ptr
<IPC::Message
> msg(renderer_host
->PopMessage());
381 EXPECT_EQ(ViewMsg_WorkerConnected::ID
, msg
->type());
382 EXPECT_EQ(connector
->route_id(), msg
->routing_id());
387 TEST_F(SharedWorkerServiceImplTest
, BasicTest
) {
388 scoped_ptr
<MockRendererProcessHost
> renderer_host(
389 new MockRendererProcessHost(kProcessIDs
[0],
390 browser_context_
->GetResourceContext(),
392 scoped_ptr
<MockSharedWorkerConnector
> connector(
393 new MockSharedWorkerConnector(renderer_host
.get()));
395 int worker_msg_port_route_id
;
397 // SharedWorkerConnector creates two message ports and sends
398 // ViewHostMsg_CreateWorker.
399 connector
->Create("http://example.com/w.js",
402 kRenderFrameRouteIDs
[0]);
403 // We need to go to UI thread to call ReserveRenderProcessOnUI().
404 RunAllPendingInMessageLoop();
405 EXPECT_EQ(2U, renderer_host
->QueuedMessageCount());
406 // WorkerProcessMsg_CreateWorker should be sent to the renderer in which
407 // SharedWorker will be created.
408 CheckWorkerProcessMsgCreateWorker(renderer_host
.get(),
409 "http://example.com/w.js",
411 blink::WebContentSecurityPolicyTypeReport
,
413 // ViewMsg_WorkerCreated(1) should be sent back to SharedWorkerConnector side.
414 CheckViewMsgWorkerCreated(renderer_host
.get(), connector
.get());
416 // SharedWorkerConnector side sends MessagePortHostMsg_QueueMessages in
417 // WebSharedWorkerProxy::connect.
418 connector
->SendQueueMessages();
419 EXPECT_EQ(1U, renderer_host
->QueuedMessageCount());
420 // MessagePortMsg_MessagesQueued(2) should be sent back to
421 // SharedWorkerConnector side.
422 CheckMessagePortMsgMessagesQueued(renderer_host
.get(), connector
.get());
424 // When SharedWorkerConnector receives ViewMsg_WorkerCreated(1), it sends
425 // WorkerMsg_Connect wrapped in ViewHostMsg_ForwardToWorker.
426 connector
->SendConnect();
427 EXPECT_EQ(1U, renderer_host
->QueuedMessageCount());
428 // WorkerMsg_Connect should be sent to SharedWorker side.
429 CheckWorkerMsgConnect(renderer_host
.get(),
431 connector
->remote_port_id(),
432 &worker_msg_port_route_id
);
434 // When SharedWorkerConnector receives MessagePortMsg_MessagesQueued(2), it
435 // sends MessagePortHostMsg_SendQueuedMessages.
436 std::vector
<QueuedMessage
> empty_messages
;
437 connector
->SendSendQueuedMessages(empty_messages
);
438 EXPECT_EQ(0U, renderer_host
->QueuedMessageCount());
440 // SharedWorker sends WorkerHostMsg_WorkerReadyForInspection in
441 // EmbeddedSharedWorkerStub::WorkerReadyForInspection().
442 EXPECT_TRUE(renderer_host
->OnMessageReceived(
443 new WorkerHostMsg_WorkerReadyForInspection(worker_route_id
)));
444 EXPECT_EQ(0U, renderer_host
->QueuedMessageCount());
446 // SharedWorker sends WorkerHostMsg_WorkerScriptLoaded in
447 // EmbeddedSharedWorkerStub::workerScriptLoaded().
448 EXPECT_TRUE(renderer_host
->OnMessageReceived(
449 new WorkerHostMsg_WorkerScriptLoaded(worker_route_id
)));
450 EXPECT_EQ(0U, renderer_host
->QueuedMessageCount());
452 // SharedWorker sends WorkerHostMsg_WorkerConnected in
453 // EmbeddedSharedWorkerStub::workerScriptLoaded().
455 renderer_host
->OnMessageReceived(new WorkerHostMsg_WorkerConnected(
456 connector
->remote_port_id(), worker_route_id
)));
457 EXPECT_EQ(1U, renderer_host
->QueuedMessageCount());
458 // ViewMsg_WorkerConnected should be sent to SharedWorkerConnector side.
459 CheckViewMsgWorkerConnected(renderer_host
.get(), connector
.get());
461 // When SharedWorkerConnector side sends MessagePortHostMsg_PostMessage,
462 // SharedWorker side shuold receive MessagePortMsg_Message.
463 connector
->SendPostMessage("test1");
464 EXPECT_EQ(1U, renderer_host
->QueuedMessageCount());
465 CheckMessagePortMsgMessage(
466 renderer_host
.get(), worker_msg_port_route_id
, "test1");
468 // When SharedWorker side sends MessagePortHostMsg_PostMessage,
469 // SharedWorkerConnector side shuold receive MessagePortMsg_Message.
470 const std::vector
<TransferredMessagePort
> empty_ports
;
472 renderer_host
->OnMessageReceived(new MessagePortHostMsg_PostMessage(
473 connector
->remote_port_id(),
474 MessagePortMessage(base::ASCIIToUTF16("test2")), empty_ports
)));
475 EXPECT_EQ(1U, renderer_host
->QueuedMessageCount());
476 CheckMessagePortMsgMessage(
477 renderer_host
.get(), connector
->local_port_route_id(), "test2");
479 // UpdateWorkerDependency should not be called.
480 EXPECT_EQ(0, s_update_worker_dependency_call_count_
);
483 TEST_F(SharedWorkerServiceImplTest
, TwoRendererTest
) {
484 // The first renderer host.
485 scoped_ptr
<MockRendererProcessHost
> renderer_host0(
486 new MockRendererProcessHost(kProcessIDs
[0],
487 browser_context_
->GetResourceContext(),
489 scoped_ptr
<MockSharedWorkerConnector
> connector0(
490 new MockSharedWorkerConnector(renderer_host0
.get()));
492 int worker_msg_port_route_id1
;
494 // SharedWorkerConnector creates two message ports and sends
495 // ViewHostMsg_CreateWorker.
496 connector0
->Create("http://example.com/w.js",
499 kRenderFrameRouteIDs
[0]);
500 // We need to go to UI thread to call ReserveRenderProcessOnUI().
501 RunAllPendingInMessageLoop();
502 EXPECT_EQ(2U, renderer_host0
->QueuedMessageCount());
503 // WorkerProcessMsg_CreateWorker should be sent to the renderer in which
504 // SharedWorker will be created.
505 CheckWorkerProcessMsgCreateWorker(renderer_host0
.get(),
506 "http://example.com/w.js",
508 blink::WebContentSecurityPolicyTypeReport
,
510 // ViewMsg_WorkerCreated(1) should be sent back to SharedWorkerConnector side.
511 CheckViewMsgWorkerCreated(renderer_host0
.get(), connector0
.get());
513 // SharedWorkerConnector side sends MessagePortHostMsg_QueueMessages in
514 // WebSharedWorkerProxy::connect.
515 connector0
->SendQueueMessages();
516 EXPECT_EQ(1U, renderer_host0
->QueuedMessageCount());
517 // MessagePortMsg_MessagesQueued(2) should be sent back to
518 // SharedWorkerConnector side.
519 CheckMessagePortMsgMessagesQueued(renderer_host0
.get(), connector0
.get());
521 // When SharedWorkerConnector receives ViewMsg_WorkerCreated(1), it sends
522 // WorkerMsg_Connect wrapped in ViewHostMsg_ForwardToWorker.
523 connector0
->SendConnect();
524 EXPECT_EQ(1U, renderer_host0
->QueuedMessageCount());
525 // WorkerMsg_Connect should be sent to SharedWorker side.
526 CheckWorkerMsgConnect(renderer_host0
.get(),
528 connector0
->remote_port_id(),
529 &worker_msg_port_route_id1
);
531 // When SharedWorkerConnector receives MessagePortMsg_MessagesQueued(2), it
532 // sends MessagePortHostMsg_SendQueuedMessages.
533 std::vector
<QueuedMessage
> empty_messages
;
534 connector0
->SendSendQueuedMessages(empty_messages
);
535 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
537 // SharedWorker sends WorkerHostMsg_WorkerReadyForInspection in
538 // EmbeddedSharedWorkerStub::WorkerReadyForInspection().
539 EXPECT_TRUE(renderer_host0
->OnMessageReceived(
540 new WorkerHostMsg_WorkerReadyForInspection(worker_route_id
)));
541 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
543 // SharedWorker sends WorkerHostMsg_WorkerScriptLoaded in
544 // EmbeddedSharedWorkerStub::workerScriptLoaded().
545 EXPECT_TRUE(renderer_host0
->OnMessageReceived(
546 new WorkerHostMsg_WorkerScriptLoaded(worker_route_id
)));
547 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
549 // SharedWorker sends WorkerHostMsg_WorkerConnected in
550 // EmbeddedSharedWorkerStub::workerScriptLoaded().
552 renderer_host0
->OnMessageReceived(new WorkerHostMsg_WorkerConnected(
553 connector0
->remote_port_id(), worker_route_id
)));
554 EXPECT_EQ(1U, renderer_host0
->QueuedMessageCount());
555 // ViewMsg_WorkerConnected should be sent to SharedWorkerConnector side.
556 CheckViewMsgWorkerConnected(renderer_host0
.get(), connector0
.get());
558 // When SharedWorkerConnector side sends MessagePortHostMsg_PostMessage,
559 // SharedWorker side shuold receive MessagePortMsg_Message.
560 connector0
->SendPostMessage("test1");
561 EXPECT_EQ(1U, renderer_host0
->QueuedMessageCount());
562 CheckMessagePortMsgMessage(
563 renderer_host0
.get(), worker_msg_port_route_id1
, "test1");
565 // When SharedWorker side sends MessagePortHostMsg_PostMessage,
566 // SharedWorkerConnector side shuold receive MessagePortMsg_Message.
567 const std::vector
<TransferredMessagePort
> empty_ports
;
569 renderer_host0
->OnMessageReceived(new MessagePortHostMsg_PostMessage(
570 connector0
->remote_port_id(),
571 MessagePortMessage(base::ASCIIToUTF16("test2")), empty_ports
)));
572 EXPECT_EQ(1U, renderer_host0
->QueuedMessageCount());
573 CheckMessagePortMsgMessage(
574 renderer_host0
.get(), connector0
->local_port_route_id(), "test2");
576 // The second renderer host.
577 scoped_ptr
<MockRendererProcessHost
> renderer_host1(
578 new MockRendererProcessHost(kProcessIDs
[1],
579 browser_context_
->GetResourceContext(),
581 scoped_ptr
<MockSharedWorkerConnector
> connector1(
582 new MockSharedWorkerConnector(renderer_host1
.get()));
583 int worker_msg_port_route_id2
;
585 // UpdateWorkerDependency should not be called yet.
586 EXPECT_EQ(0, s_update_worker_dependency_call_count_
);
588 // SharedWorkerConnector creates two message ports and sends
589 // ViewHostMsg_CreateWorker.
590 connector1
->Create("http://example.com/w.js",
593 kRenderFrameRouteIDs
[1]);
594 // We need to go to UI thread to call ReserveRenderProcessOnUI().
595 RunAllPendingInMessageLoop();
596 EXPECT_EQ(1U, renderer_host1
->QueuedMessageCount());
597 // ViewMsg_WorkerCreated(3) should be sent back to SharedWorkerConnector side.
598 CheckViewMsgWorkerCreated(renderer_host1
.get(), connector1
.get());
600 // UpdateWorkerDependency should be called.
601 EXPECT_EQ(1, s_update_worker_dependency_call_count_
);
602 EXPECT_EQ(1U, s_worker_dependency_added_ids_
.size());
603 EXPECT_EQ(kProcessIDs
[0], s_worker_dependency_added_ids_
[0]);
604 EXPECT_EQ(0U, s_worker_dependency_removed_ids_
.size());
606 // SharedWorkerConnector side sends MessagePortHostMsg_QueueMessages in
607 // WebSharedWorkerProxy::connect.
608 connector1
->SendQueueMessages();
609 EXPECT_EQ(1U, renderer_host1
->QueuedMessageCount());
610 // MessagePortMsg_MessagesQueued(4) should be sent back to
611 // SharedWorkerConnector side.
612 CheckMessagePortMsgMessagesQueued(renderer_host1
.get(), connector1
.get());
614 // When SharedWorkerConnector receives ViewMsg_WorkerCreated(3), it sends
615 // WorkerMsg_Connect wrapped in ViewHostMsg_ForwardToWorker.
616 connector1
->SendConnect();
617 EXPECT_EQ(1U, renderer_host0
->QueuedMessageCount());
618 // WorkerMsg_Connect should be sent to SharedWorker side.
619 CheckWorkerMsgConnect(renderer_host0
.get(),
621 connector1
->remote_port_id(),
622 &worker_msg_port_route_id2
);
624 // When SharedWorkerConnector receives MessagePortMsg_MessagesQueued(4), it
625 // sends MessagePortHostMsg_SendQueuedMessages.
626 connector1
->SendSendQueuedMessages(empty_messages
);
627 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
629 // SharedWorker sends WorkerHostMsg_WorkerConnected in
630 // EmbeddedSharedWorkerStub::OnConnect().
632 renderer_host0
->OnMessageReceived(new WorkerHostMsg_WorkerConnected(
633 connector1
->remote_port_id(), worker_route_id
)));
634 EXPECT_EQ(1U, renderer_host1
->QueuedMessageCount());
635 // ViewMsg_WorkerConnected should be sent to SharedWorkerConnector side.
636 CheckViewMsgWorkerConnected(renderer_host1
.get(), connector1
.get());
638 // When SharedWorkerConnector side sends MessagePortHostMsg_PostMessage,
639 // SharedWorker side shuold receive MessagePortMsg_Message.
640 connector1
->SendPostMessage("test3");
641 EXPECT_EQ(1U, renderer_host0
->QueuedMessageCount());
642 CheckMessagePortMsgMessage(
643 renderer_host0
.get(), worker_msg_port_route_id2
, "test3");
645 // When SharedWorker side sends MessagePortHostMsg_PostMessage,
646 // SharedWorkerConnector side shuold receive MessagePortMsg_Message.
648 renderer_host0
->OnMessageReceived(new MessagePortHostMsg_PostMessage(
649 connector1
->remote_port_id(),
650 MessagePortMessage(base::ASCIIToUTF16("test4")), empty_ports
)));
651 EXPECT_EQ(1U, renderer_host1
->QueuedMessageCount());
652 CheckMessagePortMsgMessage(
653 renderer_host1
.get(), connector1
->local_port_route_id(), "test4");
655 EXPECT_EQ(1, s_update_worker_dependency_call_count_
);
656 renderer_host1
.reset();
657 // UpdateWorkerDependency should be called.
658 EXPECT_EQ(2, s_update_worker_dependency_call_count_
);
659 EXPECT_EQ(0U, s_worker_dependency_added_ids_
.size());
660 EXPECT_EQ(1U, s_worker_dependency_removed_ids_
.size());
661 EXPECT_EQ(kProcessIDs
[0], s_worker_dependency_removed_ids_
[0]);
664 TEST_F(SharedWorkerServiceImplTest
, CreateWorkerTest
) {
665 // The first renderer host.
666 scoped_ptr
<MockRendererProcessHost
> renderer_host0(
667 new MockRendererProcessHost(kProcessIDs
[0],
668 browser_context_
->GetResourceContext(),
670 // The second renderer host.
671 scoped_ptr
<MockRendererProcessHost
> renderer_host1(
672 new MockRendererProcessHost(kProcessIDs
[1],
673 browser_context_
->GetResourceContext(),
679 scoped_ptr
<MockSharedWorkerConnector
> connector0(
680 new MockSharedWorkerConnector(renderer_host0
.get()));
681 scoped_ptr
<MockSharedWorkerConnector
> connector1(
682 new MockSharedWorkerConnector(renderer_host1
.get()));
683 connector0
->Create("http://example.com/w1.js",
686 kRenderFrameRouteIDs
[0]);
687 EXPECT_NE(MSG_ROUTING_NONE
, connector0
->route_id());
688 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
689 RunAllPendingInMessageLoop();
690 EXPECT_EQ(2U, renderer_host0
->QueuedMessageCount());
691 CheckWorkerProcessMsgCreateWorker(renderer_host0
.get(),
692 "http://example.com/w1.js",
694 blink::WebContentSecurityPolicyTypeReport
,
696 CheckViewMsgWorkerCreated(renderer_host0
.get(), connector0
.get());
697 connector1
->Create("http://example.com/w1.js",
700 kRenderFrameRouteIDs
[1]);
701 EXPECT_NE(MSG_ROUTING_NONE
, connector1
->route_id());
702 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
703 RunAllPendingInMessageLoop();
704 EXPECT_EQ(1U, renderer_host1
->QueuedMessageCount());
705 CheckViewMsgWorkerCreated(renderer_host1
.get(), connector1
.get());
708 // Normal case (URL mismatch).
710 scoped_ptr
<MockSharedWorkerConnector
> connector0(
711 new MockSharedWorkerConnector(renderer_host0
.get()));
712 scoped_ptr
<MockSharedWorkerConnector
> connector1(
713 new MockSharedWorkerConnector(renderer_host1
.get()));
714 connector0
->Create("http://example.com/w2.js",
717 kRenderFrameRouteIDs
[0]);
718 EXPECT_NE(MSG_ROUTING_NONE
, connector0
->route_id());
719 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
720 RunAllPendingInMessageLoop();
721 EXPECT_EQ(2U, renderer_host0
->QueuedMessageCount());
722 CheckWorkerProcessMsgCreateWorker(renderer_host0
.get(),
723 "http://example.com/w2.js",
725 blink::WebContentSecurityPolicyTypeReport
,
727 CheckViewMsgWorkerCreated(renderer_host0
.get(), connector0
.get());
728 connector1
->Create("http://example.com/w2x.js",
731 kRenderFrameRouteIDs
[1]);
732 EXPECT_EQ(MSG_ROUTING_NONE
, connector1
->route_id());
733 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
734 RunAllPendingInMessageLoop();
735 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
740 scoped_ptr
<MockSharedWorkerConnector
> connector0(
741 new MockSharedWorkerConnector(renderer_host0
.get()));
742 scoped_ptr
<MockSharedWorkerConnector
> connector1(
743 new MockSharedWorkerConnector(renderer_host1
.get()));
744 connector0
->Create("http://example.com/w3.js",
747 kRenderFrameRouteIDs
[0]);
748 EXPECT_NE(MSG_ROUTING_NONE
, connector0
->route_id());
749 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
750 connector1
->Create("http://example.com/w3.js",
753 kRenderFrameRouteIDs
[1]);
754 EXPECT_NE(MSG_ROUTING_NONE
, connector1
->route_id());
755 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
756 RunAllPendingInMessageLoop();
757 EXPECT_EQ(2U, renderer_host0
->QueuedMessageCount());
758 CheckWorkerProcessMsgCreateWorker(renderer_host0
.get(),
759 "http://example.com/w3.js",
761 blink::WebContentSecurityPolicyTypeReport
,
763 CheckViewMsgWorkerCreated(renderer_host0
.get(), connector0
.get());
764 EXPECT_EQ(1U, renderer_host1
->QueuedMessageCount());
765 CheckViewMsgWorkerCreated(renderer_host1
.get(), connector1
.get());
768 // Pending case (URL mismatch).
770 scoped_ptr
<MockSharedWorkerConnector
> connector0(
771 new MockSharedWorkerConnector(renderer_host0
.get()));
772 scoped_ptr
<MockSharedWorkerConnector
> connector1(
773 new MockSharedWorkerConnector(renderer_host1
.get()));
774 connector0
->Create("http://example.com/w4.js",
777 kRenderFrameRouteIDs
[0]);
778 EXPECT_NE(MSG_ROUTING_NONE
, connector0
->route_id());
779 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
780 connector1
->Create("http://example.com/w4x.js",
783 kRenderFrameRouteIDs
[1]);
784 EXPECT_EQ(MSG_ROUTING_NONE
, connector1
->route_id());
785 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
786 RunAllPendingInMessageLoop();
787 EXPECT_EQ(2U, renderer_host0
->QueuedMessageCount());
788 CheckWorkerProcessMsgCreateWorker(renderer_host0
.get(),
789 "http://example.com/w4.js",
791 blink::WebContentSecurityPolicyTypeReport
,
793 CheckViewMsgWorkerCreated(renderer_host0
.get(), connector0
.get());
794 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
798 TEST_F(SharedWorkerServiceImplTest
, CreateWorkerRaceTest
) {
799 // Create three renderer hosts.
800 scoped_ptr
<MockRendererProcessHost
> renderer_host0(
801 new MockRendererProcessHost(kProcessIDs
[0],
802 browser_context_
->GetResourceContext(),
804 scoped_ptr
<MockRendererProcessHost
> renderer_host1(
805 new MockRendererProcessHost(kProcessIDs
[1],
806 browser_context_
->GetResourceContext(),
808 scoped_ptr
<MockRendererProcessHost
> renderer_host2(
809 new MockRendererProcessHost(kProcessIDs
[2],
810 browser_context_
->GetResourceContext(),
814 scoped_ptr
<MockSharedWorkerConnector
> connector0(
815 new MockSharedWorkerConnector(renderer_host0
.get()));
816 scoped_ptr
<MockSharedWorkerConnector
> connector1(
817 new MockSharedWorkerConnector(renderer_host1
.get()));
818 scoped_ptr
<MockSharedWorkerConnector
> connector2(
819 new MockSharedWorkerConnector(renderer_host2
.get()));
820 connector0
->Create("http://example.com/w1.js",
823 kRenderFrameRouteIDs
[0]);
824 EXPECT_NE(MSG_ROUTING_NONE
, connector0
->route_id());
825 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
826 RunAllPendingInMessageLoop();
827 EXPECT_EQ(2U, renderer_host0
->QueuedMessageCount());
828 CheckWorkerProcessMsgCreateWorker(renderer_host0
.get(),
829 "http://example.com/w1.js",
831 blink::WebContentSecurityPolicyTypeReport
,
833 CheckViewMsgWorkerCreated(renderer_host0
.get(), connector0
.get());
834 renderer_host0
->FastShutdownIfPossible();
836 connector1
->Create("http://example.com/w1.js",
839 kRenderFrameRouteIDs
[1]);
840 EXPECT_NE(MSG_ROUTING_NONE
, connector1
->route_id());
841 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
842 RunAllPendingInMessageLoop();
843 EXPECT_EQ(2U, renderer_host1
->QueuedMessageCount());
844 CheckWorkerProcessMsgCreateWorker(renderer_host1
.get(),
845 "http://example.com/w1.js",
847 blink::WebContentSecurityPolicyTypeReport
,
849 CheckViewMsgWorkerCreated(renderer_host1
.get(), connector1
.get());
851 connector2
->Create("http://example.com/w1.js",
854 kRenderFrameRouteIDs
[2]);
855 EXPECT_NE(MSG_ROUTING_NONE
, connector2
->route_id());
856 EXPECT_EQ(0U, renderer_host2
->QueuedMessageCount());
857 RunAllPendingInMessageLoop();
858 EXPECT_EQ(1U, renderer_host2
->QueuedMessageCount());
859 CheckViewMsgWorkerCreated(renderer_host2
.get(), connector2
.get());
862 TEST_F(SharedWorkerServiceImplTest
, CreateWorkerRaceTest2
) {
863 // Create three renderer hosts.
864 scoped_ptr
<MockRendererProcessHost
> renderer_host0(
865 new MockRendererProcessHost(kProcessIDs
[0],
866 browser_context_
->GetResourceContext(),
868 scoped_ptr
<MockRendererProcessHost
> renderer_host1(
869 new MockRendererProcessHost(kProcessIDs
[1],
870 browser_context_
->GetResourceContext(),
872 scoped_ptr
<MockRendererProcessHost
> renderer_host2(
873 new MockRendererProcessHost(kProcessIDs
[2],
874 browser_context_
->GetResourceContext(),
878 scoped_ptr
<MockSharedWorkerConnector
> connector0(
879 new MockSharedWorkerConnector(renderer_host0
.get()));
880 scoped_ptr
<MockSharedWorkerConnector
> connector1(
881 new MockSharedWorkerConnector(renderer_host1
.get()));
882 scoped_ptr
<MockSharedWorkerConnector
> connector2(
883 new MockSharedWorkerConnector(renderer_host2
.get()));
884 connector0
->Create("http://example.com/w1.js",
887 kRenderFrameRouteIDs
[0]);
888 EXPECT_NE(MSG_ROUTING_NONE
, connector0
->route_id());
889 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
890 renderer_host0
->FastShutdownIfPossible();
892 connector1
->Create("http://example.com/w1.js",
895 kRenderFrameRouteIDs
[1]);
896 EXPECT_NE(MSG_ROUTING_NONE
, connector1
->route_id());
897 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
898 RunAllPendingInMessageLoop();
899 EXPECT_EQ(2U, renderer_host1
->QueuedMessageCount());
900 CheckWorkerProcessMsgCreateWorker(renderer_host1
.get(),
901 "http://example.com/w1.js",
903 blink::WebContentSecurityPolicyTypeReport
,
905 CheckViewMsgWorkerCreated(renderer_host1
.get(), connector1
.get());
907 connector2
->Create("http://example.com/w1.js",
910 kRenderFrameRouteIDs
[2]);
911 EXPECT_NE(MSG_ROUTING_NONE
, connector2
->route_id());
912 EXPECT_EQ(0U, renderer_host2
->QueuedMessageCount());
913 RunAllPendingInMessageLoop();
914 EXPECT_EQ(1U, renderer_host2
->QueuedMessageCount());
915 CheckViewMsgWorkerCreated(renderer_host2
.get(), connector2
.get());
918 } // namespace content