OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/socket/socket_api.h" | 5 #include "extensions/browser/api/socket/socket_api.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
11 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" | 11 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" |
12 #include "chrome/browser/extensions/api/socket/socket.h" | |
13 #include "chrome/browser/extensions/api/socket/tcp_socket.h" | |
14 #include "chrome/browser/extensions/api/socket/udp_socket.h" | |
15 #include "chrome/common/extensions/permissions/socket_permission.h" | 12 #include "chrome/common/extensions/permissions/socket_permission.h" |
16 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
17 #include "content/public/browser/resource_context.h" | 14 #include "content/public/browser/resource_context.h" |
| 15 #include "extensions/browser/api/socket/socket.h" |
| 16 #include "extensions/browser/api/socket/tcp_socket.h" |
| 17 #include "extensions/browser/api/socket/udp_socket.h" |
18 #include "extensions/browser/extension_system.h" | 18 #include "extensions/browser/extension_system.h" |
19 #include "extensions/common/extension.h" | 19 #include "extensions/common/extension.h" |
20 #include "extensions/common/permissions/permissions_data.h" | 20 #include "extensions/common/permissions/permissions_data.h" |
21 #include "net/base/host_port_pair.h" | 21 #include "net/base/host_port_pair.h" |
22 #include "net/base/io_buffer.h" | 22 #include "net/base/io_buffer.h" |
23 #include "net/base/ip_endpoint.h" | 23 #include "net/base/ip_endpoint.h" |
24 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
25 #include "net/base/net_log.h" | 25 #include "net/base/net_log.h" |
26 #include "net/base/net_util.h" | 26 #include "net/base/net_util.h" |
27 | 27 |
28 namespace extensions { | 28 namespace extensions { |
29 | 29 |
30 using content::SocketPermissionRequest; | 30 using content::SocketPermissionRequest; |
31 | 31 |
32 const char kAddressKey[] = "address"; | 32 const char kAddressKey[] = "address"; |
33 const char kPortKey[] = "port"; | 33 const char kPortKey[] = "port"; |
34 const char kBytesWrittenKey[] = "bytesWritten"; | 34 const char kBytesWrittenKey[] = "bytesWritten"; |
35 const char kDataKey[] = "data"; | 35 const char kDataKey[] = "data"; |
36 const char kResultCodeKey[] = "resultCode"; | 36 const char kResultCodeKey[] = "resultCode"; |
37 const char kSocketIdKey[] = "socketId"; | 37 const char kSocketIdKey[] = "socketId"; |
38 | 38 |
39 const char kSocketNotFoundError[] = "Socket not found"; | 39 const char kSocketNotFoundError[] = "Socket not found"; |
40 const char kDnsLookupFailedError[] = "DNS resolution failed"; | 40 const char kDnsLookupFailedError[] = "DNS resolution failed"; |
41 const char kPermissionError[] = "App does not have permission"; | 41 const char kPermissionError[] = "App does not have permission"; |
42 const char kNetworkListError[] = "Network lookup failed or unsupported"; | 42 const char kNetworkListError[] = "Network lookup failed or unsupported"; |
43 const char kTCPSocketBindError[] = | 43 const char kTCPSocketBindError[] = |
44 "TCP socket does not support bind. For TCP server please use listen."; | 44 "TCP socket does not support bind. For TCP server please use listen."; |
45 const char kMulticastSocketTypeError[] = | 45 const char kMulticastSocketTypeError[] = "Only UDP socket supports multicast."; |
46 "Only UDP socket supports multicast."; | |
47 const char kWildcardAddress[] = "*"; | 46 const char kWildcardAddress[] = "*"; |
48 const int kWildcardPort = 0; | 47 const int kWildcardPort = 0; |
49 | 48 |
50 SocketAsyncApiFunction::SocketAsyncApiFunction() { | 49 SocketAsyncApiFunction::SocketAsyncApiFunction() {} |
51 } | |
52 | 50 |
53 SocketAsyncApiFunction::~SocketAsyncApiFunction() { | 51 SocketAsyncApiFunction::~SocketAsyncApiFunction() {} |
54 } | |
55 | 52 |
56 bool SocketAsyncApiFunction::PrePrepare() { | 53 bool SocketAsyncApiFunction::PrePrepare() { |
57 manager_ = CreateSocketResourceManager(); | 54 manager_ = CreateSocketResourceManager(); |
58 return manager_->SetBrowserContext(browser_context()); | 55 return manager_->SetBrowserContext(browser_context()); |
59 } | 56 } |
60 | 57 |
61 bool SocketAsyncApiFunction::Respond() { | 58 bool SocketAsyncApiFunction::Respond() { return error_.empty(); } |
62 return error_.empty(); | |
63 } | |
64 | 59 |
65 scoped_ptr<SocketResourceManagerInterface> | 60 scoped_ptr<SocketResourceManagerInterface> |
66 SocketAsyncApiFunction::CreateSocketResourceManager() { | 61 SocketAsyncApiFunction::CreateSocketResourceManager() { |
67 return scoped_ptr<SocketResourceManagerInterface>( | 62 return scoped_ptr<SocketResourceManagerInterface>( |
68 new SocketResourceManager<Socket>()).Pass(); | 63 new SocketResourceManager<Socket>()).Pass(); |
69 } | 64 } |
70 | 65 |
71 int SocketAsyncApiFunction::AddSocket(Socket* socket) { | 66 int SocketAsyncApiFunction::AddSocket(Socket* socket) { |
72 return manager_->Add(socket); | 67 return manager_->Add(socket); |
73 } | 68 } |
74 | 69 |
75 Socket* SocketAsyncApiFunction::GetSocket(int api_resource_id) { | 70 Socket* SocketAsyncApiFunction::GetSocket(int api_resource_id) { |
76 return manager_->Get(extension_->id(), api_resource_id); | 71 return manager_->Get(extension_->id(), api_resource_id); |
77 } | 72 } |
78 | 73 |
79 base::hash_set<int>* SocketAsyncApiFunction::GetSocketIds() { | 74 base::hash_set<int>* SocketAsyncApiFunction::GetSocketIds() { |
80 return manager_->GetResourceIds(extension_->id()); | 75 return manager_->GetResourceIds(extension_->id()); |
81 } | 76 } |
82 | 77 |
83 void SocketAsyncApiFunction::RemoveSocket(int api_resource_id) { | 78 void SocketAsyncApiFunction::RemoveSocket(int api_resource_id) { |
84 manager_->Remove(extension_->id(), api_resource_id); | 79 manager_->Remove(extension_->id(), api_resource_id); |
85 } | 80 } |
86 | 81 |
87 SocketExtensionWithDnsLookupFunction::SocketExtensionWithDnsLookupFunction() | 82 SocketExtensionWithDnsLookupFunction::SocketExtensionWithDnsLookupFunction() |
88 : resource_context_(NULL), | 83 : resource_context_(NULL), |
89 request_handle_(new net::HostResolver::RequestHandle), | 84 request_handle_(new net::HostResolver::RequestHandle), |
90 addresses_(new net::AddressList) {} | 85 addresses_(new net::AddressList) {} |
91 | 86 |
92 SocketExtensionWithDnsLookupFunction::~SocketExtensionWithDnsLookupFunction() { | 87 SocketExtensionWithDnsLookupFunction::~SocketExtensionWithDnsLookupFunction() {} |
93 } | |
94 | 88 |
95 bool SocketExtensionWithDnsLookupFunction::PrePrepare() { | 89 bool SocketExtensionWithDnsLookupFunction::PrePrepare() { |
96 if (!SocketAsyncApiFunction::PrePrepare()) | 90 if (!SocketAsyncApiFunction::PrePrepare()) |
97 return false; | 91 return false; |
98 resource_context_ = browser_context()->GetResourceContext(); | 92 resource_context_ = browser_context()->GetResourceContext(); |
99 return resource_context_ != NULL; | 93 return resource_context_ != NULL; |
100 } | 94 } |
101 | 95 |
102 void SocketExtensionWithDnsLookupFunction::StartDnsLookup( | 96 void SocketExtensionWithDnsLookupFunction::StartDnsLookup( |
103 const std::string& hostname) { | 97 const std::string& hostname) { |
(...skipping 25 matching lines...) Expand all Loading... |
129 if (resolve_result == net::OK) { | 123 if (resolve_result == net::OK) { |
130 DCHECK(!addresses_->empty()); | 124 DCHECK(!addresses_->empty()); |
131 resolved_address_ = addresses_->front().ToStringWithoutPort(); | 125 resolved_address_ = addresses_->front().ToStringWithoutPort(); |
132 } else { | 126 } else { |
133 error_ = kDnsLookupFailedError; | 127 error_ = kDnsLookupFailedError; |
134 } | 128 } |
135 AfterDnsLookup(resolve_result); | 129 AfterDnsLookup(resolve_result); |
136 } | 130 } |
137 | 131 |
138 SocketCreateFunction::SocketCreateFunction() | 132 SocketCreateFunction::SocketCreateFunction() |
139 : socket_type_(kSocketTypeInvalid) { | 133 : socket_type_(kSocketTypeInvalid) {} |
140 } | |
141 | 134 |
142 SocketCreateFunction::~SocketCreateFunction() {} | 135 SocketCreateFunction::~SocketCreateFunction() {} |
143 | 136 |
144 bool SocketCreateFunction::Prepare() { | 137 bool SocketCreateFunction::Prepare() { |
145 params_ = api::socket::Create::Params::Create(*args_); | 138 params_ = core_api::socket::Create::Params::Create(*args_); |
146 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 139 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
147 | 140 |
148 switch (params_->type) { | 141 switch (params_->type) { |
149 case extensions::api::socket::SOCKET_TYPE_TCP: | 142 case extensions::core_api::socket::SOCKET_TYPE_TCP: |
150 socket_type_ = kSocketTypeTCP; | 143 socket_type_ = kSocketTypeTCP; |
151 break; | 144 break; |
152 case extensions::api::socket::SOCKET_TYPE_UDP: | 145 case extensions::core_api::socket::SOCKET_TYPE_UDP: |
153 socket_type_ = kSocketTypeUDP; | 146 socket_type_ = kSocketTypeUDP; |
154 break; | 147 break; |
155 case extensions::api::socket::SOCKET_TYPE_NONE: | 148 case extensions::core_api::socket::SOCKET_TYPE_NONE: |
156 NOTREACHED(); | 149 NOTREACHED(); |
157 break; | 150 break; |
158 } | 151 } |
159 | 152 |
160 return true; | 153 return true; |
161 } | 154 } |
162 | 155 |
163 void SocketCreateFunction::Work() { | 156 void SocketCreateFunction::Work() { |
164 Socket* socket = NULL; | 157 Socket* socket = NULL; |
165 if (socket_type_ == kSocketTypeTCP) { | 158 if (socket_type_ == kSocketTypeTCP) { |
166 socket = new TCPSocket(extension_->id()); | 159 socket = new TCPSocket(extension_->id()); |
167 } else if (socket_type_== kSocketTypeUDP) { | 160 } else if (socket_type_ == kSocketTypeUDP) { |
168 socket = new UDPSocket(extension_->id()); | 161 socket = new UDPSocket(extension_->id()); |
169 } | 162 } |
170 DCHECK(socket); | 163 DCHECK(socket); |
171 | 164 |
172 base::DictionaryValue* result = new base::DictionaryValue(); | 165 base::DictionaryValue* result = new base::DictionaryValue(); |
173 result->SetInteger(kSocketIdKey, AddSocket(socket)); | 166 result->SetInteger(kSocketIdKey, AddSocket(socket)); |
174 SetResult(result); | 167 SetResult(result); |
175 } | 168 } |
176 | 169 |
177 bool SocketDestroyFunction::Prepare() { | 170 bool SocketDestroyFunction::Prepare() { |
178 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 171 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
179 return true; | 172 return true; |
180 } | 173 } |
181 | 174 |
182 void SocketDestroyFunction::Work() { | 175 void SocketDestroyFunction::Work() { RemoveSocket(socket_id_); } |
183 RemoveSocket(socket_id_); | |
184 } | |
185 | 176 |
186 SocketConnectFunction::SocketConnectFunction() | 177 SocketConnectFunction::SocketConnectFunction() |
187 : socket_id_(0), | 178 : socket_id_(0), hostname_(), port_(0), socket_(NULL) {} |
188 hostname_(), | |
189 port_(0), | |
190 socket_(NULL) { | |
191 } | |
192 | 179 |
193 SocketConnectFunction::~SocketConnectFunction() { | 180 SocketConnectFunction::~SocketConnectFunction() {} |
194 } | |
195 | 181 |
196 bool SocketConnectFunction::Prepare() { | 182 bool SocketConnectFunction::Prepare() { |
197 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 183 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
198 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); | 184 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); |
199 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); | 185 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); |
200 return true; | 186 return true; |
201 } | 187 } |
202 | 188 |
203 void SocketConnectFunction::AsyncWorkStart() { | 189 void SocketConnectFunction::AsyncWorkStart() { |
204 socket_ = GetSocket(socket_id_); | 190 socket_ = GetSocket(socket_id_); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 void SocketConnectFunction::AfterDnsLookup(int lookup_result) { | 224 void SocketConnectFunction::AfterDnsLookup(int lookup_result) { |
239 if (lookup_result == net::OK) { | 225 if (lookup_result == net::OK) { |
240 StartConnect(); | 226 StartConnect(); |
241 } else { | 227 } else { |
242 SetResult(new base::FundamentalValue(lookup_result)); | 228 SetResult(new base::FundamentalValue(lookup_result)); |
243 AsyncWorkCompleted(); | 229 AsyncWorkCompleted(); |
244 } | 230 } |
245 } | 231 } |
246 | 232 |
247 void SocketConnectFunction::StartConnect() { | 233 void SocketConnectFunction::StartConnect() { |
248 socket_->Connect(resolved_address_, port_, | 234 socket_->Connect(resolved_address_, |
| 235 port_, |
249 base::Bind(&SocketConnectFunction::OnConnect, this)); | 236 base::Bind(&SocketConnectFunction::OnConnect, this)); |
250 } | 237 } |
251 | 238 |
252 void SocketConnectFunction::OnConnect(int result) { | 239 void SocketConnectFunction::OnConnect(int result) { |
253 SetResult(new base::FundamentalValue(result)); | 240 SetResult(new base::FundamentalValue(result)); |
254 AsyncWorkCompleted(); | 241 AsyncWorkCompleted(); |
255 } | 242 } |
256 | 243 |
257 bool SocketDisconnectFunction::Prepare() { | 244 bool SocketDisconnectFunction::Prepare() { |
258 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 245 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
(...skipping 23 matching lines...) Expand all Loading... |
282 if (!socket) { | 269 if (!socket) { |
283 error_ = kSocketNotFoundError; | 270 error_ = kSocketNotFoundError; |
284 SetResult(new base::FundamentalValue(result)); | 271 SetResult(new base::FundamentalValue(result)); |
285 return; | 272 return; |
286 } | 273 } |
287 | 274 |
288 if (socket->GetSocketType() == Socket::TYPE_UDP) { | 275 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
289 SocketPermission::CheckParam param( | 276 SocketPermission::CheckParam param( |
290 SocketPermissionRequest::UDP_BIND, address_, port_); | 277 SocketPermissionRequest::UDP_BIND, address_, port_); |
291 if (!PermissionsData::CheckAPIPermissionWithParam( | 278 if (!PermissionsData::CheckAPIPermissionWithParam( |
292 GetExtension(), | 279 GetExtension(), APIPermission::kSocket, ¶m)) { |
293 APIPermission::kSocket, | |
294 ¶m)) { | |
295 error_ = kPermissionError; | 280 error_ = kPermissionError; |
296 SetResult(new base::FundamentalValue(result)); | 281 SetResult(new base::FundamentalValue(result)); |
297 return; | 282 return; |
298 } | 283 } |
299 } else if (socket->GetSocketType() == Socket::TYPE_TCP) { | 284 } else if (socket->GetSocketType() == Socket::TYPE_TCP) { |
300 error_ = kTCPSocketBindError; | 285 error_ = kTCPSocketBindError; |
301 SetResult(new base::FundamentalValue(result)); | 286 SetResult(new base::FundamentalValue(result)); |
302 return; | 287 return; |
303 } | 288 } |
304 | 289 |
305 result = socket->Bind(address_, port_); | 290 result = socket->Bind(address_, port_); |
306 SetResult(new base::FundamentalValue(result)); | 291 SetResult(new base::FundamentalValue(result)); |
307 } | 292 } |
308 | 293 |
309 SocketListenFunction::SocketListenFunction() {} | 294 SocketListenFunction::SocketListenFunction() {} |
310 | 295 |
311 SocketListenFunction::~SocketListenFunction() {} | 296 SocketListenFunction::~SocketListenFunction() {} |
312 | 297 |
313 bool SocketListenFunction::Prepare() { | 298 bool SocketListenFunction::Prepare() { |
314 params_ = api::socket::Listen::Params::Create(*args_); | 299 params_ = core_api::socket::Listen::Params::Create(*args_); |
315 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 300 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
316 return true; | 301 return true; |
317 } | 302 } |
318 | 303 |
319 void SocketListenFunction::Work() { | 304 void SocketListenFunction::Work() { |
320 int result = -1; | 305 int result = -1; |
321 | 306 |
322 Socket* socket = GetSocket(params_->socket_id); | 307 Socket* socket = GetSocket(params_->socket_id); |
323 if (socket) { | 308 if (socket) { |
324 SocketPermission::CheckParam param( | 309 SocketPermission::CheckParam param( |
325 SocketPermissionRequest::TCP_LISTEN, params_->address, params_->port); | 310 SocketPermissionRequest::TCP_LISTEN, params_->address, params_->port); |
326 if (!PermissionsData::CheckAPIPermissionWithParam( | 311 if (!PermissionsData::CheckAPIPermissionWithParam( |
327 GetExtension(), | 312 GetExtension(), APIPermission::kSocket, ¶m)) { |
328 APIPermission::kSocket, | |
329 ¶m)) { | |
330 error_ = kPermissionError; | 313 error_ = kPermissionError; |
331 SetResult(new base::FundamentalValue(result)); | 314 SetResult(new base::FundamentalValue(result)); |
332 return; | 315 return; |
333 } | 316 } |
334 | 317 |
335 result = socket->Listen( | 318 result = |
336 params_->address, | 319 socket->Listen(params_->address, |
337 params_->port, | 320 params_->port, |
338 params_->backlog.get() ? *params_->backlog.get() : 5, | 321 params_->backlog.get() ? *params_->backlog.get() : 5, |
339 &error_); | 322 &error_); |
340 } else { | 323 } else { |
341 error_ = kSocketNotFoundError; | 324 error_ = kSocketNotFoundError; |
342 } | 325 } |
343 | 326 |
344 SetResult(new base::FundamentalValue(result)); | 327 SetResult(new base::FundamentalValue(result)); |
345 } | 328 } |
346 | 329 |
347 SocketAcceptFunction::SocketAcceptFunction() {} | 330 SocketAcceptFunction::SocketAcceptFunction() {} |
348 | 331 |
349 SocketAcceptFunction::~SocketAcceptFunction() {} | 332 SocketAcceptFunction::~SocketAcceptFunction() {} |
350 | 333 |
351 bool SocketAcceptFunction::Prepare() { | 334 bool SocketAcceptFunction::Prepare() { |
352 params_ = api::socket::Accept::Params::Create(*args_); | 335 params_ = core_api::socket::Accept::Params::Create(*args_); |
353 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 336 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
354 return true; | 337 return true; |
355 } | 338 } |
356 | 339 |
357 void SocketAcceptFunction::AsyncWorkStart() { | 340 void SocketAcceptFunction::AsyncWorkStart() { |
358 Socket* socket = GetSocket(params_->socket_id); | 341 Socket* socket = GetSocket(params_->socket_id); |
359 if (socket) { | 342 if (socket) { |
360 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this)); | 343 socket->Accept(base::Bind(&SocketAcceptFunction::OnAccept, this)); |
361 } else { | 344 } else { |
362 error_ = kSocketNotFoundError; | 345 error_ = kSocketNotFoundError; |
363 OnAccept(-1, NULL); | 346 OnAccept(-1, NULL); |
364 } | 347 } |
365 } | 348 } |
366 | 349 |
367 void SocketAcceptFunction::OnAccept(int result_code, | 350 void SocketAcceptFunction::OnAccept(int result_code, |
368 net::TCPClientSocket *socket) { | 351 net::TCPClientSocket* socket) { |
369 base::DictionaryValue* result = new base::DictionaryValue(); | 352 base::DictionaryValue* result = new base::DictionaryValue(); |
370 result->SetInteger(kResultCodeKey, result_code); | 353 result->SetInteger(kResultCodeKey, result_code); |
371 if (socket) { | 354 if (socket) { |
372 Socket *client_socket = new TCPSocket(socket, extension_id(), true); | 355 Socket* client_socket = new TCPSocket(socket, extension_id(), true); |
373 result->SetInteger(kSocketIdKey, AddSocket(client_socket)); | 356 result->SetInteger(kSocketIdKey, AddSocket(client_socket)); |
374 } | 357 } |
375 SetResult(result); | 358 SetResult(result); |
376 | 359 |
377 AsyncWorkCompleted(); | 360 AsyncWorkCompleted(); |
378 } | 361 } |
379 | 362 |
380 SocketReadFunction::SocketReadFunction() {} | 363 SocketReadFunction::SocketReadFunction() {} |
381 | 364 |
382 SocketReadFunction::~SocketReadFunction() {} | 365 SocketReadFunction::~SocketReadFunction() {} |
383 | 366 |
384 bool SocketReadFunction::Prepare() { | 367 bool SocketReadFunction::Prepare() { |
385 params_ = api::socket::Read::Params::Create(*args_); | 368 params_ = core_api::socket::Read::Params::Create(*args_); |
386 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 369 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
387 return true; | 370 return true; |
388 } | 371 } |
389 | 372 |
390 void SocketReadFunction::AsyncWorkStart() { | 373 void SocketReadFunction::AsyncWorkStart() { |
391 Socket* socket = GetSocket(params_->socket_id); | 374 Socket* socket = GetSocket(params_->socket_id); |
392 if (!socket) { | 375 if (!socket) { |
393 error_ = kSocketNotFoundError; | 376 error_ = kSocketNotFoundError; |
394 OnCompleted(-1, NULL); | 377 OnCompleted(-1, NULL); |
395 return; | 378 return; |
(...skipping 13 matching lines...) Expand all Loading... |
409 bytes_read)); | 392 bytes_read)); |
410 } else { | 393 } else { |
411 result->Set(kDataKey, new base::BinaryValue()); | 394 result->Set(kDataKey, new base::BinaryValue()); |
412 } | 395 } |
413 SetResult(result); | 396 SetResult(result); |
414 | 397 |
415 AsyncWorkCompleted(); | 398 AsyncWorkCompleted(); |
416 } | 399 } |
417 | 400 |
418 SocketWriteFunction::SocketWriteFunction() | 401 SocketWriteFunction::SocketWriteFunction() |
419 : socket_id_(0), | 402 : socket_id_(0), io_buffer_(NULL), io_buffer_size_(0) {} |
420 io_buffer_(NULL), | |
421 io_buffer_size_(0) { | |
422 } | |
423 | 403 |
424 SocketWriteFunction::~SocketWriteFunction() {} | 404 SocketWriteFunction::~SocketWriteFunction() {} |
425 | 405 |
426 bool SocketWriteFunction::Prepare() { | 406 bool SocketWriteFunction::Prepare() { |
427 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 407 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
428 base::BinaryValue *data = NULL; | 408 base::BinaryValue* data = NULL; |
429 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); | 409 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); |
430 | 410 |
431 io_buffer_size_ = data->GetSize(); | 411 io_buffer_size_ = data->GetSize(); |
432 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); | 412 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); |
433 return true; | 413 return true; |
434 } | 414 } |
435 | 415 |
436 void SocketWriteFunction::AsyncWorkStart() { | 416 void SocketWriteFunction::AsyncWorkStart() { |
437 Socket* socket = GetSocket(socket_id_); | 417 Socket* socket = GetSocket(socket_id_); |
438 | 418 |
439 if (!socket) { | 419 if (!socket) { |
440 error_ = kSocketNotFoundError; | 420 error_ = kSocketNotFoundError; |
441 OnCompleted(-1); | 421 OnCompleted(-1); |
442 return; | 422 return; |
443 } | 423 } |
444 | 424 |
445 socket->Write(io_buffer_, io_buffer_size_, | 425 socket->Write(io_buffer_, |
| 426 io_buffer_size_, |
446 base::Bind(&SocketWriteFunction::OnCompleted, this)); | 427 base::Bind(&SocketWriteFunction::OnCompleted, this)); |
447 } | 428 } |
448 | 429 |
449 void SocketWriteFunction::OnCompleted(int bytes_written) { | 430 void SocketWriteFunction::OnCompleted(int bytes_written) { |
450 base::DictionaryValue* result = new base::DictionaryValue(); | 431 base::DictionaryValue* result = new base::DictionaryValue(); |
451 result->SetInteger(kBytesWrittenKey, bytes_written); | 432 result->SetInteger(kBytesWrittenKey, bytes_written); |
452 SetResult(result); | 433 SetResult(result); |
453 | 434 |
454 AsyncWorkCompleted(); | 435 AsyncWorkCompleted(); |
455 } | 436 } |
456 | 437 |
457 SocketRecvFromFunction::SocketRecvFromFunction() {} | 438 SocketRecvFromFunction::SocketRecvFromFunction() {} |
458 | 439 |
459 SocketRecvFromFunction::~SocketRecvFromFunction() {} | 440 SocketRecvFromFunction::~SocketRecvFromFunction() {} |
460 | 441 |
461 bool SocketRecvFromFunction::Prepare() { | 442 bool SocketRecvFromFunction::Prepare() { |
462 params_ = api::socket::RecvFrom::Params::Create(*args_); | 443 params_ = core_api::socket::RecvFrom::Params::Create(*args_); |
463 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 444 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
464 return true; | 445 return true; |
465 } | 446 } |
466 | 447 |
467 void SocketRecvFromFunction::AsyncWorkStart() { | 448 void SocketRecvFromFunction::AsyncWorkStart() { |
468 Socket* socket = GetSocket(params_->socket_id); | 449 Socket* socket = GetSocket(params_->socket_id); |
469 if (!socket) { | 450 if (!socket) { |
470 error_ = kSocketNotFoundError; | 451 error_ = kSocketNotFoundError; |
471 OnCompleted(-1, NULL, std::string(), 0); | 452 OnCompleted(-1, NULL, std::string(), 0); |
472 return; | 453 return; |
(...skipping 21 matching lines...) Expand all Loading... |
494 SetResult(result); | 475 SetResult(result); |
495 | 476 |
496 AsyncWorkCompleted(); | 477 AsyncWorkCompleted(); |
497 } | 478 } |
498 | 479 |
499 SocketSendToFunction::SocketSendToFunction() | 480 SocketSendToFunction::SocketSendToFunction() |
500 : socket_id_(0), | 481 : socket_id_(0), |
501 io_buffer_(NULL), | 482 io_buffer_(NULL), |
502 io_buffer_size_(0), | 483 io_buffer_size_(0), |
503 port_(0), | 484 port_(0), |
504 socket_(NULL) { | 485 socket_(NULL) {} |
505 } | |
506 | 486 |
507 SocketSendToFunction::~SocketSendToFunction() {} | 487 SocketSendToFunction::~SocketSendToFunction() {} |
508 | 488 |
509 bool SocketSendToFunction::Prepare() { | 489 bool SocketSendToFunction::Prepare() { |
510 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 490 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
511 base::BinaryValue *data = NULL; | 491 base::BinaryValue* data = NULL; |
512 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); | 492 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); |
513 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); | 493 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); |
514 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port_)); | 494 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port_)); |
515 | 495 |
516 io_buffer_size_ = data->GetSize(); | 496 io_buffer_size_ = data->GetSize(); |
517 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); | 497 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); |
518 return true; | 498 return true; |
519 } | 499 } |
520 | 500 |
521 void SocketSendToFunction::AsyncWorkStart() { | 501 void SocketSendToFunction::AsyncWorkStart() { |
522 socket_ = GetSocket(socket_id_); | 502 socket_ = GetSocket(socket_id_); |
523 if (!socket_) { | 503 if (!socket_) { |
524 error_ = kSocketNotFoundError; | 504 error_ = kSocketNotFoundError; |
525 SetResult(new base::FundamentalValue(-1)); | 505 SetResult(new base::FundamentalValue(-1)); |
526 AsyncWorkCompleted(); | 506 AsyncWorkCompleted(); |
527 return; | 507 return; |
528 } | 508 } |
529 | 509 |
530 if (socket_->GetSocketType() == Socket::TYPE_UDP) { | 510 if (socket_->GetSocketType() == Socket::TYPE_UDP) { |
531 SocketPermission::CheckParam param(SocketPermissionRequest::UDP_SEND_TO, | 511 SocketPermission::CheckParam param( |
532 hostname_, port_); | 512 SocketPermissionRequest::UDP_SEND_TO, hostname_, port_); |
533 if (!PermissionsData::CheckAPIPermissionWithParam( | 513 if (!PermissionsData::CheckAPIPermissionWithParam( |
534 GetExtension(), | 514 GetExtension(), APIPermission::kSocket, ¶m)) { |
535 APIPermission::kSocket, | |
536 ¶m)) { | |
537 error_ = kPermissionError; | 515 error_ = kPermissionError; |
538 SetResult(new base::FundamentalValue(-1)); | 516 SetResult(new base::FundamentalValue(-1)); |
539 AsyncWorkCompleted(); | 517 AsyncWorkCompleted(); |
540 return; | 518 return; |
541 } | 519 } |
542 } | 520 } |
543 | 521 |
544 StartDnsLookup(hostname_); | 522 StartDnsLookup(hostname_); |
545 } | 523 } |
546 | 524 |
547 void SocketSendToFunction::AfterDnsLookup(int lookup_result) { | 525 void SocketSendToFunction::AfterDnsLookup(int lookup_result) { |
548 if (lookup_result == net::OK) { | 526 if (lookup_result == net::OK) { |
549 StartSendTo(); | 527 StartSendTo(); |
550 } else { | 528 } else { |
551 SetResult(new base::FundamentalValue(lookup_result)); | 529 SetResult(new base::FundamentalValue(lookup_result)); |
552 AsyncWorkCompleted(); | 530 AsyncWorkCompleted(); |
553 } | 531 } |
554 } | 532 } |
555 | 533 |
556 void SocketSendToFunction::StartSendTo() { | 534 void SocketSendToFunction::StartSendTo() { |
557 socket_->SendTo(io_buffer_, io_buffer_size_, resolved_address_, port_, | 535 socket_->SendTo(io_buffer_, |
| 536 io_buffer_size_, |
| 537 resolved_address_, |
| 538 port_, |
558 base::Bind(&SocketSendToFunction::OnCompleted, this)); | 539 base::Bind(&SocketSendToFunction::OnCompleted, this)); |
559 } | 540 } |
560 | 541 |
561 void SocketSendToFunction::OnCompleted(int bytes_written) { | 542 void SocketSendToFunction::OnCompleted(int bytes_written) { |
562 base::DictionaryValue* result = new base::DictionaryValue(); | 543 base::DictionaryValue* result = new base::DictionaryValue(); |
563 result->SetInteger(kBytesWrittenKey, bytes_written); | 544 result->SetInteger(kBytesWrittenKey, bytes_written); |
564 SetResult(result); | 545 SetResult(result); |
565 | 546 |
566 AsyncWorkCompleted(); | 547 AsyncWorkCompleted(); |
567 } | 548 } |
568 | 549 |
569 SocketSetKeepAliveFunction::SocketSetKeepAliveFunction() {} | 550 SocketSetKeepAliveFunction::SocketSetKeepAliveFunction() {} |
570 | 551 |
571 SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() {} | 552 SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() {} |
572 | 553 |
573 bool SocketSetKeepAliveFunction::Prepare() { | 554 bool SocketSetKeepAliveFunction::Prepare() { |
574 params_ = api::socket::SetKeepAlive::Params::Create(*args_); | 555 params_ = core_api::socket::SetKeepAlive::Params::Create(*args_); |
575 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 556 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
576 return true; | 557 return true; |
577 } | 558 } |
578 | 559 |
579 void SocketSetKeepAliveFunction::Work() { | 560 void SocketSetKeepAliveFunction::Work() { |
580 bool result = false; | 561 bool result = false; |
581 Socket* socket = GetSocket(params_->socket_id); | 562 Socket* socket = GetSocket(params_->socket_id); |
582 if (socket) { | 563 if (socket) { |
583 int delay = 0; | 564 int delay = 0; |
584 if (params_->delay.get()) | 565 if (params_->delay.get()) |
585 delay = *params_->delay; | 566 delay = *params_->delay; |
586 result = socket->SetKeepAlive(params_->enable, delay); | 567 result = socket->SetKeepAlive(params_->enable, delay); |
587 } else { | 568 } else { |
588 error_ = kSocketNotFoundError; | 569 error_ = kSocketNotFoundError; |
589 } | 570 } |
590 SetResult(new base::FundamentalValue(result)); | 571 SetResult(new base::FundamentalValue(result)); |
591 } | 572 } |
592 | 573 |
593 SocketSetNoDelayFunction::SocketSetNoDelayFunction() {} | 574 SocketSetNoDelayFunction::SocketSetNoDelayFunction() {} |
594 | 575 |
595 SocketSetNoDelayFunction::~SocketSetNoDelayFunction() {} | 576 SocketSetNoDelayFunction::~SocketSetNoDelayFunction() {} |
596 | 577 |
597 bool SocketSetNoDelayFunction::Prepare() { | 578 bool SocketSetNoDelayFunction::Prepare() { |
598 params_ = api::socket::SetNoDelay::Params::Create(*args_); | 579 params_ = core_api::socket::SetNoDelay::Params::Create(*args_); |
599 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 580 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
600 return true; | 581 return true; |
601 } | 582 } |
602 | 583 |
603 void SocketSetNoDelayFunction::Work() { | 584 void SocketSetNoDelayFunction::Work() { |
604 bool result = false; | 585 bool result = false; |
605 Socket* socket = GetSocket(params_->socket_id); | 586 Socket* socket = GetSocket(params_->socket_id); |
606 if (socket) | 587 if (socket) |
607 result = socket->SetNoDelay(params_->no_delay); | 588 result = socket->SetNoDelay(params_->no_delay); |
608 else | 589 else |
609 error_ = kSocketNotFoundError; | 590 error_ = kSocketNotFoundError; |
610 SetResult(new base::FundamentalValue(result)); | 591 SetResult(new base::FundamentalValue(result)); |
611 } | 592 } |
612 | 593 |
613 SocketGetInfoFunction::SocketGetInfoFunction() {} | 594 SocketGetInfoFunction::SocketGetInfoFunction() {} |
614 | 595 |
615 SocketGetInfoFunction::~SocketGetInfoFunction() {} | 596 SocketGetInfoFunction::~SocketGetInfoFunction() {} |
616 | 597 |
617 bool SocketGetInfoFunction::Prepare() { | 598 bool SocketGetInfoFunction::Prepare() { |
618 params_ = api::socket::GetInfo::Params::Create(*args_); | 599 params_ = core_api::socket::GetInfo::Params::Create(*args_); |
619 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 600 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
620 return true; | 601 return true; |
621 } | 602 } |
622 | 603 |
623 void SocketGetInfoFunction::Work() { | 604 void SocketGetInfoFunction::Work() { |
624 Socket* socket = GetSocket(params_->socket_id); | 605 Socket* socket = GetSocket(params_->socket_id); |
625 if (!socket) { | 606 if (!socket) { |
626 error_ = kSocketNotFoundError; | 607 error_ = kSocketNotFoundError; |
627 return; | 608 return; |
628 } | 609 } |
629 | 610 |
630 api::socket::SocketInfo info; | 611 core_api::socket::SocketInfo info; |
631 // This represents what we know about the socket, and does not call through | 612 // This represents what we know about the socket, and does not call through |
632 // to the system. | 613 // to the system. |
633 if (socket->GetSocketType() == Socket::TYPE_TCP) | 614 if (socket->GetSocketType() == Socket::TYPE_TCP) |
634 info.socket_type = extensions::api::socket::SOCKET_TYPE_TCP; | 615 info.socket_type = extensions::core_api::socket::SOCKET_TYPE_TCP; |
635 else | 616 else |
636 info.socket_type = extensions::api::socket::SOCKET_TYPE_UDP; | 617 info.socket_type = extensions::core_api::socket::SOCKET_TYPE_UDP; |
637 info.connected = socket->IsConnected(); | 618 info.connected = socket->IsConnected(); |
638 | 619 |
639 // Grab the peer address as known by the OS. This and the call below will | 620 // Grab the peer address as known by the OS. This and the call below will |
640 // always succeed while the socket is connected, even if the socket has | 621 // always succeed while the socket is connected, even if the socket has |
641 // been remotely closed by the peer; only reading the socket will reveal | 622 // been remotely closed by the peer; only reading the socket will reveal |
642 // that it should be closed locally. | 623 // that it should be closed locally. |
643 net::IPEndPoint peerAddress; | 624 net::IPEndPoint peerAddress; |
644 if (socket->GetPeerAddress(&peerAddress)) { | 625 if (socket->GetPeerAddress(&peerAddress)) { |
645 info.peer_address.reset( | 626 info.peer_address.reset(new std::string(peerAddress.ToStringWithoutPort())); |
646 new std::string(peerAddress.ToStringWithoutPort())); | |
647 info.peer_port.reset(new int(peerAddress.port())); | 627 info.peer_port.reset(new int(peerAddress.port())); |
648 } | 628 } |
649 | 629 |
650 // Grab the local address as known by the OS. | 630 // Grab the local address as known by the OS. |
651 net::IPEndPoint localAddress; | 631 net::IPEndPoint localAddress; |
652 if (socket->GetLocalAddress(&localAddress)) { | 632 if (socket->GetLocalAddress(&localAddress)) { |
653 info.local_address.reset( | 633 info.local_address.reset( |
654 new std::string(localAddress.ToStringWithoutPort())); | 634 new std::string(localAddress.ToStringWithoutPort())); |
655 info.local_port.reset(new int(localAddress.port())); | 635 info.local_port.reset(new int(localAddress.port())); |
656 } | 636 } |
657 | 637 |
658 SetResult(info.ToValue().release()); | 638 SetResult(info.ToValue().release()); |
659 } | 639 } |
660 | 640 |
661 bool SocketGetNetworkListFunction::RunImpl() { | 641 bool SocketGetNetworkListFunction::RunImpl() { |
662 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, | 642 content::BrowserThread::PostTask( |
| 643 content::BrowserThread::FILE, |
| 644 FROM_HERE, |
663 base::Bind(&SocketGetNetworkListFunction::GetNetworkListOnFileThread, | 645 base::Bind(&SocketGetNetworkListFunction::GetNetworkListOnFileThread, |
664 this)); | 646 this)); |
665 return true; | 647 return true; |
666 } | 648 } |
667 | 649 |
668 void SocketGetNetworkListFunction::GetNetworkListOnFileThread() { | 650 void SocketGetNetworkListFunction::GetNetworkListOnFileThread() { |
669 net::NetworkInterfaceList interface_list; | 651 net::NetworkInterfaceList interface_list; |
670 if (GetNetworkList(&interface_list, | 652 if (GetNetworkList(&interface_list, |
671 net::INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)) { | 653 net::INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)) { |
672 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 654 content::BrowserThread::PostTask( |
| 655 content::BrowserThread::UI, |
| 656 FROM_HERE, |
673 base::Bind(&SocketGetNetworkListFunction::SendResponseOnUIThread, | 657 base::Bind(&SocketGetNetworkListFunction::SendResponseOnUIThread, |
674 this, interface_list)); | 658 this, |
| 659 interface_list)); |
675 return; | 660 return; |
676 } | 661 } |
677 | 662 |
678 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 663 content::BrowserThread::PostTask( |
| 664 content::BrowserThread::UI, |
| 665 FROM_HERE, |
679 base::Bind(&SocketGetNetworkListFunction::HandleGetNetworkListError, | 666 base::Bind(&SocketGetNetworkListFunction::HandleGetNetworkListError, |
680 this)); | 667 this)); |
681 } | 668 } |
682 | 669 |
683 void SocketGetNetworkListFunction::HandleGetNetworkListError() { | 670 void SocketGetNetworkListFunction::HandleGetNetworkListError() { |
684 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 671 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
685 error_ = kNetworkListError; | 672 error_ = kNetworkListError; |
686 SendResponse(false); | 673 SendResponse(false); |
687 } | 674 } |
688 | 675 |
689 void SocketGetNetworkListFunction::SendResponseOnUIThread( | 676 void SocketGetNetworkListFunction::SendResponseOnUIThread( |
690 const net::NetworkInterfaceList& interface_list) { | 677 const net::NetworkInterfaceList& interface_list) { |
691 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 678 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
692 | 679 |
693 std::vector<linked_ptr<api::socket::NetworkInterface> > create_arg; | 680 std::vector<linked_ptr<core_api::socket::NetworkInterface> > create_arg; |
694 create_arg.reserve(interface_list.size()); | 681 create_arg.reserve(interface_list.size()); |
695 for (net::NetworkInterfaceList::const_iterator i = interface_list.begin(); | 682 for (net::NetworkInterfaceList::const_iterator i = interface_list.begin(); |
696 i != interface_list.end(); ++i) { | 683 i != interface_list.end(); |
697 linked_ptr<api::socket::NetworkInterface> info = | 684 ++i) { |
698 make_linked_ptr(new api::socket::NetworkInterface); | 685 linked_ptr<core_api::socket::NetworkInterface> info = |
| 686 make_linked_ptr(new core_api::socket::NetworkInterface); |
699 info->name = i->name; | 687 info->name = i->name; |
700 info->address = net::IPAddressToString(i->address); | 688 info->address = net::IPAddressToString(i->address); |
701 info->prefix_length = i->network_prefix; | 689 info->prefix_length = i->network_prefix; |
702 create_arg.push_back(info); | 690 create_arg.push_back(info); |
703 } | 691 } |
704 | 692 |
705 results_ = api::socket::GetNetworkList::Results::Create(create_arg); | 693 results_ = core_api::socket::GetNetworkList::Results::Create(create_arg); |
706 SendResponse(true); | 694 SendResponse(true); |
707 } | 695 } |
708 | 696 |
709 SocketJoinGroupFunction::SocketJoinGroupFunction() {} | 697 SocketJoinGroupFunction::SocketJoinGroupFunction() {} |
710 | 698 |
711 SocketJoinGroupFunction::~SocketJoinGroupFunction() {} | 699 SocketJoinGroupFunction::~SocketJoinGroupFunction() {} |
712 | 700 |
713 bool SocketJoinGroupFunction::Prepare() { | 701 bool SocketJoinGroupFunction::Prepare() { |
714 params_ = api::socket::JoinGroup::Params::Create(*args_); | 702 params_ = core_api::socket::JoinGroup::Params::Create(*args_); |
715 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 703 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
716 return true; | 704 return true; |
717 } | 705 } |
718 | 706 |
719 void SocketJoinGroupFunction::Work() { | 707 void SocketJoinGroupFunction::Work() { |
720 int result = -1; | 708 int result = -1; |
721 Socket* socket = GetSocket(params_->socket_id); | 709 Socket* socket = GetSocket(params_->socket_id); |
722 if (!socket) { | 710 if (!socket) { |
723 error_ = kSocketNotFoundError; | 711 error_ = kSocketNotFoundError; |
724 SetResult(new base::FundamentalValue(result)); | 712 SetResult(new base::FundamentalValue(result)); |
(...skipping 23 matching lines...) Expand all Loading... |
748 error_ = net::ErrorToString(result); | 736 error_ = net::ErrorToString(result); |
749 } | 737 } |
750 SetResult(new base::FundamentalValue(result)); | 738 SetResult(new base::FundamentalValue(result)); |
751 } | 739 } |
752 | 740 |
753 SocketLeaveGroupFunction::SocketLeaveGroupFunction() {} | 741 SocketLeaveGroupFunction::SocketLeaveGroupFunction() {} |
754 | 742 |
755 SocketLeaveGroupFunction::~SocketLeaveGroupFunction() {} | 743 SocketLeaveGroupFunction::~SocketLeaveGroupFunction() {} |
756 | 744 |
757 bool SocketLeaveGroupFunction::Prepare() { | 745 bool SocketLeaveGroupFunction::Prepare() { |
758 params_ = api::socket::LeaveGroup::Params::Create(*args_); | 746 params_ = core_api::socket::LeaveGroup::Params::Create(*args_); |
759 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 747 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
760 return true; | 748 return true; |
761 } | 749 } |
762 | 750 |
763 void SocketLeaveGroupFunction::Work() { | 751 void SocketLeaveGroupFunction::Work() { |
764 int result = -1; | 752 int result = -1; |
765 Socket* socket = GetSocket(params_->socket_id); | 753 Socket* socket = GetSocket(params_->socket_id); |
766 | 754 |
767 if (!socket) { | 755 if (!socket) { |
768 error_ = kSocketNotFoundError; | 756 error_ = kSocketNotFoundError; |
769 SetResult(new base::FundamentalValue(result)); | 757 SetResult(new base::FundamentalValue(result)); |
770 return; | 758 return; |
771 } | 759 } |
772 | 760 |
773 if (socket->GetSocketType() != Socket::TYPE_UDP) { | 761 if (socket->GetSocketType() != Socket::TYPE_UDP) { |
774 error_ = kMulticastSocketTypeError; | 762 error_ = kMulticastSocketTypeError; |
775 SetResult(new base::FundamentalValue(result)); | 763 SetResult(new base::FundamentalValue(result)); |
776 return; | 764 return; |
777 } | 765 } |
778 | 766 |
779 SocketPermission::CheckParam param( | 767 SocketPermission::CheckParam param( |
780 SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP, | 768 SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP, |
781 kWildcardAddress, | 769 kWildcardAddress, |
782 kWildcardPort); | 770 kWildcardPort); |
783 if (!PermissionsData::CheckAPIPermissionWithParam(GetExtension(), | 771 if (!PermissionsData::CheckAPIPermissionWithParam( |
784 APIPermission::kSocket, | 772 GetExtension(), APIPermission::kSocket, ¶m)) { |
785 ¶m)) { | |
786 error_ = kPermissionError; | 773 error_ = kPermissionError; |
787 SetResult(new base::FundamentalValue(result)); | 774 SetResult(new base::FundamentalValue(result)); |
788 return; | 775 return; |
789 } | 776 } |
790 | 777 |
791 result = static_cast<UDPSocket*>(socket)->LeaveGroup(params_->address); | 778 result = static_cast<UDPSocket*>(socket)->LeaveGroup(params_->address); |
792 if (result != 0) | 779 if (result != 0) |
793 error_ = net::ErrorToString(result); | 780 error_ = net::ErrorToString(result); |
794 SetResult(new base::FundamentalValue(result)); | 781 SetResult(new base::FundamentalValue(result)); |
795 } | 782 } |
796 | 783 |
797 SocketSetMulticastTimeToLiveFunction::SocketSetMulticastTimeToLiveFunction() {} | 784 SocketSetMulticastTimeToLiveFunction::SocketSetMulticastTimeToLiveFunction() {} |
798 | 785 |
799 SocketSetMulticastTimeToLiveFunction::~SocketSetMulticastTimeToLiveFunction() {} | 786 SocketSetMulticastTimeToLiveFunction::~SocketSetMulticastTimeToLiveFunction() {} |
800 | 787 |
801 bool SocketSetMulticastTimeToLiveFunction::Prepare() { | 788 bool SocketSetMulticastTimeToLiveFunction::Prepare() { |
802 params_ = api::socket::SetMulticastTimeToLive::Params::Create(*args_); | 789 params_ = core_api::socket::SetMulticastTimeToLive::Params::Create(*args_); |
803 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 790 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
804 return true; | 791 return true; |
805 } | 792 } |
806 void SocketSetMulticastTimeToLiveFunction::Work() { | 793 void SocketSetMulticastTimeToLiveFunction::Work() { |
807 int result = -1; | 794 int result = -1; |
808 Socket* socket = GetSocket(params_->socket_id); | 795 Socket* socket = GetSocket(params_->socket_id); |
809 if (!socket) { | 796 if (!socket) { |
810 error_ = kSocketNotFoundError; | 797 error_ = kSocketNotFoundError; |
811 SetResult(new base::FundamentalValue(result)); | 798 SetResult(new base::FundamentalValue(result)); |
812 return; | 799 return; |
813 } | 800 } |
814 | 801 |
815 if (socket->GetSocketType() != Socket::TYPE_UDP) { | 802 if (socket->GetSocketType() != Socket::TYPE_UDP) { |
816 error_ = kMulticastSocketTypeError; | 803 error_ = kMulticastSocketTypeError; |
817 SetResult(new base::FundamentalValue(result)); | 804 SetResult(new base::FundamentalValue(result)); |
818 return; | 805 return; |
819 } | 806 } |
820 | 807 |
821 result = static_cast<UDPSocket*>(socket)->SetMulticastTimeToLive( | 808 result = |
822 params_->ttl); | 809 static_cast<UDPSocket*>(socket)->SetMulticastTimeToLive(params_->ttl); |
823 if (result != 0) | 810 if (result != 0) |
824 error_ = net::ErrorToString(result); | 811 error_ = net::ErrorToString(result); |
825 SetResult(new base::FundamentalValue(result)); | 812 SetResult(new base::FundamentalValue(result)); |
826 } | 813 } |
827 | 814 |
828 SocketSetMulticastLoopbackModeFunction:: | 815 SocketSetMulticastLoopbackModeFunction:: |
829 SocketSetMulticastLoopbackModeFunction() {} | 816 SocketSetMulticastLoopbackModeFunction() {} |
830 | 817 |
831 SocketSetMulticastLoopbackModeFunction:: | 818 SocketSetMulticastLoopbackModeFunction:: |
832 ~SocketSetMulticastLoopbackModeFunction() {} | 819 ~SocketSetMulticastLoopbackModeFunction() {} |
833 | 820 |
834 bool SocketSetMulticastLoopbackModeFunction::Prepare() { | 821 bool SocketSetMulticastLoopbackModeFunction::Prepare() { |
835 params_ = api::socket::SetMulticastLoopbackMode::Params::Create(*args_); | 822 params_ = core_api::socket::SetMulticastLoopbackMode::Params::Create(*args_); |
836 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 823 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
837 return true; | 824 return true; |
838 } | 825 } |
839 | 826 |
840 void SocketSetMulticastLoopbackModeFunction::Work() { | 827 void SocketSetMulticastLoopbackModeFunction::Work() { |
841 int result = -1; | 828 int result = -1; |
842 Socket* socket = GetSocket(params_->socket_id); | 829 Socket* socket = GetSocket(params_->socket_id); |
843 if (!socket) { | 830 if (!socket) { |
844 error_ = kSocketNotFoundError; | 831 error_ = kSocketNotFoundError; |
845 SetResult(new base::FundamentalValue(result)); | 832 SetResult(new base::FundamentalValue(result)); |
846 return; | 833 return; |
847 } | 834 } |
848 | 835 |
849 if (socket->GetSocketType() != Socket::TYPE_UDP) { | 836 if (socket->GetSocketType() != Socket::TYPE_UDP) { |
850 error_ = kMulticastSocketTypeError; | 837 error_ = kMulticastSocketTypeError; |
851 SetResult(new base::FundamentalValue(result)); | 838 SetResult(new base::FundamentalValue(result)); |
852 return; | 839 return; |
853 } | 840 } |
854 | 841 |
855 result = static_cast<UDPSocket*>(socket)-> | 842 result = static_cast<UDPSocket*>(socket) |
856 SetMulticastLoopbackMode(params_->enabled); | 843 ->SetMulticastLoopbackMode(params_->enabled); |
857 if (result != 0) | 844 if (result != 0) |
858 error_ = net::ErrorToString(result); | 845 error_ = net::ErrorToString(result); |
859 SetResult(new base::FundamentalValue(result)); | 846 SetResult(new base::FundamentalValue(result)); |
860 } | 847 } |
861 | 848 |
862 SocketGetJoinedGroupsFunction::SocketGetJoinedGroupsFunction() {} | 849 SocketGetJoinedGroupsFunction::SocketGetJoinedGroupsFunction() {} |
863 | 850 |
864 SocketGetJoinedGroupsFunction::~SocketGetJoinedGroupsFunction() {} | 851 SocketGetJoinedGroupsFunction::~SocketGetJoinedGroupsFunction() {} |
865 | 852 |
866 bool SocketGetJoinedGroupsFunction::Prepare() { | 853 bool SocketGetJoinedGroupsFunction::Prepare() { |
867 params_ = api::socket::GetJoinedGroups::Params::Create(*args_); | 854 params_ = core_api::socket::GetJoinedGroups::Params::Create(*args_); |
868 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 855 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
869 return true; | 856 return true; |
870 } | 857 } |
871 | 858 |
872 void SocketGetJoinedGroupsFunction::Work() { | 859 void SocketGetJoinedGroupsFunction::Work() { |
873 int result = -1; | 860 int result = -1; |
874 Socket* socket = GetSocket(params_->socket_id); | 861 Socket* socket = GetSocket(params_->socket_id); |
875 if (!socket) { | 862 if (!socket) { |
876 error_ = kSocketNotFoundError; | 863 error_ = kSocketNotFoundError; |
877 SetResult(new base::FundamentalValue(result)); | 864 SetResult(new base::FundamentalValue(result)); |
878 return; | 865 return; |
879 } | 866 } |
880 | 867 |
881 if (socket->GetSocketType() != Socket::TYPE_UDP) { | 868 if (socket->GetSocketType() != Socket::TYPE_UDP) { |
882 error_ = kMulticastSocketTypeError; | 869 error_ = kMulticastSocketTypeError; |
883 SetResult(new base::FundamentalValue(result)); | 870 SetResult(new base::FundamentalValue(result)); |
884 return; | 871 return; |
885 } | 872 } |
886 | 873 |
887 SocketPermission::CheckParam param( | 874 SocketPermission::CheckParam param( |
888 SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP, | 875 SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP, |
889 kWildcardAddress, | 876 kWildcardAddress, |
890 kWildcardPort); | 877 kWildcardPort); |
891 if (!PermissionsData::CheckAPIPermissionWithParam( | 878 if (!PermissionsData::CheckAPIPermissionWithParam( |
892 GetExtension(), | 879 GetExtension(), APIPermission::kSocket, ¶m)) { |
893 APIPermission::kSocket, | |
894 ¶m)) { | |
895 error_ = kPermissionError; | 880 error_ = kPermissionError; |
896 SetResult(new base::FundamentalValue(result)); | 881 SetResult(new base::FundamentalValue(result)); |
897 return; | 882 return; |
898 } | 883 } |
899 | 884 |
900 base::ListValue* values = new base::ListValue(); | 885 base::ListValue* values = new base::ListValue(); |
901 values->AppendStrings((std::vector<std::string>&) | 886 values->AppendStrings((std::vector<std::string>&)static_cast<UDPSocket*>( |
902 static_cast<UDPSocket*>(socket)->GetJoinedGroups()); | 887 socket)->GetJoinedGroups()); |
903 SetResult(values); | 888 SetResult(values); |
904 } | 889 } |
905 | 890 |
906 } // namespace extensions | 891 } // namespace extensions |
OLD | NEW |