Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: tools/battor_agent/battor_connection_impl.cc

Issue 1869503004: Convert //tools to use std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase, change iwyu fixes for converted directories to include <memory> Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "tools/battor_agent/battor_connection_impl.h" 5 #include "tools/battor_agent/battor_connection_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/memory/ptr_util.h"
9 #include "base/thread_task_runner_handle.h" 10 #include "base/thread_task_runner_handle.h"
10 #include "device/serial/buffer.h" 11 #include "device/serial/buffer.h"
11 #include "device/serial/serial_io_handler.h" 12 #include "device/serial/serial_io_handler.h"
12 #include "net/base/io_buffer.h" 13 #include "net/base/io_buffer.h"
13 14
14 using std::vector; 15 using std::vector;
15 16
16 namespace battor { 17 namespace battor {
17 18
18 namespace { 19 namespace {
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 bytes[i] == BATTOR_CONTROL_BYTE_END) { 113 bytes[i] == BATTOR_CONTROL_BYTE_END) {
113 data.push_back(BATTOR_CONTROL_BYTE_ESCAPE); 114 data.push_back(BATTOR_CONTROL_BYTE_ESCAPE);
114 } 115 }
115 116
116 data.push_back(bytes[i]); 117 data.push_back(bytes[i]);
117 } 118 }
118 119
119 data.push_back(BATTOR_CONTROL_BYTE_END); 120 data.push_back(BATTOR_CONTROL_BYTE_END);
120 121
121 pending_write_length_ = data.size(); 122 pending_write_length_ = data.size();
122 io_handler_->Write(make_scoped_ptr(new device::SendBuffer( 123 io_handler_->Write(base::WrapUnique(new device::SendBuffer(
123 data, base::Bind(&BattOrConnectionImpl::OnBytesSent, AsWeakPtr())))); 124 data, base::Bind(&BattOrConnectionImpl::OnBytesSent, AsWeakPtr()))));
124 } 125 }
125 126
126 void BattOrConnectionImpl::ReadMessage(BattOrMessageType type) { 127 void BattOrConnectionImpl::ReadMessage(BattOrMessageType type) {
127 pending_read_message_type_ = type; 128 pending_read_message_type_ = type;
128 size_t max_bytes_to_read = GetMaxBytesForMessageType(type); 129 size_t max_bytes_to_read = GetMaxBytesForMessageType(type);
129 130
130 // Check the left-over bytes from the last read to make sure that we don't 131 // Check the left-over bytes from the last read to make sure that we don't
131 // already have a full message. 132 // already have a full message.
132 BattOrMessageType parsed_type; 133 BattOrMessageType parsed_type;
133 scoped_ptr<vector<char>> bytes(new vector<char>()); 134 std::unique_ptr<vector<char>> bytes(new vector<char>());
134 bytes->reserve(max_bytes_to_read); 135 bytes->reserve(max_bytes_to_read);
135 136
136 if (ParseMessage(&parsed_type, bytes.get())) { 137 if (ParseMessage(&parsed_type, bytes.get())) {
137 base::ThreadTaskRunnerHandle::Get()->PostTask( 138 base::ThreadTaskRunnerHandle::Get()->PostTask(
138 FROM_HERE, 139 FROM_HERE,
139 base::Bind(&Listener::OnMessageRead, base::Unretained(listener_), true, 140 base::Bind(&Listener::OnMessageRead, base::Unretained(listener_), true,
140 parsed_type, base::Passed(std::move(bytes)))); 141 parsed_type, base::Passed(std::move(bytes))));
141 return; 142 return;
142 } 143 }
143 144
(...skipping 10 matching lines...) Expand all
154 ui_thread_task_runner_); 155 ui_thread_task_runner_);
155 } 156 }
156 157
157 void BattOrConnectionImpl::BeginReadBytes(size_t max_bytes_to_read) { 158 void BattOrConnectionImpl::BeginReadBytes(size_t max_bytes_to_read) {
158 pending_read_buffer_ = 159 pending_read_buffer_ =
159 make_scoped_refptr(new net::IOBuffer(max_bytes_to_read)); 160 make_scoped_refptr(new net::IOBuffer(max_bytes_to_read));
160 161
161 auto on_receive_buffer_filled = 162 auto on_receive_buffer_filled =
162 base::Bind(&BattOrConnectionImpl::OnBytesRead, AsWeakPtr()); 163 base::Bind(&BattOrConnectionImpl::OnBytesRead, AsWeakPtr());
163 164
164 io_handler_->Read(make_scoped_ptr(new device::ReceiveBuffer( 165 io_handler_->Read(base::WrapUnique(new device::ReceiveBuffer(
165 pending_read_buffer_, static_cast<uint32_t>(max_bytes_to_read), 166 pending_read_buffer_, static_cast<uint32_t>(max_bytes_to_read),
166 on_receive_buffer_filled))); 167 on_receive_buffer_filled)));
167 } 168 }
168 169
169 void BattOrConnectionImpl::OnBytesRead(int bytes_read, 170 void BattOrConnectionImpl::OnBytesRead(int bytes_read,
170 device::serial::ReceiveError error) { 171 device::serial::ReceiveError error) {
171 if (bytes_read == 0 || error != device::serial::ReceiveError::NONE) { 172 if (bytes_read == 0 || error != device::serial::ReceiveError::NONE) {
172 // If we didn't have a message before, and we weren't able to read any 173 // If we didn't have a message before, and we weren't able to read any
173 // additional bytes, then there's no valid message available. 174 // additional bytes, then there's no valid message available.
174 EndReadBytes(false, BATTOR_MESSAGE_TYPE_CONTROL, nullptr); 175 EndReadBytes(false, BATTOR_MESSAGE_TYPE_CONTROL, nullptr);
175 return; 176 return;
176 } 177 }
177 178
178 already_read_buffer_.insert(already_read_buffer_.end(), 179 already_read_buffer_.insert(already_read_buffer_.end(),
179 pending_read_buffer_->data(), 180 pending_read_buffer_->data(),
180 pending_read_buffer_->data() + bytes_read); 181 pending_read_buffer_->data() + bytes_read);
181 182
182 BattOrMessageType type; 183 BattOrMessageType type;
183 scoped_ptr<vector<char>> bytes(new vector<char>()); 184 std::unique_ptr<vector<char>> bytes(new vector<char>());
184 bytes->reserve(GetMaxBytesForMessageType(pending_read_message_type_)); 185 bytes->reserve(GetMaxBytesForMessageType(pending_read_message_type_));
185 186
186 if (!ParseMessage(&type, bytes.get())) { 187 if (!ParseMessage(&type, bytes.get())) {
187 // Even after reading the max number of bytes, we still don't have a valid 188 // Even after reading the max number of bytes, we still don't have a valid
188 // message. 189 // message.
189 EndReadBytes(false, BATTOR_MESSAGE_TYPE_CONTROL, nullptr); 190 EndReadBytes(false, BATTOR_MESSAGE_TYPE_CONTROL, nullptr);
190 return; 191 return;
191 } 192 }
192 193
193 if (type != pending_read_message_type_) { 194 if (type != pending_read_message_type_) {
194 // We received a complete message, but it wasn't the type we were expecting. 195 // We received a complete message, but it wasn't the type we were expecting.
195 EndReadBytes(false, BATTOR_MESSAGE_TYPE_CONTROL, nullptr); 196 EndReadBytes(false, BATTOR_MESSAGE_TYPE_CONTROL, nullptr);
196 return; 197 return;
197 } 198 }
198 199
199 EndReadBytes(true, type, std::move(bytes)); 200 EndReadBytes(true, type, std::move(bytes));
200 } 201 }
201 202
202 void BattOrConnectionImpl::EndReadBytes(bool success, 203 void BattOrConnectionImpl::EndReadBytes(
203 BattOrMessageType type, 204 bool success,
204 scoped_ptr<std::vector<char>> bytes) { 205 BattOrMessageType type,
206 std::unique_ptr<std::vector<char>> bytes) {
205 pending_read_buffer_ = nullptr; 207 pending_read_buffer_ = nullptr;
206 base::ThreadTaskRunnerHandle::Get()->PostTask( 208 base::ThreadTaskRunnerHandle::Get()->PostTask(
207 FROM_HERE, 209 FROM_HERE,
208 base::Bind(&Listener::OnMessageRead, base::Unretained(listener_), success, 210 base::Bind(&Listener::OnMessageRead, base::Unretained(listener_), success,
209 type, base::Passed(std::move(bytes)))); 211 type, base::Passed(std::move(bytes))));
210 } 212 }
211 213
212 bool BattOrConnectionImpl::ParseMessage(BattOrMessageType* type, 214 bool BattOrConnectionImpl::ParseMessage(BattOrMessageType* type,
213 vector<char>* bytes) { 215 vector<char>* bytes) {
214 if (already_read_buffer_.size() <= 3) 216 if (already_read_buffer_.size() <= 3)
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 void BattOrConnectionImpl::OnBytesSent(int bytes_sent, 267 void BattOrConnectionImpl::OnBytesSent(int bytes_sent,
266 device::serial::SendError error) { 268 device::serial::SendError error) {
267 bool success = (error == device::serial::SendError::NONE) && 269 bool success = (error == device::serial::SendError::NONE) &&
268 (pending_write_length_ == static_cast<size_t>(bytes_sent)); 270 (pending_write_length_ == static_cast<size_t>(bytes_sent));
269 base::ThreadTaskRunnerHandle::Get()->PostTask( 271 base::ThreadTaskRunnerHandle::Get()->PostTask(
270 FROM_HERE, 272 FROM_HERE,
271 base::Bind(&Listener::OnBytesSent, base::Unretained(listener_), success)); 273 base::Bind(&Listener::OnBytesSent, base::Unretained(listener_), success));
272 } 274 }
273 275
274 } // namespace battor 276 } // namespace battor
OLDNEW
« no previous file with comments | « tools/battor_agent/battor_connection_impl.h ('k') | tools/battor_agent/battor_connection_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698