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