| 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 "chrome/browser/extensions/api/socket/socket_api.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "chrome/common/extensions/permissions/socket_permission.h" | 8 #include "chrome/common/extensions/permissions/socket_permission.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" | 10 #include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h" |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 src_id_ = ExtractSrcId(options.get()); | 123 src_id_ = ExtractSrcId(options.get()); |
| 124 event_notifier_ = CreateEventNotifier(src_id_); | 124 event_notifier_ = CreateEventNotifier(src_id_); |
| 125 } | 125 } |
| 126 | 126 |
| 127 return true; | 127 return true; |
| 128 } | 128 } |
| 129 | 129 |
| 130 void SocketCreateFunction::Work() { | 130 void SocketCreateFunction::Work() { |
| 131 Socket* socket = NULL; | 131 Socket* socket = NULL; |
| 132 if (socket_type_ == kSocketTypeTCP) { | 132 if (socket_type_ == kSocketTypeTCP) { |
| 133 socket = new TCPSocket(event_notifier_); | 133 socket = new TCPSocket(extension_->id(), event_notifier_); |
| 134 } else if (socket_type_== kSocketTypeUDP) { | 134 } else if (socket_type_== kSocketTypeUDP) { |
| 135 socket = new UDPSocket(event_notifier_); | 135 socket = new UDPSocket(extension_->id(), event_notifier_); |
| 136 } | 136 } |
| 137 DCHECK(socket); | 137 DCHECK(socket); |
| 138 | 138 |
| 139 DictionaryValue* result = new DictionaryValue(); | 139 DictionaryValue* result = new DictionaryValue(); |
| 140 result->SetInteger(kSocketIdKey, manager_->Add(socket)); | 140 result->SetInteger(kSocketIdKey, manager_->Add(socket)); |
| 141 SetResult(result); | 141 SetResult(result); |
| 142 } | 142 } |
| 143 | 143 |
| 144 bool SocketDestroyFunction::Prepare() { | 144 bool SocketDestroyFunction::Prepare() { |
| 145 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 145 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 146 return true; | 146 return true; |
| 147 } | 147 } |
| 148 | 148 |
| 149 void SocketDestroyFunction::Work() { | 149 void SocketDestroyFunction::Work() { |
| 150 manager_->Remove(socket_id_); | 150 manager_->Remove(extension_->id(), socket_id_); |
| 151 } | 151 } |
| 152 | 152 |
| 153 SocketConnectFunction::SocketConnectFunction() | 153 SocketConnectFunction::SocketConnectFunction() |
| 154 : socket_id_(0), | 154 : socket_id_(0), |
| 155 port_(0) { | 155 port_(0) { |
| 156 } | 156 } |
| 157 | 157 |
| 158 SocketConnectFunction::~SocketConnectFunction() { | 158 SocketConnectFunction::~SocketConnectFunction() { |
| 159 } | 159 } |
| 160 | 160 |
| 161 bool SocketConnectFunction::Prepare() { | 161 bool SocketConnectFunction::Prepare() { |
| 162 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 162 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 163 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); | 163 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); |
| 164 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); | 164 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); |
| 165 return true; | 165 return true; |
| 166 } | 166 } |
| 167 | 167 |
| 168 void SocketConnectFunction::AsyncWorkStart() { | 168 void SocketConnectFunction::AsyncWorkStart() { |
| 169 socket_ = manager_->Get(socket_id_); | 169 socket_ = manager_->Get(extension_->id(), socket_id_); |
| 170 if (!socket_) { | 170 if (!socket_) { |
| 171 error_ = kSocketNotFoundError; | 171 error_ = kSocketNotFoundError; |
| 172 SetResult(Value::CreateIntegerValue(-1)); | 172 SetResult(Value::CreateIntegerValue(-1)); |
| 173 AsyncWorkCompleted(); | 173 AsyncWorkCompleted(); |
| 174 return; | 174 return; |
| 175 } | 175 } |
| 176 | 176 |
| 177 SocketPermissionData::OperationType operation_type; | 177 SocketPermissionData::OperationType operation_type; |
| 178 switch (socket_->GetSocketType()) { | 178 switch (socket_->GetSocketType()) { |
| 179 case Socket::TYPE_TCP: | 179 case Socket::TYPE_TCP: |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 SetResult(Value::CreateIntegerValue(result)); | 218 SetResult(Value::CreateIntegerValue(result)); |
| 219 AsyncWorkCompleted(); | 219 AsyncWorkCompleted(); |
| 220 } | 220 } |
| 221 | 221 |
| 222 bool SocketDisconnectFunction::Prepare() { | 222 bool SocketDisconnectFunction::Prepare() { |
| 223 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 223 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 224 return true; | 224 return true; |
| 225 } | 225 } |
| 226 | 226 |
| 227 void SocketDisconnectFunction::Work() { | 227 void SocketDisconnectFunction::Work() { |
| 228 Socket* socket = manager_->Get(socket_id_); | 228 Socket* socket = manager_->Get(extension_->id(), socket_id_); |
| 229 if (socket) | 229 if (socket) |
| 230 socket->Disconnect(); | 230 socket->Disconnect(); |
| 231 else | 231 else |
| 232 error_ = kSocketNotFoundError; | 232 error_ = kSocketNotFoundError; |
| 233 SetResult(Value::CreateNullValue()); | 233 SetResult(Value::CreateNullValue()); |
| 234 } | 234 } |
| 235 | 235 |
| 236 bool SocketBindFunction::Prepare() { | 236 bool SocketBindFunction::Prepare() { |
| 237 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 237 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 238 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_)); | 238 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_)); |
| 239 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); | 239 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); |
| 240 return true; | 240 return true; |
| 241 } | 241 } |
| 242 | 242 |
| 243 void SocketBindFunction::Work() { | 243 void SocketBindFunction::Work() { |
| 244 int result = -1; | 244 int result = -1; |
| 245 Socket* socket = manager_->Get(socket_id_); | 245 Socket* socket = manager_->Get(extension_->id(), socket_id_); |
| 246 | 246 |
| 247 if (!socket) { | 247 if (!socket) { |
| 248 error_ = kSocketNotFoundError; | 248 error_ = kSocketNotFoundError; |
| 249 SetResult(Value::CreateIntegerValue(result)); | 249 SetResult(Value::CreateIntegerValue(result)); |
| 250 return; | 250 return; |
| 251 } | 251 } |
| 252 | 252 |
| 253 if (socket->GetSocketType() == Socket::TYPE_UDP) { | 253 if (socket->GetSocketType() == Socket::TYPE_UDP) { |
| 254 SocketPermission::CheckParam param( | 254 SocketPermission::CheckParam param( |
| 255 SocketPermissionData::UDP_BIND, address_, port_); | 255 SocketPermissionData::UDP_BIND, address_, port_); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 271 | 271 |
| 272 SocketReadFunction::~SocketReadFunction() {} | 272 SocketReadFunction::~SocketReadFunction() {} |
| 273 | 273 |
| 274 bool SocketReadFunction::Prepare() { | 274 bool SocketReadFunction::Prepare() { |
| 275 params_ = api::socket::Read::Params::Create(*args_); | 275 params_ = api::socket::Read::Params::Create(*args_); |
| 276 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 276 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 277 return true; | 277 return true; |
| 278 } | 278 } |
| 279 | 279 |
| 280 void SocketReadFunction::AsyncWorkStart() { | 280 void SocketReadFunction::AsyncWorkStart() { |
| 281 Socket* socket = manager_->Get(params_->socket_id); | 281 Socket* socket = manager_->Get(extension_->id(), params_->socket_id); |
| 282 if (!socket) { | 282 if (!socket) { |
| 283 error_ = kSocketNotFoundError; | 283 error_ = kSocketNotFoundError; |
| 284 OnCompleted(-1, NULL); | 284 OnCompleted(-1, NULL); |
| 285 return; | 285 return; |
| 286 } | 286 } |
| 287 | 287 |
| 288 socket->Read(params_->buffer_size.get() ? *params_->buffer_size.get() : 4096, | 288 socket->Read(params_->buffer_size.get() ? *params_->buffer_size.get() : 4096, |
| 289 base::Bind(&SocketReadFunction::OnCompleted, this)); | 289 base::Bind(&SocketReadFunction::OnCompleted, this)); |
| 290 } | 290 } |
| 291 | 291 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 319 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); | 319 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); |
| 320 base::BinaryValue *data = NULL; | 320 base::BinaryValue *data = NULL; |
| 321 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); | 321 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); |
| 322 | 322 |
| 323 io_buffer_size_ = data->GetSize(); | 323 io_buffer_size_ = data->GetSize(); |
| 324 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); | 324 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); |
| 325 return true; | 325 return true; |
| 326 } | 326 } |
| 327 | 327 |
| 328 void SocketWriteFunction::AsyncWorkStart() { | 328 void SocketWriteFunction::AsyncWorkStart() { |
| 329 Socket* socket = manager_->Get(socket_id_); | 329 Socket* socket = manager_->Get(extension_->id(), socket_id_); |
| 330 | 330 |
| 331 if (!socket) { | 331 if (!socket) { |
| 332 error_ = kSocketNotFoundError; | 332 error_ = kSocketNotFoundError; |
| 333 OnCompleted(-1); | 333 OnCompleted(-1); |
| 334 return; | 334 return; |
| 335 } | 335 } |
| 336 | 336 |
| 337 socket->Write(io_buffer_, io_buffer_size_, | 337 socket->Write(io_buffer_, io_buffer_size_, |
| 338 base::Bind(&SocketWriteFunction::OnCompleted, this)); | 338 base::Bind(&SocketWriteFunction::OnCompleted, this)); |
| 339 } | 339 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 352 | 352 |
| 353 SocketRecvFromFunction::~SocketRecvFromFunction() {} | 353 SocketRecvFromFunction::~SocketRecvFromFunction() {} |
| 354 | 354 |
| 355 bool SocketRecvFromFunction::Prepare() { | 355 bool SocketRecvFromFunction::Prepare() { |
| 356 params_ = api::socket::RecvFrom::Params::Create(*args_); | 356 params_ = api::socket::RecvFrom::Params::Create(*args_); |
| 357 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 357 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 358 return true; | 358 return true; |
| 359 } | 359 } |
| 360 | 360 |
| 361 void SocketRecvFromFunction::AsyncWorkStart() { | 361 void SocketRecvFromFunction::AsyncWorkStart() { |
| 362 Socket* socket = manager_->Get(params_->socket_id); | 362 Socket* socket = manager_->Get(extension_->id(), params_->socket_id); |
| 363 if (!socket) { | 363 if (!socket) { |
| 364 error_ = kSocketNotFoundError; | 364 error_ = kSocketNotFoundError; |
| 365 OnCompleted(-1, NULL, std::string(), 0); | 365 OnCompleted(-1, NULL, std::string(), 0); |
| 366 return; | 366 return; |
| 367 } | 367 } |
| 368 | 368 |
| 369 socket->RecvFrom(params_->buffer_size.get() ? *params_->buffer_size : 4096, | 369 socket->RecvFrom(params_->buffer_size.get() ? *params_->buffer_size : 4096, |
| 370 base::Bind(&SocketRecvFromFunction::OnCompleted, this)); | 370 base::Bind(&SocketRecvFromFunction::OnCompleted, this)); |
| 371 } | 371 } |
| 372 | 372 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); | 407 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); |
| 408 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); | 408 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); |
| 409 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port_)); | 409 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port_)); |
| 410 | 410 |
| 411 io_buffer_size_ = data->GetSize(); | 411 io_buffer_size_ = data->GetSize(); |
| 412 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); | 412 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); |
| 413 return true; | 413 return true; |
| 414 } | 414 } |
| 415 | 415 |
| 416 void SocketSendToFunction::AsyncWorkStart() { | 416 void SocketSendToFunction::AsyncWorkStart() { |
| 417 socket_ = manager_->Get(socket_id_); | 417 socket_ = manager_->Get(extension_->id(), socket_id_); |
| 418 if (!socket_) { | 418 if (!socket_) { |
| 419 error_ = kSocketNotFoundError; | 419 error_ = kSocketNotFoundError; |
| 420 SetResult(Value::CreateIntegerValue(-1)); | 420 SetResult(Value::CreateIntegerValue(-1)); |
| 421 AsyncWorkCompleted(); | 421 AsyncWorkCompleted(); |
| 422 return; | 422 return; |
| 423 } | 423 } |
| 424 | 424 |
| 425 if (socket_->GetSocketType() == Socket::TYPE_UDP) { | 425 if (socket_->GetSocketType() == Socket::TYPE_UDP) { |
| 426 SocketPermission::CheckParam param(SocketPermissionData::UDP_SEND_TO, | 426 SocketPermission::CheckParam param(SocketPermissionData::UDP_SEND_TO, |
| 427 hostname_, port_); | 427 hostname_, port_); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() {} | 466 SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() {} |
| 467 | 467 |
| 468 bool SocketSetKeepAliveFunction::Prepare() { | 468 bool SocketSetKeepAliveFunction::Prepare() { |
| 469 params_ = api::socket::SetKeepAlive::Params::Create(*args_); | 469 params_ = api::socket::SetKeepAlive::Params::Create(*args_); |
| 470 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 470 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 471 return true; | 471 return true; |
| 472 } | 472 } |
| 473 | 473 |
| 474 void SocketSetKeepAliveFunction::Work() { | 474 void SocketSetKeepAliveFunction::Work() { |
| 475 bool result = false; | 475 bool result = false; |
| 476 Socket* socket = manager_->Get(params_->socket_id); | 476 Socket* socket = manager_->Get(extension_->id(), params_->socket_id); |
| 477 if (socket) { | 477 if (socket) { |
| 478 int delay = 0; | 478 int delay = 0; |
| 479 if (params_->delay.get()) | 479 if (params_->delay.get()) |
| 480 delay = *params_->delay; | 480 delay = *params_->delay; |
| 481 result = socket->SetKeepAlive(params_->enable, delay); | 481 result = socket->SetKeepAlive(params_->enable, delay); |
| 482 } else { | 482 } else { |
| 483 error_ = kSocketNotFoundError; | 483 error_ = kSocketNotFoundError; |
| 484 } | 484 } |
| 485 SetResult(Value::CreateBooleanValue(result)); | 485 SetResult(Value::CreateBooleanValue(result)); |
| 486 } | 486 } |
| 487 | 487 |
| 488 SocketSetNoDelayFunction::SocketSetNoDelayFunction() | 488 SocketSetNoDelayFunction::SocketSetNoDelayFunction() |
| 489 : params_(NULL) { | 489 : params_(NULL) { |
| 490 } | 490 } |
| 491 | 491 |
| 492 SocketSetNoDelayFunction::~SocketSetNoDelayFunction() {} | 492 SocketSetNoDelayFunction::~SocketSetNoDelayFunction() {} |
| 493 | 493 |
| 494 bool SocketSetNoDelayFunction::Prepare() { | 494 bool SocketSetNoDelayFunction::Prepare() { |
| 495 params_ = api::socket::SetNoDelay::Params::Create(*args_); | 495 params_ = api::socket::SetNoDelay::Params::Create(*args_); |
| 496 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 496 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 497 return true; | 497 return true; |
| 498 } | 498 } |
| 499 | 499 |
| 500 void SocketSetNoDelayFunction::Work() { | 500 void SocketSetNoDelayFunction::Work() { |
| 501 bool result = false; | 501 bool result = false; |
| 502 Socket* socket = manager_->Get(params_->socket_id); | 502 Socket* socket = manager_->Get(extension_->id(), params_->socket_id); |
| 503 if (socket) | 503 if (socket) |
| 504 result = socket->SetNoDelay(params_->no_delay); | 504 result = socket->SetNoDelay(params_->no_delay); |
| 505 else | 505 else |
| 506 error_ = kSocketNotFoundError; | 506 error_ = kSocketNotFoundError; |
| 507 SetResult(Value::CreateBooleanValue(result)); | 507 SetResult(Value::CreateBooleanValue(result)); |
| 508 } | 508 } |
| 509 | 509 |
| 510 SocketGetInfoFunction::SocketGetInfoFunction() | 510 SocketGetInfoFunction::SocketGetInfoFunction() |
| 511 : params_(NULL) {} | 511 : params_(NULL) {} |
| 512 | 512 |
| 513 SocketGetInfoFunction::~SocketGetInfoFunction() {} | 513 SocketGetInfoFunction::~SocketGetInfoFunction() {} |
| 514 | 514 |
| 515 bool SocketGetInfoFunction::Prepare() { | 515 bool SocketGetInfoFunction::Prepare() { |
| 516 params_ = api::socket::GetInfo::Params::Create(*args_); | 516 params_ = api::socket::GetInfo::Params::Create(*args_); |
| 517 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 517 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 518 return true; | 518 return true; |
| 519 } | 519 } |
| 520 | 520 |
| 521 void SocketGetInfoFunction::Work() { | 521 void SocketGetInfoFunction::Work() { |
| 522 api::socket::SocketInfo info; | 522 api::socket::SocketInfo info; |
| 523 Socket* socket = manager_->Get(params_->socket_id); | 523 Socket* socket = manager_->Get(extension_->id(), params_->socket_id); |
| 524 if (socket) { | 524 if (socket) { |
| 525 // This represents what we know about the socket, and does not call through | 525 // This represents what we know about the socket, and does not call through |
| 526 // to the system. | 526 // to the system. |
| 527 switch (socket->GetSocketType()) { | 527 switch (socket->GetSocketType()) { |
| 528 case Socket::TYPE_TCP: | 528 case Socket::TYPE_TCP: |
| 529 info.socket_type = kTCPOption; | 529 info.socket_type = kTCPOption; |
| 530 break; | 530 break; |
| 531 case Socket::TYPE_UDP: | 531 case Socket::TYPE_UDP: |
| 532 info.socket_type = kUDPOption; | 532 info.socket_type = kUDPOption; |
| 533 break; | 533 break; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 556 new std::string(localAddress.ToStringWithoutPort())); | 556 new std::string(localAddress.ToStringWithoutPort())); |
| 557 info.local_port.reset(new int(localAddress.port())); | 557 info.local_port.reset(new int(localAddress.port())); |
| 558 } | 558 } |
| 559 } else { | 559 } else { |
| 560 error_ = kSocketNotFoundError; | 560 error_ = kSocketNotFoundError; |
| 561 } | 561 } |
| 562 SetResult(info.ToValue().release()); | 562 SetResult(info.ToValue().release()); |
| 563 } | 563 } |
| 564 | 564 |
| 565 } // namespace extensions | 565 } // namespace extensions |
| OLD | NEW |