blob: c4354f5081ca41bc4955ca388759f953fd8c8184 [file] [log] [blame]
dvallet233f57ab2016-12-21 03:17:101// Copyright (c) 2012 The Chromium Authors. All rights reserved.
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 "chrome/browser/net_benchmarking.h"
6
7#include <memory>
8#include <utility>
9
10#include "base/bind.h"
11#include "base/bind_helpers.h"
12#include "base/command_line.h"
13#include "base/macros.h"
Robbie McElrathb0f248c2018-07-24 19:44:1614#include "base/time/time.h"
Alexandr Ilin4909ec82018-06-06 10:22:4815#include "chrome/browser/predictors/loading_predictor.h"
dvallet233f57ab2016-12-21 03:17:1016#include "chrome/common/chrome_switches.h"
17#include "chrome/common/net_benchmarking.mojom.h"
18#include "content/public/browser/browser_thread.h"
Robbie McElrathb0f248c2018-07-24 19:44:1619#include "content/public/browser/render_process_host.h"
20#include "content/public/browser/storage_partition.h"
Julie Jeongeun Kim924acc82019-09-28 01:26:1021#include "mojo/public/cpp/bindings/self_owned_receiver.h"
Robbie McElrathb0f248c2018-07-24 19:44:1622#include "services/network/public/mojom/network_context.mojom.h"
dvallet233f57ab2016-12-21 03:17:1023
24using content::BrowserThread;
25
26namespace {
27
Robbie McElrathb0f248c2018-07-24 19:44:1628network::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)
32 return nullptr;
33 return render_process_host->GetStoragePartition()->GetNetworkContext();
dvallet233f57ab2016-12-21 03:17:1034}
35
36} // namespace
37
Alexandr Ilin4909ec82018-06-06 10:22:4838NetBenchmarking::NetBenchmarking(
39 base::WeakPtr<predictors::LoadingPredictor> loading_predictor,
Robbie McElrathb0f248c2018-07-24 19:44:1640 int render_process_id)
Alexandr Ilin4909ec82018-06-06 10:22:4841 : loading_predictor_(loading_predictor),
Robbie McElrathb0f248c2018-07-24 19:44:1642 render_process_id_(render_process_id) {
43 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Alexandr Ilin4909ec82018-06-06 10:22:4844}
dvallet233f57ab2016-12-21 03:17:1045
Alexandr Ilin4909ec82018-06-06 10:22:4846NetBenchmarking::~NetBenchmarking() {
Robbie McElrathb0f248c2018-07-24 19:44:1647 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Alexandr Ilin4909ec82018-06-06 10:22:4848}
dvallet233f57ab2016-12-21 03:17:1049
50// static
Alexandr Ilin4909ec82018-06-06 10:22:4851void NetBenchmarking::Create(
52 base::WeakPtr<predictors::LoadingPredictor> loading_predictor,
Robbie McElrathb0f248c2018-07-24 19:44:1653 int render_process_id,
Julie Jeongeun Kim924acc82019-09-28 01:26:1054 mojo::PendingReceiver<chrome::mojom::NetBenchmarking> receiver) {
Robbie McElrathb0f248c2018-07-24 19:44:1655 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Julie Jeongeun Kim924acc82019-09-28 01:26:1056 mojo::MakeSelfOwnedReceiver(
57 std::make_unique<NetBenchmarking>(std::move(loading_predictor),
58 render_process_id),
59 std::move(receiver));
dvallet233f57ab2016-12-21 03:17:1060}
61
62// static
63bool NetBenchmarking::CheckBenchmarkingEnabled() {
64 const base::CommandLine& command_line =
65 *base::CommandLine::ForCurrentProcess();
66 return command_line.HasSwitch(switches::kEnableNetBenchmarking);
67}
68
tzikcd02d3b2018-08-09 21:32:4769void NetBenchmarking::ClearCache(ClearCacheCallback callback) {
Robbie McElrathb0f248c2018-07-24 19:44:1670 DCHECK_CURRENTLY_ON(BrowserThread::UI);
71 auto* network_context = GetNetworkContext(render_process_id_);
tzikcd02d3b2018-08-09 21:32:4772 CHECK(network_context);
73 network_context->ClearHttpCache(base::Time(), base::Time(), nullptr,
74 std::move(callback));
dvallet233f57ab2016-12-21 03:17:1075}
76
77void NetBenchmarking::ClearHostResolverCache(
tzikcd02d3b2018-08-09 21:32:4778 ClearHostResolverCacheCallback callback) {
Robbie McElrathb0f248c2018-07-24 19:44:1679 DCHECK_CURRENTLY_ON(BrowserThread::UI);
80 auto* network_context = GetNetworkContext(render_process_id_);
tzikcd02d3b2018-08-09 21:32:4781 CHECK(network_context);
82 network_context->ClearHostCache(nullptr, std::move(callback));
dvallet233f57ab2016-12-21 03:17:1083}
84
85void NetBenchmarking::CloseCurrentConnections(
tzikcd02d3b2018-08-09 21:32:4786 CloseCurrentConnectionsCallback callback) {
Robbie McElrathb0f248c2018-07-24 19:44:1687 DCHECK_CURRENTLY_ON(BrowserThread::UI);
88 auto* network_context = GetNetworkContext(render_process_id_);
tzikcd02d3b2018-08-09 21:32:4789 CHECK(network_context);
90 network_context->CloseAllConnections(std::move(callback));
dvallet233f57ab2016-12-21 03:17:1091}
92
93void NetBenchmarking::ClearPredictorCache(
tzikcd02d3b2018-08-09 21:32:4794 ClearPredictorCacheCallback callback) {
Robbie McElrathb0f248c2018-07-24 19:44:1695 DCHECK_CURRENTLY_ON(BrowserThread::UI);
96 if (loading_predictor_)
97 loading_predictor_->resource_prefetch_predictor()->DeleteAllUrls();
tzikcd02d3b2018-08-09 21:32:4798 std::move(callback).Run();
dvallet233f57ab2016-12-21 03:17:1099}