OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/common/webmessageportchannel_impl.h" | 5 #include "content/common/webmessageportchannel_impl.h" |
6 | 6 |
7 #include "content/common/child_process.h" | 7 #include "content/common/child_process.h" |
8 #include "content/common/child_thread.h" | 8 #include "content/common/child_thread.h" |
9 #include "content/common/worker_messages.h" | 9 #include "content/common/worker_messages.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMessagePortChannel
Client.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMessagePortChannel
Client.h" |
12 | 12 |
13 using WebKit::WebMessagePortChannel; | 13 using WebKit::WebMessagePortChannel; |
14 using WebKit::WebMessagePortChannelArray; | 14 using WebKit::WebTransferableReceiptArray; |
15 using WebKit::WebMessagePortChannelClient; | 15 using WebKit::WebMessagePortChannelClient; |
16 using WebKit::WebString; | 16 using WebKit::WebString; |
17 | 17 |
18 WebMessagePortChannelImpl::WebMessagePortChannelImpl() | 18 WebMessagePortChannelImpl::WebMessagePortChannelImpl() |
19 : client_(NULL), | 19 : client_(NULL), |
20 route_id_(MSG_ROUTING_NONE), | 20 route_id_(MSG_ROUTING_NONE), |
21 message_port_id_(MSG_ROUTING_NONE) { | 21 message_port_id_(MSG_ROUTING_NONE) { |
22 AddRef(); | 22 AddRef(); |
23 Init(); | 23 Init(); |
24 } | 24 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 // The message port ids might not be set up yet, if this channel wasn't | 69 // The message port ids might not be set up yet, if this channel wasn't |
70 // created on the main thread. So need to wait until we're on the main thread | 70 // created on the main thread. So need to wait until we're on the main thread |
71 // before getting the other message port id. | 71 // before getting the other message port id. |
72 scoped_refptr<WebMessagePortChannelImpl> webchannel( | 72 scoped_refptr<WebMessagePortChannelImpl> webchannel( |
73 static_cast<WebMessagePortChannelImpl*>(channel)); | 73 static_cast<WebMessagePortChannelImpl*>(channel)); |
74 Entangle(webchannel); | 74 Entangle(webchannel); |
75 } | 75 } |
76 | 76 |
77 void WebMessagePortChannelImpl::postMessage( | 77 void WebMessagePortChannelImpl::postMessage( |
78 const WebString& message, | 78 const WebString& message, |
79 WebMessagePortChannelArray* channels) { | 79 WebTransferableReceiptArray* receipts) { |
80 if (MessageLoop::current() != ChildThread::current()->message_loop()) { | 80 if (MessageLoop::current() != ChildThread::current()->message_loop()) { |
81 ChildThread::current()->message_loop()->PostTask(FROM_HERE, | 81 ChildThread::current()->message_loop()->PostTask(FROM_HERE, |
82 NewRunnableMethod(this, &WebMessagePortChannelImpl::postMessage, | 82 NewRunnableMethod(this, &WebMessagePortChannelImpl::postMessage, |
83 message, channels)); | 83 message, receipts)); |
84 return; | 84 return; |
85 } | 85 } |
86 | 86 |
87 std::vector<int> message_port_ids(channels ? channels->size() : 0); | 87 std::vector<int> message_port_ids(receipts ? receipts->size() : 0); |
88 if (channels) { | 88 if (receipts) { |
89 // Extract the port IDs from the source array, then free it. | 89 // Extract the port IDs from the source array, then free it. |
90 for (size_t i = 0; i < channels->size(); ++i) { | 90 for (size_t i = 0; i < receipts->size(); ++i) { |
91 WebMessagePortChannelImpl* webchannel = | 91 WebMessagePortChannelImpl* webchannel = |
92 static_cast<WebMessagePortChannelImpl*>((*channels)[i]); | 92 static_cast<WebMessagePortChannelImpl*>((*receipts)[i]); |
93 message_port_ids[i] = webchannel->message_port_id(); | 93 message_port_ids[i] = webchannel->message_port_id(); |
94 webchannel->QueueMessages(); | 94 webchannel->QueueMessages(); |
95 DCHECK(message_port_ids[i] != MSG_ROUTING_NONE); | 95 DCHECK(message_port_ids[i] != MSG_ROUTING_NONE); |
96 } | 96 } |
97 delete channels; | 97 delete receipts; |
98 } | 98 } |
99 | 99 |
100 IPC::Message* msg = new WorkerProcessHostMsg_PostMessage( | 100 IPC::Message* msg = new WorkerProcessHostMsg_PostMessage( |
101 message_port_id_, message, message_port_ids); | 101 message_port_id_, message, message_port_ids); |
102 Send(msg); | 102 Send(msg); |
103 } | 103 } |
104 | 104 |
105 bool WebMessagePortChannelImpl::tryGetMessage( | 105 bool WebMessagePortChannelImpl::tryGetMessage( |
106 WebString* message, | 106 WebString* message, |
107 WebMessagePortChannelArray& channels) { | 107 WebTransferableReceiptArray& receipts) { |
108 base::AutoLock auto_lock(lock_); | 108 base::AutoLock auto_lock(lock_); |
109 if (message_queue_.empty()) | 109 if (message_queue_.empty()) |
110 return false; | 110 return false; |
111 | 111 |
112 *message = message_queue_.front().message; | 112 *message = message_queue_.front().message; |
113 const std::vector<WebMessagePortChannelImpl*>& channel_array = | 113 const std::vector<WebMessagePortChannelImpl*>& channel_array = |
114 message_queue_.front().ports; | 114 message_queue_.front().ports; |
115 WebMessagePortChannelArray result_ports(channel_array.size()); | 115 WebTransferableReceiptArray result_ports(channel_array.size()); |
116 for (size_t i = 0; i < channel_array.size(); i++) { | 116 for (size_t i = 0; i < channel_array.size(); i++) { |
117 result_ports[i] = channel_array[i]; | 117 result_ports[i] = channel_array[i]; |
118 } | 118 } |
119 | 119 |
120 channels.swap(result_ports); | 120 receipts.swap(result_ports); |
121 message_queue_.pop(); | 121 message_queue_.pop(); |
122 return true; | 122 return true; |
123 } | 123 } |
124 | 124 |
125 void WebMessagePortChannelImpl::Init() { | 125 void WebMessagePortChannelImpl::Init() { |
126 if (MessageLoop::current() != ChildThread::current()->message_loop()) { | 126 if (MessageLoop::current() != ChildThread::current()->message_loop()) { |
127 ChildThread::current()->message_loop()->PostTask(FROM_HERE, | 127 ChildThread::current()->message_loop()->PostTask(FROM_HERE, |
128 NewRunnableMethod(this, &WebMessagePortChannelImpl::Init)); | 128 NewRunnableMethod(this, &WebMessagePortChannelImpl::Init)); |
129 return; | 129 return; |
130 } | 130 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 | 235 |
236 message_port_id_ = MSG_ROUTING_NONE; | 236 message_port_id_ = MSG_ROUTING_NONE; |
237 | 237 |
238 Release(); | 238 Release(); |
239 ChildProcess::current()->ReleaseProcess(); | 239 ChildProcess::current()->ReleaseProcess(); |
240 } | 240 } |
241 | 241 |
242 WebMessagePortChannelImpl::Message::Message() {} | 242 WebMessagePortChannelImpl::Message::Message() {} |
243 | 243 |
244 WebMessagePortChannelImpl::Message::~Message() {} | 244 WebMessagePortChannelImpl::Message::~Message() {} |
OLD | NEW |