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 "chrome/browser/devtools/device/adb/mock_adb_server.h" | 5 #include "chrome/browser/devtools/device/adb/mock_adb_server.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 std::unique_ptr<Parser> parser_; | 212 std::unique_ptr<Parser> parser_; |
213 scoped_refptr<net::GrowableIOBuffer> input_buffer_; | 213 scoped_refptr<net::GrowableIOBuffer> input_buffer_; |
214 scoped_refptr<net::GrowableIOBuffer> output_buffer_; | 214 scoped_refptr<net::GrowableIOBuffer> output_buffer_; |
215 int bytes_to_write_; | 215 int bytes_to_write_; |
216 bool read_closed_; | 216 bool read_closed_; |
217 base::WeakPtrFactory<Connection> weak_factory_; | 217 base::WeakPtrFactory<Connection> weak_factory_; |
218 | 218 |
219 DISALLOW_COPY_AND_ASSIGN(Connection); | 219 DISALLOW_COPY_AND_ASSIGN(Connection); |
220 }; | 220 }; |
221 | 221 |
222 void AcceptConnection(); | 222 void OnConnect(); |
223 void OnAccepted(int result); | 223 void OnAccepted(int result); |
224 | 224 |
225 ParserFactory factory_; | 225 ParserFactory factory_; |
226 std::unique_ptr<net::TCPServerSocket> socket_; | 226 std::unique_ptr<net::TCPServerSocket> socket_; |
227 std::unique_ptr<net::StreamSocket> client_socket_; | 227 std::unique_ptr<net::StreamSocket> client_socket_; |
228 base::WeakPtrFactory<SimpleHttpServer> weak_factory_; | 228 base::WeakPtrFactory<SimpleHttpServer> weak_factory_; |
229 | 229 |
230 DISALLOW_COPY_AND_ASSIGN(SimpleHttpServer); | 230 DISALLOW_COPY_AND_ASSIGN(SimpleHttpServer); |
231 }; | 231 }; |
232 | 232 |
233 SimpleHttpServer::SimpleHttpServer(const ParserFactory& factory, | 233 SimpleHttpServer::SimpleHttpServer(const ParserFactory& factory, |
234 net::IPEndPoint endpoint) | 234 net::IPEndPoint endpoint) |
235 : factory_(factory), | 235 : factory_(factory), |
236 socket_(new net::TCPServerSocket(nullptr, net::NetLog::Source())), | 236 socket_(new net::TCPServerSocket(nullptr, net::NetLog::Source())), |
237 weak_factory_(this) { | 237 weak_factory_(this) { |
238 socket_->Listen(endpoint, 5); | 238 socket_->Listen(endpoint, 5); |
239 AcceptConnection(); | 239 OnConnect(); |
240 } | 240 } |
241 | 241 |
242 SimpleHttpServer::~SimpleHttpServer() { | 242 SimpleHttpServer::~SimpleHttpServer() { |
243 } | 243 } |
244 | 244 |
245 SimpleHttpServer::Connection::Connection(net::StreamSocket* socket, | 245 SimpleHttpServer::Connection::Connection(net::StreamSocket* socket, |
246 const ParserFactory& factory) | 246 const ParserFactory& factory) |
247 : socket_(socket), | 247 : socket_(socket), |
248 parser_(factory.Run(base::Bind(&Connection::Send, | 248 parser_(factory.Run(base::Bind(&Connection::Send, |
249 base::Unretained(this)))), | 249 base::Unretained(this)))), |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 | 357 |
358 if (bytes_to_write_ != 0) | 358 if (bytes_to_write_ != 0) |
359 // Posting to avoid deep recursion in case of synchronous IO | 359 // Posting to avoid deep recursion in case of synchronous IO |
360 base::ThreadTaskRunnerHandle::Get()->PostTask( | 360 base::ThreadTaskRunnerHandle::Get()->PostTask( |
361 FROM_HERE, | 361 FROM_HERE, |
362 base::Bind(&Connection::WriteData, weak_factory_.GetWeakPtr())); | 362 base::Bind(&Connection::WriteData, weak_factory_.GetWeakPtr())); |
363 else if (read_closed_) | 363 else if (read_closed_) |
364 delete this; | 364 delete this; |
365 } | 365 } |
366 | 366 |
367 void SimpleHttpServer::AcceptConnection() { | 367 void SimpleHttpServer::OnConnect() { |
368 CHECK(CalledOnValidThread()); | 368 CHECK(CalledOnValidThread()); |
369 | 369 |
370 int accept_result = socket_->Accept(&client_socket_, | 370 int accept_result = socket_->Accept(&client_socket_, |
371 base::Bind(&SimpleHttpServer::OnAccepted, base::Unretained(this))); | 371 base::Bind(&SimpleHttpServer::OnAccepted, base::Unretained(this))); |
372 | 372 |
373 if (accept_result != net::ERR_IO_PENDING) | 373 if (accept_result != net::ERR_IO_PENDING) |
374 base::ThreadTaskRunnerHandle::Get()->PostTask( | 374 base::ThreadTaskRunnerHandle::Get()->PostTask( |
375 FROM_HERE, base::Bind(&SimpleHttpServer::OnAccepted, | 375 FROM_HERE, base::Bind(&SimpleHttpServer::OnAccepted, |
376 weak_factory_.GetWeakPtr(), accept_result)); | 376 weak_factory_.GetWeakPtr(), accept_result)); |
377 } | 377 } |
378 | 378 |
379 void SimpleHttpServer::OnAccepted(int result) { | 379 void SimpleHttpServer::OnAccepted(int result) { |
380 CHECK(CalledOnValidThread()); | 380 CHECK(CalledOnValidThread()); |
381 ASSERT_EQ(result, 0); // Fails if the socket is already in use. | 381 ASSERT_EQ(result, 0); // Fails if the socket is already in use. |
382 new Connection(client_socket_.release(), factory_); | 382 new Connection(client_socket_.release(), factory_); |
383 AcceptConnection(); | 383 OnConnect(); |
384 } | 384 } |
385 | 385 |
386 class AdbParser : public SimpleHttpServer::Parser, | 386 class AdbParser : public SimpleHttpServer::Parser, |
387 public base::NonThreadSafe, | 387 public base::NonThreadSafe, |
388 public MockAndroidConnection::Delegate { | 388 public MockAndroidConnection::Delegate { |
389 public: | 389 public: |
390 static Parser* Create(FlushMode flush_mode, | 390 static Parser* Create(FlushMode flush_mode, |
391 const SimpleHttpServer::SendCallback& callback) { | 391 const SimpleHttpServer::SendCallback& callback) { |
392 return new AdbParser(flush_mode, callback); | 392 return new AdbParser(flush_mode, callback); |
393 } | 393 } |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
611 content::RunMessageLoop(); | 611 content::RunMessageLoop(); |
612 } | 612 } |
613 | 613 |
614 void StopMockAdbServer() { | 614 void StopMockAdbServer() { |
615 BrowserThread::PostTaskAndReply(BrowserThread::IO, FROM_HERE, | 615 BrowserThread::PostTaskAndReply(BrowserThread::IO, FROM_HERE, |
616 base::Bind(&StopMockAdbServerOnIOThread), | 616 base::Bind(&StopMockAdbServerOnIOThread), |
617 base::MessageLoop::QuitWhenIdleClosure()); | 617 base::MessageLoop::QuitWhenIdleClosure()); |
618 content::RunMessageLoop(); | 618 content::RunMessageLoop(); |
619 } | 619 } |
620 | 620 |
OLD | NEW |