| 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 const GURL url = GenerateSearchURL(search_terms_data); |
| 1341 engine_type_ = url.is_valid() ? |
| 1342 TemplateURLPrepopulateData::GetEngineType(url) : SEARCH_ENGINE_OTHER; |
| 1343 DCHECK_NE(SEARCH_ENGINE_UNKNOWN, engine_type_); |
| 1344 } |
| 1345 return engine_type_; |
| 1346 } |
| 1347 |
| 1335 bool TemplateURL::ExtractSearchTermsFromURL( | 1348 bool TemplateURL::ExtractSearchTermsFromURL( |
| 1336 const GURL& url, | 1349 const GURL& url, |
| 1337 const SearchTermsData& search_terms_data, | 1350 const SearchTermsData& search_terms_data, |
| 1338 base::string16* search_terms) const { | 1351 base::string16* search_terms) const { |
| 1339 return FindSearchTermsInURL(url, search_terms_data, search_terms, NULL, NULL); | 1352 return FindSearchTermsInURL(url, search_terms_data, search_terms, NULL, NULL); |
| 1340 } | 1353 } |
| 1341 | 1354 |
| 1342 bool TemplateURL::IsSearchURL(const GURL& url, | 1355 bool TemplateURL::IsSearchURL(const GURL& url, |
| 1343 const SearchTermsData& search_terms_data) const { | 1356 const SearchTermsData& search_terms_data) const { |
| 1344 base::string16 search_terms; | 1357 base::string16 search_terms; |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1462 return; | 1475 return; |
| 1463 | 1476 |
| 1464 data_ = other.data_; | 1477 data_ = other.data_; |
| 1465 ResizeURLRefVector(); | 1478 ResizeURLRefVector(); |
| 1466 InvalidateCachedValues(); | 1479 InvalidateCachedValues(); |
| 1467 SetPrepopulateId(other.data_.prepopulate_id); | 1480 SetPrepopulateId(other.data_.prepopulate_id); |
| 1468 } | 1481 } |
| 1469 | 1482 |
| 1470 void TemplateURL::SetURL(const std::string& url) { | 1483 void TemplateURL::SetURL(const std::string& url) { |
| 1471 data_.SetURL(url); | 1484 data_.SetURL(url); |
| 1485 engine_type_ = SEARCH_ENGINE_UNKNOWN; |
| 1472 url_ref_->InvalidateCachedValues(); | 1486 url_ref_->InvalidateCachedValues(); |
| 1473 } | 1487 } |
| 1474 | 1488 |
| 1475 void TemplateURL::SetPrepopulateId(int id) { | 1489 void TemplateURL::SetPrepopulateId(int id) { |
| 1476 data_.prepopulate_id = id; | 1490 data_.prepopulate_id = id; |
| 1477 const bool prepopulated = id > 0; | 1491 const bool prepopulated = id > 0; |
| 1478 for (TemplateURLRef& ref : url_refs_) | 1492 for (TemplateURLRef& ref : url_refs_) |
| 1479 ref.prepopulated_ = prepopulated; | 1493 ref.prepopulated_ = prepopulated; |
| 1480 suggestions_url_ref_.prepopulated_ = prepopulated; | 1494 suggestions_url_ref_.prepopulated_ = prepopulated; |
| 1481 instant_url_ref_.prepopulated_ = prepopulated; | 1495 instant_url_ref_.prepopulated_ = prepopulated; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1539 // patterns. This means that given patterns | 1553 // patterns. This means that given patterns |
| 1540 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], | 1554 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], |
| 1541 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would | 1555 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would |
| 1542 // return false. This is important for at least Google, where such URLs | 1556 // return false. This is important for at least Google, where such URLs |
| 1543 // are invalid. | 1557 // are invalid. |
| 1544 return !search_terms->empty(); | 1558 return !search_terms->empty(); |
| 1545 } | 1559 } |
| 1546 } | 1560 } |
| 1547 return false; | 1561 return false; |
| 1548 } | 1562 } |
| OLD | NEW |