OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/renderer/shared_worker/embedded_shared_worker_stub.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/message_loop/message_loop_proxy.h" |
8 #include "content/child/child_process.h" | |
9 #include "content/child/child_thread.h" | 8 #include "content/child/child_thread.h" |
10 #include "content/child/fileapi/file_system_dispatcher.h" | 9 #include "content/child/scoped_child_process_reference.h" |
| 10 #include "content/child/thread_safe_sender.h" |
11 #include "content/child/webmessageportchannel_impl.h" | 11 #include "content/child/webmessageportchannel_impl.h" |
| 12 #include "content/child/worker_thread_task_runner.h" |
12 #include "content/common/worker_messages.h" | 13 #include "content/common/worker_messages.h" |
13 #include "content/worker/shared_worker_devtools_agent.h" | 14 #include "content/renderer/render_thread_impl.h" |
14 #include "content/worker/worker_thread.h" | 15 #include "content/renderer/shared_worker/embedded_shared_worker_devtools_agent.h
" |
| 16 #include "ipc/ipc_message_macros.h" |
15 #include "third_party/WebKit/public/web/WebSharedWorker.h" | 17 #include "third_party/WebKit/public/web/WebSharedWorker.h" |
16 #include "third_party/WebKit/public/platform/WebString.h" | 18 #include "third_party/WebKit/public/web/WebSharedWorkerClient.h" |
17 #include "third_party/WebKit/public/platform/WebURL.h" | |
18 | 19 |
19 namespace content { | 20 namespace content { |
20 | 21 |
21 WebSharedWorkerStub::WebSharedWorkerStub( | 22 EmbeddedSharedWorkerStub::EmbeddedSharedWorkerStub( |
22 const GURL& url, | 23 const GURL& url, |
23 const base::string16& name, | 24 const base::string16& name, |
24 const base::string16& content_security_policy, | 25 const base::string16& content_security_policy, |
25 blink::WebContentSecurityPolicyType security_policy_type, | 26 blink::WebContentSecurityPolicyType security_policy_type, |
26 int route_id) | 27 int route_id) |
27 : route_id_(route_id), | 28 : route_id_(route_id), |
28 client_(route_id, this), | 29 name_(name), |
29 running_(false), | 30 runing_(false), |
30 url_(url) { | 31 url_(url) { |
31 | 32 RenderThreadImpl::current()->AddRoute(route_id_, this); |
32 WorkerThread* worker_thread = WorkerThread::current(); | 33 impl_ = blink::WebSharedWorker::create(this); |
33 DCHECK(worker_thread); | 34 worker_devtools_agent_.reset( |
34 worker_thread->AddWorkerStub(this); | 35 new EmbeddedSharedWorkerDevToolsAgent(route_id, impl_)); |
35 // Start processing incoming IPCs for this worker. | 36 impl_->startWorkerContext(url, name_, |
36 worker_thread->AddRoute(route_id_, this); | |
37 | |
38 // TODO(atwilson): Add support for NaCl when they support MessagePorts. | |
39 impl_ = blink::WebSharedWorker::create(client()); | |
40 worker_devtools_agent_.reset(new SharedWorkerDevToolsAgent(route_id, impl_)); | |
41 client()->set_devtools_agent(worker_devtools_agent_.get()); | |
42 impl_->startWorkerContext(url_, name, | |
43 content_security_policy, security_policy_type); | 37 content_security_policy, security_policy_type); |
44 } | 38 } |
45 | 39 |
46 WebSharedWorkerStub::~WebSharedWorkerStub() { | 40 EmbeddedSharedWorkerStub::~EmbeddedSharedWorkerStub() { |
47 impl_->clientDestroyed(); | 41 RenderThreadImpl::current()->RemoveRoute(route_id_); |
48 WorkerThread* worker_thread = WorkerThread::current(); | |
49 DCHECK(worker_thread); | |
50 worker_thread->RemoveWorkerStub(this); | |
51 worker_thread->RemoveRoute(route_id_); | |
52 } | 42 } |
53 | 43 |
54 void WebSharedWorkerStub::Shutdown() { | 44 bool EmbeddedSharedWorkerStub::OnMessageReceived( |
55 // The worker has exited - free ourselves and the client. | 45 const IPC::Message& message) { |
56 delete this; | |
57 } | |
58 | |
59 void WebSharedWorkerStub::EnsureWorkerContextTerminates() { | |
60 client_.EnsureWorkerContextTerminates(); | |
61 } | |
62 | |
63 bool WebSharedWorkerStub::OnMessageReceived(const IPC::Message& message) { | |
64 if (worker_devtools_agent_->OnMessageReceived(message)) | 46 if (worker_devtools_agent_->OnMessageReceived(message)) |
65 return true; | 47 return true; |
66 | |
67 bool handled = true; | 48 bool handled = true; |
68 IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerStub, message) | 49 IPC_BEGIN_MESSAGE_MAP(EmbeddedSharedWorkerStub, message) |
69 IPC_MESSAGE_HANDLER(WorkerMsg_TerminateWorkerContext, | 50 IPC_MESSAGE_HANDLER(WorkerMsg_TerminateWorkerContext, |
70 OnTerminateWorkerContext) | 51 OnTerminateWorkerContext) |
71 IPC_MESSAGE_HANDLER(WorkerMsg_Connect, OnConnect) | 52 IPC_MESSAGE_HANDLER(WorkerMsg_Connect, OnConnect) |
72 IPC_MESSAGE_UNHANDLED(handled = false) | 53 IPC_MESSAGE_UNHANDLED(handled = false) |
73 IPC_END_MESSAGE_MAP() | 54 IPC_END_MESSAGE_MAP() |
74 return handled; | 55 return handled; |
75 } | 56 } |
76 | 57 |
77 void WebSharedWorkerStub::OnChannelError() { | 58 void EmbeddedSharedWorkerStub::OnChannelError() { |
78 OnTerminateWorkerContext(); | 59 OnTerminateWorkerContext(); |
79 } | 60 } |
80 | 61 |
81 const GURL& WebSharedWorkerStub::url() { | 62 void EmbeddedSharedWorkerStub::workerScriptLoaded() { |
82 return url_; | 63 Send(new WorkerHostMsg_WorkerScriptLoaded(route_id_)); |
83 } | 64 runing_ = true; |
84 | |
85 void WebSharedWorkerStub::OnConnect(int sent_message_port_id, int routing_id) { | |
86 WebMessagePortChannelImpl* channel = | |
87 new WebMessagePortChannelImpl(routing_id, | |
88 sent_message_port_id, | |
89 base::MessageLoopProxy::current().get()); | |
90 if (running_) { | |
91 impl_->connect(channel); | |
92 WorkerThread::current()->Send( | |
93 new WorkerHostMsg_WorkerConnected(channel->message_port_id(), | |
94 route_id_)); | |
95 } else { | |
96 // If two documents try to load a SharedWorker at the same time, the | |
97 // WorkerMsg_Connect for one of the documents can come in before the | |
98 // worker is started. Just queue up the connect and deliver it once the | |
99 // worker starts. | |
100 pending_channels_.push_back(channel); | |
101 } | |
102 } | |
103 | |
104 void WebSharedWorkerStub::OnTerminateWorkerContext() { | |
105 impl_->terminateWorkerContext(); | |
106 | |
107 // Call the client to make sure context exits. | |
108 EnsureWorkerContextTerminates(); | |
109 running_ = false; | |
110 } | |
111 | |
112 void WebSharedWorkerStub::WorkerScriptLoaded() { | |
113 running_ = true; | |
114 // Process any pending connections. | 65 // Process any pending connections. |
115 for (PendingChannelList::const_iterator iter = pending_channels_.begin(); | 66 for (PendingChannelList::const_iterator iter = pending_channels_.begin(); |
116 iter != pending_channels_.end(); | 67 iter != pending_channels_.end(); |
117 ++iter) { | 68 ++iter) { |
118 impl_->connect(*iter); | 69 impl_->connect(*iter); |
119 WorkerThread::current()->Send( | 70 Send(new WorkerHostMsg_WorkerConnected((*iter)->message_port_id(), |
120 new WorkerHostMsg_WorkerConnected((*iter)->message_port_id(), | 71 route_id_)); |
121 route_id_)); | |
122 } | 72 } |
123 pending_channels_.clear(); | 73 pending_channels_.clear(); |
124 } | 74 } |
125 | 75 |
126 void WebSharedWorkerStub::WorkerScriptLoadFailed() { | 76 void EmbeddedSharedWorkerStub::workerScriptLoadFailed() { |
| 77 Send(new WorkerHostMsg_WorkerScriptLoadFailed(route_id_)); |
127 for (PendingChannelList::const_iterator iter = pending_channels_.begin(); | 78 for (PendingChannelList::const_iterator iter = pending_channels_.begin(); |
128 iter != pending_channels_.end(); | 79 iter != pending_channels_.end(); |
129 ++iter) { | 80 ++iter) { |
130 blink::WebMessagePortChannel* channel = *iter; | 81 blink::WebMessagePortChannel* channel = *iter; |
131 channel->destroy(); | 82 channel->destroy(); |
132 } | 83 } |
133 pending_channels_.clear(); | 84 pending_channels_.clear(); |
134 Shutdown(); | 85 Shutdown(); |
135 } | 86 } |
136 | 87 |
| 88 void EmbeddedSharedWorkerStub::workerContextClosed() { |
| 89 Send(new WorkerHostMsg_WorkerContextClosed(route_id_)); |
| 90 } |
| 91 |
| 92 void EmbeddedSharedWorkerStub::workerContextDestroyed() { |
| 93 Send(new WorkerHostMsg_WorkerContextDestroyed(route_id_)); |
| 94 Shutdown(); |
| 95 } |
| 96 |
| 97 void EmbeddedSharedWorkerStub::selectAppCacheID(long long) { |
| 98 //TODO(horo): implement this. |
| 99 } |
| 100 |
| 101 blink::WebNotificationPresenter* |
| 102 EmbeddedSharedWorkerStub::notificationPresenter() { |
| 103 // TODO(johnnyg): Notifications are not yet hooked up to workers. |
| 104 // Coming soon. |
| 105 NOTREACHED(); |
| 106 return NULL; |
| 107 } |
| 108 |
| 109 blink::WebApplicationCacheHost* |
| 110 EmbeddedSharedWorkerStub::createApplicationCacheHost( |
| 111 blink::WebApplicationCacheHostClient*) { |
| 112 //TODO(horo): implement this. |
| 113 return NULL; |
| 114 } |
| 115 |
| 116 blink::WebWorkerPermissionClientProxy* |
| 117 EmbeddedSharedWorkerStub::createWorkerPermissionClientProxy( |
| 118 const blink::WebSecurityOrigin& origin) { |
| 119 //TODO(horo): implement this. |
| 120 return NULL; |
| 121 } |
| 122 |
| 123 void EmbeddedSharedWorkerStub::dispatchDevToolsMessage( |
| 124 const blink::WebString& message) { |
| 125 worker_devtools_agent_->SendDevToolsMessage(message); |
| 126 } |
| 127 |
| 128 void EmbeddedSharedWorkerStub::saveDevToolsAgentState( |
| 129 const blink::WebString& state) { |
| 130 worker_devtools_agent_->SaveDevToolsAgentState(state); |
| 131 } |
| 132 |
| 133 void EmbeddedSharedWorkerStub::Shutdown() { |
| 134 delete this; |
| 135 } |
| 136 |
| 137 bool EmbeddedSharedWorkerStub::Send(IPC::Message* message) { |
| 138 return RenderThreadImpl::current()->Send(message); |
| 139 } |
| 140 |
| 141 void EmbeddedSharedWorkerStub::OnConnect(int sent_message_port_id, |
| 142 int routing_id) { |
| 143 WebMessagePortChannelImpl* channel = |
| 144 new WebMessagePortChannelImpl(routing_id, |
| 145 sent_message_port_id, |
| 146 base::MessageLoopProxy::current().get()); |
| 147 if (runing_) { |
| 148 impl_->connect(channel); |
| 149 } else { |
| 150 // If two documents try to load a SharedWorker at the same time, the |
| 151 // WorkerMsg_Connect for one of the documents can come in before the |
| 152 // worker is started. Just queue up the connect and deliver it once the |
| 153 // worker starts. |
| 154 pending_channels_.push_back(channel); |
| 155 } |
| 156 } |
| 157 |
| 158 void EmbeddedSharedWorkerStub::OnTerminateWorkerContext() { |
| 159 runing_ = false; |
| 160 impl_->terminateWorkerContext(); |
| 161 } |
| 162 |
137 } // namespace content | 163 } // namespace content |
OLD | NEW |