File: search_host_to_urls_map.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (88 lines) | stat: -rw-r--r-- 2,961 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2014 The Chromium Authors
// 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_host_to_urls_map.h"

#include <algorithm>
#include <memory>
#include <string_view>

#include "components/search_engines/template_url.h"

SearchHostToURLsMap::SearchHostToURLsMap()
    : initialized_(false) {
}

SearchHostToURLsMap::~SearchHostToURLsMap() {
}

void SearchHostToURLsMap::Init(
    const TemplateURL::OwnedTemplateURLVector& template_urls,
    const SearchTermsData& search_terms_data) {
  DCHECK(!initialized_);
  initialized_ = true;  // Set here so Add doesn't assert.
  Add(template_urls, search_terms_data);
}

void SearchHostToURLsMap::Add(TemplateURL* template_url,
                              const SearchTermsData& search_terms_data) {
  DCHECK(initialized_);
  DCHECK(template_url);
  DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, template_url->type());

  const GURL url(template_url->GenerateSearchURL(search_terms_data));
  if (!url.is_valid() || !url.has_host())
    return;

  host_to_urls_map_[url.host()].insert(template_url);
}

void SearchHostToURLsMap::Remove(const TemplateURL* template_url) {
  DCHECK(initialized_);
  DCHECK(template_url);
  DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, template_url->type());

  // A given TemplateURL only occurs once in the map.
  auto set_with_url = std::ranges::find_if(
      host_to_urls_map_,
      [&](std::pair<const std::string, TemplateURLSet>& entry) {
        return entry.second.erase(template_url);
      });

  if (set_with_url != host_to_urls_map_.end() && set_with_url->second.empty())
    host_to_urls_map_.erase(set_with_url);
}

TemplateURL* SearchHostToURLsMap::GetTemplateURLForHost(std::string_view host) {
  DCHECK(initialized_);

  HostToURLsMap::const_iterator iter = host_to_urls_map_.find(host);
  if (iter == host_to_urls_map_.end() || iter->second.empty())
    return nullptr;

  // Because we have to happily tolerate duplicates in TemplateURLService now,
  /// return the best TemplateURL for `host`, just like
  // `GetTemplateURLForKeyword` returns the best TemplateURL for a keyword.
  return *std::min_element(iter->second.begin(), iter->second.end(),
                           [](const auto& a, const auto& b) {
                             return a->IsBetterThanConflictingEngine(b);
                           });
}

SearchHostToURLsMap::TemplateURLSet* SearchHostToURLsMap::GetURLsForHost(
    std::string_view host) {
  DCHECK(initialized_);

  auto urls_for_host = host_to_urls_map_.find(host);
  if (urls_for_host == host_to_urls_map_.end() || urls_for_host->second.empty())
    return nullptr;
  return &urls_for_host->second;
}

void SearchHostToURLsMap::Add(
    const TemplateURL::OwnedTemplateURLVector& template_urls,
    const SearchTermsData& search_terms_data) {
  for (const auto& turl : template_urls)
    Add(turl.get(), search_terms_data);
}