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/upload_data_presenter.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/common/extensions/api/web_request.h" | 34 #include "chrome/common/extensions/api/web_request.h" |
34 #include "chrome/common/extensions/event_filtering_info.h" | 35 #include "chrome/common/extensions/event_filtering_info.h" |
35 #include "chrome/common/extensions/extension.h" | 36 #include "chrome/common/extensions/extension.h" |
36 #include "chrome/common/extensions/extension_constants.h" | 37 #include "chrome/common/extensions/extension_constants.h" |
37 #include "chrome/common/extensions/extension_error_utils.h" | 38 #include "chrome/common/extensions/extension_error_utils.h" |
38 #include "chrome/common/extensions/extension_messages.h" | 39 #include "chrome/common/extensions/extension_messages.h" |
40 #include "chrome/common/extensions/features/feature.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" |
44 #include "content/public/browser/resource_request_info.h" | 46 #include "content/public/browser/resource_request_info.h" |
45 #include "googleurl/src/gurl.h" | 47 #include "googleurl/src/gurl.h" |
46 #include "grit/generated_resources.h" | 48 #include "grit/generated_resources.h" |
47 #include "net/base/auth.h" | 49 #include "net/base/auth.h" |
48 #include "net/base/net_errors.h" | 50 #include "net/base/net_errors.h" |
51 #include "net/base/upload_data.h" | |
52 #include "net/base/upload_element.h" | |
49 #include "net/http/http_response_headers.h" | 53 #include "net/http/http_response_headers.h" |
50 #include "net/url_request/url_request.h" | 54 #include "net/url_request/url_request.h" |
51 #include "ui/base/l10n/l10n_util.h" | 55 #include "ui/base/l10n/l10n_util.h" |
52 | 56 |
57 using base::DictionaryValue; | |
58 using base::ListValue; | |
59 using base::StringValue; | |
60 using chrome::VersionInfo; | |
53 using content::BrowserMessageFilter; | 61 using content::BrowserMessageFilter; |
54 using content::BrowserThread; | 62 using content::BrowserThread; |
55 using content::ResourceRequestInfo; | 63 using content::ResourceRequestInfo; |
56 using extensions::Extension; | 64 using extensions::Extension; |
65 using extensions::Feature; | |
57 | 66 |
58 using extensions::web_navigation_api_helpers::GetFrameId; | 67 using extensions::web_navigation_api_helpers::GetFrameId; |
59 | 68 |
60 namespace helpers = extension_web_request_api_helpers; | 69 namespace helpers = extension_web_request_api_helpers; |
61 namespace keys = extension_web_request_api_constants; | 70 namespace keys = extension_web_request_api_constants; |
62 namespace web_request = extensions::api::web_request; | 71 namespace web_request = extensions::api::web_request; |
63 | 72 |
64 namespace { | 73 namespace { |
65 | 74 |
66 // List of all the webRequest events. | 75 // List of all the webRequest events. |
67 static const char* const kWebRequestEvents[] = { | 76 const char* const kWebRequestEvents[] = { |
68 keys::kOnBeforeRedirect, | 77 keys::kOnBeforeRedirect, |
69 keys::kOnBeforeRequest, | 78 keys::kOnBeforeRequest, |
70 keys::kOnBeforeSendHeaders, | 79 keys::kOnBeforeSendHeaders, |
71 keys::kOnCompleted, | 80 keys::kOnCompleted, |
72 keys::kOnErrorOccurred, | 81 keys::kOnErrorOccurred, |
73 keys::kOnSendHeaders, | 82 keys::kOnSendHeaders, |
74 keys::kOnAuthRequired, | 83 keys::kOnAuthRequired, |
75 keys::kOnResponseStarted, | 84 keys::kOnResponseStarted, |
76 keys::kOnHeadersReceived, | 85 keys::kOnHeadersReceived, |
77 }; | 86 }; |
78 | 87 |
79 #define ARRAYEND(array) (array + arraysize(array)) | 88 #define ARRAYEND(array) (array + arraysize(array)) |
80 | 89 |
90 // Access to request body (crbug.com/91191/) is currently only enabled in dev | |
91 // and canary channels. This function caches the release channel info from | |
92 // chrome::VersionInfo. Don't use this outside IO thread. | |
battre
2012/09/09 22:08:35
where is this caching? I don't understand why we c
vabr (Chromium)
2012/09/12 11:18:08
Thanks for catching that. The comment was obsolete
| |
93 bool IsWebRequestBodyDataAccessEnabled() { | |
94 return Feature::GetCurrentChannel() <= VersionInfo::CHANNEL_DEV; | |
95 } | |
96 | |
81 bool IsWebRequestEvent(const std::string& event_name) { | 97 bool IsWebRequestEvent(const std::string& event_name) { |
82 return std::find(kWebRequestEvents, ARRAYEND(kWebRequestEvents), | 98 return std::find(kWebRequestEvents, ARRAYEND(kWebRequestEvents), |
83 event_name) != ARRAYEND(kWebRequestEvents); | 99 event_name) != ARRAYEND(kWebRequestEvents); |
84 } | 100 } |
85 | 101 |
86 // Returns whether |request| has been triggered by an extension in | 102 // Returns whether |request| has been triggered by an extension in |
87 // |extension_info_map|. | 103 // |extension_info_map|. |
88 bool IsRequestFromExtension(const net::URLRequest* request, | 104 bool IsRequestFromExtension(const net::URLRequest* request, |
89 const ExtensionInfoMap* extension_info_map) { | 105 const ExtensionInfoMap* extension_info_map) { |
90 // |extension_info_map| is NULL for system-level requests. | 106 // |extension_info_map| is NULL for system-level requests. |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 base::Uint64ToString(request->identifier())); | 167 base::Uint64ToString(request->identifier())); |
152 out->SetString(keys::kUrlKey, request->url().spec()); | 168 out->SetString(keys::kUrlKey, request->url().spec()); |
153 out->SetString(keys::kMethodKey, request->method()); | 169 out->SetString(keys::kMethodKey, request->method()); |
154 out->SetInteger(keys::kFrameIdKey, frame_id_for_extension); | 170 out->SetInteger(keys::kFrameIdKey, frame_id_for_extension); |
155 out->SetInteger(keys::kParentFrameIdKey, parent_frame_id_for_extension); | 171 out->SetInteger(keys::kParentFrameIdKey, parent_frame_id_for_extension); |
156 out->SetInteger(keys::kTabIdKey, tab_id); | 172 out->SetInteger(keys::kTabIdKey, tab_id); |
157 out->SetString(keys::kTypeKey, helpers::ResourceTypeToString(resource_type)); | 173 out->SetString(keys::kTypeKey, helpers::ResourceTypeToString(resource_type)); |
158 out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000); | 174 out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000); |
159 } | 175 } |
160 | 176 |
177 // Extracts the body from |request| and writes the data into |out|. | |
178 void ExtractRequestInfoBody(const net::URLRequest* request, | |
179 DictionaryValue* out) { | |
180 if (request->method() != "POST" && request->method() != "PUT") | |
181 return; // Need to exit without "out->Set(keys::kRequestBodyKey, ...);" . | |
182 | |
183 DictionaryValue* requestBody = new DictionaryValue(); | |
184 out->Set(keys::kRequestBodyKey, requestBody); | |
185 | |
186 // Get the data presenters, ordered by being interesting. | |
battre
2012/09/09 22:08:35
"ordered by being interesting"? grammar and what d
vabr (Chromium)
2012/09/12 11:18:08
I refined that comment, and also corrected the two
| |
187 extensions::RawDataPresenter raw_data_presenter; | |
188 extensions::ParsedDataPresenter parsed_data_presenter(request); | |
battre
2012/09/09 22:08:35
nit: swap previous two lines?
vabr (Chromium)
2012/09/12 11:18:08
Done.
| |
189 extensions::UploadDataPresenter* const presenters[] = { | |
190 &parsed_data_presenter, // Second -- any parseable forms? | |
191 &raw_data_presenter // Third -- any data at all? | |
192 }; | |
193 // Keys for the results of the corresponding presenters. | |
194 static const char* const kKeys[] = { | |
195 keys::kRequestBodyFormDataKey, | |
196 keys::kRequestBodyRawKey | |
197 }; | |
198 | |
199 const std::vector<net::UploadElement>* elements = | |
200 request->get_upload()->elements(); | |
201 bool some_succeeded = false; | |
202 for (size_t i = 0; !some_succeeded && i < arraysize(presenters); ++i) { | |
203 std::vector<net::UploadElement>::const_iterator element; | |
204 for (element = elements->begin(); element != elements->end(); ++element) | |
205 presenters[i]->FeedNext(*element); | |
206 if (presenters[i]->Succeeded()) { | |
207 requestBody->Set(kKeys[i], presenters[i]->Result().release()); | |
208 some_succeeded = true; | |
209 } | |
210 } | |
211 if (!some_succeeded) | |
212 requestBody->SetString(keys::kRequestBodyErrorKey, "Unknown error."); | |
213 } | |
214 | |
161 // Converts a HttpHeaders dictionary to a |name|, |value| pair. Returns | 215 // Converts a HttpHeaders dictionary to a |name|, |value| pair. Returns |
162 // true if successful. | 216 // true if successful. |
163 bool FromHeaderDictionary(const DictionaryValue* header_value, | 217 bool FromHeaderDictionary(const DictionaryValue* header_value, |
164 std::string* name, | 218 std::string* name, |
165 std::string* value) { | 219 std::string* value) { |
166 if (!header_value->GetString(keys::kHeaderNameKey, name)) | 220 if (!header_value->GetString(keys::kHeaderNameKey, name)) |
167 return false; | 221 return false; |
168 | 222 |
169 // We require either a "value" or a "binaryValue" entry. | 223 // We require either a "value" or a "binaryValue" entry. |
170 if (!(header_value->HasKey(keys::kHeaderValueKey) ^ | 224 if (!(header_value->HasKey(keys::kHeaderValueKey) ^ |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
399 return false; | 453 return false; |
400 | 454 |
401 if (str == "requestHeaders") | 455 if (str == "requestHeaders") |
402 *extra_info_spec |= REQUEST_HEADERS; | 456 *extra_info_spec |= REQUEST_HEADERS; |
403 else if (str == "responseHeaders") | 457 else if (str == "responseHeaders") |
404 *extra_info_spec |= RESPONSE_HEADERS; | 458 *extra_info_spec |= RESPONSE_HEADERS; |
405 else if (str == "blocking") | 459 else if (str == "blocking") |
406 *extra_info_spec |= BLOCKING; | 460 *extra_info_spec |= BLOCKING; |
407 else if (str == "asyncBlocking") | 461 else if (str == "asyncBlocking") |
408 *extra_info_spec |= ASYNC_BLOCKING; | 462 *extra_info_spec |= ASYNC_BLOCKING; |
463 else if (str == "requestBody") | |
464 *extra_info_spec |= | |
465 IsWebRequestBodyDataAccessEnabled() ? REQUEST_BODY : 0; | |
409 else | 466 else |
410 return false; | 467 return false; |
411 | 468 |
412 // BLOCKING and ASYNC_BLOCKING are mutually exclusive. | 469 // BLOCKING and ASYNC_BLOCKING are mutually exclusive. |
413 if ((*extra_info_spec & BLOCKING) && (*extra_info_spec & ASYNC_BLOCKING)) | 470 if ((*extra_info_spec & BLOCKING) && (*extra_info_spec & ASYNC_BLOCKING)) |
414 return false; | 471 return false; |
415 } | 472 } |
416 return true; | 473 return true; |
417 } | 474 } |
418 | 475 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
489 | 546 |
490 int extra_info_spec = 0; | 547 int extra_info_spec = 0; |
491 std::vector<const EventListener*> listeners = | 548 std::vector<const EventListener*> listeners = |
492 GetMatchingListeners(profile, extension_info_map, keys::kOnBeforeRequest, | 549 GetMatchingListeners(profile, extension_info_map, keys::kOnBeforeRequest, |
493 request, &extra_info_spec); | 550 request, &extra_info_spec); |
494 if (!listeners.empty() && | 551 if (!listeners.empty() && |
495 !GetAndSetSignaled(request->identifier(), kOnBeforeRequest)) { | 552 !GetAndSetSignaled(request->identifier(), kOnBeforeRequest)) { |
496 ListValue args; | 553 ListValue args; |
497 DictionaryValue* dict = new DictionaryValue(); | 554 DictionaryValue* dict = new DictionaryValue(); |
498 ExtractRequestInfo(request, dict); | 555 ExtractRequestInfo(request, dict); |
556 if (extra_info_spec & ExtraInfoSpec::REQUEST_BODY) | |
557 ExtractRequestInfoBody(request, dict); | |
499 args.Append(dict); | 558 args.Append(dict); |
500 | 559 |
501 initialize_blocked_requests |= | 560 initialize_blocked_requests |= |
502 DispatchEvent(profile, request, listeners, args); | 561 DispatchEvent(profile, request, listeners, args); |
503 } | 562 } |
504 | 563 |
505 if (!initialize_blocked_requests) | 564 if (!initialize_blocked_requests) |
506 return net::OK; // Nobody saw a reason for modifying the request. | 565 return net::OK; // Nobody saw a reason for modifying the request. |
507 | 566 |
508 blocked_requests_[request->identifier()].event = kOnBeforeRequest; | 567 blocked_requests_[request->identifier()].event = kOnBeforeRequest; |
(...skipping 1331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1840 } else if ((*it)->name().find("AdBlock") != std::string::npos) { | 1899 } else if ((*it)->name().find("AdBlock") != std::string::npos) { |
1841 adblock = true; | 1900 adblock = true; |
1842 } else { | 1901 } else { |
1843 other = true; | 1902 other = true; |
1844 } | 1903 } |
1845 } | 1904 } |
1846 } | 1905 } |
1847 | 1906 |
1848 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other)); | 1907 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other)); |
1849 } | 1908 } |
OLD | NEW |