OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/worker/websharedworker_stub.h" | 5 #include "content/worker/websharedworker_stub.h" |
6 | 6 |
| 7 #include "content/common/child_process.h" |
7 #include "content/common/child_thread.h" | 8 #include "content/common/child_thread.h" |
8 #include "content/common/file_system/file_system_dispatcher.h" | 9 #include "content/common/file_system/file_system_dispatcher.h" |
9 #include "content/common/webmessageportchannel_impl.h" | 10 #include "content/common/webmessageportchannel_impl.h" |
10 #include "content/common/worker_messages.h" | 11 #include "content/common/worker_messages.h" |
| 12 #include "base/compiler_specific.h" |
| 13 #include "content/worker/worker_thread.h" |
11 #include "content/worker/shared_worker_devtools_agent.h" | 14 #include "content/worker/shared_worker_devtools_agent.h" |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSharedWorker.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSharedWorker.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" |
15 | 18 |
16 WebSharedWorkerStub::WebSharedWorkerStub( | 19 WebSharedWorkerStub::WebSharedWorkerStub( |
17 const string16& name, int route_id, | 20 const string16& name, int route_id, |
18 const WorkerAppCacheInitInfo& appcache_init_info) | 21 const WorkerAppCacheInitInfo& appcache_init_info) |
19 : WebWorkerStubBase(route_id, appcache_init_info), | 22 : route_id_(route_id), |
| 23 appcache_init_info_(appcache_init_info), |
| 24 ALLOW_THIS_IN_INITIALIZER_LIST(client_(route_id, this)), |
20 name_(name), | 25 name_(name), |
21 started_(false), | 26 started_(false), |
22 worker_devtools_agent_(NULL) { | 27 worker_devtools_agent_(NULL) { |
| 28 |
| 29 WorkerThread* worker_thread = WorkerThread::current(); |
| 30 DCHECK(worker_thread); |
| 31 worker_thread->AddWorkerStub(this); |
| 32 // Start processing incoming IPCs for this worker. |
| 33 worker_thread->AddRoute(route_id_, this); |
| 34 ChildProcess::current()->AddRefProcess(); |
| 35 |
23 // TODO(atwilson): Add support for NaCl when they support MessagePorts. | 36 // TODO(atwilson): Add support for NaCl when they support MessagePorts. |
24 impl_ = WebKit::WebSharedWorker::create(client()); | 37 impl_ = WebKit::WebSharedWorker::create(client()); |
25 worker_devtools_agent_.reset(new SharedWorkerDevToolsAgent(route_id, impl_)); | 38 worker_devtools_agent_.reset(new SharedWorkerDevToolsAgent(route_id, impl_)); |
26 client()->set_devtools_agent(worker_devtools_agent_.get()); | 39 client()->set_devtools_agent(worker_devtools_agent_.get()); |
27 } | 40 } |
28 | 41 |
29 WebSharedWorkerStub::~WebSharedWorkerStub() { | 42 WebSharedWorkerStub::~WebSharedWorkerStub() { |
30 impl_->clientDestroyed(); | 43 impl_->clientDestroyed(); |
| 44 WorkerThread* worker_thread = WorkerThread::current(); |
| 45 DCHECK(worker_thread); |
| 46 worker_thread->RemoveWorkerStub(this); |
| 47 worker_thread->RemoveRoute(route_id_); |
| 48 ChildProcess::current()->ReleaseProcess(); |
| 49 } |
| 50 |
| 51 void WebSharedWorkerStub::Shutdown() { |
| 52 // The worker has exited - free ourselves and the client. |
| 53 delete this; |
| 54 } |
| 55 |
| 56 void WebSharedWorkerStub::EnsureWorkerContextTerminates() { |
| 57 client_.EnsureWorkerContextTerminates(); |
31 } | 58 } |
32 | 59 |
33 bool WebSharedWorkerStub::OnMessageReceived(const IPC::Message& message) { | 60 bool WebSharedWorkerStub::OnMessageReceived(const IPC::Message& message) { |
34 if (worker_devtools_agent_->OnMessageReceived(message)) | 61 if (worker_devtools_agent_->OnMessageReceived(message)) |
35 return true; | 62 return true; |
36 | 63 |
37 bool handled = true; | 64 bool handled = true; |
38 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerStub, message) | 65 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerStub, message) |
39 IPC_MESSAGE_HANDLER(WorkerMsg_StartWorkerContext, OnStartWorkerContext) | 66 IPC_MESSAGE_HANDLER(WorkerMsg_StartWorkerContext, OnStartWorkerContext) |
40 IPC_MESSAGE_HANDLER(WorkerMsg_TerminateWorkerContext, | 67 IPC_MESSAGE_HANDLER(WorkerMsg_TerminateWorkerContext, |
41 OnTerminateWorkerContext) | 68 OnTerminateWorkerContext) |
42 IPC_MESSAGE_HANDLER(WorkerMsg_Connect, OnConnect) | 69 IPC_MESSAGE_HANDLER(WorkerMsg_Connect, OnConnect) |
43 IPC_MESSAGE_UNHANDLED(handled = false) | 70 IPC_MESSAGE_UNHANDLED(handled = false) |
44 IPC_END_MESSAGE_MAP() | 71 IPC_END_MESSAGE_MAP() |
45 return handled; | 72 return handled; |
46 } | 73 } |
47 | 74 |
48 void WebSharedWorkerStub::OnChannelError() { | 75 void WebSharedWorkerStub::OnChannelError() { |
49 OnTerminateWorkerContext(); | 76 OnTerminateWorkerContext(); |
50 } | 77 } |
51 | 78 |
52 const GURL& WebSharedWorkerStub::url() const { | 79 const GURL& WebSharedWorkerStub::url() { |
53 return url_; | 80 return url_; |
54 } | 81 } |
55 | 82 |
56 void WebSharedWorkerStub::OnStartWorkerContext( | 83 void WebSharedWorkerStub::OnStartWorkerContext( |
57 const GURL& url, const string16& user_agent, const string16& source_code) { | 84 const GURL& url, const string16& user_agent, const string16& source_code) { |
58 // Ignore multiple attempts to start this worker (can happen if two pages | 85 // Ignore multiple attempts to start this worker (can happen if two pages |
59 // try to start it simultaneously). | 86 // try to start it simultaneously). |
60 if (started_) | 87 if (started_) |
61 return; | 88 return; |
62 | 89 |
(...skipping 25 matching lines...) Expand all Loading... |
88 } | 115 } |
89 } | 116 } |
90 | 117 |
91 void WebSharedWorkerStub::OnTerminateWorkerContext() { | 118 void WebSharedWorkerStub::OnTerminateWorkerContext() { |
92 impl_->terminateWorkerContext(); | 119 impl_->terminateWorkerContext(); |
93 | 120 |
94 // Call the client to make sure context exits. | 121 // Call the client to make sure context exits. |
95 EnsureWorkerContextTerminates(); | 122 EnsureWorkerContextTerminates(); |
96 started_ = false; | 123 started_ = false; |
97 } | 124 } |
OLD | NEW |