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_; |
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 scoped_ptr<net::UDPClientSocket> udp_client_socket_; |
42 bool is_connected_; | 42 bool is_connected_; |
43 net::OldCompletionCallbackImpl<Socket> io_callback_; | 43 net::OldCompletionCallbackImpl<Socket> io_callback_; |
44 | 44 |
45 // A callback required by UDPClientSocket::Write(). | 45 // A callback required by UDPClientSocket::Write(). |
46 void OnIOComplete(int result); | 46 void OnIOComplete(int result); |
47 }; | 47 }; |
48 | 48 |
49 Socket::Socket(const Profile* profile, const std::string& src_extension_id, | 49 Socket::Socket(const Profile* profile, const std::string& src_extension_id, |
50 const GURL& src_url) | 50 const GURL& src_url) |
51 : profile_(profile), | 51 : profile_(profile), |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 if (rv <= 0) { | 94 if (rv <= 0) { |
95 // We pass all errors, including ERROR_IO_PENDING, back to the caller. | 95 // We pass all errors, including ERROR_IO_PENDING, back to the caller. |
96 return bytes_sent > 0 ? bytes_sent : rv; | 96 return bytes_sent > 0 ? bytes_sent : rv; |
97 } | 97 } |
98 bytes_sent += rv; | 98 bytes_sent += rv; |
99 buffer->DidConsume(rv); | 99 buffer->DidConsume(rv); |
100 } | 100 } |
101 return bytes_sent; | 101 return bytes_sent; |
102 } | 102 } |
103 | 103 |
104 SocketController* SocketController::GetInstance() { | 104 SocketController::SocketController() : next_socket_id_(1) { |
105 return Singleton<SocketController>::get(); | |
106 } | 105 } |
107 | 106 |
108 SocketController::SocketController() : next_socket_id_(1) {} | 107 SocketController::~SocketController() {} |
109 | |
110 SocketController::~SocketController() { | |
111 STLDeleteValues(&socket_map_); | |
112 } | |
113 | 108 |
114 Socket* SocketController::GetSocket(int socket_id) { | 109 Socket* SocketController::GetSocket(int socket_id) { |
115 // TODO(miket): we should verify that the extension asking for the | 110 // TODO(miket): we should verify that the extension asking for the |
116 // socket is the same one that created it. | 111 // socket is the same one that created it. |
117 SocketMap::iterator i = socket_map_.find(socket_id); | 112 SocketMap::iterator i = socket_map_.find(socket_id); |
118 if (i != socket_map_.end()) | 113 if (i != socket_map_.end()) |
119 return i->second; | 114 return i->second.get(); |
120 return NULL; | 115 return NULL; |
121 } | 116 } |
122 | 117 |
123 int SocketController::CreateUdp(const Profile* profile, | 118 int SocketController::CreateUdp(const Profile* profile, |
124 const std::string& extension_id, | 119 const std::string& extension_id, |
125 const GURL& src_url) { | 120 const GURL& src_url) { |
126 Socket* socket = new Socket(profile, extension_id, src_url); | 121 linked_ptr<Socket> socket(new Socket(profile, extension_id, src_url)); |
127 CHECK(socket); | 122 CHECK(socket.get()); |
128 socket_map_[next_socket_id_] = socket; | 123 socket_map_[next_socket_id_] = socket; |
129 return next_socket_id_++; | 124 return next_socket_id_++; |
130 } | 125 } |
131 | 126 |
132 bool SocketController::DestroyUdp(int socket_id) { | 127 bool SocketController::DestroyUdp(int socket_id) { |
133 Socket* socket = GetSocket(socket_id); | 128 Socket* socket = GetSocket(socket_id); |
134 if (!socket) | 129 if (!socket) |
135 return false; | 130 return false; |
136 delete socket; | 131 delete socket; |
137 socket_map_.erase(socket_id); | 132 socket_map_.erase(socket_id); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 | 167 |
173 int SocketController::WriteUdp(int socket_id, const std::string message) { | 168 int SocketController::WriteUdp(int socket_id, const std::string message) { |
174 Socket* socket = GetSocket(socket_id); | 169 Socket* socket = GetSocket(socket_id); |
175 if (!socket) { | 170 if (!socket) { |
176 return -1; | 171 return -1; |
177 } | 172 } |
178 return socket->Write(message); | 173 return socket->Write(message); |
179 } | 174 } |
180 | 175 |
181 } // namespace extensions | 176 } // namespace extensions |
OLD | NEW |