Chromium Code Reviews| 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 // DeclarativeWebRequestData | |
| 44 // | |
| 45 | |
| 46 DeclarativeWebRequestData::DeclarativeWebRequestData(net::URLRequest* request, | |
| 47 RequestStage stage) | |
| 48 : request(request), stage(stage), original_response_headers(NULL) {} | |
| 49 | |
| 50 DeclarativeWebRequestData::DeclarativeWebRequestData( | |
| 51 net::URLRequest* request, | |
| 52 RequestStage stage, | |
| 53 const net::HttpResponseHeaders* original_response_headers) | |
| 54 : request(request), | |
| 55 stage(stage), | |
| 56 original_response_headers(original_response_headers) {} | |
| 57 | |
| 58 DeclarativeWebRequestData::~DeclarativeWebRequestData() {} | |
| 59 | |
| 60 // | |
| 43 // WebRequestCondition | 61 // WebRequestCondition |
| 44 // | 62 // |
| 45 | 63 |
| 46 WebRequestCondition::WebRequestCondition( | 64 WebRequestCondition::WebRequestCondition( |
| 47 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, | 65 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, |
| 66 scoped_refptr<URLMatcherConditionSet> first_party_url_matcher_conditions, | |
| 48 const WebRequestConditionAttributes& condition_attributes) | 67 const WebRequestConditionAttributes& condition_attributes) |
| 49 : url_matcher_conditions_(url_matcher_conditions), | 68 : url_matcher_conditions_(url_matcher_conditions), |
| 69 first_party_url_matcher_conditions_(first_party_url_matcher_conditions), | |
| 50 condition_attributes_(condition_attributes), | 70 condition_attributes_(condition_attributes), |
| 51 applicable_request_stages_(~0) { | 71 applicable_request_stages_(~0) { |
| 52 for (WebRequestConditionAttributes::const_iterator i = | 72 for (WebRequestConditionAttributes::const_iterator i = |
| 53 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { | 73 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { |
| 54 applicable_request_stages_ &= (*i)->GetStages(); | 74 applicable_request_stages_ &= (*i)->GetStages(); |
| 55 } | 75 } |
| 56 } | 76 } |
| 57 | 77 |
| 58 WebRequestCondition::~WebRequestCondition() {} | 78 WebRequestCondition::~WebRequestCondition() {} |
| 59 | 79 |
| 60 bool WebRequestCondition::IsFulfilled( | 80 bool WebRequestCondition::IsFulfilled( |
| 61 const std::set<URLMatcherConditionSet::ID>& url_matches, | |
| 62 const DeclarativeWebRequestData& request_data) const { | 81 const DeclarativeWebRequestData& request_data) const { |
| 63 if (!(request_data.stage & applicable_request_stages_)) { | 82 if (!(request_data.stage & applicable_request_stages_)) { |
| 64 // A condition that cannot be evaluated is considered as violated. | 83 // A condition that cannot be evaluated is considered as violated. |
| 65 return false; | 84 return false; |
| 66 } | 85 } |
| 67 | 86 |
| 68 // Check a UrlFilter attribute if present. | 87 // Check URL attributes if present. |
| 69 if (url_matcher_conditions_.get() && | 88 if ((url_matcher_conditions_.get() && |
| 70 !ContainsKey(url_matches, url_matcher_conditions_->id())) | 89 !ContainsKey(request_data.url_match_ids, |
| 90 url_matcher_conditions_->id())) || | |
|
Jeffrey Yasskin
2013/01/24 00:39:17
nit: I would generally split ||'ed conditions like
vabr (Chromium)
2013/01/24 18:24:08
Done. I agree, it's more readable, and since the b
| |
| 91 (first_party_url_matcher_conditions_.get() && | |
| 92 !ContainsKey(request_data.first_party_url_match_ids, | |
| 93 first_party_url_matcher_conditions_->id()))) | |
| 71 return false; | 94 return false; |
| 72 | 95 |
| 73 // All condition attributes must be fulfilled for a fulfilled condition. | 96 // All condition attributes must be fulfilled for a fulfilled condition. |
| 74 for (WebRequestConditionAttributes::const_iterator i = | 97 for (WebRequestConditionAttributes::const_iterator i = |
| 75 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { | 98 condition_attributes_.begin(); |
| 99 i != condition_attributes_.end(); ++i) { | |
| 76 if (!(*i)->IsFulfilled(request_data)) | 100 if (!(*i)->IsFulfilled(request_data)) |
| 77 return false; | 101 return false; |
| 78 } | 102 } |
| 79 return true; | 103 return true; |
| 80 } | 104 } |
| 81 | 105 |
| 106 void WebRequestCondition::GetURLMatcherConditionSets( | |
| 107 URLMatcherConditionSet::Vector* condition_sets) const { | |
| 108 if (url_matcher_conditions_) | |
| 109 condition_sets->push_back(url_matcher_conditions_); | |
| 110 if (first_party_url_matcher_conditions_) | |
| 111 condition_sets->push_back(first_party_url_matcher_conditions_); | |
| 112 } | |
| 113 | |
| 82 // static | 114 // static |
| 83 scoped_ptr<WebRequestCondition> WebRequestCondition::Create( | 115 scoped_ptr<WebRequestCondition> WebRequestCondition::Create( |
| 84 URLMatcherConditionFactory* url_matcher_condition_factory, | 116 URLMatcherConditionFactory* url_matcher_condition_factory, |
| 85 const base::Value& condition, | 117 const base::Value& condition, |
| 86 std::string* error) { | 118 std::string* error) { |
| 87 const base::DictionaryValue* condition_dict = NULL; | 119 const base::DictionaryValue* condition_dict = NULL; |
| 88 if (!condition.GetAsDictionary(&condition_dict)) { | 120 if (!condition.GetAsDictionary(&condition_dict)) { |
| 89 *error = kExpectedDictionary; | 121 *error = kExpectedDictionary; |
| 90 return scoped_ptr<WebRequestCondition>(NULL); | 122 return scoped_ptr<WebRequestCondition>(NULL); |
| 91 } | 123 } |
| 92 | 124 |
| 93 // Verify that we are dealing with a Condition whose type we understand. | 125 // Verify that we are dealing with a Condition whose type we understand. |
| 94 std::string instance_type; | 126 std::string instance_type; |
| 95 if (!condition_dict->GetString(keys::kInstanceTypeKey, &instance_type)) { | 127 if (!condition_dict->GetString(keys::kInstanceTypeKey, &instance_type)) { |
| 96 *error = kConditionWithoutInstanceType; | 128 *error = kConditionWithoutInstanceType; |
| 97 return scoped_ptr<WebRequestCondition>(NULL); | 129 return scoped_ptr<WebRequestCondition>(NULL); |
| 98 } | 130 } |
| 99 if (instance_type != keys::kRequestMatcherType) { | 131 if (instance_type != keys::kRequestMatcherType) { |
| 100 *error = kExpectedOtherConditionType; | 132 *error = kExpectedOtherConditionType; |
| 101 return scoped_ptr<WebRequestCondition>(NULL); | 133 return scoped_ptr<WebRequestCondition>(NULL); |
| 102 } | 134 } |
| 103 | 135 |
| 104 WebRequestConditionAttributes attributes; | 136 WebRequestConditionAttributes attributes; |
| 105 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set; | 137 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set; |
| 138 scoped_refptr<URLMatcherConditionSet> first_party_url_matcher_condition_set; | |
| 106 | 139 |
| 107 for (base::DictionaryValue::Iterator iter(*condition_dict); | 140 for (base::DictionaryValue::Iterator iter(*condition_dict); |
| 108 iter.HasNext(); iter.Advance()) { | 141 iter.HasNext(); iter.Advance()) { |
| 109 const std::string& condition_attribute_name = iter.key(); | 142 const std::string& condition_attribute_name = iter.key(); |
| 110 const Value& condition_attribute_value = iter.value(); | 143 const Value& condition_attribute_value = iter.value(); |
| 144 const bool name_is_url = condition_attribute_name == keys::kUrlKey; | |
| 111 if (condition_attribute_name == keys::kInstanceTypeKey) { | 145 if (condition_attribute_name == keys::kInstanceTypeKey) { |
| 112 // Skip this. | 146 // Skip this. |
| 113 } else if (condition_attribute_name == keys::kUrlKey) { | 147 } else if (name_is_url || |
| 148 condition_attribute_name == keys::kFirstPartyForCookiesUrlKey) { | |
| 114 const base::DictionaryValue* dict = NULL; | 149 const base::DictionaryValue* dict = NULL; |
| 115 if (!condition_attribute_value.GetAsDictionary(&dict)) { | 150 if (!condition_attribute_value.GetAsDictionary(&dict)) { |
| 116 *error = base::StringPrintf(kInvalidTypeOfParamter, | 151 *error = base::StringPrintf(kInvalidTypeOfParamter, |
| 117 condition_attribute_name.c_str()); | 152 condition_attribute_name.c_str()); |
| 118 } else { | 153 } else { |
| 119 url_matcher_condition_set = | 154 if (!name_is_url) { |
|
Jeffrey Yasskin
2013/01/24 00:39:17
To avoid double-negatives (the else block happens
vabr (Chromium)
2013/01/24 18:24:08
Done.
| |
| 120 URLMatcherFactory::CreateFromURLFilterDictionary( | 155 first_party_url_matcher_condition_set = |
| 121 url_matcher_condition_factory, dict, ++g_next_id, error); | 156 URLMatcherFactory::CreateFromURLFilterDictionary( |
| 157 url_matcher_condition_factory, dict, ++g_next_id, error); | |
| 158 } else { | |
| 159 url_matcher_condition_set = | |
| 160 URLMatcherFactory::CreateFromURLFilterDictionary( | |
| 161 url_matcher_condition_factory, dict, ++g_next_id, error); | |
| 162 } | |
| 122 } | 163 } |
| 123 } else if (WebRequestConditionAttribute::IsKnownType( | 164 } else if (WebRequestConditionAttribute::IsKnownType( |
| 124 condition_attribute_name)) { | 165 condition_attribute_name)) { |
| 125 scoped_ptr<WebRequestConditionAttribute> attribute = | 166 scoped_ptr<WebRequestConditionAttribute> attribute = |
| 126 WebRequestConditionAttribute::Create( | 167 WebRequestConditionAttribute::Create( |
| 127 condition_attribute_name, | 168 condition_attribute_name, |
| 128 &condition_attribute_value, | 169 &condition_attribute_value, |
| 129 error); | 170 error); |
| 130 if (attribute.get()) | 171 if (attribute.get()) |
| 131 attributes.push_back(make_linked_ptr(attribute.release())); | 172 attributes.push_back(make_linked_ptr(attribute.release())); |
| 132 } else { | 173 } else { |
| 133 *error = base::StringPrintf(kUnknownConditionAttribute, | 174 *error = base::StringPrintf(kUnknownConditionAttribute, |
| 134 condition_attribute_name.c_str()); | 175 condition_attribute_name.c_str()); |
| 135 } | 176 } |
| 136 if (!error->empty()) | 177 if (!error->empty()) |
| 137 return scoped_ptr<WebRequestCondition>(NULL); | 178 return scoped_ptr<WebRequestCondition>(NULL); |
| 138 } | 179 } |
| 139 | 180 |
| 140 scoped_ptr<WebRequestCondition> result( | 181 scoped_ptr<WebRequestCondition> result( |
| 141 new WebRequestCondition(url_matcher_condition_set, attributes)); | 182 new WebRequestCondition(url_matcher_condition_set, |
| 183 first_party_url_matcher_condition_set, | |
| 184 attributes)); | |
| 142 | 185 |
| 143 if (!result->stages()) { | 186 if (!result->stages()) { |
| 144 *error = kConditionCannotBeFulfilled; | 187 *error = kConditionCannotBeFulfilled; |
| 145 return scoped_ptr<WebRequestCondition>(NULL); | 188 return scoped_ptr<WebRequestCondition>(NULL); |
| 146 } | 189 } |
| 147 | 190 |
| 148 return result.Pass(); | 191 return result.Pass(); |
| 149 } | 192 } |
| 150 | 193 |
| 151 } // namespace extensions | 194 } // namespace extensions |
| OLD | NEW |