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/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" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 base::Uint64ToString(request->identifier())); | 151 base::Uint64ToString(request->identifier())); |
151 out->SetString(keys::kUrlKey, request->url().spec()); | 152 out->SetString(keys::kUrlKey, request->url().spec()); |
152 out->SetString(keys::kMethodKey, request->method()); | 153 out->SetString(keys::kMethodKey, request->method()); |
153 out->SetInteger(keys::kFrameIdKey, frame_id_for_extension); | 154 out->SetInteger(keys::kFrameIdKey, frame_id_for_extension); |
154 out->SetInteger(keys::kParentFrameIdKey, parent_frame_id_for_extension); | 155 out->SetInteger(keys::kParentFrameIdKey, parent_frame_id_for_extension); |
155 out->SetInteger(keys::kTabIdKey, tab_id); | 156 out->SetInteger(keys::kTabIdKey, tab_id); |
156 out->SetString(keys::kTypeKey, helpers::ResourceTypeToString(resource_type)); | 157 out->SetString(keys::kTypeKey, helpers::ResourceTypeToString(resource_type)); |
157 out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000); | 158 out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000); |
158 } | 159 } |
159 | 160 |
161 // Extracts the POST data from |request| and writes the data into |out|. | |
162 // This can be expensive, so it's separated from ExtractRequestInfo(). | |
163 void ExtractRequestInfoPost(const net::URLRequest* request, | |
164 DictionaryValue* out) { | |
165 if (request->method() != "POST") | |
166 return; // Need to exit without "out->Set(keys::kPostDataKey, ...);" . | |
167 | |
168 DictionaryValue* post_data = new DictionaryValue(); | |
169 out->Set(keys::kPostDataKey, post_data); | |
170 | |
171 // Chunked encoding (defined in RFC 2616) is currently not supported. | |
172 std::string transfer_encoding; | |
173 if (request->extra_request_headers().GetHeader( | |
174 net::HttpRequestHeaders::kTransferEncoding, &transfer_encoding)) { | |
175 if (base::strcasecmp(transfer_encoding.c_str(), "chunked") == 0) { | |
176 StringValue* error = new StringValue("chunked_encoding"); | |
177 post_data->Set(keys::kPostDataErrorKey, error); | |
battre
2012/07/30 17:54:25
nit: post_data->SetString(keys::kPostDataErrorKey,
vabr (Chromium)
2012/07/31 09:03:18
Done.
| |
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 Loading... | |
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") | |
battre
2012/07/30 17:54:25
I think at this place we need to check whether we
Matt Perry
2012/07/31 09:14:12
Good catch. I was thinking we could do it through
| |
441 *extra_info_spec |= POST_DATA; | |
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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
487 | 517 |
488 int extra_info_spec = 0; | 518 int extra_info_spec = 0; |
489 std::vector<const EventListener*> listeners = | 519 std::vector<const EventListener*> listeners = |
490 GetMatchingListeners(profile, extension_info_map, keys::kOnBeforeRequest, | 520 GetMatchingListeners(profile, extension_info_map, keys::kOnBeforeRequest, |
491 request, &extra_info_spec); | 521 request, &extra_info_spec); |
492 if (!listeners.empty() && | 522 if (!listeners.empty() && |
493 !GetAndSetSignaled(request->identifier(), kOnBeforeRequest)) { | 523 !GetAndSetSignaled(request->identifier(), kOnBeforeRequest)) { |
494 ListValue args; | 524 ListValue args; |
495 DictionaryValue* dict = new DictionaryValue(); | 525 DictionaryValue* dict = new DictionaryValue(); |
496 ExtractRequestInfo(request, dict); | 526 ExtractRequestInfo(request, dict); |
527 if (extra_info_spec & ExtraInfoSpec::POST_DATA) | |
528 ExtractRequestInfoPost(request, dict); | |
497 args.Append(dict); | 529 args.Append(dict); |
498 | 530 |
499 initialize_blocked_requests |= | 531 initialize_blocked_requests |= |
500 DispatchEvent(profile, request, listeners, args); | 532 DispatchEvent(profile, request, listeners, args); |
501 } | 533 } |
502 | 534 |
503 if (!initialize_blocked_requests) | 535 if (!initialize_blocked_requests) |
504 return net::OK; // Nobody saw a reason for modifying the request. | 536 return net::OK; // Nobody saw a reason for modifying the request. |
505 | 537 |
506 blocked_requests_[request->identifier()].event = kOnBeforeRequest; | 538 blocked_requests_[request->identifier()].event = kOnBeforeRequest; |
(...skipping 1280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1787 } else if ((*it)->name().find("AdBlock") != std::string::npos) { | 1819 } else if ((*it)->name().find("AdBlock") != std::string::npos) { |
1788 adblock = true; | 1820 adblock = true; |
1789 } else { | 1821 } else { |
1790 other = true; | 1822 other = true; |
1791 } | 1823 } |
1792 } | 1824 } |
1793 } | 1825 } |
1794 | 1826 |
1795 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other)); | 1827 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other)); |
1796 } | 1828 } |
OLD | NEW |