Chromium Code Reviews| Index: chrome/browser/extensions/api/declarative_content/content_rules_registry.cc |
| diff --git a/chrome/browser/extensions/api/declarative_content/content_rules_registry.cc b/chrome/browser/extensions/api/declarative_content/content_rules_registry.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c5736fe1fc3776c4760b7bfd715d265de2be7478 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/declarative_content/content_rules_registry.cc |
| @@ -0,0 +1,285 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/api/declarative_content/content_rules_registry.h" |
| + |
| +#include "chrome/browser/extensions/api/declarative_content/content_action.h" |
| +#include "chrome/browser/extensions/api/declarative_content/content_condition.h" |
| +#include "chrome/browser/extensions/extension_system.h" |
| +#include "chrome/browser/extensions/extension_tab_util.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/extensions/extension_messages.h" |
| +#include "content/public/browser/navigation_details.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "content/public/browser/web_contents.h" |
| + |
| +namespace extensions { |
| + |
| +ContentRulesRegistry::ContentRulesRegistry(Profile* profile, Delegate* delegate) |
| + : RulesRegistryWithCache(delegate), |
| + profile_(profile) { |
| + DCHECK_EQ(profile->GetOriginalProfile(), profile) |
| + << "Don't pass incognito profiles."; |
| + extension_info_map_ = ExtensionSystem::Get(profile)->info_map(); |
| + |
| + registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| + content::NotificationService::AllBrowserContextsAndSources()); |
| +} |
| + |
| +void ContentRulesRegistry::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + switch (type) { |
| + case content::NOTIFICATION_RENDERER_PROCESS_CREATED: |
| + content::RenderProcessHost* process = |
| + content::Source<content::RenderProcessHost>(source).ptr(); |
| + Profile* process_profile = |
| + Profile::FromBrowserContext(process->GetBrowserContext()); |
| + if (process_profile->GetOriginalProfile() == profile_) |
| + InstructRenderProcess(process); |
| + break; |
| + } |
| +} |
| + |
| +void ContentRulesRegistry::Apply( |
| + content::WebContents* contents, |
| + const std::vector<std::string>& matching_css_selectors) { |
| + const int tab_id = ExtensionTabUtil::GetTabId(contents); |
| + RendererContentMatchData renderer_data; |
| + renderer_data.top_url = contents->GetURL(); |
| + renderer_data.css_rules.insert(matching_css_selectors.begin(), |
| + matching_css_selectors.end()); |
| + std::set<ContentRule*> matching_rules = GetMatches(renderer_data); |
| + std::set<ContentRule*>& prev_matching_rules = active_rules_[tab_id]; |
| + ContentAction::ApplyInfo apply_info = { |
| + profile_, contents |
| + }; |
| + for (std::set<ContentRule*>::const_iterator it = matching_rules.begin(); |
| + it != matching_rules.end(); ++it) { |
| + if (!ContainsKey(prev_matching_rules, *it)) |
| + (*it)->actions().Apply((*it)->extension_id(), base::Time(), apply_info); |
| + } |
| + for (std::set<ContentRule*>::const_iterator it = prev_matching_rules.begin(); |
| + it != prev_matching_rules.end(); ++it) { |
| + if (!ContainsKey(matching_rules, *it)) |
| + (*it)->actions().Revert((*it)->extension_id(), base::Time(), apply_info); |
| + } |
| + swap(matching_rules, prev_matching_rules); |
| +} |
| + |
| +void ContentRulesRegistry::DidNavigateMainFrame( |
| + content::WebContents* contents, |
| + const content::LoadCommittedDetails& details, |
| + const content::FrameNavigateParams& params) { |
| + if (details.is_in_page) { |
| + // Within-page navigations don't change the set of elements that |
| + // exist, and we only support filtering on the top-level URL, so |
| + // this can't change which rules match. |
| + return; |
| + } |
| + |
| + // Top-level navigation produces a new document. Initially, the |
| + // document's empty, so no CSS rules match. The renderer will send |
| + // an ExtensionHostMsg_OnWatchedPageChange later if any CSS rules |
| + // 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
|
| + std::vector<std::string> no_css_selectors; |
| + Apply(contents, no_css_selectors); |
| +} |
| + |
| +std::set<ContentRule*> |
| +ContentRulesRegistry::GetMatches( |
| + const RendererContentMatchData& renderer_data) { |
| + std::set<ContentRule*> result; |
| + |
| + // Figure out for which rules the URL match conditions were fulfilled. |
| + std::set<URLMatcherConditionSet::ID> url_matches = |
| + url_matcher_.MatchURL(renderer_data.top_url); |
| + |
| + // Then we need to check for each of these, whether the other |
| + // attributes are also fulfilled. |
| + for (std::set<URLMatcherConditionSet::ID>::iterator |
| + url_match = url_matches.begin(); |
| + url_match != url_matches.end(); ++url_match) { |
| + RuleTriggers::iterator rule_trigger = rule_triggers_.find(*url_match); |
| + CHECK(rule_trigger != rule_triggers_.end()); |
| + |
| + ContentRule* rule = rule_trigger->second; |
| + if (rule->conditions().IsFulfilled(*url_match, renderer_data)) |
| + result.insert(rule); |
| + } |
| + return result; |
| +} |
| + |
| +std::string ContentRulesRegistry::AddRulesImpl( |
| + const std::string& extension_id, |
| + const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) { |
| + base::Time extension_installation_time = |
| + GetExtensionInstallationTime(extension_id); |
| + |
| + std::string error; |
| + RulesMap new_content_rules; |
| + |
| + for (std::vector<linked_ptr<RulesRegistry::Rule> >::const_iterator rule = |
| + rules.begin(); rule != rules.end(); ++rule) { |
| + ContentRule::GlobalRuleId rule_id(extension_id, *(*rule)->id); |
| + DCHECK(content_rules_.find(rule_id) == content_rules_.end()); |
| + |
| + scoped_ptr<ContentRule> content_rule( |
| + ContentRule::Create(url_matcher_.condition_factory(), extension_id, |
| + extension_installation_time, *rule, NULL, &error)); |
| + if (!error.empty()) { |
| + // Clean up temporary condition sets created during rule creation. |
| + url_matcher_.ClearUnusedConditionSets(); |
| + return error; |
| + } |
| + |
| + new_content_rules[rule_id] = make_linked_ptr(content_rule.release()); |
| + } |
| + |
| + // Wohoo, everything worked fine. |
| + content_rules_.insert(new_content_rules.begin(), new_content_rules.end()); |
| + |
| + // Create the triggers. |
| + for (RulesMap::iterator i = new_content_rules.begin(); |
| + i != new_content_rules.end(); ++i) { |
| + URLMatcherConditionSet::Vector url_condition_sets; |
| + const ContentConditionSet& conditions = i->second->conditions(); |
| + conditions.GetURLMatcherConditionSets(&url_condition_sets); |
| + for (URLMatcherConditionSet::Vector::iterator j = |
| + url_condition_sets.begin(); j != url_condition_sets.end(); ++j) { |
| + rule_triggers_[(*j)->id()] = i->second.get(); |
| + } |
| + } |
| + |
| + // Register url patterns in url_matcher_. |
| + URLMatcherConditionSet::Vector all_new_condition_sets; |
| + for (RulesMap::iterator i = new_content_rules.begin(); |
| + i != new_content_rules.end(); ++i) { |
| + i->second->conditions().GetURLMatcherConditionSets(&all_new_condition_sets); |
| + } |
| + url_matcher_.AddConditionSets(all_new_condition_sets); |
| + |
| + UpdateConditionCache(); |
| + |
| + return ""; |
| +} |
| + |
| +std::string ContentRulesRegistry::RemoveRulesImpl( |
| + const std::string& extension_id, |
| + const std::vector<std::string>& rule_identifiers) { |
| + // URLMatcherConditionSet IDs that can be removed from URLMatcher. |
| + std::vector<URLMatcherConditionSet::ID> remove_from_url_matcher; |
| + |
| + for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); |
| + i != rule_identifiers.end(); ++i) { |
| + ContentRule::GlobalRuleId rule_id(extension_id, *i); |
| + |
| + // Skip unknown rules. |
| + RulesMap::iterator content_rules_entry = content_rules_.find(rule_id); |
| + if (content_rules_entry == content_rules_.end()) |
| + continue; |
| + |
| + // Remove all triggers but collect their IDs. |
| + URLMatcherConditionSet::Vector condition_sets; |
| + ContentRule* rule = content_rules_entry->second.get(); |
| + rule->conditions().GetURLMatcherConditionSets(&condition_sets); |
| + for (URLMatcherConditionSet::Vector::iterator j = condition_sets.begin(); |
| + j != condition_sets.end(); ++j) { |
| + remove_from_url_matcher.push_back((*j)->id()); |
| + rule_triggers_.erase((*j)->id()); |
| + } |
| + |
| + // Remove the ContentRule from active_rules_. |
| + for (std::map<int, std::set<ContentRule*> >::iterator |
| + it = active_rules_.begin(); |
| + it != active_rules_.end(); ++it) { |
| + // Has no effect if the rule wasn't present. |
| + it->second.erase(rule); |
| + } |
| + |
| + // Remove reference to actual rule. |
| + content_rules_.erase(content_rules_entry); |
| + } |
| + |
| + // Clear URLMatcher based on condition_set_ids that are not needed any more. |
| + url_matcher_.RemoveConditionSets(remove_from_url_matcher); |
| + |
| + UpdateConditionCache(); |
| + |
| + return ""; |
| +} |
| + |
| +std::string ContentRulesRegistry::RemoveAllRulesImpl( |
| + const std::string& extension_id) { |
| + // Search all identifiers of rules that belong to extension |extension_id|. |
| + std::vector<std::string> rule_identifiers; |
| + for (RulesMap::iterator i = content_rules_.begin(); |
| + i != content_rules_.end(); ++i) { |
| + const ContentRule::GlobalRuleId& global_rule_id = i->first; |
| + if (global_rule_id.first == extension_id) |
| + rule_identifiers.push_back(global_rule_id.second); |
| + } |
| + |
| + return RemoveRulesImpl(extension_id, rule_identifiers); |
| +} |
| + |
| +void ContentRulesRegistry::UpdateConditionCache() { |
| + std::set<std::string> css_selectors; // We rely on this being sorted. |
| + for (RulesMap::const_iterator i = content_rules_.begin(); |
| + i != content_rules_.end(); ++i) { |
| + ContentRule& rule = *i->second; |
| + for (ContentConditionSet::const_iterator |
| + condition = rule.conditions().begin(); |
| + condition != rule.conditions().end(); ++condition) { |
| + const std::vector<std::string>& css_rules = (*condition)->css_rules(); |
| + css_selectors.insert(css_rules.begin(), css_rules.end()); |
| + } |
| + } |
| + |
| + if (css_selectors.size() != watched_css_selectors_.size() || |
| + !std::equal(css_selectors.begin(), css_selectors.end(), |
| + watched_css_selectors_.begin())) { |
| + watched_css_selectors_.assign(css_selectors.begin(), css_selectors.end()); |
| + |
| + for (content::RenderProcessHost::iterator it( |
| + content::RenderProcessHost::AllHostsIterator()); |
| + !it.IsAtEnd(); it.Advance()) { |
| + content::RenderProcessHost* process = it.GetCurrentValue(); |
| + Profile* process_profile = |
| + Profile::FromBrowserContext(process->GetBrowserContext()); |
| + // Watch the conditions in incognito profiles too to avoid needing two |
| + // sets of conditions. We'll filter out the extensions that can't run in |
| + // 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
|
| + if (process_profile->GetOriginalProfile() == profile_) |
|
Matt Perry
2012/12/13 20:27:48
nit: IsSameProfile(profile_) does this too
|
| + InstructRenderProcess(process); |
| + } |
| + } |
| +} |
| + |
| +void ContentRulesRegistry::InstructRenderProcess( |
| + content::RenderProcessHost* process) { |
| + process->Send(new ExtensionMsg_WatchPages(watched_css_selectors_)); |
| +} |
| + |
| +content::BrowserThread::ID ContentRulesRegistry::GetOwnerThread() const { |
| + return content::BrowserThread::UI; |
| +} |
| + |
| +bool ContentRulesRegistry::IsEmpty() const { |
| + return rule_triggers_.empty() && content_rules_.empty() && |
| + url_matcher_.IsEmpty(); |
| +} |
| + |
| +ContentRulesRegistry::~ContentRulesRegistry() {} |
| + |
| +base::Time ContentRulesRegistry::GetExtensionInstallationTime( |
| + const std::string& extension_id) const { |
| + if (!extension_info_map_.get()) // May be NULL during testing. |
| + return base::Time(); |
| + |
| + return extension_info_map_->GetInstallTime(extension_id); |
| +} |
| + |
| +} // namespace extensions |