| 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.h" | 5 #include "components/search_engines/template_url.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
| 12 #include "base/i18n/icu_string_conversions.h" | 12 #include "base/i18n/icu_string_conversions.h" |
| 13 #include "base/i18n/rtl.h" | 13 #include "base/i18n/rtl.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/metrics/field_trial.h" | 16 #include "base/metrics/field_trial.h" |
| 17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/string_piece.h" | 18 #include "base/strings/string_piece.h" |
| 19 #include "base/strings/string_split.h" | 19 #include "base/strings/string_split.h" |
| 20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 22 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 23 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 24 #include "components/google/core/browser/google_util.h" | 24 #include "components/google/core/browser/google_util.h" |
| 25 #include "components/metrics/proto/omnibox_input_type.pb.h" | 25 #include "components/metrics/proto/omnibox_input_type.pb.h" |
| 26 #include "components/search_engines/search_engines_switches.h" | 26 #include "components/search_engines/search_engines_switches.h" |
| 27 #include "components/search_engines/search_terms_data.h" | 27 #include "components/search_engines/search_terms_data.h" |
| 28 #include "components/search_engines/template_url_prepopulate_data.h" |
| 28 #include "components/url_formatter/url_formatter.h" | 29 #include "components/url_formatter/url_formatter.h" |
| 29 #include "google_apis/google_api_keys.h" | 30 #include "google_apis/google_api_keys.h" |
| 30 #include "net/base/escape.h" | 31 #include "net/base/escape.h" |
| 31 #include "net/base/mime_util.h" | 32 #include "net/base/mime_util.h" |
| 32 #include "ui/base/device_form_factor.h" | 33 #include "ui/base/device_form_factor.h" |
| 33 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 34 | 35 |
| 35 namespace { | 36 namespace { |
| 36 | 37 |
| 37 // The TemplateURLRef has any number of terms that need to be replaced. Each of | 38 // The TemplateURLRef has any number of terms that need to be replaced. Each of |
| (...skipping 1159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1197 | 1198 |
| 1198 TemplateURL::TemplateURL(const TemplateURLData& data) | 1199 TemplateURL::TemplateURL(const TemplateURLData& data) |
| 1199 : data_(data), | 1200 : data_(data), |
| 1200 url_ref_(nullptr), | 1201 url_ref_(nullptr), |
| 1201 suggestions_url_ref_(this, | 1202 suggestions_url_ref_(this, |
| 1202 TemplateURLRef::SUGGEST), | 1203 TemplateURLRef::SUGGEST), |
| 1203 instant_url_ref_(this, | 1204 instant_url_ref_(this, |
| 1204 TemplateURLRef::INSTANT), | 1205 TemplateURLRef::INSTANT), |
| 1205 image_url_ref_(this, TemplateURLRef::IMAGE), | 1206 image_url_ref_(this, TemplateURLRef::IMAGE), |
| 1206 new_tab_url_ref_(this, TemplateURLRef::NEW_TAB), | 1207 new_tab_url_ref_(this, TemplateURLRef::NEW_TAB), |
| 1207 contextual_search_url_ref_(this, TemplateURLRef::CONTEXTUAL_SEARCH) { | 1208 contextual_search_url_ref_(this, TemplateURLRef::CONTEXTUAL_SEARCH), |
| 1209 engine_type_(SEARCH_ENGINE_UNKNOWN) { |
| 1208 ResizeURLRefVector(); | 1210 ResizeURLRefVector(); |
| 1209 SetPrepopulateId(data_.prepopulate_id); | 1211 SetPrepopulateId(data_.prepopulate_id); |
| 1210 | 1212 |
| 1211 if (data_.search_terms_replacement_key == | 1213 if (data_.search_terms_replacement_key == |
| 1212 "{google:instantExtendedEnabledKey}") { | 1214 "{google:instantExtendedEnabledKey}") { |
| 1213 data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam; | 1215 data_.search_terms_replacement_key = google_util::kInstantExtendedAPIParam; |
| 1214 } | 1216 } |
| 1215 } | 1217 } |
| 1216 | 1218 |
| 1217 TemplateURL::~TemplateURL() { | 1219 TemplateURL::~TemplateURL() { |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1325 | 1327 |
| 1326 TemplateURL::Type TemplateURL::GetType() const { | 1328 TemplateURL::Type TemplateURL::GetType() const { |
| 1327 return extension_info_ ? extension_info_->type : NORMAL; | 1329 return extension_info_ ? extension_info_->type : NORMAL; |
| 1328 } | 1330 } |
| 1329 | 1331 |
| 1330 std::string TemplateURL::GetExtensionId() const { | 1332 std::string TemplateURL::GetExtensionId() const { |
| 1331 DCHECK(extension_info_); | 1333 DCHECK(extension_info_); |
| 1332 return extension_info_->extension_id; | 1334 return extension_info_->extension_id; |
| 1333 } | 1335 } |
| 1334 | 1336 |
| 1337 SearchEngineType TemplateURL::GetEngineType( |
| 1338 const SearchTermsData& search_terms_data) const { |
| 1339 if (engine_type_ != SEARCH_ENGINE_UNKNOWN) |
| 1340 return engine_type_; |
| 1341 if (data_.engine_type != SEARCH_ENGINE_UNKNOWN) { |
| 1342 engine_type_ = data_.engine_type; |
| 1343 return engine_type_; |
| 1344 } |
| 1345 const GURL url = GenerateSearchURL(search_terms_data); |
| 1346 engine_type_ = url.is_valid() ? |
| 1347 TemplateURLPrepopulateData::GetEngineType(url) : SEARCH_ENGINE_OTHER; |
| 1348 DCHECK_NE(SEARCH_ENGINE_UNKNOWN, engine_type_); |
| 1349 return engine_type_; |
| 1350 } |
| 1351 |
| 1335 bool TemplateURL::ExtractSearchTermsFromURL( | 1352 bool TemplateURL::ExtractSearchTermsFromURL( |
| 1336 const GURL& url, | 1353 const GURL& url, |
| 1337 const SearchTermsData& search_terms_data, | 1354 const SearchTermsData& search_terms_data, |
| 1338 base::string16* search_terms) const { | 1355 base::string16* search_terms) const { |
| 1339 return FindSearchTermsInURL(url, search_terms_data, search_terms, NULL, NULL); | 1356 return FindSearchTermsInURL(url, search_terms_data, search_terms, NULL, NULL); |
| 1340 } | 1357 } |
| 1341 | 1358 |
| 1342 bool TemplateURL::IsSearchURL(const GURL& url, | 1359 bool TemplateURL::IsSearchURL(const GURL& url, |
| 1343 const SearchTermsData& search_terms_data) const { | 1360 const SearchTermsData& search_terms_data) const { |
| 1344 base::string16 search_terms; | 1361 base::string16 search_terms; |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1462 return; | 1479 return; |
| 1463 | 1480 |
| 1464 data_ = other.data_; | 1481 data_ = other.data_; |
| 1465 ResizeURLRefVector(); | 1482 ResizeURLRefVector(); |
| 1466 InvalidateCachedValues(); | 1483 InvalidateCachedValues(); |
| 1467 SetPrepopulateId(other.data_.prepopulate_id); | 1484 SetPrepopulateId(other.data_.prepopulate_id); |
| 1468 } | 1485 } |
| 1469 | 1486 |
| 1470 void TemplateURL::SetURL(const std::string& url) { | 1487 void TemplateURL::SetURL(const std::string& url) { |
| 1471 data_.SetURL(url); | 1488 data_.SetURL(url); |
| 1489 data_.engine_type = SEARCH_ENGINE_UNKNOWN; |
| 1472 url_ref_->InvalidateCachedValues(); | 1490 url_ref_->InvalidateCachedValues(); |
| 1491 engine_type_ = SEARCH_ENGINE_UNKNOWN; |
| 1473 } | 1492 } |
| 1474 | 1493 |
| 1475 void TemplateURL::SetPrepopulateId(int id) { | 1494 void TemplateURL::SetPrepopulateId(int id) { |
| 1476 data_.prepopulate_id = id; | 1495 data_.prepopulate_id = id; |
| 1477 const bool prepopulated = id > 0; | 1496 const bool prepopulated = id > 0; |
| 1478 url_ref_->prepopulated_ = prepopulated; | 1497 url_ref_->prepopulated_ = prepopulated; |
| 1479 suggestions_url_ref_.prepopulated_ = prepopulated; | 1498 suggestions_url_ref_.prepopulated_ = prepopulated; |
| 1480 instant_url_ref_.prepopulated_ = prepopulated; | 1499 instant_url_ref_.prepopulated_ = prepopulated; |
| 1481 } | 1500 } |
| 1482 | 1501 |
| 1483 void TemplateURL::ResetKeywordIfNecessary( | 1502 void TemplateURL::ResetKeywordIfNecessary( |
| 1484 const SearchTermsData& search_terms_data, | 1503 const SearchTermsData& search_terms_data, |
| 1485 bool force) { | 1504 bool force) { |
| 1486 if (IsGoogleSearchURLWithReplaceableKeyword(search_terms_data) || force) { | 1505 if (IsGoogleSearchURLWithReplaceableKeyword(search_terms_data) || force) { |
| 1487 DCHECK(GetType() != OMNIBOX_API_EXTENSION); | 1506 DCHECK(GetType() != OMNIBOX_API_EXTENSION); |
| 1488 GURL url(GenerateSearchURL(search_terms_data)); | 1507 GURL url(GenerateSearchURL(search_terms_data)); |
| 1489 if (url.is_valid()) | 1508 if (url.is_valid()) |
| 1490 data_.SetKeyword(GenerateKeyword(url)); | 1509 data_.SetKeyword(GenerateKeyword(url)); |
| 1491 } | 1510 } |
| 1492 } | 1511 } |
| 1493 | 1512 |
| 1494 void TemplateURL::InvalidateCachedValues() const { | 1513 void TemplateURL::InvalidateCachedValues() const { |
| 1495 for (const TemplateURLRef& ref : url_refs_) | 1514 for (const TemplateURLRef& ref : url_refs_) |
| 1496 ref.InvalidateCachedValues(); | 1515 ref.InvalidateCachedValues(); |
| 1497 suggestions_url_ref_.InvalidateCachedValues(); | 1516 suggestions_url_ref_.InvalidateCachedValues(); |
| 1498 instant_url_ref_.InvalidateCachedValues(); | 1517 instant_url_ref_.InvalidateCachedValues(); |
| 1499 image_url_ref_.InvalidateCachedValues(); | 1518 image_url_ref_.InvalidateCachedValues(); |
| 1500 new_tab_url_ref_.InvalidateCachedValues(); | 1519 new_tab_url_ref_.InvalidateCachedValues(); |
| 1501 contextual_search_url_ref_.InvalidateCachedValues(); | 1520 contextual_search_url_ref_.InvalidateCachedValues(); |
| 1521 engine_type_ = SEARCH_ENGINE_UNKNOWN; |
| 1502 } | 1522 } |
| 1503 | 1523 |
| 1504 void TemplateURL::ResizeURLRefVector() { | 1524 void TemplateURL::ResizeURLRefVector() { |
| 1505 const size_t new_size = data_.alternate_urls.size() + 1; | 1525 const size_t new_size = data_.alternate_urls.size() + 1; |
| 1506 if (url_refs_.size() == new_size) | 1526 if (url_refs_.size() == new_size) |
| 1507 return; | 1527 return; |
| 1508 | 1528 |
| 1509 // See comment on TemplateURL::ExtractSearchTermsFromURL() for understanding | 1529 // See comment on TemplateURL::ExtractSearchTermsFromURL() for understanding |
| 1510 // the order of TemplateURLRefs in the |url_refs_| vector. | 1530 // the order of TemplateURLRefs in the |url_refs_| vector. |
| 1511 url_refs_.clear(); | 1531 url_refs_.clear(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1535 // patterns. This means that given patterns | 1555 // patterns. This means that given patterns |
| 1536 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], | 1556 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], |
| 1537 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would | 1557 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would |
| 1538 // return false. This is important for at least Google, where such URLs | 1558 // return false. This is important for at least Google, where such URLs |
| 1539 // are invalid. | 1559 // are invalid. |
| 1540 return !search_terms->empty(); | 1560 return !search_terms->empty(); |
| 1541 } | 1561 } |
| 1542 } | 1562 } |
| 1543 return false; | 1563 return false; |
| 1544 } | 1564 } |
| OLD | NEW |