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

Unified 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: Rebased + some corrections 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/web_request/web_request_api.cc
diff --git a/chrome/browser/extensions/api/web_request/web_request_api.cc b/chrome/browser/extensions/api/web_request/web_request_api.cc
index 2d8d8ad862d2cb5c7d27b3f3f3187f1bb5cf37b6..271e3a5994ac0b3af25bacbc35bcf60a62ca491b 100644
--- a/chrome/browser/extensions/api/web_request/web_request_api.cc
+++ b/chrome/browser/extensions/api/web_request/web_request_api.cc
@@ -19,6 +19,7 @@
#include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h"
#include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_registry.h"
#include "chrome/browser/extensions/api/web_navigation/web_navigation_api_helpers.h"
+#include "chrome/browser/extensions/api/web_request/post_data_parser.h"
#include "chrome/browser/extensions/api/web_request/web_request_api_constants.h"
#include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h"
#include "chrome/browser/extensions/api/web_request/web_request_time_tracker.h"
@@ -50,6 +51,7 @@
#include "net/url_request/url_request.h"
#include "ui/base/l10n/l10n_util.h"
+using chrome::VersionInfo;
using content::BrowserMessageFilter;
using content::BrowserThread;
using content::ResourceRequestInfo;
@@ -63,7 +65,7 @@ namespace keys = extension_web_request_api_constants;
namespace {
// List of all the webRequest events.
-static const char* const kWebRequestEvents[] = {
+const char* const kWebRequestEvents[] = {
keys::kOnBeforeRedirect,
keys::kOnBeforeRequest,
keys::kOnBeforeSendHeaders,
@@ -77,6 +79,20 @@ static const char* const kWebRequestEvents[] = {
#define ARRAYEND(array) (array + arraysize(array))
+bool g_channel_cached = false;
+VersionInfo::Channel g_channel = VersionInfo::CHANNEL_UNKNOWN;
+
+// Access to request body (crbug.com/91191/) is currently only enabled in dev
+// and canary channels. This function caches the release channel info from
+// chrome::VersionInfo. Don't use this outside IO thread.
+bool IsWebRequestBodyDataAccessEnabled() {
+ if (!g_channel_cached) {
+ g_channel = VersionInfo::GetChannel();
+ g_channel_cached = true;
+ }
+ return g_channel <= VersionInfo::CHANNEL_DEV;
+}
+
bool IsWebRequestEvent(const std::string& event_name) {
return std::find(kWebRequestEvents, ARRAYEND(kWebRequestEvents),
event_name) != ARRAYEND(kWebRequestEvents);
@@ -157,6 +173,49 @@ void ExtractRequestInfo(net::URLRequest* request, DictionaryValue* out) {
out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000);
}
+// Extracts the body from |request| and writes the data into |out|.
+void ExtractRequestInfoBody(const net::URLRequest* request,
+ DictionaryValue* out) {
+ if (request->method() != "POST" && request->method() != "PUT")
+ return; // Need to exit without "out->Set(keys::kBodyKey, ...);" .
+
+ DictionaryValue* body = new DictionaryValue();
+ out->Set(keys::kBodyKey, body);
+
+ // Get the data producers, ordered by being interesting.
+ extensions::ChunkedErrorProducer chunked_error_producer(request);
+ extensions::RawDataProducer raw_data_producer;
+ extensions::ParsedDataProducer parsed_data_producer(request);
+ 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.
+ &chunked_error_producer, // First -- any errors?
+ &parsed_data_producer, // Second -- any parseable forms?
+ &raw_data_producer // Third -- any data at all?
+ };
+ // Keys for the results of the corresponding producers.
+ static const char* const kKeys[] = {
+ keys::kBodyErrorKey,
+ keys::kBodyParsedFormKey,
+ keys::kBodyRawKey
+ };
+
+ const std::vector<net::UploadData::Element>* elements =
+ request->get_upload()->elements();
+ bool some_succeeded = false;
+ 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).
+ std::vector<net::UploadData::Element>::const_iterator element;
+ for (element = elements->begin(); element != elements->end(); ++element) {
+ kProducers[n]->FeedNext(*element);
+ }
+ if (kProducers[n]->Succeeded()) {
+ body->Set(kKeys[n], kProducers[n]->Result().release());
+ some_succeeded = true;
+ }
+ }
+ if (!some_succeeded) {
+ body->SetString(keys::kBodyErrorKey, "Unknown error.");
+ }
+}
+
// Converts a HttpHeaders dictionary to a |name|, |value| pair. Returns
// true if successful.
bool FromHeaderDictionary(const DictionaryValue* header_value,
@@ -251,6 +310,17 @@ void ClearCacheOnNavigationOnUI() {
} // namespace
+// Don't use this outside IO thread.
+void WebRequestSetChannelForTesting(VersionInfo::Channel channel) {
+ g_channel = channel;
+ g_channel_cached = true;
+}
+
+// Don't use this outside IO thread.
+void WebRequestResetChannelForTesting() {
+ g_channel_cached = false;
+}
+
// Represents a single unique listener to an event, along with whatever filter
// parameters and extra_info_spec were specified at the time the listener was
// added.
@@ -409,6 +479,8 @@ bool ExtensionWebRequestEventRouter::ExtraInfoSpec::InitFromValue(
*extra_info_spec |= BLOCKING;
else if (str == "asyncBlocking")
*extra_info_spec |= ASYNC_BLOCKING;
+ else if (str == "body")
+ *extra_info_spec |= IsWebRequestBodyDataAccessEnabled() ? BODY : 0;
else
return false;
@@ -498,6 +570,8 @@ int ExtensionWebRequestEventRouter::OnBeforeRequest(
ListValue args;
DictionaryValue* dict = new DictionaryValue();
ExtractRequestInfo(request, dict);
+ if (extra_info_spec & ExtraInfoSpec::BODY)
+ ExtractRequestInfoBody(request, dict);
args.Append(dict);
initialize_blocked_requests |=

Powered by Google App Engine
This is Rietveld 408576698