net_log_proxy_source.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2020 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. #include "components/net_log/net_log_proxy_source.h"
  5. namespace net_log {
  6. NetLogProxySource::NetLogProxySource(
  7. mojo::PendingReceiver<network::mojom::NetLogProxySource>
  8. proxy_source_receiver,
  9. mojo::Remote<network::mojom::NetLogProxySink> proxy_sink_remote)
  10. : proxy_source_receiver_(this, std::move(proxy_source_receiver)),
  11. proxy_sink_remote_(std::move(proxy_sink_remote)),
  12. task_runner_(base::SequencedTaskRunnerHandle::Get()) {
  13. // Initialize a WeakPtr instance that can be safely referred to from other
  14. // threads when binding tasks posted back to this thread.
  15. weak_this_ = weak_factory_.GetWeakPtr();
  16. }
  17. NetLogProxySource::~NetLogProxySource() {
  18. DCHECK(task_runner_->RunsTasksInCurrentSequence());
  19. // Just in case ShutDown() was not called, make sure observer is removed.
  20. UpdateCaptureModes(0);
  21. }
  22. void NetLogProxySource::ShutDown() {
  23. DCHECK(task_runner_->RunsTasksInCurrentSequence());
  24. UpdateCaptureModes(0);
  25. weak_factory_.InvalidateWeakPtrs();
  26. proxy_source_receiver_.reset();
  27. proxy_sink_remote_.reset();
  28. }
  29. void NetLogProxySource::OnAddEntry(const net::NetLogEntry& entry) {
  30. if (task_runner_->RunsTasksInCurrentSequence()) {
  31. SendNetLogEntry(entry.type, entry.source.type, entry.source.id,
  32. entry.source.start_time, entry.phase, entry.time,
  33. entry.params.Clone());
  34. } else {
  35. task_runner_->PostTask(
  36. FROM_HERE,
  37. base::BindOnce(&NetLogProxySource::SendNetLogEntry, weak_this_,
  38. entry.type, entry.source.type, entry.source.id,
  39. entry.source.start_time, entry.phase, entry.time,
  40. entry.params.Clone()));
  41. }
  42. }
  43. void NetLogProxySource::UpdateCaptureModes(
  44. net::NetLogCaptureModeSet new_modes) {
  45. DCHECK(task_runner_->RunsTasksInCurrentSequence());
  46. // Not capturing, remove observer if necessary.
  47. if (new_modes == 0) {
  48. if (net_log())
  49. net::NetLog::Get()->RemoveObserver(this);
  50. return;
  51. }
  52. // NetLog allows multiple observers to be registered at once, and each might
  53. // have a different capture mode. In the common case there would normally be
  54. // at most one observer registered. To avoid the complication of sending
  55. // different sets of params for each capture mode, if multiple capture modes
  56. // are active, only one observer is registered in the source process, using
  57. // the lowest common denominator capture mode. This handles the common case,
  58. // and in the rare case where multiple capture modes active, is a safe
  59. // fallback.
  60. //
  61. // Register observer for the lowest level that is set in |new_modes|.
  62. for (int i = 0; i <= static_cast<int>(net::NetLogCaptureMode::kLast); ++i) {
  63. net::NetLogCaptureMode mode = static_cast<net::NetLogCaptureMode>(i);
  64. if (net::NetLogCaptureModeSetContains(mode, new_modes)) {
  65. if (net_log() && capture_mode() == mode) {
  66. // Already listening at the desired level.
  67. return;
  68. }
  69. if (net_log()) {
  70. // Listening at the wrong level, remove observer.
  71. net::NetLog::Get()->RemoveObserver(this);
  72. }
  73. net::NetLog::Get()->AddObserver(this, mode);
  74. return;
  75. }
  76. }
  77. NOTREACHED();
  78. }
  79. void NetLogProxySource::SendNetLogEntry(net::NetLogEventType type,
  80. net::NetLogSourceType source_type,
  81. uint32_t source_id,
  82. base::TimeTicks source_start_time,
  83. net::NetLogEventPhase phase,
  84. base::TimeTicks time,
  85. base::Value params) {
  86. proxy_sink_remote_->AddEntry(
  87. static_cast<uint32_t>(type), static_cast<uint32_t>(source_type),
  88. source_id, source_start_time, phase, time, std::move(params));
  89. }
  90. } // namespace net_log