Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(602)

Side by Side Diff: chrome/browser/extensions/api/declarative_webrequest/webrequest_condition.h

Issue 11572061: Create DeclarativeConditionSet, DeclarativeActionSet, and DeclarativeRule templates (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix Dominic's comments Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_webrequest/webrequest_condit ion_attribute.h" 16 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion_attribute.h"
16 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.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 19
19 namespace extensions { 20 namespace extensions {
20 21
22 // Container for information about a URLRequest to determine which
23 // rules apply to the request.
24 struct DeclarativeWebRequestData {
25 DeclarativeWebRequestData(net::URLRequest* request, RequestStage stage)
26 : request(request), stage(stage),
27 original_response_headers(NULL) {}
28 DeclarativeWebRequestData(
29 net::URLRequest* request, RequestStage stage,
30 const net::HttpResponseHeaders* original_response_headers)
31 : request(request), stage(stage),
32 original_response_headers(original_response_headers) {}
33 net::URLRequest* request;
34 RequestStage stage;
35 // Additional information about requests that is not
36 // available in all request stages.
37 const net::HttpResponseHeaders* original_response_headers;
38 };
39
21 // Representation of a condition in the Declarative WebRequest API. A condition 40 // Representation of a condition in the Declarative WebRequest API. A condition
22 // consists of several attributes. Each of these attributes needs to be 41 // consists of several attributes. Each of these attributes needs to be
23 // fulfilled in order for the condition to be fulfilled. 42 // fulfilled in order for the condition to be fulfilled.
24 // 43 //
25 // We distinguish between two types of conditions: 44 // We distinguish between two types of conditions:
26 // - URL Matcher conditions are conditions that test the URL of a request. 45 // - URL Matcher conditions are conditions that test the URL of a request.
27 // These are treated separately because we use a URLMatcher to efficiently 46 // These are treated separately because we use a URLMatcher to efficiently
28 // test many of these conditions in parallel by using some advanced 47 // test many of these conditions in parallel by using some advanced
29 // data structures. The URLMatcher tells us if all URL Matcher conditions 48 // data structures. The URLMatcher tells us if all URL Matcher conditions
30 // are fulfilled for a WebRequestCondition. 49 // are fulfilled for a WebRequestCondition.
31 // - All other conditions are represented as WebRequestConditionAttributes. 50 // - All other conditions are represented as WebRequestConditionAttributes.
32 // These conditions are probed linearly (only if the URL Matcher found a hit). 51 // These conditions are probed linearly (only if the URL Matcher found a hit).
33 // 52 //
34 // TODO(battre) Consider making the URLMatcher an owner of the 53 // TODO(battre) Consider making the URLMatcher an owner of the
35 // URLMatcherConditionSet and only pass a pointer to URLMatcherConditionSet 54 // URLMatcherConditionSet and only pass a pointer to URLMatcherConditionSet
36 // in url_matcher_condition_set(). This saves some copying in 55 // in url_matcher_condition_set(). This saves some copying in
37 // WebRequestConditionSet::GetURLMatcherConditionSets. 56 // WebRequestConditionSet::GetURLMatcherConditionSets.
38 class WebRequestCondition { 57 class WebRequestCondition {
39 public: 58 public:
59 typedef DeclarativeWebRequestData MatchData;
60
40 WebRequestCondition( 61 WebRequestCondition(
41 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, 62 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions,
42 const WebRequestConditionAttributes& condition_attributes); 63 const WebRequestConditionAttributes& condition_attributes);
43 ~WebRequestCondition(); 64 ~WebRequestCondition();
44 65
45 // Factory method that instantiates a WebRequestCondition according to 66 // Factory method that instantiates a WebRequestCondition according to
46 // the description |condition| passed by the extension API. 67 // the description |condition| passed by the extension API.
47 static scoped_ptr<WebRequestCondition> Create( 68 static scoped_ptr<WebRequestCondition> Create(
48 URLMatcherConditionFactory* url_matcher_condition_factory, 69 URLMatcherConditionFactory* url_matcher_condition_factory,
49 const base::Value& condition, 70 const base::Value& condition,
50 std::string* error); 71 std::string* error);
51 72
52 // Returns whether the request matches this condition. |url_matches| lists 73 // Returns whether the request matches this condition. |url_matches| lists
53 // the IDs that match the request's URL. 74 // the IDs that match the request's URL.
54 bool IsFulfilled(const std::set<URLMatcherConditionSet::ID> &url_matches, 75 bool IsFulfilled(const std::set<URLMatcherConditionSet::ID> &url_matches,
55 const WebRequestRule::RequestData &request_data) const; 76 const DeclarativeWebRequestData& request_data) const;
56 77
57 // Returns a URLMatcherConditionSet::ID which is the canonical representation 78 // True if this condition has a url filter.
58 // for all URL patterns that need to be matched by this WebRequestCondition. 79 bool has_url_matcher_condition_set() const {
59 // This ID is registered in a URLMatcher that can inform us in case of a 80 return url_matcher_conditions_ != NULL;
60 // match.
61 URLMatcherConditionSet::ID url_matcher_condition_set_id() const {
62 DCHECK(url_matcher_conditions_.get());
63 return url_matcher_conditions_->id();
64 } 81 }
65 82
66 // Returns the set of conditions that are checked on the URL. May be NULL. 83 // If this Condition has a url filter, appends it to |condition_sets|.
67 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set() const { 84 void GetURLMatcherConditionSets(
68 return url_matcher_conditions_; 85 URLMatcherConditionSet::Vector* condition_sets) const {
86 if (url_matcher_conditions_)
87 condition_sets->push_back(url_matcher_conditions_);
69 } 88 }
70 89
71 // Returns the condition attributes checked by this condition. 90 // Returns the condition attributes checked by this condition.
72 const WebRequestConditionAttributes condition_attributes() const { 91 const WebRequestConditionAttributes condition_attributes() const {
73 return condition_attributes_; 92 return condition_attributes_;
74 } 93 }
75 94
76 // Returns a bit vector representing extensions::RequestStage. The bit vector 95 // Returns a bit vector representing extensions::RequestStage. The bit vector
77 // contains a 1 for each request stage during which the condition can be 96 // contains a 1 for each request stage during which the condition can be
78 // tested. 97 // tested.
79 int stages() const { return applicable_request_stages_; } 98 int stages() const { return applicable_request_stages_; }
80 99
81 private: 100 private:
82 // Represents the 'url' attribute of this condition. If NULL, then there was 101 // Represents the 'url' attribute of this condition. If NULL, then there was
83 // no 'url' attribute in this condition. 102 // no 'url' attribute in this condition.
84 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions_; 103 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions_;
85 104
86 // All non-UrlFilter attributes of this condition. 105 // All non-UrlFilter attributes of this condition.
87 WebRequestConditionAttributes condition_attributes_; 106 WebRequestConditionAttributes condition_attributes_;
88 107
89 // Bit vector indicating all RequestStage during which all 108 // Bit vector indicating all RequestStage during which all
90 // |condition_attributes_| can be evaluated. 109 // |condition_attributes_| can be evaluated.
91 int applicable_request_stages_; 110 int applicable_request_stages_;
92 111
93 DISALLOW_COPY_AND_ASSIGN(WebRequestCondition); 112 DISALLOW_COPY_AND_ASSIGN(WebRequestCondition);
94 }; 113 };
95 114
96 // This class stores a set of conditions that may be part of a WebRequestRule. 115 typedef DeclarativeConditionSet<WebRequestCondition> WebRequestConditionSet;
97 // If any condition is fulfilled, the WebRequestActions of the WebRequestRule
98 // can be triggered.
99 class WebRequestConditionSet {
100 public:
101 typedef std::vector<linked_ptr<json_schema_compiler::any::Any> > AnyVector;
102 typedef std::vector<linked_ptr<WebRequestCondition> > Conditions;
103
104 ~WebRequestConditionSet();
105
106 // Factory method that creates an WebRequestConditionSet according to the JSON
107 // array |conditions| passed by the extension API.
108 // Sets |error| and returns NULL in case of an error.
109 static scoped_ptr<WebRequestConditionSet> Create(
110 URLMatcherConditionFactory* url_matcher_condition_factory,
111 const AnyVector& conditions,
112 std::string* error);
113
114 const Conditions& conditions() const {
115 return conditions_;
116 }
117
118 // If |url_match_trigger| is a member of |url_matches|, then this returns
119 // whether the corresponding condition is fulfilled wrt. |request_data|. If
120 // |url_match_trigger| is -1, this function returns whether any of the
121 // conditions without URL attributes is satisfied.
122 bool IsFulfilled(
123 URLMatcherConditionSet::ID url_match_trigger,
124 const std::set<URLMatcherConditionSet::ID>& url_matches,
125 const WebRequestRule::RequestData& request_data) const;
126
127 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|.
128 void GetURLMatcherConditionSets(
129 URLMatcherConditionSet::Vector* condition_sets) const;
130
131 // Returns whether there are some conditions without UrlFilter attributes.
132 bool HasConditionsWithoutUrls() const;
133
134 private:
135 typedef std::map<URLMatcherConditionSet::ID, const WebRequestCondition*>
136 URLMatcherIdToCondition;
137
138 WebRequestConditionSet(
139 const Conditions& conditions,
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_;
146
147 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionSet);
148 };
149 116
150 } // namespace extensions 117 } // namespace extensions
151 118
152 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI TION_H_ 119 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI TION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698