Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: chrome/browser/extensions/api/declarative_content/content_rules_registry.cc

Issue 11547033: Implement declarativeContent API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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::const_iterator rule_iter =
102 match_id_to_rule_.find(*url_match);
103 CHECK(rule_iter != match_id_to_rule_.end());
104
105 ContentRule* rule = rule_iter->second;
106 if (rule->conditions().IsFulfilled(*url_match, url_matches, renderer_data))
107 result.insert(rule);
108 }
109 return result;
110 }
111
112 std::string ContentRulesRegistry::AddRulesImpl(
113 const std::string& extension_id,
114 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) {
115 base::Time extension_installation_time =
116 GetExtensionInstallationTime(extension_id);
117
118 std::string error;
119 RulesMap new_content_rules;
120
121 for (std::vector<linked_ptr<RulesRegistry::Rule> >::const_iterator rule =
122 rules.begin(); rule != rules.end(); ++rule) {
123 ContentRule::GlobalRuleId rule_id(extension_id, *(*rule)->id);
124 DCHECK(content_rules_.find(rule_id) == content_rules_.end());
125
126 scoped_ptr<ContentRule> content_rule(
127 ContentRule::Create(url_matcher_.condition_factory(), extension_id,
128 extension_installation_time, *rule, NULL, &error));
129 if (!error.empty()) {
130 // Clean up temporary condition sets created during rule creation.
131 url_matcher_.ClearUnusedConditionSets();
132 return error;
133 }
134 DCHECK(content_rule);
135
136 new_content_rules[rule_id] = make_linked_ptr(content_rule.release());
137 }
138
139 // Wohoo, everything worked fine.
140 content_rules_.insert(new_content_rules.begin(), new_content_rules.end());
141
142 // Create the triggers.
143 for (RulesMap::iterator i = new_content_rules.begin();
144 i != new_content_rules.end(); ++i) {
145 URLMatcherConditionSet::Vector url_condition_sets;
146 const ContentConditionSet& conditions = i->second->conditions();
147 conditions.GetURLMatcherConditionSets(&url_condition_sets);
148 for (URLMatcherConditionSet::Vector::iterator j =
149 url_condition_sets.begin(); j != url_condition_sets.end(); ++j) {
150 match_id_to_rule_[(*j)->id()] = i->second.get();
151 }
152 }
153
154 // Register url patterns in url_matcher_.
155 URLMatcherConditionSet::Vector all_new_condition_sets;
156 for (RulesMap::iterator i = new_content_rules.begin();
157 i != new_content_rules.end(); ++i) {
158 i->second->conditions().GetURLMatcherConditionSets(&all_new_condition_sets);
159 }
160 url_matcher_.AddConditionSets(all_new_condition_sets);
161
162 UpdateConditionCache();
163
164 return "";
165 }
166
167 std::string ContentRulesRegistry::RemoveRulesImpl(
168 const std::string& extension_id,
169 const std::vector<std::string>& rule_identifiers) {
170 // URLMatcherConditionSet IDs that can be removed from URLMatcher.
171 std::vector<URLMatcherConditionSet::ID> remove_from_url_matcher;
172
173 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin();
174 i != rule_identifiers.end(); ++i) {
175 ContentRule::GlobalRuleId rule_id(extension_id, *i);
176
177 // Skip unknown rules.
178 RulesMap::iterator content_rules_entry = content_rules_.find(rule_id);
179 if (content_rules_entry == content_rules_.end())
180 continue;
181
182 // Remove all triggers but collect their IDs.
183 URLMatcherConditionSet::Vector condition_sets;
184 ContentRule* rule = content_rules_entry->second.get();
185 rule->conditions().GetURLMatcherConditionSets(&condition_sets);
186 for (URLMatcherConditionSet::Vector::iterator j = condition_sets.begin();
187 j != condition_sets.end(); ++j) {
188 remove_from_url_matcher.push_back((*j)->id());
189 match_id_to_rule_.erase((*j)->id());
190 }
191
192 // Remove the ContentRule from active_rules_.
193 for (std::map<int, std::set<ContentRule*> >::iterator
194 it = active_rules_.begin();
195 it != active_rules_.end(); ++it) {
196 // Has no effect if the rule wasn't present.
197 it->second.erase(rule);
198 }
199
200 // Remove reference to actual rule.
201 content_rules_.erase(content_rules_entry);
202 }
203
204 // Clear URLMatcher based on condition_set_ids that are not needed any more.
205 url_matcher_.RemoveConditionSets(remove_from_url_matcher);
206
207 UpdateConditionCache();
208
209 return "";
210 }
211
212 std::string ContentRulesRegistry::RemoveAllRulesImpl(
213 const std::string& extension_id) {
214 // Search all identifiers of rules that belong to extension |extension_id|.
215 std::vector<std::string> rule_identifiers;
216 for (RulesMap::iterator i = content_rules_.begin();
217 i != content_rules_.end(); ++i) {
218 const ContentRule::GlobalRuleId& global_rule_id = i->first;
219 if (global_rule_id.first == extension_id)
220 rule_identifiers.push_back(global_rule_id.second);
221 }
222
223 return RemoveRulesImpl(extension_id, rule_identifiers);
224 }
225
226 void ContentRulesRegistry::UpdateConditionCache() {
227 std::set<std::string> css_selectors; // We rely on this being sorted.
228 for (RulesMap::const_iterator i = content_rules_.begin();
229 i != content_rules_.end(); ++i) {
230 ContentRule& rule = *i->second;
231 for (ContentConditionSet::const_iterator
232 condition = rule.conditions().begin();
233 condition != rule.conditions().end(); ++condition) {
234 const std::vector<std::string>& condition_css_selectors =
235 (*condition)->css_selectors();
236 css_selectors.insert(condition_css_selectors.begin(),
237 condition_css_selectors.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 if (process->GetBrowserContext() == profile_)
251 InstructRenderProcess(process);
252 }
253 }
254 }
255
256 void ContentRulesRegistry::InstructRenderProcess(
257 content::RenderProcessHost* process) {
258 process->Send(new ExtensionMsg_WatchPages(watched_css_selectors_));
259 }
260
261 content::BrowserThread::ID ContentRulesRegistry::GetOwnerThread() const {
262 return content::BrowserThread::UI;
263 }
264
265 bool ContentRulesRegistry::IsEmpty() const {
266 return match_id_to_rule_.empty() && content_rules_.empty() &&
267 url_matcher_.IsEmpty();
268 }
269
270 ContentRulesRegistry::~ContentRulesRegistry() {}
271
272 base::Time ContentRulesRegistry::GetExtensionInstallationTime(
273 const std::string& extension_id) const {
274 if (!extension_info_map_.get()) // May be NULL during testing.
275 return base::Time();
276
277 return extension_info_map_->GetInstallTime(extension_id);
278 }
279
280 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698