Upload upstream chromium 114.0.5735.31
[platform/framework/web/chromium-efl.git] / components / search_engines / search_engine_utils.cc
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.
4
5 #include "components/search_engines/search_engine_utils.h"
6
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"
10 #include "url/gurl.h"
11
12 namespace SearchEngineUtils {
13
14 namespace {
15
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);
21 }
22
23 }  // namespace
24
25 // Global functions -----------------------------------------------------------
26
27 SearchEngineType GetEngineType(const GURL& url) {
28   DCHECK(url.is_valid());
29
30   // Check using TLD+1s, in order to more aggressively match search engine types
31   // for data imported from other browsers.
32   //
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;
39
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.
43     if (SameDomain(
44             url, GURL(TemplateURLPrepopulateData::kAllEngines[i]->search_url)))
45       return TemplateURLPrepopulateData::kAllEngines[i]->type;
46
47     // Then check the alternate URLs.
48     for (size_t j = 0;
49          j < TemplateURLPrepopulateData::kAllEngines[i]->alternate_urls_size;
50          ++j) {
51       if (SameDomain(url, GURL(TemplateURLPrepopulateData::kAllEngines[i]
52                                    ->alternate_urls[j])))
53         return TemplateURLPrepopulateData::kAllEngines[i]->type;
54     }
55   }
56
57   return SEARCH_ENGINE_OTHER;
58 }
59
60 }  // namespace SearchEngineUtils