Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/api/socket/socket.h" | 5 #include "chrome/browser/extensions/api/socket/socket.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" | 8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" |
| 9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 10 #include "net/base/ip_endpoint.h" | 10 #include "net/base/ip_endpoint.h" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "net/socket/socket.h" | 12 #include "net/socket/socket.h" |
| 13 | 13 |
| 14 namespace extensions { | 14 namespace extensions { |
| 15 | 15 |
| 16 Socket::Socket(const std::string& address, int port, | 16 Socket::Socket(const std::string& address, int port, |
| 17 APIResourceEventNotifier* event_notifier) | 17 APIResourceEventNotifier* event_notifier) |
| 18 : APIResource(APIResource::SocketResource, event_notifier), | 18 : APIResource(APIResource::SocketResource, event_notifier), |
| 19 address_(address), | 19 address_(address), |
| 20 port_(port), | 20 port_(port), |
| 21 is_connected_(false), | 21 is_connected_(false) { |
| 22 read_buffer_(new net::IOBufferWithSize(kMaxRead)) { | |
| 23 } | 22 } |
| 24 | 23 |
| 25 Socket::~Socket() { | 24 Socket::~Socket() { |
| 26 // Derived destructors should make sure the socket has been closed. | 25 // Derived destructors should make sure the socket has been closed. |
| 27 DCHECK(!is_connected_); | 26 DCHECK(!is_connected_); |
| 28 } | 27 } |
| 29 | 28 |
| 30 void Socket::OnDataRead(int result) { | 29 void Socket::OnDataRead(scoped_refptr<net::IOBuffer> io_buffer, int result) { |
| 31 std::string message; | 30 // OnDataRead will take ownership of data_value. |
| 32 if (result >= 0) | 31 ListValue* data_value = new ListValue(); |
| 33 message = std::string(read_buffer_->data(), result); | 32 if (result >= 0) { |
| 34 event_notifier()->OnDataRead(result, message); | 33 size_t bytes_size = static_cast<size_t>(result); |
| 34 const char* io_buffer_start = io_buffer->data(); | |
| 35 for (size_t i = 0; i < bytes_size; ++i) { | |
| 36 data_value->Set(i, Value::CreateIntegerValue(io_buffer_start[i])); | |
| 37 } | |
| 38 } | |
| 39 event_notifier()->OnDataRead(result, data_value); | |
| 35 } | 40 } |
| 36 | 41 |
| 37 void Socket::OnWriteComplete(int result) { | 42 void Socket::OnWriteComplete(int result) { |
| 38 event_notifier()->OnWriteComplete(result); | 43 event_notifier()->OnWriteComplete(result); |
| 39 } | 44 } |
| 40 | 45 |
| 41 std::string Socket::Read() { | 46 int Socket::Read(scoped_refptr<net::IOBuffer> io_buffer, int io_buffer_len) { |
| 42 int result = socket()->Read( | 47 return socket()->Read( |
| 43 read_buffer_, kMaxRead, | 48 io_buffer.get(), |
| 44 base::Bind(&Socket::OnDataRead, base::Unretained(this))); | 49 io_buffer_len, |
| 45 if (result == net::ERR_IO_PENDING) | 50 base::Bind(&Socket::OnDataRead, base::Unretained(this), io_buffer)); |
|
jeremya
2012/04/16 22:28:27
Is it possible that this read will call the callba
miket_OOO
2012/04/16 23:15:53
I can't point you to the code that proves it, but
| |
| 46 return ""; | |
| 47 if (result < 0) | |
| 48 return ""; | |
| 49 return std::string(read_buffer_->data(), result); | |
| 50 } | 51 } |
| 51 | 52 |
| 52 int Socket::Write(const std::string& message) { | 53 int Socket::Write(scoped_refptr<net::IOBuffer> io_buffer, int byte_count) { |
| 53 int length = message.length(); | 54 scoped_refptr<net::DrainableIOBuffer> drainable_buffer( |
| 54 scoped_refptr<net::StringIOBuffer> io_buffer( | 55 new net::DrainableIOBuffer(io_buffer.get(), byte_count)); |
| 55 new net::StringIOBuffer(message)); | |
| 56 scoped_refptr<net::DrainableIOBuffer> buffer( | |
| 57 new net::DrainableIOBuffer(io_buffer, length)); | |
| 58 | 56 |
| 59 int bytes_sent = 0; | 57 int bytes_sent = 0; |
| 60 while (buffer->BytesRemaining()) { | 58 while (drainable_buffer->BytesRemaining()) { |
| 61 int result = socket()->Write( | 59 int result = socket()->Write( |
| 62 buffer, buffer->BytesRemaining(), | 60 drainable_buffer, drainable_buffer->BytesRemaining(), |
| 63 base::Bind(&Socket::OnWriteComplete, base::Unretained(this))); | 61 base::Bind(&Socket::OnWriteComplete, base::Unretained(this))); |
| 64 if (result <= 0) | 62 if (result <= 0) |
| 65 // We pass all errors, including ERROR_IO_PENDING, back to the caller. | 63 // We pass all errors, including ERROR_IO_PENDING, back to the caller. |
| 66 return bytes_sent > 0 ? bytes_sent : result; | 64 return bytes_sent > 0 ? bytes_sent : result; |
|
jeremya
2012/04/16 22:28:27
(Side note, since you don't change this behaviour
miket_OOO
2012/04/16 23:15:53
We're OK, but I think the code needs to be refacto
| |
| 67 bytes_sent += result; | 65 bytes_sent += result; |
| 68 buffer->DidConsume(result); | 66 drainable_buffer->DidConsume(result); |
| 69 } | 67 } |
| 70 return bytes_sent; | 68 return bytes_sent; |
| 71 } | 69 } |
| 72 | 70 |
| 73 } // namespace extensions | 71 } // namespace extensions |
| OLD | NEW |