net_log_proxy_source.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #ifndef COMPONENTS_NET_LOG_NET_LOG_PROXY_SOURCE_H_
  5. #define COMPONENTS_NET_LOG_NET_LOG_PROXY_SOURCE_H_
  6. #include "base/memory/weak_ptr.h"
  7. #include "mojo/public/cpp/bindings/receiver.h"
  8. #include "mojo/public/cpp/bindings/remote.h"
  9. #include "net/log/net_log.h"
  10. #include "services/network/public/mojom/network_service.mojom.h"
  11. namespace base {
  12. class SequencedTaskRunner;
  13. }
  14. namespace net_log {
  15. // Implementation of NetLogProxySource mojo interface which observes local
  16. // NetLog events and proxies them over mojo to a remote NetLogProxySink.
  17. // Observing and proxying is only active when notified through
  18. // UpdateCaptureModes() that capturing is active in the remote process.
  19. class NetLogProxySource : public net::NetLog::ThreadSafeObserver,
  20. public network::mojom::NetLogProxySource {
  21. public:
  22. // When notified through |proxy_source_receiver| that capturing is active,
  23. // registers a local NetLog observer and sends all NetLog events to
  24. // |proxy_sink_remote|.
  25. // The caller is expected to create a new NetLogProxySource if the remote
  26. // process is restarted.
  27. NetLogProxySource(
  28. mojo::PendingReceiver<network::mojom::NetLogProxySource>
  29. proxy_source_receiver,
  30. mojo::Remote<network::mojom::NetLogProxySink> proxy_sink_remote);
  31. ~NetLogProxySource() override;
  32. NetLogProxySource(const NetLogProxySource&) = delete;
  33. NetLogProxySource& operator=(const NetLogProxySource&) = delete;
  34. // Remove NetLog observer and close mojo pipes.
  35. void ShutDown();
  36. // NetLog::ThreadSafeObserver:
  37. void OnAddEntry(const net::NetLogEntry& entry) override;
  38. // mojom::NetLogProxySource:
  39. void UpdateCaptureModes(net::NetLogCaptureModeSet modes) override;
  40. private:
  41. // Proxy entry to the remote. Must only be called on |task_runner_|.
  42. void SendNetLogEntry(net::NetLogEventType type,
  43. net::NetLogSourceType source_type,
  44. uint32_t source_id,
  45. base::TimeTicks source_start_time,
  46. net::NetLogEventPhase phase,
  47. base::TimeTicks time,
  48. base::Value params);
  49. mojo::Receiver<network::mojom::NetLogProxySource> proxy_source_receiver_;
  50. mojo::Remote<network::mojom::NetLogProxySink> proxy_sink_remote_;
  51. scoped_refptr<base::SequencedTaskRunner> task_runner_;
  52. // A WeakPtr to |this|, which is used when posting tasks from the
  53. // NetLog::ThreadSafeObserver to |task_runner_|. This single WeakPtr instance
  54. // is used for all tasks as the ThreadSafeObserver may call on any thread, so
  55. // the weak_factory_ cannot be accessed safely from those threads.
  56. base::WeakPtr<NetLogProxySource> weak_this_;
  57. base::WeakPtrFactory<NetLogProxySource> weak_factory_{this};
  58. };
  59. } // namespace net_log
  60. #endif // COMPONENTS_NET_LOG_NET_LOG_PROXY_SOURCE_H_