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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/extensions/api/declarative_content/content_rules_registry.h" 6 7 #include "chrome/browser/chrome_notification_types.h" 8 #include "chrome/browser/extensions/api/declarative_content/content_action.h" 9 #include "chrome/browser/extensions/api/declarative_content/content_condition.h" 10 #include "chrome/browser/extensions/api/declarative_content/content_constants.h" 11 #include "chrome/browser/extensions/extension_system.h" 12 #include "chrome/browser/extensions/extension_tab_util.h" 13 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/common/extensions/extension_messages.h" 15 #include "content/public/browser/navigation_details.h" 16 #include "content/public/browser/notification_service.h" 17 #include "content/public/browser/notification_source.h" 18 #include "content/public/browser/render_process_host.h" 19 #include "content/public/browser/web_contents.h" 20 21 namespace extensions { 22 23 ContentRulesRegistry::ContentRulesRegistry( 24 Profile* profile, 25 scoped_ptr<RulesRegistryWithCache::RuleStorageOnUI>* ui_part) 26 : RulesRegistryWithCache((ui_part ? profile : NULL), 27 declarative_content_constants::kOnPageChanged, 28 content::BrowserThread::UI, 29 false /*log_storage_init_delay*/, 30 ui_part), 31 profile_(profile) { 32 extension_info_map_ = ExtensionSystem::Get(profile)->info_map(); 33 34 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, 35 content::NotificationService::AllBrowserContextsAndSources()); 36 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 37 content::NotificationService::AllBrowserContextsAndSources()); 38 } 39 40 void ContentRulesRegistry::Observe( 41 int type, 42 const content::NotificationSource& source, 43 const content::NotificationDetails& details) { 44 switch (type) { 45 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: { 46 content::RenderProcessHost* process = 47 content::Source<content::RenderProcessHost>(source).ptr(); 48 if (process->GetBrowserContext() == profile_) 49 InstructRenderProcess(process); 50 break; 51 } 52 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: { 53 content::WebContents* tab = 54 content::Source<content::WebContents>(source).ptr(); 55 // GetTabId() returns -1 for non-tab WebContents, which won't be 56 // in the map. Similarly, tabs from other profiles won't be in 57 // the map. 58 active_rules_.erase(ExtensionTabUtil::GetTabId(tab)); 59 break; 60 } 61 } 62 } 63 64 void ContentRulesRegistry::Apply( 65 content::WebContents* contents, 66 const std::vector<std::string>& matching_css_selectors) { 67 const int tab_id = ExtensionTabUtil::GetTabId(contents); 68 RendererContentMatchData renderer_data; 69 renderer_data.page_url_matches = url_matcher_.MatchURL(contents->GetURL()); 70 renderer_data.css_selectors.insert(matching_css_selectors.begin(), 71 matching_css_selectors.end()); 72 std::set<ContentRule*> matching_rules = GetMatches(renderer_data); 73 if (matching_rules.empty() && !ContainsKey(active_rules_, tab_id)) 74 return; 75 76 std::set<ContentRule*>& prev_matching_rules = active_rules_[tab_id]; 77 ContentAction::ApplyInfo apply_info = { 78 profile_, contents 79 }; 80 for (std::set<ContentRule*>::const_iterator it = matching_rules.begin(); 81 it != matching_rules.end(); ++it) { 82 if (!ContainsKey(prev_matching_rules, *it)) 83 (*it)->actions().Apply((*it)->extension_id(), base::Time(), &apply_info); 84 } 85 for (std::set<ContentRule*>::const_iterator it = prev_matching_rules.begin(); 86 it != prev_matching_rules.end(); ++it) { 87 if (!ContainsKey(matching_rules, *it)) 88 (*it)->actions().Revert((*it)->extension_id(), base::Time(), &apply_info); 89 } 90 91 if (matching_rules.empty()) 92 active_rules_.erase(tab_id); 93 else 94 swap(matching_rules, prev_matching_rules); 95 } 96 97 void ContentRulesRegistry::DidNavigateMainFrame( 98 content::WebContents* contents, 99 const content::LoadCommittedDetails& details, 100 const content::FrameNavigateParams& params) { 101 if (details.is_in_page) { 102 // Within-page navigations don't change the set of elements that 103 // exist, and we only support filtering on the top-level URL, so 104 // this can't change which rules match. 105 return; 106 } 107 108 // Top-level navigation produces a new document. Initially, the 109 // document's empty, so no CSS rules match. The renderer will send 110 // an ExtensionHostMsg_OnWatchedPageChange later if any CSS rules 111 // match. 112 std::vector<std::string> no_css_selectors; 113 Apply(contents, no_css_selectors); 114 } 115 116 std::set<ContentRule*> 117 ContentRulesRegistry::GetMatches( 118 const RendererContentMatchData& renderer_data) const { 119 std::set<ContentRule*> result; 120 121 // Then we need to check for each of these, whether the other 122 // attributes are also fulfilled. 123 for (std::set<URLMatcherConditionSet::ID>::iterator 124 url_match = renderer_data.page_url_matches.begin(); 125 url_match != renderer_data.page_url_matches.end(); ++url_match) { 126 URLMatcherIdToRule::const_iterator rule_iter = 127 match_id_to_rule_.find(*url_match); 128 CHECK(rule_iter != match_id_to_rule_.end()); 129 130 ContentRule* rule = rule_iter->second; 131 if (rule->conditions().IsFulfilled(*url_match, renderer_data)) 132 result.insert(rule); 133 } 134 return result; 135 } 136 137 std::string ContentRulesRegistry::AddRulesImpl( 138 const std::string& extension_id, 139 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) { 140 base::Time extension_installation_time = 141 GetExtensionInstallationTime(extension_id); 142 143 std::string error; 144 RulesMap new_content_rules; 145 146 for (std::vector<linked_ptr<RulesRegistry::Rule> >::const_iterator rule = 147 rules.begin(); rule != rules.end(); ++rule) { 148 ContentRule::GlobalRuleId rule_id(extension_id, *(*rule)->id); 149 DCHECK(content_rules_.find(rule_id) == content_rules_.end()); 150 151 scoped_ptr<ContentRule> content_rule( 152 ContentRule::Create(url_matcher_.condition_factory(), 153 extension_id, 154 extension_installation_time, 155 *rule, 156 ContentRule::ConsistencyChecker(), 157 &error)); 158 if (!error.empty()) { 159 // Clean up temporary condition sets created during rule creation. 160 url_matcher_.ClearUnusedConditionSets(); 161 return error; 162 } 163 DCHECK(content_rule); 164 165 new_content_rules[rule_id] = make_linked_ptr(content_rule.release()); 166 } 167 168 // Wohoo, everything worked fine. 169 content_rules_.insert(new_content_rules.begin(), new_content_rules.end()); 170 171 // Create the triggers. 172 for (RulesMap::iterator i = new_content_rules.begin(); 173 i != new_content_rules.end(); ++i) { 174 URLMatcherConditionSet::Vector url_condition_sets; 175 const ContentConditionSet& conditions = i->second->conditions(); 176 conditions.GetURLMatcherConditionSets(&url_condition_sets); 177 for (URLMatcherConditionSet::Vector::iterator j = 178 url_condition_sets.begin(); j != url_condition_sets.end(); ++j) { 179 match_id_to_rule_[(*j)->id()] = i->second.get(); 180 } 181 } 182 183 // Register url patterns in url_matcher_. 184 URLMatcherConditionSet::Vector all_new_condition_sets; 185 for (RulesMap::iterator i = new_content_rules.begin(); 186 i != new_content_rules.end(); ++i) { 187 i->second->conditions().GetURLMatcherConditionSets(&all_new_condition_sets); 188 } 189 url_matcher_.AddConditionSets(all_new_condition_sets); 190 191 UpdateConditionCache(); 192 193 return std::string(); 194 } 195 196 std::string ContentRulesRegistry::RemoveRulesImpl( 197 const std::string& extension_id, 198 const std::vector<std::string>& rule_identifiers) { 199 // URLMatcherConditionSet IDs that can be removed from URLMatcher. 200 std::vector<URLMatcherConditionSet::ID> remove_from_url_matcher; 201 202 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); 203 i != rule_identifiers.end(); ++i) { 204 ContentRule::GlobalRuleId rule_id(extension_id, *i); 205 206 // Skip unknown rules. 207 RulesMap::iterator content_rules_entry = content_rules_.find(rule_id); 208 if (content_rules_entry == content_rules_.end()) 209 continue; 210 211 // Remove all triggers but collect their IDs. 212 URLMatcherConditionSet::Vector condition_sets; 213 ContentRule* rule = content_rules_entry->second.get(); 214 rule->conditions().GetURLMatcherConditionSets(&condition_sets); 215 for (URLMatcherConditionSet::Vector::iterator j = condition_sets.begin(); 216 j != condition_sets.end(); ++j) { 217 remove_from_url_matcher.push_back((*j)->id()); 218 match_id_to_rule_.erase((*j)->id()); 219 } 220 221 // Remove the ContentRule from active_rules_. 222 for (std::map<int, std::set<ContentRule*> >::iterator 223 it = active_rules_.begin(); 224 it != active_rules_.end(); ++it) { 225 // Has no effect if the rule wasn't present. 226 it->second.erase(rule); 227 } 228 229 // Remove reference to actual rule. 230 content_rules_.erase(content_rules_entry); 231 } 232 233 // Clear URLMatcher based on condition_set_ids that are not needed any more. 234 url_matcher_.RemoveConditionSets(remove_from_url_matcher); 235 236 UpdateConditionCache(); 237 238 return std::string(); 239 } 240 241 std::string ContentRulesRegistry::RemoveAllRulesImpl( 242 const std::string& extension_id) { 243 // Search all identifiers of rules that belong to extension |extension_id|. 244 std::vector<std::string> rule_identifiers; 245 for (RulesMap::iterator i = content_rules_.begin(); 246 i != content_rules_.end(); ++i) { 247 const ContentRule::GlobalRuleId& global_rule_id = i->first; 248 if (global_rule_id.first == extension_id) 249 rule_identifiers.push_back(global_rule_id.second); 250 } 251 252 return RemoveRulesImpl(extension_id, rule_identifiers); 253 } 254 255 void ContentRulesRegistry::UpdateConditionCache() { 256 std::set<std::string> css_selectors; // We rely on this being sorted. 257 for (RulesMap::const_iterator i = content_rules_.begin(); 258 i != content_rules_.end(); ++i) { 259 ContentRule& rule = *i->second; 260 for (ContentConditionSet::const_iterator 261 condition = rule.conditions().begin(); 262 condition != rule.conditions().end(); ++condition) { 263 const std::vector<std::string>& condition_css_selectors = 264 (*condition)->css_selectors(); 265 css_selectors.insert(condition_css_selectors.begin(), 266 condition_css_selectors.end()); 267 } 268 } 269 270 if (css_selectors.size() != watched_css_selectors_.size() || 271 !std::equal(css_selectors.begin(), css_selectors.end(), 272 watched_css_selectors_.begin())) { 273 watched_css_selectors_.assign(css_selectors.begin(), css_selectors.end()); 274 275 for (content::RenderProcessHost::iterator it( 276 content::RenderProcessHost::AllHostsIterator()); 277 !it.IsAtEnd(); it.Advance()) { 278 content::RenderProcessHost* process = it.GetCurrentValue(); 279 if (process->GetBrowserContext() == profile_) 280 InstructRenderProcess(process); 281 } 282 } 283 } 284 285 void ContentRulesRegistry::InstructRenderProcess( 286 content::RenderProcessHost* process) { 287 process->Send(new ExtensionMsg_WatchPages(watched_css_selectors_)); 288 } 289 290 bool ContentRulesRegistry::IsEmpty() const { 291 return match_id_to_rule_.empty() && content_rules_.empty() && 292 url_matcher_.IsEmpty(); 293 } 294 295 ContentRulesRegistry::~ContentRulesRegistry() {} 296 297 base::Time ContentRulesRegistry::GetExtensionInstallationTime( 298 const std::string& extension_id) const { 299 if (!extension_info_map_.get()) // May be NULL during testing. 300 return base::Time(); 301 302 return extension_info_map_->GetInstallTime(extension_id); 303 } 304 305 } // namespace extensions 306