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

Side by Side Diff: chrome/browser/extensions/api/web_request/web_request_api_helpers.cc

Issue 154473002: Support redirectUrl at onHeadersReceived in WebRequest / DWR API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add more tests Created 6 years, 9 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
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/web_request/web_request_api_helpers.h" 5 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 } 317 }
318 } 318 }
319 } 319 }
320 return result; 320 return result;
321 } 321 }
322 322
323 EventResponseDelta* CalculateOnHeadersReceivedDelta( 323 EventResponseDelta* CalculateOnHeadersReceivedDelta(
324 const std::string& extension_id, 324 const std::string& extension_id,
325 const base::Time& extension_install_time, 325 const base::Time& extension_install_time,
326 bool cancel, 326 bool cancel,
327 const GURL& new_url,
327 const net::HttpResponseHeaders* old_response_headers, 328 const net::HttpResponseHeaders* old_response_headers,
328 ResponseHeaders* new_response_headers) { 329 ResponseHeaders* new_response_headers) {
329 EventResponseDelta* result = 330 EventResponseDelta* result =
330 new EventResponseDelta(extension_id, extension_install_time); 331 new EventResponseDelta(extension_id, extension_install_time);
331 result->cancel = cancel; 332 result->cancel = cancel;
333 result->new_url = new_url;
332 334
333 if (!new_response_headers) 335 if (!new_response_headers)
334 return result; 336 return result;
335 337
336 // Find deleted headers (header keys are treated case insensitively). 338 // Find deleted headers (header keys are treated case insensitively).
337 { 339 {
338 void* iter = NULL; 340 void* iter = NULL;
339 std::string name; 341 std::string name;
340 std::string value; 342 std::string value;
341 while (old_response_headers->EnumerateHeaderLines(&iter, &name, &value)) { 343 while (old_response_headers->EnumerateHeaderLines(&iter, &name, &value)) {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 if ((*i)->cancel) { 398 if ((*i)->cancel) {
397 *canceled = true; 399 *canceled = true;
398 net_log->AddEvent( 400 net_log->AddEvent(
399 net::NetLog::TYPE_CHROME_EXTENSION_ABORTED_REQUEST, 401 net::NetLog::TYPE_CHROME_EXTENSION_ABORTED_REQUEST,
400 CreateNetLogExtensionIdCallback(i->get())); 402 CreateNetLogExtensionIdCallback(i->get()));
401 break; 403 break;
402 } 404 }
403 } 405 }
404 } 406 }
405 407
406 // Helper function for MergeOnBeforeRequestResponses() that allows ignoring 408 // Helper function for MergeRedirectUrlOfResponses() that allows ignoring
407 // all redirects but those to data:// urls and about:blank. This is important 409 // all redirects but those to data:// urls and about:blank. This is important
408 // to treat these URLs as "cancel urls", i.e. URLs that extensions redirect 410 // to treat these URLs as "cancel urls", i.e. URLs that extensions redirect
409 // to if they want to express that they want to cancel a request. This reduces 411 // to if they want to express that they want to cancel a request. This reduces
410 // the number of conflicts that we need to flag, as canceling is considered 412 // the number of conflicts that we need to flag, as canceling is considered
411 // a higher precedence operation that redirects. 413 // a higher precedence operation that redirects.
412 // Returns whether a redirect occurred. 414 // Returns whether a redirect occurred.
413 static bool MergeOnBeforeRequestResponsesHelper( 415 static bool MergeRedirectUrlOfResponsesHelper(
414 const EventResponseDeltas& deltas, 416 const EventResponseDeltas& deltas,
415 GURL* new_url, 417 GURL* new_url,
416 extensions::ExtensionWarningSet* conflicting_extensions, 418 extensions::ExtensionWarningSet* conflicting_extensions,
417 const net::BoundNetLog* net_log, 419 const net::BoundNetLog* net_log,
418 bool consider_only_cancel_scheme_urls) { 420 bool consider_only_cancel_scheme_urls) {
419 bool redirected = false; 421 bool redirected = false;
420 422
421 // Extension that determines the |new_url|. 423 // Extension that determines the |new_url|.
422 std::string winning_extension_id; 424 std::string winning_extension_id;
423 EventResponseDeltas::const_iterator delta; 425 EventResponseDeltas::const_iterator delta;
(...skipping 21 matching lines...) Expand all
445 (*delta)->new_url, 447 (*delta)->new_url,
446 *new_url)); 448 *new_url));
447 net_log->AddEvent( 449 net_log->AddEvent(
448 net::NetLog::TYPE_CHROME_EXTENSION_IGNORED_DUE_TO_CONFLICT, 450 net::NetLog::TYPE_CHROME_EXTENSION_IGNORED_DUE_TO_CONFLICT,
449 CreateNetLogExtensionIdCallback(delta->get())); 451 CreateNetLogExtensionIdCallback(delta->get()));
450 } 452 }
451 } 453 }
452 return redirected; 454 return redirected;
453 } 455 }
454 456
455 void MergeOnBeforeRequestResponses( 457 void MergeRedirectUrlOfResponses(
456 const EventResponseDeltas& deltas, 458 const EventResponseDeltas& deltas,
457 GURL* new_url, 459 GURL* new_url,
458 extensions::ExtensionWarningSet* conflicting_extensions, 460 extensions::ExtensionWarningSet* conflicting_extensions,
459 const net::BoundNetLog* net_log) { 461 const net::BoundNetLog* net_log) {
460 462
461 // First handle only redirects to data:// URLs and about:blank. These are a 463 // First handle only redirects to data:// URLs and about:blank. These are a
462 // special case as they represent a way of cancelling a request. 464 // special case as they represent a way of cancelling a request.
463 if (MergeOnBeforeRequestResponsesHelper( 465 if (MergeRedirectUrlOfResponsesHelper(
464 deltas, new_url, conflicting_extensions, net_log, true)) { 466 deltas, new_url, conflicting_extensions, net_log, true)) {
465 // If any extension cancelled a request by redirecting to a data:// URL or 467 // If any extension cancelled a request by redirecting to a data:// URL or
466 // about:blank, we don't consider the other redirects. 468 // about:blank, we don't consider the other redirects.
467 return; 469 return;
468 } 470 }
469 471
470 // Handle all other redirects. 472 // Handle all other redirects.
471 MergeOnBeforeRequestResponsesHelper( 473 MergeRedirectUrlOfResponsesHelper(
472 deltas, new_url, conflicting_extensions, net_log, false); 474 deltas, new_url, conflicting_extensions, net_log, false);
473 } 475 }
474 476
477 void MergeOnBeforeRequestResponses(
478 const EventResponseDeltas& deltas,
479 GURL* new_url,
480 extensions::ExtensionWarningSet* conflicting_extensions,
481 const net::BoundNetLog* net_log) {
482 MergeRedirectUrlOfResponses(deltas, new_url, conflicting_extensions, net_log);
483 }
484
475 // Assumes that |header_value| is the cookie header value of a HTTP Request 485 // Assumes that |header_value| is the cookie header value of a HTTP Request
476 // following the cookie-string schema of RFC 6265, section 4.2.1, and returns 486 // following the cookie-string schema of RFC 6265, section 4.2.1, and returns
477 // cookie name/value pairs. If cookie values are presented in double quotes, 487 // cookie name/value pairs. If cookie values are presented in double quotes,
478 // these will appear in |parsed| as well. We can assume that the cookie header 488 // these will appear in |parsed| as well. We can assume that the cookie header
479 // is written by Chromium and therefore, well-formed. 489 // is written by Chromium and therefore, well-formed.
480 static void ParseRequestCookieLine( 490 static void ParseRequestCookieLine(
481 const std::string& header_value, 491 const std::string& header_value,
482 ParsedRequestCookies* parsed_cookies) { 492 ParsedRequestCookies* parsed_cookies) {
483 std::string::const_iterator i = header_value.begin(); 493 std::string::const_iterator i = header_value.begin();
484 while (i != header_value.end()) { 494 while (i != header_value.end()) {
(...skipping 689 matching lines...) Expand 10 before | Expand all | Expand 10 after
1174 (*delta)->extension_id, winning_extension_id, 1184 (*delta)->extension_id, winning_extension_id,
1175 conflicting_header)); 1185 conflicting_header));
1176 net_log->AddEvent( 1186 net_log->AddEvent(
1177 net::NetLog::TYPE_CHROME_EXTENSION_IGNORED_DUE_TO_CONFLICT, 1187 net::NetLog::TYPE_CHROME_EXTENSION_IGNORED_DUE_TO_CONFLICT,
1178 CreateNetLogExtensionIdCallback(delta->get())); 1188 CreateNetLogExtensionIdCallback(delta->get()));
1179 } 1189 }
1180 } 1190 }
1181 1191
1182 MergeCookiesInOnHeadersReceivedResponses(deltas, original_response_headers, 1192 MergeCookiesInOnHeadersReceivedResponses(deltas, original_response_headers,
1183 override_response_headers, conflicting_extensions, net_log); 1193 override_response_headers, conflicting_extensions, net_log);
1194
1195 GURL new_url;
1196 MergeRedirectUrlOfResponses(deltas, &new_url, conflicting_extensions,
1197 net_log);
1198 if (new_url.is_valid()) {
1199 // Only create a copy if we really want to modify the response headers.
1200 if (override_response_headers->get() == NULL) {
1201 *override_response_headers = new net::HttpResponseHeaders(
1202 original_response_headers->raw_headers());
1203 }
1204 (*override_response_headers)->SetSafeRedirect(new_url);
1205 }
1184 } 1206 }
1185 1207
1186 bool MergeOnAuthRequiredResponses( 1208 bool MergeOnAuthRequiredResponses(
1187 const EventResponseDeltas& deltas, 1209 const EventResponseDeltas& deltas,
1188 net::AuthCredentials* auth_credentials, 1210 net::AuthCredentials* auth_credentials,
1189 extensions::ExtensionWarningSet* conflicting_extensions, 1211 extensions::ExtensionWarningSet* conflicting_extensions,
1190 const net::BoundNetLog* net_log) { 1212 const net::BoundNetLog* net_log) {
1191 CHECK(auth_credentials); 1213 CHECK(auth_credentials);
1192 bool credentials_set = false; 1214 bool credentials_set = false;
1193 std::string winning_extension_id; 1215 std::string winning_extension_id;
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
1275 for (content::RenderProcessHost::iterator it = 1297 for (content::RenderProcessHost::iterator it =
1276 content::RenderProcessHost::AllHostsIterator(); 1298 content::RenderProcessHost::AllHostsIterator();
1277 !it.IsAtEnd(); it.Advance()) { 1299 !it.IsAtEnd(); it.Advance()) {
1278 content::RenderProcessHost* host = it.GetCurrentValue(); 1300 content::RenderProcessHost* host = it.GetCurrentValue();
1279 if (host->GetBrowserContext() == browser_context) 1301 if (host->GetBrowserContext() == browser_context)
1280 SendExtensionWebRequestStatusToHost(host); 1302 SendExtensionWebRequestStatusToHost(host);
1281 } 1303 }
1282 } 1304 }
1283 1305
1284 } // namespace extension_web_request_api_helpers 1306 } // namespace extension_web_request_api_helpers
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698