OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/renderer/webworker_base.h" | |
6 | |
7 #include "chrome/common/render_messages.h" | |
8 #include "chrome/common/render_messages_params.h" | |
9 #include "content/common/child_thread.h" | |
10 #include "content/common/webmessageportchannel_impl.h" | |
11 #include "content/common/worker_messages.h" | |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" | |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebWorkerClient.h" | |
14 | |
15 using WebKit::WebMessagePortChannel; | |
16 using WebKit::WebMessagePortChannelArray; | |
17 using WebKit::WebString; | |
18 using WebKit::WebURL; | |
19 using WebKit::WebWorkerClient; | |
20 | |
21 WebWorkerBase::WebWorkerBase( | |
22 ChildThread* child_thread, | |
23 unsigned long long document_id, | |
24 int route_id, | |
25 int render_view_route_id, | |
26 int parent_appcache_host_id) | |
27 : route_id_(route_id), | |
28 render_view_route_id_(render_view_route_id), | |
29 child_thread_(child_thread), | |
30 document_id_(document_id), | |
31 parent_appcache_host_id_(parent_appcache_host_id) { | |
32 if (route_id_ != MSG_ROUTING_NONE) | |
33 child_thread_->AddRoute(route_id_, this); | |
34 } | |
35 | |
36 WebWorkerBase::~WebWorkerBase() { | |
37 Disconnect(); | |
38 | |
39 // Free up any unsent queued messages. | |
40 for (size_t i = 0; i < queued_messages_.size(); ++i) | |
41 delete queued_messages_[i]; | |
42 } | |
43 | |
44 void WebWorkerBase::Disconnect() { | |
45 if (route_id_ == MSG_ROUTING_NONE) | |
46 return; | |
47 | |
48 // So the messages from WorkerContext (like WorkerContextDestroyed) do not | |
49 // come after nobody is listening. Since Worker and WorkerContext can | |
50 // terminate independently, already sent messages may still be in the pipe. | |
51 child_thread_->RemoveRoute(route_id_); | |
52 | |
53 route_id_ = MSG_ROUTING_NONE; | |
54 } | |
55 | |
56 void WebWorkerBase::CreateWorkerContext(const GURL& script_url, | |
57 bool is_shared, | |
58 const string16& name, | |
59 const string16& user_agent, | |
60 const string16& source_code, | |
61 int pending_route_id, | |
62 int64 script_resource_appcache_id) { | |
63 DCHECK(route_id_ == MSG_ROUTING_NONE); | |
64 ViewHostMsg_CreateWorker_Params params; | |
65 params.url = script_url; | |
66 params.is_shared = is_shared; | |
67 params.name = name; | |
68 params.document_id = document_id_; | |
69 params.render_view_route_id = render_view_route_id_; | |
70 params.route_id = pending_route_id; | |
71 params.parent_appcache_host_id = parent_appcache_host_id_; | |
72 params.script_resource_appcache_id = script_resource_appcache_id; | |
73 IPC::Message* create_message = new ViewHostMsg_CreateWorker( | |
74 params, &route_id_); | |
75 child_thread_->Send(create_message); | |
76 if (route_id_ == MSG_ROUTING_NONE) | |
77 return; | |
78 | |
79 child_thread_->AddRoute(route_id_, this); | |
80 | |
81 // We make sure that the start message is the first, since postMessage or | |
82 // connect might have already been called. | |
83 queued_messages_.insert(queued_messages_.begin(), | |
84 new WorkerMsg_StartWorkerContext( | |
85 route_id_, script_url, user_agent, source_code)); | |
86 } | |
87 | |
88 bool WebWorkerBase::IsStarted() { | |
89 // Worker is started if we have a route ID and there are no queued messages | |
90 // (meaning we've sent the WorkerMsg_StartWorkerContext already). | |
91 return (route_id_ != MSG_ROUTING_NONE && queued_messages_.empty()); | |
92 } | |
93 | |
94 bool WebWorkerBase::Send(IPC::Message* message) { | |
95 // It's possible that messages will be sent before the worker is created, in | |
96 // which case route_id_ will be none. Or the worker object can be interacted | |
97 // with before the browser process told us that it started, in which case we | |
98 // also want to queue the message. | |
99 if (!IsStarted()) { | |
100 queued_messages_.push_back(message); | |
101 return true; | |
102 } | |
103 | |
104 // For now we proxy all messages to the worker process through the browser. | |
105 // Revisit if we find this slow. | |
106 // TODO(jabdelmalek): handle sync messages if we need them. | |
107 IPC::Message* wrapped_msg = new ViewHostMsg_ForwardToWorker(*message); | |
108 delete message; | |
109 return child_thread_->Send(wrapped_msg); | |
110 } | |
111 | |
112 void WebWorkerBase::SendQueuedMessages() { | |
113 DCHECK(queued_messages_.size()); | |
114 std::vector<IPC::Message*> queued_messages = queued_messages_; | |
115 queued_messages_.clear(); | |
116 for (size_t i = 0; i < queued_messages.size(); ++i) { | |
117 queued_messages[i]->set_routing_id(route_id_); | |
118 Send(queued_messages[i]); | |
119 } | |
120 } | |
OLD | NEW |