| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <map> | 7 #include <map> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "chrome/browser/search_engines/template_url.h" | 13 #include "chrome/browser/search_engines/template_url.h" |
| 14 #include "chrome/common/url_constants.h" |
| 14 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 15 #include "libxml/parser.h" | 16 #include "libxml/parser.h" |
| 16 #include "libxml/xmlwriter.h" | 17 #include "libxml/xmlwriter.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // | 21 // |
| 21 // NOTE: libxml uses the UTF-8 encoding. As 0-127 of UTF-8 corresponds | 22 // NOTE: libxml uses the UTF-8 encoding. As 0-127 of UTF-8 corresponds |
| 22 // to that of char, the following names are all in terms of char. This avoids | 23 // to that of char, the following names are all in terms of char. This avoids |
| 23 // having to convert to wide, then do comparisons | 24 // having to convert to wide, then do comparisons |
| (...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 ParsingContext* context = reinterpret_cast<ParsingContext*>(ctx); | 476 ParsingContext* context = reinterpret_cast<ParsingContext*>(ctx); |
| 476 switch (context->GetKnownType()) { | 477 switch (context->GetKnownType()) { |
| 477 case ParsingContext::SHORT_NAME: | 478 case ParsingContext::SHORT_NAME: |
| 478 context->template_url()->set_short_name(context->GetString()); | 479 context->template_url()->set_short_name(context->GetString()); |
| 479 break; | 480 break; |
| 480 case ParsingContext::DESCRIPTION: | 481 case ParsingContext::DESCRIPTION: |
| 481 context->template_url()->set_description(context->GetString()); | 482 context->template_url()->set_description(context->GetString()); |
| 482 break; | 483 break; |
| 483 case ParsingContext::IMAGE: { | 484 case ParsingContext::IMAGE: { |
| 484 GURL image_url(WideToUTF8(context->GetString())); | 485 GURL image_url(WideToUTF8(context->GetString())); |
| 485 if (image_url.SchemeIs("data")) { | 486 if (image_url.SchemeIs(chrome::kDataScheme)) { |
| 486 // 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 |
| 487 // 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 |
| 488 // fav icon from the URL. | 489 // fav icon from the URL. |
| 489 context->set_derive_image_from_url(true); | 490 context->set_derive_image_from_url(true); |
| 490 } else { | 491 } else { |
| 491 context->SetImageURL(context->GetString()); | 492 context->SetImageURL(context->GetString()); |
| 492 } | 493 } |
| 493 context->EndImage(); | 494 context->EndImage(); |
| 494 break; | 495 break; |
| 495 } | 496 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 516 ParsingContext* context = reinterpret_cast<ParsingContext*>(ctx); | 517 ParsingContext* context = reinterpret_cast<ParsingContext*>(ctx); |
| 517 context->AppendString(XMLCharToWide(ch, len)); | 518 context->AppendString(XMLCharToWide(ch, len)); |
| 518 } | 519 } |
| 519 | 520 |
| 520 // 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 |
| 521 // valid with a spec of http/https. | 522 // valid with a spec of http/https. |
| 522 bool IsHTTPRef(const TemplateURLRef* ref) { | 523 bool IsHTTPRef(const TemplateURLRef* ref) { |
| 523 if (ref == NULL) | 524 if (ref == NULL) |
| 524 return true; | 525 return true; |
| 525 GURL url(WideToUTF8(ref->url())); | 526 GURL url(WideToUTF8(ref->url())); |
| 526 return (url.is_valid() && (url.SchemeIs("http") || url.SchemeIs("https"))); | 527 return (url.is_valid() && (url.SchemeIs(chrome::kHttpScheme) || |
| 528 url.SchemeIs(chrome::kHttpsScheme))); |
| 527 } | 529 } |
| 528 | 530 |
| 529 // Returns true if the TemplateURL is legal. A legal TemplateURL is one | 531 // Returns true if the TemplateURL is legal. A legal TemplateURL is one |
| 530 // where all URLs have a spec of http/https. | 532 // where all URLs have a spec of http/https. |
| 531 bool IsLegal(TemplateURL* url) { | 533 bool IsLegal(TemplateURL* url) { |
| 532 if (!IsHTTPRef(url->url()) || !IsHTTPRef(url->suggestions_url())) | 534 if (!IsHTTPRef(url->url()) || !IsHTTPRef(url->suggestions_url())) |
| 533 return false; | 535 return false; |
| 534 // Make sure all the image refs are legal. | 536 // Make sure all the image refs are legal. |
| 535 const std::vector<TemplateURL::ImageRef>& image_refs = url->image_refs(); | 537 const std::vector<TemplateURL::ImageRef>& image_refs = url->image_refs(); |
| 536 for (size_t i = 0; i < image_refs.size(); i++) { | 538 for (size_t i = 0; i < image_refs.size(); i++) { |
| 537 GURL image_url(image_refs[i].url); | 539 GURL image_url(image_refs[i].url); |
| 538 if (!image_url.is_valid() || | 540 if (!image_url.is_valid() || |
| 539 !(image_url.SchemeIs("http") || image_url.SchemeIs("https"))) { | 541 !(image_url.SchemeIs(chrome::kHttpScheme) || |
| 542 image_url.SchemeIs(chrome::kHttpsScheme))) { |
| 540 return false; | 543 return false; |
| 541 } | 544 } |
| 542 } | 545 } |
| 543 return true; | 546 return true; |
| 544 } | 547 } |
| 545 | 548 |
| 546 } // namespace | 549 } // namespace |
| 547 | 550 |
| 548 // static | 551 // static |
| 549 bool TemplateURLParser::Parse(const unsigned char* data, size_t length, | 552 bool TemplateURLParser::Parse(const unsigned char* data, size_t length, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 577 url->SetSuggestionsURL(L"", 0, 0); | 580 url->SetSuggestionsURL(L"", 0, 0); |
| 578 | 581 |
| 579 if (!url->short_name().empty() && !url->description().empty()) { | 582 if (!url->short_name().empty() && !url->description().empty()) { |
| 580 // So far so good, make sure the urls are http. | 583 // So far so good, make sure the urls are http. |
| 581 return IsLegal(url); | 584 return IsLegal(url); |
| 582 } | 585 } |
| 583 return false; | 586 return false; |
| 584 } | 587 } |
| 585 | 588 |
| 586 | 589 |
| OLD | NEW |