Commit abf8d983 authored by Kelvin Jiang's avatar Kelvin Jiang Committed by Commit Bot

[DNR] Add flatbuffer schemas and indexing for modifyHeader rules

Bug: 947591
Change-Id: I06b2ff6505ed0f1efd326df2a73cac8ee3ec35bf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2081363
Commit-Queue: Kelvin Jiang <kelvinjiang@chromium.org>
Reviewed-by: default avatarKaran Bhatia <karandeepb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#751592}
parent 05edc003
......@@ -59,7 +59,9 @@ bool IsExtraHeadersMatcherInternal(
const ExtensionUrlPatternIndexMatcher::UrlPatternIndexList* index_list) {
// We only support removing a subset of extra headers currently. If that
// changes, the implementation here should change as well.
static_assert(flat::IndexType_count == 5,
// TODO(crbug.com/947591): Modify this method for
// flat::IndexType_modify_headers.
static_assert(flat::IndexType_count == 6,
"Modify this method to ensure IsExtraHeadersMatcherInternal is "
"updated as new actions are added.");
static const flat::IndexType extra_header_indices[] = {
......@@ -189,6 +191,7 @@ ExtensionUrlPatternIndexMatcher::GetBeforeRequestActionHelper(
return CreateUpgradeAction(params, *rule);
case flat::ActionType_allow_all_requests:
case flat::ActionType_remove_headers:
case flat::ActionType_modify_headers:
case flat::ActionType_count:
NOTREACHED();
}
......
......@@ -18,6 +18,7 @@ enum ActionType : ubyte {
redirect,
upgrade_scheme,
remove_headers,
modify_headers,
allow_all_requests,
/// Number of actions. Must be the last entry.
count
......@@ -74,6 +75,11 @@ table UrlRuleMetadata {
/// UrlTransform for this rule.
transform : UrlTransform;
/// A list of ModifyHeaderInfo, for both request and response headers. Valid
/// for "modifyHeaders" rules.
request_headers: [ModifyHeaderInfo];
response_headers: [ModifyHeaderInfo];
}
/// This provides a mapping from an index type to its index within
......@@ -91,6 +97,8 @@ enum IndexType : ubyte {
remove_referer_header,
remove_set_cookie_header,
modify_headers,
/// Number of indices. Must be the last entry.
count
}
......@@ -103,6 +111,19 @@ enum RemoveHeaderType : ubyte (bit_flags) {
set_cookie
}
/// The type of header operation for modifyHeaders rules. Corresponds to
/// extensions::api::declarative_net_request::HeaderOperation.
enum HeaderOperation : ubyte {
remove
}
/// Describes the header to be modified and operation to be performed on it.
/// Corresponds to extensions::api::declarative_net_request::ModifyHeaderInfo.
table ModifyHeaderInfo {
operation: HeaderOperation;
header: string;
}
/// Completely represents a rule with a regex filter.
table RegexRule {
/// The underlying UrlRule.
......
......@@ -152,6 +152,33 @@ FlatOffset<flat::UrlTransform> BuildTransformOffset(
clear_fragment, fragment, username, password);
}
FlatVectorOffset<flat::ModifyHeaderInfo> BuildModifyHeaderInfoOffset(
flatbuffers::FlatBufferBuilder* builder,
const std::vector<dnr_api::ModifyHeaderInfo>& modify_header_list) {
std::vector<FlatOffset<flat::ModifyHeaderInfo>> flat_modify_header_list;
flat_modify_header_list.reserve(modify_header_list.size());
for (const dnr_api::ModifyHeaderInfo& header_info : modify_header_list) {
flat::HeaderOperation operation = flat::HeaderOperation_remove;
switch (header_info.operation) {
case dnr_api::HeaderOperation::HEADER_OPERATION_NONE:
NOTREACHED();
break;
case dnr_api::HEADER_OPERATION_REMOVE:
operation = flat::HeaderOperation_remove;
break;
}
FlatStringOffset header_name =
builder->CreateSharedString(header_info.header);
flat_modify_header_list.push_back(
flat::CreateModifyHeaderInfo(*builder, operation, header_name));
}
return builder->CreateVector(flat_modify_header_list);
}
} // namespace
FlatRulesetIndexer::FlatRulesetIndexer()
......@@ -206,10 +233,17 @@ void FlatRulesetIndexer::AddUrlRule(const IndexedRule& indexed_rule) {
transform_offset =
BuildTransformOffset(&builder_, *indexed_rule.url_transform);
}
FlatVectorOffset<flat::ModifyHeaderInfo> request_headers_offset =
BuildModifyHeaderInfoOffset(&builder_, indexed_rule.request_headers);
FlatVectorOffset<flat::ModifyHeaderInfo> response_headers_offset =
BuildModifyHeaderInfoOffset(&builder_, indexed_rule.response_headers);
metadata_.push_back(flat::CreateUrlRuleMetadata(
builder_, indexed_rule.id,
ConvertToFlatActionType(indexed_rule.action_type), redirect_url_offset,
transform_offset));
transform_offset, request_headers_offset, response_headers_offset));
}
void FlatRulesetIndexer::Finish() {
......@@ -281,6 +315,7 @@ FlatRulesetIndexer::GetBuilders(const IndexedRule& indexed_rule) {
case dnr_api::RULE_ACTION_TYPE_REMOVEHEADERS:
return GetRemoveHeaderBuilders(indexed_rule.remove_headers_set);
case dnr_api::RULE_ACTION_TYPE_MODIFYHEADERS:
return {index_builders_[flat::IndexType_modify_headers].get()};
case dnr_api::RULE_ACTION_TYPE_NONE:
break;
}
......
......@@ -26,6 +26,7 @@ enum ActionType : ubyte {
redirect,
upgrade_scheme,
remove_headers,
modify_headers,
allow_all_requests,
count
}
......@@ -54,6 +55,8 @@ table UrlRuleMetadata {
action : ActionType;
redirect_url : string;
transform : UrlTransform;
request_headers: [ModifyHeaderInfo];
response_headers: [ModifyHeaderInfo];
}
enum IndexType : ubyte {
before_request_except_allow_all_requests = 0,
......@@ -61,6 +64,7 @@ enum IndexType : ubyte {
remove_cookie_header,
remove_referer_header,
remove_set_cookie_header,
modify_headers,
count
}
enum RemoveHeaderType : ubyte (bit_flags) {
......@@ -68,6 +72,13 @@ enum RemoveHeaderType : ubyte (bit_flags) {
referer,
set_cookie
}
enum HeaderOperation : ubyte {
remove
}
table ModifyHeaderInfo {
operation: HeaderOperation;
header: string;
}
table RegexRule {
url_rule: url_pattern_index.flat.UrlRule;
action_type: ActionType;
......@@ -145,7 +156,7 @@ TEST_F(IndexedRulesetFormatVersionTest, CheckVersionUpdated) {
EXPECT_EQ(StripCommentsAndWhitespace(kFlatbufferSchemaExpected),
StripCommentsAndWhitespace(flatbuffer_schema))
<< "Schema change detected; update this test and the schema version.";
EXPECT_EQ(15, GetIndexedRulesetFormatVersionForTesting())
EXPECT_EQ(16, GetIndexedRulesetFormatVersionForTesting())
<< "Update this test if you update the schema version.";
}
......
......@@ -25,7 +25,9 @@ bool IsExtraHeadersMatcherInternal(
// We only support removing a subset of extra headers currently. If that
// changes, the implementation here should change as well.
static_assert(flat::ActionType_count == 6,
// TODO(crbug.com/947591): Modify this method for
// flat::ActionType_modify_headers.
static_assert(flat::ActionType_count == 7,
"Modify this method to ensure IsExtraHeadersMatcherInternal is "
"updated as new actions are added.");
......@@ -64,6 +66,7 @@ bool IsBeforeRequestAction(flat::ActionType action_type) {
case flat::ActionType_allow_all_requests:
return true;
case flat::ActionType_remove_headers:
case flat::ActionType_modify_headers:
return false;
case flat::ActionType_count:
NOTREACHED();
......@@ -185,6 +188,7 @@ RegexRulesMatcher::GetBeforeRequestActionIgnoringAncestors(
case flat::ActionType_allow_all_requests:
return CreateAllowAllRequestsAction(params, rule);
case flat::ActionType_remove_headers:
case flat::ActionType_modify_headers:
case flat::ActionType_count:
NOTREACHED();
break;
......
......@@ -180,7 +180,9 @@ bool RulesetManager::HasExtraHeadersMatcherForRequest(
// We only support removing a subset of extra headers currently. If that
// changes, the implementation here should change as well.
static_assert(flat::ActionType_count == 6,
// TODO(crbug.com/947591): Modify this method for
// flat::ActionType_modify_headers.
static_assert(flat::ActionType_count == 7,
"Modify this method to ensure HasExtraHeadersMatcherForRequest "
"is updated as new actions are added.");
......
......@@ -57,7 +57,9 @@ RequestAction CreateRequestActionForTesting(RequestAction::Type type,
// with gtest. This reuses the logic used to test action equality in
// TestRequestACtion in test_utils.h.
bool operator==(const RequestAction& lhs, const RequestAction& rhs) {
static_assert(flat::IndexType_count == 5,
// TODO(crbug.com/947591): Modify this method for
// flat::IndexType_modify_headers.
static_assert(flat::IndexType_count == 6,
"Modify this method to ensure it stays updated as new actions "
"are added.");
......
......@@ -37,7 +37,7 @@ namespace dnr_api = api::declarative_net_request;
// url_pattern_index.fbs. Whenever an extension with an indexed ruleset format
// version different from the one currently used by Chrome is loaded, the
// extension ruleset will be reindexed.
constexpr int kIndexedRulesetFormatVersion = 15;
constexpr int kIndexedRulesetFormatVersion = 16;
// This static assert is meant to catch cases where
// url_pattern_index::kUrlPatternIndexFormatVersion is incremented without
......@@ -256,11 +256,12 @@ flat::ActionType ConvertToFlatActionType(dnr_api::RuleActionType action_type) {
return flat::ActionType_redirect;
case dnr_api::RULE_ACTION_TYPE_REMOVEHEADERS:
return flat::ActionType_remove_headers;
case dnr_api::RULE_ACTION_TYPE_MODIFYHEADERS:
return flat::ActionType_modify_headers;
case dnr_api::RULE_ACTION_TYPE_UPGRADESCHEME:
return flat::ActionType_upgrade_scheme;
case dnr_api::RULE_ACTION_TYPE_ALLOWALLREQUESTS:
return flat::ActionType_allow_all_requests;
case dnr_api::RULE_ACTION_TYPE_MODIFYHEADERS:
case dnr_api::RULE_ACTION_TYPE_NONE:
break;
}
......
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