File: chrome_prefetch_service_delegate.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 (153 lines) | stat: -rw-r--r-- 5,914 bytes parent folder | download | duplicates (5)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2022 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/preloading/prefetch/prefetch_service/chrome_prefetch_service_delegate.h"

#include "chrome/browser/battery/battery_saver.h"
#include "chrome/browser/data_saver/data_saver.h"
#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/flags/android/chrome_feature_list.h"
#endif  // BUILDFLAG(IS_ANDROID)
#include "chrome/browser/preloading/prefetch/prefetch_service/prefetch_origin_decider.h"
#include "chrome/browser/preloading/preloading_features.h"
#include "chrome/browser/preloading/preloading_prefs.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "components/google/core/common/google_util.h"
#include "components/language/core/browser/pref_names.h"
#include "components/page_load_metrics/browser/metrics_web_contents_observer.h"
#include "components/prefs/pref_service.h"
#include "components/search_engines/template_url_service.h"
#include "components/unified_consent/url_keyed_data_collection_consent_helper.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h"
#include "google_apis/google_api_keys.h"
#include "net/http/http_util.h"
#include "url/gurl.h"

ChromePrefetchServiceDelegate::ChromePrefetchServiceDelegate(
    content::BrowserContext* browser_context)
    : profile_(Profile::FromBrowserContext(browser_context)),
      origin_decider_(
          std::make_unique<PrefetchOriginDecider>(profile_->GetPrefs())) {}

ChromePrefetchServiceDelegate::~ChromePrefetchServiceDelegate() = default;

std::string ChromePrefetchServiceDelegate::GetAcceptLanguageHeader() {
  return net::HttpUtil::GenerateAcceptLanguageHeader(
      profile_->GetPrefs()->GetString(language::prefs::kAcceptLanguages));
}

GURL ChromePrefetchServiceDelegate::GetDefaultPrefetchProxyHost() {
  return GURL("https://tunnel.googlezip.net/");
}

std::string ChromePrefetchServiceDelegate::GetAPIKey() {
  return google_apis::GetAPIKey();
}

GURL ChromePrefetchServiceDelegate::GetDefaultDNSCanaryCheckURL() {
  return GURL("http://dns-tunnel-check.googlezip.net/connect");
}

GURL ChromePrefetchServiceDelegate::GetDefaultTLSCanaryCheckURL() {
  return GURL("http://tls-tunnel-check.googlezip.net/connect");
}

void ChromePrefetchServiceDelegate::ReportOriginRetryAfter(
    const GURL& url,
    base::TimeDelta retry_after) {
  return origin_decider_->ReportOriginRetryAfter(url, retry_after);
}

bool ChromePrefetchServiceDelegate::IsOriginOutsideRetryAfterWindow(
    const GURL& url) {
  return origin_decider_->IsOriginOutsideRetryAfterWindow(url);
}

void ChromePrefetchServiceDelegate::ClearData() {
  origin_decider_->OnBrowsingDataCleared();
}

bool ChromePrefetchServiceDelegate::DisableDecoysBasedOnUserSettings() {
  // If the user has opted-in to Make Search and Browsing Better, then there is
  // no need to send decoy requests.
  std::unique_ptr<unified_consent::UrlKeyedDataCollectionConsentHelper> helper =
      unified_consent::UrlKeyedDataCollectionConsentHelper::
          NewAnonymizedDataCollectionConsentHelper(profile_->GetPrefs());
  return helper->IsEnabled();
}

content::PreloadingEligibility
ChromePrefetchServiceDelegate::IsSomePreloadingEnabled() {
  return prefetch::IsSomePreloadingEnabled(*profile_->GetPrefs());
}

bool ChromePrefetchServiceDelegate::IsPreloadingPrefEnabled() {
  return prefetch::IsSomePreloadingEnabled(*profile_->GetPrefs()) !=
         content::PreloadingEligibility::kPreloadingDisabled;
}

bool ChromePrefetchServiceDelegate::IsDataSaverEnabled() {
  return data_saver::IsDataSaverEnabled();
}

bool ChromePrefetchServiceDelegate::IsBatterySaverEnabled() {
  return battery::IsBatterySaverEnabled();
}

bool ChromePrefetchServiceDelegate::IsExtendedPreloadingEnabled() {
  return prefetch::GetPreloadPagesState(*profile_->GetPrefs()) ==
         prefetch::PreloadPagesState::kExtendedPreloading;
}

bool ChromePrefetchServiceDelegate::IsDomainInPrefetchAllowList(
    const GURL& referring_url) {
  return IsGoogleDomainUrl(referring_url, google_util::ALLOW_SUBDOMAIN,
                           google_util::ALLOW_NON_STANDARD_PORTS) ||
         IsYoutubeDomainUrl(referring_url, google_util::ALLOW_SUBDOMAIN,
                            google_util::ALLOW_NON_STANDARD_PORTS);
}

bool ChromePrefetchServiceDelegate::IsContaminationExempt(
    const GURL& referring_url) {
  // The default search engine has been chosen by the user and its cross-site
  // navigations have a significant performance impact.
  TemplateURLService* template_url_service =
      TemplateURLServiceFactory::GetForProfile(profile_);
  return template_url_service &&
         template_url_service->IsSearchResultsPageFromDefaultSearchProvider(
             referring_url);
}

bool ChromePrefetchServiceDelegate::IsContaminationExemptPerOrigin(
    const url::Origin& referring_origin) {
#if BUILDFLAG(IS_ANDROID)
  if (base::FeatureList::IsEnabled(chrome::android::kCCTNavigationalPrefetch)) {
    TemplateURLService* template_url_service =
        TemplateURLServiceFactory::GetForProfile(profile_);
    return template_url_service &&
           template_url_service->GetDefaultSearchProviderOrigin() ==
               referring_origin;
  }
#endif  // BUILDFLAG(IS_ANDROID)
  return false;
}

void ChromePrefetchServiceDelegate::OnPrefetchLikely(
    content::WebContents* web_contents) {
  page_load_metrics::MetricsWebContentsObserver* metrics_web_contents_observer =
      page_load_metrics::MetricsWebContentsObserver::FromWebContents(
          web_contents);
  if (!metrics_web_contents_observer) {
    return;
  }

  metrics_web_contents_observer->OnPrefetchLikely();
}

void ChromePrefetchServiceDelegate::SetAcceptLanguageHeader(
    std::string accept_language_header) {
  // no-op.
}