File: search_engine_base_url_tracker.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (74 lines) | stat: -rw-r--r-- 2,964 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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/search/search_engine_base_url_tracker.h"


#include "components/search_engines/search_terms_data.h"

SearchEngineBaseURLTracker::SearchEngineBaseURLTracker(
    TemplateURLService* template_url_service,
    std::unique_ptr<SearchTermsData> search_terms_data,
    const BaseURLChangedCallback& base_url_changed_callback)
    : template_url_service_(template_url_service),
      search_terms_data_(std::move(search_terms_data)),
      base_url_changed_callback_(base_url_changed_callback),
      previous_google_base_url_(search_terms_data_->GoogleBaseURLValue()) {
  DCHECK(template_url_service_);

  observation_.Observe(template_url_service_.get());

  const TemplateURL* default_search_provider =
      template_url_service_->GetDefaultSearchProvider();
  if (default_search_provider)
    previous_default_search_provider_data_ = default_search_provider->data();
}

SearchEngineBaseURLTracker::~SearchEngineBaseURLTracker() = default;

void SearchEngineBaseURLTracker::OnTemplateURLServiceChanged() {
  GURL google_base_url;
  if (HasGoogleBaseURL())
    google_base_url = GURL(search_terms_data_->GoogleBaseURLValue());

  // Check whether the default search provider was changed.
  const TemplateURL* template_url =
      template_url_service_->GetDefaultSearchProvider();
  const TemplateURLData* previous_data =
      previous_default_search_provider_data_.has_value()
          ? &previous_default_search_provider_data_.value()
          : nullptr;
  bool default_search_provider_changed = !TemplateURL::MatchesData(
      template_url, previous_data, *search_terms_data_);
  if (default_search_provider_changed) {
    if (template_url)
      previous_default_search_provider_data_ = template_url->data();
    else
      previous_default_search_provider_data_ = std::nullopt;

    // Also update the cached Google base URL, without separately notifying.
    previous_google_base_url_ = google_base_url;

    base_url_changed_callback_.Run(ChangeReason::DEFAULT_SEARCH_PROVIDER);
    return;
  }

  // Check whether the Google base URL has changed.
  // Note that, even if the TemplateURL for the Default Search Provider has not
  // changed, the effective URLs might change if they reference the Google base
  // URL. The TemplateURLService will notify us when the effective URL changes
  // in this way but it's up to us to do the work to check both.
  if (google_base_url != previous_google_base_url_) {
    previous_google_base_url_ = google_base_url;
    if (HasGoogleBaseURL())
      base_url_changed_callback_.Run(ChangeReason::GOOGLE_BASE_URL);
  }
}

bool SearchEngineBaseURLTracker::HasGoogleBaseURL() {
  const TemplateURL* template_url =
      template_url_service_->GetDefaultSearchProvider();

  return template_url && template_url->HasGoogleBaseURLs(*search_terms_data_);
}