| 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 EXTENSIONS_BROWSER_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 10 #ifndef EXTENSIONS_BROWSER_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
| 11 #define EXTENSIONS_BROWSER_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 11 #define EXTENSIONS_BROWSER_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
| 12 | 12 |
| 13 #include <limits> | 13 #include <limits> |
| 14 #include <set> | 14 #include <set> |
| 15 #include <string> | 15 #include <string> |
| 16 #include <vector> | 16 #include <vector> |
| 17 | 17 |
| 18 #include "base/callback.h" | 18 #include "base/callback.h" |
| 19 #include "base/memory/linked_ptr.h" | 19 #include "base/memory/linked_ptr.h" |
| 20 #include "base/memory/scoped_vector.h" | 20 #include "base/memory/scoped_vector.h" |
| 21 #include "base/stl_util.h" | 21 #include "base/stl_util.h" |
| 22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 23 #include "components/url_matcher/url_matcher.h" | 23 #include "components/url_matcher/url_matcher.h" |
| 24 #include "extensions/common/api/events.h" | 24 #include "extensions/common/api/events.h" |
| 25 #include "extensions/common/extension.h" | 25 #include "extensions/common/extension.h" |
| 26 #include "extensions/common/host_id.h" |
| 26 | 27 |
| 27 namespace base { | 28 namespace base { |
| 28 class Time; | 29 class Time; |
| 29 class Value; | 30 class Value; |
| 30 } | 31 } |
| 31 | 32 |
| 32 namespace content { | 33 namespace content { |
| 33 class BrowserContext; | 34 class BrowserContext; |
| 34 } | 35 } |
| 35 | 36 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 typedef std::vector<linked_ptr<base::Value> > AnyVector; | 146 typedef std::vector<linked_ptr<base::Value> > AnyVector; |
| 146 typedef std::vector<scoped_refptr<const ActionT> > Actions; | 147 typedef std::vector<scoped_refptr<const ActionT> > Actions; |
| 147 | 148 |
| 148 explicit DeclarativeActionSet(const Actions& actions); | 149 explicit DeclarativeActionSet(const Actions& actions); |
| 149 | 150 |
| 150 // Factory method that instantiates a DeclarativeActionSet for |extension| | 151 // Factory method that instantiates a DeclarativeActionSet for |extension| |
| 151 // according to |actions| which represents the array of actions received from | 152 // according to |actions| which represents the array of actions received from |
| 152 // the extension API. | 153 // the extension API. |
| 153 static scoped_ptr<DeclarativeActionSet> Create( | 154 static scoped_ptr<DeclarativeActionSet> Create( |
| 154 content::BrowserContext* browser_context, | 155 content::BrowserContext* browser_context, |
| 156 const HostID& host_id, |
| 155 const Extension* extension, | 157 const Extension* extension, |
| 156 const AnyVector& actions, | 158 const AnyVector& actions, |
| 157 std::string* error, | 159 std::string* error, |
| 158 bool* bad_message); | 160 bool* bad_message); |
| 159 | 161 |
| 160 // Rules call this method when their conditions are fulfilled. | 162 // Rules call this method when their conditions are fulfilled. |
| 161 void Apply(const std::string& extension_id, | 163 void Apply(const std::string& extension_id, |
| 162 const base::Time& extension_install_time, | 164 const base::Time& extension_install_time, |
| 163 typename ActionT::ApplyInfo* apply_info) const; | 165 typename ActionT::ApplyInfo* apply_info) const; |
| 164 | 166 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 // format of each condition and action's json is up to the specific ConditionT | 225 // format of each condition and action's json is up to the specific ConditionT |
| 224 // and ActionT. |extension| may be NULL in tests. | 226 // and ActionT. |extension| may be NULL in tests. |
| 225 // | 227 // |
| 226 // Before constructing the final rule, calls check_consistency(conditions, | 228 // Before constructing the final rule, calls check_consistency(conditions, |
| 227 // actions, error) and returns NULL if it fails. Pass NULL if no consistency | 229 // actions, error) and returns NULL if it fails. Pass NULL if no consistency |
| 228 // check is needed. If |error| is empty, the translation was successful and | 230 // check is needed. If |error| is empty, the translation was successful and |
| 229 // the returned rule is internally consistent. | 231 // the returned rule is internally consistent. |
| 230 static scoped_ptr<DeclarativeRule> Create( | 232 static scoped_ptr<DeclarativeRule> Create( |
| 231 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, | 233 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, |
| 232 content::BrowserContext* browser_context, | 234 content::BrowserContext* browser_context, |
| 235 const HostID& host_id, |
| 233 const Extension* extension, | 236 const Extension* extension, |
| 234 base::Time extension_installation_time, | 237 base::Time extension_installation_time, |
| 235 linked_ptr<JsonRule> rule, | 238 linked_ptr<JsonRule> rule, |
| 236 ConsistencyChecker check_consistency, | 239 ConsistencyChecker check_consistency, |
| 237 std::string* error); | 240 std::string* error); |
| 238 | 241 |
| 239 const GlobalRuleId& id() const { return id_; } | 242 const GlobalRuleId& id() const { return id_; } |
| 240 const Tags& tags() const { return tags_; } | 243 const Tags& tags() const { return tags_; } |
| 241 const std::string& extension_id() const { return id_.first; } | 244 const std::string& extension_id() const { return id_.first; } |
| 242 const ConditionSet& conditions() const { return *conditions_; } | 245 const ConditionSet& conditions() const { return *conditions_; } |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 | 362 |
| 360 template<typename ActionT> | 363 template<typename ActionT> |
| 361 DeclarativeActionSet<ActionT>::DeclarativeActionSet(const Actions& actions) | 364 DeclarativeActionSet<ActionT>::DeclarativeActionSet(const Actions& actions) |
| 362 : actions_(actions) {} | 365 : actions_(actions) {} |
| 363 | 366 |
| 364 // static | 367 // static |
| 365 template<typename ActionT> | 368 template<typename ActionT> |
| 366 scoped_ptr<DeclarativeActionSet<ActionT> > | 369 scoped_ptr<DeclarativeActionSet<ActionT> > |
| 367 DeclarativeActionSet<ActionT>::Create( | 370 DeclarativeActionSet<ActionT>::Create( |
| 368 content::BrowserContext* browser_context, | 371 content::BrowserContext* browser_context, |
| 372 const HostID& host_id, |
| 369 const Extension* extension, | 373 const Extension* extension, |
| 370 const AnyVector& actions, | 374 const AnyVector& actions, |
| 371 std::string* error, | 375 std::string* error, |
| 372 bool* bad_message) { | 376 bool* bad_message) { |
| 373 *error = ""; | 377 *error = ""; |
| 374 *bad_message = false; | 378 *bad_message = false; |
| 375 Actions result; | 379 Actions result; |
| 376 | 380 |
| 377 for (AnyVector::const_iterator i = actions.begin(); | 381 for (AnyVector::const_iterator i = actions.begin(); |
| 378 i != actions.end(); ++i) { | 382 i != actions.end(); ++i) { |
| 379 CHECK(i->get()); | 383 CHECK(i->get()); |
| 380 scoped_refptr<const ActionT> action = | 384 scoped_refptr<const ActionT> action = |
| 381 ActionT::Create(browser_context, extension, **i, error, bad_message); | 385 ActionT::Create( |
| 386 browser_context, host_id, extension, **i, error, bad_message); |
| 382 if (!error->empty() || *bad_message) | 387 if (!error->empty() || *bad_message) |
| 383 return scoped_ptr<DeclarativeActionSet>(); | 388 return scoped_ptr<DeclarativeActionSet>(); |
| 384 result.push_back(action); | 389 result.push_back(action); |
| 385 } | 390 } |
| 386 | 391 |
| 387 return scoped_ptr<DeclarativeActionSet>(new DeclarativeActionSet(result)); | 392 return scoped_ptr<DeclarativeActionSet>(new DeclarativeActionSet(result)); |
| 388 } | 393 } |
| 389 | 394 |
| 390 template<typename ActionT> | 395 template<typename ActionT> |
| 391 void DeclarativeActionSet<ActionT>::Apply( | 396 void DeclarativeActionSet<ActionT>::Apply( |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 CHECK(conditions_.get()); | 453 CHECK(conditions_.get()); |
| 449 CHECK(actions_.get()); | 454 CHECK(actions_.get()); |
| 450 } | 455 } |
| 451 | 456 |
| 452 // static | 457 // static |
| 453 template<typename ConditionT, typename ActionT> | 458 template<typename ConditionT, typename ActionT> |
| 454 scoped_ptr<DeclarativeRule<ConditionT, ActionT> > | 459 scoped_ptr<DeclarativeRule<ConditionT, ActionT> > |
| 455 DeclarativeRule<ConditionT, ActionT>::Create( | 460 DeclarativeRule<ConditionT, ActionT>::Create( |
| 456 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, | 461 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, |
| 457 content::BrowserContext* browser_context, | 462 content::BrowserContext* browser_context, |
| 463 const HostID& host_id, |
| 458 const Extension* extension, | 464 const Extension* extension, |
| 459 base::Time extension_installation_time, | 465 base::Time extension_installation_time, |
| 460 linked_ptr<JsonRule> rule, | 466 linked_ptr<JsonRule> rule, |
| 461 ConsistencyChecker check_consistency, | 467 ConsistencyChecker check_consistency, |
| 462 std::string* error) { | 468 std::string* error) { |
| 463 scoped_ptr<DeclarativeRule> error_result; | 469 scoped_ptr<DeclarativeRule> error_result; |
| 464 | 470 |
| 465 scoped_ptr<ConditionSet> conditions = ConditionSet::Create( | 471 scoped_ptr<ConditionSet> conditions = ConditionSet::Create( |
| 466 extension, url_matcher_condition_factory, rule->conditions, error); | 472 extension, url_matcher_condition_factory, rule->conditions, error); |
| 467 if (!error->empty()) | 473 if (!error->empty()) |
| 468 return error_result.Pass(); | 474 return error_result.Pass(); |
| 469 CHECK(conditions.get()); | 475 CHECK(conditions.get()); |
| 470 | 476 |
| 471 bool bad_message = false; | 477 bool bad_message = false; |
| 472 scoped_ptr<ActionSet> actions = | 478 scoped_ptr<ActionSet> actions = |
| 473 ActionSet::Create( | 479 ActionSet::Create(browser_context, host_id, extension, |
| 474 browser_context, extension, rule->actions, error, &bad_message); | 480 rule->actions, error, &bad_message); |
| 475 if (bad_message) { | 481 if (bad_message) { |
| 476 // TODO(battre) Export concept of bad_message to caller, the extension | 482 // TODO(battre) Export concept of bad_message to caller, the extension |
| 477 // should be killed in case it is true. | 483 // should be killed in case it is true. |
| 478 *error = "An action of a rule set had an invalid " | 484 *error = "An action of a rule set had an invalid " |
| 479 "structure that should have been caught by the JSON validator."; | 485 "structure that should have been caught by the JSON validator."; |
| 480 return error_result.Pass(); | 486 return error_result.Pass(); |
| 481 } | 487 } |
| 482 if (!error->empty() || bad_message) | 488 if (!error->empty() || bad_message) |
| 483 return error_result.Pass(); | 489 return error_result.Pass(); |
| 484 CHECK(actions.get()); | 490 CHECK(actions.get()); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 508 } | 514 } |
| 509 | 515 |
| 510 template<typename ConditionT, typename ActionT> | 516 template<typename ConditionT, typename ActionT> |
| 511 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { | 517 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { |
| 512 return actions_->GetMinimumPriority(); | 518 return actions_->GetMinimumPriority(); |
| 513 } | 519 } |
| 514 | 520 |
| 515 } // namespace extensions | 521 } // namespace extensions |
| 516 | 522 |
| 517 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 523 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
| OLD | NEW |