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

Side by Side Diff: chrome/browser/extensions/extension_webrequest_api.cc

Issue 7346024: Get rid of the ProfileId. It was added for ceee. I reverted the original change, since it led to ... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 5 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/extension_webrequest_api.h" 5 #include "chrome/browser/extensions/extension_webrequest_api.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 return Singleton<ExtensionWebRequestEventRouter>::get(); 329 return Singleton<ExtensionWebRequestEventRouter>::get();
330 } 330 }
331 331
332 ExtensionWebRequestEventRouter::ExtensionWebRequestEventRouter() { 332 ExtensionWebRequestEventRouter::ExtensionWebRequestEventRouter() {
333 } 333 }
334 334
335 ExtensionWebRequestEventRouter::~ExtensionWebRequestEventRouter() { 335 ExtensionWebRequestEventRouter::~ExtensionWebRequestEventRouter() {
336 } 336 }
337 337
338 int ExtensionWebRequestEventRouter::OnBeforeRequest( 338 int ExtensionWebRequestEventRouter::OnBeforeRequest(
339 ProfileId profile_id, 339 void* profile,
340 ExtensionInfoMap* extension_info_map, 340 ExtensionInfoMap* extension_info_map,
341 net::URLRequest* request, 341 net::URLRequest* request,
342 net::CompletionCallback* callback, 342 net::CompletionCallback* callback,
343 GURL* new_url) { 343 GURL* new_url) {
344 // TODO(jochen): Figure out what to do with events from the system context. 344 // TODO(jochen): Figure out what to do with events from the system context.
345 if (profile_id == Profile::kInvalidProfileId) 345 if (!profile)
346 return net::OK; 346 return net::OK;
347 347
348 if (!HasWebRequestScheme(request->url())) 348 if (!HasWebRequestScheme(request->url()))
349 return net::OK; 349 return net::OK;
350 350
351 // If this is an HTTP request, keep track of it. HTTP-specific events only 351 // If this is an HTTP request, keep track of it. HTTP-specific events only
352 // have the request ID, so we'll need to look up the URLRequest from that. 352 // have the request ID, so we'll need to look up the URLRequest from that.
353 // We need to do this even if no extension subscribes to OnBeforeRequest to 353 // We need to do this even if no extension subscribes to OnBeforeRequest to
354 // guarantee that |http_requests_| is populated if an extension subscribes 354 // guarantee that |http_requests_| is populated if an extension subscribes
355 // to OnBeforeSendHeaders or OnRequestSent. 355 // to OnBeforeSendHeaders or OnRequestSent.
356 if (request->url().SchemeIs(chrome::kHttpScheme) || 356 if (request->url().SchemeIs(chrome::kHttpScheme) ||
357 request->url().SchemeIs(chrome::kHttpsScheme)) { 357 request->url().SchemeIs(chrome::kHttpsScheme)) {
358 http_requests_[request->identifier()] = request; 358 http_requests_[request->identifier()] = request;
359 } 359 }
360 360
361 int tab_id = -1; 361 int tab_id = -1;
362 int window_id = -1; 362 int window_id = -1;
363 ResourceType::Type resource_type = ResourceType::LAST_TYPE; 363 ResourceType::Type resource_type = ResourceType::LAST_TYPE;
364 ExtractRequestInfo(request, &tab_id, &window_id, &resource_type); 364 ExtractRequestInfo(request, &tab_id, &window_id, &resource_type);
365 365
366 int extra_info_spec = 0; 366 int extra_info_spec = 0;
367 std::vector<const EventListener*> listeners = 367 std::vector<const EventListener*> listeners =
368 GetMatchingListeners(profile_id, extension_info_map, 368 GetMatchingListeners(profile, extension_info_map, keys::kOnBeforeRequest,
369 keys::kOnBeforeRequest, request->url(), 369 request->url(), tab_id, window_id, resource_type,
370 tab_id, window_id, resource_type, &extra_info_spec); 370 &extra_info_spec);
371 if (listeners.empty()) 371 if (listeners.empty())
372 return net::OK; 372 return net::OK;
373 373
374 if (GetAndSetSignaled(request->identifier(), kOnBeforeRequest)) 374 if (GetAndSetSignaled(request->identifier(), kOnBeforeRequest))
375 return net::OK; 375 return net::OK;
376 376
377 ListValue args; 377 ListValue args;
378 DictionaryValue* dict = new DictionaryValue(); 378 DictionaryValue* dict = new DictionaryValue();
379 dict->SetString(keys::kRequestIdKey, 379 dict->SetString(keys::kRequestIdKey,
380 base::Uint64ToString(request->identifier())); 380 base::Uint64ToString(request->identifier()));
381 dict->SetString(keys::kUrlKey, request->url().spec()); 381 dict->SetString(keys::kUrlKey, request->url().spec());
382 dict->SetString(keys::kMethodKey, request->method()); 382 dict->SetString(keys::kMethodKey, request->method());
383 dict->SetInteger(keys::kTabIdKey, tab_id); 383 dict->SetInteger(keys::kTabIdKey, tab_id);
384 dict->SetString(keys::kTypeKey, ResourceTypeToString(resource_type)); 384 dict->SetString(keys::kTypeKey, ResourceTypeToString(resource_type));
385 dict->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000); 385 dict->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000);
386 args.Append(dict); 386 args.Append(dict);
387 387
388 if (DispatchEvent(profile_id, request, listeners, args)) { 388 if (DispatchEvent(profile, request, listeners, args)) {
389 blocked_requests_[request->identifier()].event = kOnBeforeRequest; 389 blocked_requests_[request->identifier()].event = kOnBeforeRequest;
390 blocked_requests_[request->identifier()].callback = callback; 390 blocked_requests_[request->identifier()].callback = callback;
391 blocked_requests_[request->identifier()].new_url = new_url; 391 blocked_requests_[request->identifier()].new_url = new_url;
392 return net::ERR_IO_PENDING; 392 return net::ERR_IO_PENDING;
393 } 393 }
394 return net::OK; 394 return net::OK;
395 } 395 }
396 396
397 int ExtensionWebRequestEventRouter::OnBeforeSendHeaders( 397 int ExtensionWebRequestEventRouter::OnBeforeSendHeaders(
398 ProfileId profile_id, 398 void* profile,
399 ExtensionInfoMap* extension_info_map, 399 ExtensionInfoMap* extension_info_map,
400 net::URLRequest* request, 400 net::URLRequest* request,
401 net::CompletionCallback* callback, 401 net::CompletionCallback* callback,
402 net::HttpRequestHeaders* headers) { 402 net::HttpRequestHeaders* headers) {
403 // TODO(jochen): Figure out what to do with events from the system context. 403 // TODO(jochen): Figure out what to do with events from the system context.
404 if (profile_id == Profile::kInvalidProfileId) 404 if (!profile)
405 return net::OK; 405 return net::OK;
406 406
407 if (!HasWebRequestScheme(request->url())) 407 if (!HasWebRequestScheme(request->url()))
408 return net::OK; 408 return net::OK;
409 409
410 if (GetAndSetSignaled(request->identifier(), kOnBeforeSendHeaders)) 410 if (GetAndSetSignaled(request->identifier(), kOnBeforeSendHeaders))
411 return net::OK; 411 return net::OK;
412 412
413 int extra_info_spec = 0; 413 int extra_info_spec = 0;
414 std::vector<const EventListener*> listeners = 414 std::vector<const EventListener*> listeners =
415 GetMatchingListeners(profile_id, extension_info_map, 415 GetMatchingListeners(profile, extension_info_map,
416 keys::kOnBeforeSendHeaders, request, 416 keys::kOnBeforeSendHeaders, request,
417 &extra_info_spec); 417 &extra_info_spec);
418 if (listeners.empty()) 418 if (listeners.empty())
419 return net::OK; 419 return net::OK;
420 420
421 ListValue args; 421 ListValue args;
422 DictionaryValue* dict = new DictionaryValue(); 422 DictionaryValue* dict = new DictionaryValue();
423 dict->SetString(keys::kRequestIdKey, 423 dict->SetString(keys::kRequestIdKey,
424 base::Uint64ToString(request->identifier())); 424 base::Uint64ToString(request->identifier()));
425 dict->SetString(keys::kUrlKey, request->url().spec()); 425 dict->SetString(keys::kUrlKey, request->url().spec());
426 dict->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000); 426 dict->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000);
427 427
428 if (extra_info_spec & ExtraInfoSpec::REQUEST_HEADERS) 428 if (extra_info_spec & ExtraInfoSpec::REQUEST_HEADERS)
429 dict->Set(keys::kRequestHeadersKey, GetRequestHeadersList(headers)); 429 dict->Set(keys::kRequestHeadersKey, GetRequestHeadersList(headers));
430 // TODO(battre): implement request line. 430 // TODO(battre): implement request line.
431 431
432 args.Append(dict); 432 args.Append(dict);
433 433
434 if (DispatchEvent(profile_id, request, listeners, args)) { 434 if (DispatchEvent(profile, request, listeners, args)) {
435 blocked_requests_[request->identifier()].event = kOnBeforeSendHeaders; 435 blocked_requests_[request->identifier()].event = kOnBeforeSendHeaders;
436 blocked_requests_[request->identifier()].callback = callback; 436 blocked_requests_[request->identifier()].callback = callback;
437 blocked_requests_[request->identifier()].request_headers = headers; 437 blocked_requests_[request->identifier()].request_headers = headers;
438 return net::ERR_IO_PENDING; 438 return net::ERR_IO_PENDING;
439 } 439 }
440 return net::OK; 440 return net::OK;
441 } 441 }
442 442
443 void ExtensionWebRequestEventRouter::OnRequestSent( 443 void ExtensionWebRequestEventRouter::OnRequestSent(
444 ProfileId profile_id, 444 void* profile,
445 ExtensionInfoMap* extension_info_map, 445 ExtensionInfoMap* extension_info_map,
446 uint64 request_id, 446 uint64 request_id,
447 const net::HostPortPair& socket_address, 447 const net::HostPortPair& socket_address,
448 const net::HttpRequestHeaders& headers) { 448 const net::HttpRequestHeaders& headers) {
449 if (profile_id == Profile::kInvalidProfileId) 449 if (!profile)
450 return; 450 return;
451 451
452 base::Time time(base::Time::Now()); 452 base::Time time(base::Time::Now());
453 453
454 HttpRequestMap::iterator iter = http_requests_.find(request_id); 454 HttpRequestMap::iterator iter = http_requests_.find(request_id);
455 if (iter == http_requests_.end()) 455 if (iter == http_requests_.end())
456 return; 456 return;
457 457
458 net::URLRequest* request = iter->second; 458 net::URLRequest* request = iter->second;
459 459
460 if (!HasWebRequestScheme(request->url())) 460 if (!HasWebRequestScheme(request->url()))
461 return; 461 return;
462 462
463 if (GetAndSetSignaled(request->identifier(), kOnRequestSent)) 463 if (GetAndSetSignaled(request->identifier(), kOnRequestSent))
464 return; 464 return;
465 465
466 ClearSignaled(request->identifier(), kOnBeforeRedirect); 466 ClearSignaled(request->identifier(), kOnBeforeRedirect);
467 467
468 int extra_info_spec = 0; 468 int extra_info_spec = 0;
469 std::vector<const EventListener*> listeners = 469 std::vector<const EventListener*> listeners =
470 GetMatchingListeners(profile_id, extension_info_map, 470 GetMatchingListeners(profile, extension_info_map,
471 keys::kOnRequestSent, request, &extra_info_spec); 471 keys::kOnRequestSent, request, &extra_info_spec);
472 if (listeners.empty()) 472 if (listeners.empty())
473 return; 473 return;
474 474
475 ListValue args; 475 ListValue args;
476 DictionaryValue* dict = new DictionaryValue(); 476 DictionaryValue* dict = new DictionaryValue();
477 dict->SetString(keys::kRequestIdKey, 477 dict->SetString(keys::kRequestIdKey,
478 base::Uint64ToString(request->identifier())); 478 base::Uint64ToString(request->identifier()));
479 dict->SetString(keys::kUrlKey, request->url().spec()); 479 dict->SetString(keys::kUrlKey, request->url().spec());
480 dict->SetString(keys::kIpKey, socket_address.host()); 480 dict->SetString(keys::kIpKey, socket_address.host());
481 dict->SetDouble(keys::kTimeStampKey, time.ToDoubleT() * 1000); 481 dict->SetDouble(keys::kTimeStampKey, time.ToDoubleT() * 1000);
482 if (extra_info_spec & ExtraInfoSpec::REQUEST_HEADERS) 482 if (extra_info_spec & ExtraInfoSpec::REQUEST_HEADERS)
483 dict->Set(keys::kRequestHeadersKey, GetRequestHeadersList(&headers)); 483 dict->Set(keys::kRequestHeadersKey, GetRequestHeadersList(&headers));
484 // TODO(battre): support "request line". 484 // TODO(battre): support "request line".
485 args.Append(dict); 485 args.Append(dict);
486 486
487 DispatchEvent(profile_id, request, listeners, args); 487 DispatchEvent(profile, request, listeners, args);
488 } 488 }
489 489
490 void ExtensionWebRequestEventRouter::OnBeforeRedirect( 490 void ExtensionWebRequestEventRouter::OnBeforeRedirect(
491 ProfileId profile_id, 491 void* profile,
492 ExtensionInfoMap* extension_info_map, 492 ExtensionInfoMap* extension_info_map,
493 net::URLRequest* request, 493 net::URLRequest* request,
494 const GURL& new_location) { 494 const GURL& new_location) {
495 if (profile_id == Profile::kInvalidProfileId) 495 if (!profile)
496 return; 496 return;
497 497
498 if (!HasWebRequestScheme(request->url())) 498 if (!HasWebRequestScheme(request->url()))
499 return; 499 return;
500 500
501 base::Time time(base::Time::Now()); 501 base::Time time(base::Time::Now());
502 502
503 if (GetAndSetSignaled(request->identifier(), kOnBeforeRedirect)) 503 if (GetAndSetSignaled(request->identifier(), kOnBeforeRedirect))
504 return; 504 return;
505 505
506 ClearSignaled(request->identifier(), kOnBeforeRequest); 506 ClearSignaled(request->identifier(), kOnBeforeRequest);
507 ClearSignaled(request->identifier(), kOnBeforeSendHeaders); 507 ClearSignaled(request->identifier(), kOnBeforeSendHeaders);
508 ClearSignaled(request->identifier(), kOnRequestSent); 508 ClearSignaled(request->identifier(), kOnRequestSent);
509 509
510 int extra_info_spec = 0; 510 int extra_info_spec = 0;
511 std::vector<const EventListener*> listeners = 511 std::vector<const EventListener*> listeners =
512 GetMatchingListeners(profile_id, extension_info_map, 512 GetMatchingListeners(profile, extension_info_map,
513 keys::kOnBeforeRedirect, request, &extra_info_spec); 513 keys::kOnBeforeRedirect, request, &extra_info_spec);
514 if (listeners.empty()) 514 if (listeners.empty())
515 return; 515 return;
516 516
517 int http_status_code = request->GetResponseCode(); 517 int http_status_code = request->GetResponseCode();
518 518
519 ListValue args; 519 ListValue args;
520 DictionaryValue* dict = new DictionaryValue(); 520 DictionaryValue* dict = new DictionaryValue();
521 dict->SetString(keys::kRequestIdKey, 521 dict->SetString(keys::kRequestIdKey,
522 base::Uint64ToString(request->identifier())); 522 base::Uint64ToString(request->identifier()));
523 dict->SetString(keys::kUrlKey, request->url().spec()); 523 dict->SetString(keys::kUrlKey, request->url().spec());
524 dict->SetString(keys::kRedirectUrlKey, new_location.spec()); 524 dict->SetString(keys::kRedirectUrlKey, new_location.spec());
525 dict->SetInteger(keys::kStatusCodeKey, http_status_code); 525 dict->SetInteger(keys::kStatusCodeKey, http_status_code);
526 dict->SetDouble(keys::kTimeStampKey, time.ToDoubleT() * 1000); 526 dict->SetDouble(keys::kTimeStampKey, time.ToDoubleT() * 1000);
527 if (extra_info_spec & ExtraInfoSpec::RESPONSE_HEADERS) { 527 if (extra_info_spec & ExtraInfoSpec::RESPONSE_HEADERS) {
528 dict->Set(keys::kResponseHeadersKey, 528 dict->Set(keys::kResponseHeadersKey,
529 GetResponseHeadersList(request->response_headers())); 529 GetResponseHeadersList(request->response_headers()));
530 } 530 }
531 if (extra_info_spec & ExtraInfoSpec::STATUS_LINE) 531 if (extra_info_spec & ExtraInfoSpec::STATUS_LINE)
532 dict->Set(keys::kStatusLineKey, GetStatusLine(request->response_headers())); 532 dict->Set(keys::kStatusLineKey, GetStatusLine(request->response_headers()));
533 args.Append(dict); 533 args.Append(dict);
534 534
535 DispatchEvent(profile_id, request, listeners, args); 535 DispatchEvent(profile, request, listeners, args);
536 } 536 }
537 537
538 void ExtensionWebRequestEventRouter::OnResponseStarted( 538 void ExtensionWebRequestEventRouter::OnResponseStarted(
539 ProfileId profile_id, 539 void* profile,
540 ExtensionInfoMap* extension_info_map, 540 ExtensionInfoMap* extension_info_map,
541 net::URLRequest* request) { 541 net::URLRequest* request) {
542 if (profile_id == Profile::kInvalidProfileId) 542 if (!profile)
543 return; 543 return;
544 544
545 if (!HasWebRequestScheme(request->url())) 545 if (!HasWebRequestScheme(request->url()))
546 return; 546 return;
547 547
548 // OnResponseStarted is even triggered, when the request was cancelled. 548 // OnResponseStarted is even triggered, when the request was cancelled.
549 if (request->status().status() != net::URLRequestStatus::SUCCESS) 549 if (request->status().status() != net::URLRequestStatus::SUCCESS)
550 return; 550 return;
551 551
552 base::Time time(base::Time::Now()); 552 base::Time time(base::Time::Now());
553 553
554 int extra_info_spec = 0; 554 int extra_info_spec = 0;
555 std::vector<const EventListener*> listeners = 555 std::vector<const EventListener*> listeners =
556 GetMatchingListeners(profile_id, extension_info_map, 556 GetMatchingListeners(profile, extension_info_map,
557 keys::kOnResponseStarted, request, &extra_info_spec); 557 keys::kOnResponseStarted, request, &extra_info_spec);
558 if (listeners.empty()) 558 if (listeners.empty())
559 return; 559 return;
560 560
561 // UrlRequestFileJobs do not send headers, so we simulate their behavior. 561 // UrlRequestFileJobs do not send headers, so we simulate their behavior.
562 int response_code = 200; 562 int response_code = 200;
563 if (request->response_headers()) 563 if (request->response_headers())
564 response_code = request->response_headers()->response_code(); 564 response_code = request->response_headers()->response_code();
565 565
566 ListValue args; 566 ListValue args;
567 DictionaryValue* dict = new DictionaryValue(); 567 DictionaryValue* dict = new DictionaryValue();
568 dict->SetString(keys::kRequestIdKey, 568 dict->SetString(keys::kRequestIdKey,
569 base::Uint64ToString(request->identifier())); 569 base::Uint64ToString(request->identifier()));
570 dict->SetString(keys::kUrlKey, request->url().spec()); 570 dict->SetString(keys::kUrlKey, request->url().spec());
571 dict->SetInteger(keys::kStatusCodeKey, response_code); 571 dict->SetInteger(keys::kStatusCodeKey, response_code);
572 dict->SetDouble(keys::kTimeStampKey, time.ToDoubleT() * 1000); 572 dict->SetDouble(keys::kTimeStampKey, time.ToDoubleT() * 1000);
573 if (extra_info_spec & ExtraInfoSpec::RESPONSE_HEADERS) { 573 if (extra_info_spec & ExtraInfoSpec::RESPONSE_HEADERS) {
574 dict->Set(keys::kResponseHeadersKey, 574 dict->Set(keys::kResponseHeadersKey,
575 GetResponseHeadersList(request->response_headers())); 575 GetResponseHeadersList(request->response_headers()));
576 } 576 }
577 if (extra_info_spec & ExtraInfoSpec::STATUS_LINE) 577 if (extra_info_spec & ExtraInfoSpec::STATUS_LINE)
578 dict->Set(keys::kStatusLineKey, GetStatusLine(request->response_headers())); 578 dict->Set(keys::kStatusLineKey, GetStatusLine(request->response_headers()));
579 args.Append(dict); 579 args.Append(dict);
580 580
581 DispatchEvent(profile_id, request, listeners, args); 581 DispatchEvent(profile, request, listeners, args);
582 } 582 }
583 583
584 void ExtensionWebRequestEventRouter::OnCompleted( 584 void ExtensionWebRequestEventRouter::OnCompleted(
585 ProfileId profile_id, 585 void* profile,
586 ExtensionInfoMap* extension_info_map, 586 ExtensionInfoMap* extension_info_map,
587 net::URLRequest* request) { 587 net::URLRequest* request) {
588 if (profile_id == Profile::kInvalidProfileId) 588 if (!profile)
589 return; 589 return;
590 590
591 if (!HasWebRequestScheme(request->url())) 591 if (!HasWebRequestScheme(request->url()))
592 return; 592 return;
593 593
594 DCHECK(request->status().status() == net::URLRequestStatus::SUCCESS); 594 DCHECK(request->status().status() == net::URLRequestStatus::SUCCESS);
595 595
596 DCHECK(!GetAndSetSignaled(request->identifier(), kOnCompleted)); 596 DCHECK(!GetAndSetSignaled(request->identifier(), kOnCompleted));
597 597
598 base::Time time(base::Time::Now()); 598 base::Time time(base::Time::Now());
599 599
600 int extra_info_spec = 0; 600 int extra_info_spec = 0;
601 std::vector<const EventListener*> listeners = 601 std::vector<const EventListener*> listeners =
602 GetMatchingListeners(profile_id, extension_info_map, 602 GetMatchingListeners(profile, extension_info_map,
603 keys::kOnCompleted, request, &extra_info_spec); 603 keys::kOnCompleted, request, &extra_info_spec);
604 if (listeners.empty()) 604 if (listeners.empty())
605 return; 605 return;
606 606
607 // UrlRequestFileJobs do not send headers, so we simulate their behavior. 607 // UrlRequestFileJobs do not send headers, so we simulate their behavior.
608 int response_code = 200; 608 int response_code = 200;
609 if (request->response_headers()) 609 if (request->response_headers())
610 response_code = request->response_headers()->response_code(); 610 response_code = request->response_headers()->response_code();
611 611
612 ListValue args; 612 ListValue args;
613 DictionaryValue* dict = new DictionaryValue(); 613 DictionaryValue* dict = new DictionaryValue();
614 dict->SetString(keys::kRequestIdKey, 614 dict->SetString(keys::kRequestIdKey,
615 base::Uint64ToString(request->identifier())); 615 base::Uint64ToString(request->identifier()));
616 dict->SetString(keys::kUrlKey, request->url().spec()); 616 dict->SetString(keys::kUrlKey, request->url().spec());
617 dict->SetInteger(keys::kStatusCodeKey, response_code); 617 dict->SetInteger(keys::kStatusCodeKey, response_code);
618 dict->SetDouble(keys::kTimeStampKey, time.ToDoubleT() * 1000); 618 dict->SetDouble(keys::kTimeStampKey, time.ToDoubleT() * 1000);
619 if (extra_info_spec & ExtraInfoSpec::RESPONSE_HEADERS) { 619 if (extra_info_spec & ExtraInfoSpec::RESPONSE_HEADERS) {
620 dict->Set(keys::kResponseHeadersKey, 620 dict->Set(keys::kResponseHeadersKey,
621 GetResponseHeadersList(request->response_headers())); 621 GetResponseHeadersList(request->response_headers()));
622 } 622 }
623 if (extra_info_spec & ExtraInfoSpec::STATUS_LINE) 623 if (extra_info_spec & ExtraInfoSpec::STATUS_LINE)
624 dict->Set(keys::kStatusLineKey, GetStatusLine(request->response_headers())); 624 dict->Set(keys::kStatusLineKey, GetStatusLine(request->response_headers()));
625 args.Append(dict); 625 args.Append(dict);
626 626
627 DispatchEvent(profile_id, request, listeners, args); 627 DispatchEvent(profile, request, listeners, args);
628 } 628 }
629 629
630 void ExtensionWebRequestEventRouter::OnErrorOccurred( 630 void ExtensionWebRequestEventRouter::OnErrorOccurred(
631 ProfileId profile_id, 631 void* profile,
632 ExtensionInfoMap* extension_info_map, 632 ExtensionInfoMap* extension_info_map,
633 net::URLRequest* request) { 633 net::URLRequest* request) {
634 if (profile_id == Profile::kInvalidProfileId) 634 if (!profile)
635 return; 635 return;
636 636
637 if (!HasWebRequestScheme(request->url())) 637 if (!HasWebRequestScheme(request->url()))
638 return; 638 return;
639 639
640 DCHECK(request->status().status() == net::URLRequestStatus::FAILED); 640 DCHECK(request->status().status() == net::URLRequestStatus::FAILED);
641 641
642 DCHECK(!GetAndSetSignaled(request->identifier(), kOnErrorOccurred)); 642 DCHECK(!GetAndSetSignaled(request->identifier(), kOnErrorOccurred));
643 643
644 base::Time time(base::Time::Now()); 644 base::Time time(base::Time::Now());
645 645
646 int extra_info_spec = 0; 646 int extra_info_spec = 0;
647 std::vector<const EventListener*> listeners = 647 std::vector<const EventListener*> listeners =
648 GetMatchingListeners(profile_id, extension_info_map, 648 GetMatchingListeners(profile, extension_info_map,
649 keys::kOnErrorOccurred, request, &extra_info_spec); 649 keys::kOnErrorOccurred, request, &extra_info_spec);
650 if (listeners.empty()) 650 if (listeners.empty())
651 return; 651 return;
652 652
653 ListValue args; 653 ListValue args;
654 DictionaryValue* dict = new DictionaryValue(); 654 DictionaryValue* dict = new DictionaryValue();
655 dict->SetString(keys::kRequestIdKey, 655 dict->SetString(keys::kRequestIdKey,
656 base::Uint64ToString(request->identifier())); 656 base::Uint64ToString(request->identifier()));
657 dict->SetString(keys::kUrlKey, request->url().spec()); 657 dict->SetString(keys::kUrlKey, request->url().spec());
658 dict->SetString(keys::kErrorKey, 658 dict->SetString(keys::kErrorKey,
659 net::ErrorToString(request->status().os_error())); 659 net::ErrorToString(request->status().os_error()));
660 dict->SetDouble(keys::kTimeStampKey, time.ToDoubleT() * 1000); 660 dict->SetDouble(keys::kTimeStampKey, time.ToDoubleT() * 1000);
661 args.Append(dict); 661 args.Append(dict);
662 662
663 DispatchEvent(profile_id, request, listeners, args); 663 DispatchEvent(profile, request, listeners, args);
664 } 664 }
665 665
666 void ExtensionWebRequestEventRouter::OnURLRequestDestroyed( 666 void ExtensionWebRequestEventRouter::OnURLRequestDestroyed(
667 ProfileId profile_id, net::URLRequest* request) { 667 void* profile, net::URLRequest* request) {
668 blocked_requests_.erase(request->identifier()); 668 blocked_requests_.erase(request->identifier());
669 signaled_requests_.erase(request->identifier()); 669 signaled_requests_.erase(request->identifier());
670 http_requests_.erase(request->identifier()); 670 http_requests_.erase(request->identifier());
671 } 671 }
672 672
673 void ExtensionWebRequestEventRouter::OnHttpTransactionDestroyed( 673 void ExtensionWebRequestEventRouter::OnHttpTransactionDestroyed(
674 ProfileId profile_id, uint64 request_id) { 674 void* profile, uint64 request_id) {
675 if (blocked_requests_.find(request_id) != blocked_requests_.end() && 675 if (blocked_requests_.find(request_id) != blocked_requests_.end() &&
676 blocked_requests_[request_id].event == kOnBeforeSendHeaders) { 676 blocked_requests_[request_id].event == kOnBeforeSendHeaders) {
677 // Ensure we don't call into the deleted HttpTransaction. 677 // Ensure we don't call into the deleted HttpTransaction.
678 blocked_requests_[request_id].callback = NULL; 678 blocked_requests_[request_id].callback = NULL;
679 blocked_requests_[request_id].request_headers = NULL; 679 blocked_requests_[request_id].request_headers = NULL;
680 } 680 }
681 } 681 }
682 682
683 bool ExtensionWebRequestEventRouter::DispatchEvent( 683 bool ExtensionWebRequestEventRouter::DispatchEvent(
684 ProfileId profile_id, 684 void* profile,
685 net::URLRequest* request, 685 net::URLRequest* request,
686 const std::vector<const EventListener*>& listeners, 686 const std::vector<const EventListener*>& listeners,
687 const ListValue& args) { 687 const ListValue& args) {
688 std::string json_args; 688 std::string json_args;
689 689
690 // TODO(mpcomplete): Consider consolidating common (extension_id,json_args) 690 // TODO(mpcomplete): Consider consolidating common (extension_id,json_args)
691 // pairs into a single message sent to a list of sub_event_names. 691 // pairs into a single message sent to a list of sub_event_names.
692 int num_handlers_blocking = 0; 692 int num_handlers_blocking = 0;
693 for (std::vector<const EventListener*>::const_iterator it = listeners.begin(); 693 for (std::vector<const EventListener*>::const_iterator it = listeners.begin();
694 it != listeners.end(); ++it) { 694 it != listeners.end(); ++it) {
(...skipping 26 matching lines...) Expand all
721 num_handlers_blocking; 721 num_handlers_blocking;
722 blocked_requests_[request->identifier()].blocking_time = base::Time::Now(); 722 blocked_requests_[request->identifier()].blocking_time = base::Time::Now();
723 723
724 return true; 724 return true;
725 } 725 }
726 726
727 return false; 727 return false;
728 } 728 }
729 729
730 void ExtensionWebRequestEventRouter::OnEventHandled( 730 void ExtensionWebRequestEventRouter::OnEventHandled(
731 ProfileId profile_id, 731 void* profile,
732 const std::string& extension_id, 732 const std::string& extension_id,
733 const std::string& event_name, 733 const std::string& event_name,
734 const std::string& sub_event_name, 734 const std::string& sub_event_name,
735 uint64 request_id, 735 uint64 request_id,
736 EventResponse* response) { 736 EventResponse* response) {
737 EventListener listener; 737 EventListener listener;
738 listener.extension_id = extension_id; 738 listener.extension_id = extension_id;
739 listener.sub_event_name = sub_event_name; 739 listener.sub_event_name = sub_event_name;
740 740
741 // The listener may have been removed (e.g. due to the process going away) 741 // The listener may have been removed (e.g. due to the process going away)
742 // before we got here. 742 // before we got here.
743 std::set<EventListener>::iterator found = 743 std::set<EventListener>::iterator found =
744 listeners_[profile_id][event_name].find(listener); 744 listeners_[profile][event_name].find(listener);
745 if (found != listeners_[profile_id][event_name].end()) 745 if (found != listeners_[profile][event_name].end())
746 found->blocked_requests.erase(request_id); 746 found->blocked_requests.erase(request_id);
747 747
748 DecrementBlockCount(request_id, response); 748 DecrementBlockCount(request_id, response);
749 } 749 }
750 750
751 void ExtensionWebRequestEventRouter::AddEventListener( 751 void ExtensionWebRequestEventRouter::AddEventListener(
752 ProfileId profile_id, 752 void* profile,
753 const std::string& extension_id, 753 const std::string& extension_id,
754 const std::string& event_name, 754 const std::string& event_name,
755 const std::string& sub_event_name, 755 const std::string& sub_event_name,
756 const RequestFilter& filter, 756 const RequestFilter& filter,
757 int extra_info_spec, 757 int extra_info_spec,
758 base::WeakPtr<IPC::Message::Sender> ipc_sender) { 758 base::WeakPtr<IPC::Message::Sender> ipc_sender) {
759 if (!IsWebRequestEvent(event_name)) 759 if (!IsWebRequestEvent(event_name))
760 return; 760 return;
761 761
762 EventListener listener; 762 EventListener listener;
763 listener.extension_id = extension_id; 763 listener.extension_id = extension_id;
764 listener.sub_event_name = sub_event_name; 764 listener.sub_event_name = sub_event_name;
765 listener.filter = filter; 765 listener.filter = filter;
766 listener.extra_info_spec = extra_info_spec; 766 listener.extra_info_spec = extra_info_spec;
767 listener.ipc_sender = ipc_sender; 767 listener.ipc_sender = ipc_sender;
768 768
769 CHECK_EQ(listeners_[profile_id][event_name].count(listener), 0u) << 769 CHECK_EQ(listeners_[profile][event_name].count(listener), 0u) <<
770 "extension=" << extension_id << " event=" << event_name; 770 "extension=" << extension_id << " event=" << event_name;
771 listeners_[profile_id][event_name].insert(listener); 771 listeners_[profile][event_name].insert(listener);
772 } 772 }
773 773
774 void ExtensionWebRequestEventRouter::RemoveEventListener( 774 void ExtensionWebRequestEventRouter::RemoveEventListener(
775 ProfileId profile_id, 775 void* profile,
776 const std::string& extension_id, 776 const std::string& extension_id,
777 const std::string& sub_event_name) { 777 const std::string& sub_event_name) {
778 size_t slash_sep = sub_event_name.find('/'); 778 size_t slash_sep = sub_event_name.find('/');
779 std::string event_name = sub_event_name.substr(0, slash_sep); 779 std::string event_name = sub_event_name.substr(0, slash_sep);
780 780
781 if (!IsWebRequestEvent(event_name)) 781 if (!IsWebRequestEvent(event_name))
782 return; 782 return;
783 783
784 EventListener listener; 784 EventListener listener;
785 listener.extension_id = extension_id; 785 listener.extension_id = extension_id;
786 listener.sub_event_name = sub_event_name; 786 listener.sub_event_name = sub_event_name;
787 787
788 // It's possible for AddEventListener to fail asynchronously. In that case, 788 // It's possible for AddEventListener to fail asynchronously. In that case,
789 // the renderer believes the listener exists, while the browser does not. 789 // the renderer believes the listener exists, while the browser does not.
790 // Ignore a RemoveEventListener in that case. 790 // Ignore a RemoveEventListener in that case.
791 std::set<EventListener>::iterator found = 791 std::set<EventListener>::iterator found =
792 listeners_[profile_id][event_name].find(listener); 792 listeners_[profile][event_name].find(listener);
793 if (found == listeners_[profile_id][event_name].end()) 793 if (found == listeners_[profile][event_name].end())
794 return; 794 return;
795 795
796 CHECK_EQ(listeners_[profile_id][event_name].count(listener), 1u) << 796 CHECK_EQ(listeners_[profile][event_name].count(listener), 1u) <<
797 "extension=" << extension_id << " event=" << event_name; 797 "extension=" << extension_id << " event=" << event_name;
798 798
799 // Unblock any request that this event listener may have been blocking. 799 // Unblock any request that this event listener may have been blocking.
800 for (std::set<uint64>::iterator it = found->blocked_requests.begin(); 800 for (std::set<uint64>::iterator it = found->blocked_requests.begin();
801 it != found->blocked_requests.end(); ++it) { 801 it != found->blocked_requests.end(); ++it) {
802 DecrementBlockCount(*it, NULL); 802 DecrementBlockCount(*it, NULL);
803 } 803 }
804 804
805 listeners_[profile_id][event_name].erase(listener); 805 listeners_[profile][event_name].erase(listener);
806 } 806 }
807 807
808 void ExtensionWebRequestEventRouter::OnOTRProfileCreated( 808 void ExtensionWebRequestEventRouter::OnOTRProfileCreated(
809 ProfileId original_profile_id, ProfileId otr_profile_id) { 809 void* original_profile, void* otr_profile) {
810 cross_profile_map_[original_profile_id] = otr_profile_id; 810 cross_profile_map_[original_profile] = otr_profile;
811 cross_profile_map_[otr_profile_id] = original_profile_id; 811 cross_profile_map_[otr_profile] = original_profile;
812 } 812 }
813 813
814 void ExtensionWebRequestEventRouter::OnOTRProfileDestroyed( 814 void ExtensionWebRequestEventRouter::OnOTRProfileDestroyed(
815 ProfileId original_profile_id, ProfileId otr_profile_id) { 815 void* original_profile, void* otr_profile) {
816 cross_profile_map_.erase(otr_profile_id); 816 cross_profile_map_.erase(otr_profile);
817 cross_profile_map_.erase(original_profile_id); 817 cross_profile_map_.erase(original_profile);
818 } 818 }
819 819
820 void ExtensionWebRequestEventRouter::GetMatchingListenersImpl( 820 void ExtensionWebRequestEventRouter::GetMatchingListenersImpl(
821 ProfileId profile_id, 821 void* profile,
822 ExtensionInfoMap* extension_info_map, 822 ExtensionInfoMap* extension_info_map,
823 bool crosses_incognito, 823 bool crosses_incognito,
824 const std::string& event_name, 824 const std::string& event_name,
825 const GURL& url, 825 const GURL& url,
826 int tab_id, 826 int tab_id,
827 int window_id, 827 int window_id,
828 ResourceType::Type resource_type, 828 ResourceType::Type resource_type,
829 int* extra_info_spec, 829 int* extra_info_spec,
830 std::vector<const ExtensionWebRequestEventRouter::EventListener*>* 830 std::vector<const ExtensionWebRequestEventRouter::EventListener*>*
831 matching_listeners) { 831 matching_listeners) {
832 std::set<EventListener>& listeners = listeners_[profile_id][event_name]; 832 std::set<EventListener>& listeners = listeners_[profile][event_name];
833 for (std::set<EventListener>::iterator it = listeners.begin(); 833 for (std::set<EventListener>::iterator it = listeners.begin();
834 it != listeners.end(); ++it) { 834 it != listeners.end(); ++it) {
835 if (!it->ipc_sender.get()) { 835 if (!it->ipc_sender.get()) {
836 // The IPC sender has been deleted. This listener will be removed soon 836 // The IPC sender has been deleted. This listener will be removed soon
837 // via a call to RemoveEventListener. For now, just skip it. 837 // via a call to RemoveEventListener. For now, just skip it.
838 continue; 838 continue;
839 } 839 }
840 840
841 if (!it->filter.urls.is_empty() && !it->filter.urls.MatchesURL(url)) 841 if (!it->filter.urls.is_empty() && !it->filter.urls.MatchesURL(url))
842 continue; 842 continue;
(...skipping 17 matching lines...) Expand all
860 continue; 860 continue;
861 } 861 }
862 862
863 matching_listeners->push_back(&(*it)); 863 matching_listeners->push_back(&(*it));
864 *extra_info_spec |= it->extra_info_spec; 864 *extra_info_spec |= it->extra_info_spec;
865 } 865 }
866 } 866 }
867 867
868 std::vector<const ExtensionWebRequestEventRouter::EventListener*> 868 std::vector<const ExtensionWebRequestEventRouter::EventListener*>
869 ExtensionWebRequestEventRouter::GetMatchingListeners( 869 ExtensionWebRequestEventRouter::GetMatchingListeners(
870 ProfileId profile_id, 870 void* profile,
871 ExtensionInfoMap* extension_info_map, 871 ExtensionInfoMap* extension_info_map,
872 const std::string& event_name, 872 const std::string& event_name,
873 const GURL& url, 873 const GURL& url,
874 int tab_id, 874 int tab_id,
875 int window_id, 875 int window_id,
876 ResourceType::Type resource_type, 876 ResourceType::Type resource_type,
877 int* extra_info_spec) { 877 int* extra_info_spec) {
878 // TODO(mpcomplete): handle profile_id == invalid (should collect all 878 // TODO(mpcomplete): handle profile == NULL (should collect all listeners).
879 // listeners).
880 *extra_info_spec = 0; 879 *extra_info_spec = 0;
881 880
882 std::vector<const ExtensionWebRequestEventRouter::EventListener*> 881 std::vector<const ExtensionWebRequestEventRouter::EventListener*>
883 matching_listeners; 882 matching_listeners;
884 883
885 GetMatchingListenersImpl( 884 GetMatchingListenersImpl(
886 profile_id, extension_info_map, false, event_name, url, 885 profile, extension_info_map, false, event_name, url,
887 tab_id, window_id, resource_type, extra_info_spec, &matching_listeners); 886 tab_id, window_id, resource_type, extra_info_spec, &matching_listeners);
888 CrossProfileMap::const_iterator cross_profile_id = 887 CrossProfileMap::const_iterator cross_profile =
889 cross_profile_map_.find(profile_id); 888 cross_profile_map_.find(profile);
890 if (cross_profile_id != cross_profile_map_.end()) { 889 if (cross_profile != cross_profile_map_.end()) {
891 GetMatchingListenersImpl( 890 GetMatchingListenersImpl(
892 cross_profile_id->second, extension_info_map, true, event_name, url, 891 cross_profile->second, extension_info_map, true, event_name, url,
893 tab_id, window_id, resource_type, extra_info_spec, &matching_listeners); 892 tab_id, window_id, resource_type, extra_info_spec, &matching_listeners);
894 } 893 }
895 894
896 return matching_listeners; 895 return matching_listeners;
897 } 896 }
898 897
899 std::vector<const ExtensionWebRequestEventRouter::EventListener*> 898 std::vector<const ExtensionWebRequestEventRouter::EventListener*>
900 ExtensionWebRequestEventRouter::GetMatchingListeners( 899 ExtensionWebRequestEventRouter::GetMatchingListeners(
901 ProfileId profile_id, 900 void* profile,
902 ExtensionInfoMap* extension_info_map, 901 ExtensionInfoMap* extension_info_map,
903 const std::string& event_name, 902 const std::string& event_name,
904 net::URLRequest* request, 903 net::URLRequest* request,
905 int* extra_info_spec) { 904 int* extra_info_spec) {
906 int tab_id = -1; 905 int tab_id = -1;
907 int window_id = -1; 906 int window_id = -1;
908 ResourceType::Type resource_type = ResourceType::LAST_TYPE; 907 ResourceType::Type resource_type = ResourceType::LAST_TYPE;
909 ExtractRequestInfo(request, &tab_id, &window_id, &resource_type); 908 ExtractRequestInfo(request, &tab_id, &window_id, &resource_type);
910 909
911 return GetMatchingListeners( 910 return GetMatchingListeners(
912 profile_id, extension_info_map, event_name, request->url(), 911 profile, extension_info_map, event_name, request->url(),
913 tab_id, window_id, resource_type, extra_info_spec); 912 tab_id, window_id, resource_type, extra_info_spec);
914 } 913 }
915 914
916 void ExtensionWebRequestEventRouter::DecrementBlockCount( 915 void ExtensionWebRequestEventRouter::DecrementBlockCount(
917 uint64 request_id, 916 uint64 request_id,
918 EventResponse* response) { 917 EventResponse* response) {
919 scoped_ptr<EventResponse> response_scoped(response); 918 scoped_ptr<EventResponse> response_scoped(response);
920 919
921 // It's possible that this request was deleted, or cancelled by a previous 920 // It's possible that this request was deleted, or cancelled by a previous
922 // event handler. If so, ignore this response. 921 // event handler. If so, ignore this response.
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
1028 *value, &extra_info_spec)); 1027 *value, &extra_info_spec));
1029 } 1028 }
1030 1029
1031 std::string event_name; 1030 std::string event_name;
1032 EXTENSION_FUNCTION_VALIDATE(args_->GetString(3, &event_name)); 1031 EXTENSION_FUNCTION_VALIDATE(args_->GetString(3, &event_name));
1033 1032
1034 std::string sub_event_name; 1033 std::string sub_event_name;
1035 EXTENSION_FUNCTION_VALIDATE(args_->GetString(4, &sub_event_name)); 1034 EXTENSION_FUNCTION_VALIDATE(args_->GetString(4, &sub_event_name));
1036 1035
1037 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( 1036 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener(
1038 profile_id(), extension_id(), event_name, sub_event_name, filter, 1037 profile(), extension_id(), event_name, sub_event_name, filter,
1039 extra_info_spec, ipc_sender_weak()); 1038 extra_info_spec, ipc_sender_weak());
1040 1039
1041 return true; 1040 return true;
1042 } 1041 }
1043 1042
1044 bool WebRequestEventHandled::RunImpl() { 1043 bool WebRequestEventHandled::RunImpl() {
1045 std::string event_name; 1044 std::string event_name;
1046 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); 1045 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
1047 1046
1048 std::string sub_event_name; 1047 std::string sub_event_name;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1105 header_value->GetString(keys::kHeaderNameKey, &name)); 1104 header_value->GetString(keys::kHeaderNameKey, &name));
1106 EXTENSION_FUNCTION_VALIDATE( 1105 EXTENSION_FUNCTION_VALIDATE(
1107 header_value->GetString(keys::kHeaderValueKey, &value)); 1106 header_value->GetString(keys::kHeaderValueKey, &value));
1108 1107
1109 response->request_headers->SetHeader(name, value); 1108 response->request_headers->SetHeader(name, value);
1110 } 1109 }
1111 } 1110 }
1112 } 1111 }
1113 1112
1114 ExtensionWebRequestEventRouter::GetInstance()->OnEventHandled( 1113 ExtensionWebRequestEventRouter::GetInstance()->OnEventHandled(
1115 profile_id(), extension_id(), event_name, sub_event_name, request_id, 1114 profile(), extension_id(), event_name, sub_event_name, request_id,
1116 response.release()); 1115 response.release());
1117 1116
1118 return true; 1117 return true;
1119 } 1118 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_webrequest_api.h ('k') | chrome/browser/extensions/extension_webrequest_api_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698