Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/search_engines/template_url_parser.h" | 5 #include "components/search_engines/template_url_parser.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 // having to convert to wide, then do comparisons. | 27 // having to convert to wide, then do comparisons. |
| 28 | 28 |
| 29 // Defines for element names of the OSD document: | 29 // Defines for element names of the OSD document: |
| 30 const char kURLElement[] = "Url"; | 30 const char kURLElement[] = "Url"; |
| 31 const char kParamElement[] = "Param"; | 31 const char kParamElement[] = "Param"; |
| 32 const char kShortNameElement[] = "ShortName"; | 32 const char kShortNameElement[] = "ShortName"; |
| 33 const char kImageElement[] = "Image"; | 33 const char kImageElement[] = "Image"; |
| 34 const char kOpenSearchDescriptionElement[] = "OpenSearchDescription"; | 34 const char kOpenSearchDescriptionElement[] = "OpenSearchDescription"; |
| 35 const char kFirefoxSearchDescriptionElement[] = "SearchPlugin"; | 35 const char kFirefoxSearchDescriptionElement[] = "SearchPlugin"; |
| 36 const char kInputEncodingElement[] = "InputEncoding"; | 36 const char kInputEncodingElement[] = "InputEncoding"; |
| 37 const char kAliasElement[] = "Alias"; | |
| 37 | 38 |
| 38 // Various XML attributes used. | 39 // Various XML attributes used. |
| 39 const char kURLTypeAttribute[] = "type"; | 40 const char kURLTypeAttribute[] = "type"; |
| 40 const char kURLTemplateAttribute[] = "template"; | 41 const char kURLTemplateAttribute[] = "template"; |
| 41 const char kImageTypeAttribute[] = "type"; | 42 const char kImageTypeAttribute[] = "type"; |
| 42 const char kImageWidthAttribute[] = "width"; | 43 const char kImageWidthAttribute[] = "width"; |
| 43 const char kImageHeightAttribute[] = "height"; | 44 const char kImageHeightAttribute[] = "height"; |
| 44 const char kParamNameAttribute[] = "name"; | 45 const char kParamNameAttribute[] = "name"; |
| 45 const char kParamValueAttribute[] = "value"; | 46 const char kParamValueAttribute[] = "value"; |
| 46 const char kParamMethodAttribute[] = "method"; | 47 const char kParamMethodAttribute[] = "method"; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 public: | 109 public: |
| 109 // Enum of the known element types. | 110 // Enum of the known element types. |
| 110 enum ElementType { | 111 enum ElementType { |
| 111 UNKNOWN, | 112 UNKNOWN, |
| 112 OPEN_SEARCH_DESCRIPTION, | 113 OPEN_SEARCH_DESCRIPTION, |
| 113 URL, | 114 URL, |
| 114 PARAM, | 115 PARAM, |
| 115 SHORT_NAME, | 116 SHORT_NAME, |
| 116 IMAGE, | 117 IMAGE, |
| 117 INPUT_ENCODING, | 118 INPUT_ENCODING, |
| 119 ALIAS | |
|
Peter Kasting
2014/07/28 22:48:31
Nit: Add trailing comma (means future additions wo
Nikhil
2014/07/29 08:29:21
Done.
| |
| 118 }; | 120 }; |
| 119 | 121 |
| 120 enum Method { | 122 enum Method { |
| 121 GET, | 123 GET, |
| 122 POST | 124 POST |
| 123 }; | 125 }; |
| 124 | 126 |
| 125 // Key/value of a Param node. | 127 // Key/value of a Param node. |
| 126 typedef std::pair<std::string, std::string> Param; | 128 typedef std::pair<std::string, std::string> Param; |
| 127 | 129 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 break; | 235 break; |
| 234 } | 236 } |
| 235 context->string_.clear(); | 237 context->string_.clear(); |
| 236 } | 238 } |
| 237 | 239 |
| 238 // static | 240 // static |
| 239 void TemplateURLParsingContext::EndElementImpl(void* ctx, const xmlChar* name) { | 241 void TemplateURLParsingContext::EndElementImpl(void* ctx, const xmlChar* name) { |
| 240 TemplateURLParsingContext* context = | 242 TemplateURLParsingContext* context = |
| 241 reinterpret_cast<TemplateURLParsingContext*>(ctx); | 243 reinterpret_cast<TemplateURLParsingContext*>(ctx); |
| 242 switch (context->GetKnownType()) { | 244 switch (context->GetKnownType()) { |
| 243 case TemplateURLParsingContext::SHORT_NAME: | 245 case TemplateURLParsingContext::SHORT_NAME: |
|
Peter Kasting
2014/07/28 22:48:31
Nit: The order of cases in this switch seems rando
Nikhil
2014/07/29 08:29:21
Done.
| |
| 244 context->data_.short_name = context->string_; | 246 context->data_.short_name = context->string_; |
| 245 break; | 247 break; |
| 248 case TemplateURLParsingContext::ALIAS: | |
| 249 context->data_.SetKeyword(context->string_); | |
| 250 break; | |
| 246 case TemplateURLParsingContext::IMAGE: { | 251 case TemplateURLParsingContext::IMAGE: { |
| 247 GURL image_url(base::UTF16ToUTF8(context->string_)); | 252 GURL image_url(base::UTF16ToUTF8(context->string_)); |
| 248 if (image_url.SchemeIs(url::kDataScheme)) { | 253 if (image_url.SchemeIs(url::kDataScheme)) { |
| 249 // TODO (jcampan): bug 1169256: when dealing with data URL, we need to | 254 // TODO (jcampan): bug 1169256: when dealing with data URL, we need to |
| 250 // decode the data URL in the renderer. For now, we'll just point to the | 255 // decode the data URL in the renderer. For now, we'll just point to the |
| 251 // favicon from the URL. | 256 // favicon from the URL. |
| 252 context->derive_image_from_url_ = true; | 257 context->derive_image_from_url_ = true; |
| 253 } else if (context->image_is_valid_for_favicon_ && image_url.is_valid() && | 258 } else if (context->image_is_valid_for_favicon_ && image_url.is_valid() && |
| 254 (image_url.SchemeIs(url::kHttpScheme) || | 259 (image_url.SchemeIs(url::kHttpScheme) || |
| 255 image_url.SchemeIs(url::kHttpsScheme))) { | 260 image_url.SchemeIs(url::kHttpsScheme))) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 291 return NULL; | 296 return NULL; |
| 292 if (suggestion_method_ == TemplateURLParsingContext::POST) | 297 if (suggestion_method_ == TemplateURLParsingContext::POST) |
| 293 data_.suggestions_url.clear(); | 298 data_.suggestions_url.clear(); |
| 294 | 299 |
| 295 // If the image was a data URL, use the favicon from the search URL instead. | 300 // If the image was a data URL, use the favicon from the search URL instead. |
| 296 // (see the TODO in EndElementImpl()). | 301 // (see the TODO in EndElementImpl()). |
| 297 GURL search_url(data_.url()); | 302 GURL search_url(data_.url()); |
| 298 if (derive_image_from_url_ && data_.favicon_url.is_empty()) | 303 if (derive_image_from_url_ && data_.favicon_url.is_empty()) |
| 299 data_.favicon_url = TemplateURL::GenerateFaviconURL(search_url); | 304 data_.favicon_url = TemplateURL::GenerateFaviconURL(search_url); |
| 300 | 305 |
| 301 data_.SetKeyword(TemplateURL::GenerateKeyword(search_url)); | 306 // When |keyword| is equal to "dummy", it means that no alias was mentioned |
| 307 // in XML file while importing search engines. We generate based on url in | |
| 308 // that case. Else, we use alias mentioned in XML file. | |
|
Peter Kasting
2014/07/28 22:48:31
Nit: Clearer and shorter:
// Generate a keyword
Nikhil
2014/07/29 08:29:21
Done.
| |
| 309 if (base::UTF16ToUTF8(data_.keyword()) == "dummy") | |
|
Peter Kasting
2014/07/28 22:48:31
I'm not a big fan of checking against "dummy", as
Nikhil
2014/07/29 08:29:21
Noted, it could very well be possible. To track th
| |
| 310 data_.SetKeyword(TemplateURL::GenerateKeyword(search_url)); | |
| 311 | |
| 302 data_.show_in_default_list = show_in_default_list; | 312 data_.show_in_default_list = show_in_default_list; |
| 303 | 313 |
| 304 // Bail if the search URL is empty or if either TemplateURLRef is invalid. | 314 // Bail if the search URL is empty or if either TemplateURLRef is invalid. |
| 305 scoped_ptr<TemplateURL> template_url(new TemplateURL(data_)); | 315 scoped_ptr<TemplateURL> template_url(new TemplateURL(data_)); |
| 306 if (template_url->url().empty() || | 316 if (template_url->url().empty() || |
| 307 !template_url->url_ref().IsValid(search_terms_data) || | 317 !template_url->url_ref().IsValid(search_terms_data) || |
| 308 (!template_url->suggestions_url().empty() && | 318 (!template_url->suggestions_url().empty() && |
| 309 !template_url->suggestions_url_ref().IsValid(search_terms_data))) { | 319 !template_url->suggestions_url_ref().IsValid(search_terms_data))) { |
| 310 return NULL; | 320 return NULL; |
| 311 } | 321 } |
| 312 | 322 |
| 313 return template_url.release(); | 323 return template_url.release(); |
| 314 } | 324 } |
| 315 | 325 |
| 316 // static | 326 // static |
| 317 void TemplateURLParsingContext::InitMapping() { | 327 void TemplateURLParsingContext::InitMapping() { |
| 318 kElementNameToElementTypeMap = new std::map<std::string, ElementType>; | 328 kElementNameToElementTypeMap = new std::map<std::string, ElementType>; |
| 319 (*kElementNameToElementTypeMap)[kURLElement] = URL; | 329 (*kElementNameToElementTypeMap)[kURLElement] = URL; |
| 320 (*kElementNameToElementTypeMap)[kParamElement] = PARAM; | 330 (*kElementNameToElementTypeMap)[kParamElement] = PARAM; |
| 321 (*kElementNameToElementTypeMap)[kShortNameElement] = SHORT_NAME; | 331 (*kElementNameToElementTypeMap)[kShortNameElement] = SHORT_NAME; |
| 322 (*kElementNameToElementTypeMap)[kImageElement] = IMAGE; | 332 (*kElementNameToElementTypeMap)[kImageElement] = IMAGE; |
| 323 (*kElementNameToElementTypeMap)[kOpenSearchDescriptionElement] = | 333 (*kElementNameToElementTypeMap)[kOpenSearchDescriptionElement] = |
| 324 OPEN_SEARCH_DESCRIPTION; | 334 OPEN_SEARCH_DESCRIPTION; |
| 325 (*kElementNameToElementTypeMap)[kFirefoxSearchDescriptionElement] = | 335 (*kElementNameToElementTypeMap)[kFirefoxSearchDescriptionElement] = |
| 326 OPEN_SEARCH_DESCRIPTION; | 336 OPEN_SEARCH_DESCRIPTION; |
| 327 (*kElementNameToElementTypeMap)[kInputEncodingElement] = INPUT_ENCODING; | 337 (*kElementNameToElementTypeMap)[kInputEncodingElement] = INPUT_ENCODING; |
| 338 (*kElementNameToElementTypeMap)[kAliasElement] = ALIAS; | |
| 328 } | 339 } |
| 329 | 340 |
| 330 void TemplateURLParsingContext::ParseURL(const xmlChar** atts) { | 341 void TemplateURLParsingContext::ParseURL(const xmlChar** atts) { |
| 331 if (!atts) | 342 if (!atts) |
| 332 return; | 343 return; |
| 333 | 344 |
| 334 std::string template_url; | 345 std::string template_url; |
| 335 bool is_post = false; | 346 bool is_post = false; |
| 336 bool is_html_url = false; | 347 bool is_html_url = false; |
| 337 bool is_suggest_url = false; | 348 bool is_suggest_url = false; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 484 sax_handler.startElement = &TemplateURLParsingContext::StartElementImpl; | 495 sax_handler.startElement = &TemplateURLParsingContext::StartElementImpl; |
| 485 sax_handler.endElement = &TemplateURLParsingContext::EndElementImpl; | 496 sax_handler.endElement = &TemplateURLParsingContext::EndElementImpl; |
| 486 sax_handler.characters = &TemplateURLParsingContext::CharactersImpl; | 497 sax_handler.characters = &TemplateURLParsingContext::CharactersImpl; |
| 487 int error = xmlSAXUserParseMemory(&sax_handler, &context, data, | 498 int error = xmlSAXUserParseMemory(&sax_handler, &context, data, |
| 488 static_cast<int>(length)); | 499 static_cast<int>(length)); |
| 489 xmlSubstituteEntitiesDefault(last_sub_entities_value); | 500 xmlSubstituteEntitiesDefault(last_sub_entities_value); |
| 490 | 501 |
| 491 return error ? | 502 return error ? |
| 492 NULL : context.GetTemplateURL(search_terms_data, show_in_default_list); | 503 NULL : context.GetTemplateURL(search_terms_data, show_in_default_list); |
| 493 } | 504 } |
| OLD | NEW |