OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // DeclarativeRule<>, DeclarativeConditionSet<>, and DeclarativeActionSet<> | 5 // DeclarativeRule<>, DeclarativeConditionSet<>, and DeclarativeActionSet<> |
6 // templates usable with multiple different declarativeFoo systems. These are | 6 // templates usable with multiple different declarativeFoo systems. These are |
7 // templated on the Condition and Action types that define the behavior of a | 7 // templated on the Condition and Action types that define the behavior of a |
8 // particular declarative event. | 8 // particular declarative event. |
9 | 9 |
10 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 10 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
(...skipping 11 matching lines...) Expand all Loading... | |
22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
23 #include "chrome/common/extensions/api/events.h" | 23 #include "chrome/common/extensions/api/events.h" |
24 #include "components/url_matcher/url_matcher.h" | 24 #include "components/url_matcher/url_matcher.h" |
25 #include "extensions/common/extension.h" | 25 #include "extensions/common/extension.h" |
26 | 26 |
27 namespace base { | 27 namespace base { |
28 class Time; | 28 class Time; |
29 class Value; | 29 class Value; |
30 } | 30 } |
31 | 31 |
32 class Profile; | |
33 | |
32 namespace extensions { | 34 namespace extensions { |
33 | 35 |
34 // This class stores a set of conditions that may be part of a DeclarativeRule. | 36 // This class stores a set of conditions that may be part of a DeclarativeRule. |
35 // If any condition is fulfilled, the Actions of the DeclarativeRule can be | 37 // If any condition is fulfilled, the Actions of the DeclarativeRule can be |
36 // triggered. | 38 // triggered. |
37 // | 39 // |
38 // ConditionT should be immutable after creation. It must define the following | 40 // ConditionT should be immutable after creation. It must define the following |
39 // members: | 41 // members: |
40 // | 42 // |
41 // // Arguments passed through from DeclarativeConditionSet::Create. | 43 // // Arguments passed through from DeclarativeConditionSet::Create. |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 class DeclarativeActionSet { | 141 class DeclarativeActionSet { |
140 public: | 142 public: |
141 typedef std::vector<linked_ptr<base::Value> > AnyVector; | 143 typedef std::vector<linked_ptr<base::Value> > AnyVector; |
142 typedef std::vector<scoped_refptr<const ActionT> > Actions; | 144 typedef std::vector<scoped_refptr<const ActionT> > Actions; |
143 | 145 |
144 explicit DeclarativeActionSet(const Actions& actions); | 146 explicit DeclarativeActionSet(const Actions& actions); |
145 | 147 |
146 // Factory method that instantiates a DeclarativeActionSet for |extension| | 148 // Factory method that instantiates a DeclarativeActionSet for |extension| |
147 // according to |actions| which represents the array of actions received from | 149 // according to |actions| which represents the array of actions received from |
148 // the extension API. | 150 // the extension API. |
149 static scoped_ptr<DeclarativeActionSet> Create(const Extension* extension, | 151 static scoped_ptr<DeclarativeActionSet> Create(Profile* profile, |
Jeffrey Yasskin
2014/08/22 01:12:24
Can this take a BrowserContext instead of a Profil
Mark Dittmer
2014/08/22 19:59:18
Yes. Done.
| |
152 const Extension* extension, | |
150 const AnyVector& actions, | 153 const AnyVector& actions, |
151 std::string* error, | 154 std::string* error, |
152 bool* bad_message); | 155 bool* bad_message); |
153 | 156 |
154 // Rules call this method when their conditions are fulfilled. | 157 // Rules call this method when their conditions are fulfilled. |
155 void Apply(const std::string& extension_id, | 158 void Apply(const std::string& extension_id, |
156 const base::Time& extension_install_time, | 159 const base::Time& extension_install_time, |
157 typename ActionT::ApplyInfo* apply_info) const; | 160 typename ActionT::ApplyInfo* apply_info) const; |
158 | 161 |
159 // Rules call this method when their conditions are fulfilled, but Apply has | 162 // Rules call this method when their conditions are fulfilled, but Apply has |
160 // already been called. | 163 // already been called. |
161 void Reapply(const std::string& extension_id, | 164 void Reapply(const std::string& extension_id, |
162 const base::Time& extension_install_time, | 165 const base::Time& extension_install_time, |
163 typename ActionT::ApplyInfo* apply_info) const; | 166 typename ActionT::ApplyInfo* apply_info) const; |
164 | 167 |
165 // Rules call this method when they have stateful conditions, and those | 168 // Rules call this method when they have stateful conditions, and those |
166 // conditions stop being fulfilled. Rules with event-based conditions (e.g. a | 169 // conditions stop being fulfilled. Rules with event-based conditions (e.g. a |
167 // network request happened) will never Revert() an action. | 170 // network request happened) will never Revert() an action. |
168 void Revert(const std::string& extension_id, | 171 void Revert(const std::string& extension_id, |
169 const base::Time& extension_install_time, | 172 const base::Time& extension_install_time, |
170 typename ActionT::ApplyInfo* apply_info) const; | 173 typename ActionT::ApplyInfo* apply_info) const; |
171 | 174 |
175 // Rules call this method when their conditions are checked and were already | |
176 // fulfilled previously. | |
177 void Reapply(const std::string& extension_id, | |
Jeffrey Yasskin
2014/08/22 01:12:24
This looks like it duplicates the other Reapply().
Mark Dittmer
2014/08/22 19:59:18
Yes. Merge fail. Fixed.
| |
178 const base::Time& extension_install_time, | |
179 typename ActionT::ApplyInfo* apply_info) const; | |
180 | |
172 // Returns the minimum priority of rules that may be evaluated after | 181 // Returns the minimum priority of rules that may be evaluated after |
173 // this rule. Defaults to MIN_INT. | 182 // this rule. Defaults to MIN_INT. |
174 int GetMinimumPriority() const; | 183 int GetMinimumPriority() const; |
175 | 184 |
176 const Actions& actions() const { return actions_; } | 185 const Actions& actions() const { return actions_; } |
177 | 186 |
178 private: | 187 private: |
179 const Actions actions_; | 188 const Actions actions_; |
180 | 189 |
181 DISALLOW_COPY_AND_ASSIGN(DeclarativeActionSet); | 190 DISALLOW_COPY_AND_ASSIGN(DeclarativeActionSet); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
216 // Creates a DeclarativeRule for |extension| given a json definition. The | 225 // Creates a DeclarativeRule for |extension| given a json definition. The |
217 // format of each condition and action's json is up to the specific ConditionT | 226 // format of each condition and action's json is up to the specific ConditionT |
218 // and ActionT. |extension| may be NULL in tests. | 227 // and ActionT. |extension| may be NULL in tests. |
219 // | 228 // |
220 // Before constructing the final rule, calls check_consistency(conditions, | 229 // Before constructing the final rule, calls check_consistency(conditions, |
221 // actions, error) and returns NULL if it fails. Pass NULL if no consistency | 230 // actions, error) and returns NULL if it fails. Pass NULL if no consistency |
222 // check is needed. If |error| is empty, the translation was successful and | 231 // check is needed. If |error| is empty, the translation was successful and |
223 // the returned rule is internally consistent. | 232 // the returned rule is internally consistent. |
224 static scoped_ptr<DeclarativeRule> Create( | 233 static scoped_ptr<DeclarativeRule> Create( |
225 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, | 234 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, |
235 Profile* profile, | |
226 const Extension* extension, | 236 const Extension* extension, |
227 base::Time extension_installation_time, | 237 base::Time extension_installation_time, |
228 linked_ptr<JsonRule> rule, | 238 linked_ptr<JsonRule> rule, |
229 ConsistencyChecker check_consistency, | 239 ConsistencyChecker check_consistency, |
230 std::string* error); | 240 std::string* error); |
231 | 241 |
232 const GlobalRuleId& id() const { return id_; } | 242 const GlobalRuleId& id() const { return id_; } |
233 const Tags& tags() const { return tags_; } | 243 const Tags& tags() const { return tags_; } |
234 const std::string& extension_id() const { return id_.first; } | 244 const std::string& extension_id() const { return id_.first; } |
235 const ConditionSet& conditions() const { return *conditions_; } | 245 const ConditionSet& conditions() const { return *conditions_; } |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
351 // | 361 // |
352 | 362 |
353 template<typename ActionT> | 363 template<typename ActionT> |
354 DeclarativeActionSet<ActionT>::DeclarativeActionSet(const Actions& actions) | 364 DeclarativeActionSet<ActionT>::DeclarativeActionSet(const Actions& actions) |
355 : actions_(actions) {} | 365 : actions_(actions) {} |
356 | 366 |
357 // static | 367 // static |
358 template<typename ActionT> | 368 template<typename ActionT> |
359 scoped_ptr<DeclarativeActionSet<ActionT> > | 369 scoped_ptr<DeclarativeActionSet<ActionT> > |
360 DeclarativeActionSet<ActionT>::Create( | 370 DeclarativeActionSet<ActionT>::Create( |
371 Profile* profile, | |
361 const Extension* extension, | 372 const Extension* extension, |
362 const AnyVector& actions, | 373 const AnyVector& actions, |
363 std::string* error, | 374 std::string* error, |
364 bool* bad_message) { | 375 bool* bad_message) { |
365 *error = ""; | 376 *error = ""; |
366 *bad_message = false; | 377 *bad_message = false; |
367 Actions result; | 378 Actions result; |
368 | 379 |
369 for (AnyVector::const_iterator i = actions.begin(); | 380 for (AnyVector::const_iterator i = actions.begin(); |
370 i != actions.end(); ++i) { | 381 i != actions.end(); ++i) { |
371 CHECK(i->get()); | 382 CHECK(i->get()); |
372 scoped_refptr<const ActionT> action = | 383 scoped_refptr<const ActionT> action = |
373 ActionT::Create(extension, **i, error, bad_message); | 384 ActionT::Create(profile, extension, **i, error, bad_message); |
374 if (!error->empty() || *bad_message) | 385 if (!error->empty() || *bad_message) |
375 return scoped_ptr<DeclarativeActionSet>(); | 386 return scoped_ptr<DeclarativeActionSet>(); |
376 result.push_back(action); | 387 result.push_back(action); |
377 } | 388 } |
378 | 389 |
379 return scoped_ptr<DeclarativeActionSet>(new DeclarativeActionSet(result)); | 390 return scoped_ptr<DeclarativeActionSet>(new DeclarativeActionSet(result)); |
380 } | 391 } |
381 | 392 |
382 template<typename ActionT> | 393 template<typename ActionT> |
383 void DeclarativeActionSet<ActionT>::Apply( | 394 void DeclarativeActionSet<ActionT>::Apply( |
(...skipping 19 matching lines...) Expand all Loading... | |
403 void DeclarativeActionSet<ActionT>::Revert( | 414 void DeclarativeActionSet<ActionT>::Revert( |
404 const std::string& extension_id, | 415 const std::string& extension_id, |
405 const base::Time& extension_install_time, | 416 const base::Time& extension_install_time, |
406 typename ActionT::ApplyInfo* apply_info) const { | 417 typename ActionT::ApplyInfo* apply_info) const { |
407 for (typename Actions::const_iterator i = actions_.begin(); | 418 for (typename Actions::const_iterator i = actions_.begin(); |
408 i != actions_.end(); ++i) | 419 i != actions_.end(); ++i) |
409 (*i)->Revert(extension_id, extension_install_time, apply_info); | 420 (*i)->Revert(extension_id, extension_install_time, apply_info); |
410 } | 421 } |
411 | 422 |
412 template<typename ActionT> | 423 template<typename ActionT> |
424 void DeclarativeActionSet<ActionT>::Reapply( | |
425 const std::string& extension_id, | |
426 const base::Time& extension_install_time, | |
427 typename ActionT::ApplyInfo* apply_info) const { | |
428 for (typename Actions::const_iterator i = actions_.begin(); | |
429 i != actions_.end(); ++i) | |
430 (*i)->Reapply(extension_id, extension_install_time, apply_info); | |
431 } | |
432 | |
433 template<typename ActionT> | |
413 int DeclarativeActionSet<ActionT>::GetMinimumPriority() const { | 434 int DeclarativeActionSet<ActionT>::GetMinimumPriority() const { |
414 int minimum_priority = std::numeric_limits<int>::min(); | 435 int minimum_priority = std::numeric_limits<int>::min(); |
415 for (typename Actions::const_iterator i = actions_.begin(); | 436 for (typename Actions::const_iterator i = actions_.begin(); |
416 i != actions_.end(); ++i) { | 437 i != actions_.end(); ++i) { |
417 minimum_priority = std::max(minimum_priority, (*i)->minimum_priority()); | 438 minimum_priority = std::max(minimum_priority, (*i)->minimum_priority()); |
418 } | 439 } |
419 return minimum_priority; | 440 return minimum_priority; |
420 } | 441 } |
421 | 442 |
422 // | 443 // |
(...skipping 16 matching lines...) Expand all Loading... | |
439 priority_(priority) { | 460 priority_(priority) { |
440 CHECK(conditions_.get()); | 461 CHECK(conditions_.get()); |
441 CHECK(actions_.get()); | 462 CHECK(actions_.get()); |
442 } | 463 } |
443 | 464 |
444 // static | 465 // static |
445 template<typename ConditionT, typename ActionT> | 466 template<typename ConditionT, typename ActionT> |
446 scoped_ptr<DeclarativeRule<ConditionT, ActionT> > | 467 scoped_ptr<DeclarativeRule<ConditionT, ActionT> > |
447 DeclarativeRule<ConditionT, ActionT>::Create( | 468 DeclarativeRule<ConditionT, ActionT>::Create( |
448 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, | 469 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, |
470 Profile* profile, | |
449 const Extension* extension, | 471 const Extension* extension, |
450 base::Time extension_installation_time, | 472 base::Time extension_installation_time, |
451 linked_ptr<JsonRule> rule, | 473 linked_ptr<JsonRule> rule, |
452 ConsistencyChecker check_consistency, | 474 ConsistencyChecker check_consistency, |
453 std::string* error) { | 475 std::string* error) { |
454 scoped_ptr<DeclarativeRule> error_result; | 476 scoped_ptr<DeclarativeRule> error_result; |
455 | 477 |
456 scoped_ptr<ConditionSet> conditions = ConditionSet::Create( | 478 scoped_ptr<ConditionSet> conditions = ConditionSet::Create( |
457 extension, url_matcher_condition_factory, rule->conditions, error); | 479 extension, url_matcher_condition_factory, rule->conditions, error); |
458 if (!error->empty()) | 480 if (!error->empty()) |
459 return error_result.Pass(); | 481 return error_result.Pass(); |
460 CHECK(conditions.get()); | 482 CHECK(conditions.get()); |
461 | 483 |
462 bool bad_message = false; | 484 bool bad_message = false; |
463 scoped_ptr<ActionSet> actions = | 485 scoped_ptr<ActionSet> actions = |
464 ActionSet::Create(extension, rule->actions, error, &bad_message); | 486 ActionSet::Create(profile, extension, rule->actions, error, &bad_message); |
465 if (bad_message) { | 487 if (bad_message) { |
466 // TODO(battre) Export concept of bad_message to caller, the extension | 488 // TODO(battre) Export concept of bad_message to caller, the extension |
467 // should be killed in case it is true. | 489 // should be killed in case it is true. |
468 *error = "An action of a rule set had an invalid " | 490 *error = "An action of a rule set had an invalid " |
469 "structure that should have been caught by the JSON validator."; | 491 "structure that should have been caught by the JSON validator."; |
470 return error_result.Pass(); | 492 return error_result.Pass(); |
471 } | 493 } |
472 if (!error->empty() || bad_message) | 494 if (!error->empty() || bad_message) |
473 return error_result.Pass(); | 495 return error_result.Pass(); |
474 CHECK(actions.get()); | 496 CHECK(actions.get()); |
(...skipping 23 matching lines...) Expand all Loading... | |
498 } | 520 } |
499 | 521 |
500 template<typename ConditionT, typename ActionT> | 522 template<typename ConditionT, typename ActionT> |
501 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { | 523 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { |
502 return actions_->GetMinimumPriority(); | 524 return actions_->GetMinimumPriority(); |
503 } | 525 } |
504 | 526 |
505 } // namespace extensions | 527 } // namespace extensions |
506 | 528 |
507 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 529 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
OLD | NEW |