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