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