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

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

Issue 10919201: Make sure that a given app/extension requests only its own resources. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Improve tests; fix merge conflicts. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 "If this assertion is failing during a test, then it is likely that " 51 "If this assertion is failing during a test, then it is likely that "
52 "TestExtensionSystem is failing to provide an instance of " 52 "TestExtensionSystem is failing to provide an instance of "
53 "ApiResourceManager<Socket>."; 53 "ApiResourceManager<Socket>.";
54 return manager_ != NULL; 54 return manager_ != NULL;
55 } 55 }
56 56
57 bool SocketAsyncApiFunction::Respond() { 57 bool SocketAsyncApiFunction::Respond() {
58 return error_.empty(); 58 return error_.empty();
59 } 59 }
60 60
61 Socket* SocketAsyncApiFunction::GetSocket(int api_resource_id) {
62 return manager_->Get(extension_->id(), api_resource_id);
63 }
64
65 void SocketAsyncApiFunction::RemoveSocket(int api_resource_id) {
66 manager_->Remove(extension_->id(), api_resource_id);
67 }
68
61 SocketExtensionWithDnsLookupFunction::SocketExtensionWithDnsLookupFunction() 69 SocketExtensionWithDnsLookupFunction::SocketExtensionWithDnsLookupFunction()
62 : io_thread_(g_browser_process->io_thread()), 70 : io_thread_(g_browser_process->io_thread()),
63 request_handle_(new net::HostResolver::RequestHandle), 71 request_handle_(new net::HostResolver::RequestHandle),
64 addresses_(new net::AddressList) { 72 addresses_(new net::AddressList) {
65 } 73 }
66 74
67 SocketExtensionWithDnsLookupFunction::~SocketExtensionWithDnsLookupFunction() { 75 SocketExtensionWithDnsLookupFunction::~SocketExtensionWithDnsLookupFunction() {
68 } 76 }
69 77
70 void SocketExtensionWithDnsLookupFunction::StartDnsLookup( 78 void SocketExtensionWithDnsLookupFunction::StartDnsLookup(
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 src_id_ = ExtractSrcId(options.get()); 133 src_id_ = ExtractSrcId(options.get());
126 event_notifier_ = CreateEventNotifier(src_id_); 134 event_notifier_ = CreateEventNotifier(src_id_);
127 } 135 }
128 136
129 return true; 137 return true;
130 } 138 }
131 139
132 void SocketCreateFunction::Work() { 140 void SocketCreateFunction::Work() {
133 Socket* socket = NULL; 141 Socket* socket = NULL;
134 if (socket_type_ == kSocketTypeTCP) { 142 if (socket_type_ == kSocketTypeTCP) {
135 socket = new TCPSocket(event_notifier_); 143 socket = new TCPSocket(extension_->id(), event_notifier_);
136 } else if (socket_type_== kSocketTypeUDP) { 144 } else if (socket_type_== kSocketTypeUDP) {
137 socket = new UDPSocket(event_notifier_); 145 socket = new UDPSocket(extension_->id(), event_notifier_);
138 } 146 }
139 DCHECK(socket); 147 DCHECK(socket);
140 148
141 DictionaryValue* result = new DictionaryValue(); 149 DictionaryValue* result = new DictionaryValue();
142 result->SetInteger(kSocketIdKey, manager_->Add(socket)); 150 result->SetInteger(kSocketIdKey, manager_->Add(socket));
143 SetResult(result); 151 SetResult(result);
144 } 152 }
145 153
146 bool SocketDestroyFunction::Prepare() { 154 bool SocketDestroyFunction::Prepare() {
147 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); 155 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
148 return true; 156 return true;
149 } 157 }
150 158
151 void SocketDestroyFunction::Work() { 159 void SocketDestroyFunction::Work() {
152 manager_->Remove(socket_id_); 160 RemoveSocket(socket_id_);
153 } 161 }
154 162
155 SocketConnectFunction::SocketConnectFunction() 163 SocketConnectFunction::SocketConnectFunction()
156 : socket_id_(0), 164 : socket_id_(0),
157 port_(0) { 165 port_(0) {
158 } 166 }
159 167
160 SocketConnectFunction::~SocketConnectFunction() { 168 SocketConnectFunction::~SocketConnectFunction() {
161 } 169 }
162 170
163 bool SocketConnectFunction::Prepare() { 171 bool SocketConnectFunction::Prepare() {
164 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); 172 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
165 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_)); 173 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &hostname_));
166 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); 174 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_));
167 return true; 175 return true;
168 } 176 }
169 177
170 void SocketConnectFunction::AsyncWorkStart() { 178 void SocketConnectFunction::AsyncWorkStart() {
171 socket_ = manager_->Get(socket_id_); 179 socket_ = GetSocket(socket_id_);
172 if (!socket_) { 180 if (!socket_) {
173 error_ = kSocketNotFoundError; 181 error_ = kSocketNotFoundError;
174 SetResult(Value::CreateIntegerValue(-1)); 182 SetResult(Value::CreateIntegerValue(-1));
175 AsyncWorkCompleted(); 183 AsyncWorkCompleted();
176 return; 184 return;
177 } 185 }
178 186
179 SocketPermissionData::OperationType operation_type; 187 SocketPermissionData::OperationType operation_type;
180 switch (socket_->GetSocketType()) { 188 switch (socket_->GetSocketType()) {
181 case Socket::TYPE_TCP: 189 case Socket::TYPE_TCP:
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 SetResult(Value::CreateIntegerValue(result)); 228 SetResult(Value::CreateIntegerValue(result));
221 AsyncWorkCompleted(); 229 AsyncWorkCompleted();
222 } 230 }
223 231
224 bool SocketDisconnectFunction::Prepare() { 232 bool SocketDisconnectFunction::Prepare() {
225 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); 233 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
226 return true; 234 return true;
227 } 235 }
228 236
229 void SocketDisconnectFunction::Work() { 237 void SocketDisconnectFunction::Work() {
230 Socket* socket = manager_->Get(socket_id_); 238 Socket* socket = GetSocket(socket_id_);
231 if (socket) 239 if (socket)
232 socket->Disconnect(); 240 socket->Disconnect();
233 else 241 else
234 error_ = kSocketNotFoundError; 242 error_ = kSocketNotFoundError;
235 SetResult(Value::CreateNullValue()); 243 SetResult(Value::CreateNullValue());
236 } 244 }
237 245
238 bool SocketBindFunction::Prepare() { 246 bool SocketBindFunction::Prepare() {
239 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); 247 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
240 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_)); 248 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_));
241 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); 249 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_));
242 return true; 250 return true;
243 } 251 }
244 252
245 void SocketBindFunction::Work() { 253 void SocketBindFunction::Work() {
246 int result = -1; 254 int result = -1;
247 Socket* socket = manager_->Get(socket_id_); 255 Socket* socket = GetSocket(socket_id_);
248 256
249 if (!socket) { 257 if (!socket) {
250 error_ = kSocketNotFoundError; 258 error_ = kSocketNotFoundError;
251 SetResult(Value::CreateIntegerValue(result)); 259 SetResult(Value::CreateIntegerValue(result));
252 return; 260 return;
253 } 261 }
254 262
255 if (socket->GetSocketType() == Socket::TYPE_UDP) { 263 if (socket->GetSocketType() == Socket::TYPE_UDP) {
256 SocketPermission::CheckParam param( 264 SocketPermission::CheckParam param(
257 SocketPermissionData::UDP_BIND, address_, port_); 265 SocketPermissionData::UDP_BIND, address_, port_);
(...skipping 15 matching lines...) Expand all
273 281
274 SocketReadFunction::~SocketReadFunction() {} 282 SocketReadFunction::~SocketReadFunction() {}
275 283
276 bool SocketReadFunction::Prepare() { 284 bool SocketReadFunction::Prepare() {
277 params_ = api::socket::Read::Params::Create(*args_); 285 params_ = api::socket::Read::Params::Create(*args_);
278 EXTENSION_FUNCTION_VALIDATE(params_.get()); 286 EXTENSION_FUNCTION_VALIDATE(params_.get());
279 return true; 287 return true;
280 } 288 }
281 289
282 void SocketReadFunction::AsyncWorkStart() { 290 void SocketReadFunction::AsyncWorkStart() {
283 Socket* socket = manager_->Get(params_->socket_id); 291 Socket* socket = GetSocket(params_->socket_id);
284 if (!socket) { 292 if (!socket) {
285 error_ = kSocketNotFoundError; 293 error_ = kSocketNotFoundError;
286 OnCompleted(-1, NULL); 294 OnCompleted(-1, NULL);
287 return; 295 return;
288 } 296 }
289 297
290 socket->Read(params_->buffer_size.get() ? *params_->buffer_size.get() : 4096, 298 socket->Read(params_->buffer_size.get() ? *params_->buffer_size.get() : 4096,
291 base::Bind(&SocketReadFunction::OnCompleted, this)); 299 base::Bind(&SocketReadFunction::OnCompleted, this));
292 } 300 }
293 301
(...skipping 27 matching lines...) Expand all
321 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); 329 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
322 base::BinaryValue *data = NULL; 330 base::BinaryValue *data = NULL;
323 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); 331 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data));
324 332
325 io_buffer_size_ = data->GetSize(); 333 io_buffer_size_ = data->GetSize();
326 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); 334 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer());
327 return true; 335 return true;
328 } 336 }
329 337
330 void SocketWriteFunction::AsyncWorkStart() { 338 void SocketWriteFunction::AsyncWorkStart() {
331 Socket* socket = manager_->Get(socket_id_); 339 Socket* socket = GetSocket(socket_id_);
332 340
333 if (!socket) { 341 if (!socket) {
334 error_ = kSocketNotFoundError; 342 error_ = kSocketNotFoundError;
335 OnCompleted(-1); 343 OnCompleted(-1);
336 return; 344 return;
337 } 345 }
338 346
339 socket->Write(io_buffer_, io_buffer_size_, 347 socket->Write(io_buffer_, io_buffer_size_,
340 base::Bind(&SocketWriteFunction::OnCompleted, this)); 348 base::Bind(&SocketWriteFunction::OnCompleted, this));
341 } 349 }
(...skipping 12 matching lines...) Expand all
354 362
355 SocketRecvFromFunction::~SocketRecvFromFunction() {} 363 SocketRecvFromFunction::~SocketRecvFromFunction() {}
356 364
357 bool SocketRecvFromFunction::Prepare() { 365 bool SocketRecvFromFunction::Prepare() {
358 params_ = api::socket::RecvFrom::Params::Create(*args_); 366 params_ = api::socket::RecvFrom::Params::Create(*args_);
359 EXTENSION_FUNCTION_VALIDATE(params_.get()); 367 EXTENSION_FUNCTION_VALIDATE(params_.get());
360 return true; 368 return true;
361 } 369 }
362 370
363 void SocketRecvFromFunction::AsyncWorkStart() { 371 void SocketRecvFromFunction::AsyncWorkStart() {
364 Socket* socket = manager_->Get(params_->socket_id); 372 Socket* socket = GetSocket(params_->socket_id);
365 if (!socket) { 373 if (!socket) {
366 error_ = kSocketNotFoundError; 374 error_ = kSocketNotFoundError;
367 OnCompleted(-1, NULL, std::string(), 0); 375 OnCompleted(-1, NULL, std::string(), 0);
368 return; 376 return;
369 } 377 }
370 378
371 socket->RecvFrom(params_->buffer_size.get() ? *params_->buffer_size : 4096, 379 socket->RecvFrom(params_->buffer_size.get() ? *params_->buffer_size : 4096,
372 base::Bind(&SocketRecvFromFunction::OnCompleted, this)); 380 base::Bind(&SocketRecvFromFunction::OnCompleted, this));
373 } 381 }
374 382
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data)); 417 EXTENSION_FUNCTION_VALIDATE(args_->GetBinary(1, &data));
410 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_)); 418 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &hostname_));
411 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port_)); 419 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(3, &port_));
412 420
413 io_buffer_size_ = data->GetSize(); 421 io_buffer_size_ = data->GetSize();
414 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer()); 422 io_buffer_ = new net::WrappedIOBuffer(data->GetBuffer());
415 return true; 423 return true;
416 } 424 }
417 425
418 void SocketSendToFunction::AsyncWorkStart() { 426 void SocketSendToFunction::AsyncWorkStart() {
419 socket_ = manager_->Get(socket_id_); 427 socket_ = GetSocket(socket_id_);
420 if (!socket_) { 428 if (!socket_) {
421 error_ = kSocketNotFoundError; 429 error_ = kSocketNotFoundError;
422 SetResult(Value::CreateIntegerValue(-1)); 430 SetResult(Value::CreateIntegerValue(-1));
423 AsyncWorkCompleted(); 431 AsyncWorkCompleted();
424 return; 432 return;
425 } 433 }
426 434
427 if (socket_->GetSocketType() == Socket::TYPE_UDP) { 435 if (socket_->GetSocketType() == Socket::TYPE_UDP) {
428 SocketPermission::CheckParam param(SocketPermissionData::UDP_SEND_TO, 436 SocketPermission::CheckParam param(SocketPermissionData::UDP_SEND_TO,
429 hostname_, port_); 437 hostname_, port_);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() {} 476 SocketSetKeepAliveFunction::~SocketSetKeepAliveFunction() {}
469 477
470 bool SocketSetKeepAliveFunction::Prepare() { 478 bool SocketSetKeepAliveFunction::Prepare() {
471 params_ = api::socket::SetKeepAlive::Params::Create(*args_); 479 params_ = api::socket::SetKeepAlive::Params::Create(*args_);
472 EXTENSION_FUNCTION_VALIDATE(params_.get()); 480 EXTENSION_FUNCTION_VALIDATE(params_.get());
473 return true; 481 return true;
474 } 482 }
475 483
476 void SocketSetKeepAliveFunction::Work() { 484 void SocketSetKeepAliveFunction::Work() {
477 bool result = false; 485 bool result = false;
478 Socket* socket = manager_->Get(params_->socket_id); 486 Socket* socket = GetSocket(params_->socket_id);
479 if (socket) { 487 if (socket) {
480 int delay = 0; 488 int delay = 0;
481 if (params_->delay.get()) 489 if (params_->delay.get())
482 delay = *params_->delay; 490 delay = *params_->delay;
483 result = socket->SetKeepAlive(params_->enable, delay); 491 result = socket->SetKeepAlive(params_->enable, delay);
484 } else { 492 } else {
485 error_ = kSocketNotFoundError; 493 error_ = kSocketNotFoundError;
486 } 494 }
487 SetResult(Value::CreateBooleanValue(result)); 495 SetResult(Value::CreateBooleanValue(result));
488 } 496 }
489 497
490 SocketSetNoDelayFunction::SocketSetNoDelayFunction() 498 SocketSetNoDelayFunction::SocketSetNoDelayFunction()
491 : params_(NULL) { 499 : params_(NULL) {
492 } 500 }
493 501
494 SocketSetNoDelayFunction::~SocketSetNoDelayFunction() {} 502 SocketSetNoDelayFunction::~SocketSetNoDelayFunction() {}
495 503
496 bool SocketSetNoDelayFunction::Prepare() { 504 bool SocketSetNoDelayFunction::Prepare() {
497 params_ = api::socket::SetNoDelay::Params::Create(*args_); 505 params_ = api::socket::SetNoDelay::Params::Create(*args_);
498 EXTENSION_FUNCTION_VALIDATE(params_.get()); 506 EXTENSION_FUNCTION_VALIDATE(params_.get());
499 return true; 507 return true;
500 } 508 }
501 509
502 void SocketSetNoDelayFunction::Work() { 510 void SocketSetNoDelayFunction::Work() {
503 bool result = false; 511 bool result = false;
504 Socket* socket = manager_->Get(params_->socket_id); 512 Socket* socket = GetSocket(params_->socket_id);
505 if (socket) 513 if (socket)
506 result = socket->SetNoDelay(params_->no_delay); 514 result = socket->SetNoDelay(params_->no_delay);
507 else 515 else
508 error_ = kSocketNotFoundError; 516 error_ = kSocketNotFoundError;
509 SetResult(Value::CreateBooleanValue(result)); 517 SetResult(Value::CreateBooleanValue(result));
510 } 518 }
511 519
512 SocketGetInfoFunction::SocketGetInfoFunction() 520 SocketGetInfoFunction::SocketGetInfoFunction()
513 : params_(NULL) {} 521 : params_(NULL) {}
514 522
515 SocketGetInfoFunction::~SocketGetInfoFunction() {} 523 SocketGetInfoFunction::~SocketGetInfoFunction() {}
516 524
517 bool SocketGetInfoFunction::Prepare() { 525 bool SocketGetInfoFunction::Prepare() {
518 params_ = api::socket::GetInfo::Params::Create(*args_); 526 params_ = api::socket::GetInfo::Params::Create(*args_);
519 EXTENSION_FUNCTION_VALIDATE(params_.get()); 527 EXTENSION_FUNCTION_VALIDATE(params_.get());
520 return true; 528 return true;
521 } 529 }
522 530
523 void SocketGetInfoFunction::Work() { 531 void SocketGetInfoFunction::Work() {
524 api::socket::SocketInfo info; 532 api::socket::SocketInfo info;
525 Socket* socket = manager_->Get(params_->socket_id); 533 Socket* socket = GetSocket(params_->socket_id);
526 if (socket) { 534 if (socket) {
527 // This represents what we know about the socket, and does not call through 535 // This represents what we know about the socket, and does not call through
528 // to the system. 536 // to the system.
529 switch (socket->GetSocketType()) { 537 switch (socket->GetSocketType()) {
530 case Socket::TYPE_TCP: 538 case Socket::TYPE_TCP:
531 info.socket_type = kTCPOption; 539 info.socket_type = kTCPOption;
532 break; 540 break;
533 case Socket::TYPE_UDP: 541 case Socket::TYPE_UDP:
534 info.socket_type = kUDPOption; 542 info.socket_type = kUDPOption;
535 break; 543 break;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 info->name = i->name; 612 info->name = i->name;
605 info->address = net::IPAddressToString(i->address); 613 info->address = net::IPAddressToString(i->address);
606 create_arg.push_back(info); 614 create_arg.push_back(info);
607 } 615 }
608 616
609 results_ = api::socket::GetNetworkList::Results::Create(create_arg); 617 results_ = api::socket::GetNetworkList::Results::Create(create_arg);
610 SendResponse(true); 618 SendResponse(true);
611 } 619 }
612 620
613 } // namespace extensions 621 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/socket/socket_api.h ('k') | chrome/browser/extensions/api/socket/tcp_socket.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698