| 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 "device/serial/data_source_sender.h" | 5 #include "device/serial/data_source_sender.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 // The DataSourceSender of whose buffer we are providing a view. | 64 // The DataSourceSender of whose buffer we are providing a view. |
| 65 scoped_refptr<DataSourceSender> sender_; | 65 scoped_refptr<DataSourceSender> sender_; |
| 66 | 66 |
| 67 // The PendingSend to which this buffer has been created in response. | 67 // The PendingSend to which this buffer has been created in response. |
| 68 PendingSend* pending_send_; | 68 PendingSend* pending_send_; |
| 69 | 69 |
| 70 char* buffer_; | 70 char* buffer_; |
| 71 uint32_t buffer_size_; | 71 uint32_t buffer_size_; |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 DataSourceSender::DataSourceSender(const ReadyCallback& ready_callback, | 74 DataSourceSender::DataSourceSender( |
| 75 const ErrorCallback& error_callback) | 75 mojo::InterfaceRequest<serial::DataSource> source, |
| 76 : ready_callback_(ready_callback), | 76 mojo::InterfacePtr<serial::DataSourceClient> client, |
| 77 const ReadyCallback& ready_callback, |
| 78 const ErrorCallback& error_callback) |
| 79 : binding_(this, source.Pass()), |
| 80 client_(client.Pass()), |
| 81 ready_callback_(ready_callback), |
| 77 error_callback_(error_callback), | 82 error_callback_(error_callback), |
| 78 available_buffer_capacity_(0), | 83 available_buffer_capacity_(0), |
| 79 paused_(false), | 84 paused_(false), |
| 80 shut_down_(false), | 85 shut_down_(false), |
| 81 weak_factory_(this) { | 86 weak_factory_(this) { |
| 82 DCHECK(!ready_callback.is_null() && !error_callback.is_null()); | 87 DCHECK(!ready_callback.is_null() && !error_callback.is_null()); |
| 88 binding_.set_error_handler(this); |
| 89 client_.set_error_handler(this); |
| 83 } | 90 } |
| 84 | 91 |
| 85 void DataSourceSender::ShutDown() { | 92 void DataSourceSender::ShutDown() { |
| 86 shut_down_ = true; | 93 shut_down_ = true; |
| 87 ready_callback_.Reset(); | 94 ready_callback_.Reset(); |
| 88 error_callback_.Reset(); | 95 error_callback_.Reset(); |
| 89 } | 96 } |
| 90 | 97 |
| 91 DataSourceSender::~DataSourceSender() { | 98 DataSourceSender::~DataSourceSender() { |
| 92 DCHECK(shut_down_); | 99 DCHECK(shut_down_); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 base::MessageLoop::current()->PostTask( | 138 base::MessageLoop::current()->PostTask( |
| 132 FROM_HERE, | 139 FROM_HERE, |
| 133 base::Bind(&DataSourceSender::GetMoreData, weak_factory_.GetWeakPtr())); | 140 base::Bind(&DataSourceSender::GetMoreData, weak_factory_.GetWeakPtr())); |
| 134 } | 141 } |
| 135 } | 142 } |
| 136 | 143 |
| 137 void DataSourceSender::DoneWithError(const std::vector<char>& data, | 144 void DataSourceSender::DoneWithError(const std::vector<char>& data, |
| 138 int32_t error) { | 145 int32_t error) { |
| 139 DoneInternal(data); | 146 DoneInternal(data); |
| 140 if (!shut_down_) | 147 if (!shut_down_) |
| 141 client()->OnError(error); | 148 client_->OnError(error); |
| 142 paused_ = true; | 149 paused_ = true; |
| 143 // We don't call GetMoreData here so we don't send any additional data until | 150 // We don't call GetMoreData here so we don't send any additional data until |
| 144 // Resume() is called. | 151 // Resume() is called. |
| 145 } | 152 } |
| 146 | 153 |
| 147 void DataSourceSender::DoneInternal(const std::vector<char>& data) { | 154 void DataSourceSender::DoneInternal(const std::vector<char>& data) { |
| 148 DCHECK(pending_send_); | 155 DCHECK(pending_send_); |
| 149 if (shut_down_) | 156 if (shut_down_) |
| 150 return; | 157 return; |
| 151 | 158 |
| 152 available_buffer_capacity_ -= static_cast<uint32_t>(data.size()); | 159 available_buffer_capacity_ -= static_cast<uint32_t>(data.size()); |
| 153 if (!data.empty()) { | 160 if (!data.empty()) { |
| 154 mojo::Array<uint8_t> data_to_send(data.size()); | 161 mojo::Array<uint8_t> data_to_send(data.size()); |
| 155 std::copy(data.begin(), data.end(), &data_to_send[0]); | 162 std::copy(data.begin(), data.end(), &data_to_send[0]); |
| 156 client()->OnData(data_to_send.Pass()); | 163 client_->OnData(data_to_send.Pass()); |
| 157 } | 164 } |
| 158 pending_send_.reset(); | 165 pending_send_.reset(); |
| 159 } | 166 } |
| 160 | 167 |
| 161 void DataSourceSender::DispatchFatalError() { | 168 void DataSourceSender::DispatchFatalError() { |
| 162 if (shut_down_) | 169 if (shut_down_) |
| 163 return; | 170 return; |
| 164 | 171 |
| 165 error_callback_.Run(); | 172 error_callback_.Run(); |
| 166 ShutDown(); | 173 ShutDown(); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 uint32_t bytes_written, | 240 uint32_t bytes_written, |
| 234 int32_t error) { | 241 int32_t error) { |
| 235 DCHECK(sender_.get()); | 242 DCHECK(sender_.get()); |
| 236 PendingSend* send = pending_send_; | 243 PendingSend* send = pending_send_; |
| 237 pending_send_ = nullptr; | 244 pending_send_ = nullptr; |
| 238 send->DoneWithError(bytes_written, error); | 245 send->DoneWithError(bytes_written, error); |
| 239 sender_ = nullptr; | 246 sender_ = nullptr; |
| 240 } | 247 } |
| 241 | 248 |
| 242 } // namespace device | 249 } // namespace device |
| OLD | NEW |