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 namespace content { |
| 33 class BrowserContext; |
| 34 } |
| 35 |
32 namespace extensions { | 36 namespace extensions { |
33 | 37 |
34 // This class stores a set of conditions that may be part of a DeclarativeRule. | 38 // 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 | 39 // If any condition is fulfilled, the Actions of the DeclarativeRule can be |
36 // triggered. | 40 // triggered. |
37 // | 41 // |
38 // ConditionT should be immutable after creation. It must define the following | 42 // ConditionT should be immutable after creation. It must define the following |
39 // members: | 43 // members: |
40 // | 44 // |
41 // // Arguments passed through from DeclarativeConditionSet::Create. | 45 // // Arguments passed through from DeclarativeConditionSet::Create. |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 class DeclarativeActionSet { | 143 class DeclarativeActionSet { |
140 public: | 144 public: |
141 typedef std::vector<linked_ptr<base::Value> > AnyVector; | 145 typedef std::vector<linked_ptr<base::Value> > AnyVector; |
142 typedef std::vector<scoped_refptr<const ActionT> > Actions; | 146 typedef std::vector<scoped_refptr<const ActionT> > Actions; |
143 | 147 |
144 explicit DeclarativeActionSet(const Actions& actions); | 148 explicit DeclarativeActionSet(const Actions& actions); |
145 | 149 |
146 // Factory method that instantiates a DeclarativeActionSet for |extension| | 150 // Factory method that instantiates a DeclarativeActionSet for |extension| |
147 // according to |actions| which represents the array of actions received from | 151 // according to |actions| which represents the array of actions received from |
148 // the extension API. | 152 // the extension API. |
149 static scoped_ptr<DeclarativeActionSet> Create(const Extension* extension, | 153 static scoped_ptr<DeclarativeActionSet> Create( |
150 const AnyVector& actions, | 154 content::BrowserContext* browser_context, |
151 std::string* error, | 155 const Extension* extension, |
152 bool* bad_message); | 156 const AnyVector& actions, |
| 157 std::string* error, |
| 158 bool* bad_message); |
153 | 159 |
154 // Rules call this method when their conditions are fulfilled. | 160 // Rules call this method when their conditions are fulfilled. |
155 void Apply(const std::string& extension_id, | 161 void Apply(const std::string& extension_id, |
156 const base::Time& extension_install_time, | 162 const base::Time& extension_install_time, |
157 typename ActionT::ApplyInfo* apply_info) const; | 163 typename ActionT::ApplyInfo* apply_info) const; |
158 | 164 |
159 // Rules call this method when their conditions are fulfilled, but Apply has | 165 // Rules call this method when their conditions are fulfilled, but Apply has |
160 // already been called. | 166 // already been called. |
161 void Reapply(const std::string& extension_id, | 167 void Reapply(const std::string& extension_id, |
162 const base::Time& extension_install_time, | 168 const base::Time& extension_install_time, |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 // Creates a DeclarativeRule for |extension| given a json definition. The | 222 // 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 | 223 // format of each condition and action's json is up to the specific ConditionT |
218 // and ActionT. |extension| may be NULL in tests. | 224 // and ActionT. |extension| may be NULL in tests. |
219 // | 225 // |
220 // Before constructing the final rule, calls check_consistency(conditions, | 226 // Before constructing the final rule, calls check_consistency(conditions, |
221 // actions, error) and returns NULL if it fails. Pass NULL if no consistency | 227 // 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 | 228 // check is needed. If |error| is empty, the translation was successful and |
223 // the returned rule is internally consistent. | 229 // the returned rule is internally consistent. |
224 static scoped_ptr<DeclarativeRule> Create( | 230 static scoped_ptr<DeclarativeRule> Create( |
225 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, | 231 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, |
| 232 content::BrowserContext* browser_context, |
226 const Extension* extension, | 233 const Extension* extension, |
227 base::Time extension_installation_time, | 234 base::Time extension_installation_time, |
228 linked_ptr<JsonRule> rule, | 235 linked_ptr<JsonRule> rule, |
229 ConsistencyChecker check_consistency, | 236 ConsistencyChecker check_consistency, |
230 std::string* error); | 237 std::string* error); |
231 | 238 |
232 const GlobalRuleId& id() const { return id_; } | 239 const GlobalRuleId& id() const { return id_; } |
233 const Tags& tags() const { return tags_; } | 240 const Tags& tags() const { return tags_; } |
234 const std::string& extension_id() const { return id_.first; } | 241 const std::string& extension_id() const { return id_.first; } |
235 const ConditionSet& conditions() const { return *conditions_; } | 242 const ConditionSet& conditions() const { return *conditions_; } |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 // | 358 // |
352 | 359 |
353 template<typename ActionT> | 360 template<typename ActionT> |
354 DeclarativeActionSet<ActionT>::DeclarativeActionSet(const Actions& actions) | 361 DeclarativeActionSet<ActionT>::DeclarativeActionSet(const Actions& actions) |
355 : actions_(actions) {} | 362 : actions_(actions) {} |
356 | 363 |
357 // static | 364 // static |
358 template<typename ActionT> | 365 template<typename ActionT> |
359 scoped_ptr<DeclarativeActionSet<ActionT> > | 366 scoped_ptr<DeclarativeActionSet<ActionT> > |
360 DeclarativeActionSet<ActionT>::Create( | 367 DeclarativeActionSet<ActionT>::Create( |
| 368 content::BrowserContext* browser_context, |
361 const Extension* extension, | 369 const Extension* extension, |
362 const AnyVector& actions, | 370 const AnyVector& actions, |
363 std::string* error, | 371 std::string* error, |
364 bool* bad_message) { | 372 bool* bad_message) { |
365 *error = ""; | 373 *error = ""; |
366 *bad_message = false; | 374 *bad_message = false; |
367 Actions result; | 375 Actions result; |
368 | 376 |
369 for (AnyVector::const_iterator i = actions.begin(); | 377 for (AnyVector::const_iterator i = actions.begin(); |
370 i != actions.end(); ++i) { | 378 i != actions.end(); ++i) { |
371 CHECK(i->get()); | 379 CHECK(i->get()); |
372 scoped_refptr<const ActionT> action = | 380 scoped_refptr<const ActionT> action = |
373 ActionT::Create(extension, **i, error, bad_message); | 381 ActionT::Create(browser_context, extension, **i, error, bad_message); |
374 if (!error->empty() || *bad_message) | 382 if (!error->empty() || *bad_message) |
375 return scoped_ptr<DeclarativeActionSet>(); | 383 return scoped_ptr<DeclarativeActionSet>(); |
376 result.push_back(action); | 384 result.push_back(action); |
377 } | 385 } |
378 | 386 |
379 return scoped_ptr<DeclarativeActionSet>(new DeclarativeActionSet(result)); | 387 return scoped_ptr<DeclarativeActionSet>(new DeclarativeActionSet(result)); |
380 } | 388 } |
381 | 389 |
382 template<typename ActionT> | 390 template<typename ActionT> |
383 void DeclarativeActionSet<ActionT>::Apply( | 391 void DeclarativeActionSet<ActionT>::Apply( |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 priority_(priority) { | 447 priority_(priority) { |
440 CHECK(conditions_.get()); | 448 CHECK(conditions_.get()); |
441 CHECK(actions_.get()); | 449 CHECK(actions_.get()); |
442 } | 450 } |
443 | 451 |
444 // static | 452 // static |
445 template<typename ConditionT, typename ActionT> | 453 template<typename ConditionT, typename ActionT> |
446 scoped_ptr<DeclarativeRule<ConditionT, ActionT> > | 454 scoped_ptr<DeclarativeRule<ConditionT, ActionT> > |
447 DeclarativeRule<ConditionT, ActionT>::Create( | 455 DeclarativeRule<ConditionT, ActionT>::Create( |
448 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, | 456 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, |
| 457 content::BrowserContext* browser_context, |
449 const Extension* extension, | 458 const Extension* extension, |
450 base::Time extension_installation_time, | 459 base::Time extension_installation_time, |
451 linked_ptr<JsonRule> rule, | 460 linked_ptr<JsonRule> rule, |
452 ConsistencyChecker check_consistency, | 461 ConsistencyChecker check_consistency, |
453 std::string* error) { | 462 std::string* error) { |
454 scoped_ptr<DeclarativeRule> error_result; | 463 scoped_ptr<DeclarativeRule> error_result; |
455 | 464 |
456 scoped_ptr<ConditionSet> conditions = ConditionSet::Create( | 465 scoped_ptr<ConditionSet> conditions = ConditionSet::Create( |
457 extension, url_matcher_condition_factory, rule->conditions, error); | 466 extension, url_matcher_condition_factory, rule->conditions, error); |
458 if (!error->empty()) | 467 if (!error->empty()) |
459 return error_result.Pass(); | 468 return error_result.Pass(); |
460 CHECK(conditions.get()); | 469 CHECK(conditions.get()); |
461 | 470 |
462 bool bad_message = false; | 471 bool bad_message = false; |
463 scoped_ptr<ActionSet> actions = | 472 scoped_ptr<ActionSet> actions = |
464 ActionSet::Create(extension, rule->actions, error, &bad_message); | 473 ActionSet::Create( |
| 474 browser_context, extension, rule->actions, error, &bad_message); |
465 if (bad_message) { | 475 if (bad_message) { |
466 // TODO(battre) Export concept of bad_message to caller, the extension | 476 // TODO(battre) Export concept of bad_message to caller, the extension |
467 // should be killed in case it is true. | 477 // should be killed in case it is true. |
468 *error = "An action of a rule set had an invalid " | 478 *error = "An action of a rule set had an invalid " |
469 "structure that should have been caught by the JSON validator."; | 479 "structure that should have been caught by the JSON validator."; |
470 return error_result.Pass(); | 480 return error_result.Pass(); |
471 } | 481 } |
472 if (!error->empty() || bad_message) | 482 if (!error->empty() || bad_message) |
473 return error_result.Pass(); | 483 return error_result.Pass(); |
474 CHECK(actions.get()); | 484 CHECK(actions.get()); |
(...skipping 23 matching lines...) Expand all Loading... |
498 } | 508 } |
499 | 509 |
500 template<typename ConditionT, typename ActionT> | 510 template<typename ConditionT, typename ActionT> |
501 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { | 511 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { |
502 return actions_->GetMinimumPriority(); | 512 return actions_->GetMinimumPriority(); |
503 } | 513 } |
504 | 514 |
505 } // namespace extensions | 515 } // namespace extensions |
506 | 516 |
507 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 517 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
OLD | NEW |