| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/child/shared_memory_data_consumer_handle.h" | 5 #include "content/child/shared_memory_data_consumer_handle.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 // We post a task even in the writer thread in order to avoid a | 82 // We post a task even in the writer thread in order to avoid a |
| 83 // reentrance problem as calling |on_reader_detached_| may manipulate | 83 // reentrance problem as calling |on_reader_detached_| may manipulate |
| 84 // the context synchronously. | 84 // the context synchronously. |
| 85 writer_task_runner_->PostTask(FROM_HERE, on_reader_detached_); | 85 writer_task_runner_->PostTask(FROM_HERE, on_reader_detached_); |
| 86 } | 86 } |
| 87 Clear(); | 87 Clear(); |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 void ClearQueue() { | 90 void ClearQueue() { |
| 91 lock_.AssertAcquired(); | 91 lock_.AssertAcquired(); |
| 92 for (auto* data : queue_) { | |
| 93 delete data; | |
| 94 } | |
| 95 queue_.clear(); | 92 queue_.clear(); |
| 96 first_offset_ = 0; | 93 first_offset_ = 0; |
| 97 } | 94 } |
| 98 RequestPeer::ThreadSafeReceivedData* Top() { | 95 RequestPeer::ThreadSafeReceivedData* Top() { |
| 99 lock_.AssertAcquired(); | 96 lock_.AssertAcquired(); |
| 100 return queue_.front(); | 97 return queue_.front().get(); |
| 101 } | 98 } |
| 102 void Push(std::unique_ptr<RequestPeer::ThreadSafeReceivedData> data) { | 99 void Push(std::unique_ptr<RequestPeer::ThreadSafeReceivedData> data) { |
| 103 lock_.AssertAcquired(); | 100 lock_.AssertAcquired(); |
| 104 queue_.push_back(data.release()); | 101 queue_.push_back(std::move(data)); |
| 105 } | 102 } |
| 106 size_t first_offset() const { | 103 size_t first_offset() const { |
| 107 lock_.AssertAcquired(); | 104 lock_.AssertAcquired(); |
| 108 return first_offset_; | 105 return first_offset_; |
| 109 } | 106 } |
| 110 Result result() const { | 107 Result result() const { |
| 111 lock_.AssertAcquired(); | 108 lock_.AssertAcquired(); |
| 112 return result_; | 109 return result_; |
| 113 } | 110 } |
| 114 void set_result(Result r) { | 111 void set_result(Result r) { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 } | 178 } |
| 182 void set_is_handle_active(bool b) { | 179 void set_is_handle_active(bool b) { |
| 183 lock_.AssertAcquired(); | 180 lock_.AssertAcquired(); |
| 184 is_handle_active_ = b; | 181 is_handle_active_ = b; |
| 185 } | 182 } |
| 186 void Consume(size_t s) { | 183 void Consume(size_t s) { |
| 187 lock_.AssertAcquired(); | 184 lock_.AssertAcquired(); |
| 188 first_offset_ += s; | 185 first_offset_ += s; |
| 189 auto* top = Top(); | 186 auto* top = Top(); |
| 190 if (static_cast<size_t>(top->length()) <= first_offset_) { | 187 if (static_cast<size_t>(top->length()) <= first_offset_) { |
| 191 delete top; | |
| 192 queue_.pop_front(); | 188 queue_.pop_front(); |
| 193 first_offset_ = 0; | 189 first_offset_ = 0; |
| 194 } | 190 } |
| 195 } | 191 } |
| 196 bool is_two_phase_read_in_progress() const { | 192 bool is_two_phase_read_in_progress() const { |
| 197 lock_.AssertAcquired(); | 193 lock_.AssertAcquired(); |
| 198 return is_two_phase_read_in_progress_; | 194 return is_two_phase_read_in_progress_; |
| 199 } | 195 } |
| 200 void set_is_two_phase_read_in_progress(bool b) { | 196 void set_is_two_phase_read_in_progress(bool b) { |
| 201 lock_.AssertAcquired(); | 197 lock_.AssertAcquired(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 225 if (repost) { | 221 if (repost) { |
| 226 // We don't re-post the task when the runner changes while waiting for | 222 // We don't re-post the task when the runner changes while waiting for |
| 227 // this task because in this case a new reader is obtained and | 223 // this task because in this case a new reader is obtained and |
| 228 // notification is already done at the reader creation time if necessary. | 224 // notification is already done at the reader creation time if necessary. |
| 229 runner->PostTask(FROM_HERE, | 225 runner->PostTask(FROM_HERE, |
| 230 base::Bind(&Context::NotifyInternal, this, false)); | 226 base::Bind(&Context::NotifyInternal, this, false)); |
| 231 } | 227 } |
| 232 } | 228 } |
| 233 void Clear() { | 229 void Clear() { |
| 234 lock_.AssertAcquired(); | 230 lock_.AssertAcquired(); |
| 235 for (auto* data : queue_) { | 231 ClearQueue(); |
| 236 delete data; | |
| 237 } | |
| 238 queue_.clear(); | |
| 239 first_offset_ = 0; | |
| 240 client_ = nullptr; | 232 client_ = nullptr; |
| 241 // Note this doesn't work in the destructor if |on_reader_detached_| is not | |
| 242 // null. We have an assert in the destructor. | |
| 243 ResetOnReaderDetached(); | 233 ResetOnReaderDetached(); |
| 244 } | 234 } |
| 245 // Must be called with |lock_| not aquired. | 235 // Must be called with |lock_| not aquired. |
| 246 void ResetOnReaderDetachedWithLock() { | 236 void ResetOnReaderDetachedWithLock() { |
| 247 base::AutoLock lock(lock_); | 237 base::AutoLock lock(lock_); |
| 248 ResetOnReaderDetached(); | 238 ResetOnReaderDetached(); |
| 249 } | 239 } |
| 250 | 240 |
| 251 friend class base::RefCountedThreadSafe<Context>; | 241 friend class base::RefCountedThreadSafe<Context>; |
| 252 ~Context() { | 242 ~Context() = default; |
| 253 base::AutoLock lock(lock_); | |
| 254 DCHECK(on_reader_detached_.is_null()); | |
| 255 | |
| 256 // This is necessary because the queue stores raw pointers. | |
| 257 Clear(); | |
| 258 } | |
| 259 | 243 |
| 260 base::Lock lock_; | 244 base::Lock lock_; |
| 261 // |result_| stores the ultimate state of this handle if it has. Otherwise, | 245 // |result_| stores the ultimate state of this handle if it has. Otherwise, |
| 262 // |Ok| is set. | 246 // |Ok| is set. |
| 263 Result result_; | 247 Result result_; |
| 264 // TODO(yhirano): Use std::deque<std::unique_ptr<ThreadSafeReceivedData>> | 248 std::deque<std::unique_ptr<RequestPeer::ThreadSafeReceivedData>> queue_; |
| 265 // once it is allowed. | |
| 266 std::deque<RequestPeer::ThreadSafeReceivedData*> queue_; | |
| 267 size_t first_offset_; | 249 size_t first_offset_; |
| 268 Client* client_; | 250 Client* client_; |
| 269 scoped_refptr<base::SingleThreadTaskRunner> notification_task_runner_; | 251 scoped_refptr<base::SingleThreadTaskRunner> notification_task_runner_; |
| 270 scoped_refptr<base::SingleThreadTaskRunner> writer_task_runner_; | 252 scoped_refptr<base::SingleThreadTaskRunner> writer_task_runner_; |
| 271 base::Closure on_reader_detached_; | 253 base::Closure on_reader_detached_; |
| 272 // We need this boolean variable to remember if |on_reader_detached_| is | 254 // We need this boolean variable to remember if |on_reader_detached_| is |
| 273 // callable because we need to reset |on_reader_detached_| only on the writer | 255 // callable because we need to reset |on_reader_detached_| only on the writer |
| 274 // thread and hence |on_reader_detached_.is_null()| is untrustworthy on | 256 // thread and hence |on_reader_detached_.is_null()| is untrustworthy on |
| 275 // other threads. | 257 // other threads. |
| 276 bool is_on_reader_detached_valid_; | 258 bool is_on_reader_detached_valid_; |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 std::unique_ptr<blink::WebDataConsumerHandle::Reader> | 457 std::unique_ptr<blink::WebDataConsumerHandle::Reader> |
| 476 SharedMemoryDataConsumerHandle::obtainReader(Client* client) { | 458 SharedMemoryDataConsumerHandle::obtainReader(Client* client) { |
| 477 return base::WrapUnique(new ReaderImpl(context_, client)); | 459 return base::WrapUnique(new ReaderImpl(context_, client)); |
| 478 } | 460 } |
| 479 | 461 |
| 480 const char* SharedMemoryDataConsumerHandle::debugName() const { | 462 const char* SharedMemoryDataConsumerHandle::debugName() const { |
| 481 return "SharedMemoryDataConsumerHandle"; | 463 return "SharedMemoryDataConsumerHandle"; |
| 482 } | 464 } |
| 483 | 465 |
| 484 } // namespace content | 466 } // namespace content |
| OLD | NEW |