1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/net_benchmarking.h"
10 #include "base/command_line.h"
11 #include "base/functional/bind.h"
12 #include "base/functional/callback_helpers.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/predictors/loading_predictor.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/net_benchmarking.mojom.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/storage_partition.h"
20 #include "mojo/public/cpp/bindings/self_owned_receiver.h"
21 #include "services/network/public/mojom/clear_data_filter.mojom.h"
22 #include "services/network/public/mojom/network_context.mojom.h"
24 using content::BrowserThread;
28 network::mojom::NetworkContext* GetNetworkContext(int render_process_id) {
29 content::RenderProcessHost* render_process_host =
30 content::RenderProcessHost::FromID(render_process_id);
31 if (!render_process_host)
33 return render_process_host->GetStoragePartition()->GetNetworkContext();
38 NetBenchmarking::NetBenchmarking(
39 base::WeakPtr<predictors::LoadingPredictor> loading_predictor,
40 int render_process_id)
41 : loading_predictor_(loading_predictor),
42 render_process_id_(render_process_id) {
43 DCHECK_CURRENTLY_ON(BrowserThread::UI);
46 NetBenchmarking::~NetBenchmarking() {
47 DCHECK_CURRENTLY_ON(BrowserThread::UI);
51 void NetBenchmarking::Create(
52 base::WeakPtr<predictors::LoadingPredictor> loading_predictor,
53 int render_process_id,
54 mojo::PendingReceiver<chrome::mojom::NetBenchmarking> receiver) {
55 DCHECK_CURRENTLY_ON(BrowserThread::UI);
56 mojo::MakeSelfOwnedReceiver(
57 std::make_unique<NetBenchmarking>(std::move(loading_predictor),
63 bool NetBenchmarking::CheckBenchmarkingEnabled() {
64 const base::CommandLine& command_line =
65 *base::CommandLine::ForCurrentProcess();
66 return command_line.HasSwitch(switches::kEnableNetBenchmarking);
69 void NetBenchmarking::ClearCache(ClearCacheCallback callback) {
70 DCHECK_CURRENTLY_ON(BrowserThread::UI);
71 auto* network_context = GetNetworkContext(render_process_id_);
72 CHECK(network_context);
73 network_context->ClearHttpCache(base::Time(), base::Time(), nullptr,
77 void NetBenchmarking::ClearHostResolverCache(
78 ClearHostResolverCacheCallback callback) {
79 DCHECK_CURRENTLY_ON(BrowserThread::UI);
80 auto* network_context = GetNetworkContext(render_process_id_);
81 CHECK(network_context);
82 network_context->ClearHostCache(nullptr, std::move(callback));
85 void NetBenchmarking::CloseCurrentConnections(
86 CloseCurrentConnectionsCallback callback) {
87 DCHECK_CURRENTLY_ON(BrowserThread::UI);
88 auto* network_context = GetNetworkContext(render_process_id_);
89 CHECK(network_context);
90 network_context->CloseAllConnections(std::move(callback));
93 void NetBenchmarking::ClearPredictorCache(
94 ClearPredictorCacheCallback callback) {
95 DCHECK_CURRENTLY_ON(BrowserThread::UI);
96 if (loading_predictor_)
97 loading_predictor_->resource_prefetch_predictor()->DeleteAllUrls();
98 std::move(callback).Run();