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

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: Now checking the channel for real Created 8 years, 5 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 7c2f278321dc8eb4887b0322a8fb6847a315f6e0..e3b3995c751fc1df9afcce0a92a6ed1b34dcc1c5 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"
@@ -31,6 +32,7 @@
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
#include "chrome/browser/renderer_host/web_cache_manager.h"
+#include "chrome/common/chrome_version_info.h"
#include "chrome/common/extensions/event_filtering_info.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_constants.h"
@@ -157,6 +159,32 @@ void ExtractRequestInfo(net::URLRequest* request, DictionaryValue* out) {
out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000);
}
+// Extracts the POST data from |request| and writes the data into |out|.
+// This can be expensive, so it's separated from ExtractRequestInfo().
+void ExtractRequestInfoPost(const net::URLRequest* request,
+ DictionaryValue* out) {
+ if (request->method() != "POST")
wtc 2012/08/01 17:24:41 If you want to support the PUT method, add && re
+ return; // Need to exit without "out->Set(keys::kPostDataKey, ...);" .
+
+ DictionaryValue* post_data = new DictionaryValue();
+ out->Set(keys::kPostDataKey, post_data);
+
+ // Chunked encoding (defined in RFC 2616) is currently not supported.
+ std::string transfer_encoding;
+ if (request->extra_request_headers().GetHeader(
+ net::HttpRequestHeaders::kTransferEncoding, &transfer_encoding)) {
+ if (base::strcasecmp(transfer_encoding.c_str(), "chunked") == 0) {
+ post_data->SetString(keys::kPostDataErrorKey, "chunked_encoding");
+ return;
+ }
+ }
+
+ scoped_ptr<DictionaryValue> form_data =
+ extensions::PostDataParser::ParseURLRequestData(request);
+ if (form_data.get() != NULL)
+ post_data->Set(keys::kFormDataKey, form_data.release());
+}
+
// Converts a HttpHeaders dictionary to a |name|, |value| pair. Returns
// true if successful.
bool FromHeaderDictionary(const DictionaryValue* header_value,
@@ -409,6 +437,8 @@ bool ExtensionWebRequestEventRouter::ExtraInfoSpec::InitFromValue(
*extra_info_spec |= BLOCKING;
else if (str == "asyncBlocking")
*extra_info_spec |= ASYNC_BLOCKING;
+ else if (str == "postData")
+ *extra_info_spec |= (IsPostDataEnabled()) ? POST_DATA : 0;
wtc 2012/08/01 17:24:41 Nit: you can also write this as else if (str ==
vabr (Chromium) 2012/08/02 09:15:15 Actually, that would be different. The current cod
Matt Perry 2012/08/02 10:11:03 I'm torn, so let's just go with what you have.
wtc 2012/08/02 21:58:49 You're right. My suggested change was wrong. Ple
else
return false;
@@ -498,6 +528,8 @@ int ExtensionWebRequestEventRouter::OnBeforeRequest(
ListValue args;
DictionaryValue* dict = new DictionaryValue();
ExtractRequestInfo(request, dict);
+ if (extra_info_spec & ExtraInfoSpec::POST_DATA)
+ ExtractRequestInfoPost(request, dict);
args.Append(dict);
initialize_blocked_requests |=
@@ -1827,3 +1859,16 @@ void SendExtensionWebRequestStatusToHost(content::RenderProcessHost* host) {
host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other));
}
+
+bool IsPostDataEnabled() {
+ static bool cached = false;
+ static bool enabled = false;
+ if (!cached) {
+ chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
Matt Perry 2012/08/01 09:46:31 channel is a POD (it's just an enum), so you can m
wtc 2012/08/01 17:24:41 mpcomplete's suggestion is correct. Note that nei
vabr (Chromium) 2012/08/01 18:03:22 Thanks, I learned something new! :) However, due
+ enabled = channel == chrome::VersionInfo::CHANNEL_UNKNOWN ||
+ channel == chrome::VersionInfo::CHANNEL_CANARY ||
+ channel == chrome::VersionInfo::CHANNEL_DEV;
+ cached = true;
+ }
+ return enabled;
+}

Powered by Google App Engine
This is Rietveld 408576698