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 CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULES_REGISTRY_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULES_REGISTRY_H_
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
17 #include "chrome/browser/extensions/api/declarative_content/content_action.h"
18 #include "chrome/browser/extensions/api/declarative_content/content_condition.h"
19 #include "chrome/browser/extensions/api/declarative_content/declarative_content_condition_tracker_delegate.h"
20 #include "chrome/browser/extensions/api/declarative_content/declarative_content_css_condition_tracker.h"
21 #include "chrome/browser/extensions/api/declarative_content/declarative_content_is_bookmarked_condition_tracker.h"
22 #include "chrome/browser/extensions/api/declarative_content/declarative_content_page_url_condition_tracker.h"
23 #include "components/url_matcher/url_matcher.h"
24 #include "content/public/browser/notification_observer.h"
25 #include "content/public/browser/notification_registrar.h"
26 #include "extensions/browser/api/declarative_content/content_rules_registry.h"
28 class ContentPermissions
;
32 class RenderProcessHost
;
34 struct FrameNavigateParams
;
35 struct LoadCommittedDetails
;
38 namespace extension_web_request_api_helpers
{
39 struct EventResponseDelta
;
46 namespace extensions
{
48 // The ChromeContentRulesRegistry is responsible for managing
49 // the internal representation of rules for the Declarative Content API.
51 // Here is the high level overview of this functionality:
53 // api::events::Rule consists of conditions and actions, these are
54 // represented as a ContentRule with ContentConditions and ContentRuleActions.
56 // The evaluation of URL related condition attributes (host_suffix, path_prefix)
57 // is delegated to a URLMatcher, because this is capable of evaluating many
58 // of such URL related condition attributes in parallel.
60 // A note on incognito support: separate instances of ChromeContentRulesRegistry
61 // are created for incognito and non-incognito contexts. The incognito instance,
62 // however, is only responsible for applying rules registered by the incognito
63 // side of split-mode extensions to incognito tabs. The non-incognito instance
64 // handles incognito tabs for spanning-mode extensions, plus all non-incognito
66 class ChromeContentRulesRegistry
67 : public ContentRulesRegistry
,
68 public content::NotificationObserver
,
69 public DeclarativeContentConditionTrackerDelegate
{
71 // For testing, |ui_part| can be NULL. In that case it constructs the
72 // registry with storage functionality suspended.
73 ChromeContentRulesRegistry(content::BrowserContext
* browser_context
,
74 RulesCacheDelegate
* cache_delegate
);
76 // ContentRulesRegistry:
77 void MonitorWebContentsForRuleEvaluation(
78 content::WebContents
* contents
) override
;
79 void DidNavigateMainFrame(
80 content::WebContents
* tab
,
81 const content::LoadCommittedDetails
& details
,
82 const content::FrameNavigateParams
& params
) override
;
85 std::string
AddRulesImpl(
86 const std::string
& extension_id
,
87 const std::vector
<linked_ptr
<api::events::Rule
>>& rules
) override
;
88 std::string
RemoveRulesImpl(
89 const std::string
& extension_id
,
90 const std::vector
<std::string
>& rule_identifiers
) override
;
91 std::string
RemoveAllRulesImpl(const std::string
& extension_id
) override
;
93 // content::NotificationObserver:
94 void Observe(int type
,
95 const content::NotificationSource
& source
,
96 const content::NotificationDetails
& details
) override
;
98 // DeclarativeContentConditionTrackerDelegate:
99 void RequestEvaluation(content::WebContents
* contents
) override
;
100 bool ShouldManageConditionsForBrowserContext(
101 content::BrowserContext
* context
) override
;
103 // Returns true if this object retains no allocated data. Only for debugging.
104 bool IsEmpty() const;
106 // TODO(wittman): Remove once DeclarativeChromeContentRulesRegistry no longer
107 // depends on concrete condition implementations. At that point
108 // DeclarativeChromeContentRulesRegistryTest.ActiveRulesDoesntGrow will be
109 // able to use a test condition object and not need to depend on force setting
110 // matching CSS seleectors.
111 void UpdateMatchingCssSelectorsForTesting(
112 content::WebContents
* contents
,
113 const std::vector
<std::string
>& matching_css_selectors
);
115 // Returns the number of active rules.
116 size_t GetActiveRulesCountForTesting();
119 ~ChromeContentRulesRegistry() override
;
122 // The internal declarative rule representation. Corresponds to a declarative
123 // API rule: https://developer.chrome.com/extensions/events.html#declarative.
126 ContentRule(const Extension
* extension
,
127 ScopedVector
<const ContentCondition
> conditions
,
128 ScopedVector
<const ContentAction
> actions
,
132 const Extension
* extension
;
133 ScopedVector
<const ContentCondition
> conditions
;
134 ScopedVector
<const ContentAction
> actions
;
138 DISALLOW_COPY_AND_ASSIGN(ContentRule
);
141 // Specifies what to do with evaluation requests.
142 // TODO(wittman): Try to eliminate the need for IGNORE after refactoring to
143 // treat all condition evaluation consistently. Currently RemoveRulesImpl only
144 // updates the CSS selectors after the rules are removed, which is too late
146 enum EvaluationDisposition
{
147 EVALUATE_REQUESTS
, // Evaluate immediately.
148 DEFER_REQUESTS
, // Defer for later evaluation.
149 IGNORE_REQUESTS
// Ignore.
152 class EvaluationScope
;
154 // Creates a ContentRule for |extension| given a json definition. The format
155 // of each condition and action's json is up to the specific ContentCondition
156 // and ContentAction. |extension| may be NULL in tests. If |error| is empty,
157 // the translation was successful and the returned rule is internally
159 scoped_ptr
<const ContentRule
> CreateRule(const Extension
* extension
,
160 const api::events::Rule
& api_rule
,
163 // True if this object is managing the rules for |context|.
164 bool ManagingRulesForBrowserContext(content::BrowserContext
* context
);
166 std::set
<const ContentRule
*> GetMatches(
167 const RendererContentMatchData
& renderer_data
,
168 bool is_incognito_renderer
) const;
170 // Updates the condition evaluator with the current watched CSS selectors.
171 void UpdateCssSelectorsFromRules();
173 // Evaluates the conditions for |tab| based on the tab state and matching CSS
175 void EvaluateConditionsForTab(content::WebContents
* tab
);
177 // Evaluates the conditions for tabs in each browser window.
178 void EvaluateConditionsForAllTabs();
180 using ExtensionRuleIdPair
= std::pair
<const Extension
*, std::string
>;
181 using RuleAndConditionForURLMatcherId
=
182 std::map
<url_matcher::URLMatcherConditionSet::ID
,
183 std::pair
<const ContentRule
*, const ContentCondition
*>>;
184 using RulesMap
= std::map
<ExtensionRuleIdPair
, linked_ptr
<const ContentRule
>>;
186 // Map that tells us which ContentRules and ContentConditions may
187 // match for a URLMatcherConditionSet::ID returned by the |url_matcher_|.
188 RuleAndConditionForURLMatcherId rule_and_conditions_for_match_id_
;
190 RulesMap content_rules_
;
192 // Maps a WebContents to the set of rules that match on that WebContents.
193 // This lets us call Revert as appropriate. Note that this is expected to have
194 // a key-value pair for every WebContents the registry is tracking, even if
195 // the value is the empty set.
196 std::map
<content::WebContents
*, std::set
<const ContentRule
*>> active_rules_
;
198 // Responsible for tracking declarative content page URL condition state.
199 DeclarativeContentPageUrlConditionTracker page_url_condition_tracker_
;
201 // Responsible for tracking declarative content CSS condition state.
202 DeclarativeContentCssConditionTracker css_condition_tracker_
;
204 // Responsible for tracking declarative content bookmarked condition state.
205 DeclarativeContentIsBookmarkedConditionTracker
206 is_bookmarked_condition_tracker_
;
208 // Specifies what to do with evaluation requests.
209 EvaluationDisposition evaluation_disposition_
;
211 // Contains WebContents which require rule evaluation. Only used while
212 // |evaluation_disposition_| is DEFER.
213 std::set
<content::WebContents
*> evaluation_pending_
;
215 // Manages our notification registrations.
216 content::NotificationRegistrar registrar_
;
218 DISALLOW_COPY_AND_ASSIGN(ChromeContentRulesRegistry
);
221 } // namespace extensions
223 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULES_REGISTRY_H_