fake_port_allocator.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2014 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 "remoting/test/fake_port_allocator.h"
  5. #include <memory>
  6. #include "remoting/protocol/transport_context.h"
  7. #include "remoting/test/fake_network_dispatcher.h"
  8. #include "remoting/test/fake_network_manager.h"
  9. #include "remoting/test/fake_socket_factory.h"
  10. #include "third_party/abseil-cpp/absl/strings/string_view.h"
  11. #include "third_party/webrtc/p2p/client/basic_port_allocator.h"
  12. namespace remoting {
  13. namespace {
  14. class FakePortAllocatorSession : public cricket::BasicPortAllocatorSession {
  15. public:
  16. FakePortAllocatorSession(FakePortAllocator* allocator,
  17. const std::string& content_name,
  18. int component,
  19. const std::string& ice_username_fragment,
  20. const std::string& ice_password);
  21. FakePortAllocatorSession(const FakePortAllocatorSession&) = delete;
  22. FakePortAllocatorSession& operator=(const FakePortAllocatorSession&) = delete;
  23. ~FakePortAllocatorSession() override;
  24. };
  25. FakePortAllocatorSession::FakePortAllocatorSession(
  26. FakePortAllocator* allocator,
  27. const std::string& content_name,
  28. int component,
  29. const std::string& ice_username_fragment,
  30. const std::string& ice_password)
  31. : BasicPortAllocatorSession(allocator,
  32. content_name,
  33. component,
  34. ice_username_fragment,
  35. ice_password) {}
  36. FakePortAllocatorSession::~FakePortAllocatorSession() = default;
  37. } // namespace
  38. FakePortAllocator::FakePortAllocator(
  39. rtc::NetworkManager* network_manager,
  40. rtc::PacketSocketFactory* socket_factory,
  41. scoped_refptr<protocol::TransportContext> transport_context)
  42. : BasicPortAllocator(network_manager, socket_factory),
  43. transport_context_(transport_context) {
  44. set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
  45. cricket::PORTALLOCATOR_ENABLE_IPV6 |
  46. cricket::PORTALLOCATOR_DISABLE_STUN |
  47. cricket::PORTALLOCATOR_DISABLE_RELAY);
  48. Initialize();
  49. }
  50. FakePortAllocator::~FakePortAllocator() = default;
  51. cricket::PortAllocatorSession* FakePortAllocator::CreateSessionInternal(
  52. absl::string_view content_name,
  53. int component,
  54. absl::string_view ice_username_fragment,
  55. absl::string_view ice_password) {
  56. return new FakePortAllocatorSession(
  57. this, std::string(content_name), component,
  58. std::string(ice_username_fragment), std::string(ice_password));
  59. }
  60. FakePortAllocatorFactory::FakePortAllocatorFactory(
  61. scoped_refptr<FakeNetworkDispatcher> fake_network_dispatcher) {
  62. socket_factory_ =
  63. std::make_unique<FakePacketSocketFactory>(fake_network_dispatcher.get());
  64. network_manager_ =
  65. std::make_unique<FakeNetworkManager>(socket_factory_->GetAddress());
  66. }
  67. FakePortAllocatorFactory::~FakePortAllocatorFactory() = default;
  68. std::unique_ptr<cricket::PortAllocator>
  69. FakePortAllocatorFactory::CreatePortAllocator(
  70. scoped_refptr<protocol::TransportContext> transport_context,
  71. base::WeakPtr<protocol::SessionOptionsProvider> session_options_provider) {
  72. return std::make_unique<FakePortAllocator>(
  73. network_manager_.get(), socket_factory_.get(), transport_context);
  74. }
  75. } // namespace remoting