[go: nahoru, domu]

blob: 6bbf868e44c332d0d2cbd10f50e4af565b8c0d9f [file] [log] [blame]
Avi Drissman4e1b7bc2022-09-15 14:03:501// Copyright 2022 The Chromium Authors
Shuran Huang71a22ff2022-02-11 14:54:102// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Shuran Huang10398152022-03-28 21:52:185#include "content/browser/first_party_sets/first_party_sets_loader.h"
Shuran Huang71a22ff2022-02-11 14:54:106
Chris Fredrickson4ef2d242022-09-06 22:47:187#include <iterator>
Lei Zhangbd722512022-03-14 23:21:588#include <set>
Chris Fredrickson878d06b2022-09-08 15:20:379#include <sstream>
Shuran Huang71a22ff2022-02-11 14:54:1010#include <utility>
11#include <vector>
12
13#include "base/check.h"
Shuran Huang71a22ff2022-02-11 14:54:1014#include "base/containers/flat_map.h"
Shuran Huang71a22ff2022-02-11 14:54:1015#include "base/files/file_util.h"
Shuran Huang71a22ff2022-02-11 14:54:1016#include "base/metrics/histogram_functions.h"
Chris Fredrickson4ef2d242022-09-06 22:47:1817#include "base/ranges/algorithm.h"
Shuran Huang71a22ff2022-02-11 14:54:1018#include "base/sequence_checker.h"
Shuran Huang71a22ff2022-02-11 14:54:1019#include "base/task/thread_pool.h"
Chris Fredrickson075b4952022-11-21 15:57:3020#include "base/version.h"
Shuran Huang10398152022-03-28 21:52:1821#include "content/browser/first_party_sets/first_party_set_parser.h"
Chris Fredrickson8db7bf542022-09-06 17:47:2022#include "content/browser/first_party_sets/local_set_declaration.h"
Shuran Huang71a22ff2022-02-11 14:54:1023#include "net/base/schemeful_site.h"
Chris Fredrickson33273122022-08-31 22:41:0024#include "net/first_party_sets/first_party_set_entry.h"
Chris Fredrickson0cad3722022-09-30 14:50:3125#include "net/first_party_sets/global_first_party_sets.h"
Shuran Huang71a22ff2022-02-11 14:54:1026#include "third_party/abseil-cpp/absl/types/optional.h"
27
Shuran Huang10398152022-03-28 21:52:1828namespace content {
Shuran Huang71a22ff2022-02-11 14:54:1029
30namespace {
31
Shuran Huang71a22ff2022-02-11 14:54:1032std::string ReadSetsFile(base::File sets_file) {
33 std::string raw_sets;
34 base::ScopedFILE file(FileToFILE(std::move(sets_file), "r"));
35 return base::ReadStreamToString(file.get(), &raw_sets) ? raw_sets : "";
36}
37
38} // namespace
39
40FirstPartySetsLoader::FirstPartySetsLoader(
Kirubel Aklilu6d1ef6b92022-07-08 13:40:1941 LoadCompleteOnceCallback on_load_complete)
42 : on_load_complete_(std::move(on_load_complete)) {}
Shuran Huang71a22ff2022-02-11 14:54:1043
44FirstPartySetsLoader::~FirstPartySetsLoader() {
45 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
46}
47
48void FirstPartySetsLoader::SetManuallySpecifiedSet(
Chris Fredrickson8db7bf542022-09-06 17:47:2049 const LocalSetDeclaration& local_set) {
Shuran Huang71a22ff2022-02-11 14:54:1050 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Chris Fredrickson8db7bf542022-09-06 17:47:2051 manually_specified_set_ = local_set;
cfredriccc53588b2022-03-04 19:09:0052 UmaHistogramTimes(
Chris Fredrickson92e21d02022-04-22 21:16:1953 "Cookie.FirstPartySets.InitializationDuration.ReadCommandLineSet2",
cfredriccc53588b2022-03-04 19:09:0054 construction_timer_.Elapsed());
Shuran Huang71a22ff2022-02-11 14:54:1055
Shuran Huang71a22ff2022-02-11 14:54:1056 MaybeFinishLoading();
57}
58
Chris Fredrickson075b4952022-11-21 15:57:3059void FirstPartySetsLoader::SetComponentSets(base::Version version,
60 base::File sets_file) {
Shuran Huang71a22ff2022-02-11 14:54:1061 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
62 if (component_sets_parse_progress_ != Progress::kNotStarted) {
63 DisposeFile(std::move(sets_file));
64 return;
65 }
66
67 component_sets_parse_progress_ = Progress::kStarted;
68
Chris Fredrickson075b4952022-11-21 15:57:3069 if (!sets_file.IsValid() || !version.IsValid()) {
70 OnReadSetsFile(base::Version(), "");
Shuran Huang71a22ff2022-02-11 14:54:1071 return;
72 }
73
74 // We use USER_BLOCKING here since First-Party Set initialization blocks
75 // network navigations at startup.
76 base::ThreadPool::PostTaskAndReplyWithResult(
77 FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_BLOCKING},
78 base::BindOnce(&ReadSetsFile, std::move(sets_file)),
79 base::BindOnce(&FirstPartySetsLoader::OnReadSetsFile,
Chris Fredrickson075b4952022-11-21 15:57:3080 weak_factory_.GetWeakPtr(), std::move(version)));
Shuran Huang71a22ff2022-02-11 14:54:1081}
82
Chris Fredrickson075b4952022-11-21 15:57:3083void FirstPartySetsLoader::OnReadSetsFile(base::Version version,
84 const std::string& raw_sets) {
Shuran Huang71a22ff2022-02-11 14:54:1085 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
86 DCHECK_EQ(component_sets_parse_progress_, Progress::kStarted);
87
cfredric6b6174462022-02-16 18:12:4888 std::istringstream stream(raw_sets);
Chris Fredricksond149b842022-08-17 21:41:0089 FirstPartySetParser::SetsAndAliases public_sets =
Chris Fredricksonf9de3452022-09-12 21:46:5690 FirstPartySetParser::ParseSetsFromStream(stream, /*emit_errors=*/false);
Chris Fredrickson075b4952022-11-21 15:57:3091 sets_ = net::GlobalFirstPartySets(std::move(version),
92 std::move(public_sets.first),
Chris Fredrickson0cad3722022-09-30 14:50:3193 std::move(public_sets.second));
Shuran Huang71a22ff2022-02-11 14:54:1094
Shuran Huang71a22ff2022-02-11 14:54:1095 component_sets_parse_progress_ = Progress::kFinished;
cfredriccc53588b2022-03-04 19:09:0096 UmaHistogramTimes(
Chris Fredrickson92e21d02022-04-22 21:16:1997 "Cookie.FirstPartySets.InitializationDuration.ReadComponentSets2",
cfredriccc53588b2022-03-04 19:09:0098 construction_timer_.Elapsed());
Shuran Huang71a22ff2022-02-11 14:54:1099 MaybeFinishLoading();
100}
101
102void FirstPartySetsLoader::DisposeFile(base::File sets_file) {
103 if (sets_file.IsValid()) {
104 base::ThreadPool::PostTask(
105 FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
106 base::BindOnce(
107 [](base::File sets_file) {
108 // Run `sets_file`'s dtor in the threadpool.
109 },
110 std::move(sets_file)));
111 }
112}
113
Shuran Huang71a22ff2022-02-11 14:54:10114void FirstPartySetsLoader::MaybeFinishLoading() {
115 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Chris Fredrickson9e17aa22022-09-20 15:57:16116 if (component_sets_parse_progress_ != Progress::kFinished ||
117 !manually_specified_set_.has_value()) {
Shuran Huang71a22ff2022-02-11 14:54:10118 return;
Chris Fredrickson9e17aa22022-09-20 15:57:16119 }
Chris Fredricksonad7f3c72022-09-14 15:11:45120 if (!manually_specified_set_->empty()) {
Chris Fredrickson0cad3722022-09-30 14:50:31121 sets_->ApplyManuallySpecifiedSet(manually_specified_set_->GetSet());
Chris Fredricksonad7f3c72022-09-14 15:11:45122 }
Chris Fredrickson0cad3722022-09-30 14:50:31123 std::move(on_load_complete_).Run(std::move(sets_).value());
Shuran Huang71a22ff2022-02-11 14:54:10124}
125
Kirubel Aklilu583f57ab2022-04-12 16:10:50126} // namespace content