OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/child/threadeddataprovider.h" |
| 6 |
| 7 #include "content/child/child_process.h" |
| 8 #include "content/child/child_thread.h" |
| 9 #include "content/child/resource_dispatcher.h" |
| 10 #include "content/child/thread_safe_sender.h" |
| 11 #include "content/child/webthread_impl.h" |
| 12 #include "content/common/resource_messages.h" |
| 13 #include "ipc/ipc_sync_channel.h" |
| 14 #include "third_party/WebKit/public/platform/WebThread.h" |
| 15 #include "third_party/WebKit/public/platform/WebThreadedDataReceiver.h" |
| 16 |
| 17 namespace content { |
| 18 |
| 19 namespace { |
| 20 |
| 21 class DataProviderMessageFilter : public IPC::ChannelProxy::MessageFilter { |
| 22 public: |
| 23 DataProviderMessageFilter( |
| 24 const scoped_refptr<base::MessageLoopProxy>& io_message_loop, |
| 25 base::MessageLoop* main_thread_message_loop, |
| 26 const WebThreadImpl& background_thread, |
| 27 const base::WeakPtr<ThreadedDataProvider>& |
| 28 background_thread_resource_provider, |
| 29 const base::WeakPtr<ThreadedDataProvider>& |
| 30 main_thread_resource_provider, |
| 31 int request_id); |
| 32 |
| 33 // IPC::ChannelProxy::MessageFilter |
| 34 virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE FINAL; |
| 35 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE FINAL; |
| 36 |
| 37 private: |
| 38 virtual ~DataProviderMessageFilter() { } |
| 39 |
| 40 void OnReceivedData(int request_id, int data_offset, int data_length, |
| 41 int encoded_data_length); |
| 42 |
| 43 const scoped_refptr<base::MessageLoopProxy> io_message_loop_; |
| 44 base::MessageLoop* main_thread_message_loop_; |
| 45 const WebThreadImpl& background_thread_; |
| 46 // This weakptr can only be dereferenced on the background thread. |
| 47 base::WeakPtr<ThreadedDataProvider> |
| 48 background_thread_resource_provider_; |
| 49 // This weakptr can only be dereferenced on the main thread. |
| 50 base::WeakPtr<ThreadedDataProvider> |
| 51 main_thread_resource_provider_; |
| 52 int request_id_; |
| 53 }; |
| 54 |
| 55 DataProviderMessageFilter::DataProviderMessageFilter( |
| 56 const scoped_refptr<base::MessageLoopProxy>& io_message_loop, |
| 57 base::MessageLoop* main_thread_message_loop, |
| 58 const WebThreadImpl& background_thread, |
| 59 const base::WeakPtr<ThreadedDataProvider>& |
| 60 background_thread_resource_provider, |
| 61 const base::WeakPtr<ThreadedDataProvider>& |
| 62 main_thread_resource_provider, |
| 63 int request_id) |
| 64 : io_message_loop_(io_message_loop), |
| 65 main_thread_message_loop_(main_thread_message_loop), |
| 66 background_thread_(background_thread), |
| 67 background_thread_resource_provider_(background_thread_resource_provider), |
| 68 main_thread_resource_provider_(main_thread_resource_provider), |
| 69 request_id_(request_id) { |
| 70 DCHECK(main_thread_message_loop != NULL); |
| 71 } |
| 72 |
| 73 void DataProviderMessageFilter::OnFilterAdded(IPC::Channel* channel) { |
| 74 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 75 |
| 76 main_thread_message_loop_->PostTask(FROM_HERE, |
| 77 base::Bind( |
| 78 &ThreadedDataProvider::OnResourceMessageFilterAddedMainThread, |
| 79 main_thread_resource_provider_)); |
| 80 } |
| 81 |
| 82 bool DataProviderMessageFilter::OnMessageReceived( |
| 83 const IPC::Message& message) { |
| 84 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 85 |
| 86 if (message.type() != ResourceMsg_DataReceived::ID) |
| 87 return false; |
| 88 |
| 89 int request_id; |
| 90 |
| 91 PickleIterator iter(message); |
| 92 if (!message.ReadInt(&iter, &request_id)) { |
| 93 NOTREACHED() << "malformed resource message"; |
| 94 return true; |
| 95 } |
| 96 |
| 97 if (request_id == request_id_) { |
| 98 ResourceMsg_DataReceived::Schema::Param arg; |
| 99 if (ResourceMsg_DataReceived::Read(&message, &arg)) { |
| 100 OnReceivedData(arg.a, arg.b, arg.c, arg.d); |
| 101 return true; |
| 102 } |
| 103 } |
| 104 |
| 105 return false; |
| 106 } |
| 107 |
| 108 void DataProviderMessageFilter::OnReceivedData(int request_id, |
| 109 int data_offset, |
| 110 int data_length, |
| 111 int encoded_data_length) { |
| 112 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 113 background_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( |
| 114 &ThreadedDataProvider::OnReceivedDataOnBackgroundThread, |
| 115 background_thread_resource_provider_, |
| 116 data_offset, data_length, encoded_data_length)); |
| 117 } |
| 118 |
| 119 } // anonymous namespace |
| 120 |
| 121 ThreadedDataProvider::ThreadedDataProvider( |
| 122 int request_id, blink::WebThreadedDataReceiver* threaded_data_receiver, |
| 123 linked_ptr<base::SharedMemory> shm_buffer, int shm_size) |
| 124 : request_id_(request_id), |
| 125 shm_buffer_(shm_buffer), |
| 126 shm_size_(shm_size), |
| 127 background_thread_weak_factory_(this), |
| 128 main_thread_weak_factory_(this), |
| 129 background_thread_( |
| 130 static_cast<WebThreadImpl&>( |
| 131 *threaded_data_receiver->backgroundThread())), |
| 132 ipc_channel_(ChildThread::current()->channel()), |
| 133 threaded_data_receiver_(threaded_data_receiver), |
| 134 resource_filter_active_(false), |
| 135 main_thread_message_loop_(ChildThread::current()->message_loop()) { |
| 136 DCHECK(ChildThread::current()); |
| 137 DCHECK(ipc_channel_); |
| 138 DCHECK(threaded_data_receiver_); |
| 139 DCHECK(main_thread_message_loop_); |
| 140 |
| 141 filter_ = new DataProviderMessageFilter( |
| 142 ChildProcess::current()->io_message_loop_proxy(), |
| 143 main_thread_message_loop_, |
| 144 background_thread_, |
| 145 background_thread_weak_factory_.GetWeakPtr(), |
| 146 main_thread_weak_factory_.GetWeakPtr(), |
| 147 request_id); |
| 148 |
| 149 ChildThread::current()->channel()->AddFilter(filter_); |
| 150 } |
| 151 |
| 152 ThreadedDataProvider::~ThreadedDataProvider() { |
| 153 DCHECK(ChildThread::current()); |
| 154 |
| 155 ChildThread::current()->channel()->RemoveFilter(filter_); |
| 156 |
| 157 delete threaded_data_receiver_; |
| 158 } |
| 159 |
| 160 void DestructOnMainThread(ThreadedDataProvider* data_provider) { |
| 161 DCHECK(ChildThread::current()); |
| 162 |
| 163 // The ThreadedDataProvider must be destructed on the main thread to |
| 164 // be threadsafe when removing the message filter and releasing the shared |
| 165 // memory buffer. |
| 166 delete data_provider; |
| 167 } |
| 168 |
| 169 void ThreadedDataProvider::Stop() { |
| 170 DCHECK(ChildThread::current()); |
| 171 |
| 172 // We can't destroy this instance directly; we need to bounce a message over |
| 173 // to the background thread and back to make sure nothing else will access it |
| 174 // there, before we can destruct it. |
| 175 background_thread_.message_loop()->PostTask(FROM_HERE, |
| 176 base::Bind(&ThreadedDataProvider::StopOnBackgroundThread, |
| 177 base::Unretained(this))); |
| 178 } |
| 179 |
| 180 void ThreadedDataProvider::StopOnBackgroundThread() { |
| 181 DCHECK(background_thread_.isCurrentThread()); |
| 182 |
| 183 // When this happens, the provider should no longer be called on the |
| 184 // background thread as it's about to be destroyed on the main |
| 185 // thread. Invalidating the weak pointers means no callbacks from the filter |
| 186 // will happen and nothing else will use this instance on the |
| 187 // background thread. |
| 188 background_thread_weak_factory_.InvalidateWeakPtrs(); |
| 189 main_thread_message_loop_->PostTask(FROM_HERE, |
| 190 base::Bind(&DestructOnMainThread, this)); |
| 191 } |
| 192 |
| 193 void ThreadedDataProvider::OnResourceMessageFilterAddedMainThread() { |
| 194 DCHECK(ChildThread::current()); |
| 195 |
| 196 // We bounce this message from the I/O thread via the main thread and then |
| 197 // to our background thread, following the same path as incoming data before |
| 198 // our filter gets added, to make sure there's nothing still incoming. |
| 199 background_thread_.message_loop()->PostTask(FROM_HERE, |
| 200 base::Bind( |
| 201 &ThreadedDataProvider::OnResourceMessageFilterAddedBackgroundThread, |
| 202 background_thread_weak_factory_.GetWeakPtr())); |
| 203 } |
| 204 |
| 205 void ThreadedDataProvider::OnResourceMessageFilterAddedBackgroundThread() { |
| 206 DCHECK(background_thread_.isCurrentThread()); |
| 207 resource_filter_active_ = true; |
| 208 |
| 209 // At this point we know no more data is going to arrive from the main thread, |
| 210 // so we can process any data we've received directly from the I/O thread |
| 211 // in the meantime. |
| 212 if (!queued_data_.empty()) { |
| 213 std::vector<QueuedSharedMemoryData>::iterator iter = queued_data_.begin(); |
| 214 for (; iter != queued_data_.end(); ++iter) { |
| 215 ForwardAndACKData(iter->data, iter->length); |
| 216 } |
| 217 |
| 218 queued_data_.clear(); |
| 219 } |
| 220 } |
| 221 |
| 222 void ThreadedDataProvider::OnReceivedDataOnBackgroundThread( |
| 223 int data_offset, int data_length, int encoded_data_length) { |
| 224 DCHECK(background_thread_.isCurrentThread()); |
| 225 DCHECK(shm_buffer_ != NULL); |
| 226 |
| 227 CHECK_GE(shm_size_, data_offset + data_length); |
| 228 const char* data_ptr = static_cast<char*>(shm_buffer_->memory()); |
| 229 CHECK(data_ptr); |
| 230 CHECK(data_ptr + data_offset); |
| 231 |
| 232 if (resource_filter_active_) { |
| 233 ForwardAndACKData(data_ptr + data_offset, data_length); |
| 234 } else { |
| 235 // There's a brief interval between the point where we know the filter |
| 236 // has been installed on the I/O thread, and when we know for sure there's |
| 237 // no more data coming in from the main thread (from before the filter |
| 238 // got added). If we get any data during that interval, we need to queue |
| 239 // it until we're certain we've processed all the main thread data to make |
| 240 // sure we forward (and ACK) everything in the right order. |
| 241 QueuedSharedMemoryData queued_data; |
| 242 queued_data.data = data_ptr + data_offset; |
| 243 queued_data.length = data_length; |
| 244 queued_data_.push_back(queued_data); |
| 245 } |
| 246 } |
| 247 |
| 248 void ThreadedDataProvider::OnReceivedDataOnForegroundThread( |
| 249 const char* data, int data_length, int encoded_data_length) { |
| 250 DCHECK(ChildThread::current()); |
| 251 |
| 252 background_thread_.message_loop()->PostTask(FROM_HERE, |
| 253 base::Bind(&ThreadedDataProvider::ForwardAndACKData, |
| 254 base::Unretained(this), |
| 255 data, data_length)); |
| 256 } |
| 257 |
| 258 void ThreadedDataProvider::ForwardAndACKData(const char* data, |
| 259 int data_length) { |
| 260 DCHECK(background_thread_.isCurrentThread()); |
| 261 |
| 262 // TODO(oysteine): SiteIsolationPolicy needs to be be checked |
| 263 // here before we pass the data to the data provider |
| 264 // (or earlier on the I/O thread), otherwise once SiteIsolationPolicy does |
| 265 // actual blocking as opposed to just UMA logging this will bypass it. |
| 266 threaded_data_receiver_->acceptData(data, data_length); |
| 267 ipc_channel_->Send(new ResourceHostMsg_DataReceived_ACK(request_id_)); |
| 268 } |
| 269 |
| 270 } // namespace content |
OLD | NEW |