search_engine_utils.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/search_engines/search_engine_utils.h"
  5. #include "components/google/core/common/google_util.h"
  6. #include "components/search_engines/prepopulated_engines.h"
  7. #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
  8. #include "url/gurl.h"
  9. namespace SearchEngineUtils {
  10. namespace {
  11. bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) {
  12. return prepopulated_url.is_valid() &&
  13. net::registry_controlled_domains::SameDomainOrHost(
  14. given_url, prepopulated_url,
  15. net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
  16. }
  17. } // namespace
  18. // Global functions -----------------------------------------------------------
  19. SearchEngineType GetEngineType(const GURL& url) {
  20. DCHECK(url.is_valid());
  21. // Check using TLD+1s, in order to more aggressively match search engine types
  22. // for data imported from other browsers.
  23. //
  24. // First special-case Google, because the prepopulate URL for it will not
  25. // convert to a GURL and thus won't have an origin. Instead see if the
  26. // incoming URL's host is "[*.]google.<TLD>".
  27. if (google_util::IsGoogleHostname(url.host(),
  28. google_util::DISALLOW_SUBDOMAIN))
  29. return TemplateURLPrepopulateData::google.type;
  30. // Now check the rest of the prepopulate data.
  31. for (size_t i = 0; i < TemplateURLPrepopulateData::kAllEnginesLength; ++i) {
  32. // First check the main search URL.
  33. if (SameDomain(
  34. url, GURL(TemplateURLPrepopulateData::kAllEngines[i]->search_url)))
  35. return TemplateURLPrepopulateData::kAllEngines[i]->type;
  36. // Then check the alternate URLs.
  37. for (size_t j = 0;
  38. j < TemplateURLPrepopulateData::kAllEngines[i]->alternate_urls_size;
  39. ++j) {
  40. if (SameDomain(url, GURL(TemplateURLPrepopulateData::kAllEngines[i]
  41. ->alternate_urls[j])))
  42. return TemplateURLPrepopulateData::kAllEngines[i]->type;
  43. }
  44. }
  45. return SEARCH_ENGINE_OTHER;
  46. }
  47. } // namespace SearchEngineUtils