Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: chrome/browser/extensions/api/declarative_content/chrome_content_rules_registry.h

Issue 1159733004: Encapsulate CSS selector declarative content condition tracking (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@stars-declarative-content-range-for
Patch Set: only notify navigation for tracked WebContents, address comments Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/extensions/api/declarative_content/chrome_content_rules_registry.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
83 // Applies all content rules given that a tab was just navigated. 81 // Applies all content rules given that a tab was just navigated.
84 void DidNavigateMainFrame( 82 void DidNavigateMainFrame(
85 content::WebContents* tab, 83 content::WebContents* tab,
86 const content::LoadCommittedDetails& details, 84 const content::LoadCommittedDetails& details,
87 const content::FrameNavigateParams& params) override; 85 const content::FrameNavigateParams& params) override;
88 86
89 // Applies all content rules given that a tab was just navigated on the 87 // Applies all content rules given that a tab was just navigated on the
90 // original context. Only invoked on the OffTheRecord registry instance. |tab| 88 // original context. Only invoked on the OffTheRecord registry instance. |tab|
91 // is owned by the original context, not this one. 89 // is owned by the original context, not this one.
92 void DidNavigateMainFrameOfOriginalContext( 90 void DidNavigateMainFrameOfOriginalContext(
93 content::WebContents* tab, 91 content::WebContents* tab,
94 const content::LoadCommittedDetails& details, 92 const content::LoadCommittedDetails& details,
95 const content::FrameNavigateParams& params) override; 93 const content::FrameNavigateParams& params) override;
96 94
97 // Implementation of RulesRegistry: 95 // RulesRegistry:
98 std::string AddRulesImpl( 96 std::string AddRulesImpl(
99 const std::string& extension_id, 97 const std::string& extension_id,
100 const std::vector<linked_ptr<RulesRegistry::Rule>>& rules) override; 98 const std::vector<linked_ptr<RulesRegistry::Rule>>& rules) override;
101 std::string RemoveRulesImpl( 99 std::string RemoveRulesImpl(
102 const std::string& extension_id, 100 const std::string& extension_id,
103 const std::vector<std::string>& rule_identifiers) override; 101 const std::vector<std::string>& rule_identifiers) override;
104 std::string RemoveAllRulesImpl(const std::string& extension_id) override; 102 std::string RemoveAllRulesImpl(const std::string& extension_id) override;
105 103
106 // content::NotificationObserver implementation. 104 // content::NotificationObserver:
107 void Observe(int type, 105 void Observe(int type,
108 const content::NotificationSource& source, 106 const content::NotificationSource& source,
109 const content::NotificationDetails& details) override; 107 const content::NotificationDetails& details) override;
110 108
109 // DeclarativeContentCssConditionTrackerDelegate:
110 void RequestEvaluation(content::WebContents* contents) override;
111 bool ShouldManageConditionsForBrowserContext(
112 content::BrowserContext* context) override;
113
111 // Returns true if this object retains no allocated data. Only for debugging. 114 // Returns true if this object retains no allocated data. Only for debugging.
112 bool IsEmpty() const; 115 bool IsEmpty() const;
113 116
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
114 protected: 129 protected:
115 ~ChromeContentRulesRegistry() override; 130 ~ChromeContentRulesRegistry() override;
116 131
117 // Virtual for testing: 132 // Virtual for testing:
118 virtual base::Time GetExtensionInstallationTime( 133 virtual base::Time GetExtensionInstallationTime(
119 const std::string& extension_id) const; 134 const std::string& extension_id) const;
120 135
121 private: 136 private:
122 friend class DeclarativeChromeContentRulesRegistryTest; 137 // Utility function for iterating a lambda over the relevant WebContents.
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);
123 143
124 // True if this object is managing the rules for |context|. 144 // True if this object is managing the rules for |context|.
125 bool ManagingRulesForBrowserContext(content::BrowserContext* context); 145 bool ManagingRulesForBrowserContext(content::BrowserContext* context);
126 146
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( 147 std::set<const ContentRule*> GetMatches(
131 const RendererContentMatchData& renderer_data, 148 const RendererContentMatchData& renderer_data,
132 bool is_incognito_renderer) const; 149 bool is_incognito_renderer) const;
133 150
134 // Scans the rules for the set of conditions they're watching. If the set has 151 // Updates the condition evaluator with the current watched CSS selectors.
135 // changed, calls InstructRenderProcess() for each RenderProcessHost in the 152 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 153
144 // Evaluates the conditions for |tab| based on the tab state and matching CSS 154 // Evaluates the conditions for |tab| based on the tab state and matching CSS
145 // selectors. 155 // selectors.
146 void EvaluateConditionsForTab(content::WebContents* tab); 156 void EvaluateConditionsForTab(content::WebContents* tab);
147 157
148 // Evaluates the conditions for tabs in each browser window. 158 // Evaluates the conditions for tabs in each browser window.
149 void EvaluateConditionsForAllTabs(); 159 void EvaluateConditionsForAllTabs();
150 160
151 typedef std::map<url_matcher::URLMatcherConditionSet::ID, const ContentRule*> 161 typedef std::map<url_matcher::URLMatcherConditionSet::ID, const ContentRule*>
152 URLMatcherIdToRule; 162 URLMatcherIdToRule;
153 typedef std::map<ContentRule::GlobalRuleId, linked_ptr<const ContentRule>> 163 typedef std::map<ContentRule::GlobalRuleId, linked_ptr<const ContentRule>>
154 RulesMap; 164 RulesMap;
155 typedef std::map<content::WebContents*, std::vector<std::string>>
156 CssSelectors;
157 165
158 // Map that tells us which ContentRules may match under the condition that 166 // Map that tells us which ContentRules may match under the condition that
159 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|. 167 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|.
160 URLMatcherIdToRule match_id_to_rule_; 168 URLMatcherIdToRule match_id_to_rule_;
161 169
162 RulesMap content_rules_; 170 RulesMap content_rules_;
163 171
164 // Maps a WebContents to the set of rules that match on that WebContents. 172 // Maps a WebContents to the set of rules that match on that WebContents.
165 // This lets us call Revert as appropriate. 173 // This lets us call Revert as appropriate. Note that this is expected to have
174 // a key-value pair for every WebContents the registry is tracking, even if
175 // the value is the empty set.
166 std::map<content::WebContents*, std::set<const ContentRule*>> active_rules_; 176 std::map<content::WebContents*, std::set<const ContentRule*>> active_rules_;
167 177
168 // Matches URLs for the page_url condition. 178 // Matches URLs for the page_url condition.
169 url_matcher::URLMatcher url_matcher_; 179 url_matcher::URLMatcher url_matcher_;
170 180
171 // All CSS selectors any rule's conditions watch for. 181 // Responsible for evaluating the declarative content conditions.
172 std::vector<std::string> watched_css_selectors_; 182 DeclarativeContentCssConditionTracker css_condition_tracker_;
173 183
174 // Manages our notification registrations. 184 // Manages our notification registrations.
175 content::NotificationRegistrar registrar_; 185 content::NotificationRegistrar registrar_;
176 186
177 scoped_refptr<InfoMap> extension_info_map_; 187 scoped_refptr<InfoMap> extension_info_map_;
178 188
179 // Maps tab_id to the matching CSS selectors for the tab.
180 CssSelectors matching_css_selectors_;
181
182 DISALLOW_COPY_AND_ASSIGN(ChromeContentRulesRegistry); 189 DISALLOW_COPY_AND_ASSIGN(ChromeContentRulesRegistry);
183 }; 190 };
184 191
185 } // namespace extensions 192 } // namespace extensions
186 193
187 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULE S_REGISTRY_H_ 194 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULE S_REGISTRY_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/api/declarative_content/chrome_content_rules_registry.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698