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 base::Tuple
<WorkerProcessMsg_CreateWorker_Params
> param
;
331 EXPECT_TRUE(WorkerProcessMsg_CreateWorker::Read(msg
.get(), ¶m
));
332 EXPECT_EQ(GURL(expected_url
), base::get
<0>(param
).url
);
333 EXPECT_EQ(base::ASCIIToUTF16(expected_name
), base::get
<0>(param
).name
);
334 EXPECT_EQ(expected_security_policy_type
,
335 base::get
<0>(param
).security_policy_type
);
336 *route_id
= base::get
<0>(param
).route_id
;
339 void CheckViewMsgWorkerCreated(MockRendererProcessHost
* renderer_host
,
340 MockSharedWorkerConnector
* connector
) {
341 scoped_ptr
<IPC::Message
> msg(renderer_host
->PopMessage());
342 EXPECT_EQ(ViewMsg_WorkerCreated::ID
, msg
->type());
343 EXPECT_EQ(connector
->route_id(), msg
->routing_id());
346 void CheckMessagePortMsgMessagesQueued(MockRendererProcessHost
* renderer_host
,
347 MockSharedWorkerConnector
* connector
) {
348 scoped_ptr
<IPC::Message
> msg(renderer_host
->PopMessage());
349 EXPECT_EQ(MessagePortMsg_MessagesQueued::ID
, msg
->type());
350 EXPECT_EQ(connector
->temporary_remote_port_route_id(), msg
->routing_id());
353 void CheckWorkerMsgConnect(MockRendererProcessHost
* renderer_host
,
354 int expected_msg_route_id
,
355 int expected_sent_message_port_id
,
357 scoped_ptr
<IPC::Message
> msg(renderer_host
->PopMessage());
358 EXPECT_EQ(WorkerMsg_Connect::ID
, msg
->type());
359 EXPECT_EQ(expected_msg_route_id
, msg
->routing_id());
360 WorkerMsg_Connect::Param params
;
361 EXPECT_TRUE(WorkerMsg_Connect::Read(msg
.get(), ¶ms
));
362 int port_id
= base::get
<0>(params
);
363 *routing_id
= base::get
<1>(params
);
364 EXPECT_EQ(expected_sent_message_port_id
, port_id
);
367 void CheckMessagePortMsgMessage(MockRendererProcessHost
* renderer_host
,
368 int expected_msg_route_id
,
369 std::string expected_data
) {
370 scoped_ptr
<IPC::Message
> msg(renderer_host
->PopMessage());
371 EXPECT_EQ(MessagePortMsg_Message::ID
, msg
->type());
372 EXPECT_EQ(expected_msg_route_id
, msg
->routing_id());
373 MessagePortMsg_Message::Param params
;
374 EXPECT_TRUE(MessagePortMsg_Message::Read(msg
.get(), ¶ms
));
375 base::string16 data
= base::get
<0>(params
).message_as_string
;
376 EXPECT_EQ(base::ASCIIToUTF16(expected_data
), data
);
379 void CheckViewMsgWorkerConnected(MockRendererProcessHost
* renderer_host
,
380 MockSharedWorkerConnector
* connector
) {
381 scoped_ptr
<IPC::Message
> msg(renderer_host
->PopMessage());
382 EXPECT_EQ(ViewMsg_WorkerConnected::ID
, msg
->type());
383 EXPECT_EQ(connector
->route_id(), msg
->routing_id());
388 TEST_F(SharedWorkerServiceImplTest
, BasicTest
) {
389 scoped_ptr
<MockRendererProcessHost
> renderer_host(
390 new MockRendererProcessHost(kProcessIDs
[0],
391 browser_context_
->GetResourceContext(),
393 scoped_ptr
<MockSharedWorkerConnector
> connector(
394 new MockSharedWorkerConnector(renderer_host
.get()));
396 int worker_msg_port_route_id
;
398 // SharedWorkerConnector creates two message ports and sends
399 // ViewHostMsg_CreateWorker.
400 connector
->Create("http://example.com/w.js",
403 kRenderFrameRouteIDs
[0]);
404 // We need to go to UI thread to call ReserveRenderProcessOnUI().
405 RunAllPendingInMessageLoop();
406 EXPECT_EQ(2U, renderer_host
->QueuedMessageCount());
407 // WorkerProcessMsg_CreateWorker should be sent to the renderer in which
408 // SharedWorker will be created.
409 CheckWorkerProcessMsgCreateWorker(renderer_host
.get(),
410 "http://example.com/w.js",
412 blink::WebContentSecurityPolicyTypeReport
,
414 // ViewMsg_WorkerCreated(1) should be sent back to SharedWorkerConnector side.
415 CheckViewMsgWorkerCreated(renderer_host
.get(), connector
.get());
417 // SharedWorkerConnector side sends MessagePortHostMsg_QueueMessages in
418 // WebSharedWorkerProxy::connect.
419 connector
->SendQueueMessages();
420 EXPECT_EQ(1U, renderer_host
->QueuedMessageCount());
421 // MessagePortMsg_MessagesQueued(2) should be sent back to
422 // SharedWorkerConnector side.
423 CheckMessagePortMsgMessagesQueued(renderer_host
.get(), connector
.get());
425 // When SharedWorkerConnector receives ViewMsg_WorkerCreated(1), it sends
426 // WorkerMsg_Connect wrapped in ViewHostMsg_ForwardToWorker.
427 connector
->SendConnect();
428 EXPECT_EQ(1U, renderer_host
->QueuedMessageCount());
429 // WorkerMsg_Connect should be sent to SharedWorker side.
430 CheckWorkerMsgConnect(renderer_host
.get(),
432 connector
->remote_port_id(),
433 &worker_msg_port_route_id
);
435 // When SharedWorkerConnector receives MessagePortMsg_MessagesQueued(2), it
436 // sends MessagePortHostMsg_SendQueuedMessages.
437 std::vector
<QueuedMessage
> empty_messages
;
438 connector
->SendSendQueuedMessages(empty_messages
);
439 EXPECT_EQ(0U, renderer_host
->QueuedMessageCount());
441 // SharedWorker sends WorkerHostMsg_WorkerReadyForInspection in
442 // EmbeddedSharedWorkerStub::WorkerReadyForInspection().
443 EXPECT_TRUE(renderer_host
->OnMessageReceived(
444 new WorkerHostMsg_WorkerReadyForInspection(worker_route_id
)));
445 EXPECT_EQ(0U, renderer_host
->QueuedMessageCount());
447 // SharedWorker sends WorkerHostMsg_WorkerScriptLoaded in
448 // EmbeddedSharedWorkerStub::workerScriptLoaded().
449 EXPECT_TRUE(renderer_host
->OnMessageReceived(
450 new WorkerHostMsg_WorkerScriptLoaded(worker_route_id
)));
451 EXPECT_EQ(0U, renderer_host
->QueuedMessageCount());
453 // SharedWorker sends WorkerHostMsg_WorkerConnected in
454 // EmbeddedSharedWorkerStub::workerScriptLoaded().
456 renderer_host
->OnMessageReceived(new WorkerHostMsg_WorkerConnected(
457 connector
->remote_port_id(), worker_route_id
)));
458 EXPECT_EQ(1U, renderer_host
->QueuedMessageCount());
459 // ViewMsg_WorkerConnected should be sent to SharedWorkerConnector side.
460 CheckViewMsgWorkerConnected(renderer_host
.get(), connector
.get());
462 // When SharedWorkerConnector side sends MessagePortHostMsg_PostMessage,
463 // SharedWorker side shuold receive MessagePortMsg_Message.
464 connector
->SendPostMessage("test1");
465 EXPECT_EQ(1U, renderer_host
->QueuedMessageCount());
466 CheckMessagePortMsgMessage(
467 renderer_host
.get(), worker_msg_port_route_id
, "test1");
469 // When SharedWorker side sends MessagePortHostMsg_PostMessage,
470 // SharedWorkerConnector side shuold receive MessagePortMsg_Message.
471 const std::vector
<TransferredMessagePort
> empty_ports
;
473 renderer_host
->OnMessageReceived(new MessagePortHostMsg_PostMessage(
474 connector
->remote_port_id(),
475 MessagePortMessage(base::ASCIIToUTF16("test2")), empty_ports
)));
476 EXPECT_EQ(1U, renderer_host
->QueuedMessageCount());
477 CheckMessagePortMsgMessage(
478 renderer_host
.get(), connector
->local_port_route_id(), "test2");
480 // UpdateWorkerDependency should not be called.
481 EXPECT_EQ(0, s_update_worker_dependency_call_count_
);
484 TEST_F(SharedWorkerServiceImplTest
, TwoRendererTest
) {
485 // The first renderer host.
486 scoped_ptr
<MockRendererProcessHost
> renderer_host0(
487 new MockRendererProcessHost(kProcessIDs
[0],
488 browser_context_
->GetResourceContext(),
490 scoped_ptr
<MockSharedWorkerConnector
> connector0(
491 new MockSharedWorkerConnector(renderer_host0
.get()));
493 int worker_msg_port_route_id1
;
495 // SharedWorkerConnector creates two message ports and sends
496 // ViewHostMsg_CreateWorker.
497 connector0
->Create("http://example.com/w.js",
500 kRenderFrameRouteIDs
[0]);
501 // We need to go to UI thread to call ReserveRenderProcessOnUI().
502 RunAllPendingInMessageLoop();
503 EXPECT_EQ(2U, renderer_host0
->QueuedMessageCount());
504 // WorkerProcessMsg_CreateWorker should be sent to the renderer in which
505 // SharedWorker will be created.
506 CheckWorkerProcessMsgCreateWorker(renderer_host0
.get(),
507 "http://example.com/w.js",
509 blink::WebContentSecurityPolicyTypeReport
,
511 // ViewMsg_WorkerCreated(1) should be sent back to SharedWorkerConnector side.
512 CheckViewMsgWorkerCreated(renderer_host0
.get(), connector0
.get());
514 // SharedWorkerConnector side sends MessagePortHostMsg_QueueMessages in
515 // WebSharedWorkerProxy::connect.
516 connector0
->SendQueueMessages();
517 EXPECT_EQ(1U, renderer_host0
->QueuedMessageCount());
518 // MessagePortMsg_MessagesQueued(2) should be sent back to
519 // SharedWorkerConnector side.
520 CheckMessagePortMsgMessagesQueued(renderer_host0
.get(), connector0
.get());
522 // When SharedWorkerConnector receives ViewMsg_WorkerCreated(1), it sends
523 // WorkerMsg_Connect wrapped in ViewHostMsg_ForwardToWorker.
524 connector0
->SendConnect();
525 EXPECT_EQ(1U, renderer_host0
->QueuedMessageCount());
526 // WorkerMsg_Connect should be sent to SharedWorker side.
527 CheckWorkerMsgConnect(renderer_host0
.get(),
529 connector0
->remote_port_id(),
530 &worker_msg_port_route_id1
);
532 // When SharedWorkerConnector receives MessagePortMsg_MessagesQueued(2), it
533 // sends MessagePortHostMsg_SendQueuedMessages.
534 std::vector
<QueuedMessage
> empty_messages
;
535 connector0
->SendSendQueuedMessages(empty_messages
);
536 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
538 // SharedWorker sends WorkerHostMsg_WorkerReadyForInspection in
539 // EmbeddedSharedWorkerStub::WorkerReadyForInspection().
540 EXPECT_TRUE(renderer_host0
->OnMessageReceived(
541 new WorkerHostMsg_WorkerReadyForInspection(worker_route_id
)));
542 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
544 // SharedWorker sends WorkerHostMsg_WorkerScriptLoaded in
545 // EmbeddedSharedWorkerStub::workerScriptLoaded().
546 EXPECT_TRUE(renderer_host0
->OnMessageReceived(
547 new WorkerHostMsg_WorkerScriptLoaded(worker_route_id
)));
548 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
550 // SharedWorker sends WorkerHostMsg_WorkerConnected in
551 // EmbeddedSharedWorkerStub::workerScriptLoaded().
553 renderer_host0
->OnMessageReceived(new WorkerHostMsg_WorkerConnected(
554 connector0
->remote_port_id(), worker_route_id
)));
555 EXPECT_EQ(1U, renderer_host0
->QueuedMessageCount());
556 // ViewMsg_WorkerConnected should be sent to SharedWorkerConnector side.
557 CheckViewMsgWorkerConnected(renderer_host0
.get(), connector0
.get());
559 // When SharedWorkerConnector side sends MessagePortHostMsg_PostMessage,
560 // SharedWorker side shuold receive MessagePortMsg_Message.
561 connector0
->SendPostMessage("test1");
562 EXPECT_EQ(1U, renderer_host0
->QueuedMessageCount());
563 CheckMessagePortMsgMessage(
564 renderer_host0
.get(), worker_msg_port_route_id1
, "test1");
566 // When SharedWorker side sends MessagePortHostMsg_PostMessage,
567 // SharedWorkerConnector side shuold receive MessagePortMsg_Message.
568 const std::vector
<TransferredMessagePort
> empty_ports
;
570 renderer_host0
->OnMessageReceived(new MessagePortHostMsg_PostMessage(
571 connector0
->remote_port_id(),
572 MessagePortMessage(base::ASCIIToUTF16("test2")), empty_ports
)));
573 EXPECT_EQ(1U, renderer_host0
->QueuedMessageCount());
574 CheckMessagePortMsgMessage(
575 renderer_host0
.get(), connector0
->local_port_route_id(), "test2");
577 // The second renderer host.
578 scoped_ptr
<MockRendererProcessHost
> renderer_host1(
579 new MockRendererProcessHost(kProcessIDs
[1],
580 browser_context_
->GetResourceContext(),
582 scoped_ptr
<MockSharedWorkerConnector
> connector1(
583 new MockSharedWorkerConnector(renderer_host1
.get()));
584 int worker_msg_port_route_id2
;
586 // UpdateWorkerDependency should not be called yet.
587 EXPECT_EQ(0, s_update_worker_dependency_call_count_
);
589 // SharedWorkerConnector creates two message ports and sends
590 // ViewHostMsg_CreateWorker.
591 connector1
->Create("http://example.com/w.js",
594 kRenderFrameRouteIDs
[1]);
595 // We need to go to UI thread to call ReserveRenderProcessOnUI().
596 RunAllPendingInMessageLoop();
597 EXPECT_EQ(1U, renderer_host1
->QueuedMessageCount());
598 // ViewMsg_WorkerCreated(3) should be sent back to SharedWorkerConnector side.
599 CheckViewMsgWorkerCreated(renderer_host1
.get(), connector1
.get());
601 // UpdateWorkerDependency should be called.
602 EXPECT_EQ(1, s_update_worker_dependency_call_count_
);
603 EXPECT_EQ(1U, s_worker_dependency_added_ids_
.size());
604 EXPECT_EQ(kProcessIDs
[0], s_worker_dependency_added_ids_
[0]);
605 EXPECT_EQ(0U, s_worker_dependency_removed_ids_
.size());
607 // SharedWorkerConnector side sends MessagePortHostMsg_QueueMessages in
608 // WebSharedWorkerProxy::connect.
609 connector1
->SendQueueMessages();
610 EXPECT_EQ(1U, renderer_host1
->QueuedMessageCount());
611 // MessagePortMsg_MessagesQueued(4) should be sent back to
612 // SharedWorkerConnector side.
613 CheckMessagePortMsgMessagesQueued(renderer_host1
.get(), connector1
.get());
615 // When SharedWorkerConnector receives ViewMsg_WorkerCreated(3), it sends
616 // WorkerMsg_Connect wrapped in ViewHostMsg_ForwardToWorker.
617 connector1
->SendConnect();
618 EXPECT_EQ(1U, renderer_host0
->QueuedMessageCount());
619 // WorkerMsg_Connect should be sent to SharedWorker side.
620 CheckWorkerMsgConnect(renderer_host0
.get(),
622 connector1
->remote_port_id(),
623 &worker_msg_port_route_id2
);
625 // When SharedWorkerConnector receives MessagePortMsg_MessagesQueued(4), it
626 // sends MessagePortHostMsg_SendQueuedMessages.
627 connector1
->SendSendQueuedMessages(empty_messages
);
628 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
630 // SharedWorker sends WorkerHostMsg_WorkerConnected in
631 // EmbeddedSharedWorkerStub::OnConnect().
633 renderer_host0
->OnMessageReceived(new WorkerHostMsg_WorkerConnected(
634 connector1
->remote_port_id(), worker_route_id
)));
635 EXPECT_EQ(1U, renderer_host1
->QueuedMessageCount());
636 // ViewMsg_WorkerConnected should be sent to SharedWorkerConnector side.
637 CheckViewMsgWorkerConnected(renderer_host1
.get(), connector1
.get());
639 // When SharedWorkerConnector side sends MessagePortHostMsg_PostMessage,
640 // SharedWorker side shuold receive MessagePortMsg_Message.
641 connector1
->SendPostMessage("test3");
642 EXPECT_EQ(1U, renderer_host0
->QueuedMessageCount());
643 CheckMessagePortMsgMessage(
644 renderer_host0
.get(), worker_msg_port_route_id2
, "test3");
646 // When SharedWorker side sends MessagePortHostMsg_PostMessage,
647 // SharedWorkerConnector side shuold receive MessagePortMsg_Message.
649 renderer_host0
->OnMessageReceived(new MessagePortHostMsg_PostMessage(
650 connector1
->remote_port_id(),
651 MessagePortMessage(base::ASCIIToUTF16("test4")), empty_ports
)));
652 EXPECT_EQ(1U, renderer_host1
->QueuedMessageCount());
653 CheckMessagePortMsgMessage(
654 renderer_host1
.get(), connector1
->local_port_route_id(), "test4");
656 EXPECT_EQ(1, s_update_worker_dependency_call_count_
);
657 renderer_host1
.reset();
658 // UpdateWorkerDependency should be called.
659 EXPECT_EQ(2, s_update_worker_dependency_call_count_
);
660 EXPECT_EQ(0U, s_worker_dependency_added_ids_
.size());
661 EXPECT_EQ(1U, s_worker_dependency_removed_ids_
.size());
662 EXPECT_EQ(kProcessIDs
[0], s_worker_dependency_removed_ids_
[0]);
665 TEST_F(SharedWorkerServiceImplTest
, CreateWorkerTest
) {
666 // The first renderer host.
667 scoped_ptr
<MockRendererProcessHost
> renderer_host0(
668 new MockRendererProcessHost(kProcessIDs
[0],
669 browser_context_
->GetResourceContext(),
671 // The second renderer host.
672 scoped_ptr
<MockRendererProcessHost
> renderer_host1(
673 new MockRendererProcessHost(kProcessIDs
[1],
674 browser_context_
->GetResourceContext(),
680 scoped_ptr
<MockSharedWorkerConnector
> connector0(
681 new MockSharedWorkerConnector(renderer_host0
.get()));
682 scoped_ptr
<MockSharedWorkerConnector
> connector1(
683 new MockSharedWorkerConnector(renderer_host1
.get()));
684 connector0
->Create("http://example.com/w1.js",
687 kRenderFrameRouteIDs
[0]);
688 EXPECT_NE(MSG_ROUTING_NONE
, connector0
->route_id());
689 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
690 RunAllPendingInMessageLoop();
691 EXPECT_EQ(2U, renderer_host0
->QueuedMessageCount());
692 CheckWorkerProcessMsgCreateWorker(renderer_host0
.get(),
693 "http://example.com/w1.js",
695 blink::WebContentSecurityPolicyTypeReport
,
697 CheckViewMsgWorkerCreated(renderer_host0
.get(), connector0
.get());
698 connector1
->Create("http://example.com/w1.js",
701 kRenderFrameRouteIDs
[1]);
702 EXPECT_NE(MSG_ROUTING_NONE
, connector1
->route_id());
703 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
704 RunAllPendingInMessageLoop();
705 EXPECT_EQ(1U, renderer_host1
->QueuedMessageCount());
706 CheckViewMsgWorkerCreated(renderer_host1
.get(), connector1
.get());
709 // Normal case (URL mismatch).
711 scoped_ptr
<MockSharedWorkerConnector
> connector0(
712 new MockSharedWorkerConnector(renderer_host0
.get()));
713 scoped_ptr
<MockSharedWorkerConnector
> connector1(
714 new MockSharedWorkerConnector(renderer_host1
.get()));
715 connector0
->Create("http://example.com/w2.js",
718 kRenderFrameRouteIDs
[0]);
719 EXPECT_NE(MSG_ROUTING_NONE
, connector0
->route_id());
720 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
721 RunAllPendingInMessageLoop();
722 EXPECT_EQ(2U, renderer_host0
->QueuedMessageCount());
723 CheckWorkerProcessMsgCreateWorker(renderer_host0
.get(),
724 "http://example.com/w2.js",
726 blink::WebContentSecurityPolicyTypeReport
,
728 CheckViewMsgWorkerCreated(renderer_host0
.get(), connector0
.get());
729 connector1
->Create("http://example.com/w2x.js",
732 kRenderFrameRouteIDs
[1]);
733 EXPECT_EQ(MSG_ROUTING_NONE
, connector1
->route_id());
734 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
735 RunAllPendingInMessageLoop();
736 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
741 scoped_ptr
<MockSharedWorkerConnector
> connector0(
742 new MockSharedWorkerConnector(renderer_host0
.get()));
743 scoped_ptr
<MockSharedWorkerConnector
> connector1(
744 new MockSharedWorkerConnector(renderer_host1
.get()));
745 connector0
->Create("http://example.com/w3.js",
748 kRenderFrameRouteIDs
[0]);
749 EXPECT_NE(MSG_ROUTING_NONE
, connector0
->route_id());
750 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
751 connector1
->Create("http://example.com/w3.js",
754 kRenderFrameRouteIDs
[1]);
755 EXPECT_NE(MSG_ROUTING_NONE
, connector1
->route_id());
756 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
757 RunAllPendingInMessageLoop();
758 EXPECT_EQ(2U, renderer_host0
->QueuedMessageCount());
759 CheckWorkerProcessMsgCreateWorker(renderer_host0
.get(),
760 "http://example.com/w3.js",
762 blink::WebContentSecurityPolicyTypeReport
,
764 CheckViewMsgWorkerCreated(renderer_host0
.get(), connector0
.get());
765 EXPECT_EQ(1U, renderer_host1
->QueuedMessageCount());
766 CheckViewMsgWorkerCreated(renderer_host1
.get(), connector1
.get());
769 // Pending case (URL mismatch).
771 scoped_ptr
<MockSharedWorkerConnector
> connector0(
772 new MockSharedWorkerConnector(renderer_host0
.get()));
773 scoped_ptr
<MockSharedWorkerConnector
> connector1(
774 new MockSharedWorkerConnector(renderer_host1
.get()));
775 connector0
->Create("http://example.com/w4.js",
778 kRenderFrameRouteIDs
[0]);
779 EXPECT_NE(MSG_ROUTING_NONE
, connector0
->route_id());
780 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
781 connector1
->Create("http://example.com/w4x.js",
784 kRenderFrameRouteIDs
[1]);
785 EXPECT_EQ(MSG_ROUTING_NONE
, connector1
->route_id());
786 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
787 RunAllPendingInMessageLoop();
788 EXPECT_EQ(2U, renderer_host0
->QueuedMessageCount());
789 CheckWorkerProcessMsgCreateWorker(renderer_host0
.get(),
790 "http://example.com/w4.js",
792 blink::WebContentSecurityPolicyTypeReport
,
794 CheckViewMsgWorkerCreated(renderer_host0
.get(), connector0
.get());
795 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
799 TEST_F(SharedWorkerServiceImplTest
, CreateWorkerRaceTest
) {
800 // Create three renderer hosts.
801 scoped_ptr
<MockRendererProcessHost
> renderer_host0(
802 new MockRendererProcessHost(kProcessIDs
[0],
803 browser_context_
->GetResourceContext(),
805 scoped_ptr
<MockRendererProcessHost
> renderer_host1(
806 new MockRendererProcessHost(kProcessIDs
[1],
807 browser_context_
->GetResourceContext(),
809 scoped_ptr
<MockRendererProcessHost
> renderer_host2(
810 new MockRendererProcessHost(kProcessIDs
[2],
811 browser_context_
->GetResourceContext(),
815 scoped_ptr
<MockSharedWorkerConnector
> connector0(
816 new MockSharedWorkerConnector(renderer_host0
.get()));
817 scoped_ptr
<MockSharedWorkerConnector
> connector1(
818 new MockSharedWorkerConnector(renderer_host1
.get()));
819 scoped_ptr
<MockSharedWorkerConnector
> connector2(
820 new MockSharedWorkerConnector(renderer_host2
.get()));
821 connector0
->Create("http://example.com/w1.js",
824 kRenderFrameRouteIDs
[0]);
825 EXPECT_NE(MSG_ROUTING_NONE
, connector0
->route_id());
826 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
827 RunAllPendingInMessageLoop();
828 EXPECT_EQ(2U, renderer_host0
->QueuedMessageCount());
829 CheckWorkerProcessMsgCreateWorker(renderer_host0
.get(),
830 "http://example.com/w1.js",
832 blink::WebContentSecurityPolicyTypeReport
,
834 CheckViewMsgWorkerCreated(renderer_host0
.get(), connector0
.get());
835 renderer_host0
->FastShutdownIfPossible();
837 connector1
->Create("http://example.com/w1.js",
840 kRenderFrameRouteIDs
[1]);
841 EXPECT_NE(MSG_ROUTING_NONE
, connector1
->route_id());
842 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
843 RunAllPendingInMessageLoop();
844 EXPECT_EQ(2U, renderer_host1
->QueuedMessageCount());
845 CheckWorkerProcessMsgCreateWorker(renderer_host1
.get(),
846 "http://example.com/w1.js",
848 blink::WebContentSecurityPolicyTypeReport
,
850 CheckViewMsgWorkerCreated(renderer_host1
.get(), connector1
.get());
852 connector2
->Create("http://example.com/w1.js",
855 kRenderFrameRouteIDs
[2]);
856 EXPECT_NE(MSG_ROUTING_NONE
, connector2
->route_id());
857 EXPECT_EQ(0U, renderer_host2
->QueuedMessageCount());
858 RunAllPendingInMessageLoop();
859 EXPECT_EQ(1U, renderer_host2
->QueuedMessageCount());
860 CheckViewMsgWorkerCreated(renderer_host2
.get(), connector2
.get());
863 TEST_F(SharedWorkerServiceImplTest
, CreateWorkerRaceTest2
) {
864 // Create three renderer hosts.
865 scoped_ptr
<MockRendererProcessHost
> renderer_host0(
866 new MockRendererProcessHost(kProcessIDs
[0],
867 browser_context_
->GetResourceContext(),
869 scoped_ptr
<MockRendererProcessHost
> renderer_host1(
870 new MockRendererProcessHost(kProcessIDs
[1],
871 browser_context_
->GetResourceContext(),
873 scoped_ptr
<MockRendererProcessHost
> renderer_host2(
874 new MockRendererProcessHost(kProcessIDs
[2],
875 browser_context_
->GetResourceContext(),
879 scoped_ptr
<MockSharedWorkerConnector
> connector0(
880 new MockSharedWorkerConnector(renderer_host0
.get()));
881 scoped_ptr
<MockSharedWorkerConnector
> connector1(
882 new MockSharedWorkerConnector(renderer_host1
.get()));
883 scoped_ptr
<MockSharedWorkerConnector
> connector2(
884 new MockSharedWorkerConnector(renderer_host2
.get()));
885 connector0
->Create("http://example.com/w1.js",
888 kRenderFrameRouteIDs
[0]);
889 EXPECT_NE(MSG_ROUTING_NONE
, connector0
->route_id());
890 EXPECT_EQ(0U, renderer_host0
->QueuedMessageCount());
891 renderer_host0
->FastShutdownIfPossible();
893 connector1
->Create("http://example.com/w1.js",
896 kRenderFrameRouteIDs
[1]);
897 EXPECT_NE(MSG_ROUTING_NONE
, connector1
->route_id());
898 EXPECT_EQ(0U, renderer_host1
->QueuedMessageCount());
899 RunAllPendingInMessageLoop();
900 EXPECT_EQ(2U, renderer_host1
->QueuedMessageCount());
901 CheckWorkerProcessMsgCreateWorker(renderer_host1
.get(),
902 "http://example.com/w1.js",
904 blink::WebContentSecurityPolicyTypeReport
,
906 CheckViewMsgWorkerCreated(renderer_host1
.get(), connector1
.get());
908 connector2
->Create("http://example.com/w1.js",
911 kRenderFrameRouteIDs
[2]);
912 EXPECT_NE(MSG_ROUTING_NONE
, connector2
->route_id());
913 EXPECT_EQ(0U, renderer_host2
->QueuedMessageCount());
914 RunAllPendingInMessageLoop();
915 EXPECT_EQ(1U, renderer_host2
->QueuedMessageCount());
916 CheckViewMsgWorkerCreated(renderer_host2
.get(), connector2
.get());
919 } // namespace content