| 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 <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
| 14 #include "chrome/browser/extensions/api/declarative/declarative_rule.h" |
| 14 #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" |
| 15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h
" | |
| 16 #include "chrome/common/extensions/matcher/url_matcher.h" | 16 #include "chrome/common/extensions/matcher/url_matcher.h" |
| 17 #include "net/http/http_response_headers.h" |
| 17 | 18 |
| 18 namespace extensions { | 19 namespace extensions { |
| 19 | 20 |
| 21 // Container for information about a URLRequest to determine which |
| 22 // rules apply to the request. |
| 23 struct DeclarativeWebRequestData { |
| 24 DeclarativeWebRequestData(net::URLRequest* request, RequestStage stage) |
| 25 : request(request), stage(stage), |
| 26 original_response_headers(NULL) {} |
| 27 DeclarativeWebRequestData( |
| 28 net::URLRequest* request, RequestStage stage, |
| 29 const net::HttpResponseHeaders* original_response_headers) |
| 30 : request(request), stage(stage), |
| 31 original_response_headers(original_response_headers) {} |
| 32 net::URLRequest* request; |
| 33 // The set of URLMatcherConditionSets (from all rules) that match |
| 34 // request->url(). |
| 35 const std::set<URLMatcherConditionSet::ID>* url_matches; |
| 36 RequestStage stage; |
| 37 // Additional information about requests that is not |
| 38 // available in all request stages. |
| 39 const net::HttpResponseHeaders* original_response_headers; |
| 40 }; |
| 41 |
| 20 // Representation of a condition in the Declarative WebRequest API. A condition | 42 // Representation of a condition in the Declarative WebRequest API. A condition |
| 21 // consists of several attributes. Each of these attributes needs to be | 43 // consists of several attributes. Each of these attributes needs to be |
| 22 // fulfilled in order for the condition to be fulfilled. | 44 // fulfilled in order for the condition to be fulfilled. |
| 23 // | 45 // |
| 24 // We distinguish between two types of conditions: | 46 // We distinguish between two types of conditions: |
| 25 // - URL Matcher conditions are conditions that test the URL of a request. | 47 // - URL Matcher conditions are conditions that test the URL of a request. |
| 26 // These are treated separately because we use a URLMatcher to efficiently | 48 // These are treated separately because we use a URLMatcher to efficiently |
| 27 // test many of these conditions in parallel by using some advanced | 49 // test many of these conditions in parallel by using some advanced |
| 28 // data structures. The URLMatcher tells us if all URL Matcher conditions | 50 // data structures. The URLMatcher tells us if all URL Matcher conditions |
| 29 // are fulfilled for a WebRequestCondition. | 51 // are fulfilled for a WebRequestCondition. |
| 30 // - All other conditions are represented as WebRequestConditionAttributes. | 52 // - All other conditions are represented as WebRequestConditionAttributes. |
| 31 // These conditions are probed linearly (only if the URL Matcher found a hit). | 53 // These conditions are probed linearly (only if the URL Matcher found a hit). |
| 32 // | 54 // |
| 33 // TODO(battre) Consider making the URLMatcher an owner of the | 55 // TODO(battre) Consider making the URLMatcher an owner of the |
| 34 // URLMatcherConditionSet and only pass a pointer to URLMatcherConditionSet | 56 // URLMatcherConditionSet and only pass a pointer to URLMatcherConditionSet |
| 35 // in url_matcher_condition_set(). This saves some copying in | 57 // in url_matcher_condition_set(). This saves some copying in |
| 36 // WebRequestConditionSet::GetURLMatcherConditionSets. | 58 // WebRequestConditionSet::GetURLMatcherConditionSets. |
| 37 class WebRequestCondition { | 59 class WebRequestCondition { |
| 38 public: | 60 public: |
| 61 typedef DeclarativeWebRequestData MatchData; |
| 62 |
| 39 WebRequestCondition( | 63 WebRequestCondition( |
| 40 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, | 64 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, |
| 41 const WebRequestConditionAttributes& condition_attributes); | 65 const WebRequestConditionAttributes& condition_attributes); |
| 42 ~WebRequestCondition(); | 66 ~WebRequestCondition(); |
| 43 | 67 |
| 44 // Factory method that instantiates a WebRequestCondition according to | 68 // Factory method that instantiates a WebRequestCondition according to |
| 45 // the description |condition| passed by the extension API. | 69 // the description |condition| passed by the extension API. |
| 46 static scoped_ptr<WebRequestCondition> Create( | 70 static scoped_ptr<WebRequestCondition> Create( |
| 47 URLMatcherConditionFactory* url_matcher_condition_factory, | 71 URLMatcherConditionFactory* url_matcher_condition_factory, |
| 48 const base::Value& condition, | 72 const base::Value& condition, |
| 49 std::string* error); | 73 std::string* error); |
| 50 | 74 |
| 51 // Returns whether the request matches this condition. |url_matches| lists | 75 // Returns whether the request matches this condition. |url_matches| lists |
| 52 // the IDs that match the request's URL. | 76 // the IDs that match the request's URL. |
| 53 bool IsFulfilled( | 77 bool IsFulfilled(const DeclarativeWebRequestData& request_data) const; |
| 54 const std::set<URLMatcherConditionSet::ID>& url_matches, | |
| 55 const WebRequestRule::RequestData& request_data) const; | |
| 56 | 78 |
| 57 // Returns a URLMatcherConditionSet::ID which is the canonical representation | 79 // True if this condition has a url filter. |
| 58 // for all URL patterns that need to be matched by this WebRequestCondition. | 80 bool has_url_matcher_condition_set() const { |
| 59 // This ID is registered in a URLMatcher that can inform us in case of a | 81 return bool(url_matcher_conditions_); |
| 60 // match. | |
| 61 URLMatcherConditionSet::ID url_matcher_condition_set_id() const { | |
| 62 DCHECK(url_matcher_conditions_.get()); | |
| 63 return url_matcher_conditions_->id(); | |
| 64 } | 82 } |
| 65 | 83 |
| 66 // Returns the set of conditions that are checked on the URL. This is the | 84 // If this Condition has a url filter, appends it to |condition_sets|. |
| 67 // primary trigger for WebRequestCondition and therefore never empty. | 85 void GetURLMatcherConditionSets( |
| 68 // (If it was empty, the URLMatcher would never notify us about network | 86 URLMatcherConditionSet::Vector* condition_sets) const { |
| 69 // requests which might fulfill the entire WebRequestCondition). | 87 if (url_matcher_conditions_) |
| 70 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set() const { | 88 condition_sets->push_back(url_matcher_conditions_); |
| 71 return url_matcher_conditions_; | |
| 72 } | 89 } |
| 73 | 90 |
| 74 // Returns the condition attributes checked by this condition. | 91 // Returns the condition attributes checked by this condition. |
| 75 const WebRequestConditionAttributes condition_attributes() const { | 92 const WebRequestConditionAttributes condition_attributes() const { |
| 76 return condition_attributes_; | 93 return condition_attributes_; |
| 77 } | 94 } |
| 78 | 95 |
| 79 // Returns a bit vector representing extensions::RequestStage. The bit vector | 96 // Returns a bit vector representing extensions::RequestStage. The bit vector |
| 80 // contains a 1 for each request stage during which the condition can be | 97 // contains a 1 for each request stage during which the condition can be |
| 81 // tested. | 98 // tested. |
| 82 int stages() const { return applicable_request_stages_; } | 99 int stages() const { return applicable_request_stages_; } |
| 83 | 100 |
| 84 private: | 101 private: |
| 85 // The 'url' attribute of this condition. If NULL then there was no 'url' | 102 // The 'url' attribute of this condition. If NULL then there was no 'url' |
| 86 // attribute in this condition. | 103 // attribute in this condition. |
| 87 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions_; | 104 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions_; |
| 88 | 105 |
| 89 // All non-UrlFilter attributes of this condition. | 106 // All non-UrlFilter attributes of this condition. |
| 90 WebRequestConditionAttributes condition_attributes_; | 107 WebRequestConditionAttributes condition_attributes_; |
| 91 | 108 |
| 92 // Bit vector indicating all RequestStage during which all | 109 // Bit vector indicating all RequestStage during which all |
| 93 // |condition_attributes_| can be evaluated. | 110 // |condition_attributes_| can be evaluated. |
| 94 int applicable_request_stages_; | 111 int applicable_request_stages_; |
| 95 | 112 |
| 96 DISALLOW_COPY_AND_ASSIGN(WebRequestCondition); | 113 DISALLOW_COPY_AND_ASSIGN(WebRequestCondition); |
| 97 }; | 114 }; |
| 98 | 115 |
| 99 // This class stores a set of conditions that may be part of a WebRequestRule. | 116 typedef DeclarativeConditionSet<WebRequestCondition> WebRequestConditionSet; |
| 100 // If any condition is fulfilled, the WebRequestActions of the WebRequestRule | |
| 101 // can be triggered. | |
| 102 class WebRequestConditionSet { | |
| 103 public: | |
| 104 typedef std::vector<linked_ptr<json_schema_compiler::any::Any> > AnyVector; | |
| 105 typedef std::vector<linked_ptr<WebRequestCondition> > Conditions; | |
| 106 | |
| 107 explicit WebRequestConditionSet(const Conditions& conditions); | |
| 108 ~WebRequestConditionSet(); | |
| 109 | |
| 110 // Factory method that creates an WebRequestConditionSet according to the JSON | |
| 111 // array |conditions| passed by the extension API. | |
| 112 // Sets |error| and returns NULL in case of an error. | |
| 113 static scoped_ptr<WebRequestConditionSet> Create( | |
| 114 URLMatcherConditionFactory* url_matcher_condition_factory, | |
| 115 const AnyVector& conditions, | |
| 116 std::string* error); | |
| 117 | |
| 118 const Conditions& conditions() const { | |
| 119 return conditions_; | |
| 120 } | |
| 121 | |
| 122 // Returns whether any condition in the condition set is fulfilled. Filters | |
| 123 // out conditions which have UrlFilter attributes but are not listed in | |
| 124 // |url_matches|, and tests the rest against |request_data.request|. | |
| 125 bool IsFulfilled( | |
| 126 const std::set<URLMatcherConditionSet::ID>& url_matches, | |
| 127 const WebRequestRule::RequestData& request_data) const; | |
| 128 | |
| 129 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|. | |
| 130 void GetURLMatcherConditionSets( | |
| 131 URLMatcherConditionSet::Vector* condition_sets) const; | |
| 132 | |
| 133 // Returns whether there are some conditions without UrlFilter attributes. | |
| 134 bool has_conditions_without_urls() const { | |
| 135 return has_conditions_without_urls_; | |
| 136 } | |
| 137 | |
| 138 private: | |
| 139 const Conditions conditions_; | |
| 140 const bool has_conditions_without_urls_; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionSet); | |
| 143 }; | |
| 144 | 117 |
| 145 } // namespace extensions | 118 } // namespace extensions |
| 146 | 119 |
| 147 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI
TION_H_ | 120 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI
TION_H_ |
| OLD | NEW |