1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_BROWSER_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_RULES_REGISTRY_H_
6 #define EXTENSIONS_BROWSER_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_RULES_REGISTRY_H_
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/linked_ptr.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/time/time.h"
19 #include "components/url_matcher/url_matcher.h"
20 #include "extensions/browser/api/declarative/declarative_rule.h"
21 #include "extensions/browser/api/declarative/rules_registry.h"
22 #include "extensions/browser/api/declarative_webrequest/request_stage.h"
23 #include "extensions/browser/api/declarative_webrequest/webrequest_action.h"
24 #include "extensions/browser/api/declarative_webrequest/webrequest_condition.h"
25 #include "extensions/browser/info_map.h"
27 class WebRequestPermissions
;
33 namespace extension_web_request_api_helpers
{
34 struct EventResponseDelta
;
41 namespace extensions
{
43 class RulesRegistryService
;
45 typedef linked_ptr
<extension_web_request_api_helpers::EventResponseDelta
>
46 LinkedPtrEventResponseDelta
;
47 typedef DeclarativeRule
<WebRequestCondition
, WebRequestAction
> WebRequestRule
;
49 // The WebRequestRulesRegistry is responsible for managing
50 // the internal representation of rules for the Declarative Web Request API.
52 // Here is the high level overview of this functionality:
54 // api::events::Rule consists of Conditions and Actions, these are
55 // represented as a WebRequestRule with WebRequestConditions and
56 // WebRequestRuleActions.
58 // WebRequestConditions represent JSON dictionaries as the following:
60 // 'instanceType': 'URLMatcher',
61 // 'host_suffix': 'example.com',
62 // 'path_prefix': '/query',
66 // The evaluation of URL related condition attributes (host_suffix, path_prefix)
67 // is delegated to a URLMatcher, because this is capable of evaluating many
68 // of such URL related condition attributes in parallel.
70 // For this, the URLRequestCondition has a URLMatcherConditionSet, which
71 // represents the {'host_suffix': 'example.com', 'path_prefix': '/query'} part.
72 // We will then ask the URLMatcher, whether a given URL
73 // "http://www.example.com/query/" has any matches, and the URLMatcher
74 // will respond with the URLMatcherConditionSet::ID. We can map this
75 // to the WebRequestRule and check whether also the other conditions (in this
76 // example 'scheme': 'http') are fulfilled.
77 class WebRequestRulesRegistry
: public RulesRegistry
{
79 // |cache_delegate| can be NULL. In that case it constructs the registry with
80 // storage functionality suspended.
81 WebRequestRulesRegistry(content::BrowserContext
* browser_context
,
82 RulesCacheDelegate
* cache_delegate
,
83 int rules_registry_id
);
85 // TODO(battre): This will become an implementation detail, because we need
86 // a way to also execute the actions of the rules.
87 std::set
<const WebRequestRule
*> GetMatches(
88 const WebRequestData
& request_data_without_ids
) const;
90 // Returns which modifications should be executed on the network request
91 // according to the rules registered in this registry.
92 std::list
<LinkedPtrEventResponseDelta
> CreateDeltas(
93 const InfoMap
* extension_info_map
,
94 const WebRequestData
& request_data
,
95 bool crosses_incognito
);
97 // Implementation of RulesRegistry:
98 std::string
AddRulesImpl(
99 const std::string
& extension_id
,
100 const std::vector
<linked_ptr
<api::events::Rule
>>& rules
) override
;
101 std::string
RemoveRulesImpl(
102 const std::string
& extension_id
,
103 const std::vector
<std::string
>& rule_identifiers
) override
;
104 std::string
RemoveAllRulesImpl(const std::string
& extension_id
) override
;
106 // Returns true if this object retains no allocated data. Only for debugging.
107 bool IsEmpty() const;
110 ~WebRequestRulesRegistry() override
;
112 // Virtual for testing:
113 virtual base::Time
GetExtensionInstallationTime(
114 const std::string
& extension_id
) const;
115 virtual void ClearCacheOnNavigation();
117 void SetExtensionInfoMapForTesting(
118 scoped_refptr
<InfoMap
> extension_info_map
) {
119 extension_info_map_
= extension_info_map
;
122 const std::set
<const WebRequestRule
*>&
123 rules_with_untriggered_conditions_for_test() const {
124 return rules_with_untriggered_conditions_
;
128 FRIEND_TEST_ALL_PREFIXES(WebRequestRulesRegistrySimpleTest
, StageChecker
);
129 FRIEND_TEST_ALL_PREFIXES(WebRequestRulesRegistrySimpleTest
,
130 HostPermissionsChecker
);
132 typedef std::map
<url_matcher::URLMatcherConditionSet::ID
,
133 const WebRequestRule
*> RuleTriggers
;
134 typedef std::map
<WebRequestRule::RuleId
, linked_ptr
<const WebRequestRule
>>
136 typedef std::set
<url_matcher::URLMatcherConditionSet::ID
> URLMatches
;
137 typedef std::set
<const WebRequestRule
*> RuleSet
;
139 // This bundles all consistency checkers. Returns true in case of consistency
140 // and MUST set |error| otherwise.
141 static bool Checker(const Extension
* extension
,
142 const WebRequestConditionSet
* conditions
,
143 const WebRequestActionSet
* actions
,
146 // Check that the |extension| has host permissions for all URLs if actions
147 // requiring them are present.
148 static bool HostPermissionsChecker(const Extension
* extension
,
149 const WebRequestActionSet
* actions
,
152 // Check that every action is applicable in the same request stage as at
153 // least one condition.
154 static bool StageChecker(const WebRequestConditionSet
* conditions
,
155 const WebRequestActionSet
* actions
,
158 // Helper for RemoveRulesImpl and RemoveAllRulesImpl. Call this before
159 // deleting |rule| from one of the maps in |webrequest_rules_|. It will erase
160 // the rule from |rule_triggers_| and |rules_with_untriggered_conditions_|,
161 // and add every of the rule's URLMatcherConditionSet to
162 // |remove_from_url_matcher|, so that the caller can remove them from the
164 void CleanUpAfterRule(const WebRequestRule
* rule
,
165 std::vector
<url_matcher::URLMatcherConditionSet::ID
>*
166 remove_from_url_matcher
);
168 // This is a helper function to GetMatches. Rules triggered by |url_matches|
169 // get added to |result| if one of their conditions is fulfilled.
170 // |request_data| gets passed to IsFulfilled of the rules' condition sets.
171 void AddTriggeredRules(const URLMatches
& url_matches
,
172 const WebRequestCondition::MatchData
& request_data
,
173 RuleSet
* result
) const;
175 // Map that tells us which WebRequestRule may match under the condition that
176 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|.
177 RuleTriggers rule_triggers_
;
179 // These rules contain condition sets with conditions without URL attributes.
180 // Such conditions are not triggered by URL matcher, so we need to test them
182 std::set
<const WebRequestRule
*> rules_with_untriggered_conditions_
;
184 std::map
<WebRequestRule::ExtensionId
, RulesMap
> webrequest_rules_
;
186 url_matcher::URLMatcher url_matcher_
;
188 content::BrowserContext
* browser_context_
;
189 scoped_refptr
<InfoMap
> extension_info_map_
;
191 DISALLOW_COPY_AND_ASSIGN(WebRequestRulesRegistry
);
194 } // namespace extensions
196 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_RULES_REGISTRY_H_