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