| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/json/json_writer.h" | 5 #include "base/json/json_writer.h" |
| 6 #include "base/stl_util.h" | 6 #include "base/stl_util.h" |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/extensions/api/socket/socket_api_controller.h" | 8 #include "chrome/browser/extensions/api/socket/socket_api_controller.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 private: | 33 private: |
| 34 // TODO(miket): this metadata will enable us to pass events back to the | 34 // TODO(miket): this metadata will enable us to pass events back to the |
| 35 // extension that created this Socket. | 35 // extension that created this Socket. |
| 36 const Profile* profile_; | 36 const Profile* profile_; |
| 37 int id_; | 37 int id_; |
| 38 std::string src_extension_id_; | 38 std::string src_extension_id_; |
| 39 GURL src_url_; | 39 GURL src_url_; |
| 40 | 40 |
| 41 net::UDPClientSocket* udp_client_socket_; | 41 net::UDPClientSocket* udp_client_socket_; |
| 42 bool is_connected_; | 42 bool is_connected_; |
| 43 net::OldCompletionCallbackImpl<Socket> io_callback_; | |
| 44 | 43 |
| 45 // A callback required by UDPClientSocket::Write(). | 44 // A callback required by UDPClientSocket::Write(). |
| 46 void OnIOComplete(int result); | 45 void OnIOComplete(int result); |
| 47 }; | 46 }; |
| 48 | 47 |
| 49 Socket::Socket(const Profile* profile, const std::string& src_extension_id, | 48 Socket::Socket(const Profile* profile, const std::string& src_extension_id, |
| 50 const GURL& src_url) | 49 const GURL& src_url) |
| 51 : profile_(profile), | 50 : profile_(profile), |
| 52 src_extension_id_(src_extension_id), | 51 src_extension_id_(src_extension_id), |
| 53 src_url_(src_url), | 52 src_url_(src_url), |
| 54 udp_client_socket_(new UDPClientSocket( | 53 udp_client_socket_(new UDPClientSocket( |
| 55 DatagramSocket::DEFAULT_BIND, | 54 DatagramSocket::DEFAULT_BIND, |
| 56 RandIntCallback(), | 55 RandIntCallback(), |
| 57 NULL, | 56 NULL, |
| 58 NetLog::Source())), | 57 NetLog::Source())), |
| 59 is_connected_(false), | 58 is_connected_(false) {} |
| 60 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 61 io_callback_(this, &Socket::OnIOComplete)) {} | |
| 62 | 59 |
| 63 Socket::~Socket() { | 60 Socket::~Socket() { |
| 64 if (is_connected_) { | 61 if (is_connected_) { |
| 65 Close(); | 62 Close(); |
| 66 } | 63 } |
| 67 } | 64 } |
| 68 | 65 |
| 69 void Socket::OnIOComplete(int result) { | 66 void Socket::OnIOComplete(int result) { |
| 70 // We don't need to do anything. | 67 // We don't need to do anything. |
| 71 } | 68 } |
| 72 | 69 |
| 73 bool Socket::Connect(const net::IPEndPoint& ip_end_point) { | 70 bool Socket::Connect(const net::IPEndPoint& ip_end_point) { |
| 74 is_connected_ = udp_client_socket_->Connect(ip_end_point) == net::OK; | 71 is_connected_ = udp_client_socket_->Connect(ip_end_point) == net::OK; |
| 75 return is_connected_; | 72 return is_connected_; |
| 76 } | 73 } |
| 77 | 74 |
| 78 void Socket::Close() { | 75 void Socket::Close() { |
| 79 is_connected_ = false; | 76 is_connected_ = false; |
| 80 udp_client_socket_->Close(); | 77 udp_client_socket_->Close(); |
| 81 } | 78 } |
| 82 | 79 |
| 83 int Socket::Write(const std::string message) { | 80 int Socket::Write(const std::string message) { |
| 84 int length = message.length(); | 81 int length = message.length(); |
| 85 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(message)); | 82 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(message)); |
| 86 scoped_refptr<DrainableIOBuffer> buffer( | 83 scoped_refptr<DrainableIOBuffer> buffer( |
| 87 new DrainableIOBuffer(io_buffer, length)); | 84 new DrainableIOBuffer(io_buffer, length)); |
| 88 | 85 |
| 89 int bytes_sent = 0; | 86 int bytes_sent = 0; |
| 90 while (buffer->BytesRemaining()) { | 87 while (buffer->BytesRemaining()) { |
| 91 int rv = udp_client_socket_->Write(buffer, | 88 int rv = udp_client_socket_->Write( |
| 92 buffer->BytesRemaining(), | 89 buffer, buffer->BytesRemaining(), |
| 93 &io_callback_); | 90 base::Bind(&Socket::OnIOComplete, base::Unretained(this))); |
| 94 if (rv <= 0) { | 91 if (rv <= 0) { |
| 95 // We pass all errors, including ERROR_IO_PENDING, back to the caller. | 92 // We pass all errors, including ERROR_IO_PENDING, back to the caller. |
| 96 return bytes_sent > 0 ? bytes_sent : rv; | 93 return bytes_sent > 0 ? bytes_sent : rv; |
| 97 } | 94 } |
| 98 bytes_sent += rv; | 95 bytes_sent += rv; |
| 99 buffer->DidConsume(rv); | 96 buffer->DidConsume(rv); |
| 100 } | 97 } |
| 101 return bytes_sent; | 98 return bytes_sent; |
| 102 } | 99 } |
| 103 | 100 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 | 169 |
| 173 int SocketController::WriteUdp(int socket_id, const std::string message) { | 170 int SocketController::WriteUdp(int socket_id, const std::string message) { |
| 174 Socket* socket = GetSocket(socket_id); | 171 Socket* socket = GetSocket(socket_id); |
| 175 if (!socket) { | 172 if (!socket) { |
| 176 return -1; | 173 return -1; |
| 177 } | 174 } |
| 178 return socket->Write(message); | 175 return socket->Write(message); |
| 179 } | 176 } |
| 180 | 177 |
| 181 } // namespace extensions | 178 } // namespace extensions |
| OLD | NEW |