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 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 const char kConditionCannotBeFulfilled[] = "A condition can never be " | 33 const char kConditionCannotBeFulfilled[] = "A condition can never be " |
34 "fulfilled because its attributes cannot all be tested at the " | 34 "fulfilled because its attributes cannot all be tested at the " |
35 "same time in the request life-cycle."; | 35 "same time in the request life-cycle."; |
36 } // namespace | 36 } // namespace |
37 | 37 |
38 namespace extensions { | 38 namespace extensions { |
39 | 39 |
40 namespace keys = declarative_webrequest_constants; | 40 namespace keys = declarative_webrequest_constants; |
41 | 41 |
42 // | 42 // |
| 43 // WebRequestData |
| 44 // |
| 45 |
| 46 WebRequestData::WebRequestData(net::URLRequest* request, RequestStage stage) |
| 47 : request(request), |
| 48 stage(stage), |
| 49 original_response_headers(NULL) {} |
| 50 |
| 51 WebRequestData::WebRequestData( |
| 52 net::URLRequest* request, |
| 53 RequestStage stage, |
| 54 const net::HttpResponseHeaders* original_response_headers) |
| 55 : request(request), |
| 56 stage(stage), |
| 57 original_response_headers(original_response_headers) {} |
| 58 |
| 59 WebRequestData::~WebRequestData() {} |
| 60 |
| 61 // |
| 62 // WebRequestDataWithMatchIds |
| 63 // |
| 64 |
| 65 WebRequestDataWithMatchIds::WebRequestDataWithMatchIds( |
| 66 const WebRequestData* request_data) |
| 67 : data(request_data) {} |
| 68 |
| 69 WebRequestDataWithMatchIds::~WebRequestDataWithMatchIds() {} |
| 70 |
| 71 // |
43 // WebRequestCondition | 72 // WebRequestCondition |
44 // | 73 // |
45 | 74 |
46 WebRequestCondition::WebRequestCondition( | 75 WebRequestCondition::WebRequestCondition( |
47 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, | 76 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, |
| 77 scoped_refptr<URLMatcherConditionSet> first_party_url_matcher_conditions, |
48 const WebRequestConditionAttributes& condition_attributes) | 78 const WebRequestConditionAttributes& condition_attributes) |
49 : url_matcher_conditions_(url_matcher_conditions), | 79 : url_matcher_conditions_(url_matcher_conditions), |
| 80 first_party_url_matcher_conditions_(first_party_url_matcher_conditions), |
50 condition_attributes_(condition_attributes), | 81 condition_attributes_(condition_attributes), |
51 applicable_request_stages_(~0) { | 82 applicable_request_stages_(~0) { |
52 for (WebRequestConditionAttributes::const_iterator i = | 83 for (WebRequestConditionAttributes::const_iterator i = |
53 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { | 84 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { |
54 applicable_request_stages_ &= (*i)->GetStages(); | 85 applicable_request_stages_ &= (*i)->GetStages(); |
55 } | 86 } |
56 } | 87 } |
57 | 88 |
58 WebRequestCondition::~WebRequestCondition() {} | 89 WebRequestCondition::~WebRequestCondition() {} |
59 | 90 |
60 bool WebRequestCondition::IsFulfilled( | 91 bool WebRequestCondition::IsFulfilled( |
61 const std::set<URLMatcherConditionSet::ID>& url_matches, | 92 const MatchData& request_data) const { |
62 const DeclarativeWebRequestData& request_data) const { | 93 if (!(request_data.data->stage & applicable_request_stages_)) { |
63 if (!(request_data.stage & applicable_request_stages_)) { | |
64 // A condition that cannot be evaluated is considered as violated. | 94 // A condition that cannot be evaluated is considered as violated. |
65 return false; | 95 return false; |
66 } | 96 } |
67 | 97 |
68 // Check a UrlFilter attribute if present. | 98 // Check URL attributes if present. |
69 if (url_matcher_conditions_.get() && | 99 if (url_matcher_conditions_.get() && |
70 !ContainsKey(url_matches, url_matcher_conditions_->id())) | 100 !ContainsKey(request_data.url_match_ids, url_matcher_conditions_->id())) |
| 101 return false; |
| 102 if (first_party_url_matcher_conditions_.get() && |
| 103 !ContainsKey(request_data.first_party_url_match_ids, |
| 104 first_party_url_matcher_conditions_->id())) |
71 return false; | 105 return false; |
72 | 106 |
73 // All condition attributes must be fulfilled for a fulfilled condition. | 107 // All condition attributes must be fulfilled for a fulfilled condition. |
74 for (WebRequestConditionAttributes::const_iterator i = | 108 for (WebRequestConditionAttributes::const_iterator i = |
75 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { | 109 condition_attributes_.begin(); |
76 if (!(*i)->IsFulfilled(request_data)) | 110 i != condition_attributes_.end(); ++i) { |
| 111 if (!(*i)->IsFulfilled(*(request_data.data))) |
77 return false; | 112 return false; |
78 } | 113 } |
79 return true; | 114 return true; |
80 } | 115 } |
81 | 116 |
| 117 void WebRequestCondition::GetURLMatcherConditionSets( |
| 118 URLMatcherConditionSet::Vector* condition_sets) const { |
| 119 if (url_matcher_conditions_) |
| 120 condition_sets->push_back(url_matcher_conditions_); |
| 121 if (first_party_url_matcher_conditions_) |
| 122 condition_sets->push_back(first_party_url_matcher_conditions_); |
| 123 } |
| 124 |
82 // static | 125 // static |
83 scoped_ptr<WebRequestCondition> WebRequestCondition::Create( | 126 scoped_ptr<WebRequestCondition> WebRequestCondition::Create( |
84 URLMatcherConditionFactory* url_matcher_condition_factory, | 127 URLMatcherConditionFactory* url_matcher_condition_factory, |
85 const base::Value& condition, | 128 const base::Value& condition, |
86 std::string* error) { | 129 std::string* error) { |
87 const base::DictionaryValue* condition_dict = NULL; | 130 const base::DictionaryValue* condition_dict = NULL; |
88 if (!condition.GetAsDictionary(&condition_dict)) { | 131 if (!condition.GetAsDictionary(&condition_dict)) { |
89 *error = kExpectedDictionary; | 132 *error = kExpectedDictionary; |
90 return scoped_ptr<WebRequestCondition>(NULL); | 133 return scoped_ptr<WebRequestCondition>(NULL); |
91 } | 134 } |
92 | 135 |
93 // Verify that we are dealing with a Condition whose type we understand. | 136 // Verify that we are dealing with a Condition whose type we understand. |
94 std::string instance_type; | 137 std::string instance_type; |
95 if (!condition_dict->GetString(keys::kInstanceTypeKey, &instance_type)) { | 138 if (!condition_dict->GetString(keys::kInstanceTypeKey, &instance_type)) { |
96 *error = kConditionWithoutInstanceType; | 139 *error = kConditionWithoutInstanceType; |
97 return scoped_ptr<WebRequestCondition>(NULL); | 140 return scoped_ptr<WebRequestCondition>(NULL); |
98 } | 141 } |
99 if (instance_type != keys::kRequestMatcherType) { | 142 if (instance_type != keys::kRequestMatcherType) { |
100 *error = kExpectedOtherConditionType; | 143 *error = kExpectedOtherConditionType; |
101 return scoped_ptr<WebRequestCondition>(NULL); | 144 return scoped_ptr<WebRequestCondition>(NULL); |
102 } | 145 } |
103 | 146 |
104 WebRequestConditionAttributes attributes; | 147 WebRequestConditionAttributes attributes; |
105 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set; | 148 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set; |
| 149 scoped_refptr<URLMatcherConditionSet> first_party_url_matcher_condition_set; |
106 | 150 |
107 for (base::DictionaryValue::Iterator iter(*condition_dict); | 151 for (base::DictionaryValue::Iterator iter(*condition_dict); |
108 iter.HasNext(); iter.Advance()) { | 152 iter.HasNext(); iter.Advance()) { |
109 const std::string& condition_attribute_name = iter.key(); | 153 const std::string& condition_attribute_name = iter.key(); |
110 const Value& condition_attribute_value = iter.value(); | 154 const Value& condition_attribute_value = iter.value(); |
| 155 const bool name_is_url = condition_attribute_name == keys::kUrlKey; |
111 if (condition_attribute_name == keys::kInstanceTypeKey) { | 156 if (condition_attribute_name == keys::kInstanceTypeKey) { |
112 // Skip this. | 157 // Skip this. |
113 } else if (condition_attribute_name == keys::kUrlKey) { | 158 } else if (name_is_url || |
| 159 condition_attribute_name == keys::kFirstPartyForCookiesUrlKey) { |
114 const base::DictionaryValue* dict = NULL; | 160 const base::DictionaryValue* dict = NULL; |
115 if (!condition_attribute_value.GetAsDictionary(&dict)) { | 161 if (!condition_attribute_value.GetAsDictionary(&dict)) { |
116 *error = base::StringPrintf(kInvalidTypeOfParamter, | 162 *error = base::StringPrintf(kInvalidTypeOfParamter, |
117 condition_attribute_name.c_str()); | 163 condition_attribute_name.c_str()); |
118 } else { | 164 } else { |
119 url_matcher_condition_set = | 165 if (name_is_url) { |
120 URLMatcherFactory::CreateFromURLFilterDictionary( | 166 url_matcher_condition_set = |
121 url_matcher_condition_factory, dict, ++g_next_id, error); | 167 URLMatcherFactory::CreateFromURLFilterDictionary( |
| 168 url_matcher_condition_factory, dict, ++g_next_id, error); |
| 169 } else { |
| 170 first_party_url_matcher_condition_set = |
| 171 URLMatcherFactory::CreateFromURLFilterDictionary( |
| 172 url_matcher_condition_factory, dict, ++g_next_id, error); |
| 173 } |
122 } | 174 } |
123 } else if (WebRequestConditionAttribute::IsKnownType( | 175 } else if (WebRequestConditionAttribute::IsKnownType( |
124 condition_attribute_name)) { | 176 condition_attribute_name)) { |
125 scoped_ptr<WebRequestConditionAttribute> attribute = | 177 scoped_ptr<WebRequestConditionAttribute> attribute = |
126 WebRequestConditionAttribute::Create( | 178 WebRequestConditionAttribute::Create( |
127 condition_attribute_name, | 179 condition_attribute_name, |
128 &condition_attribute_value, | 180 &condition_attribute_value, |
129 error); | 181 error); |
130 if (attribute.get()) | 182 if (attribute.get()) |
131 attributes.push_back(make_linked_ptr(attribute.release())); | 183 attributes.push_back(make_linked_ptr(attribute.release())); |
132 } else { | 184 } else { |
133 *error = base::StringPrintf(kUnknownConditionAttribute, | 185 *error = base::StringPrintf(kUnknownConditionAttribute, |
134 condition_attribute_name.c_str()); | 186 condition_attribute_name.c_str()); |
135 } | 187 } |
136 if (!error->empty()) | 188 if (!error->empty()) |
137 return scoped_ptr<WebRequestCondition>(NULL); | 189 return scoped_ptr<WebRequestCondition>(NULL); |
138 } | 190 } |
139 | 191 |
140 scoped_ptr<WebRequestCondition> result( | 192 scoped_ptr<WebRequestCondition> result( |
141 new WebRequestCondition(url_matcher_condition_set, attributes)); | 193 new WebRequestCondition(url_matcher_condition_set, |
| 194 first_party_url_matcher_condition_set, |
| 195 attributes)); |
142 | 196 |
143 if (!result->stages()) { | 197 if (!result->stages()) { |
144 *error = kConditionCannotBeFulfilled; | 198 *error = kConditionCannotBeFulfilled; |
145 return scoped_ptr<WebRequestCondition>(NULL); | 199 return scoped_ptr<WebRequestCondition>(NULL); |
146 } | 200 } |
147 | 201 |
148 return result.Pass(); | 202 return result.Pass(); |
149 } | 203 } |
150 | 204 |
151 } // namespace extensions | 205 } // namespace extensions |
OLD | NEW |