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> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
15 #include "chrome/browser/extensions/api/declarative/declarative_rule.h" | 15 #include "chrome/browser/extensions/api/declarative/declarative_rule.h" |
16 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" | 16 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" |
17 #include "chrome/common/extensions/matcher/url_matcher.h" | 17 #include "chrome/common/extensions/matcher/url_matcher.h" |
18 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
19 | 19 |
20 namespace extensions { | 20 namespace extensions { |
21 | 21 |
22 // Container for information about a URLRequest to determine which | 22 // Container for information about a URLRequest to determine which |
23 // rules apply to the request. | 23 // rules apply to the request. |
24 struct DeclarativeWebRequestData { | 24 struct WebRequestData { |
25 DeclarativeWebRequestData(net::URLRequest* request, RequestStage stage) | 25 WebRequestData(net::URLRequest* request, RequestStage stage); |
26 : request(request), stage(stage), | 26 WebRequestData( |
27 original_response_headers(NULL) {} | 27 net::URLRequest* request, |
28 DeclarativeWebRequestData( | 28 RequestStage stage, |
29 net::URLRequest* request, RequestStage stage, | 29 const net::HttpResponseHeaders* original_response_headers); |
30 const net::HttpResponseHeaders* original_response_headers) | 30 ~WebRequestData(); |
31 : request(request), stage(stage), | 31 |
32 original_response_headers(original_response_headers) {} | |
33 // The network request that is currently being processed. | 32 // The network request that is currently being processed. |
34 net::URLRequest* request; | 33 net::URLRequest* request; |
35 // The stage (progress) of the network request. | 34 // The stage (progress) of the network request. |
36 RequestStage stage; | 35 RequestStage stage; |
37 // Additional information about requests that is not | 36 // Additional information about requests that is not |
38 // available in all request stages. | 37 // available in all request stages. |
39 const net::HttpResponseHeaders* original_response_headers; | 38 const net::HttpResponseHeaders* original_response_headers; |
40 }; | 39 }; |
41 | 40 |
| 41 // Adds information about URL matches to WebRequestData. |
| 42 struct WebRequestDataWithMatchIds { |
| 43 explicit WebRequestDataWithMatchIds(const WebRequestData* request_data); |
| 44 ~WebRequestDataWithMatchIds(); |
| 45 |
| 46 const WebRequestData* data; |
| 47 std::set<URLMatcherConditionSet::ID> url_match_ids; |
| 48 std::set<URLMatcherConditionSet::ID> first_party_url_match_ids; |
| 49 }; |
| 50 |
42 // Representation of a condition in the Declarative WebRequest API. A condition | 51 // Representation of a condition in the Declarative WebRequest API. A condition |
43 // consists of several attributes. Each of these attributes needs to be | 52 // consists of several attributes. Each of these attributes needs to be |
44 // fulfilled in order for the condition to be fulfilled. | 53 // fulfilled in order for the condition to be fulfilled. |
45 // | 54 // |
46 // We distinguish between two types of conditions: | 55 // We distinguish between two types of conditions: |
47 // - URL Matcher conditions are conditions that test the URL of a request. | 56 // - URL Matcher conditions are conditions that test the URL of a request. |
48 // These are treated separately because we use a URLMatcher to efficiently | 57 // These are treated separately because we use a URLMatcher to efficiently |
49 // test many of these conditions in parallel by using some advanced | 58 // test many of these conditions in parallel by using some advanced |
50 // data structures. The URLMatcher tells us if all URL Matcher conditions | 59 // data structures. The URLMatcher tells us if all URL Matcher conditions |
51 // are fulfilled for a WebRequestCondition. | 60 // are fulfilled for a WebRequestCondition. |
52 // - All other conditions are represented as WebRequestConditionAttributes. | 61 // - All other conditions are represented as WebRequestConditionAttributes. |
53 // These conditions are probed linearly (only if the URL Matcher found a hit). | 62 // These conditions are probed linearly (only if the URL Matcher found a hit). |
54 // | 63 // |
55 // TODO(battre) Consider making the URLMatcher an owner of the | 64 // TODO(battre) Consider making the URLMatcher an owner of the |
56 // URLMatcherConditionSet and only pass a pointer to URLMatcherConditionSet | 65 // URLMatcherConditionSet and only pass a pointer to URLMatcherConditionSet |
57 // in url_matcher_condition_set(). This saves some copying in | 66 // in url_matcher_condition_set(). This saves some copying in |
58 // WebRequestConditionSet::GetURLMatcherConditionSets. | 67 // WebRequestConditionSet::GetURLMatcherConditionSets. |
59 class WebRequestCondition { | 68 class WebRequestCondition { |
60 public: | 69 public: |
61 typedef DeclarativeWebRequestData MatchData; | 70 typedef WebRequestDataWithMatchIds MatchData; |
62 | 71 |
63 WebRequestCondition( | 72 WebRequestCondition( |
64 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, | 73 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, |
| 74 scoped_refptr<URLMatcherConditionSet> first_party_url_matcher_conditions, |
65 const WebRequestConditionAttributes& condition_attributes); | 75 const WebRequestConditionAttributes& condition_attributes); |
66 ~WebRequestCondition(); | 76 ~WebRequestCondition(); |
67 | 77 |
68 // Factory method that instantiates a WebRequestCondition according to | 78 // Factory method that instantiates a WebRequestCondition according to |
69 // the description |condition| passed by the extension API. | 79 // the description |condition| passed by the extension API. |
70 static scoped_ptr<WebRequestCondition> Create( | 80 static scoped_ptr<WebRequestCondition> Create( |
71 URLMatcherConditionFactory* url_matcher_condition_factory, | 81 URLMatcherConditionFactory* url_matcher_condition_factory, |
72 const base::Value& condition, | 82 const base::Value& condition, |
73 std::string* error); | 83 std::string* error); |
74 | 84 |
75 // Returns whether the request matches this condition. |url_matches| lists | 85 // Returns whether the request matches this condition. |
76 // the IDs that match the request's URL. | 86 bool IsFulfilled(const MatchData& request_data) const; |
77 bool IsFulfilled(const std::set<URLMatcherConditionSet::ID> &url_matches, | |
78 const DeclarativeWebRequestData& request_data) const; | |
79 | 87 |
80 // True if this condition has a url filter. | 88 // If this condition has url attributes, appends them to |condition_sets|. |
81 bool has_url_matcher_condition_set() const { | |
82 return url_matcher_conditions_ != NULL; | |
83 } | |
84 | |
85 // If this Condition has a url filter, appends it to |condition_sets|. | |
86 void GetURLMatcherConditionSets( | 89 void GetURLMatcherConditionSets( |
87 URLMatcherConditionSet::Vector* condition_sets) const { | 90 URLMatcherConditionSet::Vector* condition_sets) const; |
88 if (url_matcher_conditions_) | |
89 condition_sets->push_back(url_matcher_conditions_); | |
90 } | |
91 | 91 |
92 // Returns the condition attributes checked by this condition. | 92 // Returns the condition attributes checked by this condition. |
93 const WebRequestConditionAttributes condition_attributes() const { | 93 const WebRequestConditionAttributes condition_attributes() const { |
94 return condition_attributes_; | 94 return condition_attributes_; |
95 } | 95 } |
96 | 96 |
97 // Returns a bit vector representing extensions::RequestStage. The bit vector | 97 // Returns a bit vector representing extensions::RequestStage. The bit vector |
98 // contains a 1 for each request stage during which the condition can be | 98 // contains a 1 for each request stage during which the condition can be |
99 // tested. | 99 // tested. |
100 int stages() const { return applicable_request_stages_; } | 100 int stages() const { return applicable_request_stages_; } |
101 | 101 |
102 private: | 102 private: |
103 // Represents the 'url' attribute of this condition. If NULL, then there was | 103 // URL attributes of this condition. |
104 // no 'url' attribute in this condition. | |
105 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions_; | 104 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions_; |
| 105 scoped_refptr<URLMatcherConditionSet> first_party_url_matcher_conditions_; |
106 | 106 |
107 // All non-UrlFilter attributes of this condition. | 107 // All non-UrlFilter attributes of this condition. |
108 WebRequestConditionAttributes condition_attributes_; | 108 WebRequestConditionAttributes condition_attributes_; |
109 | 109 |
110 // Bit vector indicating all RequestStage during which all | 110 // Bit vector indicating all RequestStage during which all |
111 // |condition_attributes_| can be evaluated. | 111 // |condition_attributes_| can be evaluated. |
112 int applicable_request_stages_; | 112 int applicable_request_stages_; |
113 | 113 |
114 DISALLOW_COPY_AND_ASSIGN(WebRequestCondition); | 114 DISALLOW_COPY_AND_ASSIGN(WebRequestCondition); |
115 }; | 115 }; |
116 | 116 |
117 typedef DeclarativeConditionSet<WebRequestCondition> WebRequestConditionSet; | 117 typedef DeclarativeConditionSet<WebRequestCondition> WebRequestConditionSet; |
118 | 118 |
119 } // namespace extensions | 119 } // namespace extensions |
120 | 120 |
121 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI
TION_H_ | 121 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI
TION_H_ |
OLD | NEW |