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" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/base/rand_callback.h" | 12 #include "net/base/rand_callback.h" |
13 #include "net/udp/datagram_socket.h" | 13 #include "net/udp/datagram_socket.h" |
14 #include "net/udp/udp_client_socket.h" | 14 #include "net/udp/udp_client_socket.h" |
15 #include "net/udp/udp_socket.h" | 15 #include "net/udp/udp_socket.h" |
16 | 16 |
17 using namespace net; | 17 using namespace net; |
18 | 18 |
19 namespace extensions { | 19 namespace extensions { |
20 | 20 |
21 // A Socket wraps a low-level socket and includes housekeeping information that | 21 // A Socket wraps a low-level socket and includes housekeeping information that |
22 // we need to manage it in the context of an extension. | 22 // we need to manage it in the context of an extension. |
23 class Socket { | 23 class Socket { |
24 public: | 24 public: |
25 explicit Socket(const Profile* profile, const std::string& src_extension_id, | 25 Socket(const Profile* profile, const std::string& src_extension_id, |
26 const GURL& src_url); | 26 const GURL& src_url); |
27 ~Socket(); | 27 ~Socket(); |
28 | 28 |
29 bool Connect(const net::IPEndPoint& ip_end_point); | 29 bool Connect(const net::IPEndPoint& ip_end_point); |
30 void Close(); | 30 void Close(); |
31 int Write(const std::string message); | 31 int Write(const std::string message); |
32 | 32 |
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_; |
(...skipping 20 matching lines...) Expand all Loading... | |
57 NULL, | 57 NULL, |
58 NetLog::Source())), | 58 NetLog::Source())), |
59 is_connected_(false), | 59 is_connected_(false), |
60 ALLOW_THIS_IN_INITIALIZER_LIST( | 60 ALLOW_THIS_IN_INITIALIZER_LIST( |
61 io_callback_(this, &Socket::OnIOComplete)) {} | 61 io_callback_(this, &Socket::OnIOComplete)) {} |
62 | 62 |
63 Socket::~Socket() { | 63 Socket::~Socket() { |
64 if (is_connected_) { | 64 if (is_connected_) { |
65 Close(); | 65 Close(); |
66 } | 66 } |
67 delete udp_client_socket_; | |
cbentzel
2011/12/06 18:56:32
Could you use a scoped_ptr instead?
| |
67 } | 68 } |
68 | 69 |
69 void Socket::OnIOComplete(int result) { | 70 void Socket::OnIOComplete(int result) { |
70 // We don't need to do anything. | 71 // We don't need to do anything. |
71 } | 72 } |
72 | 73 |
73 bool Socket::Connect(const net::IPEndPoint& ip_end_point) { | 74 bool Socket::Connect(const net::IPEndPoint& ip_end_point) { |
74 is_connected_ = udp_client_socket_->Connect(ip_end_point) == net::OK; | 75 is_connected_ = udp_client_socket_->Connect(ip_end_point) == net::OK; |
75 return is_connected_; | 76 return is_connected_; |
76 } | 77 } |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 | 173 |
173 int SocketController::WriteUdp(int socket_id, const std::string message) { | 174 int SocketController::WriteUdp(int socket_id, const std::string message) { |
174 Socket* socket = GetSocket(socket_id); | 175 Socket* socket = GetSocket(socket_id); |
175 if (!socket) { | 176 if (!socket) { |
176 return -1; | 177 return -1; |
177 } | 178 } |
178 return socket->Write(message); | 179 return socket->Write(message); |
179 } | 180 } |
180 | 181 |
181 } // namespace extensions | 182 } // namespace extensions |
OLD | NEW |