OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/renderer/websharedworker_proxy.h" | 5 #include "content/renderer/websharedworker_proxy.h" |
6 | 6 #include "content/common/child_thread.h" |
7 #include "content/common/view_messages.h" | 7 #include "content/common/view_messages.h" |
8 #include "content/common/webmessageportchannel_impl.h" | 8 #include "content/common/webmessageportchannel_impl.h" |
9 #include "content/common/worker_messages.h" | 9 #include "content/common/worker_messages.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebWorkerClient.h" |
11 | 12 |
12 WebSharedWorkerProxy::WebSharedWorkerProxy(ChildThread* child_thread, | 13 WebSharedWorkerProxy::WebSharedWorkerProxy(ChildThread* child_thread, |
13 unsigned long long document_id, | 14 unsigned long long document_id, |
14 bool exists, | 15 bool exists, |
15 int route_id, | 16 int route_id, |
16 int render_view_route_id) | 17 int render_view_route_id) |
17 : WebWorkerBase(child_thread, | 18 : route_id_(exists ? route_id : MSG_ROUTING_NONE), |
18 document_id, | 19 render_view_route_id_(render_view_route_id), |
19 exists ? route_id : MSG_ROUTING_NONE, | 20 child_thread_(child_thread), |
20 render_view_route_id, | 21 document_id_(document_id), |
21 0), | |
22 pending_route_id_(route_id), | 22 pending_route_id_(route_id), |
23 connect_listener_(NULL) { | 23 connect_listener_(NULL) { |
| 24 if (route_id_ != MSG_ROUTING_NONE) |
| 25 child_thread_->AddRoute(route_id_, this); |
| 26 } |
| 27 |
| 28 WebSharedWorkerProxy::~WebSharedWorkerProxy() { |
| 29 Disconnect(); |
| 30 |
| 31 // Free up any unsent queued messages. |
| 32 for (size_t i = 0; i < queued_messages_.size(); ++i) |
| 33 delete queued_messages_[i]; |
| 34 } |
| 35 |
| 36 void WebSharedWorkerProxy::Disconnect() { |
| 37 if (route_id_ == MSG_ROUTING_NONE) |
| 38 return; |
| 39 |
| 40 // So the messages from WorkerContext (like WorkerContextDestroyed) do not |
| 41 // come after nobody is listening. Since Worker and WorkerContext can |
| 42 // terminate independently, already sent messages may still be in the pipe. |
| 43 child_thread_->RemoveRoute(route_id_); |
| 44 |
| 45 route_id_ = MSG_ROUTING_NONE; |
| 46 } |
| 47 |
| 48 void WebSharedWorkerProxy::CreateWorkerContext(const GURL& script_url, |
| 49 bool is_shared, |
| 50 const string16& name, |
| 51 const string16& user_agent, |
| 52 const string16& source_code, |
| 53 int pending_route_id, |
| 54 int64 script_resource_appcache_id) { |
| 55 DCHECK(route_id_ == MSG_ROUTING_NONE); |
| 56 ViewHostMsg_CreateWorker_Params params; |
| 57 params.url = script_url; |
| 58 params.name = name; |
| 59 params.document_id = document_id_; |
| 60 params.render_view_route_id = render_view_route_id_; |
| 61 params.route_id = pending_route_id; |
| 62 params.script_resource_appcache_id = script_resource_appcache_id; |
| 63 IPC::Message* create_message = new ViewHostMsg_CreateWorker( |
| 64 params, &route_id_); |
| 65 child_thread_->Send(create_message); |
| 66 if (route_id_ == MSG_ROUTING_NONE) |
| 67 return; |
| 68 |
| 69 child_thread_->AddRoute(route_id_, this); |
| 70 |
| 71 // We make sure that the start message is the first, since postMessage or |
| 72 // connect might have already been called. |
| 73 queued_messages_.insert(queued_messages_.begin(), |
| 74 new WorkerMsg_StartWorkerContext( |
| 75 route_id_, script_url, user_agent, source_code)); |
| 76 } |
| 77 |
| 78 bool WebSharedWorkerProxy::IsStarted() { |
| 79 // Worker is started if we have a route ID and there are no queued messages |
| 80 // (meaning we've sent the WorkerMsg_StartWorkerContext already). |
| 81 return (route_id_ != MSG_ROUTING_NONE && queued_messages_.empty()); |
| 82 } |
| 83 |
| 84 bool WebSharedWorkerProxy::Send(IPC::Message* message) { |
| 85 // It's possible that messages will be sent before the worker is created, in |
| 86 // which case route_id_ will be none. Or the worker object can be interacted |
| 87 // with before the browser process told us that it started, in which case we |
| 88 // also want to queue the message. |
| 89 if (!IsStarted()) { |
| 90 queued_messages_.push_back(message); |
| 91 return true; |
| 92 } |
| 93 |
| 94 // For now we proxy all messages to the worker process through the browser. |
| 95 // Revisit if we find this slow. |
| 96 // TODO(jabdelmalek): handle sync messages if we need them. |
| 97 IPC::Message* wrapped_msg = new ViewHostMsg_ForwardToWorker(*message); |
| 98 delete message; |
| 99 return child_thread_->Send(wrapped_msg); |
| 100 } |
| 101 |
| 102 void WebSharedWorkerProxy::SendQueuedMessages() { |
| 103 DCHECK(queued_messages_.size()); |
| 104 std::vector<IPC::Message*> queued_messages = queued_messages_; |
| 105 queued_messages_.clear(); |
| 106 for (size_t i = 0; i < queued_messages.size(); ++i) { |
| 107 queued_messages[i]->set_routing_id(route_id_); |
| 108 Send(queued_messages[i]); |
| 109 } |
24 } | 110 } |
25 | 111 |
26 bool WebSharedWorkerProxy::isStarted() { | 112 bool WebSharedWorkerProxy::isStarted() { |
27 return IsStarted(); | 113 return IsStarted(); |
28 } | 114 } |
29 | 115 |
30 void WebSharedWorkerProxy::startWorkerContext( | 116 void WebSharedWorkerProxy::startWorkerContext( |
31 const WebKit::WebURL& script_url, | 117 const WebKit::WebURL& script_url, |
32 const WebKit::WebString& name, | 118 const WebKit::WebString& name, |
33 const WebKit::WebString& user_agent, | 119 const WebKit::WebString& user_agent, |
34 const WebKit::WebString& source_code, | 120 const WebKit::WebString& source_code, |
35 long long script_resource_appcache_id) { | 121 long long script_resource_appcache_id) { |
36 DCHECK(!isStarted()); | 122 DCHECK(!isStarted()); |
37 CreateSharedWorkerContext(script_url, name, user_agent, source_code, | 123 CreateWorkerContext(script_url, true, name, user_agent, source_code, |
38 pending_route_id_, script_resource_appcache_id); | 124 pending_route_id_, script_resource_appcache_id); |
39 } | 125 } |
40 | 126 |
41 void WebSharedWorkerProxy::terminateWorkerContext() { | 127 void WebSharedWorkerProxy::terminateWorkerContext() { |
42 // This API should only be invoked from worker context. | 128 // This API should only be invoked from worker context. |
43 NOTREACHED(); | 129 NOTREACHED(); |
44 } | 130 } |
45 | 131 |
46 void WebSharedWorkerProxy::clientDestroyed() { | 132 void WebSharedWorkerProxy::clientDestroyed() { |
47 // This API should only be invoked from worker context. | 133 // This API should only be invoked from worker context. |
48 NOTREACHED(); | 134 NOTREACHED(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 // The worker is created - now send off the CreateWorkerContext message and | 166 // The worker is created - now send off the CreateWorkerContext message and |
81 // any other queued messages | 167 // any other queued messages |
82 SendQueuedMessages(); | 168 SendQueuedMessages(); |
83 | 169 |
84 // Inform any listener that the pending connect event has been sent | 170 // Inform any listener that the pending connect event has been sent |
85 // (this can result in this object being freed). | 171 // (this can result in this object being freed). |
86 if (connect_listener_) { | 172 if (connect_listener_) { |
87 connect_listener_->connected(); | 173 connect_listener_->connected(); |
88 } | 174 } |
89 } | 175 } |
OLD | NEW |