device_media_to_mojo_adapter_unittest.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2018 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 "services/video_capture/device_media_to_mojo_adapter.h"
  5. #include "base/callback_helpers.h"
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/run_loop.h"
  8. #include "base/test/task_environment.h"
  9. #include "build/chromeos_buildflags.h"
  10. #include "media/capture/video/mock_device.h"
  11. #include "mojo/public/cpp/bindings/pending_remote.h"
  12. #include "services/video_capture/public/cpp/mock_video_frame_handler.h"
  13. #include "services/video_capture/public/mojom/video_frame_handler.mojom.h"
  14. #include "testing/gmock/include/gmock/gmock.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. using testing::Invoke;
  17. using testing::_;
  18. namespace video_capture {
  19. class DeviceMediaToMojoAdapterTest : public ::testing::Test {
  20. public:
  21. DeviceMediaToMojoAdapterTest() = default;
  22. ~DeviceMediaToMojoAdapterTest() override = default;
  23. void SetUp() override {
  24. mock_video_frame_handler_ = std::make_unique<MockVideoFrameHandler>(
  25. video_frame_handler_.InitWithNewPipeAndPassReceiver());
  26. auto mock_device = std::make_unique<media::MockDevice>();
  27. mock_device_ptr_ = mock_device.get();
  28. #if BUILDFLAG(IS_CHROMEOS_ASH)
  29. adapter_ = std::make_unique<DeviceMediaToMojoAdapter>(
  30. std::move(mock_device), base::DoNothing(),
  31. base::ThreadTaskRunnerHandle::Get());
  32. #elif BUILDFLAG(IS_WIN)
  33. adapter_ = std::make_unique<DeviceMediaToMojoAdapter>(
  34. std::move(mock_device), nullptr);
  35. #else
  36. adapter_ =
  37. std::make_unique<DeviceMediaToMojoAdapter>(std::move(mock_device));
  38. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  39. }
  40. void TearDown() override {
  41. // The internals of ReceiverOnTaskRunner perform a DeleteSoon().
  42. adapter_.reset();
  43. base::RunLoop wait_loop;
  44. wait_loop.RunUntilIdle();
  45. }
  46. protected:
  47. raw_ptr<media::MockDevice> mock_device_ptr_;
  48. std::unique_ptr<DeviceMediaToMojoAdapter> adapter_;
  49. std::unique_ptr<MockVideoFrameHandler> mock_video_frame_handler_;
  50. mojo::PendingRemote<mojom::VideoFrameHandler> video_frame_handler_;
  51. base::test::TaskEnvironment task_environment_;
  52. };
  53. TEST_F(DeviceMediaToMojoAdapterTest,
  54. DeviceIsStoppedWhenReceiverClosesConnection) {
  55. {
  56. base::RunLoop run_loop;
  57. EXPECT_CALL(*mock_device_ptr_, DoAllocateAndStart(_, _))
  58. .WillOnce(Invoke(
  59. [](const media::VideoCaptureParams& params,
  60. std::unique_ptr<media::VideoCaptureDevice::Client>* client) {
  61. (*client)->OnStarted();
  62. }));
  63. EXPECT_CALL(*mock_video_frame_handler_, OnStarted())
  64. .WillOnce(Invoke([&run_loop]() { run_loop.Quit(); }));
  65. const media::VideoCaptureParams kArbitrarySettings;
  66. adapter_->Start(kArbitrarySettings, std::move(video_frame_handler_));
  67. run_loop.Run();
  68. }
  69. {
  70. base::RunLoop run_loop;
  71. EXPECT_CALL(*mock_device_ptr_, DoStopAndDeAllocate())
  72. .WillOnce(Invoke([&run_loop]() { run_loop.Quit(); }));
  73. mock_video_frame_handler_.reset();
  74. run_loop.Run();
  75. }
  76. }
  77. // Triggers a condition that caused a use-after-free reported in
  78. // https://crbug.com/807887. The use-after-free happened because the connection
  79. // lost event handler got invoked on a base::Unretained() pointer to |adapter_|
  80. // after |adapter_| was released.
  81. TEST_F(DeviceMediaToMojoAdapterTest,
  82. ReleaseInstanceSynchronouslyAfterReceiverClosedConnection) {
  83. {
  84. base::RunLoop run_loop;
  85. EXPECT_CALL(*mock_device_ptr_, DoAllocateAndStart(_, _))
  86. .WillOnce(Invoke(
  87. [](const media::VideoCaptureParams& params,
  88. std::unique_ptr<media::VideoCaptureDevice::Client>* client) {
  89. (*client)->OnStarted();
  90. }));
  91. EXPECT_CALL(*mock_video_frame_handler_, OnStarted())
  92. .WillOnce(Invoke([&run_loop]() { run_loop.Quit(); }));
  93. const media::VideoCaptureParams kArbitrarySettings;
  94. adapter_->Start(kArbitrarySettings, std::move(video_frame_handler_));
  95. run_loop.Run();
  96. }
  97. {
  98. base::RunLoop run_loop;
  99. // This posts invocation of the error event handler to the end of the
  100. // current sequence.
  101. mock_video_frame_handler_.reset();
  102. // This destroys the DeviceMediaToMojoAdapter, which in turn posts a
  103. // DeleteSoon in ~ReceiverOnTaskRunner() to the end of the current sequence.
  104. adapter_.reset();
  105. // Give error handle chance to get invoked
  106. run_loop.RunUntilIdle();
  107. }
  108. }
  109. } // namespace video_capture