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