| 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/search_engines/template_url_parser.h" | 5 #include "chrome/browser/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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 | 137 |
| 138 static void StartElementImpl(void* ctx, | 138 static void StartElementImpl(void* ctx, |
| 139 const xmlChar* name, | 139 const xmlChar* name, |
| 140 const xmlChar** atts); | 140 const xmlChar** atts); |
| 141 static void EndElementImpl(void* ctx, const xmlChar* name); | 141 static void EndElementImpl(void* ctx, const xmlChar* name); |
| 142 static void CharactersImpl(void* ctx, const xmlChar* ch, int len); | 142 static void CharactersImpl(void* ctx, const xmlChar* ch, int len); |
| 143 | 143 |
| 144 // Returns a heap-allocated TemplateURL representing the result of parsing. | 144 // Returns a heap-allocated TemplateURL representing the result of parsing. |
| 145 // This will be NULL if parsing failed or if the results were invalid for some | 145 // This will be NULL if parsing failed or if the results were invalid for some |
| 146 // reason (e.g. the resulting URL was not HTTP[S], a name wasn't supplied, | 146 // reason (e.g. the resulting URL was not HTTP[S], a name wasn't supplied, |
| 147 // etc.). | 147 // a resulting TemplateURLRef was invalid, etc.). |
| 148 TemplateURL* GetTemplateURL(Profile* profile, bool show_in_default_list); | 148 TemplateURL* GetTemplateURL(Profile* profile, bool show_in_default_list); |
| 149 | 149 |
| 150 private: | 150 private: |
| 151 // Key is UTF8 encoded. | 151 // Key is UTF8 encoded. |
| 152 typedef std::map<std::string, ElementType> ElementNameToElementTypeMap; | 152 typedef std::map<std::string, ElementType> ElementNameToElementTypeMap; |
| 153 | 153 |
| 154 static void InitMapping(); | 154 static void InitMapping(); |
| 155 | 155 |
| 156 void ParseURL(const xmlChar** atts); | 156 void ParseURL(const xmlChar** atts); |
| 157 void ParseImage(const xmlChar** atts); | 157 void ParseImage(const xmlChar** atts); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 void TemplateURLParsingContext::CharactersImpl(void* ctx, | 284 void TemplateURLParsingContext::CharactersImpl(void* ctx, |
| 285 const xmlChar* ch, | 285 const xmlChar* ch, |
| 286 int len) { | 286 int len) { |
| 287 reinterpret_cast<TemplateURLParsingContext*>(ctx)->string_ += | 287 reinterpret_cast<TemplateURLParsingContext*>(ctx)->string_ += |
| 288 UTF8ToUTF16(std::string(reinterpret_cast<const char*>(ch), len)); | 288 UTF8ToUTF16(std::string(reinterpret_cast<const char*>(ch), len)); |
| 289 } | 289 } |
| 290 | 290 |
| 291 TemplateURL* TemplateURLParsingContext::GetTemplateURL( | 291 TemplateURL* TemplateURLParsingContext::GetTemplateURL( |
| 292 Profile* profile, | 292 Profile* profile, |
| 293 bool show_in_default_list) { | 293 bool show_in_default_list) { |
| 294 // Basic legality checks. | 294 // TODO(jcampan): Support engines that use POST; see http://crbug.com/18107 |
| 295 if (data_.short_name.empty() || !IsHTTPRef(data_.url()) || | 295 if (method_ == TemplateURLParsingContext::POST || data_.short_name.empty() || |
| 296 !IsHTTPRef(data_.suggestions_url)) | 296 !IsHTTPRef(data_.url()) || !IsHTTPRef(data_.suggestions_url)) |
| 297 return NULL; | |
| 298 | |
| 299 // If the image was a data URL, use the favicon from the search URL instead. | |
| 300 // (see TODO inEndElementImpl()). | |
| 301 GURL url(data_.url()); | |
| 302 if (derive_image_from_url_ && data_.favicon_url.is_empty()) | |
| 303 data_.favicon_url = TemplateURL::GenerateFaviconURL(url); | |
| 304 | |
| 305 // TODO(jcampan): http://b/issue?id=1196285 we do not support search engines | |
| 306 // that use POST yet. | |
| 307 if (method_ == TemplateURLParsingContext::POST) | |
| 308 return NULL; | 297 return NULL; |
| 309 if (suggestion_method_ == TemplateURLParsingContext::POST) | 298 if (suggestion_method_ == TemplateURLParsingContext::POST) |
| 310 data_.suggestions_url.clear(); | 299 data_.suggestions_url.clear(); |
| 311 | 300 |
| 312 data_.SetKeyword(TemplateURLService::GenerateKeyword(url)); | 301 // If the image was a data URL, use the favicon from the search URL instead. |
| 302 // (see the TODO in EndElementImpl()). |
| 303 GURL search_url(data_.url()); |
| 304 if (derive_image_from_url_ && data_.favicon_url.is_empty()) |
| 305 data_.favicon_url = TemplateURL::GenerateFaviconURL(search_url); |
| 306 |
| 307 data_.SetKeyword(TemplateURLService::GenerateKeyword(search_url)); |
| 313 data_.show_in_default_list = show_in_default_list; | 308 data_.show_in_default_list = show_in_default_list; |
| 314 return new TemplateURL(profile, data_); | 309 |
| 310 // Bail if the search URL is empty or if either TemplateURLRef is invalid. |
| 311 scoped_ptr<TemplateURL> template_url(new TemplateURL(profile, data_)); |
| 312 if (template_url->url().empty() || !template_url->url_ref().IsValid() || |
| 313 (!template_url->suggestions_url().empty() && |
| 314 !template_url->suggestions_url_ref().IsValid())) |
| 315 return NULL; |
| 316 |
| 317 return template_url.release(); |
| 315 } | 318 } |
| 316 | 319 |
| 317 // static | 320 // static |
| 318 void TemplateURLParsingContext::InitMapping() { | 321 void TemplateURLParsingContext::InitMapping() { |
| 319 kElementNameToElementTypeMap = new std::map<std::string, ElementType>; | 322 kElementNameToElementTypeMap = new std::map<std::string, ElementType>; |
| 320 (*kElementNameToElementTypeMap)[kURLElement] = URL; | 323 (*kElementNameToElementTypeMap)[kURLElement] = URL; |
| 321 (*kElementNameToElementTypeMap)[kParamElement] = PARAM; | 324 (*kElementNameToElementTypeMap)[kParamElement] = PARAM; |
| 322 (*kElementNameToElementTypeMap)[kShortNameElement] = SHORT_NAME; | 325 (*kElementNameToElementTypeMap)[kShortNameElement] = SHORT_NAME; |
| 323 (*kElementNameToElementTypeMap)[kImageElement] = IMAGE; | 326 (*kElementNameToElementTypeMap)[kImageElement] = IMAGE; |
| 324 (*kElementNameToElementTypeMap)[kOpenSearchDescriptionElement] = | 327 (*kElementNameToElementTypeMap)[kOpenSearchDescriptionElement] = |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 // & . Unfortunately xmlSubstituteEntitiesDefault affects global state. | 481 // & . Unfortunately xmlSubstituteEntitiesDefault affects global state. |
| 479 // If this becomes problematic we'll need to provide our own entity | 482 // If this becomes problematic we'll need to provide our own entity |
| 480 // type for &, or strip out & by hand after parsing. | 483 // type for &, or strip out & by hand after parsing. |
| 481 int last_sub_entities_value = xmlSubstituteEntitiesDefault(1); | 484 int last_sub_entities_value = xmlSubstituteEntitiesDefault(1); |
| 482 TemplateURLParsingContext context(param_filter); | 485 TemplateURLParsingContext context(param_filter); |
| 483 xmlSAXHandler sax_handler; | 486 xmlSAXHandler sax_handler; |
| 484 memset(&sax_handler, 0, sizeof(sax_handler)); | 487 memset(&sax_handler, 0, sizeof(sax_handler)); |
| 485 sax_handler.startElement = &TemplateURLParsingContext::StartElementImpl; | 488 sax_handler.startElement = &TemplateURLParsingContext::StartElementImpl; |
| 486 sax_handler.endElement = &TemplateURLParsingContext::EndElementImpl; | 489 sax_handler.endElement = &TemplateURLParsingContext::EndElementImpl; |
| 487 sax_handler.characters = &TemplateURLParsingContext::CharactersImpl; | 490 sax_handler.characters = &TemplateURLParsingContext::CharactersImpl; |
| 488 xmlSAXUserParseMemory(&sax_handler, &context, data, static_cast<int>(length)); | 491 int error = xmlSAXUserParseMemory(&sax_handler, &context, data, |
| 492 static_cast<int>(length)); |
| 489 xmlSubstituteEntitiesDefault(last_sub_entities_value); | 493 xmlSubstituteEntitiesDefault(last_sub_entities_value); |
| 490 | 494 |
| 491 return context.GetTemplateURL(profile, show_in_default_list); | 495 return error ? NULL : context.GetTemplateURL(profile, show_in_default_list); |
| 492 } | 496 } |
| OLD | NEW |