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 = 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) { | |
90 std::set<ContentRule*> result; | |
91 | |
92 // Figure out for which rules the URL match conditions were fulfilled. | |
93 std::set<URLMatcherConditionSet::ID> url_matches = | |
94 url_matcher_.MatchURL(renderer_data.page_url); | |
95 | |
96 // Then we need to check for each of these, whether the other | |
97 // attributes are also fulfilled. | |
98 for (std::set<URLMatcherConditionSet::ID>::iterator | |
99 url_match = url_matches.begin(); | |
100 url_match != url_matches.end(); ++url_match) { | |
101 URLMatcherIdToRule::iterator rule_iter = match_id_to_rule_.find(*url_match); | |
102 CHECK(rule_iter != match_id_to_rule_.end()); | |
103 | |
104 ContentRule* rule = rule_iter->second; | |
105 if (rule->conditions().IsFulfilled(*url_match, url_matches, renderer_data)) | |
106 result.insert(rule); | |
107 } | |
108 return result; | |
109 } | |
110 | |
111 std::string ContentRulesRegistry::AddRulesImpl( | |
112 const std::string& extension_id, | |
113 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) { | |
114 base::Time extension_installation_time = | |
115 GetExtensionInstallationTime(extension_id); | |
116 | |
117 std::string error; | |
118 RulesMap new_content_rules; | |
119 | |
120 for (std::vector<linked_ptr<RulesRegistry::Rule> >::const_iterator rule = | |
121 rules.begin(); rule != rules.end(); ++rule) { | |
122 ContentRule::GlobalRuleId rule_id(extension_id, *(*rule)->id); | |
123 DCHECK(content_rules_.find(rule_id) == content_rules_.end()); | |
124 | |
125 scoped_ptr<ContentRule> content_rule( | |
126 ContentRule::Create(url_matcher_.condition_factory(), extension_id, | |
127 extension_installation_time, *rule, NULL, &error)); | |
128 if (!error.empty()) { | |
battre
2013/01/18 10:37:29
|| !content_rule
Jeffrey Yasskin
2013/01/22 08:01:22
It's supposed to be impossible for DeclarativeRule
| |
129 // Clean up temporary condition sets created during rule creation. | |
130 url_matcher_.ClearUnusedConditionSets(); | |
131 return error; | |
132 } | |
133 | |
134 new_content_rules[rule_id] = make_linked_ptr(content_rule.release()); | |
135 } | |
136 | |
137 // Wohoo, everything worked fine. | |
138 content_rules_.insert(new_content_rules.begin(), new_content_rules.end()); | |
139 | |
140 // Create the triggers. | |
141 for (RulesMap::iterator i = new_content_rules.begin(); | |
142 i != new_content_rules.end(); ++i) { | |
143 URLMatcherConditionSet::Vector url_condition_sets; | |
144 const ContentConditionSet& conditions = i->second->conditions(); | |
145 conditions.GetURLMatcherConditionSets(&url_condition_sets); | |
146 for (URLMatcherConditionSet::Vector::iterator j = | |
147 url_condition_sets.begin(); j != url_condition_sets.end(); ++j) { | |
148 match_id_to_rule_[(*j)->id()] = i->second.get(); | |
149 } | |
150 } | |
151 | |
152 // Register url patterns in url_matcher_. | |
153 URLMatcherConditionSet::Vector all_new_condition_sets; | |
154 for (RulesMap::iterator i = new_content_rules.begin(); | |
155 i != new_content_rules.end(); ++i) { | |
156 i->second->conditions().GetURLMatcherConditionSets(&all_new_condition_sets); | |
157 } | |
158 url_matcher_.AddConditionSets(all_new_condition_sets); | |
159 | |
160 UpdateConditionCache(); | |
161 | |
162 return ""; | |
163 } | |
164 | |
165 std::string ContentRulesRegistry::RemoveRulesImpl( | |
166 const std::string& extension_id, | |
167 const std::vector<std::string>& rule_identifiers) { | |
168 // URLMatcherConditionSet IDs that can be removed from URLMatcher. | |
169 std::vector<URLMatcherConditionSet::ID> remove_from_url_matcher; | |
170 | |
171 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); | |
172 i != rule_identifiers.end(); ++i) { | |
173 ContentRule::GlobalRuleId rule_id(extension_id, *i); | |
174 | |
175 // Skip unknown rules. | |
176 RulesMap::iterator content_rules_entry = content_rules_.find(rule_id); | |
177 if (content_rules_entry == content_rules_.end()) | |
178 continue; | |
179 | |
180 // Remove all triggers but collect their IDs. | |
181 URLMatcherConditionSet::Vector condition_sets; | |
182 ContentRule* rule = content_rules_entry->second.get(); | |
183 rule->conditions().GetURLMatcherConditionSets(&condition_sets); | |
184 for (URLMatcherConditionSet::Vector::iterator j = condition_sets.begin(); | |
185 j != condition_sets.end(); ++j) { | |
186 remove_from_url_matcher.push_back((*j)->id()); | |
187 match_id_to_rule_.erase((*j)->id()); | |
188 } | |
189 | |
190 // Remove the ContentRule from active_rules_. | |
191 for (std::map<int, std::set<ContentRule*> >::iterator | |
192 it = active_rules_.begin(); | |
193 it != active_rules_.end(); ++it) { | |
194 // Has no effect if the rule wasn't present. | |
195 it->second.erase(rule); | |
196 } | |
197 | |
198 // Remove reference to actual rule. | |
199 content_rules_.erase(content_rules_entry); | |
200 } | |
201 | |
202 // Clear URLMatcher based on condition_set_ids that are not needed any more. | |
203 url_matcher_.RemoveConditionSets(remove_from_url_matcher); | |
204 | |
205 UpdateConditionCache(); | |
206 | |
207 return ""; | |
208 } | |
209 | |
210 std::string ContentRulesRegistry::RemoveAllRulesImpl( | |
211 const std::string& extension_id) { | |
212 // Search all identifiers of rules that belong to extension |extension_id|. | |
213 std::vector<std::string> rule_identifiers; | |
214 for (RulesMap::iterator i = content_rules_.begin(); | |
215 i != content_rules_.end(); ++i) { | |
216 const ContentRule::GlobalRuleId& global_rule_id = i->first; | |
217 if (global_rule_id.first == extension_id) | |
218 rule_identifiers.push_back(global_rule_id.second); | |
219 } | |
220 | |
221 return RemoveRulesImpl(extension_id, rule_identifiers); | |
222 } | |
223 | |
224 void ContentRulesRegistry::UpdateConditionCache() { | |
225 std::set<std::string> css_selectors; // We rely on this being sorted. | |
226 for (RulesMap::const_iterator i = content_rules_.begin(); | |
227 i != content_rules_.end(); ++i) { | |
228 ContentRule& rule = *i->second; | |
229 for (ContentConditionSet::const_iterator | |
230 condition = rule.conditions().begin(); | |
231 condition != rule.conditions().end(); ++condition) { | |
232 const std::vector<std::string>& condition_css_selectors = | |
233 (*condition)->css_selectors(); | |
234 css_selectors.insert(condition_css_selectors.begin(), | |
235 condition_css_selectors.end()); | |
236 } | |
237 } | |
238 | |
239 if (css_selectors.size() != watched_css_selectors_.size() || | |
240 !std::equal(css_selectors.begin(), css_selectors.end(), | |
241 watched_css_selectors_.begin())) { | |
242 watched_css_selectors_.assign(css_selectors.begin(), css_selectors.end()); | |
243 | |
244 for (content::RenderProcessHost::iterator it( | |
245 content::RenderProcessHost::AllHostsIterator()); | |
246 !it.IsAtEnd(); it.Advance()) { | |
247 content::RenderProcessHost* process = it.GetCurrentValue(); | |
248 if (process->GetBrowserContext() == profile_) | |
249 InstructRenderProcess(process); | |
250 } | |
251 } | |
252 } | |
253 | |
254 void ContentRulesRegistry::InstructRenderProcess( | |
255 content::RenderProcessHost* process) { | |
256 process->Send(new ExtensionMsg_WatchPages(watched_css_selectors_)); | |
257 } | |
258 | |
259 content::BrowserThread::ID ContentRulesRegistry::GetOwnerThread() const { | |
260 return content::BrowserThread::UI; | |
261 } | |
262 | |
263 bool ContentRulesRegistry::IsEmpty() const { | |
264 return match_id_to_rule_.empty() && content_rules_.empty() && | |
265 url_matcher_.IsEmpty(); | |
266 } | |
267 | |
268 ContentRulesRegistry::~ContentRulesRegistry() {} | |
269 | |
270 base::Time ContentRulesRegistry::GetExtensionInstallationTime( | |
271 const std::string& extension_id) const { | |
272 if (!extension_info_map_.get()) // May be NULL during testing. | |
273 return base::Time(); | |
274 | |
275 return extension_info_map_->GetInstallTime(extension_id); | |
276 } | |
277 | |
278 } // namespace extensions | |
OLD | NEW |