123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- // Copyright 2020 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "components/search_engines/search_engine_utils.h"
- #include "components/google/core/common/google_util.h"
- #include "components/search_engines/prepopulated_engines.h"
- #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
- #include "url/gurl.h"
- namespace SearchEngineUtils {
- namespace {
- bool SameDomain(const GURL& given_url, const GURL& prepopulated_url) {
- return prepopulated_url.is_valid() &&
- net::registry_controlled_domains::SameDomainOrHost(
- given_url, prepopulated_url,
- net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
- }
- } // namespace
- // Global functions -----------------------------------------------------------
- SearchEngineType GetEngineType(const GURL& url) {
- DCHECK(url.is_valid());
- // Check using TLD+1s, in order to more aggressively match search engine types
- // for data imported from other browsers.
- //
- // First special-case Google, because the prepopulate URL for it will not
- // convert to a GURL and thus won't have an origin. Instead see if the
- // incoming URL's host is "[*.]google.<TLD>".
- if (google_util::IsGoogleHostname(url.host(),
- google_util::DISALLOW_SUBDOMAIN))
- return TemplateURLPrepopulateData::google.type;
- // Now check the rest of the prepopulate data.
- for (size_t i = 0; i < TemplateURLPrepopulateData::kAllEnginesLength; ++i) {
- // First check the main search URL.
- if (SameDomain(
- url, GURL(TemplateURLPrepopulateData::kAllEngines[i]->search_url)))
- return TemplateURLPrepopulateData::kAllEngines[i]->type;
- // Then check the alternate URLs.
- for (size_t j = 0;
- j < TemplateURLPrepopulateData::kAllEngines[i]->alternate_urls_size;
- ++j) {
- if (SameDomain(url, GURL(TemplateURLPrepopulateData::kAllEngines[i]
- ->alternate_urls[j])))
- return TemplateURLPrepopulateData::kAllEngines[i]->type;
- }
- }
- return SEARCH_ENGINE_OTHER;
- }
- } // namespace SearchEngineUtils
|