net_log_proxy_sink.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 SERVICES_NETWORK_NET_LOG_PROXY_SINK_H_
  5. #define SERVICES_NETWORK_NET_LOG_PROXY_SINK_H_
  6. #include "base/component_export.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "mojo/public/cpp/bindings/receiver_set.h"
  9. #include "mojo/public/cpp/bindings/remote_set.h"
  10. #include "net/log/net_log.h"
  11. #include "services/network/public/mojom/network_service.mojom.h"
  12. namespace base {
  13. class SequencedTaskRunner;
  14. }
  15. namespace network {
  16. // Implementation of NetLogProxySink mojo interface which receives external
  17. // NetLog events and injects them into the NetLog. Also notifies attached
  18. // sources when the NetLog capture mode changes.
  19. class COMPONENT_EXPORT(NETWORK_SERVICE) NetLogProxySink
  20. : public net::NetLog::ThreadSafeCaptureModeObserver,
  21. public network::mojom::NetLogProxySink {
  22. public:
  23. NetLogProxySink();
  24. ~NetLogProxySink() override;
  25. NetLogProxySink(const NetLogProxySink&) = delete;
  26. NetLogProxySink& operator=(const NetLogProxySink&) = delete;
  27. // Attaches a source of external NetLog events. The source is automatically
  28. // removed if the mojo pipes are closed.
  29. void AttachSource(mojo::PendingRemote<network::mojom::NetLogProxySource>
  30. proxy_source_remote,
  31. mojo::PendingReceiver<network::mojom::NetLogProxySink>
  32. proxy_sink_receiver);
  33. // net::NetLog::ThreadSafeCaptureModeObserver:
  34. // Notifies attached sources that the capture mode has changed.
  35. void OnCaptureModeUpdated(net::NetLogCaptureModeSet modes) override;
  36. // mojom::NetLogProxySink:
  37. void AddEntry(uint32_t type,
  38. uint32_t source_type,
  39. uint32_t source_id,
  40. base::TimeTicks source_start_time,
  41. net::NetLogEventPhase phase,
  42. base::TimeTicks time,
  43. base::Value params) override;
  44. private:
  45. mojo::RemoteSet<network::mojom::NetLogProxySource> proxy_source_remotes_;
  46. mojo::ReceiverSet<network::mojom::NetLogProxySink> proxy_sink_receivers_;
  47. scoped_refptr<base::SequencedTaskRunner> task_runner_;
  48. // A WeakPtr to |this|, which is used when posting tasks from the
  49. // NetLog::ThreadSafeCaptureModeObserver to |task_runner_|. This single
  50. // WeakPtr instance is used for all tasks as the ThreadSafeObserver may call
  51. // on any thread, so the weak_factory_ cannot be accessed safely from those
  52. // threads.
  53. base::WeakPtr<NetLogProxySink> weak_this_;
  54. base::WeakPtrFactory<NetLogProxySink> weak_factory_{this};
  55. };
  56. } // namespace network
  57. #endif // SERVICES_NETWORK_NET_LOG_PROXY_SINK_H_