1 // Copyright (c) 2012 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.
5 #include "content/worker/websharedworker_stub.h"
7 #include "base/compiler_specific.h"
8 #include "content/child/child_process.h"
9 #include "content/child/child_thread.h"
10 #include "content/child/fileapi/file_system_dispatcher.h"
11 #include "content/child/webmessageportchannel_impl.h"
12 #include "content/common/worker_messages.h"
13 #include "content/worker/shared_worker_devtools_agent.h"
14 #include "content/worker/worker_thread.h"
15 #include "third_party/WebKit/public/web/WebSharedWorker.h"
16 #include "third_party/WebKit/public/platform/WebString.h"
17 #include "third_party/WebKit/public/platform/WebURL.h"
21 WebSharedWorkerStub::WebSharedWorkerStub(
24 const WorkerAppCacheInitInfo
& appcache_init_info
)
25 : route_id_(route_id
),
26 appcache_init_info_(appcache_init_info
),
27 client_(route_id
, this),
31 WorkerThread
* worker_thread
= WorkerThread::current();
32 DCHECK(worker_thread
);
33 worker_thread
->AddWorkerStub(this);
34 // Start processing incoming IPCs for this worker.
35 worker_thread
->AddRoute(route_id_
, this);
36 ChildProcess::current()->AddRefProcess();
38 // TODO(atwilson): Add support for NaCl when they support MessagePorts.
39 impl_
= WebKit::WebSharedWorker::create(client());
40 worker_devtools_agent_
.reset(new SharedWorkerDevToolsAgent(route_id
, impl_
));
41 client()->set_devtools_agent(worker_devtools_agent_
.get());
44 WebSharedWorkerStub::~WebSharedWorkerStub() {
45 impl_
->clientDestroyed();
46 WorkerThread
* worker_thread
= WorkerThread::current();
47 DCHECK(worker_thread
);
48 worker_thread
->RemoveWorkerStub(this);
49 worker_thread
->RemoveRoute(route_id_
);
50 ChildProcess::current()->ReleaseProcess();
53 void WebSharedWorkerStub::Shutdown() {
54 // The worker has exited - free ourselves and the client.
58 void WebSharedWorkerStub::EnsureWorkerContextTerminates() {
59 client_
.EnsureWorkerContextTerminates();
62 bool WebSharedWorkerStub::OnMessageReceived(const IPC::Message
& message
) {
63 if (worker_devtools_agent_
->OnMessageReceived(message
))
67 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerStub
, message
)
68 IPC_MESSAGE_HANDLER(WorkerMsg_StartWorkerContext
, OnStartWorkerContext
)
69 IPC_MESSAGE_HANDLER(WorkerMsg_TerminateWorkerContext
,
70 OnTerminateWorkerContext
)
71 IPC_MESSAGE_HANDLER(WorkerMsg_Connect
, OnConnect
)
72 IPC_MESSAGE_UNHANDLED(handled
= false)
77 void WebSharedWorkerStub::OnChannelError() {
78 OnTerminateWorkerContext();
81 const GURL
& WebSharedWorkerStub::url() {
85 void WebSharedWorkerStub::OnStartWorkerContext(
86 const GURL
& url
, const string16
& user_agent
, const string16
& source_code
,
87 const string16
& content_security_policy
,
88 WebKit::WebContentSecurityPolicyType policy_type
) {
89 // Ignore multiple attempts to start this worker (can happen if two pages
90 // try to start it simultaneously).
94 impl_
->startWorkerContext(url
, name_
, user_agent
, source_code
,
95 content_security_policy
, policy_type
, 0);
99 // Process any pending connections.
100 for (PendingConnectInfoList::const_iterator iter
= pending_connects_
.begin();
101 iter
!= pending_connects_
.end();
103 OnConnect(iter
->first
, iter
->second
);
105 pending_connects_
.clear();
108 void WebSharedWorkerStub::OnConnect(int sent_message_port_id
, int routing_id
) {
110 WebKit::WebMessagePortChannel
* channel
=
111 new WebMessagePortChannelImpl(routing_id
,
112 sent_message_port_id
,
113 base::MessageLoopProxy::current().get());
114 impl_
->connect(channel
, NULL
);
116 // If two documents try to load a SharedWorker at the same time, the
117 // WorkerMsg_Connect for one of the documents can come in before the
118 // worker is started. Just queue up the connect and deliver it once the
120 PendingConnectInfo
pending_connect(sent_message_port_id
, routing_id
);
121 pending_connects_
.push_back(pending_connect
);
125 void WebSharedWorkerStub::OnTerminateWorkerContext() {
126 impl_
->terminateWorkerContext();
128 // Call the client to make sure context exits.
129 EnsureWorkerContextTerminates();
133 } // namespace content