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