OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/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" |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 } | 177 } |
178 | 178 |
179 bool SocketDestroyFunction::Prepare() { | 179 bool SocketDestroyFunction::Prepare() { |
180 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 180 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
181 return true; | 181 return true; |
182 } | 182 } |
183 | 183 |
184 void SocketDestroyFunction::Work() { RemoveSocket(socket_id_); } | 184 void SocketDestroyFunction::Work() { RemoveSocket(socket_id_); } |
185 | 185 |
186 SocketConnectFunction::SocketConnectFunction() | 186 SocketConnectFunction::SocketConnectFunction() |
187 : socket_id_(0), hostname_(), port_(0), socket_(NULL) {} | 187 : socket_id_(0), hostname_(), port_(0) { |
| 188 } |
188 | 189 |
189 SocketConnectFunction::~SocketConnectFunction() {} | 190 SocketConnectFunction::~SocketConnectFunction() {} |
190 | 191 |
191 bool SocketConnectFunction::Prepare() { | 192 bool SocketConnectFunction::Prepare() { |
192 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 193 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
193 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); | 194 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); |
194 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); | 195 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); |
195 return true; | 196 return true; |
196 } | 197 } |
197 | 198 |
198 void SocketConnectFunction::AsyncWorkStart() { | 199 void SocketConnectFunction::AsyncWorkStart() { |
199 socket_ = GetSocket(socket_id_); | 200 Socket* socket = GetSocket(socket_id_); |
200 if (!socket_) { | 201 if (!socket) { |
201 error_ = kSocketNotFoundError; | 202 error_ = kSocketNotFoundError; |
202 SetResult(new base::FundamentalValue(-1)); | 203 SetResult(new base::FundamentalValue(-1)); |
203 AsyncWorkCompleted(); | 204 AsyncWorkCompleted(); |
204 return; | 205 return; |
205 } | 206 } |
206 | 207 |
207 socket_->set_hostname(hostname_); | 208 socket->set_hostname(hostname_); |
208 | 209 |
209 SocketPermissionRequest::OperationType operation_type; | 210 SocketPermissionRequest::OperationType operation_type; |
210 switch (socket_->GetSocketType()) { | 211 switch (socket->GetSocketType()) { |
211 case Socket::TYPE_TCP: | 212 case Socket::TYPE_TCP: |
212 operation_type = SocketPermissionRequest::TCP_CONNECT; | 213 operation_type = SocketPermissionRequest::TCP_CONNECT; |
213 break; | 214 break; |
214 case Socket::TYPE_UDP: | 215 case Socket::TYPE_UDP: |
215 operation_type = SocketPermissionRequest::UDP_SEND_TO; | 216 operation_type = SocketPermissionRequest::UDP_SEND_TO; |
216 break; | 217 break; |
217 default: | 218 default: |
218 NOTREACHED() << "Unknown socket type."; | 219 NOTREACHED() << "Unknown socket type."; |
219 operation_type = SocketPermissionRequest::NONE; | 220 operation_type = SocketPermissionRequest::NONE; |
220 break; | 221 break; |
(...skipping 14 matching lines...) Expand all Loading... |
235 void SocketConnectFunction::AfterDnsLookup(int lookup_result) { | 236 void SocketConnectFunction::AfterDnsLookup(int lookup_result) { |
236 if (lookup_result == net::OK) { | 237 if (lookup_result == net::OK) { |
237 StartConnect(); | 238 StartConnect(); |
238 } else { | 239 } else { |
239 SetResult(new base::FundamentalValue(lookup_result)); | 240 SetResult(new base::FundamentalValue(lookup_result)); |
240 AsyncWorkCompleted(); | 241 AsyncWorkCompleted(); |
241 } | 242 } |
242 } | 243 } |
243 | 244 |
244 void SocketConnectFunction::StartConnect() { | 245 void SocketConnectFunction::StartConnect() { |
245 socket_->Connect(resolved_address_, | 246 Socket* socket = GetSocket(socket_id_); |
246 port_, | 247 if (!socket) { |
247 base::Bind(&SocketConnectFunction::OnConnect, this)); | 248 error_ = kSocketNotFoundError; |
| 249 SetResult(new base::FundamentalValue(-1)); |
| 250 AsyncWorkCompleted(); |
| 251 return; |
| 252 } |
| 253 |
| 254 socket->Connect(resolved_address_, |
| 255 port_, |
| 256 base::Bind(&SocketConnectFunction::OnConnect, this)); |
248 } | 257 } |
249 | 258 |
250 void SocketConnectFunction::OnConnect(int result) { | 259 void SocketConnectFunction::OnConnect(int result) { |
251 SetResult(new base::FundamentalValue(result)); | 260 SetResult(new base::FundamentalValue(result)); |
252 AsyncWorkCompleted(); | 261 AsyncWorkCompleted(); |
253 } | 262 } |
254 | 263 |
255 bool SocketDisconnectFunction::Prepare() { | 264 bool SocketDisconnectFunction::Prepare() { |
256 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 265 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
257 return true; | 266 return true; |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 result->Set(kDataKey, new base::BinaryValue()); | 491 result->Set(kDataKey, new base::BinaryValue()); |
483 } | 492 } |
484 result->SetString(kAddressKey, address); | 493 result->SetString(kAddressKey, address); |
485 result->SetInteger(kPortKey, port); | 494 result->SetInteger(kPortKey, port); |
486 SetResult(result); | 495 SetResult(result); |
487 | 496 |
488 AsyncWorkCompleted(); | 497 AsyncWorkCompleted(); |
489 } | 498 } |
490 | 499 |
491 SocketSendToFunction::SocketSendToFunction() | 500 SocketSendToFunction::SocketSendToFunction() |
492 : socket_id_(0), | 501 : socket_id_(0), io_buffer_(NULL), io_buffer_size_(0), port_(0) { |
493 io_buffer_(NULL), | 502 } |
494 io_buffer_size_(0), | |
495 port_(0), | |
496 socket_(NULL) {} | |
497 | 503 |
498 SocketSendToFunction::~SocketSendToFunction() {} | 504 SocketSendToFunction::~SocketSendToFunction() {} |
499 | 505 |
500 bool SocketSendToFunction::Prepare() { | 506 bool SocketSendToFunction::Prepare() { |
501 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 507 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
502 base::BinaryValue* data = NULL; | 508 base::BinaryValue* data = NULL; |
503 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); | 509 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); |
504 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); | 510 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); |
505 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port_)); | 511 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port_)); |
506 | 512 |
507 io_buffer_size_ = data->GetSize(); | 513 io_buffer_size_ = data->GetSize(); |
508 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); | 514 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); |
509 return true; | 515 return true; |
510 } | 516 } |
511 | 517 |
512 void SocketSendToFunction::AsyncWorkStart() { | 518 void SocketSendToFunction::AsyncWorkStart() { |
513 socket_ = GetSocket(socket_id_); | 519 Socket* socket = GetSocket(socket_id_); |
514 if (!socket_) { | 520 if (!socket) { |
515 error_ = kSocketNotFoundError; | 521 error_ = kSocketNotFoundError; |
516 SetResult(new base::FundamentalValue(-1)); | 522 SetResult(new base::FundamentalValue(-1)); |
517 AsyncWorkCompleted(); | 523 AsyncWorkCompleted(); |
518 return; | 524 return; |
519 } | 525 } |
520 | 526 |
521 if (socket_->GetSocketType() == Socket::TYPE_UDP) { | 527 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
522 SocketPermission::CheckParam param( | 528 SocketPermission::CheckParam param( |
523 SocketPermissionRequest::UDP_SEND_TO, hostname_, port_); | 529 SocketPermissionRequest::UDP_SEND_TO, hostname_, port_); |
524 if (!extension()->permissions_data()->CheckAPIPermissionWithParam( | 530 if (!extension()->permissions_data()->CheckAPIPermissionWithParam( |
525 APIPermission::kSocket, ¶m)) { | 531 APIPermission::kSocket, ¶m)) { |
526 error_ = kPermissionError; | 532 error_ = kPermissionError; |
527 SetResult(new base::FundamentalValue(-1)); | 533 SetResult(new base::FundamentalValue(-1)); |
528 AsyncWorkCompleted(); | 534 AsyncWorkCompleted(); |
529 return; | 535 return; |
530 } | 536 } |
531 } | 537 } |
532 | 538 |
533 StartDnsLookup(hostname_); | 539 StartDnsLookup(hostname_); |
534 } | 540 } |
535 | 541 |
536 void SocketSendToFunction::AfterDnsLookup(int lookup_result) { | 542 void SocketSendToFunction::AfterDnsLookup(int lookup_result) { |
537 if (lookup_result == net::OK) { | 543 if (lookup_result == net::OK) { |
538 StartSendTo(); | 544 StartSendTo(); |
539 } else { | 545 } else { |
540 SetResult(new base::FundamentalValue(lookup_result)); | 546 SetResult(new base::FundamentalValue(lookup_result)); |
541 AsyncWorkCompleted(); | 547 AsyncWorkCompleted(); |
542 } | 548 } |
543 } | 549 } |
544 | 550 |
545 void SocketSendToFunction::StartSendTo() { | 551 void SocketSendToFunction::StartSendTo() { |
546 socket_->SendTo(io_buffer_, | 552 Socket* socket = GetSocket(socket_id_); |
547 io_buffer_size_, | 553 if (!socket) { |
548 resolved_address_, | 554 error_ = kSocketNotFoundError; |
549 port_, | 555 SetResult(new base::FundamentalValue(-1)); |
550 base::Bind(&SocketSendToFunction::OnCompleted, this)); | 556 AsyncWorkCompleted(); |
| 557 return; |
| 558 } |
| 559 |
| 560 socket->SendTo(io_buffer_, |
| 561 io_buffer_size_, |
| 562 resolved_address_, |
| 563 port_, |
| 564 base::Bind(&SocketSendToFunction::OnCompleted, this)); |
551 } | 565 } |
552 | 566 |
553 void SocketSendToFunction::OnCompleted(int bytes_written) { | 567 void SocketSendToFunction::OnCompleted(int bytes_written) { |
554 base::DictionaryValue* result = new base::DictionaryValue(); | 568 base::DictionaryValue* result = new base::DictionaryValue(); |
555 result->SetInteger(kBytesWrittenKey, bytes_written); | 569 result->SetInteger(kBytesWrittenKey, bytes_written); |
556 SetResult(result); | 570 SetResult(result); |
557 | 571 |
558 AsyncWorkCompleted(); | 572 AsyncWorkCompleted(); |
559 } | 573 } |
560 | 574 |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
965 } else { | 979 } else { |
966 RemoveSocket(params_->socket_id); | 980 RemoveSocket(params_->socket_id); |
967 error_ = net::ErrorToString(result); | 981 error_ = net::ErrorToString(result); |
968 } | 982 } |
969 | 983 |
970 results_ = core_api::socket::Secure::Results::Create(result); | 984 results_ = core_api::socket::Secure::Results::Create(result); |
971 AsyncWorkCompleted(); | 985 AsyncWorkCompleted(); |
972 } | 986 } |
973 | 987 |
974 } // namespace extensions | 988 } // namespace extensions |
OLD | NEW |