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 "chrome/browser/extensions/api/declarative_content/declarative_content_ rule.h" | |
19 #include "components/url_matcher/url_matcher.h" | 21 #include "components/url_matcher/url_matcher.h" |
20 #include "content/public/browser/notification_observer.h" | 22 #include "content/public/browser/notification_observer.h" |
21 #include "content/public/browser/notification_registrar.h" | 23 #include "content/public/browser/notification_registrar.h" |
22 #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 { |
29 class BrowserContext; | 30 class BrowserContext; |
30 class RenderProcessHost; | 31 class RenderProcessHost; |
31 class WebContents; | 32 class WebContents; |
32 struct FrameNavigateParams; | 33 struct FrameNavigateParams; |
33 struct LoadCommittedDetails; | 34 struct LoadCommittedDetails; |
34 } | 35 } |
35 | 36 |
36 namespace extension_web_request_api_helpers { | 37 namespace extension_web_request_api_helpers { |
37 struct EventResponseDelta; | 38 struct EventResponseDelta; |
38 } | 39 } |
39 | 40 |
40 namespace net { | 41 namespace net { |
41 class URLRequest; | 42 class URLRequest; |
42 } | 43 } |
43 | 44 |
44 namespace extensions { | 45 namespace extensions { |
45 | 46 |
46 class RulesRegistryService; | 47 class RulesRegistryService; |
47 | 48 |
48 typedef DeclarativeRule<ContentCondition, ContentAction> ContentRule; | 49 typedef DeclarativeContentRule ContentRule; |
not at google - send to devlin
2015/06/01 22:51:11
Yeah, again we should try to delete this typedef i
| |
49 | 50 |
50 // The ChromeContentRulesRegistry is responsible for managing | 51 // The ChromeContentRulesRegistry is responsible for managing |
51 // the internal representation of rules for the Declarative Content API. | 52 // the internal representation of rules for the Declarative Content API. |
52 // | 53 // |
53 // Here is the high level overview of this functionality: | 54 // Here is the high level overview of this functionality: |
54 // | 55 // |
55 // RulesRegistry::Rule consists of Conditions and Actions, these are | 56 // RulesRegistry::Rule consists of Conditions and Actions, these are |
56 // represented as a ContentRule with ContentConditions and | 57 // represented as a ContentRule with ContentConditions and |
57 // ContentRuleActions. | 58 // ContentRuleActions. |
58 // | 59 // |
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 // TODO(wittman): Remove once DeclarativeChromeContentRulesRegistry no longer |
78 // Applies all content rules given an update (CSS match change or | 81 // depends on concrete condition implementations. At that point |
79 // page navigation, for now) from the renderer. | 82 // DeclarativeChromeContentRulesRegistryTest.ActiveRulesDoesntGrow will be |
80 void Apply(content::WebContents* contents, | 83 // able to use a test condition object and not need to depend on force setting |
81 const std::vector<std::string>& matching_css_selectors) override; | 84 // matching CSS seleectors. |
85 void UpdateMatchingCssSelectorsForTesting( | |
86 content::WebContents* contents, | |
87 const std::vector<std::string>& matching_css_selectors); | |
82 | 88 |
89 // ContentRulesRegistry: | |
83 // Applies all content rules given that a tab was just navigated. | 90 // Applies all content rules given that a tab was just navigated. |
84 void DidNavigateMainFrame( | 91 void DidNavigateMainFrame( |
85 content::WebContents* tab, | 92 content::WebContents* tab, |
86 const content::LoadCommittedDetails& details, | 93 const content::LoadCommittedDetails& details, |
87 const content::FrameNavigateParams& params) override; | 94 const content::FrameNavigateParams& params) override; |
88 | 95 |
89 // Applies all content rules given that a tab was just navigated on the | 96 // Applies all content rules given that a tab was just navigated on the |
90 // original context. Only invoked on the OffTheRecord registry instance. |tab| | 97 // original context. Only invoked on the OffTheRecord registry instance. |tab| |
91 // is owned by the original context, not this one. | 98 // is owned by the original context, not this one. |
92 void DidNavigateMainFrameOfOriginalContext( | 99 void DidNavigateMainFrameOfOriginalContext( |
93 content::WebContents* tab, | 100 content::WebContents* tab, |
94 const content::LoadCommittedDetails& details, | 101 const content::LoadCommittedDetails& details, |
95 const content::FrameNavigateParams& params) override; | 102 const content::FrameNavigateParams& params) override; |
96 | 103 |
97 // Implementation of RulesRegistry: | 104 // RulesRegistry: |
98 std::string AddRulesImpl( | 105 std::string AddRulesImpl( |
99 const std::string& extension_id, | 106 const std::string& extension_id, |
100 const std::vector<linked_ptr<RulesRegistry::Rule>>& rules) override; | 107 const std::vector<linked_ptr<RulesRegistry::Rule>>& rules) override; |
101 std::string RemoveRulesImpl( | 108 std::string RemoveRulesImpl( |
102 const std::string& extension_id, | 109 const std::string& extension_id, |
103 const std::vector<std::string>& rule_identifiers) override; | 110 const std::vector<std::string>& rule_identifiers) override; |
104 std::string RemoveAllRulesImpl(const std::string& extension_id) override; | 111 std::string RemoveAllRulesImpl(const std::string& extension_id) override; |
105 | 112 |
106 // content::NotificationObserver implementation. | 113 // content::NotificationObserver: |
107 void Observe(int type, | 114 void Observe(int type, |
108 const content::NotificationSource& source, | 115 const content::NotificationSource& source, |
109 const content::NotificationDetails& details) override; | 116 const content::NotificationDetails& details) override; |
110 | 117 |
118 // DeclarativeContentCssConditionTrackerDelegate: | |
119 void RequestEvaluation(content::WebContents* contents) override; | |
120 bool ShouldManageConditionsForBrowserContext( | |
121 content::BrowserContext* context) override; | |
122 | |
111 // Returns true if this object retains no allocated data. Only for debugging. | 123 // Returns true if this object retains no allocated data. Only for debugging. |
112 bool IsEmpty() const; | 124 bool IsEmpty() const; |
113 | 125 |
114 protected: | 126 protected: |
115 ~ChromeContentRulesRegistry() override; | 127 ~ChromeContentRulesRegistry() override; |
116 | 128 |
117 // Virtual for testing: | 129 // Virtual for testing: |
118 virtual base::Time GetExtensionInstallationTime( | 130 virtual base::Time GetExtensionInstallationTime( |
119 const std::string& extension_id) const; | 131 const std::string& extension_id) const; |
120 | 132 |
121 private: | 133 private: |
122 friend class DeclarativeChromeContentRulesRegistryTest; | 134 friend class DeclarativeChromeContentRulesRegistryTest; |
123 | 135 |
124 // True if this object is managing the rules for |context|. | 136 // True if this object is managing the rules for |context|. |
125 bool ManagingRulesForBrowserContext(content::BrowserContext* context); | 137 bool ManagingRulesForBrowserContext(content::BrowserContext* context); |
126 | 138 |
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( | 139 std::set<const ContentRule*> GetMatches( |
131 const RendererContentMatchData& renderer_data, | 140 const RendererContentMatchData& renderer_data, |
132 bool is_incognito_renderer) const; | 141 bool is_incognito_renderer) const; |
133 | 142 |
134 // Scans the rules for the set of conditions they're watching. If the set has | 143 // Updates the condition evaluator with the current watched CSS selectors. |
135 // changed, calls InstructRenderProcess() for each RenderProcessHost in the | 144 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 | 145 |
144 // Evaluates the conditions for |tab| based on the tab state and matching CSS | 146 // Evaluates the conditions for |tab| based on the tab state and matching CSS |
145 // selectors. | 147 // selectors. |
146 void EvaluateConditionsForTab(content::WebContents* tab); | 148 void EvaluateConditionsForTab(content::WebContents* tab); |
147 | 149 |
148 // Evaluates the conditions for tabs in each browser window. | 150 // Evaluates the conditions for tabs in each browser window. |
149 void EvaluateConditionsForAllTabs(); | 151 void EvaluateConditionsForAllTabs(); |
150 | 152 |
151 typedef std::map<url_matcher::URLMatcherConditionSet::ID, const ContentRule*> | 153 typedef std::map<url_matcher::URLMatcherConditionSet::ID, const ContentRule*> |
152 URLMatcherIdToRule; | 154 URLMatcherIdToRule; |
153 typedef std::map<ContentRule::GlobalRuleId, linked_ptr<const ContentRule>> | 155 typedef std::map<ContentRule::GlobalRuleId, linked_ptr<const ContentRule>> |
154 RulesMap; | 156 RulesMap; |
155 typedef std::map<content::WebContents*, std::vector<std::string>> | |
156 CssSelectors; | |
157 | 157 |
158 // 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 |
159 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|. | 159 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|. |
160 URLMatcherIdToRule match_id_to_rule_; | 160 URLMatcherIdToRule match_id_to_rule_; |
161 | 161 |
162 RulesMap content_rules_; | 162 RulesMap content_rules_; |
163 | 163 |
164 // 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. |
165 // This lets us call Revert as appropriate. | 165 // This lets us call Revert as appropriate. |
166 std::map<content::WebContents*, std::set<const ContentRule*>> active_rules_; | 166 std::map<content::WebContents*, std::set<const ContentRule*>> active_rules_; |
167 | 167 |
168 // Matches URLs for the page_url condition. | 168 // Matches URLs for the page_url condition. |
169 url_matcher::URLMatcher url_matcher_; | 169 url_matcher::URLMatcher url_matcher_; |
170 | 170 |
171 // All CSS selectors any rule's conditions watch for. | 171 // Responsible for evaluating the declarative content conditions. |
172 std::vector<std::string> watched_css_selectors_; | 172 DeclarativeContentCssConditionTracker css_condition_tracker_; |
173 | 173 |
174 // Manages our notification registrations. | 174 // Manages our notification registrations. |
175 content::NotificationRegistrar registrar_; | 175 content::NotificationRegistrar registrar_; |
176 | 176 |
177 scoped_refptr<InfoMap> extension_info_map_; | 177 scoped_refptr<InfoMap> extension_info_map_; |
178 | 178 |
179 // Maps tab_id to the matching CSS selectors for the tab. | |
180 CssSelectors matching_css_selectors_; | |
181 | |
182 DISALLOW_COPY_AND_ASSIGN(ChromeContentRulesRegistry); | 179 DISALLOW_COPY_AND_ASSIGN(ChromeContentRulesRegistry); |
183 }; | 180 }; |
184 | 181 |
185 } // namespace extensions | 182 } // namespace extensions |
186 | 183 |
187 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULE S_REGISTRY_H_ | 184 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULE S_REGISTRY_H_ |
OLD | NEW |