OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULES_R
EGISTRY_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULES_R
EGISTRY_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULES_R
EGISTRY_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULES_R
EGISTRY_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "chrome/browser/extensions/api/declarative_content/content_action.h" | 17 #include "chrome/browser/extensions/api/declarative_content/content_action.h" |
18 #include "chrome/browser/extensions/api/declarative_content/content_condition.h" | 18 #include "chrome/browser/extensions/api/declarative_content/content_condition.h" |
19 #include "chrome/browser/extensions/api/declarative_content/declarative_content_
css_condition_tracker.h" | |
20 #include "components/url_matcher/url_matcher.h" | 19 #include "components/url_matcher/url_matcher.h" |
21 #include "content/public/browser/notification_observer.h" | 20 #include "content/public/browser/notification_observer.h" |
22 #include "content/public/browser/notification_registrar.h" | 21 #include "content/public/browser/notification_registrar.h" |
23 #include "extensions/browser/api/declarative/declarative_rule.h" | 22 #include "extensions/browser/api/declarative/declarative_rule.h" |
24 #include "extensions/browser/api/declarative_content/content_rules_registry.h" | 23 #include "extensions/browser/api/declarative_content/content_rules_registry.h" |
25 #include "extensions/browser/info_map.h" | 24 #include "extensions/browser/info_map.h" |
26 | 25 |
27 class ContentPermissions; | 26 class ContentPermissions; |
28 | 27 |
29 namespace content { | 28 namespace content { |
(...skipping 30 matching lines...) Expand all Loading... |
60 // The evaluation of URL related condition attributes (host_suffix, path_prefix) | 59 // The evaluation of URL related condition attributes (host_suffix, path_prefix) |
61 // is delegated to a URLMatcher, because this is capable of evaluating many | 60 // is delegated to a URLMatcher, because this is capable of evaluating many |
62 // of such URL related condition attributes in parallel. | 61 // of such URL related condition attributes in parallel. |
63 // | 62 // |
64 // A note on incognito support: separate instances of ChromeContentRulesRegistry | 63 // A note on incognito support: separate instances of ChromeContentRulesRegistry |
65 // are created for incognito and non-incognito contexts. The incognito instance, | 64 // are created for incognito and non-incognito contexts. The incognito instance, |
66 // however, is only responsible for applying rules registered by the incognito | 65 // however, is only responsible for applying rules registered by the incognito |
67 // side of split-mode extensions to incognito tabs. The non-incognito instance | 66 // side of split-mode extensions to incognito tabs. The non-incognito instance |
68 // handles incognito tabs for spanning-mode extensions, plus all non-incognito | 67 // handles incognito tabs for spanning-mode extensions, plus all non-incognito |
69 // tabs. | 68 // tabs. |
70 class ChromeContentRulesRegistry | 69 class ChromeContentRulesRegistry : public ContentRulesRegistry, |
71 : public ContentRulesRegistry, | 70 public content::NotificationObserver { |
72 public content::NotificationObserver, | |
73 public DeclarativeContentCssConditionTrackerDelegate { | |
74 public: | 71 public: |
75 // For testing, |ui_part| can be NULL. In that case it constructs the | 72 // For testing, |ui_part| can be NULL. In that case it constructs the |
76 // registry with storage functionality suspended. | 73 // registry with storage functionality suspended. |
77 ChromeContentRulesRegistry(content::BrowserContext* browser_context, | 74 ChromeContentRulesRegistry(content::BrowserContext* browser_context, |
78 RulesCacheDelegate* cache_delegate); | 75 RulesCacheDelegate* cache_delegate); |
79 | 76 |
80 // ContentRulesRegistry: | 77 // ChromeContentRulesRegistry implementation: |
| 78 // Applies all content rules given an update (CSS match change or |
| 79 // page navigation, for now) from the renderer. |
| 80 void Apply(content::WebContents* contents, |
| 81 const std::vector<std::string>& matching_css_selectors) override; |
| 82 |
81 // Applies all content rules given that a tab was just navigated. | 83 // Applies all content rules given that a tab was just navigated. |
82 void DidNavigateMainFrame( | 84 void DidNavigateMainFrame( |
83 content::WebContents* tab, | 85 content::WebContents* tab, |
84 const content::LoadCommittedDetails& details, | 86 const content::LoadCommittedDetails& details, |
85 const content::FrameNavigateParams& params) override; | 87 const content::FrameNavigateParams& params) override; |
86 | 88 |
87 // Applies all content rules given that a tab was just navigated on the | 89 // Applies all content rules given that a tab was just navigated on the |
88 // original context. Only invoked on the OffTheRecord registry instance. |tab| | 90 // original context. Only invoked on the OffTheRecord registry instance. |tab| |
89 // is owned by the original context, not this one. | 91 // is owned by the original context, not this one. |
90 void DidNavigateMainFrameOfOriginalContext( | 92 void DidNavigateMainFrameOfOriginalContext( |
91 content::WebContents* tab, | 93 content::WebContents* tab, |
92 const content::LoadCommittedDetails& details, | 94 const content::LoadCommittedDetails& details, |
93 const content::FrameNavigateParams& params) override; | 95 const content::FrameNavigateParams& params) override; |
94 | 96 |
95 // RulesRegistry: | 97 // Implementation of RulesRegistry: |
96 std::string AddRulesImpl( | 98 std::string AddRulesImpl( |
97 const std::string& extension_id, | 99 const std::string& extension_id, |
98 const std::vector<linked_ptr<RulesRegistry::Rule>>& rules) override; | 100 const std::vector<linked_ptr<RulesRegistry::Rule>>& rules) override; |
99 std::string RemoveRulesImpl( | 101 std::string RemoveRulesImpl( |
100 const std::string& extension_id, | 102 const std::string& extension_id, |
101 const std::vector<std::string>& rule_identifiers) override; | 103 const std::vector<std::string>& rule_identifiers) override; |
102 std::string RemoveAllRulesImpl(const std::string& extension_id) override; | 104 std::string RemoveAllRulesImpl(const std::string& extension_id) override; |
103 | 105 |
104 // content::NotificationObserver: | 106 // content::NotificationObserver implementation. |
105 void Observe(int type, | 107 void Observe(int type, |
106 const content::NotificationSource& source, | 108 const content::NotificationSource& source, |
107 const content::NotificationDetails& details) override; | 109 const content::NotificationDetails& details) override; |
108 | 110 |
109 // DeclarativeContentCssConditionTrackerDelegate: | |
110 void RequestEvaluation(content::WebContents* contents) override; | |
111 bool ShouldManageConditionsForBrowserContext( | |
112 content::BrowserContext* context) override; | |
113 | |
114 // Returns true if this object retains no allocated data. Only for debugging. | 111 // Returns true if this object retains no allocated data. Only for debugging. |
115 bool IsEmpty() const; | 112 bool IsEmpty() const; |
116 | 113 |
117 // TODO(wittman): Remove once DeclarativeChromeContentRulesRegistry no longer | |
118 // depends on concrete condition implementations. At that point | |
119 // DeclarativeChromeContentRulesRegistryTest.ActiveRulesDoesntGrow will be | |
120 // able to use a test condition object and not need to depend on force setting | |
121 // matching CSS seleectors. | |
122 void UpdateMatchingCssSelectorsForTesting( | |
123 content::WebContents* contents, | |
124 const std::vector<std::string>& matching_css_selectors); | |
125 | |
126 // Returns the number of active rules. | |
127 size_t GetActiveRulesCountForTesting(); | |
128 | |
129 protected: | 114 protected: |
130 ~ChromeContentRulesRegistry() override; | 115 ~ChromeContentRulesRegistry() override; |
131 | 116 |
132 // Virtual for testing: | 117 // Virtual for testing: |
133 virtual base::Time GetExtensionInstallationTime( | 118 virtual base::Time GetExtensionInstallationTime( |
134 const std::string& extension_id) const; | 119 const std::string& extension_id) const; |
135 | 120 |
136 private: | 121 private: |
137 // Utility function for iterating a lambda over the relevant WebContents. | 122 friend class DeclarativeChromeContentRulesRegistryTest; |
138 template <class Func> | |
139 void ForEachWebContents(const Func& func); | |
140 | |
141 // Set up the state to track rules for |contents|. | |
142 void TrackRulesForWebContents(content::WebContents* contents); | |
143 | 123 |
144 // True if this object is managing the rules for |context|. | 124 // True if this object is managing the rules for |context|. |
145 bool ManagingRulesForBrowserContext(content::BrowserContext* context); | 125 bool ManagingRulesForBrowserContext(content::BrowserContext* context); |
146 | 126 |
| 127 // Applies all content rules given that a tab was just navigated. |
| 128 void OnTabNavigation(content::WebContents* tab, bool is_in_page_navigation); |
| 129 |
147 std::set<const ContentRule*> GetMatches( | 130 std::set<const ContentRule*> GetMatches( |
148 const RendererContentMatchData& renderer_data, | 131 const RendererContentMatchData& renderer_data, |
149 bool is_incognito_renderer) const; | 132 bool is_incognito_renderer) const; |
150 | 133 |
151 // Updates the condition evaluator with the current watched CSS selectors. | 134 // Scans the rules for the set of conditions they're watching. If the set has |
152 void UpdateCssSelectorsFromRules(); | 135 // changed, calls InstructRenderProcess() for each RenderProcessHost in the |
| 136 // current browser_context. |
| 137 void UpdateConditionCache(); |
| 138 |
| 139 // If the renderer process is associated with our browser context, tells it |
| 140 // what page attributes to watch for using an ExtensionMsg_WatchPages. |
| 141 void InstructRenderProcessIfSameBrowserContext( |
| 142 content::RenderProcessHost* process); |
153 | 143 |
154 // Evaluates the conditions for |tab| based on the tab state and matching CSS | 144 // Evaluates the conditions for |tab| based on the tab state and matching CSS |
155 // selectors. | 145 // selectors. |
156 void EvaluateConditionsForTab(content::WebContents* tab); | 146 void EvaluateConditionsForTab(content::WebContents* tab); |
157 | 147 |
158 // Evaluates the conditions for tabs in each browser window. | 148 // Evaluates the conditions for tabs in each browser window. |
159 void EvaluateConditionsForAllTabs(); | 149 void EvaluateConditionsForAllTabs(); |
160 | 150 |
161 typedef std::map<url_matcher::URLMatcherConditionSet::ID, const ContentRule*> | 151 typedef std::map<url_matcher::URLMatcherConditionSet::ID, const ContentRule*> |
162 URLMatcherIdToRule; | 152 URLMatcherIdToRule; |
163 typedef std::map<ContentRule::GlobalRuleId, linked_ptr<const ContentRule>> | 153 typedef std::map<ContentRule::GlobalRuleId, linked_ptr<const ContentRule>> |
164 RulesMap; | 154 RulesMap; |
| 155 typedef std::map<content::WebContents*, std::vector<std::string>> |
| 156 CssSelectors; |
165 | 157 |
166 // Map that tells us which ContentRules may match under the condition that | 158 // Map that tells us which ContentRules may match under the condition that |
167 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|. | 159 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|. |
168 URLMatcherIdToRule match_id_to_rule_; | 160 URLMatcherIdToRule match_id_to_rule_; |
169 | 161 |
170 RulesMap content_rules_; | 162 RulesMap content_rules_; |
171 | 163 |
172 // Maps a WebContents to the set of rules that match on that WebContents. | 164 // Maps a WebContents to the set of rules that match on that WebContents. |
173 // This lets us call Revert as appropriate. Note that this is expected to have | 165 // This lets us call Revert as appropriate. |
174 // a key-value pair for every WebContents the registry is tracking, even if | |
175 // the value is the empty set. | |
176 std::map<content::WebContents*, std::set<const ContentRule*>> active_rules_; | 166 std::map<content::WebContents*, std::set<const ContentRule*>> active_rules_; |
177 | 167 |
178 // Matches URLs for the page_url condition. | 168 // Matches URLs for the page_url condition. |
179 url_matcher::URLMatcher url_matcher_; | 169 url_matcher::URLMatcher url_matcher_; |
180 | 170 |
181 // Responsible for evaluating the declarative content conditions. | 171 // All CSS selectors any rule's conditions watch for. |
182 DeclarativeContentCssConditionTracker css_condition_tracker_; | 172 std::vector<std::string> watched_css_selectors_; |
183 | 173 |
184 // Manages our notification registrations. | 174 // Manages our notification registrations. |
185 content::NotificationRegistrar registrar_; | 175 content::NotificationRegistrar registrar_; |
186 | 176 |
187 scoped_refptr<InfoMap> extension_info_map_; | 177 scoped_refptr<InfoMap> extension_info_map_; |
188 | 178 |
| 179 // Maps tab_id to the matching CSS selectors for the tab. |
| 180 CssSelectors matching_css_selectors_; |
| 181 |
189 DISALLOW_COPY_AND_ASSIGN(ChromeContentRulesRegistry); | 182 DISALLOW_COPY_AND_ASSIGN(ChromeContentRulesRegistry); |
190 }; | 183 }; |
191 | 184 |
192 } // namespace extensions | 185 } // namespace extensions |
193 | 186 |
194 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULE
S_REGISTRY_H_ | 187 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULE
S_REGISTRY_H_ |
OLD | NEW |