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, |
| 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 they have stateful conditions, and those | 162 // Rules call this method when they have stateful conditions, and those |
160 // conditions stop being fulfilled. Rules with event-based conditions (e.g. a | 163 // conditions stop being fulfilled. Rules with event-based conditions (e.g. a |
161 // network request happened) will never Revert() an action. | 164 // network request happened) will never Revert() an action. |
162 void Revert(const std::string& extension_id, | 165 void Revert(const std::string& extension_id, |
163 const base::Time& extension_install_time, | 166 const base::Time& extension_install_time, |
164 typename ActionT::ApplyInfo* apply_info) const; | 167 typename ActionT::ApplyInfo* apply_info) const; |
165 | 168 |
| 169 // Rules call this method when their conditions are checked and were already |
| 170 // fulfilled previously. |
| 171 void Reapply(const std::string& extension_id, |
| 172 const base::Time& extension_install_time, |
| 173 typename ActionT::ApplyInfo* apply_info) const; |
| 174 |
166 // Returns the minimum priority of rules that may be evaluated after | 175 // Returns the minimum priority of rules that may be evaluated after |
167 // this rule. Defaults to MIN_INT. | 176 // this rule. Defaults to MIN_INT. |
168 int GetMinimumPriority() const; | 177 int GetMinimumPriority() const; |
169 | 178 |
170 const Actions& actions() const { return actions_; } | 179 const Actions& actions() const { return actions_; } |
171 | 180 |
172 private: | 181 private: |
173 const Actions actions_; | 182 const Actions actions_; |
174 | 183 |
175 DISALLOW_COPY_AND_ASSIGN(DeclarativeActionSet); | 184 DISALLOW_COPY_AND_ASSIGN(DeclarativeActionSet); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 // Creates a DeclarativeRule for |extension| given a json definition. The | 219 // Creates a DeclarativeRule for |extension| given a json definition. The |
211 // format of each condition and action's json is up to the specific ConditionT | 220 // format of each condition and action's json is up to the specific ConditionT |
212 // and ActionT. |extension| may be NULL in tests. | 221 // and ActionT. |extension| may be NULL in tests. |
213 // | 222 // |
214 // Before constructing the final rule, calls check_consistency(conditions, | 223 // Before constructing the final rule, calls check_consistency(conditions, |
215 // actions, error) and returns NULL if it fails. Pass NULL if no consistency | 224 // actions, error) and returns NULL if it fails. Pass NULL if no consistency |
216 // check is needed. If |error| is empty, the translation was successful and | 225 // check is needed. If |error| is empty, the translation was successful and |
217 // the returned rule is internally consistent. | 226 // the returned rule is internally consistent. |
218 static scoped_ptr<DeclarativeRule> Create( | 227 static scoped_ptr<DeclarativeRule> Create( |
219 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, | 228 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, |
| 229 Profile* profile, |
220 const Extension* extension, | 230 const Extension* extension, |
221 base::Time extension_installation_time, | 231 base::Time extension_installation_time, |
222 linked_ptr<JsonRule> rule, | 232 linked_ptr<JsonRule> rule, |
223 ConsistencyChecker check_consistency, | 233 ConsistencyChecker check_consistency, |
224 std::string* error); | 234 std::string* error); |
225 | 235 |
226 const GlobalRuleId& id() const { return id_; } | 236 const GlobalRuleId& id() const { return id_; } |
227 const Tags& tags() const { return tags_; } | 237 const Tags& tags() const { return tags_; } |
228 const std::string& extension_id() const { return id_.first; } | 238 const std::string& extension_id() const { return id_.first; } |
229 const ConditionSet& conditions() const { return *conditions_; } | 239 const ConditionSet& conditions() const { return *conditions_; } |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 // | 355 // |
346 | 356 |
347 template<typename ActionT> | 357 template<typename ActionT> |
348 DeclarativeActionSet<ActionT>::DeclarativeActionSet(const Actions& actions) | 358 DeclarativeActionSet<ActionT>::DeclarativeActionSet(const Actions& actions) |
349 : actions_(actions) {} | 359 : actions_(actions) {} |
350 | 360 |
351 // static | 361 // static |
352 template<typename ActionT> | 362 template<typename ActionT> |
353 scoped_ptr<DeclarativeActionSet<ActionT> > | 363 scoped_ptr<DeclarativeActionSet<ActionT> > |
354 DeclarativeActionSet<ActionT>::Create( | 364 DeclarativeActionSet<ActionT>::Create( |
| 365 Profile* profile, |
355 const Extension* extension, | 366 const Extension* extension, |
356 const AnyVector& actions, | 367 const AnyVector& actions, |
357 std::string* error, | 368 std::string* error, |
358 bool* bad_message) { | 369 bool* bad_message) { |
359 *error = ""; | 370 *error = ""; |
360 *bad_message = false; | 371 *bad_message = false; |
361 Actions result; | 372 Actions result; |
362 | 373 |
363 for (AnyVector::const_iterator i = actions.begin(); | 374 for (AnyVector::const_iterator i = actions.begin(); |
364 i != actions.end(); ++i) { | 375 i != actions.end(); ++i) { |
365 CHECK(i->get()); | 376 CHECK(i->get()); |
366 scoped_refptr<const ActionT> action = | 377 scoped_refptr<const ActionT> action = |
367 ActionT::Create(extension, **i, error, bad_message); | 378 ActionT::Create(profile, extension, **i, error, bad_message); |
368 if (!error->empty() || *bad_message) | 379 if (!error->empty() || *bad_message) |
369 return scoped_ptr<DeclarativeActionSet>(); | 380 return scoped_ptr<DeclarativeActionSet>(); |
370 result.push_back(action); | 381 result.push_back(action); |
371 } | 382 } |
372 | 383 |
373 return scoped_ptr<DeclarativeActionSet>(new DeclarativeActionSet(result)); | 384 return scoped_ptr<DeclarativeActionSet>(new DeclarativeActionSet(result)); |
374 } | 385 } |
375 | 386 |
376 template<typename ActionT> | 387 template<typename ActionT> |
377 void DeclarativeActionSet<ActionT>::Apply( | 388 void DeclarativeActionSet<ActionT>::Apply( |
378 const std::string& extension_id, | 389 const std::string& extension_id, |
379 const base::Time& extension_install_time, | 390 const base::Time& extension_install_time, |
380 typename ActionT::ApplyInfo* apply_info) const { | 391 typename ActionT::ApplyInfo* apply_info) const { |
381 for (typename Actions::const_iterator i = actions_.begin(); | 392 for (typename Actions::const_iterator i = actions_.begin(); |
382 i != actions_.end(); ++i) | 393 i != actions_.end(); ++i) |
383 (*i)->Apply(extension_id, extension_install_time, apply_info); | 394 (*i)->Apply(extension_id, extension_install_time, apply_info); |
384 } | 395 } |
385 | 396 |
386 template<typename ActionT> | 397 template<typename ActionT> |
387 void DeclarativeActionSet<ActionT>::Revert( | 398 void DeclarativeActionSet<ActionT>::Revert( |
388 const std::string& extension_id, | 399 const std::string& extension_id, |
389 const base::Time& extension_install_time, | 400 const base::Time& extension_install_time, |
390 typename ActionT::ApplyInfo* apply_info) const { | 401 typename ActionT::ApplyInfo* apply_info) const { |
391 for (typename Actions::const_iterator i = actions_.begin(); | 402 for (typename Actions::const_iterator i = actions_.begin(); |
392 i != actions_.end(); ++i) | 403 i != actions_.end(); ++i) |
393 (*i)->Revert(extension_id, extension_install_time, apply_info); | 404 (*i)->Revert(extension_id, extension_install_time, apply_info); |
394 } | 405 } |
395 | 406 |
396 template<typename ActionT> | 407 template<typename ActionT> |
| 408 void DeclarativeActionSet<ActionT>::Reapply( |
| 409 const std::string& extension_id, |
| 410 const base::Time& extension_install_time, |
| 411 typename ActionT::ApplyInfo* apply_info) const { |
| 412 for (typename Actions::const_iterator i = actions_.begin(); |
| 413 i != actions_.end(); ++i) |
| 414 (*i)->Reapply(extension_id, extension_install_time, apply_info); |
| 415 } |
| 416 |
| 417 template<typename ActionT> |
397 int DeclarativeActionSet<ActionT>::GetMinimumPriority() const { | 418 int DeclarativeActionSet<ActionT>::GetMinimumPriority() const { |
398 int minimum_priority = std::numeric_limits<int>::min(); | 419 int minimum_priority = std::numeric_limits<int>::min(); |
399 for (typename Actions::const_iterator i = actions_.begin(); | 420 for (typename Actions::const_iterator i = actions_.begin(); |
400 i != actions_.end(); ++i) { | 421 i != actions_.end(); ++i) { |
401 minimum_priority = std::max(minimum_priority, (*i)->minimum_priority()); | 422 minimum_priority = std::max(minimum_priority, (*i)->minimum_priority()); |
402 } | 423 } |
403 return minimum_priority; | 424 return minimum_priority; |
404 } | 425 } |
405 | 426 |
406 // | 427 // |
(...skipping 16 matching lines...) Expand all Loading... |
423 priority_(priority) { | 444 priority_(priority) { |
424 CHECK(conditions_.get()); | 445 CHECK(conditions_.get()); |
425 CHECK(actions_.get()); | 446 CHECK(actions_.get()); |
426 } | 447 } |
427 | 448 |
428 // static | 449 // static |
429 template<typename ConditionT, typename ActionT> | 450 template<typename ConditionT, typename ActionT> |
430 scoped_ptr<DeclarativeRule<ConditionT, ActionT> > | 451 scoped_ptr<DeclarativeRule<ConditionT, ActionT> > |
431 DeclarativeRule<ConditionT, ActionT>::Create( | 452 DeclarativeRule<ConditionT, ActionT>::Create( |
432 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, | 453 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, |
| 454 Profile* profile, |
433 const Extension* extension, | 455 const Extension* extension, |
434 base::Time extension_installation_time, | 456 base::Time extension_installation_time, |
435 linked_ptr<JsonRule> rule, | 457 linked_ptr<JsonRule> rule, |
436 ConsistencyChecker check_consistency, | 458 ConsistencyChecker check_consistency, |
437 std::string* error) { | 459 std::string* error) { |
438 scoped_ptr<DeclarativeRule> error_result; | 460 scoped_ptr<DeclarativeRule> error_result; |
439 | 461 |
440 scoped_ptr<ConditionSet> conditions = ConditionSet::Create( | 462 scoped_ptr<ConditionSet> conditions = ConditionSet::Create( |
441 extension, url_matcher_condition_factory, rule->conditions, error); | 463 extension, url_matcher_condition_factory, rule->conditions, error); |
442 if (!error->empty()) | 464 if (!error->empty()) |
443 return error_result.Pass(); | 465 return error_result.Pass(); |
444 CHECK(conditions.get()); | 466 CHECK(conditions.get()); |
445 | 467 |
446 bool bad_message = false; | 468 bool bad_message = false; |
447 scoped_ptr<ActionSet> actions = | 469 scoped_ptr<ActionSet> actions = |
448 ActionSet::Create(extension, rule->actions, error, &bad_message); | 470 ActionSet::Create(profile, extension, rule->actions, error, &bad_message); |
449 if (bad_message) { | 471 if (bad_message) { |
450 // TODO(battre) Export concept of bad_message to caller, the extension | 472 // TODO(battre) Export concept of bad_message to caller, the extension |
451 // should be killed in case it is true. | 473 // should be killed in case it is true. |
452 *error = "An action of a rule set had an invalid " | 474 *error = "An action of a rule set had an invalid " |
453 "structure that should have been caught by the JSON validator."; | 475 "structure that should have been caught by the JSON validator."; |
454 return error_result.Pass(); | 476 return error_result.Pass(); |
455 } | 477 } |
456 if (!error->empty() || bad_message) | 478 if (!error->empty() || bad_message) |
457 return error_result.Pass(); | 479 return error_result.Pass(); |
458 CHECK(actions.get()); | 480 CHECK(actions.get()); |
(...skipping 23 matching lines...) Expand all Loading... |
482 } | 504 } |
483 | 505 |
484 template<typename ConditionT, typename ActionT> | 506 template<typename ConditionT, typename ActionT> |
485 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { | 507 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { |
486 return actions_->GetMinimumPriority(); | 508 return actions_->GetMinimumPriority(); |
487 } | 509 } |
488 | 510 |
489 } // namespace extensions | 511 } // namespace extensions |
490 | 512 |
491 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 513 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
OLD | NEW |