OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/renderer/webworker_proxy.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 "content/public/common/content_client.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::WebCommonWorkerClient; | |
16 using WebKit::WebMessagePortChannel; | |
17 using WebKit::WebMessagePortChannelArray; | |
18 using WebKit::WebString; | |
19 using WebKit::WebURL; | |
20 using WebKit::WebWorkerClient; | |
21 | |
22 WebWorkerProxy::WebWorkerProxy( | |
23 WebWorkerClient* client, | |
24 ChildThread* child_thread, | |
25 int render_view_route_id, | |
26 int parent_appcache_host_id) | |
27 : WebWorkerBase( | |
28 child_thread, | |
29 0, | |
30 MSG_ROUTING_NONE, | |
31 render_view_route_id, | |
32 parent_appcache_host_id), | |
33 client_(client) { | |
34 // TODO(atwilson): Change to pass in a real document_id when we support nested | |
35 // workers. | |
36 } | |
37 | |
38 WebWorkerProxy::~WebWorkerProxy() { | |
39 // If we're midway through starting a worker, cancel it. | |
40 CancelCreation(); | |
41 } | |
42 | |
43 void WebWorkerProxy::CancelCreation() { | |
44 if (route_id_ == MSG_ROUTING_NONE) | |
45 return; | |
46 | |
47 // Tell the browser to not start our queued worker. | |
48 if (!IsStarted()) | |
49 child_thread_->Send(new ViewHostMsg_CancelCreateDedicatedWorker(route_id_)); | |
50 } | |
51 | |
52 void WebWorkerProxy::startWorkerContext( | |
53 const WebURL& script_url, | |
54 const WebString& user_agent, | |
55 const WebString& source_code) { | |
56 CreateDedicatedWorkerContext(script_url, user_agent, source_code); | |
57 } | |
58 | |
59 void WebWorkerProxy::terminateWorkerContext() { | |
60 if (route_id_ != MSG_ROUTING_NONE) { | |
61 Send(new WorkerMsg_TerminateWorkerContext(route_id_)); | |
62 CancelCreation(); | |
63 Disconnect(); | |
64 } | |
65 } | |
66 | |
67 void WebWorkerProxy::postMessageToWorkerContext( | |
68 const WebString& message, const WebMessagePortChannelArray& channels) { | |
69 std::vector<int> message_port_ids(channels.size()); | |
70 std::vector<int> routing_ids(channels.size()); | |
71 for (size_t i = 0; i < channels.size(); ++i) { | |
72 WebMessagePortChannelImpl* webchannel = | |
73 static_cast<WebMessagePortChannelImpl*>(channels[i]); | |
74 message_port_ids[i] = webchannel->message_port_id(); | |
75 webchannel->QueueMessages(); | |
76 routing_ids[i] = MSG_ROUTING_NONE; | |
77 DCHECK(message_port_ids[i] != MSG_ROUTING_NONE); | |
78 } | |
79 | |
80 Send(new WorkerMsg_PostMessage( | |
81 route_id_, message, message_port_ids, routing_ids)); | |
82 } | |
83 | |
84 void WebWorkerProxy::workerObjectDestroyed() { | |
85 Send(new WorkerMsg_WorkerObjectDestroyed(route_id_)); | |
86 delete this; | |
87 } | |
88 | |
89 void WebWorkerProxy::clientDestroyed() { | |
90 } | |
91 | |
92 bool WebWorkerProxy::OnMessageReceived(const IPC::Message& message) { | |
93 if (!client_) | |
94 return false; | |
95 | |
96 bool handled = true; | |
97 IPC_BEGIN_MESSAGE_MAP(WebWorkerProxy, message) | |
98 IPC_MESSAGE_HANDLER(ViewMsg_WorkerCreated, OnWorkerCreated) | |
99 IPC_MESSAGE_HANDLER(WorkerMsg_PostMessage, OnPostMessage) | |
100 IPC_MESSAGE_FORWARD(WorkerHostMsg_PostExceptionToWorkerObject, | |
101 client_, | |
102 WebWorkerClient::postExceptionToWorkerObject) | |
103 IPC_MESSAGE_HANDLER(WorkerHostMsg_PostConsoleMessageToWorkerObject, | |
104 OnPostConsoleMessageToWorkerObject) | |
105 IPC_MESSAGE_FORWARD(WorkerHostMsg_ConfirmMessageFromWorkerObject, | |
106 client_, | |
107 WebWorkerClient::confirmMessageFromWorkerObject) | |
108 IPC_MESSAGE_FORWARD(WorkerHostMsg_ReportPendingActivity, | |
109 client_, | |
110 WebWorkerClient::reportPendingActivity) | |
111 IPC_MESSAGE_FORWARD(WorkerHostMsg_WorkerContextDestroyed, | |
112 static_cast<WebCommonWorkerClient*>(client_), | |
113 WebCommonWorkerClient::workerContextDestroyed) | |
114 IPC_MESSAGE_UNHANDLED(handled = false) | |
115 IPC_END_MESSAGE_MAP() | |
116 return handled; | |
117 } | |
118 | |
119 void WebWorkerProxy::OnWorkerCreated() { | |
120 // The worker is created - now send off the CreateWorkerContext message and | |
121 // any other queued messages | |
122 SendQueuedMessages(); | |
123 } | |
124 | |
125 void WebWorkerProxy::OnPostMessage( | |
126 const string16& message, | |
127 const std::vector<int>& sent_message_port_ids, | |
128 const std::vector<int>& new_routing_ids) { | |
129 DCHECK(new_routing_ids.size() == sent_message_port_ids.size()); | |
130 WebMessagePortChannelArray channels(sent_message_port_ids.size()); | |
131 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { | |
132 channels[i] = new WebMessagePortChannelImpl( | |
133 new_routing_ids[i], sent_message_port_ids[i]); | |
134 } | |
135 | |
136 client_->postMessageToWorkerObject(message, channels); | |
137 } | |
138 | |
139 void WebWorkerProxy::OnPostConsoleMessageToWorkerObject( | |
140 const WorkerHostMsg_PostConsoleMessageToWorkerObject_Params& params) { | |
141 client_->postConsoleMessageToWorkerObject(params.source_identifier, | |
142 params.message_type, params.message_level, | |
143 params.message, params.line_number, params.source_url); | |
144 } | |
145 | |
OLD | NEW |