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 ConditionSet::Create. |
|
battre
2013/01/21 17:32:08
can you please update ConditionSet->DeclarativeCon
vabr (Chromium)
2013/01/21 18:39:29
Done. To be correct, we would need to also add <Co
| |
| 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 are nonnegative. The return value always gives the next higher | |
| 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 // // ConditionSet::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 an WebRequestConditionSet according to the JSON |
| 62 // array |conditions| passed by the extension API. | 67 // array |conditions| passed by the extension API. |
|
battre
2013/01/21 17:32:08
Please add:
// As IsFulfilled gets passed the unio
vabr (Chromium)
2013/01/21 18:39:29
Done, with minor edits. Please check that you are
| |
| 63 // Sets |error| and returns NULL in case of an error. | 68 // Sets |error| and returns NULL in case of an error. |
| 64 static scoped_ptr<DeclarativeConditionSet> Create( | 69 static scoped_ptr<DeclarativeConditionSet> Create( |
| 65 URLMatcherConditionFactory* url_matcher_condition_factory, | 70 const std::vector<URLMatcherConditionFactory*>& |
| 71 url_matcher_condition_factories, | |
| 66 const AnyVector& conditions, | 72 const AnyVector& conditions, |
| 67 std::string* error); | 73 std::string* error); |
| 68 | 74 |
| 69 const Conditions& conditions() const { | 75 const Conditions& conditions() const { |
| 70 return conditions_; | 76 return conditions_; |
| 71 } | 77 } |
| 72 | 78 |
| 73 const_iterator begin() const { return conditions_.begin(); } | 79 const_iterator begin() const { return conditions_.begin(); } |
| 74 const_iterator end() const { return conditions_.end(); } | 80 const_iterator end() const { return conditions_.end(); } |
| 75 | 81 |
| 76 // If |url_match_trigger| is a member of |url_matches|, then this | 82 // If |url_match_trigger| is a member of |url_matches|, then this returns |
| 77 // returns whether the corresponding condition is fulfilled | 83 // whether the corresponding condition is fulfilled wrt. |request_data|. |
| 78 // wrt. |request_data|. If |url_match_trigger| is -1, this function | 84 // Therefore, if |match_data| contains URL matches from more matchers, the |
| 79 // returns whether any of the conditions without URL attributes is | 85 // union of the matches should appear in |url_matches|. For kinds of MatchData |
| 80 // satisfied. | 86 // 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|. | 87 // ConditionT::IsFulfilled(), so it doesn't need to appear in |match_data|. |
| 88 // If |url_match_trigger| is -1, this function returns whether any of the | |
| 89 // conditions without URL attributes is satisfied. | |
| 93 bool IsFulfilled(URLMatcherConditionSet::ID url_match_trigger, | 90 bool IsFulfilled(URLMatcherConditionSet::ID url_match_trigger, |
| 94 const std::set<URLMatcherConditionSet::ID>& url_matches, | 91 const std::set<URLMatcherConditionSet::ID>& url_matches, |
| 95 const typename ConditionT::MatchData& match_data) const; | 92 const typename ConditionT::MatchData& match_data) const; |
| 96 | 93 |
| 97 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|. | 94 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|. |
|
battre
2013/01/21 17:32:08
Please comment on the return value.
vabr (Chromium)
2013/01/21 18:39:29
Done.
| |
| 98 void GetURLMatcherConditionSets( | 95 int GetURLMatcherConditionSets( |
| 99 URLMatcherConditionSet::Vector* condition_sets) const; | 96 URLMatcherConditionSet::Vector* condition_sets, |
| 97 int index) const; | |
| 100 | 98 |
| 101 // Returns whether there are some conditions without UrlFilter attributes. | 99 // Returns whether there are some conditions without UrlFilter attributes. |
| 102 bool HasConditionsWithoutUrls() const { | 100 bool HasConditionsWithoutUrls() const { |
| 103 return !conditions_without_urls_.empty(); | 101 return !conditions_without_urls_.empty(); |
| 104 } | 102 } |
| 105 | 103 |
| 106 private: | 104 private: |
| 107 typedef std::map<URLMatcherConditionSet::ID, const ConditionT*> | 105 typedef std::map<URLMatcherConditionSet::ID, const ConditionT*> |
| 108 URLMatcherIdToCondition; | 106 URLMatcherIdToCondition; |
| 109 | 107 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 | 214 |
| 217 // Creates a DeclarativeRule for an extension given a json definition. The | 215 // 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 | 216 // format of each condition and action's json is up to the specific ConditionT |
| 219 // and ActionT. | 217 // and ActionT. |
| 220 // | 218 // |
| 221 // Before constructing the final rule, calls check_consistency(conditions, | 219 // Before constructing the final rule, calls check_consistency(conditions, |
| 222 // actions, error) and returns NULL if it fails. Pass NULL if no consistency | 220 // 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 | 221 // check is needed. If |error| is empty, the translation was successful and |
| 224 // the returned rule is internally consistent. | 222 // the returned rule is internally consistent. |
| 225 static scoped_ptr<DeclarativeRule> Create( | 223 static scoped_ptr<DeclarativeRule> Create( |
| 226 URLMatcherConditionFactory* url_matcher_condition_factory, | 224 const std::vector<URLMatcherConditionFactory*>& |
| 225 url_matcher_condition_factories, | |
| 227 const std::string& extension_id, | 226 const std::string& extension_id, |
| 228 base::Time extension_installation_time, | 227 base::Time extension_installation_time, |
| 229 linked_ptr<JsonRule> rule, | 228 linked_ptr<JsonRule> rule, |
| 230 ConsistencyChecker check_consistency, | 229 ConsistencyChecker check_consistency, |
| 231 std::string* error); | 230 std::string* error); |
| 232 | 231 |
| 233 const GlobalRuleId& id() const { return id_; } | 232 const GlobalRuleId& id() const { return id_; } |
| 234 const std::string& extension_id() const { return id_.first; } | 233 const std::string& extension_id() const { return id_.first; } |
| 235 const ConditionSet& conditions() const { return *conditions_; } | 234 const ConditionSet& conditions() const { return *conditions_; } |
| 236 const ActionSet& actions() const { return *actions_; } | 235 const ActionSet& actions() const { return *actions_; } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 return false; | 279 return false; |
| 281 } | 280 } |
| 282 | 281 |
| 283 typename URLMatcherIdToCondition::const_iterator triggered = | 282 typename URLMatcherIdToCondition::const_iterator triggered = |
| 284 match_id_to_condition_.find(url_match_trigger); | 283 match_id_to_condition_.find(url_match_trigger); |
| 285 return (triggered != match_id_to_condition_.end() && | 284 return (triggered != match_id_to_condition_.end() && |
| 286 triggered->second->IsFulfilled(url_matches, match_data)); | 285 triggered->second->IsFulfilled(url_matches, match_data)); |
| 287 } | 286 } |
| 288 | 287 |
| 289 template<typename ConditionT> | 288 template<typename ConditionT> |
| 290 void DeclarativeConditionSet<ConditionT>::GetURLMatcherConditionSets( | 289 int DeclarativeConditionSet<ConditionT>::GetURLMatcherConditionSets( |
| 291 URLMatcherConditionSet::Vector* condition_sets) const { | 290 URLMatcherConditionSet::Vector* condition_sets, |
| 291 int index) const { | |
| 292 bool positive_returned = false; | |
|
battre
2013/01/21 17:32:08
nit: rename this to "more_condition_sets_exist"?
vabr (Chromium)
2013/01/21 18:39:29
Done.
| |
| 292 for (typename Conditions::const_iterator i = conditions_.begin(); | 293 for (typename Conditions::const_iterator i = conditions_.begin(); |
| 293 i != conditions_.end(); ++i) { | 294 i != conditions_.end(); ++i) { |
| 294 (*i)->GetURLMatcherConditionSets(condition_sets); | 295 if ((*i)->GetURLMatcherConditionSets(condition_sets, index) >= 0) |
| 296 positive_returned = true; | |
| 295 } | 297 } |
| 298 return (positive_returned ? (index + 1) : -1); | |
| 296 } | 299 } |
| 297 | 300 |
| 298 // static | 301 // static |
| 299 template<typename ConditionT> | 302 template<typename ConditionT> |
| 300 scoped_ptr<DeclarativeConditionSet<ConditionT> > | 303 scoped_ptr<DeclarativeConditionSet<ConditionT> > |
| 301 DeclarativeConditionSet<ConditionT>::Create( | 304 DeclarativeConditionSet<ConditionT>::Create( |
| 302 URLMatcherConditionFactory* url_matcher_condition_factory, | 305 const std::vector<URLMatcherConditionFactory*>& |
| 306 url_matcher_condition_factories, | |
| 303 const AnyVector& conditions, | 307 const AnyVector& conditions, |
| 304 std::string* error) { | 308 std::string* error) { |
| 305 Conditions result; | 309 Conditions result; |
| 306 | 310 |
| 307 for (AnyVector::const_iterator i = conditions.begin(); | 311 for (AnyVector::const_iterator i = conditions.begin(); |
| 308 i != conditions.end(); ++i) { | 312 i != conditions.end(); ++i) { |
| 309 CHECK(i->get()); | 313 CHECK(i->get()); |
| 310 scoped_ptr<ConditionT> condition = | 314 scoped_ptr<ConditionT> condition = |
| 311 ConditionT::Create(url_matcher_condition_factory, **i, error); | 315 ConditionT::Create(url_matcher_condition_factories, **i, error); |
| 312 if (!error->empty()) | 316 if (!error->empty()) |
| 313 return scoped_ptr<DeclarativeConditionSet>(NULL); | 317 return scoped_ptr<DeclarativeConditionSet>(NULL); |
| 314 result.push_back(make_linked_ptr(condition.release())); | 318 result.push_back(make_linked_ptr(condition.release())); |
| 315 } | 319 } |
| 316 | 320 |
| 317 URLMatcherIdToCondition match_id_to_condition; | 321 URLMatcherIdToCondition match_id_to_condition; |
| 318 std::vector<const ConditionT*> conditions_without_urls; | 322 std::vector<const ConditionT*> conditions_without_urls; |
| 319 URLMatcherConditionSet::Vector condition_sets; | 323 URLMatcherConditionSet::Vector condition_sets; |
| 320 | 324 |
| 321 for (typename Conditions::const_iterator i = result.begin(); | 325 for (typename Conditions::const_iterator i = result.begin(); |
| 322 i != result.end(); ++i) { | 326 i != result.end(); ++i) { |
| 323 condition_sets.clear(); | 327 condition_sets.clear(); |
| 324 (*i)->GetURLMatcherConditionSets(&condition_sets); | 328 int index = 0; |
| 329 while (index >= 0) | |
| 330 index = (*i)->GetURLMatcherConditionSets(&condition_sets, index); | |
|
battre
2013/01/21 17:32:08
should we check the invariants here?
int old_index
vabr (Chromium)
2013/01/22 08:34:53
Done.
| |
| 325 if (condition_sets.empty()) { | 331 if (condition_sets.empty()) { |
| 326 conditions_without_urls.push_back(i->get()); | 332 conditions_without_urls.push_back(i->get()); |
| 327 } else { | 333 } else { |
| 328 for (URLMatcherConditionSet::Vector::const_iterator | 334 for (URLMatcherConditionSet::Vector::const_iterator |
| 329 match_set = condition_sets.begin(); | 335 match_set = condition_sets.begin(); |
| 330 match_set != condition_sets.end(); ++match_set) | 336 match_set != condition_sets.end(); ++match_set) |
| 331 match_id_to_condition[(*match_set)->id()] = i->get(); | 337 match_id_to_condition[(*match_set)->id()] = i->get(); |
| 332 } | 338 } |
| 333 } | 339 } |
| 334 | 340 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 423 actions_(actions.release()), | 429 actions_(actions.release()), |
| 424 priority_(priority) { | 430 priority_(priority) { |
| 425 CHECK(conditions_.get()); | 431 CHECK(conditions_.get()); |
| 426 CHECK(actions_.get()); | 432 CHECK(actions_.get()); |
| 427 } | 433 } |
| 428 | 434 |
| 429 // static | 435 // static |
| 430 template<typename ConditionT, typename ActionT> | 436 template<typename ConditionT, typename ActionT> |
| 431 scoped_ptr<DeclarativeRule<ConditionT, ActionT> > | 437 scoped_ptr<DeclarativeRule<ConditionT, ActionT> > |
| 432 DeclarativeRule<ConditionT, ActionT>::Create( | 438 DeclarativeRule<ConditionT, ActionT>::Create( |
| 433 URLMatcherConditionFactory* url_matcher_condition_factory, | 439 const std::vector<URLMatcherConditionFactory*>& |
| 440 url_matcher_condition_factories, | |
| 434 const std::string& extension_id, | 441 const std::string& extension_id, |
| 435 base::Time extension_installation_time, | 442 base::Time extension_installation_time, |
| 436 linked_ptr<JsonRule> rule, | 443 linked_ptr<JsonRule> rule, |
| 437 ConsistencyChecker check_consistency, | 444 ConsistencyChecker check_consistency, |
| 438 std::string* error) { | 445 std::string* error) { |
| 439 scoped_ptr<DeclarativeRule> error_result; | 446 scoped_ptr<DeclarativeRule> error_result; |
| 440 | 447 |
| 441 scoped_ptr<ConditionSet> conditions = ConditionSet::Create( | 448 scoped_ptr<ConditionSet> conditions = ConditionSet::Create( |
| 442 url_matcher_condition_factory, rule->conditions, error); | 449 url_matcher_condition_factories, rule->conditions, error); |
| 443 if (!error->empty()) | 450 if (!error->empty()) |
| 444 return error_result.Pass(); | 451 return error_result.Pass(); |
| 445 CHECK(conditions.get()); | 452 CHECK(conditions.get()); |
| 446 | 453 |
| 447 bool bad_message = false; | 454 bool bad_message = false; |
| 448 scoped_ptr<ActionSet> actions = | 455 scoped_ptr<ActionSet> actions = |
| 449 ActionSet::Create(rule->actions, error, &bad_message); | 456 ActionSet::Create(rule->actions, error, &bad_message); |
| 450 if (bad_message) { | 457 if (bad_message) { |
| 451 // TODO(battre) Export concept of bad_message to caller, the extension | 458 // TODO(battre) Export concept of bad_message to caller, the extension |
| 452 // should be killed in case it is true. | 459 // should be killed in case it is true. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 482 } | 489 } |
| 483 | 490 |
| 484 template<typename ConditionT, typename ActionT> | 491 template<typename ConditionT, typename ActionT> |
| 485 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { | 492 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { |
| 486 return actions_->GetMinimumPriority(); | 493 return actions_->GetMinimumPriority(); |
| 487 } | 494 } |
| 488 | 495 |
| 489 } // namespace extensions | 496 } // namespace extensions |
| 490 | 497 |
| 491 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 498 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
| OLD | NEW |