Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: chrome/browser/extensions/api/socket/socket_api_controller.cc

Issue 8857004: Delete UDPClientSocket on same thread as creation. Also refactor. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Initial. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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) {}
109
110 SocketController::~SocketController() { 107 SocketController::~SocketController() {
111 STLDeleteValues(&socket_map_); 108 STLDeleteValues(&socket_map_);
Aaron Boodman 2011/12/07 23:16:22 You can use std::map<int, linked_ptr<Socket>>. The
112 } 109 }
113 110
114 Socket* SocketController::GetSocket(int socket_id) { 111 Socket* SocketController::GetSocket(int socket_id) {
115 // TODO(miket): we should verify that the extension asking for the 112 // TODO(miket): we should verify that the extension asking for the
116 // socket is the same one that created it. 113 // socket is the same one that created it.
117 SocketMap::iterator i = socket_map_.find(socket_id); 114 SocketMap::iterator i = socket_map_.find(socket_id);
118 if (i != socket_map_.end()) 115 if (i != socket_map_.end())
119 return i->second; 116 return i->second;
120 return NULL; 117 return NULL;
121 } 118 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698