| 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> |
| 15 #include <string> |
| 16 #include <vector> |
| 14 | 17 |
| 15 #include "base/memory/linked_ptr.h" | 18 #include "base/memory/linked_ptr.h" |
| 16 #include "base/memory/scoped_vector.h" | 19 #include "base/memory/scoped_vector.h" |
| 20 #include "base/stl_util.h" |
| 17 #include "base/time.h" | 21 #include "base/time.h" |
| 18 #include "chrome/common/extensions/api/events.h" | 22 #include "chrome/common/extensions/api/events.h" |
| 19 #include "chrome/common/extensions/matcher/url_matcher.h" | 23 #include "chrome/common/extensions/matcher/url_matcher.h" |
| 20 | 24 |
| 21 namespace base { | 25 namespace base { |
| 22 class Time; | 26 class Time; |
| 23 class Value; | 27 class Value; |
| 24 } | 28 } |
| 25 | 29 |
| 26 namespace extensions { | 30 namespace extensions { |
| 27 | 31 |
| 28 // This class stores a set of conditions that may be part of a DeclarativeRule. | 32 // This class stores a set of conditions that may be part of a DeclarativeRule. |
| 29 // If any condition is fulfilled, the Actions of the DeclarativeRule can be | 33 // If any condition is fulfilled, the Actions of the DeclarativeRule can be |
| 30 // triggered. | 34 // triggered. |
| 31 // | 35 // |
| 32 // ConditionT should be immutable after creation. It must define the following | 36 // ConditionT should be immutable after creation. It must define the following |
| 33 // members: | 37 // members: |
| 34 // | 38 // |
| 35 // // Arguments passed through from ConditionSet::Create. | 39 // // Arguments passed through from DeclarativeConditionSet::Create. |
| 36 // static scoped_ptr<ConditionT> Create( | 40 // static scoped_ptr<ConditionT> Create( |
| 37 // URLMatcherConditionFactory*, | 41 // URLMatcherConditionFactory* url_matcher_condition_factory, |
| 38 // // Except this argument gets elements of the AnyVector. | 42 // // Except this argument gets elements of the AnyVector. |
| 39 // const base::Value& definition, | 43 // const base::Value& definition, |
| 40 // std::string* error); | 44 // std::string* error); |
| 41 // // If the Condition needs to be filtered by some | 45 // // If the Condition needs to be filtered by some URLMatcherConditionSets, |
| 42 // // URLMatcherConditionSets, append them to this argument. | 46 // // append them to |condition_sets|. |
| 43 // // DeclarativeConditionSet::GetURLMatcherConditionSets forwards here. | 47 // // DeclarativeConditionSet::GetURLMatcherConditionSets forwards here. |
| 44 // void GetURLMatcherConditionSets( | 48 // void GetURLMatcherConditionSets( |
| 45 // URLMatcherConditionSet::Vector* condition_sets) | 49 // URLMatcherConditionSet::Vector* condition_sets); |
| 46 // // True if GetURLMatcherConditionSets would append anything to its | 50 // // |match_data| passed through from DeclarativeConditionSet::IsFulfilled. |
| 47 // // argument. | 51 // bool IsFulfilled(const ConditionT::MatchData& match_data); |
| 48 // bool has_url_matcher_condition_set(); | |
| 49 // // |url_matches| and |match_data| passed through from | |
| 50 // // ConditionSet::IsFulfilled. | |
| 51 // bool IsFulfilled( | |
| 52 // const std::set<URLMatcherConditionSet::ID>& url_matches, | |
| 53 // const ConditionT::MatchData&); | |
| 54 template<typename ConditionT> | 52 template<typename ConditionT> |
| 55 class DeclarativeConditionSet { | 53 class DeclarativeConditionSet { |
| 56 public: | 54 public: |
| 57 typedef std::vector<linked_ptr<base::Value> > AnyVector; | 55 typedef std::vector<linked_ptr<base::Value> > AnyVector; |
| 58 typedef std::vector<linked_ptr<const ConditionT> > Conditions; | 56 typedef std::vector<linked_ptr<const ConditionT> > Conditions; |
| 59 typedef typename Conditions::const_iterator const_iterator; | 57 typedef typename Conditions::const_iterator const_iterator; |
| 60 | 58 |
| 61 // Factory method that creates an WebRequestConditionSet according to the JSON | 59 // Factory method that creates a DeclarativeConditionSet according to the JSON |
| 62 // array |conditions| passed by the extension API. | 60 // array |conditions| passed by the extension API. Sets |error| and returns |
| 63 // Sets |error| and returns NULL in case of an error. | 61 // NULL in case of an error. |
| 64 static scoped_ptr<DeclarativeConditionSet> Create( | 62 static scoped_ptr<DeclarativeConditionSet> Create( |
| 65 URLMatcherConditionFactory* url_matcher_condition_factory, | 63 URLMatcherConditionFactory* url_matcher_condition_factory, |
| 66 const AnyVector& conditions, | 64 const AnyVector& conditions, |
| 67 std::string* error); | 65 std::string* error); |
| 68 | 66 |
| 69 const Conditions& conditions() const { | 67 const Conditions& conditions() const { |
| 70 return conditions_; | 68 return conditions_; |
| 71 } | 69 } |
| 72 | 70 |
| 73 const_iterator begin() const { return conditions_.begin(); } | 71 const_iterator begin() const { return conditions_.begin(); } |
| 74 const_iterator end() const { return conditions_.end(); } | 72 const_iterator end() const { return conditions_.end(); } |
| 75 | 73 |
| 76 // If |url_match_trigger| is a member of |url_matches|, then this | 74 // If |url_match_trigger| is not -1, this function looks for a condition |
| 77 // returns whether the corresponding condition is fulfilled | 75 // with this URLMatcherConditionSet, and forwards to that condition's |
| 78 // wrt. |request_data|. If |url_match_trigger| is -1, this function | 76 // IsFulfilled(|match_data|). If there is no such condition, then false is |
| 79 // returns whether any of the conditions without URL attributes is | 77 // returned. If |url_match_trigger| is -1, this function returns whether any |
| 80 // satisfied. | 78 // of the conditions without URL attributes is satisfied. |
| 81 // | |
| 82 // Conditions for which has_url_matcher_condition_set() is false are always | |
| 83 // checked (aside from short-circuiting when an earlier condition already | |
| 84 // matched.) | |
| 85 // | |
| 86 // Conditions for which has_url_matcher_condition_set() is true are only | |
| 87 // checked if one of the URLMatcherConditionSets returned by | |
| 88 // GetURLMatcherConditionSets() has an id listed in url_matches. That means | |
| 89 // that if |match_data| contains URL matches for two pieces of a request, | |
| 90 // their union should appear in |url_matches|. For kinds of MatchData that | |
| 91 // only have one type of URL, |url_matches| is forwarded on to | |
| 92 // ConditionT::IsFulfilled(), so it doesn't need to appear in |match_data|. | |
| 93 bool IsFulfilled(URLMatcherConditionSet::ID url_match_trigger, | 79 bool IsFulfilled(URLMatcherConditionSet::ID url_match_trigger, |
| 94 const std::set<URLMatcherConditionSet::ID>& url_matches, | |
| 95 const typename ConditionT::MatchData& match_data) const; | 80 const typename ConditionT::MatchData& match_data) const; |
| 96 | 81 |
| 97 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|. | 82 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|. |
| 98 void GetURLMatcherConditionSets( | 83 void GetURLMatcherConditionSets( |
| 99 URLMatcherConditionSet::Vector* condition_sets) const; | 84 URLMatcherConditionSet::Vector* condition_sets) const; |
| 100 | 85 |
| 101 // Returns whether there are some conditions without UrlFilter attributes. | 86 // Returns whether there are some conditions without UrlFilter attributes. |
| 102 bool HasConditionsWithoutUrls() const { | 87 bool HasConditionsWithoutUrls() const { |
| 103 return !conditions_without_urls_.empty(); | 88 return !conditions_without_urls_.empty(); |
| 104 } | 89 } |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 | 244 |
| 260 // Implementation details below here. | 245 // Implementation details below here. |
| 261 | 246 |
| 262 // | 247 // |
| 263 // DeclarativeConditionSet | 248 // DeclarativeConditionSet |
| 264 // | 249 // |
| 265 | 250 |
| 266 template<typename ConditionT> | 251 template<typename ConditionT> |
| 267 bool DeclarativeConditionSet<ConditionT>::IsFulfilled( | 252 bool DeclarativeConditionSet<ConditionT>::IsFulfilled( |
| 268 URLMatcherConditionSet::ID url_match_trigger, | 253 URLMatcherConditionSet::ID url_match_trigger, |
| 269 const std::set<URLMatcherConditionSet::ID>& url_matches, | |
| 270 const typename ConditionT::MatchData& match_data) const { | 254 const typename ConditionT::MatchData& match_data) const { |
| 271 if (url_match_trigger == -1) { | 255 if (url_match_trigger == -1) { |
| 272 // Invalid trigger -- indication that we should only check conditions | 256 // Invalid trigger -- indication that we should only check conditions |
| 273 // without URL attributes. | 257 // without URL attributes. |
| 274 for (typename std::vector<const ConditionT*>::const_iterator it = | 258 for (typename std::vector<const ConditionT*>::const_iterator it = |
| 275 conditions_without_urls_.begin(); | 259 conditions_without_urls_.begin(); |
| 276 it != conditions_without_urls_.end(); ++it) { | 260 it != conditions_without_urls_.end(); ++it) { |
| 277 if ((*it)->IsFulfilled(url_matches, match_data)) | 261 if ((*it)->IsFulfilled(match_data)) |
| 278 return true; | 262 return true; |
| 279 } | 263 } |
| 280 return false; | 264 return false; |
| 281 } | 265 } |
| 282 | 266 |
| 283 typename URLMatcherIdToCondition::const_iterator triggered = | 267 typename URLMatcherIdToCondition::const_iterator triggered = |
| 284 match_id_to_condition_.find(url_match_trigger); | 268 match_id_to_condition_.find(url_match_trigger); |
| 285 return (triggered != match_id_to_condition_.end() && | 269 return (triggered != match_id_to_condition_.end() && |
| 286 triggered->second->IsFulfilled(url_matches, match_data)); | 270 triggered->second->IsFulfilled(match_data)); |
| 287 } | 271 } |
| 288 | 272 |
| 289 template<typename ConditionT> | 273 template<typename ConditionT> |
| 290 void DeclarativeConditionSet<ConditionT>::GetURLMatcherConditionSets( | 274 void DeclarativeConditionSet<ConditionT>::GetURLMatcherConditionSets( |
| 291 URLMatcherConditionSet::Vector* condition_sets) const { | 275 URLMatcherConditionSet::Vector* condition_sets) const { |
| 292 for (typename Conditions::const_iterator i = conditions_.begin(); | 276 for (typename Conditions::const_iterator i = conditions_.begin(); |
| 293 i != conditions_.end(); ++i) { | 277 i != conditions_.end(); ++i) { |
| 294 (*i)->GetURLMatcherConditionSets(condition_sets); | 278 (*i)->GetURLMatcherConditionSets(condition_sets); |
| 295 } | 279 } |
| 296 } | 280 } |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 } | 466 } |
| 483 | 467 |
| 484 template<typename ConditionT, typename ActionT> | 468 template<typename ConditionT, typename ActionT> |
| 485 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { | 469 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { |
| 486 return actions_->GetMinimumPriority(); | 470 return actions_->GetMinimumPriority(); |
| 487 } | 471 } |
| 488 | 472 |
| 489 } // namespace extensions | 473 } // namespace extensions |
| 490 | 474 |
| 491 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 475 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
| OLD | NEW |