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. |
| 92 bool IsWebRequestBodyDataAccessEnabled() { |
| 93 return Feature::GetCurrentChannel() <= VersionInfo::CHANNEL_DEV; |
| 94 } |
| 95 |
81 bool IsWebRequestEvent(const std::string& event_name) { | 96 bool IsWebRequestEvent(const std::string& event_name) { |
82 return std::find(kWebRequestEvents, ARRAYEND(kWebRequestEvents), | 97 return std::find(kWebRequestEvents, ARRAYEND(kWebRequestEvents), |
83 event_name) != ARRAYEND(kWebRequestEvents); | 98 event_name) != ARRAYEND(kWebRequestEvents); |
84 } | 99 } |
85 | 100 |
86 // Returns whether |request| has been triggered by an extension in | 101 // Returns whether |request| has been triggered by an extension in |
87 // |extension_info_map|. | 102 // |extension_info_map|. |
88 bool IsRequestFromExtension(const net::URLRequest* request, | 103 bool IsRequestFromExtension(const net::URLRequest* request, |
89 const ExtensionInfoMap* extension_info_map) { | 104 const ExtensionInfoMap* extension_info_map) { |
90 // |extension_info_map| is NULL for system-level requests. | 105 // |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())); | 166 base::Uint64ToString(request->identifier())); |
152 out->SetString(keys::kUrlKey, request->url().spec()); | 167 out->SetString(keys::kUrlKey, request->url().spec()); |
153 out->SetString(keys::kMethodKey, request->method()); | 168 out->SetString(keys::kMethodKey, request->method()); |
154 out->SetInteger(keys::kFrameIdKey, frame_id_for_extension); | 169 out->SetInteger(keys::kFrameIdKey, frame_id_for_extension); |
155 out->SetInteger(keys::kParentFrameIdKey, parent_frame_id_for_extension); | 170 out->SetInteger(keys::kParentFrameIdKey, parent_frame_id_for_extension); |
156 out->SetInteger(keys::kTabIdKey, tab_id); | 171 out->SetInteger(keys::kTabIdKey, tab_id); |
157 out->SetString(keys::kTypeKey, helpers::ResourceTypeToString(resource_type)); | 172 out->SetString(keys::kTypeKey, helpers::ResourceTypeToString(resource_type)); |
158 out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000); | 173 out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000); |
159 } | 174 } |
160 | 175 |
| 176 // Extracts the body from |request| and writes the data into |out|. |
| 177 void ExtractRequestInfoBody(const net::URLRequest* request, |
| 178 DictionaryValue* out) { |
| 179 if (request->method() != "POST" && request->method() != "PUT") |
| 180 return; // Need to exit without "out->Set(keys::kRequestBodyKey, ...);" . |
| 181 |
| 182 DictionaryValue* requestBody = new DictionaryValue(); |
| 183 out->Set(keys::kRequestBodyKey, requestBody); |
| 184 |
| 185 // Get the data presenters, ordered by how specific they are. |
| 186 extensions::ParsedDataPresenter parsed_data_presenter(request); |
| 187 extensions::RawDataPresenter raw_data_presenter; |
| 188 extensions::UploadDataPresenter* const presenters[] = { |
| 189 &parsed_data_presenter, // 1: any parseable forms? (Specific to forms.) |
| 190 &raw_data_presenter // 2: any data at all? (Non-specific.) |
| 191 }; |
| 192 // Keys for the results of the corresponding presenters. |
| 193 static const char* const kKeys[] = { |
| 194 keys::kRequestBodyFormDataKey, |
| 195 keys::kRequestBodyRawKey |
| 196 }; |
| 197 |
| 198 const std::vector<net::UploadElement>* elements = |
| 199 request->get_upload()->elements(); |
| 200 bool some_succeeded = false; |
| 201 for (size_t i = 0; !some_succeeded && i < arraysize(presenters); ++i) { |
| 202 std::vector<net::UploadElement>::const_iterator element; |
| 203 for (element = elements->begin(); element != elements->end(); ++element) |
| 204 presenters[i]->FeedNext(*element); |
| 205 if (presenters[i]->Succeeded()) { |
| 206 requestBody->Set(kKeys[i], presenters[i]->Result().release()); |
| 207 some_succeeded = true; |
| 208 } |
| 209 } |
| 210 if (!some_succeeded) |
| 211 requestBody->SetString(keys::kRequestBodyErrorKey, "Unknown error."); |
| 212 } |
| 213 |
161 // Converts a HttpHeaders dictionary to a |name|, |value| pair. Returns | 214 // Converts a HttpHeaders dictionary to a |name|, |value| pair. Returns |
162 // true if successful. | 215 // true if successful. |
163 bool FromHeaderDictionary(const DictionaryValue* header_value, | 216 bool FromHeaderDictionary(const DictionaryValue* header_value, |
164 std::string* name, | 217 std::string* name, |
165 std::string* value) { | 218 std::string* value) { |
166 if (!header_value->GetString(keys::kHeaderNameKey, name)) | 219 if (!header_value->GetString(keys::kHeaderNameKey, name)) |
167 return false; | 220 return false; |
168 | 221 |
169 // We require either a "value" or a "binaryValue" entry. | 222 // We require either a "value" or a "binaryValue" entry. |
170 if (!(header_value->HasKey(keys::kHeaderValueKey) ^ | 223 if (!(header_value->HasKey(keys::kHeaderValueKey) ^ |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 return false; | 452 return false; |
400 | 453 |
401 if (str == "requestHeaders") | 454 if (str == "requestHeaders") |
402 *extra_info_spec |= REQUEST_HEADERS; | 455 *extra_info_spec |= REQUEST_HEADERS; |
403 else if (str == "responseHeaders") | 456 else if (str == "responseHeaders") |
404 *extra_info_spec |= RESPONSE_HEADERS; | 457 *extra_info_spec |= RESPONSE_HEADERS; |
405 else if (str == "blocking") | 458 else if (str == "blocking") |
406 *extra_info_spec |= BLOCKING; | 459 *extra_info_spec |= BLOCKING; |
407 else if (str == "asyncBlocking") | 460 else if (str == "asyncBlocking") |
408 *extra_info_spec |= ASYNC_BLOCKING; | 461 *extra_info_spec |= ASYNC_BLOCKING; |
| 462 else if (str == "requestBody") |
| 463 *extra_info_spec |= |
| 464 IsWebRequestBodyDataAccessEnabled() ? REQUEST_BODY : 0; |
409 else | 465 else |
410 return false; | 466 return false; |
411 | 467 |
412 // BLOCKING and ASYNC_BLOCKING are mutually exclusive. | 468 // BLOCKING and ASYNC_BLOCKING are mutually exclusive. |
413 if ((*extra_info_spec & BLOCKING) && (*extra_info_spec & ASYNC_BLOCKING)) | 469 if ((*extra_info_spec & BLOCKING) && (*extra_info_spec & ASYNC_BLOCKING)) |
414 return false; | 470 return false; |
415 } | 471 } |
416 return true; | 472 return true; |
417 } | 473 } |
418 | 474 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 | 545 |
490 int extra_info_spec = 0; | 546 int extra_info_spec = 0; |
491 std::vector<const EventListener*> listeners = | 547 std::vector<const EventListener*> listeners = |
492 GetMatchingListeners(profile, extension_info_map, keys::kOnBeforeRequest, | 548 GetMatchingListeners(profile, extension_info_map, keys::kOnBeforeRequest, |
493 request, &extra_info_spec); | 549 request, &extra_info_spec); |
494 if (!listeners.empty() && | 550 if (!listeners.empty() && |
495 !GetAndSetSignaled(request->identifier(), kOnBeforeRequest)) { | 551 !GetAndSetSignaled(request->identifier(), kOnBeforeRequest)) { |
496 ListValue args; | 552 ListValue args; |
497 DictionaryValue* dict = new DictionaryValue(); | 553 DictionaryValue* dict = new DictionaryValue(); |
498 ExtractRequestInfo(request, dict); | 554 ExtractRequestInfo(request, dict); |
| 555 if (extra_info_spec & ExtraInfoSpec::REQUEST_BODY) |
| 556 ExtractRequestInfoBody(request, dict); |
499 args.Append(dict); | 557 args.Append(dict); |
500 | 558 |
501 initialize_blocked_requests |= | 559 initialize_blocked_requests |= |
502 DispatchEvent(profile, request, listeners, args); | 560 DispatchEvent(profile, request, listeners, args); |
503 } | 561 } |
504 | 562 |
505 if (!initialize_blocked_requests) | 563 if (!initialize_blocked_requests) |
506 return net::OK; // Nobody saw a reason for modifying the request. | 564 return net::OK; // Nobody saw a reason for modifying the request. |
507 | 565 |
508 blocked_requests_[request->identifier()].event = kOnBeforeRequest; | 566 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) { | 1898 } else if ((*it)->name().find("AdBlock") != std::string::npos) { |
1841 adblock = true; | 1899 adblock = true; |
1842 } else { | 1900 } else { |
1843 other = true; | 1901 other = true; |
1844 } | 1902 } |
1845 } | 1903 } |
1846 } | 1904 } |
1847 | 1905 |
1848 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other)); | 1906 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other)); |
1849 } | 1907 } |
OLD | NEW |