Skip to content

Commit 82d9aac

Browse files
stamphoMichal Klocek
authored andcommitted
[266]Make NetworkContext resettable
Expose StoragePartitionImpl::InitNetworkContext() and add StoragePartition::NetworkContextCreatedObserver. The public StoragePartitionImpl::ResetNetworkContext() method allows recreating the NetworkContext used inside a storage partition so as to change network settings dynamically. The old network context is destroyed before creating the replacement. The observer notifies when NetworkContext is created and makes it possible to check if StoragePartitionImpl::ResetNetworkContext() is done with its asynchronous tasks. =============================================================== We posted the OnNetworkContextCreated call right after the construction of NetworkContext, however its client() is not guaranteed to exist at this point. Magically it worked for a long time, because typically the next call after construction is SetClient(), which (most of the time) can run before the posted lambda. The following change [1] introduced a new mojo call between the constructor and SetClient, which defers SetClient and so we miss the opportunity to notify client() in the lambda. Moving OnNetworkContextCreated() to SetClient() seems to be a good solution, because setting the client typically means that we finished the construction of a NetworkContext. This brings back clearHttpCacheCompleted signal to life and fixes tst_QWebEngineProfile::clearDataFromCache(). [1] https://chromium-review.googlesource.com/c/chromium/src/+/5440136 Task-number: QTBUG-81558 Task-number: QTBUG-89670 Task-number: QTBUG-111541 Change-Id: I5f4ea50650b35e1f35ad67a009964e76038ea750 Reviewed-on: https://codereview.qt-project.org/c/qt/qtwebengine-chromium/+/493884 Reviewed-by: Michal Klocek <michal.klocek@qt.io>
1 parent ac1ffd2 commit 82d9aac

File tree

7 files changed

+51
-2
lines changed

7 files changed

+51
-2
lines changed

chromium/content/browser/preloading/prefetch/prefetch_network_context_client.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,6 @@ void PrefetchNetworkContextClient::OnCanSendSCTAuditingReport(
5757
void PrefetchNetworkContextClient::OnNewSCTAuditingReportSent() {}
5858
#endif
5959

60+
void PrefetchNetworkContextClient::OnNetworkContextCreated() {}
61+
6062
} // namespace content

chromium/content/browser/preloading/prefetch/prefetch_network_context_client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class PrefetchNetworkContextClient
4646
OnCanSendSCTAuditingReportCallback callback) override;
4747
void OnNewSCTAuditingReportSent() override;
4848
#endif
49+
void OnNetworkContextCreated() override;
4950
};
5051

5152
} // namespace content

chromium/content/browser/storage_partition_impl.cc

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3223,6 +3223,14 @@ void StoragePartitionImpl::RemoveObserver(DataRemovalObserver* observer) {
32233223
data_removal_observers_.RemoveObserver(observer);
32243224
}
32253225

3226+
void StoragePartitionImpl::SetNetworkContextCreatedObserver(NetworkContextCreatedObserver *observer) {
3227+
network_context_created_observer_ = observer;
3228+
}
3229+
3230+
StoragePartition::NetworkContextCreatedObserver *StoragePartitionImpl::GetNetworkContextCreatedObserver() {
3231+
return network_context_created_observer_;
3232+
}
3233+
32263234
void StoragePartitionImpl::FlushNetworkInterfaceForTesting() {
32273235
CHECK(initialized_);
32283236
DCHECK(network_context_owner_->network_context);
@@ -3515,7 +3523,11 @@ void StoragePartitionImpl::InitNetworkContext() {
35153523
cookie_deprecation_label_manager_->GetValue().value_or("");
35163524
}
35173525

3518-
network_context_owner_->network_context.reset();
3526+
if (network_context_owner_->network_context.is_bound()) {
3527+
network_context_owner_->network_context.set_disconnect_handler(base::OnceClosure());
3528+
network_context_owner_->network_context.reset();
3529+
network_context_client_receiver_.reset();
3530+
}
35193531
CreateNetworkContextInNetworkService(
35203532
network_context_owner_->network_context.BindNewPipeAndPassReceiver(),
35213533
std::move(context_params));
@@ -3530,13 +3542,19 @@ void StoragePartitionImpl::InitNetworkContext() {
35303542
network_context_owner_->network_context->RevokeNetworkForNonces(
35313543
nonces, base::NullCallback());
35323544

3533-
network_context_client_receiver_.reset();
35343545
network_context_owner_->network_context->SetClient(
35353546
network_context_client_receiver_.BindNewPipeAndPassRemote());
35363547
network_context_owner_->network_context.set_disconnect_handler(base::BindOnce(
35373548
&StoragePartitionImpl::InitNetworkContext, weak_factory_.GetWeakPtr()));
35383549
}
35393550

3551+
void StoragePartitionImpl::OnNetworkContextCreated() {
3552+
#if BUILDFLAG(IS_QTWEBENGINE)
3553+
if (network_context_created_observer_)
3554+
network_context_created_observer_->OnNetworkContextCreated(this);
3555+
#endif
3556+
}
3557+
35403558
network::mojom::URLLoaderFactoryParamsPtr
35413559
StoragePartitionImpl::CreateURLLoaderFactoryParams() {
35423560
network::mojom::URLLoaderFactoryParamsPtr params =

chromium/content/browser/storage_partition_impl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ class CONTENT_EXPORT StoragePartitionImpl
266266
void ClearBluetoothAllowedDevicesMapForTesting() override;
267267
void AddObserver(DataRemovalObserver* observer) override;
268268
void RemoveObserver(DataRemovalObserver* observer) override;
269+
void SetNetworkContextCreatedObserver(NetworkContextCreatedObserver *observer) override;
270+
NetworkContextCreatedObserver *GetNetworkContextCreatedObserver() override;
269271
void FlushNetworkInterfaceForTesting() override;
270272
void FlushCertVerifierInterfaceForTesting() override;
271273
void WaitForDeletionTasksForTesting() override;
@@ -344,6 +346,7 @@ class CONTENT_EXPORT StoragePartitionImpl
344346
OnCanSendSCTAuditingReportCallback callback) override;
345347
void OnNewSCTAuditingReportSent() override;
346348
#endif
349+
void OnNetworkContextCreated() override;
347350

348351
// network::mojom::URLLoaderNetworkServiceObserver interface.
349352
void OnSSLCertificateError(const GURL& url,
@@ -483,6 +486,9 @@ class CONTENT_EXPORT StoragePartitionImpl
483486

484487
std::vector<std::string> GetCorsExemptHeaderList();
485488

489+
#if BUILDFLAG(IS_QTWEBENGINE)
490+
void ResetNetworkContext() { InitNetworkContext(); }
491+
#endif
486492
void OpenLocalStorageForProcess(
487493
int process_id,
488494
const blink::StorageKey& storage_key,
@@ -930,6 +936,8 @@ class CONTENT_EXPORT StoragePartitionImpl
930936
bool on_browser_context_will_be_destroyed_called_ = false;
931937
#endif
932938

939+
NetworkContextCreatedObserver *network_context_created_observer_ = nullptr;
940+
933941
// A copy of the network revocation nonces in `NetworkContext`. It is used for
934942
// restoring the network revocation states of fenced frames when there is a
935943
// `NetworkService` crash.

chromium/content/public/browser/storage_partition.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,11 @@ class CONTENT_EXPORT StoragePartition {
290290
const base::Time end) = 0;
291291
};
292292

293+
class NetworkContextCreatedObserver {
294+
public:
295+
virtual void OnNetworkContextCreated(StoragePartition *storage) = 0;
296+
};
297+
293298
// Similar to ClearDataForOrigin().
294299
// Deletes all data out for the StoragePartition if |storage_key|'s origin is
295300
// opaque. |callback| is called when data deletion is done or at least the
@@ -355,6 +360,9 @@ class CONTENT_EXPORT StoragePartition {
355360

356361
virtual void RemoveObserver(DataRemovalObserver* observer) = 0;
357362

363+
virtual void SetNetworkContextCreatedObserver(NetworkContextCreatedObserver *observer) = 0;
364+
virtual NetworkContextCreatedObserver *GetNetworkContextCreatedObserver() = 0;
365+
358366
// Clear the bluetooth allowed devices map. For test use only.
359367
virtual void ClearBluetoothAllowedDevicesMapForTesting() = 0;
360368

chromium/services/network/network_context.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,15 @@ void NetworkContext::SetClient(
996996
mojo::PendingRemote<mojom::NetworkContextClient> client) {
997997
client_.reset();
998998
client_.Bind(std::move(client));
999+
1000+
#if BUILDFLAG(IS_QTWEBENGINE)
1001+
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
1002+
FROM_HERE, base::BindOnce([](NetworkContext *context) {
1003+
if (context->client()) {
1004+
context->client()->OnNetworkContextCreated();
1005+
}
1006+
}, this));
1007+
#endif
9991008
}
10001009

10011010
void NetworkContext::CreateURLLoaderFactory(

chromium/services/network/public/mojom/network_context_client.mojom

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,7 @@ interface NetworkContextClient {
5959
// clients.
6060
[EnableIf=is_ct_supported]
6161
OnNewSCTAuditingReportSent();
62+
63+
// Called when NetworkContext is created in the network service.
64+
OnNetworkContextCreated();
6265
};

0 commit comments

Comments
 (0)