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 | |
47 // // argument. | |
48 // bool has_url_matcher_condition_set(); | |
49 // // |url_matches| and |match_data| passed through from | 50 // // |url_matches| and |match_data| passed through from |
50 // // ConditionSet::IsFulfilled. | 51 // // DeclarativeConditionSet::IsFulfilled. |
51 // bool IsFulfilled( | 52 // bool IsFulfilled( |
52 // const std::set<URLMatcherConditionSet::ID>& url_matches, | 53 // const std::set<URLMatcherConditionSet::ID>& url_matches, |
53 // const ConditionT::MatchData&); | 54 // const ConditionT::MatchData&); |
54 template<typename ConditionT> | 55 template<typename ConditionT> |
55 class DeclarativeConditionSet { | 56 class DeclarativeConditionSet { |
56 public: | 57 public: |
57 typedef std::vector<linked_ptr<base::Value> > AnyVector; | 58 typedef std::vector<linked_ptr<base::Value> > AnyVector; |
58 typedef std::vector<linked_ptr<const ConditionT> > Conditions; | 59 typedef std::vector<linked_ptr<const ConditionT> > Conditions; |
59 typedef typename Conditions::const_iterator const_iterator; | 60 typedef typename Conditions::const_iterator const_iterator; |
60 | 61 |
61 // Factory method that creates an WebRequestConditionSet according to the JSON | 62 // Factory method that creates a WebRequestConditionSet according to the JSON |
62 // array |conditions| passed by the extension API. | 63 // array |conditions| passed by the extension API. Sets |error| and returns |
63 // Sets |error| and returns NULL in case of an error. | 64 // NULL in case of an error. |
64 static scoped_ptr<DeclarativeConditionSet> Create( | 65 static scoped_ptr<DeclarativeConditionSet> Create( |
65 URLMatcherConditionFactory* url_matcher_condition_factory, | 66 URLMatcherConditionFactory* url_matcher_condition_factory, |
66 const AnyVector& conditions, | 67 const AnyVector& conditions, |
67 std::string* error); | 68 std::string* error); |
68 | 69 |
69 const Conditions& conditions() const { | 70 const Conditions& conditions() const { |
70 return conditions_; | 71 return conditions_; |
71 } | 72 } |
72 | 73 |
73 const_iterator begin() const { return conditions_.begin(); } | 74 const_iterator begin() const { return conditions_.begin(); } |
74 const_iterator end() const { return conditions_.end(); } | 75 const_iterator end() const { return conditions_.end(); } |
75 | 76 |
76 // If |url_match_trigger| is a member of |url_matches|, then this | 77 // If |url_match_trigger| is a member of |url_matches|, then this returns |
77 // returns whether the corresponding condition is fulfilled | 78 // whether the corresponding condition is fulfilled wrt. |match_data|. |
78 // wrt. |request_data|. If |url_match_trigger| is -1, this function | 79 // Therefore, if |match_data| contains URL matches from multiple matchers, the |
79 // returns whether any of the conditions without URL attributes is | 80 // union of the matches should appear in |url_matches|. For kinds of MatchData |
80 // satisfied. | 81 // 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|. | 82 // ConditionT::IsFulfilled(), so it doesn't need to appear in |match_data|. |
| 83 // If |url_match_trigger| is -1, this function returns whether any of the |
| 84 // conditions without URL attributes is satisfied. |
93 bool IsFulfilled(URLMatcherConditionSet::ID url_match_trigger, | 85 bool IsFulfilled(URLMatcherConditionSet::ID url_match_trigger, |
94 const std::set<URLMatcherConditionSet::ID>& url_matches, | 86 const std::set<URLMatcherConditionSet::ID>& url_matches, |
95 const typename ConditionT::MatchData& match_data) const; | 87 const typename ConditionT::MatchData& match_data) const; |
96 | 88 |
97 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|. | 89 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|. |
98 void GetURLMatcherConditionSets( | 90 void GetURLMatcherConditionSets( |
99 URLMatcherConditionSet::Vector* condition_sets) const; | 91 URLMatcherConditionSet::Vector* condition_sets) const; |
100 | 92 |
101 // Returns whether there are some conditions without UrlFilter attributes. | 93 // Returns whether there are some conditions without UrlFilter attributes. |
102 bool HasConditionsWithoutUrls() const { | 94 bool HasConditionsWithoutUrls() const { |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 } | 474 } |
483 | 475 |
484 template<typename ConditionT, typename ActionT> | 476 template<typename ConditionT, typename ActionT> |
485 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { | 477 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { |
486 return actions_->GetMinimumPriority(); | 478 return actions_->GetMinimumPriority(); |
487 } | 479 } |
488 | 480 |
489 } // namespace extensions | 481 } // namespace extensions |
490 | 482 |
491 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 483 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
OLD | NEW |