| 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 // 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 void ForEachWebContents(const WebContentsCallback& callback) override; |
| 123 |
| 111 // Returns true if this object retains no allocated data. Only for debugging. | 124 // Returns true if this object retains no allocated data. Only for debugging. |
| 112 bool IsEmpty() const; | 125 bool IsEmpty() const; |
| 113 | 126 |
| 114 protected: | 127 protected: |
| 115 ~ChromeContentRulesRegistry() override; | 128 ~ChromeContentRulesRegistry() override; |
| 116 | 129 |
| 117 // Virtual for testing: | 130 // Virtual for testing: |
| 118 virtual base::Time GetExtensionInstallationTime( | 131 virtual base::Time GetExtensionInstallationTime( |
| 119 const std::string& extension_id) const; | 132 const std::string& extension_id) const; |
| 120 | 133 |
| 121 private: | 134 private: |
| 122 friend class DeclarativeChromeContentRulesRegistryTest; | 135 friend class DeclarativeChromeContentRulesRegistryTest; |
| 123 | 136 |
| 124 // True if this object is managing the rules for |context|. | 137 // True if this object is managing the rules for |context|. |
| 125 bool ManagingRulesForBrowserContext(content::BrowserContext* context); | 138 bool ManagingRulesForBrowserContext(content::BrowserContext* context); |
| 126 | 139 |
| 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( | 140 std::set<const ContentRule*> GetMatches( |
| 131 const RendererContentMatchData& renderer_data, | 141 const RendererContentMatchData& renderer_data, |
| 132 bool is_incognito_renderer) const; | 142 bool is_incognito_renderer) const; |
| 133 | 143 |
| 134 // Scans the rules for the set of conditions they're watching. If the set has | 144 // Updates the condition evaluator with the current watched CSS selectors. |
| 135 // changed, calls InstructRenderProcess() for each RenderProcessHost in the | 145 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 | 146 |
| 144 // Evaluates the conditions for |tab| based on the tab state and matching CSS | 147 // Evaluates the conditions for |tab| based on the tab state and matching CSS |
| 145 // selectors. | 148 // selectors. |
| 146 void EvaluateConditionsForTab(content::WebContents* tab); | 149 void EvaluateConditionsForTab(content::WebContents* tab); |
| 147 | 150 |
| 148 // Evaluates the conditions for tabs in each browser window. | 151 // Evaluates the conditions for tabs in each browser window. |
| 149 void EvaluateConditionsForAllTabs(); | 152 void EvaluateConditionsForAllTabs(); |
| 150 | 153 |
| 151 typedef std::map<url_matcher::URLMatcherConditionSet::ID, const ContentRule*> | 154 typedef std::map<url_matcher::URLMatcherConditionSet::ID, const ContentRule*> |
| 152 URLMatcherIdToRule; | 155 URLMatcherIdToRule; |
| 153 typedef std::map<ContentRule::GlobalRuleId, linked_ptr<const ContentRule>> | 156 typedef std::map<ContentRule::GlobalRuleId, linked_ptr<const ContentRule>> |
| 154 RulesMap; | 157 RulesMap; |
| 155 typedef std::map<content::WebContents*, std::vector<std::string>> | |
| 156 CssSelectors; | |
| 157 | 158 |
| 158 // Map that tells us which ContentRules may match under the condition that | 159 // Map that tells us which ContentRules may match under the condition that |
| 159 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|. | 160 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|. |
| 160 URLMatcherIdToRule match_id_to_rule_; | 161 URLMatcherIdToRule match_id_to_rule_; |
| 161 | 162 |
| 162 RulesMap content_rules_; | 163 RulesMap content_rules_; |
| 163 | 164 |
| 164 // Maps a WebContents to the set of rules that match on that WebContents. | 165 // Maps a WebContents to the set of rules that match on that WebContents. |
| 165 // This lets us call Revert as appropriate. | 166 // This lets us call Revert as appropriate. |
| 166 std::map<content::WebContents*, std::set<const ContentRule*>> active_rules_; | 167 std::map<content::WebContents*, std::set<const ContentRule*>> active_rules_; |
| 167 | 168 |
| 168 // Matches URLs for the page_url condition. | 169 // Matches URLs for the page_url condition. |
| 169 url_matcher::URLMatcher url_matcher_; | 170 url_matcher::URLMatcher url_matcher_; |
| 170 | 171 |
| 171 // All CSS selectors any rule's conditions watch for. | 172 // Responsible for evaluating the declarative content conditions. |
| 172 std::vector<std::string> watched_css_selectors_; | 173 DeclarativeContentCssConditionTracker css_condition_tracker_; |
| 173 | 174 |
| 174 // Manages our notification registrations. | 175 // Manages our notification registrations. |
| 175 content::NotificationRegistrar registrar_; | 176 content::NotificationRegistrar registrar_; |
| 176 | 177 |
| 177 scoped_refptr<InfoMap> extension_info_map_; | 178 scoped_refptr<InfoMap> extension_info_map_; |
| 178 | 179 |
| 179 // Maps tab_id to the matching CSS selectors for the tab. | |
| 180 CssSelectors matching_css_selectors_; | |
| 181 | |
| 182 DISALLOW_COPY_AND_ASSIGN(ChromeContentRulesRegistry); | 180 DISALLOW_COPY_AND_ASSIGN(ChromeContentRulesRegistry); |
| 183 }; | 181 }; |
| 184 | 182 |
| 185 } // namespace extensions | 183 } // namespace extensions |
| 186 | 184 |
| 187 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULE
S_REGISTRY_H_ | 185 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CHROME_CONTENT_RULE
S_REGISTRY_H_ |
| OLD | NEW |