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

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

Issue 1159733004: Encapsulate CSS selector declarative content condition tracking (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@stars-declarative-content-range-for
Patch Set: iwyu Created 5 years, 6 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
OLDNEW
(Empty)
1 // Copyright 2015 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/declarative_content_ rule.h"
6
7 #include <limits>
8
9 #include "base/time/time.h"
10 #include "extensions/common/extension.h"
11
12 namespace extensions {
13
14 //
15 // DeclarativeContentConditionSet
16 //
17
18 bool DeclarativeContentConditionSet::IsFulfilled(
19 url_matcher::URLMatcherConditionSet::ID url_match_trigger,
20 const RendererContentMatchData& match_data) const {
21 if (url_match_trigger == -1) {
22 // Invalid trigger -- indication that we should only check conditions
23 // without URL attributes.
24 for (const ContentCondition* condition : conditions_without_urls_) {
25 if (condition->IsFulfilled(match_data))
26 return true;
27 }
28 return false;
29 }
30
31 URLMatcherIdToCondition::const_iterator triggered =
32 match_id_to_condition_.find(url_match_trigger);
33 return (triggered != match_id_to_condition_.end() &&
34 triggered->second->IsFulfilled(match_data));
35 }
36
37 void DeclarativeContentConditionSet::GetURLMatcherConditionSets(
38 url_matcher::URLMatcherConditionSet::Vector* condition_sets) const {
39 for (const linked_ptr<const ContentCondition>& condition : conditions_)
40 condition->GetURLMatcherConditionSets(condition_sets);
41 }
42
43 // static
44 scoped_ptr<DeclarativeContentConditionSet>
45 DeclarativeContentConditionSet::Create(
46 const Extension* extension,
47 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory,
48 const Values& condition_values,
49 std::string* error) {
50 Conditions result;
51
52 for (const linked_ptr<base::Value>& value : condition_values) {
53 CHECK(value.get());
54 scoped_ptr<ContentCondition> condition = ContentCondition::Create(
55 extension, url_matcher_condition_factory, *value, error);
56 if (!error->empty())
57 return scoped_ptr<DeclarativeContentConditionSet>();
58 result.push_back(make_linked_ptr(condition.release()));
59 }
60
61 URLMatcherIdToCondition match_id_to_condition;
62 std::vector<const ContentCondition*> conditions_without_urls;
63 url_matcher::URLMatcherConditionSet::Vector condition_sets;
64
65 for (const linked_ptr<const ContentCondition>& condition : result) {
66 condition_sets.clear();
67 condition->GetURLMatcherConditionSets(&condition_sets);
68 if (condition_sets.empty()) {
69 conditions_without_urls.push_back(condition.get());
70 } else {
71 for (const scoped_refptr<url_matcher::URLMatcherConditionSet>& match_set :
72 condition_sets)
73 match_id_to_condition[match_set->id()] = condition.get();
74 }
75 }
76
77 return make_scoped_ptr(new DeclarativeContentConditionSet(
78 result, match_id_to_condition, conditions_without_urls));
79 }
80
81 DeclarativeContentConditionSet::~DeclarativeContentConditionSet() {
82 }
83
84 DeclarativeContentConditionSet::DeclarativeContentConditionSet(
85 const Conditions& conditions,
86 const URLMatcherIdToCondition& match_id_to_condition,
87 const std::vector<const ContentCondition*>& conditions_without_urls)
88 : match_id_to_condition_(match_id_to_condition),
89 conditions_(conditions),
90 conditions_without_urls_(conditions_without_urls) {}
91
92 //
93 // DeclarativeContentActionSet
94 //
95
96 DeclarativeContentActionSet::DeclarativeContentActionSet(const Actions& actions)
97 : actions_(actions) {}
98
99 DeclarativeContentActionSet::~DeclarativeContentActionSet() {}
100
101 // static
102 scoped_ptr<DeclarativeContentActionSet>
103 DeclarativeContentActionSet::Create(content::BrowserContext* browser_context,
104 const Extension* extension,
105 const Values& action_values,
106 std::string* error,
107 bool* bad_message) {
108 *error = "";
109 *bad_message = false;
110 Actions result;
111
112 for (const linked_ptr<base::Value>& value : action_values) {
113 CHECK(value.get());
114 scoped_refptr<const ContentAction> action =
115 ContentAction::Create(browser_context, extension, *value, error,
116 bad_message);
117 if (!error->empty() || *bad_message)
118 return scoped_ptr<DeclarativeContentActionSet>();
119 result.push_back(action);
120 }
121
122 return make_scoped_ptr(new DeclarativeContentActionSet(result));
123 }
124
125 void DeclarativeContentActionSet::Apply(
126 const std::string& extension_id,
127 const base::Time& extension_install_time,
128 ContentAction::ApplyInfo* apply_info) const {
129 for (const scoped_refptr<const ContentAction>& action : actions_)
130 action->Apply(extension_id, extension_install_time, apply_info);
131 }
132
133 void DeclarativeContentActionSet::Reapply(
134 const std::string& extension_id,
135 const base::Time& extension_install_time,
136 ContentAction::ApplyInfo* apply_info) const {
137 for (const scoped_refptr<const ContentAction>& action : actions_)
138 action->Reapply(extension_id, extension_install_time, apply_info);
139 }
140
141 void DeclarativeContentActionSet::Revert(
142 const std::string& extension_id,
143 const base::Time& extension_install_time,
144 ContentAction::ApplyInfo* apply_info) const {
145 for (const scoped_refptr<const ContentAction>& action : actions_)
146 action->Revert(extension_id, extension_install_time, apply_info);
147 }
148
149 //
150 // DeclarativeContentRule
151 //
152
153 DeclarativeContentRule::DeclarativeContentRule(
154 const GlobalRuleId& id,
155 const Tags& tags,
156 base::Time extension_installation_time,
157 scoped_ptr<ConditionSet> conditions,
158 scoped_ptr<ActionSet> actions,
159 Priority priority)
160 : id_(id),
161 tags_(tags),
162 extension_installation_time_(extension_installation_time),
163 conditions_(conditions.release()),
164 actions_(actions.release()),
165 priority_(priority) {
166 CHECK(conditions_.get());
167 CHECK(actions_.get());
168 }
169
170 DeclarativeContentRule::~DeclarativeContentRule() {}
171
172 // static
173 scoped_ptr<DeclarativeContentRule> DeclarativeContentRule::Create(
174 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory,
175 content::BrowserContext* browser_context,
176 const Extension* extension,
177 base::Time extension_installation_time,
178 linked_ptr<JsonRule> rule,
179 ConsistencyChecker check_consistency,
180 std::string* error) {
181 scoped_ptr<DeclarativeContentRule> error_result;
182
183 scoped_ptr<ConditionSet> conditions = ConditionSet::Create(
184 extension, url_matcher_condition_factory, rule->conditions, error);
185 if (!error->empty())
186 return error_result.Pass();
187 CHECK(conditions.get());
188
189 bool bad_message = false;
190 scoped_ptr<ActionSet> actions =
191 ActionSet::Create(
192 browser_context, extension, rule->actions, error, &bad_message);
193 if (bad_message) {
194 // TODO(battre) Export concept of bad_message to caller, the extension
195 // should be killed in case it is true.
196 *error = "An action of a rule set had an invalid "
197 "structure that should have been caught by the JSON validator.";
198 return error_result.Pass();
199 }
200 if (!error->empty() || bad_message)
201 return error_result.Pass();
202 CHECK(actions.get());
203
204 if (!check_consistency.is_null() &&
205 !check_consistency.Run(conditions.get(), actions.get(), error)) {
206 DCHECK(!error->empty());
207 return error_result.Pass();
208 }
209
210 CHECK(rule->priority.get());
211 int priority = *(rule->priority);
212
213 GlobalRuleId rule_id(extension->id(), *(rule->id));
214 Tags tags = rule->tags ? *rule->tags : Tags();
215 return scoped_ptr<DeclarativeContentRule>(
216 new DeclarativeContentRule(rule_id, tags, extension_installation_time,
217 conditions.Pass(), actions.Pass(), priority));
218 }
219
220 void DeclarativeContentRule::Apply(ContentAction::ApplyInfo* apply_info) const {
221 return actions_->Apply(extension_id(),
222 extension_installation_time_,
223 apply_info);
224 }
225
226 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698