File: browsing_data_helper.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 (261 lines) | stat: -rw-r--r-- 10,849 bytes parent folder | download | duplicates (3)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright 2012 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/browsing_data/content/browsing_data_helper.h"

#include <vector>

#include "base/containers/contains.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "components/content_settings/core/browser/content_settings_registry.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/no_state_prefetch/browser/no_state_prefetch_manager.h"
#include "components/prefs/pref_service.h"
#include "components/site_isolation/pref_names.h"
#include "content/public/browser/browsing_data_filter_builder.h"
#include "content/public/browser/browsing_data_remover.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "url/gurl.h"
#include "url/url_util.h"

namespace browsing_data {

namespace {

bool WebsiteSettingsFilterAdapter(
    const base::RepeatingCallback<bool(const GURL&)> predicate,
    const ContentSettingsPattern& primary_pattern,
    const ContentSettingsPattern& secondary_pattern) {
  // Ignore the default setting.
  if (primary_pattern == ContentSettingsPattern::Wildcard())
    return false;

  // Website settings only use origin-scoped patterns. The only content setting
  // this filter is used for is DURABLE_STORAGE, which also only uses
  // origin-scoped patterns. Such patterns can be directly translated to a GURL.
  GURL url(primary_pattern.ToString());
  DCHECK(url.is_valid()) << "url: '" << url.possibly_invalid_spec() << "' "
                         << "pattern: '" << primary_pattern.ToString() << "'";
  return predicate.Run(url);
}

// Callback for when cookies have been deleted. Invokes |done|.
// Receiving |cookie_manager| as a parameter so that the receive pipe is
// not deleted before the response is received.
void OnClearedCookies(
    base::OnceClosure done,
    mojo::Remote<network::mojom::CookieManager> cookie_manager,
    uint32_t num_deleted) {
  std::move(done).Run();
}

bool IsSameHost(const std::string& host, const std::string& top_frame_host) {
  return host == top_frame_host;
}

}  // namespace

bool IsWebScheme(const std::string& scheme) {
  const std::vector<std::string>& schemes = url::GetWebStorageSchemes();
  return base::Contains(schemes, scheme);
}

bool HasWebScheme(const GURL& origin) {
  return IsWebScheme(origin.scheme());
}

HostContentSettingsMap::PatternSourcePredicate CreateWebsiteSettingsFilter(
    content::BrowsingDataFilterBuilder* filter_builder) {
  return filter_builder->MatchesAllOriginsAndDomains()
             ? HostContentSettingsMap::PatternSourcePredicate()
             : base::BindRepeating(&WebsiteSettingsFilterAdapter,
                                   filter_builder->BuildUrlFilter());
}

void RemovePrerenderCacheData(
    prerender::NoStatePrefetchManager* no_state_prefetch_manager) {
  // The NoStatePrefetchManager may have a page actively being prerendered,
  // which is essentially a preemptively cached page.
  if (no_state_prefetch_manager) {
    no_state_prefetch_manager->ClearData(
        prerender::NoStatePrefetchManager::CLEAR_PRERENDER_CONTENTS);
  }
}

void RemoveSiteIsolationData(PrefService* prefs) {
  prefs->ClearPref(site_isolation::prefs::kUserTriggeredIsolatedOrigins);
  prefs->ClearPref(site_isolation::prefs::kWebTriggeredIsolatedOrigins);
  // Note that this does not clear these sites from the in-memory map in
  // ChildProcessSecurityPolicy, since that is not supported at runtime. That
  // list of isolated sites is not directly exposed to users, though, and
  // will be cleared on next restart.
}

void RemoveEmbedderCookieData(
    const base::Time& delete_begin,
    const base::Time& delete_end,
    content::BrowsingDataFilterBuilder* filter_builder,
    HostContentSettingsMap* host_content_settings_map,
    network::mojom::NetworkContext* safe_browsing_context,
    base::OnceCallback<base::OnceCallback<void()>()> callback_factory) {
  auto website_settings_filter = CreateWebsiteSettingsFilter(filter_builder);

  host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
      ContentSettingsType::CLIENT_HINTS, base::Time(), base::Time::Max(),
      website_settings_filter);

  host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
      ContentSettingsType::REDUCED_ACCEPT_LANGUAGE, base::Time(),
      base::Time::Max(), website_settings_filter);

  // Clear the safebrowsing cookies only if time period is for "all time".  It
  // doesn't make sense to apply the time period of deleting in the last X
  // hours/days to the safebrowsing cookies since they aren't the result of
  // any user action.
  if (delete_begin == base::Time() && delete_end == base::Time::Max() &&
      safe_browsing_context) {
    mojo::Remote<network::mojom::CookieManager> cookie_manager;
    safe_browsing_context->GetCookieManager(
        cookie_manager.BindNewPipeAndPassReceiver());

    network::mojom::CookieManager* manager_ptr = cookie_manager.get();

    network::mojom::CookieDeletionFilterPtr deletion_filter =
        filter_builder->BuildCookieDeletionFilter();
    if (!delete_begin.is_null())
      deletion_filter->created_after_time = delete_begin;
    if (!delete_end.is_null())
      deletion_filter->created_before_time = delete_end;

    manager_ptr->DeleteCookies(
        std::move(deletion_filter),
        base::BindOnce(&OnClearedCookies, std::move(callback_factory).Run(),
                       std::move(cookie_manager)));
  }
}

void RemoveSiteSettingsData(const base::Time& delete_begin,
                            const base::Time& delete_end,
                            HostContentSettingsMap* host_content_settings_map) {
  const auto* registry =
      content_settings::ContentSettingsRegistry::GetInstance();
  for (const content_settings::ContentSettingsInfo* info : *registry) {
    host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
        info->website_settings_info()->type(), delete_begin, delete_end,
        HostContentSettingsMap::PatternSourcePredicate());
  }

  host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
      ContentSettingsType::USB_CHOOSER_DATA, delete_begin, delete_end,
      HostContentSettingsMap::PatternSourcePredicate());

  host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
      ContentSettingsType::BLUETOOTH_CHOOSER_DATA, delete_begin, delete_end,
      HostContentSettingsMap::PatternSourcePredicate());

  RemoveFederatedSiteSettingsData(
      delete_begin, delete_end,
      HostContentSettingsMap::PatternSourcePredicate(),
      host_content_settings_map);

  host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
      ContentSettingsType::SERIAL_CHOOSER_DATA, delete_begin, delete_end,
      HostContentSettingsMap::PatternSourcePredicate());

#if !BUILDFLAG(IS_ANDROID)
  host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
      ContentSettingsType::HID_CHOOSER_DATA, delete_begin, delete_end,
      HostContentSettingsMap::PatternSourcePredicate());

  host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
      ContentSettingsType::INITIALIZED_TRANSLATIONS, delete_begin, delete_end,
      HostContentSettingsMap::PatternSourcePredicate());

  host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
      ContentSettingsType::FILE_SYSTEM_ACCESS_CHOOSER_DATA, delete_begin,
      delete_end, HostContentSettingsMap::PatternSourcePredicate());

  host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
      ContentSettingsType::FILE_SYSTEM_ACCESS_EXTENDED_PERMISSION, delete_begin,
      delete_end, HostContentSettingsMap::PatternSourcePredicate());
#endif

#if BUILDFLAG(IS_CHROMEOS)
  host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
      ContentSettingsType::SMART_CARD_DATA, delete_begin, delete_end,
      HostContentSettingsMap::PatternSourcePredicate());
#endif

  host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
      ContentSettingsType::ON_DEVICE_SPEECH_RECOGNITION_LANGUAGES_DOWNLOADED,
      delete_begin, delete_end,
      HostContentSettingsMap::PatternSourcePredicate());
}

void RemoveFederatedSiteSettingsData(
    const base::Time& delete_begin,
    const base::Time& delete_end,
    HostContentSettingsMap::PatternSourcePredicate pattern_predicate,
    HostContentSettingsMap* host_content_settings_map) {
  host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
      ContentSettingsType::FEDERATED_IDENTITY_API, delete_begin, delete_end,
      pattern_predicate);

  host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
      ContentSettingsType::FEDERATED_IDENTITY_IDENTITY_PROVIDER_SIGNIN_STATUS,
      delete_begin, delete_end, pattern_predicate);

  host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
      ContentSettingsType::FEDERATED_IDENTITY_SHARING, delete_begin, delete_end,
      pattern_predicate);

  host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
      ContentSettingsType::FEDERATED_IDENTITY_AUTO_REAUTHN_PERMISSION,
      delete_begin, delete_end, pattern_predicate);

  host_content_settings_map->ClearSettingsForOneTypeWithPredicate(
      ContentSettingsType::FEDERATED_IDENTITY_IDENTITY_PROVIDER_REGISTRATION,
      delete_begin, delete_end, pattern_predicate);
}

int GetUniqueHostCount(const BrowsingDataModel& browsing_data_model) {
  std::set<BrowsingDataModel::DataOwner> unique_hosts;
  for (auto entry : browsing_data_model) {
    unique_hosts.insert(*entry.data_owner);
  }

  return unique_hosts.size();
}

int GetUniqueThirdPartyCookiesHostCount(
    const GURL& top_frame_url,
    const BrowsingDataModel& browsing_data_model) {
  std::string top_frame_domain =
      net::registry_controlled_domains::GetDomainAndRegistry(
          top_frame_url,
          net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);

  std::set<BrowsingDataModel::DataOwner> unique_hosts;
  for (auto entry : browsing_data_model) {
    std::string host = BrowsingDataModel::GetHost(entry.data_owner.get());
    if (entry.data_details->blocked_third_party ||
        (top_frame_domain.empty() && !IsSameHost(host, top_frame_url.host())) ||
        (!top_frame_domain.empty() && !url::DomainIs(host, top_frame_domain))) {
      for (auto storage_type : entry.data_details->storage_types) {
        if (browsing_data_model.IsBlockedByThirdPartyCookieBlocking(
                entry.data_key.get(), storage_type)) {
          unique_hosts.insert(*entry.data_owner);
          break;
        }
      }
    }
  }

  return unique_hosts.size();
}

}  // namespace browsing_data