| 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 | 14 |
| 15 #include "base/memory/linked_ptr.h" | 15 #include "base/memory/linked_ptr.h" |
| 16 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
| 17 #include "base/time.h" | 17 #include "base/time.h" |
| 18 #include "chrome/common/extensions/api/events.h" | 18 #include "chrome/common/extensions/api/events.h" |
| 19 #include "chrome/common/extensions/matcher/url_matcher.h" | 19 #include "chrome/common/extensions/matcher/url_matcher.h" |
| 20 | 20 |
| 21 namespace base { | 21 namespace base { |
| 22 class Time; | 22 class Time; |
| 23 class Value; |
| 23 } | 24 } |
| 24 | 25 |
| 25 namespace extensions { | 26 namespace extensions { |
| 26 | 27 |
| 27 // This class stores a set of conditions that may be part of a DeclarativeRule. | 28 // This class stores a set of conditions that may be part of a DeclarativeRule. |
| 28 // If any condition is fulfilled, the Actions of the DeclarativeRule can be | 29 // If any condition is fulfilled, the Actions of the DeclarativeRule can be |
| 29 // triggered. | 30 // triggered. |
| 30 // | 31 // |
| 31 // ConditionT should be immutable after creation. It must define the following | 32 // ConditionT should be immutable after creation. It must define the following |
| 32 // members: | 33 // members: |
| (...skipping 13 matching lines...) Expand all Loading... |
| 46 // // argument. | 47 // // argument. |
| 47 // bool has_url_matcher_condition_set(); | 48 // bool has_url_matcher_condition_set(); |
| 48 // // |url_matches| and |match_data| passed through from | 49 // // |url_matches| and |match_data| passed through from |
| 49 // // ConditionSet::IsFulfilled. | 50 // // ConditionSet::IsFulfilled. |
| 50 // bool IsFulfilled( | 51 // bool IsFulfilled( |
| 51 // const std::set<URLMatcherConditionSet::ID>& url_matches, | 52 // const std::set<URLMatcherConditionSet::ID>& url_matches, |
| 52 // const ConditionT::MatchData&); | 53 // const ConditionT::MatchData&); |
| 53 template<typename ConditionT> | 54 template<typename ConditionT> |
| 54 class DeclarativeConditionSet { | 55 class DeclarativeConditionSet { |
| 55 public: | 56 public: |
| 56 typedef std::vector<linked_ptr<json_schema_compiler::any::Any> > AnyVector; | 57 typedef std::vector<linked_ptr<base::Value> > AnyVector; |
| 57 typedef std::vector<linked_ptr<const ConditionT> > Conditions; | 58 typedef std::vector<linked_ptr<const ConditionT> > Conditions; |
| 58 typedef typename Conditions::const_iterator const_iterator; | 59 typedef typename Conditions::const_iterator const_iterator; |
| 59 | 60 |
| 60 // Factory method that creates an WebRequestConditionSet according to the JSON | 61 // Factory method that creates an WebRequestConditionSet according to the JSON |
| 61 // array |conditions| passed by the extension API. | 62 // array |conditions| passed by the extension API. |
| 62 // Sets |error| and returns NULL in case of an error. | 63 // Sets |error| and returns NULL in case of an error. |
| 63 static scoped_ptr<DeclarativeConditionSet> Create( | 64 static scoped_ptr<DeclarativeConditionSet> Create( |
| 64 URLMatcherConditionFactory* url_matcher_condition_factory, | 65 URLMatcherConditionFactory* url_matcher_condition_factory, |
| 65 const AnyVector& conditions, | 66 const AnyVector& conditions, |
| 66 std::string* error); | 67 std::string* error); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 // // Return the minimum priority of rules that can be evaluated after this | 141 // // Return the minimum priority of rules that can be evaluated after this |
| 141 // // action runs. Return MIN_INT by default. | 142 // // action runs. Return MIN_INT by default. |
| 142 // int GetMinimumPriority() const; | 143 // int GetMinimumPriority() const; |
| 143 // | 144 // |
| 144 // TODO(battre): As DeclarativeActionSet can become the single owner of all | 145 // TODO(battre): As DeclarativeActionSet can become the single owner of all |
| 145 // actions, we can optimize here by making some of them singletons (e.g. Cancel | 146 // actions, we can optimize here by making some of them singletons (e.g. Cancel |
| 146 // actions). | 147 // actions). |
| 147 template<typename ActionT> | 148 template<typename ActionT> |
| 148 class DeclarativeActionSet { | 149 class DeclarativeActionSet { |
| 149 public: | 150 public: |
| 150 typedef std::vector<linked_ptr<json_schema_compiler::any::Any> > AnyVector; | 151 typedef std::vector<linked_ptr<base::Value> > AnyVector; |
| 151 typedef std::vector<linked_ptr<const ActionT> > Actions; | 152 typedef std::vector<linked_ptr<const ActionT> > Actions; |
| 152 | 153 |
| 153 explicit DeclarativeActionSet(const Actions& actions); | 154 explicit DeclarativeActionSet(const Actions& actions); |
| 154 | 155 |
| 155 // Factory method that instantiates a DeclarativeActionSet according to | 156 // Factory method that instantiates a DeclarativeActionSet according to |
| 156 // |actions| which represents the array of actions received from the | 157 // |actions| which represents the array of actions received from the |
| 157 // extension API. | 158 // extension API. |
| 158 static scoped_ptr<DeclarativeActionSet> Create(const AnyVector& actions, | 159 static scoped_ptr<DeclarativeActionSet> Create(const AnyVector& actions, |
| 159 std::string* error, | 160 std::string* error, |
| 160 bool* bad_message); | 161 bool* bad_message); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 DeclarativeConditionSet<ConditionT>::Create( | 301 DeclarativeConditionSet<ConditionT>::Create( |
| 301 URLMatcherConditionFactory* url_matcher_condition_factory, | 302 URLMatcherConditionFactory* url_matcher_condition_factory, |
| 302 const AnyVector& conditions, | 303 const AnyVector& conditions, |
| 303 std::string* error) { | 304 std::string* error) { |
| 304 Conditions result; | 305 Conditions result; |
| 305 | 306 |
| 306 for (AnyVector::const_iterator i = conditions.begin(); | 307 for (AnyVector::const_iterator i = conditions.begin(); |
| 307 i != conditions.end(); ++i) { | 308 i != conditions.end(); ++i) { |
| 308 CHECK(i->get()); | 309 CHECK(i->get()); |
| 309 scoped_ptr<ConditionT> condition = | 310 scoped_ptr<ConditionT> condition = |
| 310 ConditionT::Create(url_matcher_condition_factory, | 311 ConditionT::Create(url_matcher_condition_factory, **i, error); |
| 311 (*i)->value(), error); | |
| 312 if (!error->empty()) | 312 if (!error->empty()) |
| 313 return scoped_ptr<DeclarativeConditionSet>(NULL); | 313 return scoped_ptr<DeclarativeConditionSet>(NULL); |
| 314 result.push_back(make_linked_ptr(condition.release())); | 314 result.push_back(make_linked_ptr(condition.release())); |
| 315 } | 315 } |
| 316 | 316 |
| 317 URLMatcherIdToCondition match_id_to_condition; | 317 URLMatcherIdToCondition match_id_to_condition; |
| 318 std::vector<const ConditionT*> conditions_without_urls; | 318 std::vector<const ConditionT*> conditions_without_urls; |
| 319 URLMatcherConditionSet::Vector condition_sets; | 319 URLMatcherConditionSet::Vector condition_sets; |
| 320 | 320 |
| 321 for (typename Conditions::const_iterator i = result.begin(); | 321 for (typename Conditions::const_iterator i = result.begin(); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 const AnyVector& actions, | 360 const AnyVector& actions, |
| 361 std::string* error, | 361 std::string* error, |
| 362 bool* bad_message) { | 362 bool* bad_message) { |
| 363 *error = ""; | 363 *error = ""; |
| 364 *bad_message = false; | 364 *bad_message = false; |
| 365 Actions result; | 365 Actions result; |
| 366 | 366 |
| 367 for (AnyVector::const_iterator i = actions.begin(); | 367 for (AnyVector::const_iterator i = actions.begin(); |
| 368 i != actions.end(); ++i) { | 368 i != actions.end(); ++i) { |
| 369 CHECK(i->get()); | 369 CHECK(i->get()); |
| 370 scoped_ptr<ActionT> action = | 370 scoped_ptr<ActionT> action = ActionT::Create(**i, error, bad_message); |
| 371 ActionT::Create((*i)->value(), error, bad_message); | |
| 372 if (!error->empty() || *bad_message) | 371 if (!error->empty() || *bad_message) |
| 373 return scoped_ptr<DeclarativeActionSet>(NULL); | 372 return scoped_ptr<DeclarativeActionSet>(NULL); |
| 374 result.push_back(make_linked_ptr(action.release())); | 373 result.push_back(make_linked_ptr(action.release())); |
| 375 } | 374 } |
| 376 | 375 |
| 377 return scoped_ptr<DeclarativeActionSet>(new DeclarativeActionSet(result)); | 376 return scoped_ptr<DeclarativeActionSet>(new DeclarativeActionSet(result)); |
| 378 } | 377 } |
| 379 | 378 |
| 380 template<typename ActionT> | 379 template<typename ActionT> |
| 381 void DeclarativeActionSet<ActionT>::Apply( | 380 void DeclarativeActionSet<ActionT>::Apply( |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 } | 482 } |
| 484 | 483 |
| 485 template<typename ConditionT, typename ActionT> | 484 template<typename ConditionT, typename ActionT> |
| 486 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { | 485 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { |
| 487 return actions_->GetMinimumPriority(); | 486 return actions_->GetMinimumPriority(); |
| 488 } | 487 } |
| 489 | 488 |
| 490 } // namespace extensions | 489 } // namespace extensions |
| 491 | 490 |
| 492 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 491 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
| OLD | NEW |