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