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 11 matching lines...) Expand all Loading... | |
43 #include "content/public/browser/render_process_host.h" | 44 #include "content/public/browser/render_process_host.h" |
44 #include "content/public/browser/resource_request_info.h" | 45 #include "content/public/browser/resource_request_info.h" |
45 #include "googleurl/src/gurl.h" | 46 #include "googleurl/src/gurl.h" |
46 #include "grit/generated_resources.h" | 47 #include "grit/generated_resources.h" |
47 #include "net/base/auth.h" | 48 #include "net/base/auth.h" |
48 #include "net/base/net_errors.h" | 49 #include "net/base/net_errors.h" |
49 #include "net/http/http_response_headers.h" | 50 #include "net/http/http_response_headers.h" |
50 #include "net/url_request/url_request.h" | 51 #include "net/url_request/url_request.h" |
51 #include "ui/base/l10n/l10n_util.h" | 52 #include "ui/base/l10n/l10n_util.h" |
52 | 53 |
54 using chrome::VersionInfo; | |
53 using content::BrowserMessageFilter; | 55 using content::BrowserMessageFilter; |
54 using content::BrowserThread; | 56 using content::BrowserThread; |
55 using content::ResourceRequestInfo; | 57 using content::ResourceRequestInfo; |
56 using extensions::Extension; | 58 using extensions::Extension; |
57 | 59 |
58 using extensions::web_navigation_api_helpers::GetFrameId; | 60 using extensions::web_navigation_api_helpers::GetFrameId; |
59 | 61 |
60 namespace helpers = extension_web_request_api_helpers; | 62 namespace helpers = extension_web_request_api_helpers; |
61 namespace keys = extension_web_request_api_constants; | 63 namespace keys = extension_web_request_api_constants; |
62 | 64 |
63 namespace { | 65 namespace { |
64 | 66 |
65 // List of all the webRequest events. | 67 // List of all the webRequest events. |
66 static const char* const kWebRequestEvents[] = { | 68 const char* const kWebRequestEvents[] = { |
67 keys::kOnBeforeRedirect, | 69 keys::kOnBeforeRedirect, |
68 keys::kOnBeforeRequest, | 70 keys::kOnBeforeRequest, |
69 keys::kOnBeforeSendHeaders, | 71 keys::kOnBeforeSendHeaders, |
70 keys::kOnCompleted, | 72 keys::kOnCompleted, |
71 keys::kOnErrorOccurred, | 73 keys::kOnErrorOccurred, |
72 keys::kOnSendHeaders, | 74 keys::kOnSendHeaders, |
73 keys::kOnAuthRequired, | 75 keys::kOnAuthRequired, |
74 keys::kOnResponseStarted, | 76 keys::kOnResponseStarted, |
75 keys::kOnHeadersReceived, | 77 keys::kOnHeadersReceived, |
76 }; | 78 }; |
77 | 79 |
78 #define ARRAYEND(array) (array + arraysize(array)) | 80 #define ARRAYEND(array) (array + arraysize(array)) |
79 | 81 |
82 bool g_channel_cached = false; | |
83 VersionInfo::Channel g_channel = VersionInfo::CHANNEL_UNKNOWN; | |
84 | |
85 // Access to request body (crbug.com/91191/) is currently only enabled in dev | |
86 // and canary channels. This function caches the release channel info from | |
87 // chrome::VersionInfo. Don't use this outside IO thread. | |
88 bool IsWebRequestBodyDataAccessEnabled() { | |
89 if (!g_channel_cached) { | |
90 g_channel = VersionInfo::GetChannel(); | |
91 g_channel_cached = true; | |
92 } | |
93 return g_channel <= VersionInfo::CHANNEL_DEV; | |
94 } | |
95 | |
80 bool IsWebRequestEvent(const std::string& event_name) { | 96 bool IsWebRequestEvent(const std::string& event_name) { |
81 return std::find(kWebRequestEvents, ARRAYEND(kWebRequestEvents), | 97 return std::find(kWebRequestEvents, ARRAYEND(kWebRequestEvents), |
82 event_name) != ARRAYEND(kWebRequestEvents); | 98 event_name) != ARRAYEND(kWebRequestEvents); |
83 } | 99 } |
84 | 100 |
85 // Returns whether |request| has been triggered by an extension in | 101 // Returns whether |request| has been triggered by an extension in |
86 // |extension_info_map|. | 102 // |extension_info_map|. |
87 bool IsRequestFromExtension(const net::URLRequest* request, | 103 bool IsRequestFromExtension(const net::URLRequest* request, |
88 const ExtensionInfoMap* extension_info_map) { | 104 const ExtensionInfoMap* extension_info_map) { |
89 // |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... | |
150 base::Uint64ToString(request->identifier())); | 166 base::Uint64ToString(request->identifier())); |
151 out->SetString(keys::kUrlKey, request->url().spec()); | 167 out->SetString(keys::kUrlKey, request->url().spec()); |
152 out->SetString(keys::kMethodKey, request->method()); | 168 out->SetString(keys::kMethodKey, request->method()); |
153 out->SetInteger(keys::kFrameIdKey, frame_id_for_extension); | 169 out->SetInteger(keys::kFrameIdKey, frame_id_for_extension); |
154 out->SetInteger(keys::kParentFrameIdKey, parent_frame_id_for_extension); | 170 out->SetInteger(keys::kParentFrameIdKey, parent_frame_id_for_extension); |
155 out->SetInteger(keys::kTabIdKey, tab_id); | 171 out->SetInteger(keys::kTabIdKey, tab_id); |
156 out->SetString(keys::kTypeKey, helpers::ResourceTypeToString(resource_type)); | 172 out->SetString(keys::kTypeKey, helpers::ResourceTypeToString(resource_type)); |
157 out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000); | 173 out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000); |
158 } | 174 } |
159 | 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::kBodyKey, ...);" . | |
181 | |
182 DictionaryValue* body = new DictionaryValue(); | |
183 out->Set(keys::kBodyKey, body); | |
184 | |
185 // Get the data producers, ordered by being interesting. | |
186 extensions::ChunkedErrorProducer chunked_error_producer(request); | |
187 extensions::RawDataProducer raw_data_producer; | |
188 extensions::ParsedDataProducer parsed_data_producer(request); | |
189 extensions::RequestDataRepresentationProducer* const kProducers[] = { | |
Matt Perry
2012/08/06 21:06:45
just "producers". While "const", this variable doe
vabr (Chromium)
2012/08/10 17:12:55
Done.
| |
190 &chunked_error_producer, // First -- any errors? | |
191 &parsed_data_producer, // Second -- any parseable forms? | |
192 &raw_data_producer // Third -- any data at all? | |
193 }; | |
194 // Keys for the results of the corresponding producers. | |
195 static const char* const kKeys[] = { | |
196 keys::kBodyErrorKey, | |
197 keys::kBodyParsedFormKey, | |
198 keys::kBodyRawKey | |
199 }; | |
200 | |
201 const std::vector<net::UploadData::Element>* elements = | |
202 request->get_upload()->elements(); | |
203 bool some_succeeded = false; | |
204 for (size_t n = 0; !some_succeeded && n < arraysize(kProducers); ++n) { | |
wtc
2012/08/09 23:39:40
Nit: this kind of for loop index variable is tradi
vabr (Chromium)
2012/08/10 17:12:55
Done.
I agree, Index versus Number (of).
| |
205 std::vector<net::UploadData::Element>::const_iterator element; | |
206 for (element = elements->begin(); element != elements->end(); ++element) { | |
207 kProducers[n]->FeedNext(*element); | |
208 } | |
209 if (kProducers[n]->Succeeded()) { | |
210 body->Set(kKeys[n], kProducers[n]->Result().release()); | |
211 some_succeeded = true; | |
212 } | |
213 } | |
214 if (!some_succeeded) { | |
215 body->SetString(keys::kBodyErrorKey, "Unknown error."); | |
216 } | |
217 } | |
218 | |
160 // Converts a HttpHeaders dictionary to a |name|, |value| pair. Returns | 219 // Converts a HttpHeaders dictionary to a |name|, |value| pair. Returns |
161 // true if successful. | 220 // true if successful. |
162 bool FromHeaderDictionary(const DictionaryValue* header_value, | 221 bool FromHeaderDictionary(const DictionaryValue* header_value, |
163 std::string* name, | 222 std::string* name, |
164 std::string* value) { | 223 std::string* value) { |
165 if (!header_value->GetString(keys::kHeaderNameKey, name)) | 224 if (!header_value->GetString(keys::kHeaderNameKey, name)) |
166 return false; | 225 return false; |
167 | 226 |
168 // We require either a "value" or a "binaryValue" entry. | 227 // We require either a "value" or a "binaryValue" entry. |
169 if (!(header_value->HasKey(keys::kHeaderValueKey) ^ | 228 if (!(header_value->HasKey(keys::kHeaderValueKey) ^ |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 SendExtensionWebRequestStatusToHost(host); | 303 SendExtensionWebRequestStatusToHost(host); |
245 } | 304 } |
246 } | 305 } |
247 | 306 |
248 void ClearCacheOnNavigationOnUI() { | 307 void ClearCacheOnNavigationOnUI() { |
249 WebCacheManager::GetInstance()->ClearCacheOnNavigation(); | 308 WebCacheManager::GetInstance()->ClearCacheOnNavigation(); |
250 } | 309 } |
251 | 310 |
252 } // namespace | 311 } // namespace |
253 | 312 |
313 // Don't use this outside IO thread. | |
314 void WebRequestSetChannelForTesting(VersionInfo::Channel channel) { | |
315 g_channel = channel; | |
316 g_channel_cached = true; | |
317 } | |
318 | |
319 // Don't use this outside IO thread. | |
320 void WebRequestResetChannelForTesting() { | |
321 g_channel_cached = false; | |
322 } | |
323 | |
254 // Represents a single unique listener to an event, along with whatever filter | 324 // Represents a single unique listener to an event, along with whatever filter |
255 // parameters and extra_info_spec were specified at the time the listener was | 325 // parameters and extra_info_spec were specified at the time the listener was |
256 // added. | 326 // added. |
257 struct ExtensionWebRequestEventRouter::EventListener { | 327 struct ExtensionWebRequestEventRouter::EventListener { |
258 std::string extension_id; | 328 std::string extension_id; |
259 std::string extension_name; | 329 std::string extension_name; |
260 std::string sub_event_name; | 330 std::string sub_event_name; |
261 RequestFilter filter; | 331 RequestFilter filter; |
262 int extra_info_spec; | 332 int extra_info_spec; |
263 base::WeakPtr<IPC::Sender> ipc_sender; | 333 base::WeakPtr<IPC::Sender> ipc_sender; |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
402 return false; | 472 return false; |
403 | 473 |
404 if (str == "requestHeaders") | 474 if (str == "requestHeaders") |
405 *extra_info_spec |= REQUEST_HEADERS; | 475 *extra_info_spec |= REQUEST_HEADERS; |
406 else if (str == "responseHeaders") | 476 else if (str == "responseHeaders") |
407 *extra_info_spec |= RESPONSE_HEADERS; | 477 *extra_info_spec |= RESPONSE_HEADERS; |
408 else if (str == "blocking") | 478 else if (str == "blocking") |
409 *extra_info_spec |= BLOCKING; | 479 *extra_info_spec |= BLOCKING; |
410 else if (str == "asyncBlocking") | 480 else if (str == "asyncBlocking") |
411 *extra_info_spec |= ASYNC_BLOCKING; | 481 *extra_info_spec |= ASYNC_BLOCKING; |
482 else if (str == "body") | |
483 *extra_info_spec |= IsWebRequestBodyDataAccessEnabled() ? BODY : 0; | |
412 else | 484 else |
413 return false; | 485 return false; |
414 | 486 |
415 // BLOCKING and ASYNC_BLOCKING are mutually exclusive. | 487 // BLOCKING and ASYNC_BLOCKING are mutually exclusive. |
416 if ((*extra_info_spec & BLOCKING) && (*extra_info_spec & ASYNC_BLOCKING)) | 488 if ((*extra_info_spec & BLOCKING) && (*extra_info_spec & ASYNC_BLOCKING)) |
417 return false; | 489 return false; |
418 } | 490 } |
419 return true; | 491 return true; |
420 } | 492 } |
421 | 493 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
491 | 563 |
492 int extra_info_spec = 0; | 564 int extra_info_spec = 0; |
493 std::vector<const EventListener*> listeners = | 565 std::vector<const EventListener*> listeners = |
494 GetMatchingListeners(profile, extension_info_map, keys::kOnBeforeRequest, | 566 GetMatchingListeners(profile, extension_info_map, keys::kOnBeforeRequest, |
495 request, &extra_info_spec); | 567 request, &extra_info_spec); |
496 if (!listeners.empty() && | 568 if (!listeners.empty() && |
497 !GetAndSetSignaled(request->identifier(), kOnBeforeRequest)) { | 569 !GetAndSetSignaled(request->identifier(), kOnBeforeRequest)) { |
498 ListValue args; | 570 ListValue args; |
499 DictionaryValue* dict = new DictionaryValue(); | 571 DictionaryValue* dict = new DictionaryValue(); |
500 ExtractRequestInfo(request, dict); | 572 ExtractRequestInfo(request, dict); |
573 if (extra_info_spec & ExtraInfoSpec::BODY) | |
574 ExtractRequestInfoBody(request, dict); | |
501 args.Append(dict); | 575 args.Append(dict); |
502 | 576 |
503 initialize_blocked_requests |= | 577 initialize_blocked_requests |= |
504 DispatchEvent(profile, request, listeners, args); | 578 DispatchEvent(profile, request, listeners, args); |
505 } | 579 } |
506 | 580 |
507 if (!initialize_blocked_requests) | 581 if (!initialize_blocked_requests) |
508 return net::OK; // Nobody saw a reason for modifying the request. | 582 return net::OK; // Nobody saw a reason for modifying the request. |
509 | 583 |
510 blocked_requests_[request->identifier()].event = kOnBeforeRequest; | 584 blocked_requests_[request->identifier()].event = kOnBeforeRequest; |
(...skipping 1309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1820 } else if ((*it)->name().find("AdBlock") != std::string::npos) { | 1894 } else if ((*it)->name().find("AdBlock") != std::string::npos) { |
1821 adblock = true; | 1895 adblock = true; |
1822 } else { | 1896 } else { |
1823 other = true; | 1897 other = true; |
1824 } | 1898 } |
1825 } | 1899 } |
1826 } | 1900 } |
1827 | 1901 |
1828 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other)); | 1902 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other)); |
1829 } | 1903 } |
OLD | NEW |