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