summaryrefslogtreecommitdiff
path: root/chromium/services/network/net_log_proxy_sink.cc
blob: 78802920c08b64229ddb2487b59a20bf47cef74e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "services/network/net_log_proxy_sink.h"

namespace network {

NetLogProxySink::NetLogProxySink()
    : task_runner_(base::SequencedTaskRunnerHandle::Get()) {
  // Initialize a WeakPtr instance that can be safely referred to from other
  // threads when binding tasks posted back to this thread.
  weak_this_ = weak_factory_.GetWeakPtr();
  net::NetLog::Get()->AddCaptureModeObserver(this);
}

NetLogProxySink::~NetLogProxySink() {
  net::NetLog::Get()->RemoveCaptureModeObserver(this);
}

void NetLogProxySink::AttachSource(
    mojo::PendingRemote<network::mojom::NetLogProxySource> proxy_source_remote,
    mojo::PendingReceiver<network::mojom::NetLogProxySink>
        proxy_sink_receiver) {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());

  // Initialize remote with current capturing state. (Netlog capturing might
  // already be active when NetLogProxySource gets attached.)
  mojo::Remote<network::mojom::NetLogProxySource> bound_remote(
      std::move(proxy_source_remote));
  bound_remote->UpdateCaptureModes(GetObserverCaptureModes());

  proxy_source_remotes_.Add(std::move(bound_remote));
  proxy_sink_receivers_.Add(this, std::move(proxy_sink_receiver));
}

void NetLogProxySink::OnCaptureModeUpdated(net::NetLogCaptureModeSet modes) {
  if (!task_runner_->RunsTasksInCurrentSequence()) {
    task_runner_->PostTask(
        FROM_HERE, base::BindOnce(&NetLogProxySink::OnCaptureModeUpdated,
                                  weak_this_, modes));
    return;
  }

  for (const auto& source : proxy_source_remotes_) {
    source->UpdateCaptureModes(modes);
  }
}

void NetLogProxySink::AddEntry(uint32_t type,
                               uint32_t source_type,
                               uint32_t source_id,
                               base::TimeTicks source_start_time,
                               net::NetLogEventPhase phase,
                               base::TimeTicks time,
                               base::Value params) {
  // Note: There is a possible race condition, where the NetLog capture mode
  // changes, but the other process is still sending events for the old capture
  // mode, and thus might log events with a higher than expected capture mode.
  // (But if capturing is completely disabled and the other side still has some
  // events in the pipe, AddEntryWithMaterializedParams will do nothing, since
  // that implies no observers are registered.)
  // TODO(mattm): Remote side could send the capture mode along with the event,
  // and then check here before logging that the current capture mode still is
  // compatible.
  AddEntryAtTimeWithMaterializedParams(
      static_cast<net::NetLogEventType>(type),
      net::NetLogSource(static_cast<net::NetLogSourceType>(source_type),
                        source_id, source_start_time),
      phase, time, std::move(params));
}

}  // namespace network