Avi Drissman | 4e1b7bc | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Nan Lin | 8a1108a | 2021-07-16 22:23:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/aggregation_service/aggregation_service_key_fetcher.h" |
| 6 | |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 7 | #include <memory> |
Dan McArdle | 08ad611 | 2023-11-21 20:39:47 | [diff] [blame] | 8 | #include <optional> |
Nan Lin | 8a1108a | 2021-07-16 22:23:21 | [diff] [blame] | 9 | #include <utility> |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 10 | #include <vector> |
Nan Lin | 8a1108a | 2021-07-16 22:23:21 | [diff] [blame] | 11 | |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 12 | #include "base/containers/circular_deque.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 13 | #include "base/functional/bind.h" |
| 14 | #include "base/functional/callback.h" |
Nan Lin | 8a1108a | 2021-07-16 22:23:21 | [diff] [blame] | 15 | #include "base/rand_util.h" |
Alex Turner | 7151277 | 2022-06-29 19:20:27 | [diff] [blame] | 16 | #include "content/browser/aggregation_service/aggregation_service_storage.h" |
Nan Lin | 89244d6 | 2021-11-12 21:38:17 | [diff] [blame] | 17 | #include "content/browser/aggregation_service/aggregation_service_storage_context.h" |
Nan Lin | 8a1108a | 2021-07-16 22:23:21 | [diff] [blame] | 18 | #include "services/network/public/cpp/is_potentially_trustworthy.h" |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 19 | #include "url/gurl.h" |
Nan Lin | 8a1108a | 2021-07-16 22:23:21 | [diff] [blame] | 20 | |
| 21 | namespace content { |
| 22 | |
| 23 | AggregationServiceKeyFetcher::AggregationServiceKeyFetcher( |
Nan Lin | 89244d6 | 2021-11-12 21:38:17 | [diff] [blame] | 24 | AggregationServiceStorageContext* storage_context, |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 25 | std::unique_ptr<NetworkFetcher> network_fetcher) |
Nan Lin | 89244d6 | 2021-11-12 21:38:17 | [diff] [blame] | 26 | : storage_context_(storage_context), |
| 27 | network_fetcher_(std::move(network_fetcher)) {} |
Nan Lin | 8a1108a | 2021-07-16 22:23:21 | [diff] [blame] | 28 | |
| 29 | AggregationServiceKeyFetcher::~AggregationServiceKeyFetcher() = default; |
| 30 | |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 31 | void AggregationServiceKeyFetcher::GetPublicKey(const GURL& url, |
Nan Lin | 8a1108a | 2021-07-16 22:23:21 | [diff] [blame] | 32 | FetchCallback callback) { |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 33 | DCHECK(network::IsUrlPotentiallyTrustworthy(url)); |
Nan Lin | 8a1108a | 2021-07-16 22:23:21 | [diff] [blame] | 34 | |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 35 | base::circular_deque<FetchCallback>& pending_callbacks = url_callbacks_[url]; |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 36 | bool in_progress = !pending_callbacks.empty(); |
| 37 | pending_callbacks.push_back(std::move(callback)); |
| 38 | |
| 39 | // If there is already a fetch request in progress, just enqueue the |
| 40 | // callback and return. |
| 41 | if (in_progress) |
| 42 | return; |
| 43 | |
| 44 | // First we check if we already have keys stored. |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 45 | // TODO(crbug.com/1223488): Pass url by value and move after C++17. |
Alex Turner | 7151277 | 2022-06-29 19:20:27 | [diff] [blame] | 46 | storage_context_->GetStorage() |
| 47 | .AsyncCall(&AggregationServiceStorage::GetPublicKeys) |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 48 | .WithArgs(url) |
Nan Lin | 8a1108a | 2021-07-16 22:23:21 | [diff] [blame] | 49 | .Then(base::BindOnce( |
| 50 | &AggregationServiceKeyFetcher::OnPublicKeysReceivedFromStorage, |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 51 | weak_factory_.GetWeakPtr(), url)); |
Nan Lin | 8a1108a | 2021-07-16 22:23:21 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void AggregationServiceKeyFetcher::OnPublicKeysReceivedFromStorage( |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 55 | const GURL& url, |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 56 | std::vector<PublicKey> keys) { |
| 57 | if (keys.empty()) { |
| 58 | // Fetch keys from the network if not found in the storage. |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 59 | FetchPublicKeysFromNetwork(url); |
Nan Lin | 8a1108a | 2021-07-16 22:23:21 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 63 | RunCallbacksForUrl(url, keys); |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 64 | } |
| 65 | |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 66 | void AggregationServiceKeyFetcher::FetchPublicKeysFromNetwork(const GURL& url) { |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 67 | if (!network_fetcher_) { |
| 68 | // Return error if fetching from network is not enabled. |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 69 | RunCallbacksForUrl(url, /*keys=*/{}); |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 70 | return; |
| 71 | } |
| 72 | |
| 73 | // Unretained is safe because the network fetcher is owned by `this` and will |
| 74 | // be deleted before `this`. |
| 75 | network_fetcher_->FetchPublicKeys( |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 76 | url, base::BindOnce( |
| 77 | &AggregationServiceKeyFetcher::OnPublicKeysReceivedFromNetwork, |
| 78 | base::Unretained(this), url)); |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void AggregationServiceKeyFetcher::OnPublicKeysReceivedFromNetwork( |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 82 | const GURL& url, |
Dan McArdle | 08ad611 | 2023-11-21 20:39:47 | [diff] [blame] | 83 | std::optional<PublicKeyset> keyset) { |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 84 | if (!keyset.has_value() || keyset->expiry_time.is_null()) { |
Dan McArdle | 08ad611 | 2023-11-21 20:39:47 | [diff] [blame] | 85 | // `keyset` will be std::nullopt if an error occurred and `expiry_time` |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 86 | // will be null if the freshness lifetime was zero. In these cases, we will |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 87 | // still update the keys for `url`, i,e. clear them. |
Alex Turner | 7151277 | 2022-06-29 19:20:27 | [diff] [blame] | 88 | storage_context_->GetStorage() |
| 89 | .AsyncCall(&AggregationServiceStorage::ClearPublicKeys) |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 90 | .WithArgs(url); |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 91 | } else { |
| 92 | // Store public keys fetched from network to storage, the old keys will be |
| 93 | // deleted from storage. |
Alex Turner | 7151277 | 2022-06-29 19:20:27 | [diff] [blame] | 94 | storage_context_->GetStorage() |
| 95 | .AsyncCall(&AggregationServiceStorage::SetPublicKeys) |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 96 | .WithArgs(url, keyset.value()); |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 97 | } |
| 98 | |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 99 | RunCallbacksForUrl( |
| 100 | url, keyset.has_value() ? keyset->keys : std::vector<PublicKey>()); |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 101 | } |
| 102 | |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 103 | void AggregationServiceKeyFetcher::RunCallbacksForUrl( |
| 104 | const GURL& url, |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 105 | const std::vector<PublicKey>& keys) { |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 106 | auto iter = url_callbacks_.find(url); |
| 107 | DCHECK(iter != url_callbacks_.end()); |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 108 | |
| 109 | base::circular_deque<FetchCallback> pending_callbacks = |
| 110 | std::move(iter->second); |
| 111 | DCHECK(!pending_callbacks.empty()); |
| 112 | |
Nan Lin | 4c75cd8 | 2022-02-18 23:39:34 | [diff] [blame] | 113 | url_callbacks_.erase(iter); |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 114 | |
| 115 | if (keys.empty()) { |
| 116 | // Return error, don't refetch to avoid infinite loop. |
| 117 | for (auto& callback : pending_callbacks) { |
Dan McArdle | 08ad611 | 2023-11-21 20:39:47 | [diff] [blame] | 118 | std::move(callback).Run(std::nullopt, |
Nan Lin | 48eace6 | 2021-09-23 20:13:29 | [diff] [blame] | 119 | PublicKeyFetchStatus::kPublicKeyFetchFailed); |
| 120 | } |
| 121 | } else { |
| 122 | for (auto& callback : pending_callbacks) { |
| 123 | // Each report should randomly select a key. This ensures that the set of |
| 124 | // reports a client sends are not a subset of the reports identified by |
| 125 | // any one key. |
| 126 | uint64_t key_index = base::RandGenerator(keys.size()); |
| 127 | std::move(callback).Run(keys[key_index], PublicKeyFetchStatus::kOk); |
| 128 | } |
| 129 | } |
Nan Lin | 8a1108a | 2021-07-16 22:23:21 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | } // namespace content |