| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "network/public/cpp/web_socket_write_queue.h" | 5 #include "network/public/cpp/web_socket_write_queue.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "third_party/mojo/src/mojo/public/cpp/environment/logging.h" |
| 8 | 9 |
| 9 namespace mojo { | 10 namespace mojo { |
| 10 | 11 |
| 11 struct WebSocketWriteQueue::Operation { | 12 struct WebSocketWriteQueue::Operation { |
| 12 uint32_t num_bytes_; | 13 uint32_t num_bytes_; |
| 13 base::Callback<void(const char*)> callback_; | 14 base::Callback<void(const char*)> callback_; |
| 14 | 15 |
| 15 const char* data_; | 16 const char* data_; |
| 16 // Only initialized if the initial Write fails. This saves a copy in | 17 // Only initialized if the initial Write fails. This saves a copy in |
| 17 // the common case. | 18 // the common case. |
| 18 std::vector<char> data_copy_; | 19 std::vector<char> data_copy_; |
| 19 }; | 20 }; |
| 20 | 21 |
| 21 WebSocketWriteQueue::WebSocketWriteQueue(DataPipeProducerHandle handle) | 22 WebSocketWriteQueue::WebSocketWriteQueue(DataPipeProducerHandle handle) |
| 22 : handle_(handle), is_waiting_(false) { | 23 : handle_(handle), is_busy_(false), weak_factory_(this) { |
| 23 } | 24 } |
| 24 | 25 |
| 25 WebSocketWriteQueue::~WebSocketWriteQueue() { | 26 WebSocketWriteQueue::~WebSocketWriteQueue() { |
| 26 } | 27 } |
| 27 | 28 |
| 28 void WebSocketWriteQueue::Write(const char* data, | 29 void WebSocketWriteQueue::Write(const char* data, |
| 29 uint32_t num_bytes, | 30 uint32_t num_bytes, |
| 30 base::Callback<void(const char*)> callback) { | 31 base::Callback<void(const char*)> callback) { |
| 31 Operation* op = new Operation; | 32 Operation* op = new Operation; |
| 32 op->num_bytes_ = num_bytes; | 33 op->num_bytes_ = num_bytes; |
| 33 op->callback_ = callback; | 34 op->callback_ = callback; |
| 34 op->data_ = data; | 35 op->data_ = data; |
| 35 queue_.push_back(op); | 36 queue_.push_back(op); |
| 36 | 37 |
| 37 MojoResult result = MOJO_RESULT_SHOULD_WAIT; | 38 if (!is_busy_) { |
| 38 if (!is_waiting_) | 39 is_busy_ = true; |
| 39 result = TryToWrite(); | 40 // This call may reset |is_busy_| to false. |
| 41 TryToWrite(); |
| 42 } |
| 40 | 43 |
| 41 // If we have to wait, make a local copy of the data so we know it will | 44 if (is_busy_) { |
| 42 // live until we need it. | 45 // If we have to wait, make a local copy of the data so we know it will |
| 43 if (result == MOJO_RESULT_SHOULD_WAIT) { | 46 // live until we need it. |
| 44 op->data_copy_.resize(num_bytes); | 47 op->data_copy_.resize(num_bytes); |
| 45 memcpy(&op->data_copy_[0], data, num_bytes); | 48 memcpy(&op->data_copy_[0], data, num_bytes); |
| 46 op->data_ = &op->data_copy_[0]; | 49 op->data_ = &op->data_copy_[0]; |
| 47 } | 50 } |
| 48 } | 51 } |
| 49 | 52 |
| 50 MojoResult WebSocketWriteQueue::TryToWrite() { | 53 void WebSocketWriteQueue::TryToWrite() { |
| 51 Operation* op = queue_[0]; | 54 MOJO_DCHECK(is_busy_); |
| 52 uint32_t bytes_written = op->num_bytes_; | 55 MOJO_DCHECK(!queue_.empty()); |
| 53 MojoResult result = WriteDataRaw( | 56 do { |
| 54 handle_, op->data_, &bytes_written, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); | 57 Operation* op = queue_[0]; |
| 55 if (result == MOJO_RESULT_SHOULD_WAIT) { | 58 uint32_t bytes_written = op->num_bytes_; |
| 56 Wait(); | 59 MojoResult result = WriteDataRaw( |
| 57 return result; | 60 handle_, op->data_, &bytes_written, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); |
| 58 } | 61 if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 62 Wait(); |
| 63 return; |
| 64 } |
| 59 | 65 |
| 60 // Ensure |op| is deleted, whether or not |this| goes away. | 66 // Ensure |op| is deleted, whether or not |this| goes away. |
| 61 scoped_ptr<Operation> op_deleter(op); | 67 scoped_ptr<Operation> op_deleter(op); |
| 62 queue_.weak_erase(queue_.begin()); | 68 queue_.weak_erase(queue_.begin()); |
| 63 if (result != MOJO_RESULT_OK) | |
| 64 return result; | |
| 65 | 69 |
| 66 op->callback_.Run(op->data_); // may delete |this| | 70 // http://crbug.com/490193 This should run callback as well. May need to |
| 67 return result; | 71 // change the callback signature. |
| 72 if (result != MOJO_RESULT_OK) |
| 73 return; |
| 74 |
| 75 base::WeakPtr<WebSocketWriteQueue> self(weak_factory_.GetWeakPtr()); |
| 76 |
| 77 // This call may delete |this|. In that case, |self| will be invalidated. |
| 78 // It may re-enter Write() too. Because |is_busy_| is true during the whole |
| 79 // process, TryToWrite() won't be re-entered. |
| 80 op->callback_.Run(op->data_); |
| 81 |
| 82 if (!self) |
| 83 return; |
| 84 } while (!queue_.empty()); |
| 85 is_busy_ = false; |
| 68 } | 86 } |
| 69 | 87 |
| 70 void WebSocketWriteQueue::Wait() { | 88 void WebSocketWriteQueue::Wait() { |
| 71 is_waiting_ = true; | 89 MOJO_DCHECK(is_busy_); |
| 72 handle_watcher_.Start(handle_, | 90 handle_watcher_.Start(handle_, |
| 73 MOJO_HANDLE_SIGNAL_WRITABLE, | 91 MOJO_HANDLE_SIGNAL_WRITABLE, |
| 74 MOJO_DEADLINE_INDEFINITE, | 92 MOJO_DEADLINE_INDEFINITE, |
| 75 base::Bind(&WebSocketWriteQueue::OnHandleReady, | 93 base::Bind(&WebSocketWriteQueue::OnHandleReady, |
| 76 base::Unretained(this))); | 94 base::Unretained(this))); |
| 77 } | 95 } |
| 78 | 96 |
| 79 void WebSocketWriteQueue::OnHandleReady(MojoResult result) { | 97 void WebSocketWriteQueue::OnHandleReady(MojoResult result) { |
| 80 is_waiting_ = false; | 98 MOJO_DCHECK(is_busy_); |
| 81 TryToWrite(); | 99 TryToWrite(); |
| 82 } | 100 } |
| 83 | 101 |
| 84 } // namespace mojo | 102 } // namespace mojo |
| OLD | NEW |