File: net_log_proxy_source.cc

package info (click to toggle)
chromium 138.0.7204.92-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,576 kB
  • sloc: cpp: 34,933,512; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,956; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (99 lines) | stat: -rw-r--r-- 3,776 bytes parent folder | download | duplicates (6)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/net_log/net_log_proxy_source.h"
#include "base/task/sequenced_task_runner.h"

namespace net_log {

NetLogProxySource::NetLogProxySource(
    mojo::PendingReceiver<network::mojom::NetLogProxySource>
        proxy_source_receiver,
    mojo::Remote<network::mojom::NetLogProxySink> proxy_sink_remote)
    : proxy_source_receiver_(this, std::move(proxy_source_receiver)),
      proxy_sink_remote_(std::move(proxy_sink_remote)),
      task_runner_(base::SequencedTaskRunner::GetCurrentDefault()) {
  // 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();
}

NetLogProxySource::~NetLogProxySource() {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());
  // Just in case ShutDown() was not called, make sure observer is removed.
  UpdateCaptureModes(0);
}

void NetLogProxySource::ShutDown() {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());
  UpdateCaptureModes(0);
  weak_factory_.InvalidateWeakPtrs();
  proxy_source_receiver_.reset();
  proxy_sink_remote_.reset();
}

void NetLogProxySource::OnAddEntry(const net::NetLogEntry& entry) {
  if (task_runner_->RunsTasksInCurrentSequence()) {
    SendNetLogEntry(entry.type, entry.source, entry.phase, entry.time,
                    entry.params.Clone());
  } else {
    task_runner_->PostTask(
        FROM_HERE,
        base::BindOnce(&NetLogProxySource::SendNetLogEntry, weak_this_,
                       entry.type, entry.source, entry.phase, entry.time,
                       entry.params.Clone()));
  }
}

void NetLogProxySource::UpdateCaptureModes(
    net::NetLogCaptureModeSet new_modes) {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());

  // Not capturing, remove observer if necessary.
  if (new_modes == 0) {
    if (net_log())
      net::NetLog::Get()->RemoveObserver(this);
    return;
  }

  // NetLog allows multiple observers to be registered at once, and each might
  // have a different capture mode. In the common case there would normally be
  // at most one observer registered. To avoid the complication of sending
  // different sets of params for each capture mode, if multiple capture modes
  // are active, only one observer is registered in the source process, using
  // the lowest common denominator capture mode. This handles the common case,
  // and in the rare case where multiple capture modes active, is a safe
  // fallback.
  //
  // Register observer for the lowest level that is set in |new_modes|.
  for (int i = 0; i <= static_cast<int>(net::NetLogCaptureMode::kLast); ++i) {
    net::NetLogCaptureMode mode = static_cast<net::NetLogCaptureMode>(i);
    if (net::NetLogCaptureModeSetContains(mode, new_modes)) {
      if (net_log() && capture_mode() == mode) {
        // Already listening at the desired level.
        return;
      }
      if (net_log()) {
        // Listening at the wrong level, remove observer.
        net::NetLog::Get()->RemoveObserver(this);
      }

      net::NetLog::Get()->AddObserver(this, mode);
      return;
    }
  }

  NOTREACHED();
}

void NetLogProxySource::SendNetLogEntry(net::NetLogEventType type,
                                        const net::NetLogSource& net_log_source,
                                        net::NetLogEventPhase phase,
                                        base::TimeTicks time,
                                        base::Value::Dict params) {
  proxy_sink_remote_->AddEntry(static_cast<uint32_t>(type), net_log_source,
                               phase, time, std::move(params));
}

}  // namespace net_log