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