| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITIO
N_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITIO
N_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITIO
N_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITIO
N_H_ |
| 7 | 7 |
| 8 #include <map> |
| 9 #include <set> |
| 8 #include <string> | 10 #include <string> |
| 9 #include <vector> | 11 #include <vector> |
| 10 | 12 |
| 11 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 12 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
| 13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" | 15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" |
| 14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h
" | 16 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h
" |
| 15 #include "chrome/common/extensions/matcher/url_matcher.h" | 17 #include "chrome/common/extensions/matcher/url_matcher.h" |
| 16 | 18 |
| 17 namespace extensions { | 19 namespace extensions { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 40 const WebRequestConditionAttributes& condition_attributes); | 42 const WebRequestConditionAttributes& condition_attributes); |
| 41 ~WebRequestCondition(); | 43 ~WebRequestCondition(); |
| 42 | 44 |
| 43 // Factory method that instantiates a WebRequestCondition according to | 45 // Factory method that instantiates a WebRequestCondition according to |
| 44 // the description |condition| passed by the extension API. | 46 // the description |condition| passed by the extension API. |
| 45 static scoped_ptr<WebRequestCondition> Create( | 47 static scoped_ptr<WebRequestCondition> Create( |
| 46 URLMatcherConditionFactory* url_matcher_condition_factory, | 48 URLMatcherConditionFactory* url_matcher_condition_factory, |
| 47 const base::Value& condition, | 49 const base::Value& condition, |
| 48 std::string* error); | 50 std::string* error); |
| 49 | 51 |
| 50 // Returns whether the request is a match, given that the URLMatcher found | 52 // Returns whether the request matches this condition. |url_matches| lists |
| 51 // a match for |url_matcher_conditions_|. | 53 // the IDs that match the request's URL. |
| 52 bool IsFulfilled(const WebRequestRule::RequestData& request_data) const; | 54 bool IsFulfilled(const std::set<URLMatcherConditionSet::ID> &url_matches, |
| 55 const WebRequestRule::RequestData &request_data) const; |
| 53 | 56 |
| 54 // Returns a URLMatcherConditionSet::ID which is the canonical representation | 57 // Returns a URLMatcherConditionSet::ID which is the canonical representation |
| 55 // for all URL patterns that need to be matched by this WebRequestCondition. | 58 // for all URL patterns that need to be matched by this WebRequestCondition. |
| 56 // This ID is registered in a URLMatcher that can inform us in case of a | 59 // This ID is registered in a URLMatcher that can inform us in case of a |
| 57 // match. | 60 // match. |
| 58 URLMatcherConditionSet::ID url_matcher_condition_set_id() const { | 61 URLMatcherConditionSet::ID url_matcher_condition_set_id() const { |
| 62 DCHECK(url_matcher_conditions_.get()); |
| 59 return url_matcher_conditions_->id(); | 63 return url_matcher_conditions_->id(); |
| 60 } | 64 } |
| 61 | 65 |
| 62 // Returns the set of conditions that are checked on the URL. This is the | 66 // Returns the set of conditions that are checked on the URL. May be NULL. |
| 63 // primary trigger for WebRequestCondition and therefore never empty. | |
| 64 // (If it was empty, the URLMatcher would never notify us about network | |
| 65 // requests which might fulfill the entire WebRequestCondition). | |
| 66 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set() const { | 67 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set() const { |
| 67 return url_matcher_conditions_; | 68 return url_matcher_conditions_; |
| 68 } | 69 } |
| 69 | 70 |
| 70 // Returns the condition attributes checked by this condition. | 71 // Returns the condition attributes checked by this condition. |
| 71 const WebRequestConditionAttributes condition_attributes() const { | 72 const WebRequestConditionAttributes condition_attributes() const { |
| 72 return condition_attributes_; | 73 return condition_attributes_; |
| 73 } | 74 } |
| 74 | 75 |
| 75 // Returns a bit vector representing extensions::RequestStage. The bit vector | 76 // Returns a bit vector representing extensions::RequestStage. The bit vector |
| 76 // contains a 1 for each request stage during which the condition can be | 77 // contains a 1 for each request stage during which the condition can be |
| 77 // tested. | 78 // tested. |
| 78 int stages() const { return applicable_request_stages_; } | 79 int stages() const { return applicable_request_stages_; } |
| 79 | 80 |
| 80 private: | 81 private: |
| 82 // Represents the 'url' attribute of this condition. If NULL, then there was |
| 83 // no 'url' attribute in this condition. |
| 81 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions_; | 84 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions_; |
| 85 |
| 86 // All non-UrlFilter attributes of this condition. |
| 82 WebRequestConditionAttributes condition_attributes_; | 87 WebRequestConditionAttributes condition_attributes_; |
| 83 | 88 |
| 84 // Bit vector indicating all RequestStage during which all | 89 // Bit vector indicating all RequestStage during which all |
| 85 // |condition_attributes_| can be evaluated. | 90 // |condition_attributes_| can be evaluated. |
| 86 int applicable_request_stages_; | 91 int applicable_request_stages_; |
| 87 | 92 |
| 88 DISALLOW_COPY_AND_ASSIGN(WebRequestCondition); | 93 DISALLOW_COPY_AND_ASSIGN(WebRequestCondition); |
| 89 }; | 94 }; |
| 90 | 95 |
| 91 // This class stores a set of conditions that may be part of a WebRequestRule. | 96 // This class stores a set of conditions that may be part of a WebRequestRule. |
| 92 // If any condition is fulfilled, the WebRequestActions of the WebRequestRule | 97 // If any condition is fulfilled, the WebRequestActions of the WebRequestRule |
| 93 // can be triggered. | 98 // can be triggered. |
| 94 class WebRequestConditionSet { | 99 class WebRequestConditionSet { |
| 95 public: | 100 public: |
| 96 typedef std::vector<linked_ptr<json_schema_compiler::any::Any> > AnyVector; | 101 typedef std::vector<linked_ptr<json_schema_compiler::any::Any> > AnyVector; |
| 97 typedef std::vector<linked_ptr<WebRequestCondition> > Conditions; | 102 typedef std::vector<linked_ptr<WebRequestCondition> > Conditions; |
| 98 | 103 |
| 99 explicit WebRequestConditionSet(const Conditions& conditions); | |
| 100 ~WebRequestConditionSet(); | 104 ~WebRequestConditionSet(); |
| 101 | 105 |
| 102 // Factory method that creates an WebRequestConditionSet according to the JSON | 106 // Factory method that creates an WebRequestConditionSet according to the JSON |
| 103 // array |conditions| passed by the extension API. | 107 // array |conditions| passed by the extension API. |
| 104 // Sets |error| and returns NULL in case of an error. | 108 // Sets |error| and returns NULL in case of an error. |
| 105 static scoped_ptr<WebRequestConditionSet> Create( | 109 static scoped_ptr<WebRequestConditionSet> Create( |
| 106 URLMatcherConditionFactory* url_matcher_condition_factory, | 110 URLMatcherConditionFactory* url_matcher_condition_factory, |
| 107 const AnyVector& conditions, | 111 const AnyVector& conditions, |
| 108 std::string* error); | 112 std::string* error); |
| 109 | 113 |
| 110 const Conditions& conditions() const { | 114 const Conditions& conditions() const { |
| 111 return conditions_; | 115 return conditions_; |
| 112 } | 116 } |
| 113 | 117 |
| 114 // Returns whether any condition in the condition set is fulfilled | 118 // If |url_match_trigger| is a member of |url_matches|, then this returns |
| 115 // based on a match |url_match| and the value of |request_data.request|. | 119 // whether the corresponding condition is fulfilled wrt. |request_data|. If |
| 116 // This function should be called for each URLMatcherConditionSet::ID | 120 // |url_match_trigger| is -1, this function returns whether any of the |
| 117 // that was found by the URLMatcher to ensure that the each trigger in | 121 // conditions without URL attributes is satisfied. |
| 118 // |match_triggers_| is found. | |
| 119 bool IsFulfilled( | 122 bool IsFulfilled( |
| 120 URLMatcherConditionSet::ID url_match, | 123 URLMatcherConditionSet::ID url_match_trigger, |
| 124 const std::set<URLMatcherConditionSet::ID>& url_matches, |
| 121 const WebRequestRule::RequestData& request_data) const; | 125 const WebRequestRule::RequestData& request_data) const; |
| 122 | 126 |
| 123 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|. | 127 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|. |
| 124 void GetURLMatcherConditionSets( | 128 void GetURLMatcherConditionSets( |
| 125 URLMatcherConditionSet::Vector* condition_sets) const; | 129 URLMatcherConditionSet::Vector* condition_sets) const; |
| 126 | 130 |
| 131 // Returns whether there are some conditions without UrlFilter attributes. |
| 132 bool HasConditionsWithoutUrls() const; |
| 133 |
| 127 private: | 134 private: |
| 128 Conditions conditions_; | 135 typedef std::map<URLMatcherConditionSet::ID, const WebRequestCondition*> |
| 136 URLMatcherIdToCondition; |
| 129 | 137 |
| 130 typedef std::map<URLMatcherConditionSet::ID, WebRequestCondition*> | 138 WebRequestConditionSet( |
| 131 MatchTriggers; | 139 const Conditions& conditions, |
| 132 MatchTriggers match_triggers_; | 140 const URLMatcherIdToCondition& match_id_to_condition, |
| 141 const std::vector<const WebRequestCondition*>& conditions_without_urls); |
| 142 |
| 143 const URLMatcherIdToCondition match_id_to_condition_; |
| 144 const Conditions conditions_; |
| 145 const std::vector<const WebRequestCondition*> conditions_without_urls_; |
| 133 | 146 |
| 134 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionSet); | 147 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionSet); |
| 135 }; | 148 }; |
| 136 | 149 |
| 137 } // namespace extensions | 150 } // namespace extensions |
| 138 | 151 |
| 139 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI
TION_H_ | 152 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI
TION_H_ |
| OLD | NEW |