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

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

Issue 10694055: Add read-only access to POST data for webRequest's onBeforeRequest (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Now checking the channel for real Created 8 years, 4 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.h" 5 #include "chrome/browser/extensions/api/web_request/web_request_api.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/json/json_writer.h" 10 #include "base/json/json_writer.h"
11 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
12 #include "base/string_number_conversions.h" 12 #include "base/string_number_conversions.h"
13 #include "base/string_util.h" 13 #include "base/string_util.h"
14 #include "base/time.h" 14 #include "base/time.h"
15 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "chrome/browser/browser_process.h" 17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/chrome_content_browser_client.h" 18 #include "chrome/browser/chrome_content_browser_client.h"
19 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " 19 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h "
20 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" 20 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h"
21 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_helper s.h" 21 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_helper s.h"
22 #include "chrome/browser/extensions/api/web_request/post_data_parser.h"
22 #include "chrome/browser/extensions/api/web_request/web_request_api_constants.h" 23 #include "chrome/browser/extensions/api/web_request/web_request_api_constants.h"
23 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" 24 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h"
24 #include "chrome/browser/extensions/api/web_request/web_request_time_tracker.h" 25 #include "chrome/browser/extensions/api/web_request/web_request_time_tracker.h"
25 #include "chrome/browser/extensions/event_router.h" 26 #include "chrome/browser/extensions/event_router.h"
26 #include "chrome/browser/extensions/extension_info_map.h" 27 #include "chrome/browser/extensions/extension_info_map.h"
27 #include "chrome/browser/extensions/extension_prefs.h" 28 #include "chrome/browser/extensions/extension_prefs.h"
28 #include "chrome/browser/extensions/extension_service.h" 29 #include "chrome/browser/extensions/extension_service.h"
29 #include "chrome/browser/extensions/extension_tab_id_map.h" 30 #include "chrome/browser/extensions/extension_tab_id_map.h"
30 #include "chrome/browser/profiles/profile.h" 31 #include "chrome/browser/profiles/profile.h"
31 #include "chrome/browser/profiles/profile_manager.h" 32 #include "chrome/browser/profiles/profile_manager.h"
32 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" 33 #include "chrome/browser/renderer_host/chrome_render_message_filter.h"
33 #include "chrome/browser/renderer_host/web_cache_manager.h" 34 #include "chrome/browser/renderer_host/web_cache_manager.h"
35 #include "chrome/common/chrome_version_info.h"
34 #include "chrome/common/extensions/event_filtering_info.h" 36 #include "chrome/common/extensions/event_filtering_info.h"
35 #include "chrome/common/extensions/extension.h" 37 #include "chrome/common/extensions/extension.h"
36 #include "chrome/common/extensions/extension_constants.h" 38 #include "chrome/common/extensions/extension_constants.h"
37 #include "chrome/common/extensions/extension_error_utils.h" 39 #include "chrome/common/extensions/extension_error_utils.h"
38 #include "chrome/common/extensions/extension_messages.h" 40 #include "chrome/common/extensions/extension_messages.h"
39 #include "chrome/common/extensions/url_pattern.h" 41 #include "chrome/common/extensions/url_pattern.h"
40 #include "chrome/common/url_constants.h" 42 #include "chrome/common/url_constants.h"
41 #include "content/public/browser/browser_message_filter.h" 43 #include "content/public/browser/browser_message_filter.h"
42 #include "content/public/browser/browser_thread.h" 44 #include "content/public/browser/browser_thread.h"
43 #include "content/public/browser/render_process_host.h" 45 #include "content/public/browser/render_process_host.h"
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 base::Uint64ToString(request->identifier())); 152 base::Uint64ToString(request->identifier()));
151 out->SetString(keys::kUrlKey, request->url().spec()); 153 out->SetString(keys::kUrlKey, request->url().spec());
152 out->SetString(keys::kMethodKey, request->method()); 154 out->SetString(keys::kMethodKey, request->method());
153 out->SetInteger(keys::kFrameIdKey, frame_id_for_extension); 155 out->SetInteger(keys::kFrameIdKey, frame_id_for_extension);
154 out->SetInteger(keys::kParentFrameIdKey, parent_frame_id_for_extension); 156 out->SetInteger(keys::kParentFrameIdKey, parent_frame_id_for_extension);
155 out->SetInteger(keys::kTabIdKey, tab_id); 157 out->SetInteger(keys::kTabIdKey, tab_id);
156 out->SetString(keys::kTypeKey, helpers::ResourceTypeToString(resource_type)); 158 out->SetString(keys::kTypeKey, helpers::ResourceTypeToString(resource_type));
157 out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000); 159 out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000);
158 } 160 }
159 161
162 // Extracts the POST data from |request| and writes the data into |out|.
163 // This can be expensive, so it's separated from ExtractRequestInfo().
164 void ExtractRequestInfoPost(const net::URLRequest* request,
165 DictionaryValue* out) {
166 if (request->method() != "POST")
wtc 2012/08/01 17:24:41 If you want to support the PUT method, add && re
167 return; // Need to exit without "out->Set(keys::kPostDataKey, ...);" .
168
169 DictionaryValue* post_data = new DictionaryValue();
170 out->Set(keys::kPostDataKey, post_data);
171
172 // Chunked encoding (defined in RFC 2616) is currently not supported.
173 std::string transfer_encoding;
174 if (request->extra_request_headers().GetHeader(
175 net::HttpRequestHeaders::kTransferEncoding, &transfer_encoding)) {
176 if (base::strcasecmp(transfer_encoding.c_str(), "chunked") == 0) {
177 post_data->SetString(keys::kPostDataErrorKey, "chunked_encoding");
178 return;
179 }
180 }
181
182 scoped_ptr<DictionaryValue> form_data =
183 extensions::PostDataParser::ParseURLRequestData(request);
184 if (form_data.get() != NULL)
185 post_data->Set(keys::kFormDataKey, form_data.release());
186 }
187
160 // Converts a HttpHeaders dictionary to a |name|, |value| pair. Returns 188 // Converts a HttpHeaders dictionary to a |name|, |value| pair. Returns
161 // true if successful. 189 // true if successful.
162 bool FromHeaderDictionary(const DictionaryValue* header_value, 190 bool FromHeaderDictionary(const DictionaryValue* header_value,
163 std::string* name, 191 std::string* name,
164 std::string* value) { 192 std::string* value) {
165 if (!header_value->GetString(keys::kHeaderNameKey, name)) 193 if (!header_value->GetString(keys::kHeaderNameKey, name))
166 return false; 194 return false;
167 195
168 // We require either a "value" or a "binaryValue" entry. 196 // We require either a "value" or a "binaryValue" entry.
169 if (!(header_value->HasKey(keys::kHeaderValueKey) ^ 197 if (!(header_value->HasKey(keys::kHeaderValueKey) ^
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 return false; 430 return false;
403 431
404 if (str == "requestHeaders") 432 if (str == "requestHeaders")
405 *extra_info_spec |= REQUEST_HEADERS; 433 *extra_info_spec |= REQUEST_HEADERS;
406 else if (str == "responseHeaders") 434 else if (str == "responseHeaders")
407 *extra_info_spec |= RESPONSE_HEADERS; 435 *extra_info_spec |= RESPONSE_HEADERS;
408 else if (str == "blocking") 436 else if (str == "blocking")
409 *extra_info_spec |= BLOCKING; 437 *extra_info_spec |= BLOCKING;
410 else if (str == "asyncBlocking") 438 else if (str == "asyncBlocking")
411 *extra_info_spec |= ASYNC_BLOCKING; 439 *extra_info_spec |= ASYNC_BLOCKING;
440 else if (str == "postData")
441 *extra_info_spec |= (IsPostDataEnabled()) ? POST_DATA : 0;
wtc 2012/08/01 17:24:41 Nit: you can also write this as else if (str ==
vabr (Chromium) 2012/08/02 09:15:15 Actually, that would be different. The current cod
Matt Perry 2012/08/02 10:11:03 I'm torn, so let's just go with what you have.
wtc 2012/08/02 21:58:49 You're right. My suggested change was wrong. Ple
412 else 442 else
413 return false; 443 return false;
414 444
415 // BLOCKING and ASYNC_BLOCKING are mutually exclusive. 445 // BLOCKING and ASYNC_BLOCKING are mutually exclusive.
416 if ((*extra_info_spec & BLOCKING) && (*extra_info_spec & ASYNC_BLOCKING)) 446 if ((*extra_info_spec & BLOCKING) && (*extra_info_spec & ASYNC_BLOCKING))
417 return false; 447 return false;
418 } 448 }
419 return true; 449 return true;
420 } 450 }
421 451
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 521
492 int extra_info_spec = 0; 522 int extra_info_spec = 0;
493 std::vector<const EventListener*> listeners = 523 std::vector<const EventListener*> listeners =
494 GetMatchingListeners(profile, extension_info_map, keys::kOnBeforeRequest, 524 GetMatchingListeners(profile, extension_info_map, keys::kOnBeforeRequest,
495 request, &extra_info_spec); 525 request, &extra_info_spec);
496 if (!listeners.empty() && 526 if (!listeners.empty() &&
497 !GetAndSetSignaled(request->identifier(), kOnBeforeRequest)) { 527 !GetAndSetSignaled(request->identifier(), kOnBeforeRequest)) {
498 ListValue args; 528 ListValue args;
499 DictionaryValue* dict = new DictionaryValue(); 529 DictionaryValue* dict = new DictionaryValue();
500 ExtractRequestInfo(request, dict); 530 ExtractRequestInfo(request, dict);
531 if (extra_info_spec & ExtraInfoSpec::POST_DATA)
532 ExtractRequestInfoPost(request, dict);
501 args.Append(dict); 533 args.Append(dict);
502 534
503 initialize_blocked_requests |= 535 initialize_blocked_requests |=
504 DispatchEvent(profile, request, listeners, args); 536 DispatchEvent(profile, request, listeners, args);
505 } 537 }
506 538
507 if (!initialize_blocked_requests) 539 if (!initialize_blocked_requests)
508 return net::OK; // Nobody saw a reason for modifying the request. 540 return net::OK; // Nobody saw a reason for modifying the request.
509 541
510 blocked_requests_[request->identifier()].event = kOnBeforeRequest; 542 blocked_requests_[request->identifier()].event = kOnBeforeRequest;
(...skipping 1309 matching lines...) Expand 10 before | Expand all | Expand 10 after
1820 } else if ((*it)->name().find("AdBlock") != std::string::npos) { 1852 } else if ((*it)->name().find("AdBlock") != std::string::npos) {
1821 adblock = true; 1853 adblock = true;
1822 } else { 1854 } else {
1823 other = true; 1855 other = true;
1824 } 1856 }
1825 } 1857 }
1826 } 1858 }
1827 1859
1828 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other)); 1860 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other));
1829 } 1861 }
1862
1863 bool IsPostDataEnabled() {
1864 static bool cached = false;
1865 static bool enabled = false;
1866 if (!cached) {
1867 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
Matt Perry 2012/08/01 09:46:31 channel is a POD (it's just an enum), so you can m
wtc 2012/08/01 17:24:41 mpcomplete's suggestion is correct. Note that nei
vabr (Chromium) 2012/08/01 18:03:22 Thanks, I learned something new! :) However, due
1868 enabled = channel == chrome::VersionInfo::CHANNEL_UNKNOWN ||
1869 channel == chrome::VersionInfo::CHANNEL_CANARY ||
1870 channel == chrome::VersionInfo::CHANNEL_DEV;
1871 cached = true;
1872 }
1873 return enabled;
1874 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698