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

Unified Diff: chrome/browser/extensions/api/web_request/web_request_api.cc

Issue 568583002: Introduce WebRequestEventRouterDelegate. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rename the delegate to WebRequestEventRouterDelegate. Created 6 years, 3 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 a1cecb7dc9974732705c337463d1063512beebff..94481baa084a4401a0fc635a3caaab7e8dcbbe0e 100644
--- a/chrome/browser/extensions/api/web_request/web_request_api.cc
+++ b/chrome/browser/extensions/api/web_request/web_request_api.cc
@@ -16,24 +16,12 @@
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
-#include "chrome/browser/browser_process.h"
-#include "chrome/browser/chrome_content_browser_client.h"
-#include "chrome/browser/extensions/activity_log/activity_action_constants.h"
-#include "chrome/browser/extensions/activity_log/activity_actions.h"
-#include "chrome/browser/extensions/activity_log/activity_log.h"
-#include "chrome/browser/extensions/activity_log/web_request_constants.h"
#include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_registry.h"
#include "chrome/browser/extensions/api/web_request/upload_data_presenter.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"
-#include "chrome/browser/extensions/extension_renderer_state.h"
-#include "chrome/browser/profiles/profile.h"
-#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/extensions/api/web_request.h"
-#include "chrome/common/extensions/extension_constants.h"
-#include "chrome/common/url_constants.h"
-#include "chrome/grit/generated_resources.h"
#include "content/public/browser/browser_message_filter.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
@@ -42,12 +30,15 @@
#include "content/public/browser/user_metrics.h"
#include "extensions/browser/api/declarative_webrequest/request_stage.h"
#include "extensions/browser/api/declarative_webrequest/webrequest_constants.h"
+#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/api/web_request/web_request_api_utils.h"
+#include "extensions/browser/api/web_request/web_request_event_router_delegate.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_message_filter.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
+#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/guest_view/web_view/web_view_constants.h"
#include "extensions/browser/guest_view/web_view/web_view_renderer_state.h"
#include "extensions/browser/info_map.h"
@@ -62,6 +53,7 @@
#include "extensions/common/features/feature.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/common/url_pattern.h"
+#include "extensions/strings/grit/extensions_strings.h"
#include "net/base/auth.h"
#include "net/base/net_errors.h"
#include "net/base/upload_data_stream.h"
@@ -92,7 +84,6 @@ namespace utils = extension_web_request_api_utils;
namespace keys = extension_web_request_api_constants;
namespace web_request = extensions::api::web_request;
namespace declarative_keys = extensions::declarative_webrequest_constants;
-namespace activitylog = activity_log_web_request_constants;
namespace {
@@ -111,8 +102,6 @@ const char* const kWebRequestEvents[] = {
keys::kOnHeadersReceivedEvent,
};
-#define ARRAYEND(array) (array + arraysize(array))
-
const char* GetRequestStageAsString(
ExtensionWebRequestEventRouter::EventTypes type) {
switch (type) {
@@ -198,70 +187,6 @@ bool GetWebViewInfo(
GetInfo(render_process_host_id, routing_id, web_view_info);
}
-void ExtractRequestInfoDetails(net::URLRequest* request,
- bool* is_main_frame,
- int* frame_id,
- bool* parent_is_main_frame,
- int* parent_frame_id,
- int* tab_id,
- int* window_id,
- int* render_process_host_id,
- int* routing_id,
- ResourceType* resource_type) {
- if (!request->GetUserData(NULL))
- return;
-
- const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
- ExtensionRendererState::GetInstance()->GetTabAndWindowId(
- info, tab_id, window_id);
- *frame_id = info->GetRenderFrameID();
- *is_main_frame = info->IsMainFrame();
- *parent_frame_id = info->GetParentRenderFrameID();
- *parent_is_main_frame = info->ParentIsMainFrame();
- *render_process_host_id = info->GetChildID();
- *routing_id = info->GetRouteID();
-
- // Restrict the resource type to the values we care about.
- if (utils::IsRelevantResourceType(info->GetResourceType()))
- *resource_type = info->GetResourceType();
- else
- *resource_type = content::RESOURCE_TYPE_LAST_TYPE;
-}
-
-// Extracts from |request| information for the keys requestId, url, method,
-// frameId, tabId, type, and timeStamp and writes these into |out| to be passed
-// on to extensions.
-void ExtractRequestInfo(net::URLRequest* request, base::DictionaryValue* out) {
- bool is_main_frame = false;
- int frame_id = -1;
- bool parent_is_main_frame = false;
- int parent_frame_id = -1;
- int frame_id_for_extension = -1;
- int parent_frame_id_for_extension = -1;
- int tab_id = -1;
- int window_id = -1;
- int render_process_host_id = -1;
- int routing_id = -1;
- ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE;
- ExtractRequestInfoDetails(request, &is_main_frame, &frame_id,
- &parent_is_main_frame, &parent_frame_id, &tab_id,
- &window_id, &render_process_host_id, &routing_id,
- &resource_type);
- frame_id_for_extension = GetFrameId(is_main_frame, frame_id);
- parent_frame_id_for_extension = GetFrameId(parent_is_main_frame,
- parent_frame_id);
-
- out->SetString(keys::kRequestIdKey,
- base::Uint64ToString(request->identifier()));
- out->SetString(keys::kUrlKey, request->url().spec());
- out->SetString(keys::kMethodKey, request->method());
- out->SetInteger(keys::kFrameIdKey, frame_id_for_extension);
- out->SetInteger(keys::kParentFrameIdKey, parent_frame_id_for_extension);
- out->SetInteger(keys::kTabIdKey, tab_id);
- out->SetString(keys::kTypeKey, utils::ResourceTypeToString(resource_type));
- 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,
base::DictionaryValue* out) {
@@ -331,21 +256,6 @@ bool FromHeaderDictionary(const base::DictionaryValue* header_value,
return true;
}
-// Converts the |name|, |value| pair of a http header to a HttpHeaders
-// dictionary. Ownership is passed to the caller.
-base::DictionaryValue* ToHeaderDictionary(const std::string& name,
- const std::string& value) {
- base::DictionaryValue* header = new base::DictionaryValue();
- header->SetString(keys::kHeaderNameKey, name);
- if (base::IsStringUTF8(value)) {
- header->SetString(keys::kHeaderValueKey, value);
- } else {
- header->Set(keys::kHeaderBinaryValueKey,
- helpers::StringToCharList(value));
- }
- return header;
-}
-
// Creates a list of HttpHeaders (see the extension API JSON). If |headers| is
// NULL, the list is empty. Ownership is passed to the caller.
base::ListValue* GetResponseHeadersList(
@@ -356,7 +266,7 @@ base::ListValue* GetResponseHeadersList(
std::string name;
std::string value;
while (headers->EnumerateHeaderLines(&iter, &name, &value))
- headers_value->Append(ToHeaderDictionary(name, value));
+ headers_value->Append(helpers::ToHeaderDictionary(name, value));
}
return headers_value;
}
@@ -364,7 +274,7 @@ base::ListValue* GetResponseHeadersList(
base::ListValue* GetRequestHeadersList(const net::HttpRequestHeaders& headers) {
base::ListValue* headers_value = new base::ListValue();
for (net::HttpRequestHeaders::Iterator it(headers); it.GetNext(); )
- headers_value->Append(ToHeaderDictionary(it.name(), it.value()));
+ headers_value->Append(helpers::ToHeaderDictionary(it.name(), it.value()));
return headers_value;
}
@@ -382,11 +292,14 @@ void RemoveEventListenerOnUI(
const std::string& extension_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
- Profile* profile = reinterpret_cast<Profile*>(profile_id);
- if (!g_browser_process->profile_manager()->IsValidProfile(profile))
+ content::BrowserContext* browser_context =
+ reinterpret_cast<content::BrowserContext*>(profile_id);
+ if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(
+ browser_context))
return;
- extensions::EventRouter* event_router = extensions::EventRouter::Get(profile);
+ extensions::EventRouter* event_router =
+ extensions::EventRouter::Get(browser_context);
if (!event_router)
return;
@@ -414,14 +327,17 @@ void SendOnMessageEventOnUI(
scoped_ptr<base::DictionaryValue> event_argument) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
- Profile* profile = reinterpret_cast<Profile*>(profile_id);
- if (!g_browser_process->profile_manager()->IsValidProfile(profile))
+ content::BrowserContext* browser_context =
+ reinterpret_cast<content::BrowserContext*>(profile_id);
+ if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(
+ browser_context))
return;
scoped_ptr<base::ListValue> event_args(new base::ListValue);
event_args->Append(event_argument.release());
- extensions::EventRouter* event_router = extensions::EventRouter::Get(profile);
+ extensions::EventRouter* event_router =
+ extensions::EventRouter::Get(browser_context);
extensions::EventFilteringInfo event_filtering_info;
@@ -438,7 +354,7 @@ void SendOnMessageEventOnUI(
scoped_ptr<extensions::Event> event(new extensions::Event(
event_name,
- event_args.Pass(), profile, GURL(),
+ event_args.Pass(), browser_context, GURL(),
extensions::EventRouter::USER_GESTURE_UNKNOWN,
event_filtering_info));
event_router->DispatchEventToExtension(extension_id, event.Pass());
@@ -705,6 +621,9 @@ ExtensionWebRequestEventRouter* ExtensionWebRequestEventRouter::GetInstance() {
ExtensionWebRequestEventRouter::ExtensionWebRequestEventRouter()
: request_time_tracker_(new ExtensionWebRequestTimeTracker) {
+ web_request_event_router_delegate_.reset(
+ extensions::ExtensionsAPIClient::Get()->
+ CreateWebRequestEventRouterDelegate());
}
ExtensionWebRequestEventRouter::~ExtensionWebRequestEventRouter() {
@@ -721,6 +640,71 @@ void ExtensionWebRequestEventRouter::RegisterRulesRegistry(
rules_registries_.erase(key);
}
+void ExtensionWebRequestEventRouter::ExtractRequestInfoDetails(
+ net::URLRequest* request,
+ bool* is_main_frame,
+ int* frame_id,
+ bool* parent_is_main_frame,
+ int* parent_frame_id,
+ int* tab_id,
+ int* window_id,
Fady Samuel 2014/09/12 21:55:40 Can we get rid of tab_id and window_id in extensio
+ int* render_process_host_id,
+ int* routing_id,
+ ResourceType* resource_type) const {
+ if (!request->GetUserData(NULL))
+ return;
+
+ const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
+ if(web_request_event_router_delegate_) {
+ web_request_event_router_delegate_->ExtractRequestInfoDetails(
+ info, tab_id, window_id);
+ }
+ *frame_id = info->GetRenderFrameID();
+ *is_main_frame = info->IsMainFrame();
+ *parent_frame_id = info->GetParentRenderFrameID();
+ *parent_is_main_frame = info->ParentIsMainFrame();
+ *render_process_host_id = info->GetChildID();
+ *routing_id = info->GetRouteID();
+
+ // Restrict the resource type to the values we care about.
+ if (utils::IsRelevantResourceType(info->GetResourceType()))
+ *resource_type = info->GetResourceType();
+ else
+ *resource_type = content::RESOURCE_TYPE_LAST_TYPE;
+}
+
+void ExtensionWebRequestEventRouter::ExtractRequestInfo(
+ net::URLRequest* request, base::DictionaryValue* out) {
+ bool is_main_frame = false;
+ int frame_id = -1;
+ bool parent_is_main_frame = false;
+ int parent_frame_id = -1;
+ int frame_id_for_extension = -1;
+ int parent_frame_id_for_extension = -1;
+ int tab_id = -1;
+ int window_id = -1;
Fady Samuel 2014/09/12 21:55:40 The concepts of tab_id and window_id should not ex
+ int render_process_host_id = -1;
+ int routing_id = -1;
+ ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE;
+ ExtractRequestInfoDetails(request, &is_main_frame, &frame_id,
+ &parent_is_main_frame, &parent_frame_id, &tab_id,
+ &window_id, &render_process_host_id, &routing_id,
+ &resource_type);
+ frame_id_for_extension = GetFrameId(is_main_frame, frame_id);
+ parent_frame_id_for_extension = GetFrameId(parent_is_main_frame,
+ parent_frame_id);
+
+ out->SetString(keys::kRequestIdKey,
+ base::Uint64ToString(request->identifier()));
+ out->SetString(keys::kUrlKey, request->url().spec());
+ out->SetString(keys::kMethodKey, request->method());
+ out->SetInteger(keys::kFrameIdKey, frame_id_for_extension);
+ out->SetInteger(keys::kParentFrameIdKey, parent_frame_id_for_extension);
+ out->SetInteger(keys::kTabIdKey, tab_id);
+ out->SetString(keys::kTypeKey, utils::ResourceTypeToString(resource_type));
+ out->SetDouble(keys::kTimeStampKey, base::Time::Now().ToDoubleT() * 1000);
+}
+
int ExtensionWebRequestEventRouter::OnBeforeRequest(
void* profile,
Fady Samuel 2014/09/12 21:55:40 nit: rename to browser_context
Xi Han 2014/09/18 16:24:00 Done.
InfoMap* extension_info_map,
@@ -1632,149 +1616,6 @@ helpers::EventResponseDelta* CalculateDelta(
return NULL;
}
-base::Value* SerializeResponseHeaders(const helpers::ResponseHeaders& headers) {
- scoped_ptr<base::ListValue> serialized_headers(new base::ListValue());
- for (helpers::ResponseHeaders::const_iterator i = headers.begin();
- i != headers.end(); ++i) {
- serialized_headers->Append(ToHeaderDictionary(i->first, i->second));
- }
- return serialized_headers.release();
-}
-
-// Convert a RequestCookieModifications/ResponseCookieModifications object to a
-// base::ListValue which summarizes the changes made. This is templated since
-// the two types (request/response) are different but contain essentially the
-// same fields.
-template<typename CookieType>
-base::ListValue* SummarizeCookieModifications(
- const std::vector<linked_ptr<CookieType> >& modifications) {
- scoped_ptr<base::ListValue> cookie_modifications(new base::ListValue());
- for (typename std::vector<linked_ptr<CookieType> >::const_iterator i =
- modifications.begin();
- i != modifications.end(); ++i) {
- scoped_ptr<base::DictionaryValue> summary(new base::DictionaryValue());
- const CookieType& mod = *i->get();
- switch (mod.type) {
- case helpers::ADD:
- summary->SetString(activitylog::kCookieModificationTypeKey,
- activitylog::kCookieModificationAdd);
- break;
- case helpers::EDIT:
- summary->SetString(activitylog::kCookieModificationTypeKey,
- activitylog::kCookieModificationEdit);
- break;
- case helpers::REMOVE:
- summary->SetString(activitylog::kCookieModificationTypeKey,
- activitylog::kCookieModificationRemove);
- break;
- }
- if (mod.filter) {
- if (mod.filter->name)
- summary->SetString(activitylog::kCookieFilterNameKey,
- *mod.modification->name);
- if (mod.filter->domain)
- summary->SetString(activitylog::kCookieFilterDomainKey,
- *mod.modification->name);
- }
- if (mod.modification) {
- if (mod.modification->name)
- summary->SetString(activitylog::kCookieModDomainKey,
- *mod.modification->name);
- if (mod.modification->domain)
- summary->SetString(activitylog::kCookieModDomainKey,
- *mod.modification->name);
- }
- cookie_modifications->Append(summary.release());
- }
- return cookie_modifications.release();
-}
-
-// Converts an EventResponseDelta object to a dictionary value suitable for the
-// activity log.
-scoped_ptr<base::DictionaryValue> SummarizeResponseDelta(
- const std::string& event_name,
- const helpers::EventResponseDelta& delta) {
- scoped_ptr<base::DictionaryValue> details(new base::DictionaryValue());
- if (delta.cancel) {
- details->SetBoolean(activitylog::kCancelKey, true);
- }
- if (!delta.new_url.is_empty()) {
- details->SetString(activitylog::kNewUrlKey, delta.new_url.spec());
- }
-
- scoped_ptr<base::ListValue> modified_headers(new base::ListValue());
- net::HttpRequestHeaders::Iterator iter(delta.modified_request_headers);
- while (iter.GetNext()) {
- modified_headers->Append(ToHeaderDictionary(iter.name(), iter.value()));
- }
- if (!modified_headers->empty()) {
- details->Set(activitylog::kModifiedRequestHeadersKey,
- modified_headers.release());
- }
-
- scoped_ptr<base::ListValue> deleted_headers(new base::ListValue());
- deleted_headers->AppendStrings(delta.deleted_request_headers);
- if (!deleted_headers->empty()) {
- details->Set(activitylog::kDeletedRequestHeadersKey,
- deleted_headers.release());
- }
-
- if (!delta.added_response_headers.empty()) {
- details->Set(activitylog::kAddedRequestHeadersKey,
- SerializeResponseHeaders(delta.added_response_headers));
- }
- if (!delta.deleted_response_headers.empty()) {
- details->Set(activitylog::kDeletedResponseHeadersKey,
- SerializeResponseHeaders(delta.deleted_response_headers));
- }
- if (delta.auth_credentials) {
- details->SetString(activitylog::kAuthCredentialsKey,
- base::UTF16ToUTF8(
- delta.auth_credentials->username()) + ":*");
- }
-
- if (!delta.response_cookie_modifications.empty()) {
- details->Set(
- activitylog::kResponseCookieModificationsKey,
- SummarizeCookieModifications(delta.response_cookie_modifications));
- }
-
- return details.Pass();
-}
-
-void LogExtensionActivity(void* profile_id,
- bool is_incognito,
- const std::string& extension_id,
- const GURL& url,
- const std::string& api_call,
- scoped_ptr<base::DictionaryValue> details) {
- if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
- BrowserThread::PostTask(BrowserThread::UI,
- FROM_HERE,
- base::Bind(&LogExtensionActivity,
- profile_id,
- is_incognito,
- extension_id,
- url,
- api_call,
- base::Passed(&details)));
- } else {
- Profile* profile = static_cast<Profile*>(profile_id);
- if (!g_browser_process->profile_manager()->IsValidProfile(profile))
- return;
- scoped_refptr<extensions::Action> action =
- new extensions::Action(extension_id,
- base::Time::Now(),
- extensions::Action::ACTION_WEB_REQUEST,
- api_call);
- action->set_page_url(url);
- action->set_page_incognito(is_incognito);
- action->mutable_other()->Set(activity_log_constants::kActionWebRequest,
- details.release());
- extensions::ActivityLog::GetInstance(profile)->LogAction(action);
- }
-}
-
} // namespace
void ExtensionWebRequestEventRouter::DecrementBlockCount(
@@ -1798,12 +1639,15 @@ void ExtensionWebRequestEventRouter::DecrementBlockCount(
helpers::EventResponseDelta* delta =
CalculateDelta(&blocked_request, response);
- LogExtensionActivity(profile,
- blocked_request.is_incognito,
- extension_id,
- blocked_request.request->url(),
- event_name,
- SummarizeResponseDelta(event_name, *delta));
+ if(web_request_event_router_delegate_) {
+ web_request_event_router_delegate_->LogExtensionActivity(
+ profile,
+ blocked_request.is_incognito,
+ extension_id,
+ blocked_request.request->url(),
+ event_name,
+ *delta);
+ }
blocked_request.response_deltas.push_back(
linked_ptr<helpers::EventResponseDelta>(delta));
@@ -2475,15 +2319,15 @@ bool WebRequestHandlerBehaviorChangedFunction::RunSync() {
}
void SendExtensionWebRequestStatusToHost(content::RenderProcessHost* host) {
- Profile* profile = Profile::FromBrowserContext(host->GetBrowserContext());
- if (!profile)
+ content::BrowserContext* browser_context = host->GetBrowserContext();
+ if (!browser_context)
return;
bool webrequest_used = false;
const extensions::ExtensionSet& extensions =
- extensions::ExtensionRegistry::Get(profile)->enabled_extensions();
+ extensions::ExtensionRegistry::Get(browser_context)->enabled_extensions();
extensions::RuntimeData* runtime_data =
- extensions::ExtensionSystem::Get(profile)->runtime_data();
+ extensions::ExtensionSystem::Get(browser_context)->runtime_data();
for (extensions::ExtensionSet::const_iterator it = extensions.begin();
!webrequest_used && it != extensions.end();
++it) {

Powered by Google App Engine
This is Rietveld 408576698