Commit 8c5bc927 authored by Kelvin Jiang's avatar Kelvin Jiang Committed by Commit Bot

[DNR] Remove the NONE action type

This CL removes the NONE action type. Instead of returning a vector of
one action with the NONE type, RulesetManager::EvaluateRequest will
instead return an empty vector if no DNR rules match with the request.

Bug: 1004006
Change-Id: I7dc2b7b59540f3d185b9585e530818f92e6201b7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1809882Reviewed-by: default avatarKaran Bhatia <karandeepb@chromium.org>
Commit-Queue: Kelvin Jiang <kelvinjiang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#698651}
parent 1f5a93ab
...@@ -771,7 +771,7 @@ TEST(ExtensionWebRequestHelpersTest, TestMergeOnBeforeSendHeadersResponses) { ...@@ -771,7 +771,7 @@ TEST(ExtensionWebRequestHelpersTest, TestMergeOnBeforeSendHeadersResponses) {
headers0.MergeFrom(base_headers); headers0.MergeFrom(base_headers);
WebRequestInfoInitParams info_params; WebRequestInfoInitParams info_params;
WebRequestInfo info(std::move(info_params)); WebRequestInfo info(std::move(info_params));
info.dnr_actions.emplace_back(Action::Type::NONE); info.dnr_actions = std::vector<Action>();
MergeOnBeforeSendHeadersResponses(info, deltas, &headers0, &ignored_actions, MergeOnBeforeSendHeadersResponses(info, deltas, &headers0, &ignored_actions,
&ignore1, &ignore2, &ignore1, &ignore2,
&request_headers_modified0); &request_headers_modified0);
...@@ -882,7 +882,8 @@ TEST(ExtensionWebRequestHelpersTest, TestMergeOnBeforeSendHeadersResponses) { ...@@ -882,7 +882,8 @@ TEST(ExtensionWebRequestHelpersTest, TestMergeOnBeforeSendHeadersResponses) {
Action remove_headers_action(Action::Type::REMOVE_HEADERS); Action remove_headers_action(Action::Type::REMOVE_HEADERS);
remove_headers_action.request_headers_to_remove = {"key5"}; remove_headers_action.request_headers_to_remove = {"key5"};
info.dnr_actions.push_back(std::move(remove_headers_action)); info.dnr_actions = std::vector<Action>();
info.dnr_actions->push_back(std::move(remove_headers_action));
MergeOnBeforeSendHeadersResponses(info, deltas, &headers4, &ignored_actions, MergeOnBeforeSendHeadersResponses(info, deltas, &headers4, &ignored_actions,
&ignore1, &ignore2, &ignore1, &ignore2,
&request_headers_modified4); &request_headers_modified4);
...@@ -929,7 +930,7 @@ TEST(ExtensionWebRequestHelpersTest, ...@@ -929,7 +930,7 @@ TEST(ExtensionWebRequestHelpersTest,
WebRequestInfoInitParams info_params; WebRequestInfoInitParams info_params;
WebRequestInfo info(std::move(info_params)); WebRequestInfo info(std::move(info_params));
info.dnr_actions.emplace_back(Action::Type::NONE); info.dnr_actions = std::vector<Action>();
helpers::IgnoredActions ignored_actions; helpers::IgnoredActions ignored_actions;
std::set<std::string> removed_headers, set_headers; std::set<std::string> removed_headers, set_headers;
bool request_headers_modified = false; bool request_headers_modified = false;
...@@ -1272,7 +1273,7 @@ TEST(ExtensionWebRequestHelpersTest, TestMergeOnHeadersReceivedResponses) { ...@@ -1272,7 +1273,7 @@ TEST(ExtensionWebRequestHelpersTest, TestMergeOnHeadersReceivedResponses) {
WebRequestInfoInitParams info_params; WebRequestInfoInitParams info_params;
info_params.url = GURL(kExampleUrl); info_params.url = GURL(kExampleUrl);
WebRequestInfo info(std::move(info_params)); WebRequestInfo info(std::move(info_params));
info.dnr_actions.emplace_back(Action::Type::NONE); info.dnr_actions = std::vector<Action>();
MergeOnHeadersReceivedResponses(info, deltas, base_headers.get(), MergeOnHeadersReceivedResponses(info, deltas, base_headers.get(),
&new_headers0, &allowed_unsafe_redirect_url0, &new_headers0, &allowed_unsafe_redirect_url0,
...@@ -1354,7 +1355,8 @@ TEST(ExtensionWebRequestHelpersTest, TestMergeOnHeadersReceivedResponses) { ...@@ -1354,7 +1355,8 @@ TEST(ExtensionWebRequestHelpersTest, TestMergeOnHeadersReceivedResponses) {
// request extensions and result in a conflict. // request extensions and result in a conflict.
Action remove_headers_action(Action::Type::REMOVE_HEADERS); Action remove_headers_action(Action::Type::REMOVE_HEADERS);
remove_headers_action.response_headers_to_remove = {"key3"}; remove_headers_action.response_headers_to_remove = {"key3"};
info.dnr_actions.push_back(std::move(remove_headers_action)); info.dnr_actions = std::vector<Action>();
info.dnr_actions->push_back(std::move(remove_headers_action));
ignored_actions.clear(); ignored_actions.clear();
bool response_headers_modified3 = false; bool response_headers_modified3 = false;
...@@ -1410,7 +1412,7 @@ TEST(ExtensionWebRequestHelpersTest, ...@@ -1410,7 +1412,7 @@ TEST(ExtensionWebRequestHelpersTest,
WebRequestInfoInitParams info_params; WebRequestInfoInitParams info_params;
info_params.url = GURL(kExampleUrl); info_params.url = GURL(kExampleUrl);
WebRequestInfo info(std::move(info_params)); WebRequestInfo info(std::move(info_params));
info.dnr_actions.emplace_back(Action::Type::NONE); info.dnr_actions = std::vector<Action>();
MergeOnHeadersReceivedResponses(info, deltas, base_headers.get(), MergeOnHeadersReceivedResponses(info, deltas, base_headers.get(),
&new_headers1, &allowed_unsafe_redirect_url1, &new_headers1, &allowed_unsafe_redirect_url1,
......
...@@ -337,12 +337,12 @@ const std::vector<RulesetManager::Action>& RulesetManager::EvaluateRequest( ...@@ -337,12 +337,12 @@ const std::vector<RulesetManager::Action>& RulesetManager::EvaluateRequest(
// |is_incognito_context| will stay the same for a given |request|. This also // |is_incognito_context| will stay the same for a given |request|. This also
// assumes that the core state of the WebRequestInfo isn't changed between the // assumes that the core state of the WebRequestInfo isn't changed between the
// different EvaluateRequest invocations. // different EvaluateRequest invocations.
if (request.dnr_actions.empty()) { if (!request.dnr_actions) {
request.dnr_actions = request.dnr_actions =
EvaluateRequestInternal(request, is_incognito_context); EvaluateRequestInternal(request, is_incognito_context);
} }
return request.dnr_actions; return *request.dnr_actions;
} }
bool RulesetManager::HasAnyExtraHeadersMatcher() const { bool RulesetManager::HasAnyExtraHeadersMatcher() const {
...@@ -496,22 +496,18 @@ std::vector<RulesetManager::Action> RulesetManager::EvaluateRequestInternal( ...@@ -496,22 +496,18 @@ std::vector<RulesetManager::Action> RulesetManager::EvaluateRequestInternal(
const WebRequestInfo& request, const WebRequestInfo& request,
bool is_incognito_context) const { bool is_incognito_context) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(request.dnr_actions.empty()); DCHECK(!request.dnr_actions);
std::vector<Action> actions; std::vector<Action> actions;
if (!ShouldEvaluateRequest(request)) { if (!ShouldEvaluateRequest(request))
actions.emplace_back(Action::Type::NONE);
return actions; return actions;
}
if (test_observer_) if (test_observer_)
test_observer_->OnEvaluateRequest(request, is_incognito_context); test_observer_->OnEvaluateRequest(request, is_incognito_context);
if (rulesets_.empty()) { if (rulesets_.empty())
actions.emplace_back(Action::Type::NONE);
return actions; return actions;
}
SCOPED_UMA_HISTOGRAM_TIMER( SCOPED_UMA_HISTOGRAM_TIMER(
"Extensions.DeclarativeNetRequest.EvaluateRequestTime.AllExtensions2"); "Extensions.DeclarativeNetRequest.EvaluateRequestTime.AllExtensions2");
...@@ -574,7 +570,6 @@ std::vector<RulesetManager::Action> RulesetManager::EvaluateRequestInternal( ...@@ -574,7 +570,6 @@ std::vector<RulesetManager::Action> RulesetManager::EvaluateRequestInternal(
if (!remove_headers_actions.empty()) if (!remove_headers_actions.empty())
return remove_headers_actions; return remove_headers_actions;
actions.emplace_back(Action::Type::NONE);
return actions; return actions;
} }
......
...@@ -41,7 +41,6 @@ class RulesetManager { ...@@ -41,7 +41,6 @@ class RulesetManager {
public: public:
struct Action { struct Action {
enum class Type { enum class Type {
NONE,
// Block the network request. // Block the network request.
BLOCK, BLOCK,
// Block the network request and collapse the corresponding DOM element. // Block the network request and collapse the corresponding DOM element.
...@@ -57,14 +56,13 @@ class RulesetManager { ...@@ -57,14 +56,13 @@ class RulesetManager {
Action(Action&&); Action(Action&&);
Action& operator=(Action&&); Action& operator=(Action&&);
Type type = Type::NONE; Type type = Type::BLOCK;
// Valid iff |type| is |REDIRECT|. // Valid iff |type| is |REDIRECT|.
base::Optional<GURL> redirect_url; base::Optional<GURL> redirect_url;
// The id of the extension the action is attributed to. Valid iff |type| is // The id of the extension the action is attributed to.
// not |NONE|. ExtensionId extension_id;
base::Optional<ExtensionId> extension_id;
// Valid iff |type| is |REMOVE_HEADERS|. The vectors point to strings of // Valid iff |type| is |REMOVE_HEADERS|. The vectors point to strings of
// static storage duration. // static storage duration.
......
...@@ -514,8 +514,7 @@ void OnDNRActionMatched( ...@@ -514,8 +514,7 @@ void OnDNRActionMatched(
->ruleset_manager() ->ruleset_manager()
->action_tracker(); ->action_tracker();
DCHECK(action.extension_id.has_value()); action_tracker.OnRuleMatched(action.extension_id, tab_id);
action_tracker.OnRuleMatched(action.extension_id.value(), tab_id);
} }
// Helper to remove request headers based on a matched DNR action. Returns // Helper to remove request headers based on a matched DNR action. Returns
...@@ -1044,7 +1043,7 @@ int ExtensionWebRequestEventRouter::OnBeforeRequest( ...@@ -1044,7 +1043,7 @@ int ExtensionWebRequestEventRouter::OnBeforeRequest(
DCHECK(should_collapse_initiator); DCHECK(should_collapse_initiator);
if (ShouldHideEvent(browser_context, *request)) { if (ShouldHideEvent(browser_context, *request)) {
request->dnr_actions.emplace_back(Action::Type::NONE); request->dnr_actions = std::vector<Action>();
return net::OK; return net::OK;
} }
...@@ -1101,9 +1100,6 @@ int ExtensionWebRequestEventRouter::OnBeforeRequest( ...@@ -1101,9 +1100,6 @@ int ExtensionWebRequestEventRouter::OnBeforeRequest(
->EvaluateRequest(*request, is_incognito_context); ->EvaluateRequest(*request, is_incognito_context);
for (const auto& action : actions) { for (const auto& action : actions) {
switch (action.type) { switch (action.type) {
case Action::Type::NONE:
DCHECK_EQ(1u, actions.size());
break;
case Action::Type::BLOCK: case Action::Type::BLOCK:
ClearPendingCallbacks(*request); ClearPendingCallbacks(*request);
DCHECK_EQ(1u, actions.size()); DCHECK_EQ(1u, actions.size());
...@@ -1126,8 +1122,8 @@ int ExtensionWebRequestEventRouter::OnBeforeRequest( ...@@ -1126,8 +1122,8 @@ int ExtensionWebRequestEventRouter::OnBeforeRequest(
// Unlike other actions, allow web request extensions to intercept the // Unlike other actions, allow web request extensions to intercept the
// request here. The headers will be removed during subsequent request // request here. The headers will be removed during subsequent request
// stages. // stages.
DCHECK(std::all_of(request->dnr_actions.begin(), DCHECK(std::all_of(request->dnr_actions->begin(),
request->dnr_actions.end(), [](const auto& action) { request->dnr_actions->end(), [](const auto& action) {
return action.type == Action::Type::REMOVE_HEADERS; return action.type == Action::Type::REMOVE_HEADERS;
})); }));
break; break;
...@@ -1163,10 +1159,10 @@ int ExtensionWebRequestEventRouter::OnBeforeSendHeaders( ...@@ -1163,10 +1159,10 @@ int ExtensionWebRequestEventRouter::OnBeforeSendHeaders(
// Remove request headers for the Declarative Net Request API. It is given // Remove request headers for the Declarative Net Request API. It is given
// preference over the Web Request API and this also hides the removed headers // preference over the Web Request API and this also hides the removed headers
// from extensions using the Web Request API. // from extensions using the Web Request API.
DCHECK(!request->dnr_actions.empty()); DCHECK(request->dnr_actions);
std::set<std::string> removed_headers; std::set<std::string> removed_headers;
for (const auto& action : request->dnr_actions) { for (const auto& action : *request->dnr_actions) {
bool headers_removed_for_action = bool headers_removed_for_action =
RemoveRequestHeadersForAction(headers, action, &removed_headers); RemoveRequestHeadersForAction(headers, action, &removed_headers);
...@@ -1258,9 +1254,10 @@ int ExtensionWebRequestEventRouter::OnHeadersReceived( ...@@ -1258,9 +1254,10 @@ int ExtensionWebRequestEventRouter::OnHeadersReceived(
// Handle header removal by the Declarative Net Request API. We filter these // Handle header removal by the Declarative Net Request API. We filter these
// headers so that headers removed by Declarative Net Request API are not // headers so that headers removed by Declarative Net Request API are not
// visible to web request extensions. // visible to web request extensions.
DCHECK(!request->dnr_actions.empty()); DCHECK(request->dnr_actions);
bool should_remove_headers = bool should_remove_headers =
request->dnr_actions[0].type == Action::Type::REMOVE_HEADERS && !request->dnr_actions->empty() &&
(*request->dnr_actions)[0].type == Action::Type::REMOVE_HEADERS &&
original_response_headers; original_response_headers;
scoped_refptr<const net::HttpResponseHeaders> filtered_response_headers; scoped_refptr<const net::HttpResponseHeaders> filtered_response_headers;
...@@ -1274,7 +1271,7 @@ int ExtensionWebRequestEventRouter::OnHeadersReceived( ...@@ -1274,7 +1271,7 @@ int ExtensionWebRequestEventRouter::OnHeadersReceived(
original_response_headers->raw_headers()); original_response_headers->raw_headers());
bool headers_filtered = false; bool headers_filtered = false;
for (const auto& action : request->dnr_actions) { for (const auto& action : *request->dnr_actions) {
bool headers_filtered_for_action = FilterResponseHeaders( bool headers_filtered_for_action = FilterResponseHeaders(
mutable_response_headers.get(), action.response_headers_to_remove); mutable_response_headers.get(), action.response_headers_to_remove);
......
...@@ -328,7 +328,7 @@ static_assert(ValidateHeaderEntries(kResponseHeaderEntries), ...@@ -328,7 +328,7 @@ static_assert(ValidateHeaderEntries(kResponseHeaderEntries),
bool HasMatchingRemovedDNRRequestHeader( bool HasMatchingRemovedDNRRequestHeader(
const extensions::WebRequestInfo& request, const extensions::WebRequestInfo& request,
const std::string& header) { const std::string& header) {
for (const auto& action : request.dnr_actions) { for (const auto& action : *request.dnr_actions) {
if (std::find_if(action.request_headers_to_remove.begin(), if (std::find_if(action.request_headers_to_remove.begin(),
action.request_headers_to_remove.end(), action.request_headers_to_remove.end(),
[&header](const char* header_to_remove) { [&header](const char* header_to_remove) {
...@@ -345,7 +345,7 @@ bool HasMatchingRemovedDNRRequestHeader( ...@@ -345,7 +345,7 @@ bool HasMatchingRemovedDNRRequestHeader(
bool HasMatchingRemovedDNRResponseHeader( bool HasMatchingRemovedDNRResponseHeader(
const extensions::WebRequestInfo& request, const extensions::WebRequestInfo& request,
const std::string& header) { const std::string& header) {
for (const auto& action : request.dnr_actions) { for (const auto& action : *request.dnr_actions) {
if (std::find_if(action.response_headers_to_remove.begin(), if (std::find_if(action.response_headers_to_remove.begin(),
action.response_headers_to_remove.end(), action.response_headers_to_remove.end(),
[&header](const char* header_to_remove) { [&header](const char* header_to_remove) {
...@@ -968,7 +968,7 @@ void MergeOnBeforeSendHeadersResponses( ...@@ -968,7 +968,7 @@ void MergeOnBeforeSendHeadersResponses(
// Prevent extensions from adding any header removed by the Declarative // Prevent extensions from adding any header removed by the Declarative
// Net Request API. // Net Request API.
DCHECK(!request.dnr_actions.empty()); DCHECK(request.dnr_actions);
if (HasMatchingRemovedDNRRequestHeader(request, key)) { if (HasMatchingRemovedDNRRequestHeader(request, key)) {
extension_conflicts = true; extension_conflicts = true;
break; break;
...@@ -1340,8 +1340,7 @@ void MergeOnHeadersReceivedResponses( ...@@ -1340,8 +1340,7 @@ void MergeOnHeadersReceivedResponses(
// Prevent extensions from adding any response header which was removed by // Prevent extensions from adding any response header which was removed by
// the Declarative Net Request API. // the Declarative Net Request API.
DCHECK(!request.dnr_actions.empty()); DCHECK(request.dnr_actions);
if (!extension_conflicts) { if (!extension_conflicts) {
for (const ResponseHeader& header : delta.added_response_headers) { for (const ResponseHeader& header : delta.added_response_headers) {
if (HasMatchingRemovedDNRResponseHeader(request, header.first)) { if (HasMatchingRemovedDNRResponseHeader(request, header.first)) {
......
...@@ -166,11 +166,12 @@ struct WebRequestInfo { ...@@ -166,11 +166,12 @@ struct WebRequestInfo {
const int web_view_rules_registry_id; const int web_view_rules_registry_id;
const int web_view_embedder_process_id; const int web_view_embedder_process_id;
// The Declarative Net Request action associated with this request. Mutable // The Declarative Net Request actions associated with this request. Mutable
// since this is lazily computed. Cached to avoid redundant computations. // since this is lazily computed. Cached to avoid redundant computations.
// Valid when non-empty. In case no action is taken, populated with // Valid when not null. In case no actions are taken, populated with an empty
// Action::Type::NONE. // vector.
mutable std::vector<declarative_net_request::RulesetManager::Action> mutable base::Optional<
std::vector<declarative_net_request::RulesetManager::Action>>
dnr_actions; dnr_actions;
const bool is_service_worker_script; const bool is_service_worker_script;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment