Chromium Code Reviews| 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 <vector> | |
| 14 | 15 |
| 15 #include "base/memory/linked_ptr.h" | 16 #include "base/memory/linked_ptr.h" |
| 16 #include "base/memory/scoped_vector.h" | 17 #include "base/memory/scoped_vector.h" |
| 17 #include "base/time.h" | 18 #include "base/time.h" |
| 18 #include "chrome/common/extensions/api/events.h" | 19 #include "chrome/common/extensions/api/events.h" |
| 19 #include "chrome/common/extensions/matcher/url_matcher.h" | 20 #include "chrome/common/extensions/matcher/url_matcher.h" |
| 20 | 21 |
| 21 namespace base { | 22 namespace base { |
| 22 class Time; | 23 class Time; |
| 23 class Value; | 24 class Value; |
| 24 } | 25 } |
| 25 | 26 |
| 26 namespace extensions { | 27 namespace extensions { |
| 27 | 28 |
| 28 // This class stores a set of conditions that may be part of a DeclarativeRule. | 29 // 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 | 30 // If any condition is fulfilled, the Actions of the DeclarativeRule can be |
| 30 // triggered. | 31 // triggered. |
| 31 // | 32 // |
| 32 // ConditionT should be immutable after creation. It must define the following | 33 // ConditionT should be immutable after creation. It must define the following |
| 33 // members: | 34 // members: |
| 34 // | 35 // |
| 35 // // Arguments passed through from ConditionSet::Create. | 36 // // Arguments passed through from DeclarativeConditionSet::Create. |
| 36 // static scoped_ptr<ConditionT> Create( | 37 // static scoped_ptr<ConditionT> Create( |
| 37 // URLMatcherConditionFactory*, | 38 // const std::vector<URLMatcherConditionFactory*>& |
| 39 // url_matcher_condition_factories, | |
| 38 // // Except this argument gets elements of the AnyVector. | 40 // // Except this argument gets elements of the AnyVector. |
| 39 // const base::Value& definition, | 41 // const base::Value& definition, |
| 40 // std::string* error); | 42 // std::string* error); |
| 41 // // If the Condition needs to be filtered by some | 43 // // If the Condition needs to be filtered by some URLMatcherConditionSets, |
| 42 // // URLMatcherConditionSets, append them to this argument. | 44 // // append them to |condition_sets|. Use |index| to select the type of a |
| 45 // // URL attribute, multiple can be present. Valid values of |index| start | |
| 46 // // at 0 and increase by 1. The return value always gives the next higher | |
|
Jeffrey Yasskin
2013/01/22 08:21:06
This seems too complicated, and you didn't manage
| |
| 47 // // valid index, or -1 if there is none. It is thus possible to iterate over | |
| 48 // // all valid indices by starting with 0 and using the return values in | |
| 49 // // subsequent calls until -1 is returned. | |
| 43 // // DeclarativeConditionSet::GetURLMatcherConditionSets forwards here. | 50 // // DeclarativeConditionSet::GetURLMatcherConditionSets forwards here. |
| 44 // void GetURLMatcherConditionSets( | 51 // int GetURLMatcherConditionSets( |
| 45 // URLMatcherConditionSet::Vector* condition_sets) | 52 // URLMatcherConditionSet::Vector* condition_sets, |
| 46 // // True if GetURLMatcherConditionSets would append anything to its | 53 // int index); |
| 47 // // argument. | |
| 48 // bool has_url_matcher_condition_set(); | |
| 49 // // |url_matches| and |match_data| passed through from | 54 // // |url_matches| and |match_data| passed through from |
| 50 // // ConditionSet::IsFulfilled. | 55 // // DeclarativeConditionSet::IsFulfilled. |
| 51 // bool IsFulfilled( | 56 // bool IsFulfilled( |
| 52 // const std::set<URLMatcherConditionSet::ID>& url_matches, | 57 // const std::set<URLMatcherConditionSet::ID>& url_matches, |
| 53 // const ConditionT::MatchData&); | 58 // const ConditionT::MatchData&); |
| 54 template<typename ConditionT> | 59 template<typename ConditionT> |
| 55 class DeclarativeConditionSet { | 60 class DeclarativeConditionSet { |
| 56 public: | 61 public: |
| 57 typedef std::vector<linked_ptr<base::Value> > AnyVector; | 62 typedef std::vector<linked_ptr<base::Value> > AnyVector; |
| 58 typedef std::vector<linked_ptr<const ConditionT> > Conditions; | 63 typedef std::vector<linked_ptr<const ConditionT> > Conditions; |
| 59 typedef typename Conditions::const_iterator const_iterator; | 64 typedef typename Conditions::const_iterator const_iterator; |
| 60 | 65 |
| 61 // Factory method that creates an WebRequestConditionSet according to the JSON | 66 // Factory method that creates a WebRequestConditionSet according to the JSON |
| 62 // array |conditions| passed by the extension API. | 67 // array |conditions| passed by the extension API. Because IsFulfilled gets |
| 68 // passed the union of IDs of all matching URLMatcherConditionSets coming | |
| 69 // from all URLMatcherConditionFactories, you should make sure that the | |
| 70 // factories use strictly disjoined URLMatcherConditionSets::IDs. This allows | |
|
Jeffrey Yasskin
2013/01/22 08:21:06
How? Using a single URLMatcher would guarantee thi
| |
| 71 // one to tell which attribute had a match in IsFulfilled. | |
| 63 // Sets |error| and returns NULL in case of an error. | 72 // Sets |error| and returns NULL in case of an error. |
| 64 static scoped_ptr<DeclarativeConditionSet> Create( | 73 static scoped_ptr<DeclarativeConditionSet> Create( |
| 65 URLMatcherConditionFactory* url_matcher_condition_factory, | 74 const std::vector<URLMatcherConditionFactory*>& |
| 75 url_matcher_condition_factories, | |
| 66 const AnyVector& conditions, | 76 const AnyVector& conditions, |
| 67 std::string* error); | 77 std::string* error); |
| 68 | 78 |
| 69 const Conditions& conditions() const { | 79 const Conditions& conditions() const { |
| 70 return conditions_; | 80 return conditions_; |
| 71 } | 81 } |
| 72 | 82 |
| 73 const_iterator begin() const { return conditions_.begin(); } | 83 const_iterator begin() const { return conditions_.begin(); } |
| 74 const_iterator end() const { return conditions_.end(); } | 84 const_iterator end() const { return conditions_.end(); } |
| 75 | 85 |
| 76 // If |url_match_trigger| is a member of |url_matches|, then this | 86 // If |url_match_trigger| is a member of |url_matches|, then this returns |
| 77 // returns whether the corresponding condition is fulfilled | 87 // whether the corresponding condition is fulfilled wrt. |request_data|. |
|
Jeffrey Yasskin
2013/01/22 08:21:06
s/request_data/match_data/
vabr (Chromium)
2013/01/22 14:54:30
Done.
| |
| 78 // wrt. |request_data|. If |url_match_trigger| is -1, this function | 88 // Therefore, if |match_data| contains URL matches from more matchers, the |
|
Jeffrey Yasskin
2013/01/22 08:21:06
"more matchers" than what?
vabr (Chromium)
2013/01/22 14:54:30
I meant "multiple", i.e., more than 1. Changed.
E
| |
| 79 // returns whether any of the conditions without URL attributes is | 89 // union of the matches should appear in |url_matches|. For kinds of MatchData |
| 80 // satisfied. | 90 // that only have one type of URL, |url_matches| is forwarded on to |
| 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|. | 91 // ConditionT::IsFulfilled(), so it doesn't need to appear in |match_data|. |
| 92 // If |url_match_trigger| is -1, this function returns whether any of the | |
| 93 // conditions without URL attributes is satisfied. | |
| 93 bool IsFulfilled(URLMatcherConditionSet::ID url_match_trigger, | 94 bool IsFulfilled(URLMatcherConditionSet::ID url_match_trigger, |
| 94 const std::set<URLMatcherConditionSet::ID>& url_matches, | 95 const std::set<URLMatcherConditionSet::ID>& url_matches, |
| 95 const typename ConditionT::MatchData& match_data) const; | 96 const typename ConditionT::MatchData& match_data) const; |
| 96 | 97 |
| 97 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|. | 98 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|. |
| 98 void GetURLMatcherConditionSets( | 99 // Returns |index| + 1 if at least one condition's GetURLMatcherConditionSets |
| 99 URLMatcherConditionSet::Vector* condition_sets) const; | 100 // returns |index| + 1, otherwise returns -1. |
| 101 int GetURLMatcherConditionSets( | |
|
Jeffrey Yasskin
2013/01/22 08:21:06
It looks like every use of this function just iter
| |
| 102 URLMatcherConditionSet::Vector* condition_sets, | |
| 103 int index) const; | |
| 100 | 104 |
| 101 // Returns whether there are some conditions without UrlFilter attributes. | 105 // Returns whether there are some conditions without UrlFilter attributes. |
| 102 bool HasConditionsWithoutUrls() const { | 106 bool HasConditionsWithoutUrls() const { |
| 103 return !conditions_without_urls_.empty(); | 107 return !conditions_without_urls_.empty(); |
| 104 } | 108 } |
| 105 | 109 |
| 106 private: | 110 private: |
| 107 typedef std::map<URLMatcherConditionSet::ID, const ConditionT*> | 111 typedef std::map<URLMatcherConditionSet::ID, const ConditionT*> |
| 108 URLMatcherIdToCondition; | 112 URLMatcherIdToCondition; |
| 109 | 113 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 | 220 |
| 217 // Creates a DeclarativeRule for an extension given a json definition. The | 221 // Creates a DeclarativeRule for an extension given a json definition. The |
| 218 // format of each condition and action's json is up to the specific ConditionT | 222 // format of each condition and action's json is up to the specific ConditionT |
| 219 // and ActionT. | 223 // and ActionT. |
| 220 // | 224 // |
| 221 // Before constructing the final rule, calls check_consistency(conditions, | 225 // Before constructing the final rule, calls check_consistency(conditions, |
| 222 // actions, error) and returns NULL if it fails. Pass NULL if no consistency | 226 // actions, error) and returns NULL if it fails. Pass NULL if no consistency |
| 223 // check is needed. If |error| is empty, the translation was successful and | 227 // check is needed. If |error| is empty, the translation was successful and |
| 224 // the returned rule is internally consistent. | 228 // the returned rule is internally consistent. |
| 225 static scoped_ptr<DeclarativeRule> Create( | 229 static scoped_ptr<DeclarativeRule> Create( |
| 226 URLMatcherConditionFactory* url_matcher_condition_factory, | 230 const std::vector<URLMatcherConditionFactory*>& |
| 231 url_matcher_condition_factories, | |
| 227 const std::string& extension_id, | 232 const std::string& extension_id, |
| 228 base::Time extension_installation_time, | 233 base::Time extension_installation_time, |
| 229 linked_ptr<JsonRule> rule, | 234 linked_ptr<JsonRule> rule, |
| 230 ConsistencyChecker check_consistency, | 235 ConsistencyChecker check_consistency, |
| 231 std::string* error); | 236 std::string* error); |
| 232 | 237 |
| 233 const GlobalRuleId& id() const { return id_; } | 238 const GlobalRuleId& id() const { return id_; } |
| 234 const std::string& extension_id() const { return id_.first; } | 239 const std::string& extension_id() const { return id_.first; } |
| 235 const ConditionSet& conditions() const { return *conditions_; } | 240 const ConditionSet& conditions() const { return *conditions_; } |
| 236 const ActionSet& actions() const { return *actions_; } | 241 const ActionSet& actions() const { return *actions_; } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 return false; | 285 return false; |
| 281 } | 286 } |
| 282 | 287 |
| 283 typename URLMatcherIdToCondition::const_iterator triggered = | 288 typename URLMatcherIdToCondition::const_iterator triggered = |
| 284 match_id_to_condition_.find(url_match_trigger); | 289 match_id_to_condition_.find(url_match_trigger); |
| 285 return (triggered != match_id_to_condition_.end() && | 290 return (triggered != match_id_to_condition_.end() && |
| 286 triggered->second->IsFulfilled(url_matches, match_data)); | 291 triggered->second->IsFulfilled(url_matches, match_data)); |
| 287 } | 292 } |
| 288 | 293 |
| 289 template<typename ConditionT> | 294 template<typename ConditionT> |
| 290 void DeclarativeConditionSet<ConditionT>::GetURLMatcherConditionSets( | 295 int DeclarativeConditionSet<ConditionT>::GetURLMatcherConditionSets( |
| 291 URLMatcherConditionSet::Vector* condition_sets) const { | 296 URLMatcherConditionSet::Vector* condition_sets, |
| 297 int index) const { | |
| 298 bool more_condition_sets_exist = false; | |
| 292 for (typename Conditions::const_iterator i = conditions_.begin(); | 299 for (typename Conditions::const_iterator i = conditions_.begin(); |
| 293 i != conditions_.end(); ++i) { | 300 i != conditions_.end(); ++i) { |
| 294 (*i)->GetURLMatcherConditionSets(condition_sets); | 301 if ((*i)->GetURLMatcherConditionSets(condition_sets, index) >= 0) |
| 302 more_condition_sets_exist = true; | |
| 295 } | 303 } |
| 304 return (more_condition_sets_exist ? (index + 1) : -1); | |
| 296 } | 305 } |
| 297 | 306 |
| 298 // static | 307 // static |
| 299 template<typename ConditionT> | 308 template<typename ConditionT> |
| 300 scoped_ptr<DeclarativeConditionSet<ConditionT> > | 309 scoped_ptr<DeclarativeConditionSet<ConditionT> > |
| 301 DeclarativeConditionSet<ConditionT>::Create( | 310 DeclarativeConditionSet<ConditionT>::Create( |
| 302 URLMatcherConditionFactory* url_matcher_condition_factory, | 311 const std::vector<URLMatcherConditionFactory*>& |
| 312 url_matcher_condition_factories, | |
| 303 const AnyVector& conditions, | 313 const AnyVector& conditions, |
| 304 std::string* error) { | 314 std::string* error) { |
| 305 Conditions result; | 315 Conditions result; |
| 306 | 316 |
| 307 for (AnyVector::const_iterator i = conditions.begin(); | 317 for (AnyVector::const_iterator i = conditions.begin(); |
| 308 i != conditions.end(); ++i) { | 318 i != conditions.end(); ++i) { |
| 309 CHECK(i->get()); | 319 CHECK(i->get()); |
| 310 scoped_ptr<ConditionT> condition = | 320 scoped_ptr<ConditionT> condition = |
| 311 ConditionT::Create(url_matcher_condition_factory, **i, error); | 321 ConditionT::Create(url_matcher_condition_factories, **i, error); |
| 312 if (!error->empty()) | 322 if (!error->empty()) |
| 313 return scoped_ptr<DeclarativeConditionSet>(NULL); | 323 return scoped_ptr<DeclarativeConditionSet>(NULL); |
| 314 result.push_back(make_linked_ptr(condition.release())); | 324 result.push_back(make_linked_ptr(condition.release())); |
| 315 } | 325 } |
| 316 | 326 |
| 317 URLMatcherIdToCondition match_id_to_condition; | 327 URLMatcherIdToCondition match_id_to_condition; |
| 318 std::vector<const ConditionT*> conditions_without_urls; | 328 std::vector<const ConditionT*> conditions_without_urls; |
| 319 URLMatcherConditionSet::Vector condition_sets; | 329 URLMatcherConditionSet::Vector condition_sets; |
| 320 | 330 |
| 321 for (typename Conditions::const_iterator i = result.begin(); | 331 for (typename Conditions::const_iterator i = result.begin(); |
| 322 i != result.end(); ++i) { | 332 i != result.end(); ++i) { |
| 323 condition_sets.clear(); | 333 condition_sets.clear(); |
| 324 (*i)->GetURLMatcherConditionSets(&condition_sets); | 334 int index = 0; |
| 335 while (index >= 0) | |
| 336 index = (*i)->GetURLMatcherConditionSets(&condition_sets, index); | |
| 325 if (condition_sets.empty()) { | 337 if (condition_sets.empty()) { |
| 326 conditions_without_urls.push_back(i->get()); | 338 conditions_without_urls.push_back(i->get()); |
| 327 } else { | 339 } else { |
| 328 for (URLMatcherConditionSet::Vector::const_iterator | 340 for (URLMatcherConditionSet::Vector::const_iterator |
| 329 match_set = condition_sets.begin(); | 341 match_set = condition_sets.begin(); |
| 330 match_set != condition_sets.end(); ++match_set) | 342 match_set != condition_sets.end(); ++match_set) |
| 331 match_id_to_condition[(*match_set)->id()] = i->get(); | 343 match_id_to_condition[(*match_set)->id()] = i->get(); |
| 332 } | 344 } |
| 333 } | 345 } |
| 334 | 346 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 423 actions_(actions.release()), | 435 actions_(actions.release()), |
| 424 priority_(priority) { | 436 priority_(priority) { |
| 425 CHECK(conditions_.get()); | 437 CHECK(conditions_.get()); |
| 426 CHECK(actions_.get()); | 438 CHECK(actions_.get()); |
| 427 } | 439 } |
| 428 | 440 |
| 429 // static | 441 // static |
| 430 template<typename ConditionT, typename ActionT> | 442 template<typename ConditionT, typename ActionT> |
| 431 scoped_ptr<DeclarativeRule<ConditionT, ActionT> > | 443 scoped_ptr<DeclarativeRule<ConditionT, ActionT> > |
| 432 DeclarativeRule<ConditionT, ActionT>::Create( | 444 DeclarativeRule<ConditionT, ActionT>::Create( |
| 433 URLMatcherConditionFactory* url_matcher_condition_factory, | 445 const std::vector<URLMatcherConditionFactory*>& |
| 446 url_matcher_condition_factories, | |
| 434 const std::string& extension_id, | 447 const std::string& extension_id, |
| 435 base::Time extension_installation_time, | 448 base::Time extension_installation_time, |
| 436 linked_ptr<JsonRule> rule, | 449 linked_ptr<JsonRule> rule, |
| 437 ConsistencyChecker check_consistency, | 450 ConsistencyChecker check_consistency, |
| 438 std::string* error) { | 451 std::string* error) { |
| 439 scoped_ptr<DeclarativeRule> error_result; | 452 scoped_ptr<DeclarativeRule> error_result; |
| 440 | 453 |
| 441 scoped_ptr<ConditionSet> conditions = ConditionSet::Create( | 454 scoped_ptr<ConditionSet> conditions = ConditionSet::Create( |
| 442 url_matcher_condition_factory, rule->conditions, error); | 455 url_matcher_condition_factories, rule->conditions, error); |
| 443 if (!error->empty()) | 456 if (!error->empty()) |
| 444 return error_result.Pass(); | 457 return error_result.Pass(); |
| 445 CHECK(conditions.get()); | 458 CHECK(conditions.get()); |
| 446 | 459 |
| 447 bool bad_message = false; | 460 bool bad_message = false; |
| 448 scoped_ptr<ActionSet> actions = | 461 scoped_ptr<ActionSet> actions = |
| 449 ActionSet::Create(rule->actions, error, &bad_message); | 462 ActionSet::Create(rule->actions, error, &bad_message); |
| 450 if (bad_message) { | 463 if (bad_message) { |
| 451 // TODO(battre) Export concept of bad_message to caller, the extension | 464 // TODO(battre) Export concept of bad_message to caller, the extension |
| 452 // should be killed in case it is true. | 465 // should be killed in case it is true. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 482 } | 495 } |
| 483 | 496 |
| 484 template<typename ConditionT, typename ActionT> | 497 template<typename ConditionT, typename ActionT> |
| 485 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { | 498 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { |
| 486 return actions_->GetMinimumPriority(); | 499 return actions_->GetMinimumPriority(); |
| 487 } | 500 } |
| 488 | 501 |
| 489 } // namespace extensions | 502 } // namespace extensions |
| 490 | 503 |
| 491 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 504 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
| OLD | NEW |