OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 // We only expect PARAM nodes under the Url node | 131 // We only expect PARAM nodes under the Url node |
132 if (elements_.size() == 3 && elements_[0] == OPEN_SEARCH_DESCRIPTION && | 132 if (elements_.size() == 3 && elements_[0] == OPEN_SEARCH_DESCRIPTION && |
133 elements_[1] == URL && elements_[2] == PARAM) | 133 elements_[1] == URL && elements_[2] == PARAM) |
134 return PARAM; | 134 return PARAM; |
135 | 135 |
136 return UNKNOWN; | 136 return UNKNOWN; |
137 } | 137 } |
138 | 138 |
139 TemplateURL* template_url() { return url_; } | 139 TemplateURL* template_url() { return url_; } |
140 | 140 |
141 void AddImageRef(const std::wstring& type, int width, int height) { | 141 void AddImageRef(const std::string& type, int width, int height) { |
142 if (width > 0 && height > 0) | 142 if (width > 0 && height > 0) |
143 current_image_.reset(new TemplateURL::ImageRef(type, width, height)); | 143 current_image_.reset(new TemplateURL::ImageRef(type, width, height)); |
144 } | 144 } |
145 | 145 |
146 void EndImage() { | 146 void EndImage() { |
147 current_image_.reset(); | 147 current_image_.reset(); |
148 } | 148 } |
149 | 149 |
150 void SetImageURL(const std::wstring& url) { | 150 void SetImageURL(const GURL& url) { |
151 if (current_image_.get()) { | 151 if (current_image_.get()) { |
152 current_image_->url = GURL(WideToUTF8(url)); | 152 current_image_->url = url; |
153 url_->add_image_ref(*current_image_); | 153 url_->add_image_ref(*current_image_); |
154 current_image_.reset(); | 154 current_image_.reset(); |
155 } | 155 } |
156 } | 156 } |
157 | 157 |
158 void ResetString() { | 158 void ResetString() { |
159 string_.clear(); | 159 string_.clear(); |
160 } | 160 } |
161 | 161 |
162 void AppendString(const std::wstring& string) { | 162 void AppendString(const string16& string) { |
163 string_ += string; | 163 string_ += string; |
164 } | 164 } |
165 | 165 |
166 const std::wstring& GetString() { | 166 const string16& GetString() { |
167 return string_; | 167 return string_; |
168 } | 168 } |
169 | 169 |
170 void ResetExtraParams() { | 170 void ResetExtraParams() { |
171 extra_params_.clear(); | 171 extra_params_.clear(); |
172 } | 172 } |
173 | 173 |
174 void AddExtraParams(const std::string& key, const std::string& value) { | 174 void AddExtraParams(const std::string& key, const std::string& value) { |
175 if (parameter_filter_ && !parameter_filter_->KeepParameter(key, value)) | 175 if (parameter_filter_ && !parameter_filter_->KeepParameter(key, value)) |
176 return; | 176 return; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 | 226 |
227 // Key is UTF8 encoded. | 227 // Key is UTF8 encoded. |
228 static std::map<std::string, ElementType>* kElementNameToElementTypeMap; | 228 static std::map<std::string, ElementType>* kElementNameToElementTypeMap; |
229 // TemplateURL supplied to Read method. It's owned by the caller, so we | 229 // TemplateURL supplied to Read method. It's owned by the caller, so we |
230 // don't need to free it. | 230 // don't need to free it. |
231 TemplateURL* url_; | 231 TemplateURL* url_; |
232 std::vector<ElementType> elements_; | 232 std::vector<ElementType> elements_; |
233 scoped_ptr<TemplateURL::ImageRef> current_image_; | 233 scoped_ptr<TemplateURL::ImageRef> current_image_; |
234 | 234 |
235 // Character content for the current element. | 235 // Character content for the current element. |
236 std::wstring string_; | 236 string16 string_; |
237 | 237 |
238 TemplateURLParser::ParameterFilter* parameter_filter_; | 238 TemplateURLParser::ParameterFilter* parameter_filter_; |
239 | 239 |
240 // The list of parameters parsed in the Param nodes of a Url node. | 240 // The list of parameters parsed in the Param nodes of a Url node. |
241 std::vector<Param> extra_params_; | 241 std::vector<Param> extra_params_; |
242 | 242 |
243 // The HTTP methods used. | 243 // The HTTP methods used. |
244 Method method_; | 244 Method method_; |
245 Method suggestion_method_; | 245 Method suggestion_method_; |
246 | 246 |
247 // If true, we are currently parsing a suggest URL, otherwise it is an HTML | 247 // If true, we are currently parsing a suggest URL, otherwise it is an HTML |
248 // search. Note that we don't need a stack as Url nodes cannot be nested. | 248 // search. Note that we don't need a stack as Url nodes cannot be nested. |
249 bool is_suggest_url_; | 249 bool is_suggest_url_; |
250 | 250 |
251 // Whether we should derive the image from the URL (when images are data | 251 // Whether we should derive the image from the URL (when images are data |
252 // URLs). | 252 // URLs). |
253 bool derive_image_from_url_; | 253 bool derive_image_from_url_; |
254 | 254 |
255 DISALLOW_COPY_AND_ASSIGN(ParsingContext); | 255 DISALLOW_COPY_AND_ASSIGN(ParsingContext); |
256 }; | 256 }; |
257 | 257 |
258 // static | 258 // static |
259 std::map<std::string, ParsingContext::ElementType>* | 259 std::map<std::string, ParsingContext::ElementType>* |
260 ParsingContext::kElementNameToElementTypeMap = NULL; | 260 ParsingContext::kElementNameToElementTypeMap = NULL; |
261 | 261 |
262 std::wstring XMLCharToWide(const xmlChar* value) { | 262 string16 XMLCharToUTF16(const xmlChar* value, int length) { |
263 return UTF8ToWide(std::string((const char*)value)); | 263 return UTF8ToUTF16(std::string((const char*)value, length)); |
264 } | |
265 | |
266 std::wstring XMLCharToWide(const xmlChar* value, int length) { | |
267 return UTF8ToWide(std::string((const char*)value, length)); | |
268 } | 264 } |
269 | 265 |
270 std::string XMLCharToString(const xmlChar* value) { | 266 std::string XMLCharToString(const xmlChar* value) { |
271 return std::string((const char*)value); | 267 return std::string((const char*)value); |
272 } | 268 } |
273 | 269 |
274 // Returns true if input_encoding contains a valid input encoding string. This | 270 // Returns true if input_encoding contains a valid input encoding string. This |
275 // doesn't verify that we have a valid encoding for the string, just that the | 271 // doesn't verify that we have a valid encoding for the string, just that the |
276 // string contains characters that constitute a valid input encoding. | 272 // string contains characters that constitute a valid input encoding. |
277 bool IsValidEncodingString(const std::string& input_encoding) { | 273 bool IsValidEncodingString(const std::string& input_encoding) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 } | 333 } |
338 } | 334 } |
339 | 335 |
340 void ParseImage(const xmlChar** atts, ParsingContext* context) { | 336 void ParseImage(const xmlChar** atts, ParsingContext* context) { |
341 if (!atts) | 337 if (!atts) |
342 return; | 338 return; |
343 | 339 |
344 const xmlChar** attributes = atts; | 340 const xmlChar** attributes = atts; |
345 int width = 0; | 341 int width = 0; |
346 int height = 0; | 342 int height = 0; |
347 std::wstring type; | 343 std::string type; |
348 while (*attributes) { | 344 while (*attributes) { |
349 std::string name(XMLCharToString(*attributes)); | 345 std::string name(XMLCharToString(*attributes)); |
350 const xmlChar* value = attributes[1]; | 346 const xmlChar* value = attributes[1]; |
351 if (name == kImageTypeAttribute) { | 347 if (name == kImageTypeAttribute) { |
352 type = XMLCharToWide(value); | 348 type = XMLCharToString(value); |
353 } else if (name == kImageWidthAttribute) { | 349 } else if (name == kImageWidthAttribute) { |
354 base::StringToInt(XMLCharToString(value), &width); | 350 base::StringToInt(XMLCharToString(value), &width); |
355 } else if (name == kImageHeightAttribute) { | 351 } else if (name == kImageHeightAttribute) { |
356 base::StringToInt(XMLCharToString(value), &height); | 352 base::StringToInt(XMLCharToString(value), &height); |
357 } | 353 } |
358 attributes += 2; | 354 attributes += 2; |
359 } | 355 } |
360 if (width > 0 && height > 0 && !type.empty()) { | 356 if (width > 0 && height > 0 && !type.empty()) { |
361 // Valid Image URL. | 357 // Valid Image URL. |
362 context->AddImageRef(type, width, height); | 358 context->AddImageRef(type, width, height); |
363 } | 359 } |
364 } | 360 } |
365 | 361 |
366 void ParseParam(const xmlChar** atts, ParsingContext* context) { | 362 void ParseParam(const xmlChar** atts, ParsingContext* context) { |
367 if (!atts) | 363 if (!atts) |
368 return; | 364 return; |
369 | 365 |
370 const xmlChar** attributes = atts; | 366 const xmlChar** attributes = atts; |
371 std::wstring type; | |
372 std::string key, value; | 367 std::string key, value; |
373 while (*attributes) { | 368 while (*attributes) { |
374 std::string name(XMLCharToString(*attributes)); | 369 std::string name(XMLCharToString(*attributes)); |
375 const xmlChar* val = attributes[1]; | 370 const xmlChar* val = attributes[1]; |
376 if (name == kParamNameAttribute) { | 371 if (name == kParamNameAttribute) { |
377 key = XMLCharToString(val); | 372 key = XMLCharToString(val); |
378 } else if (name == kParamValueAttribute) { | 373 } else if (name == kParamValueAttribute) { |
379 value = XMLCharToString(val); | 374 value = XMLCharToString(val); |
380 } | 375 } |
381 attributes += 2; | 376 attributes += 2; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 void EndElementImpl(void *ctx, const xmlChar *name) { | 475 void EndElementImpl(void *ctx, const xmlChar *name) { |
481 ParsingContext* context = reinterpret_cast<ParsingContext*>(ctx); | 476 ParsingContext* context = reinterpret_cast<ParsingContext*>(ctx); |
482 switch (context->GetKnownType()) { | 477 switch (context->GetKnownType()) { |
483 case ParsingContext::SHORT_NAME: | 478 case ParsingContext::SHORT_NAME: |
484 context->template_url()->set_short_name(context->GetString()); | 479 context->template_url()->set_short_name(context->GetString()); |
485 break; | 480 break; |
486 case ParsingContext::DESCRIPTION: | 481 case ParsingContext::DESCRIPTION: |
487 context->template_url()->set_description(context->GetString()); | 482 context->template_url()->set_description(context->GetString()); |
488 break; | 483 break; |
489 case ParsingContext::IMAGE: { | 484 case ParsingContext::IMAGE: { |
490 GURL image_url(WideToUTF8(context->GetString())); | 485 GURL image_url(UTF16ToUTF8(context->GetString())); |
491 if (image_url.SchemeIs(chrome::kDataScheme)) { | 486 if (image_url.SchemeIs(chrome::kDataScheme)) { |
492 // TODO (jcampan): bug 1169256: when dealing with data URL, we need to | 487 // TODO (jcampan): bug 1169256: when dealing with data URL, we need to |
493 // decode the data URL in the renderer. For now, we'll just point to the | 488 // decode the data URL in the renderer. For now, we'll just point to the |
494 // fav icon from the URL. | 489 // fav icon from the URL. |
495 context->set_derive_image_from_url(true); | 490 context->set_derive_image_from_url(true); |
496 } else { | 491 } else { |
497 context->SetImageURL(context->GetString()); | 492 context->SetImageURL(image_url); |
498 } | 493 } |
499 context->EndImage(); | 494 context->EndImage(); |
500 break; | 495 break; |
501 } | 496 } |
502 case ParsingContext::LANGUAGE: | 497 case ParsingContext::LANGUAGE: |
503 context->template_url()->add_language(context->GetString()); | 498 context->template_url()->add_language(context->GetString()); |
504 break; | 499 break; |
505 case ParsingContext::INPUT_ENCODING: { | 500 case ParsingContext::INPUT_ENCODING: { |
506 std::string input_encoding = WideToASCII(context->GetString()); | 501 std::string input_encoding = UTF16ToASCII(context->GetString()); |
507 if (IsValidEncodingString(input_encoding)) | 502 if (IsValidEncodingString(input_encoding)) |
508 context->template_url()->add_input_encoding(input_encoding); | 503 context->template_url()->add_input_encoding(input_encoding); |
509 break; | 504 break; |
510 } | 505 } |
511 case ParsingContext::URL: | 506 case ParsingContext::URL: |
512 ProcessURLParams(context); | 507 ProcessURLParams(context); |
513 break; | 508 break; |
514 default: | 509 default: |
515 break; | 510 break; |
516 } | 511 } |
517 context->ResetString(); | 512 context->ResetString(); |
518 context->PopElement(); | 513 context->PopElement(); |
519 } | 514 } |
520 | 515 |
521 void CharactersImpl(void *ctx, const xmlChar *ch, int len) { | 516 void CharactersImpl(void *ctx, const xmlChar *ch, int len) { |
522 ParsingContext* context = reinterpret_cast<ParsingContext*>(ctx); | 517 ParsingContext* context = reinterpret_cast<ParsingContext*>(ctx); |
523 context->AppendString(XMLCharToWide(ch, len)); | 518 context->AppendString(XMLCharToUTF16(ch, len)); |
524 } | 519 } |
525 | 520 |
526 // Returns true if the ref is null, or the url wrapped by ref is | 521 // Returns true if the ref is null, or the url wrapped by ref is |
527 // valid with a spec of http/https. | 522 // valid with a spec of http/https. |
528 bool IsHTTPRef(const TemplateURLRef* ref) { | 523 bool IsHTTPRef(const TemplateURLRef* ref) { |
529 if (ref == NULL) | 524 if (ref == NULL) |
530 return true; | 525 return true; |
531 GURL url(ref->url()); | 526 GURL url(ref->url()); |
532 return (url.is_valid() && (url.SchemeIs(chrome::kHttpScheme) || | 527 return (url.is_valid() && (url.SchemeIs(chrome::kHttpScheme) || |
533 url.SchemeIs(chrome::kHttpsScheme))); | 528 url.SchemeIs(chrome::kHttpsScheme))); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
583 return false; | 578 return false; |
584 if (context.suggestion_method() == ParsingContext::POST) | 579 if (context.suggestion_method() == ParsingContext::POST) |
585 url->SetSuggestionsURL("", 0, 0); | 580 url->SetSuggestionsURL("", 0, 0); |
586 | 581 |
587 if (!url->short_name().empty() && !url->description().empty()) { | 582 if (!url->short_name().empty() && !url->description().empty()) { |
588 // So far so good, make sure the urls are http. | 583 // So far so good, make sure the urls are http. |
589 return IsLegal(url); | 584 return IsLegal(url); |
590 } | 585 } |
591 return false; | 586 return false; |
592 } | 587 } |
OLD | NEW |