| 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__ |
| 11 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 11 #define CHROME_BROWSER_EXTENSIONS_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/memory/linked_ptr.h" | 19 #include "base/memory/linked_ptr.h" |
| 19 #include "base/memory/scoped_vector.h" | 20 #include "base/memory/scoped_vector.h" |
| 20 #include "base/stl_util.h" | 21 #include "base/stl_util.h" |
| 21 #include "base/time.h" | 22 #include "base/time.h" |
| 22 #include "chrome/common/extensions/api/events.h" | 23 #include "chrome/common/extensions/api/events.h" |
| 23 #include "extensions/common/matcher/url_matcher.h" | 24 #include "extensions/common/matcher/url_matcher.h" |
| 24 | 25 |
| 25 namespace base { | 26 namespace base { |
| 26 class Time; | 27 class Time; |
| 27 class Value; | 28 class Value; |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 typedef std::string RuleId; | 184 typedef std::string RuleId; |
| 184 typedef std::pair<ExtensionId, RuleId> GlobalRuleId; | 185 typedef std::pair<ExtensionId, RuleId> GlobalRuleId; |
| 185 typedef int Priority; | 186 typedef int Priority; |
| 186 typedef DeclarativeConditionSet<ConditionT> ConditionSet; | 187 typedef DeclarativeConditionSet<ConditionT> ConditionSet; |
| 187 typedef DeclarativeActionSet<ActionT> ActionSet; | 188 typedef DeclarativeActionSet<ActionT> ActionSet; |
| 188 typedef extensions::api::events::Rule JsonRule; | 189 typedef extensions::api::events::Rule JsonRule; |
| 189 typedef std::vector<std::string> Tags; | 190 typedef std::vector<std::string> Tags; |
| 190 | 191 |
| 191 // Checks whether the set of |conditions| and |actions| are consistent. | 192 // Checks whether the set of |conditions| and |actions| are consistent. |
| 192 // Returns true in case of consistency and MUST set |error| otherwise. | 193 // Returns true in case of consistency and MUST set |error| otherwise. |
| 193 typedef bool (*ConsistencyChecker)(const ConditionSet* conditions, | 194 typedef base::Callback<bool(const ConditionSet* conditions, |
| 194 const ActionSet* actions, | 195 const ActionSet* actions, |
| 195 std::string* error); | 196 std::string* error)> ConsistencyChecker; |
| 196 | 197 |
| 197 DeclarativeRule(const GlobalRuleId& id, | 198 DeclarativeRule(const GlobalRuleId& id, |
| 198 const Tags& tags, | 199 const Tags& tags, |
| 199 base::Time extension_installation_time, | 200 base::Time extension_installation_time, |
| 200 scoped_ptr<ConditionSet> conditions, | 201 scoped_ptr<ConditionSet> conditions, |
| 201 scoped_ptr<ActionSet> actions, | 202 scoped_ptr<ActionSet> actions, |
| 202 Priority priority); | 203 Priority priority); |
| 203 | 204 |
| 204 // Creates a DeclarativeRule for an extension given a json definition. The | 205 // Creates a DeclarativeRule for an extension given a json definition. The |
| 205 // format of each condition and action's json is up to the specific ConditionT | 206 // format of each condition and action's json is up to the specific ConditionT |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 // TODO(battre) Export concept of bad_message to caller, the extension | 442 // TODO(battre) Export concept of bad_message to caller, the extension |
| 442 // should be killed in case it is true. | 443 // should be killed in case it is true. |
| 443 *error = "An action of a rule set had an invalid " | 444 *error = "An action of a rule set had an invalid " |
| 444 "structure that should have been caught by the JSON validator."; | 445 "structure that should have been caught by the JSON validator."; |
| 445 return error_result.Pass(); | 446 return error_result.Pass(); |
| 446 } | 447 } |
| 447 if (!error->empty() || bad_message) | 448 if (!error->empty() || bad_message) |
| 448 return error_result.Pass(); | 449 return error_result.Pass(); |
| 449 CHECK(actions.get()); | 450 CHECK(actions.get()); |
| 450 | 451 |
| 451 if (check_consistency && | 452 if (!check_consistency.is_null() && |
| 452 !check_consistency(conditions.get(), actions.get(), error)) { | 453 !check_consistency.Run(conditions.get(), actions.get(), error)) { |
| 453 DCHECK(!error->empty()); | 454 DCHECK(!error->empty()); |
| 454 return error_result.Pass(); | 455 return error_result.Pass(); |
| 455 } | 456 } |
| 456 | 457 |
| 457 CHECK(rule->priority.get()); | 458 CHECK(rule->priority.get()); |
| 458 int priority = *(rule->priority); | 459 int priority = *(rule->priority); |
| 459 | 460 |
| 460 GlobalRuleId rule_id(extension_id, *(rule->id)); | 461 GlobalRuleId rule_id(extension_id, *(rule->id)); |
| 461 Tags tags = rule->tags ? *rule->tags : Tags(); | 462 Tags tags = rule->tags ? *rule->tags : Tags(); |
| 462 return scoped_ptr<DeclarativeRule>( | 463 return scoped_ptr<DeclarativeRule>( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 473 } | 474 } |
| 474 | 475 |
| 475 template<typename ConditionT, typename ActionT> | 476 template<typename ConditionT, typename ActionT> |
| 476 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { | 477 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { |
| 477 return actions_->GetMinimumPriority(); | 478 return actions_->GetMinimumPriority(); |
| 478 } | 479 } |
| 479 | 480 |
| 480 } // namespace extensions | 481 } // namespace extensions |
| 481 | 482 |
| 482 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 483 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
| OLD | NEW |