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