| 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_attribute.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 WebRequestConditionAttributeResourceType::GetType() const { | 137 WebRequestConditionAttributeResourceType::GetType() const { |
| 138 return CONDITION_RESOURCE_TYPE; | 138 return CONDITION_RESOURCE_TYPE; |
| 139 } | 139 } |
| 140 | 140 |
| 141 // | 141 // |
| 142 // WebRequestConditionAttributeContentType | 142 // WebRequestConditionAttributeContentType |
| 143 // | 143 // |
| 144 | 144 |
| 145 WebRequestConditionAttributeContentType:: | 145 WebRequestConditionAttributeContentType:: |
| 146 WebRequestConditionAttributeContentType( | 146 WebRequestConditionAttributeContentType( |
| 147 const std::vector<std::string>& content_types) | 147 const std::vector<std::string>& content_types, |
| 148 : content_types_(content_types) {} | 148 bool inclusive) |
| 149 : content_types_(content_types), |
| 150 inclusive_(inclusive) {} |
| 149 | 151 |
| 150 WebRequestConditionAttributeContentType:: | 152 WebRequestConditionAttributeContentType:: |
| 151 ~WebRequestConditionAttributeContentType() {} | 153 ~WebRequestConditionAttributeContentType() {} |
| 152 | 154 |
| 153 // static | 155 // static |
| 154 bool WebRequestConditionAttributeContentType::IsMatchingType( | 156 bool WebRequestConditionAttributeContentType::IsMatchingType( |
| 155 const std::string& instance_type) { | 157 const std::string& instance_type) { |
| 156 return instance_type == keys::kContentTypeKey; | 158 return instance_type == keys::kContentTypeKey || |
| 159 instance_type == keys::kExcludeContentTypeKey; |
| 157 } | 160 } |
| 158 | 161 |
| 159 // static | 162 // static |
| 160 scoped_ptr<WebRequestConditionAttribute> | 163 scoped_ptr<WebRequestConditionAttribute> |
| 161 WebRequestConditionAttributeContentType::Create( | 164 WebRequestConditionAttributeContentType::Create( |
| 162 const std::string& name, | 165 const std::string& name, |
| 163 const base::Value* value, | 166 const base::Value* value, |
| 164 std::string* error) { | 167 std::string* error) { |
| 165 std::vector<std::string> content_types; | 168 DCHECK(IsMatchingType(name)); |
| 166 | 169 |
| 167 const ListValue* value_as_list = NULL; | 170 const ListValue* value_as_list = NULL; |
| 168 if (!value->GetAsList(&value_as_list)) { | 171 if (!value->GetAsList(&value_as_list)) { |
| 169 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, | 172 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, name); |
| 170 keys::kContentTypeKey); | |
| 171 return scoped_ptr<WebRequestConditionAttribute>(NULL); | 173 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
| 172 } | 174 } |
| 173 | 175 std::vector<std::string> content_types; |
| 174 for (ListValue::const_iterator it = value_as_list->begin(); | 176 for (ListValue::const_iterator it = value_as_list->begin(); |
| 175 it != value_as_list->end(); ++it) { | 177 it != value_as_list->end(); ++it) { |
| 176 std::string content_type; | 178 std::string content_type; |
| 177 if (!(*it)->GetAsString(&content_type)) { | 179 if (!(*it)->GetAsString(&content_type)) { |
| 178 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, | 180 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, name); |
| 179 keys::kContentTypeKey); | |
| 180 return scoped_ptr<WebRequestConditionAttribute>(NULL); | 181 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
| 181 } | 182 } |
| 182 content_types.push_back(content_type); | 183 content_types.push_back(content_type); |
| 183 } | 184 } |
| 185 |
| 184 return scoped_ptr<WebRequestConditionAttribute>( | 186 return scoped_ptr<WebRequestConditionAttribute>( |
| 185 new WebRequestConditionAttributeContentType(content_types)); | 187 new WebRequestConditionAttributeContentType( |
| 188 content_types, name == keys::kContentTypeKey)); |
| 186 } | 189 } |
| 187 | 190 |
| 188 int WebRequestConditionAttributeContentType::GetStages() const { | 191 int WebRequestConditionAttributeContentType::GetStages() const { |
| 189 return ON_HEADERS_RECEIVED; | 192 return ON_HEADERS_RECEIVED; |
| 190 } | 193 } |
| 191 | 194 |
| 192 bool WebRequestConditionAttributeContentType::IsFulfilled( | 195 bool WebRequestConditionAttributeContentType::IsFulfilled( |
| 193 const WebRequestRule::RequestData& request_data) { | 196 const WebRequestRule::RequestData& request_data) { |
| 194 if (!(request_data.stage & GetStages())) | 197 if (!(request_data.stage & GetStages())) |
| 195 return false; | 198 return false; |
| 196 std::string content_type; | 199 std::string content_type; |
| 197 request_data.original_response_headers->GetNormalizedHeader( | 200 request_data.original_response_headers->GetNormalizedHeader( |
| 198 net::HttpRequestHeaders::kContentType, &content_type); | 201 net::HttpRequestHeaders::kContentType, &content_type); |
| 199 std::string mime_type; | 202 std::string mime_type; |
| 200 std::string charset; | 203 std::string charset; |
| 201 bool had_charset = false; | 204 bool had_charset = false; |
| 202 net::HttpUtil::ParseContentType( | 205 net::HttpUtil::ParseContentType( |
| 203 content_type, &mime_type, &charset, &had_charset, NULL); | 206 content_type, &mime_type, &charset, &had_charset, NULL); |
| 204 | 207 |
| 205 return std::find(content_types_.begin(), content_types_.end(), | 208 if (inclusive_) { |
| 206 mime_type) != content_types_.end(); | 209 return std::find(content_types_.begin(), content_types_.end(), |
| 210 mime_type) != content_types_.end(); |
| 211 } else { |
| 212 return std::find(content_types_.begin(), content_types_.end(), |
| 213 mime_type) == content_types_.end(); |
| 214 } |
| 207 } | 215 } |
| 208 | 216 |
| 209 WebRequestConditionAttribute::Type | 217 WebRequestConditionAttribute::Type |
| 210 WebRequestConditionAttributeContentType::GetType() const { | 218 WebRequestConditionAttributeContentType::GetType() const { |
| 211 return CONDITION_CONTENT_TYPE; | 219 return CONDITION_CONTENT_TYPE; |
| 212 } | 220 } |
| 213 | 221 |
| 214 } // namespace extensions | 222 } // namespace extensions |
| OLD | NEW |