OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/api/sockets_tcp_server/sockets_tcp_server_ap
i.h" | |
6 | |
7 #include "chrome/browser/extensions/api/socket/tcp_socket.h" | |
8 #include "chrome/browser/extensions/api/sockets_tcp_server/tcp_server_socket_eve
nt_dispatcher.h" | |
9 #include "chrome/common/extensions/api/sockets/sockets_manifest_data.h" | |
10 #include "chrome/common/extensions/permissions/socket_permission.h" | |
11 #include "content/public/common/socket_permission_request.h" | |
12 #include "extensions/common/permissions/permissions_data.h" | |
13 #include "net/base/net_errors.h" | |
14 | |
15 using content::SocketPermissionRequest; | |
16 using extensions::ResumableTCPServerSocket; | |
17 using extensions::api::sockets_tcp_server::SocketInfo; | |
18 using extensions::api::sockets_tcp_server::SocketProperties; | |
19 | |
20 namespace { | |
21 | |
22 const char kSocketNotFoundError[] = "Socket not found"; | |
23 const char kPermissionError[] = "Does not have permission"; | |
24 const int kDefaultListenBacklog = SOMAXCONN; | |
25 | |
26 linked_ptr<SocketInfo> CreateSocketInfo(int socket_id, | |
27 ResumableTCPServerSocket* socket) { | |
28 linked_ptr<SocketInfo> socket_info(new SocketInfo()); | |
29 // This represents what we know about the socket, and does not call through | |
30 // to the system. | |
31 socket_info->socket_id = socket_id; | |
32 if (!socket->name().empty()) { | |
33 socket_info->name.reset(new std::string(socket->name())); | |
34 } | |
35 socket_info->persistent = socket->persistent(); | |
36 socket_info->paused = socket->paused(); | |
37 | |
38 // Grab the local address as known by the OS. | |
39 net::IPEndPoint localAddress; | |
40 if (socket->GetLocalAddress(&localAddress)) { | |
41 socket_info->local_address.reset( | |
42 new std::string(localAddress.ToStringWithoutPort())); | |
43 socket_info->local_port.reset(new int(localAddress.port())); | |
44 } | |
45 | |
46 return socket_info; | |
47 } | |
48 | |
49 void SetSocketProperties(ResumableTCPServerSocket* socket, | |
50 SocketProperties* properties) { | |
51 if (properties->name.get()) { | |
52 socket->set_name(*properties->name.get()); | |
53 } | |
54 if (properties->persistent.get()) { | |
55 socket->set_persistent(*properties->persistent.get()); | |
56 } | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 namespace extensions { | |
62 namespace api { | |
63 | |
64 TCPServerSocketAsyncApiFunction::~TCPServerSocketAsyncApiFunction() {} | |
65 | |
66 scoped_ptr<SocketResourceManagerInterface> | |
67 TCPServerSocketAsyncApiFunction::CreateSocketResourceManager() { | |
68 return scoped_ptr<SocketResourceManagerInterface>( | |
69 new SocketResourceManager<ResumableTCPServerSocket>()).Pass(); | |
70 } | |
71 | |
72 ResumableTCPServerSocket* TCPServerSocketAsyncApiFunction::GetTcpSocket( | |
73 int socket_id) { | |
74 return static_cast<ResumableTCPServerSocket*>(GetSocket(socket_id)); | |
75 } | |
76 | |
77 SocketsTcpServerCreateFunction::SocketsTcpServerCreateFunction() {} | |
78 | |
79 SocketsTcpServerCreateFunction::~SocketsTcpServerCreateFunction() {} | |
80 | |
81 bool SocketsTcpServerCreateFunction::Prepare() { | |
82 params_ = sockets_tcp_server::Create::Params::Create(*args_); | |
83 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
84 return true; | |
85 } | |
86 | |
87 void SocketsTcpServerCreateFunction::Work() { | |
88 ResumableTCPServerSocket* socket = | |
89 new ResumableTCPServerSocket(extension_->id()); | |
90 | |
91 sockets_tcp_server::SocketProperties* properties = | |
92 params_.get()->properties.get(); | |
93 if (properties) { | |
94 SetSocketProperties(socket, properties); | |
95 } | |
96 | |
97 sockets_tcp_server::CreateInfo create_info; | |
98 create_info.socket_id = AddSocket(socket); | |
99 results_ = sockets_tcp_server::Create::Results::Create(create_info); | |
100 } | |
101 | |
102 SocketsTcpServerUpdateFunction::SocketsTcpServerUpdateFunction() {} | |
103 | |
104 SocketsTcpServerUpdateFunction::~SocketsTcpServerUpdateFunction() {} | |
105 | |
106 bool SocketsTcpServerUpdateFunction::Prepare() { | |
107 params_ = sockets_tcp_server::Update::Params::Create(*args_); | |
108 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
109 return true; | |
110 } | |
111 | |
112 void SocketsTcpServerUpdateFunction::Work() { | |
113 ResumableTCPServerSocket* socket = GetTcpSocket(params_->socket_id); | |
114 if (!socket) { | |
115 error_ = kSocketNotFoundError; | |
116 return; | |
117 } | |
118 | |
119 SetSocketProperties(socket, ¶ms_.get()->properties); | |
120 results_ = sockets_tcp_server::Update::Results::Create(); | |
121 } | |
122 | |
123 SocketsTcpServerSetPausedFunction::SocketsTcpServerSetPausedFunction() | |
124 : socket_event_dispatcher_(NULL) {} | |
125 | |
126 SocketsTcpServerSetPausedFunction::~SocketsTcpServerSetPausedFunction() {} | |
127 | |
128 bool SocketsTcpServerSetPausedFunction::Prepare() { | |
129 params_ = api::sockets_tcp_server::SetPaused::Params::Create(*args_); | |
130 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
131 | |
132 socket_event_dispatcher_ = | |
133 TCPServerSocketEventDispatcher::Get(browser_context()); | |
134 DCHECK(socket_event_dispatcher_) << "There is no socket event dispatcher. " | |
135 "If this assertion is failing during a test, then it is likely that " | |
136 "TestExtensionSystem is failing to provide an instance of " | |
137 "TCPServerSocketEventDispatcher."; | |
138 return socket_event_dispatcher_ != NULL; | |
139 } | |
140 | |
141 void SocketsTcpServerSetPausedFunction::Work() { | |
142 ResumableTCPServerSocket* socket = GetTcpSocket(params_->socket_id); | |
143 if (!socket) { | |
144 error_ = kSocketNotFoundError; | |
145 return; | |
146 } | |
147 | |
148 if (socket->paused() != params_->paused) { | |
149 socket->set_paused(params_->paused); | |
150 if (socket->IsConnected() && !params_->paused) { | |
151 socket_event_dispatcher_->OnServerSocketResume(extension_->id(), | |
152 params_->socket_id); | |
153 } | |
154 } | |
155 | |
156 results_ = sockets_tcp_server::SetPaused::Results::Create(); | |
157 } | |
158 | |
159 SocketsTcpServerListenFunction::SocketsTcpServerListenFunction() | |
160 : socket_event_dispatcher_(NULL) {} | |
161 | |
162 SocketsTcpServerListenFunction::~SocketsTcpServerListenFunction() {} | |
163 | |
164 bool SocketsTcpServerListenFunction::Prepare() { | |
165 params_ = api::sockets_tcp_server::Listen::Params::Create(*args_); | |
166 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
167 | |
168 socket_event_dispatcher_ = | |
169 TCPServerSocketEventDispatcher::Get(browser_context()); | |
170 DCHECK(socket_event_dispatcher_) << "There is no socket event dispatcher. " | |
171 "If this assertion is failing during a test, then it is likely that " | |
172 "TestExtensionSystem is failing to provide an instance of " | |
173 "TCPServerSocketEventDispatcher."; | |
174 return socket_event_dispatcher_ != NULL; | |
175 } | |
176 | |
177 void SocketsTcpServerListenFunction::Work() { | |
178 ResumableTCPServerSocket* socket = GetTcpSocket(params_->socket_id); | |
179 if (!socket) { | |
180 error_ = kSocketNotFoundError; | |
181 return; | |
182 } | |
183 | |
184 SocketPermissionRequest param( | |
185 SocketPermissionRequest::TCP_LISTEN, | |
186 params_->address, | |
187 params_->port); | |
188 if (!SocketsManifestData::CheckRequest(GetExtension(), param)) { | |
189 error_ = kPermissionError; | |
190 return; | |
191 } | |
192 | |
193 int net_result = socket->Listen( | |
194 params_->address, | |
195 params_->port, | |
196 params_->backlog.get() ? *params_->backlog.get() : kDefaultListenBacklog, | |
197 &error_); | |
198 | |
199 if (net_result != net::OK) | |
200 error_ = net::ErrorToString(net_result); | |
201 | |
202 | |
203 if (net_result == net::OK) { | |
204 socket_event_dispatcher_->OnServerSocketListen(extension_->id(), | |
205 params_->socket_id); | |
206 } | |
207 | |
208 results_ = sockets_tcp_server::Listen::Results::Create(net_result); | |
209 } | |
210 | |
211 SocketsTcpServerDisconnectFunction::SocketsTcpServerDisconnectFunction() {} | |
212 | |
213 SocketsTcpServerDisconnectFunction::~SocketsTcpServerDisconnectFunction() {} | |
214 | |
215 bool SocketsTcpServerDisconnectFunction::Prepare() { | |
216 params_ = sockets_tcp_server::Disconnect::Params::Create(*args_); | |
217 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
218 return true; | |
219 } | |
220 | |
221 void SocketsTcpServerDisconnectFunction::Work() { | |
222 ResumableTCPServerSocket* socket = GetTcpSocket(params_->socket_id); | |
223 if (!socket) { | |
224 error_ = kSocketNotFoundError; | |
225 return; | |
226 } | |
227 | |
228 socket->Disconnect(); | |
229 results_ = sockets_tcp_server::Disconnect::Results::Create(); | |
230 } | |
231 | |
232 SocketsTcpServerCloseFunction::SocketsTcpServerCloseFunction() {} | |
233 | |
234 SocketsTcpServerCloseFunction::~SocketsTcpServerCloseFunction() {} | |
235 | |
236 bool SocketsTcpServerCloseFunction::Prepare() { | |
237 params_ = sockets_tcp_server::Close::Params::Create(*args_); | |
238 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
239 return true; | |
240 } | |
241 | |
242 void SocketsTcpServerCloseFunction::Work() { | |
243 ResumableTCPServerSocket* socket = GetTcpSocket(params_->socket_id); | |
244 if (!socket) { | |
245 error_ = kSocketNotFoundError; | |
246 return; | |
247 } | |
248 | |
249 RemoveSocket(params_->socket_id); | |
250 results_ = sockets_tcp_server::Close::Results::Create(); | |
251 } | |
252 | |
253 SocketsTcpServerGetInfoFunction::SocketsTcpServerGetInfoFunction() {} | |
254 | |
255 SocketsTcpServerGetInfoFunction::~SocketsTcpServerGetInfoFunction() {} | |
256 | |
257 bool SocketsTcpServerGetInfoFunction::Prepare() { | |
258 params_ = sockets_tcp_server::GetInfo::Params::Create(*args_); | |
259 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
260 return true; | |
261 } | |
262 | |
263 void SocketsTcpServerGetInfoFunction::Work() { | |
264 ResumableTCPServerSocket* socket = GetTcpSocket(params_->socket_id); | |
265 if (!socket) { | |
266 error_ = kSocketNotFoundError; | |
267 return; | |
268 } | |
269 | |
270 linked_ptr<sockets_tcp_server::SocketInfo> socket_info = | |
271 CreateSocketInfo(params_->socket_id, socket); | |
272 results_ = sockets_tcp_server::GetInfo::Results::Create(*socket_info); | |
273 } | |
274 | |
275 SocketsTcpServerGetSocketsFunction::SocketsTcpServerGetSocketsFunction() {} | |
276 | |
277 SocketsTcpServerGetSocketsFunction::~SocketsTcpServerGetSocketsFunction() {} | |
278 | |
279 bool SocketsTcpServerGetSocketsFunction::Prepare() { | |
280 return true; | |
281 } | |
282 | |
283 void SocketsTcpServerGetSocketsFunction::Work() { | |
284 std::vector<linked_ptr<sockets_tcp_server::SocketInfo> > socket_infos; | |
285 base::hash_set<int>* resource_ids = GetSocketIds(); | |
286 if (resource_ids != NULL) { | |
287 for (base::hash_set<int>::iterator it = resource_ids->begin(); | |
288 it != resource_ids->end(); ++it) { | |
289 int socket_id = *it; | |
290 ResumableTCPServerSocket* socket = GetTcpSocket(socket_id); | |
291 if (socket) { | |
292 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); | |
293 } | |
294 } | |
295 } | |
296 results_ = sockets_tcp_server::GetSockets::Results::Create(socket_infos); | |
297 } | |
298 | |
299 } // namespace api | |
300 } // namespace extensions | |
OLD | NEW |