| OLD | NEW | 
|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "content/browser/renderer_host/media/media_devices_dispatcher_host.h" | 5 #include "content/browser/renderer_host/media/media_devices_dispatcher_host.h" | 
| 6 | 6 | 
| 7 #include <stddef.h> | 7 #include <stddef.h> | 
| 8 | 8 | 
| 9 #include <memory> | 9 #include <memory> | 
| 10 #include <queue> | 10 #include <queue> | 
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 64   ::mojom::MediaDevicesListenerPtr CreateInterfacePtrAndBind() { | 64   ::mojom::MediaDevicesListenerPtr CreateInterfacePtrAndBind() { | 
| 65     return binding_.CreateInterfacePtrAndBind(); | 65     return binding_.CreateInterfacePtrAndBind(); | 
| 66   } | 66   } | 
| 67 | 67 | 
| 68  private: | 68  private: | 
| 69   mojo::Binding<::mojom::MediaDevicesListener> binding_; | 69   mojo::Binding<::mojom::MediaDevicesListener> binding_; | 
| 70 }; | 70 }; | 
| 71 | 71 | 
| 72 }  // namespace | 72 }  // namespace | 
| 73 | 73 | 
| 74 class MediaDevicesDispatcherHostTest : public testing::Test { | 74 class MediaDevicesDispatcherHostTest : public testing::TestWithParam<GURL> { | 
| 75  public: | 75  public: | 
| 76   MediaDevicesDispatcherHostTest() | 76   MediaDevicesDispatcherHostTest() | 
| 77       : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 77       : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 
| 78         origin_(GURL("https://test.com")) { | 78         origin_(GetParam()) { | 
| 79     // Make sure we use fake devices to avoid long delays. | 79     // Make sure we use fake devices to avoid long delays. | 
| 80     base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 80     base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 
| 81         switches::kUseFakeDeviceForMediaStream, | 81         switches::kUseFakeDeviceForMediaStream, | 
| 82         base::StringPrintf("device-count=%zu, video-input-default-id=%s", | 82         base::StringPrintf("device-count=%zu, video-input-default-id=%s", | 
| 83                            kNumFakeVideoDevices, kDefaultVideoDeviceID)); | 83                            kNumFakeVideoDevices, kDefaultVideoDeviceID)); | 
| 84     audio_manager_.reset( | 84     audio_manager_.reset( | 
| 85         new media::MockAudioManager(base::ThreadTaskRunnerHandle::Get())); | 85         new media::MockAudioManager(base::ThreadTaskRunnerHandle::Get())); | 
| 86     audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); | 86     audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); | 
| 87     media_stream_manager_ = | 87     media_stream_manager_ = | 
| 88         base::MakeUnique<MediaStreamManager>(audio_system_.get()); | 88         base::MakeUnique<MediaStreamManager>(audio_system_.get()); | 
| 89 | 89 | 
| 90     host_ = base::MakeUnique<MediaDevicesDispatcherHost>( | 90     host_ = base::MakeUnique<MediaDevicesDispatcherHost>( | 
| 91         kProcessId, kRenderId, browser_context_.GetMediaDeviceIDSalt(), | 91         kProcessId, kRenderId, browser_context_.GetMediaDeviceIDSalt(), | 
| 92         media_stream_manager_.get()); | 92         media_stream_manager_.get()); | 
|  | 93     host_->SetSecurityOriginForTesting(origin_); | 
| 93   } | 94   } | 
| 94 | 95 | 
| 95   void SetUp() override { | 96   void SetUp() override { | 
| 96     base::RunLoop run_loop; | 97     base::RunLoop run_loop; | 
| 97     MediaDevicesManager::BoolDeviceTypes devices_to_enumerate; | 98     MediaDevicesManager::BoolDeviceTypes devices_to_enumerate; | 
| 98     devices_to_enumerate[MEDIA_DEVICE_TYPE_AUDIO_INPUT] = true; | 99     devices_to_enumerate[MEDIA_DEVICE_TYPE_AUDIO_INPUT] = true; | 
| 99     devices_to_enumerate[MEDIA_DEVICE_TYPE_VIDEO_INPUT] = true; | 100     devices_to_enumerate[MEDIA_DEVICE_TYPE_VIDEO_INPUT] = true; | 
| 100     devices_to_enumerate[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT] = true; | 101     devices_to_enumerate[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT] = true; | 
| 101     media_stream_manager_->media_devices_manager()->EnumerateDevices( | 102     media_stream_manager_->media_devices_manager()->EnumerateDevices( | 
| 102         devices_to_enumerate, | 103         devices_to_enumerate, | 
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 150 | 151 | 
| 151   void EnumerateDevicesAndWaitForResult(bool enumerate_audio_input, | 152   void EnumerateDevicesAndWaitForResult(bool enumerate_audio_input, | 
| 152                                         bool enumerate_video_input, | 153                                         bool enumerate_video_input, | 
| 153                                         bool enumerate_audio_output, | 154                                         bool enumerate_audio_output, | 
| 154                                         bool permission_override_value = true) { | 155                                         bool permission_override_value = true) { | 
| 155     host_->SetPermissionChecker(base::MakeUnique<MediaDevicesPermissionChecker>( | 156     host_->SetPermissionChecker(base::MakeUnique<MediaDevicesPermissionChecker>( | 
| 156         permission_override_value)); | 157         permission_override_value)); | 
| 157     base::RunLoop run_loop; | 158     base::RunLoop run_loop; | 
| 158     host_->EnumerateDevices( | 159     host_->EnumerateDevices( | 
| 159         enumerate_audio_input, enumerate_video_input, enumerate_audio_output, | 160         enumerate_audio_input, enumerate_video_input, enumerate_audio_output, | 
| 160         origin_, base::Bind(&MediaDevicesDispatcherHostTest::DevicesEnumerated, | 161         base::Bind(&MediaDevicesDispatcherHostTest::DevicesEnumerated, | 
| 161                             base::Unretained(this), run_loop.QuitClosure())); | 162                    base::Unretained(this), run_loop.QuitClosure())); | 
| 162     run_loop.Run(); | 163     run_loop.Run(); | 
| 163 | 164 | 
| 164     ASSERT_FALSE(enumerated_devices_.empty()); | 165     ASSERT_FALSE(enumerated_devices_.empty()); | 
| 165     if (enumerate_audio_input) | 166     if (enumerate_audio_input) | 
| 166       EXPECT_FALSE(enumerated_devices_[MEDIA_DEVICE_TYPE_AUDIO_INPUT].empty()); | 167       EXPECT_FALSE(enumerated_devices_[MEDIA_DEVICE_TYPE_AUDIO_INPUT].empty()); | 
| 167     if (enumerate_video_input) | 168     if (enumerate_video_input) | 
| 168       EXPECT_FALSE(enumerated_devices_[MEDIA_DEVICE_TYPE_VIDEO_INPUT].empty()); | 169       EXPECT_FALSE(enumerated_devices_[MEDIA_DEVICE_TYPE_VIDEO_INPUT].empty()); | 
| 169     if (enumerate_audio_output) | 170     if (enumerate_audio_output) | 
| 170       EXPECT_FALSE(enumerated_devices_[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT].empty()); | 171       EXPECT_FALSE(enumerated_devices_[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT].empty()); | 
| 171 | 172 | 
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 249       if (!DoesNotContainLabels(device_infos)) | 250       if (!DoesNotContainLabels(device_infos)) | 
| 250         return false; | 251         return false; | 
| 251     } | 252     } | 
| 252     return true; | 253     return true; | 
| 253   } | 254   } | 
| 254 | 255 | 
| 255   void SubscribeAndWaitForResult(bool has_permission) { | 256   void SubscribeAndWaitForResult(bool has_permission) { | 
| 256     host_->SetPermissionChecker( | 257     host_->SetPermissionChecker( | 
| 257         base::MakeUnique<MediaDevicesPermissionChecker>(has_permission)); | 258         base::MakeUnique<MediaDevicesPermissionChecker>(has_permission)); | 
| 258     uint32_t subscription_id = 0u; | 259     uint32_t subscription_id = 0u; | 
| 259     uint32_t unique_origin_subscription_id = 1u; |  | 
| 260     url::Origin origin(GURL("http://localhost")); |  | 
| 261     url::Origin unique_origin; |  | 
| 262     for (size_t i = 0; i < NUM_MEDIA_DEVICE_TYPES; ++i) { | 260     for (size_t i = 0; i < NUM_MEDIA_DEVICE_TYPES; ++i) { | 
| 263       MediaDeviceType type = static_cast<MediaDeviceType>(i); | 261       MediaDeviceType type = static_cast<MediaDeviceType>(i); | 
| 264       host_->SubscribeDeviceChangeNotifications(type, subscription_id, origin); | 262       host_->SubscribeDeviceChangeNotifications(type, subscription_id); | 
| 265       MockMediaDevicesListener device_change_listener; | 263       MockMediaDevicesListener device_change_listener; | 
| 266       host_->SetDeviceChangeListenerForTesting( | 264       host_->SetDeviceChangeListenerForTesting( | 
| 267           device_change_listener.CreateInterfacePtrAndBind()); | 265           device_change_listener.CreateInterfacePtrAndBind()); | 
| 268       MediaDeviceInfoArray changed_devices; | 266       MediaDeviceInfoArray changed_devices; | 
| 269       EXPECT_CALL(device_change_listener, | 267       EXPECT_CALL(device_change_listener, | 
| 270                   OnDevicesChanged(type, subscription_id, testing::_)) | 268                   OnDevicesChanged(type, subscription_id, testing::_)) | 
| 271           .WillRepeatedly(SaveArg<2>(&changed_devices)); | 269           .WillRepeatedly(SaveArg<2>(&changed_devices)); | 
| 272       // The subscription with unique origin is ignored, so it should not get |  | 
| 273       // notifications. |  | 
| 274       EXPECT_CALL( |  | 
| 275           device_change_listener, |  | 
| 276           OnDevicesChanged(type, unique_origin_subscription_id, testing::_)) |  | 
| 277           .Times(0); |  | 
| 278 | 270 | 
| 279       // Simulate device-change notification | 271       // Simulate device-change notification | 
| 280       MediaDeviceInfoArray updated_devices = { | 272       MediaDeviceInfoArray updated_devices = { | 
| 281           {"fake_device_id", "fake_label", "fake_group"}}; | 273           {"fake_device_id", "fake_label", "fake_group"}}; | 
| 282       host_->OnDevicesChanged(type, updated_devices); | 274       host_->OnDevicesChanged(type, updated_devices); | 
| 283       base::RunLoop().RunUntilIdle(); | 275       base::RunLoop().RunUntilIdle(); | 
| 284       host_->UnsubscribeDeviceChangeNotifications(type, subscription_id); | 276       host_->UnsubscribeDeviceChangeNotifications(type, subscription_id); | 
| 285 | 277 | 
| 286       if (has_permission) | 278       if (has_permission) | 
| 287         EXPECT_TRUE(DoesContainLabels(changed_devices)); | 279         EXPECT_TRUE(DoesContainLabels(changed_devices)); | 
| (...skipping 13 matching lines...) Expand all  Loading... | 
| 301   std::unique_ptr<media::AudioManager, media::AudioManagerDeleter> | 293   std::unique_ptr<media::AudioManager, media::AudioManagerDeleter> | 
| 302       audio_manager_; | 294       audio_manager_; | 
| 303   std::unique_ptr<media::AudioSystem> audio_system_; | 295   std::unique_ptr<media::AudioSystem> audio_system_; | 
| 304   content::TestBrowserContext browser_context_; | 296   content::TestBrowserContext browser_context_; | 
| 305   MediaDeviceEnumeration physical_devices_; | 297   MediaDeviceEnumeration physical_devices_; | 
| 306   url::Origin origin_; | 298   url::Origin origin_; | 
| 307 | 299 | 
| 308   std::vector<MediaDeviceInfoArray> enumerated_devices_; | 300   std::vector<MediaDeviceInfoArray> enumerated_devices_; | 
| 309 }; | 301 }; | 
| 310 | 302 | 
| 311 TEST_F(MediaDevicesDispatcherHostTest, EnumerateAudioInputDevices) { | 303 TEST_P(MediaDevicesDispatcherHostTest, EnumerateAudioInputDevices) { | 
| 312   EnumerateDevicesAndWaitForResult(true, false, false); | 304   EnumerateDevicesAndWaitForResult(true, false, false); | 
| 313   EXPECT_TRUE(DoesContainLabels(enumerated_devices_)); | 305   EXPECT_TRUE(DoesContainLabels(enumerated_devices_)); | 
| 314 } | 306 } | 
| 315 | 307 | 
| 316 TEST_F(MediaDevicesDispatcherHostTest, EnumerateVideoInputDevices) { | 308 TEST_P(MediaDevicesDispatcherHostTest, EnumerateVideoInputDevices) { | 
| 317   EnumerateDevicesAndWaitForResult(false, true, false); | 309   EnumerateDevicesAndWaitForResult(false, true, false); | 
| 318   EXPECT_TRUE(DoesContainLabels(enumerated_devices_)); | 310   EXPECT_TRUE(DoesContainLabels(enumerated_devices_)); | 
| 319 } | 311 } | 
| 320 | 312 | 
| 321 TEST_F(MediaDevicesDispatcherHostTest, EnumerateAudioOutputDevices) { | 313 TEST_P(MediaDevicesDispatcherHostTest, EnumerateAudioOutputDevices) { | 
| 322   EnumerateDevicesAndWaitForResult(false, false, true); | 314   EnumerateDevicesAndWaitForResult(false, false, true); | 
| 323   EXPECT_TRUE(DoesContainLabels(enumerated_devices_)); | 315   EXPECT_TRUE(DoesContainLabels(enumerated_devices_)); | 
| 324 } | 316 } | 
| 325 | 317 | 
| 326 TEST_F(MediaDevicesDispatcherHostTest, EnumerateAllDevices) { | 318 TEST_P(MediaDevicesDispatcherHostTest, EnumerateAllDevices) { | 
| 327   EnumerateDevicesAndWaitForResult(true, true, true); | 319   EnumerateDevicesAndWaitForResult(true, true, true); | 
| 328   EXPECT_TRUE(DoesContainLabels(enumerated_devices_)); | 320   EXPECT_TRUE(DoesContainLabels(enumerated_devices_)); | 
| 329 } | 321 } | 
| 330 | 322 | 
| 331 TEST_F(MediaDevicesDispatcherHostTest, EnumerateAudioInputDevicesNoAccess) { | 323 TEST_P(MediaDevicesDispatcherHostTest, EnumerateAudioInputDevicesNoAccess) { | 
| 332   EnumerateDevicesAndWaitForResult(true, false, false, false); | 324   EnumerateDevicesAndWaitForResult(true, false, false, false); | 
| 333   EXPECT_TRUE(DoesNotContainLabels(enumerated_devices_)); | 325   EXPECT_TRUE(DoesNotContainLabels(enumerated_devices_)); | 
| 334 } | 326 } | 
| 335 | 327 | 
| 336 TEST_F(MediaDevicesDispatcherHostTest, EnumerateVideoInputDevicesNoAccess) { | 328 TEST_P(MediaDevicesDispatcherHostTest, EnumerateVideoInputDevicesNoAccess) { | 
| 337   EnumerateDevicesAndWaitForResult(false, true, false, false); | 329   EnumerateDevicesAndWaitForResult(false, true, false, false); | 
| 338   EXPECT_TRUE(DoesNotContainLabels(enumerated_devices_)); | 330   EXPECT_TRUE(DoesNotContainLabels(enumerated_devices_)); | 
| 339 } | 331 } | 
| 340 | 332 | 
| 341 TEST_F(MediaDevicesDispatcherHostTest, EnumerateAudioOutputDevicesNoAccess) { | 333 TEST_P(MediaDevicesDispatcherHostTest, EnumerateAudioOutputDevicesNoAccess) { | 
| 342   EnumerateDevicesAndWaitForResult(false, false, true, false); | 334   EnumerateDevicesAndWaitForResult(false, false, true, false); | 
| 343   EXPECT_TRUE(DoesNotContainLabels(enumerated_devices_)); | 335   EXPECT_TRUE(DoesNotContainLabels(enumerated_devices_)); | 
| 344 } | 336 } | 
| 345 | 337 | 
| 346 TEST_F(MediaDevicesDispatcherHostTest, EnumerateAllDevicesNoAccess) { | 338 TEST_P(MediaDevicesDispatcherHostTest, EnumerateAllDevicesNoAccess) { | 
| 347   EnumerateDevicesAndWaitForResult(true, true, true, false); | 339   EnumerateDevicesAndWaitForResult(true, true, true, false); | 
| 348   EXPECT_TRUE(DoesNotContainLabels(enumerated_devices_)); | 340   EXPECT_TRUE(DoesNotContainLabels(enumerated_devices_)); | 
| 349 } | 341 } | 
| 350 | 342 | 
| 351 TEST_F(MediaDevicesDispatcherHostTest, SubscribeDeviceChange) { | 343 TEST_P(MediaDevicesDispatcherHostTest, SubscribeDeviceChange) { | 
| 352   SubscribeAndWaitForResult(true); | 344   SubscribeAndWaitForResult(true); | 
| 353 } | 345 } | 
| 354 | 346 | 
| 355 TEST_F(MediaDevicesDispatcherHostTest, SubscribeDeviceChangeNoAccess) { | 347 TEST_P(MediaDevicesDispatcherHostTest, SubscribeDeviceChangeNoAccess) { | 
| 356   SubscribeAndWaitForResult(false); | 348   SubscribeAndWaitForResult(false); | 
| 357 } | 349 } | 
| 358 | 350 | 
| 359 TEST_F(MediaDevicesDispatcherHostTest, EnumerateAllDevicesUniqueOrigin) { | 351 TEST_P(MediaDevicesDispatcherHostTest, GetVideoInputCapabilities) { | 
| 360   EXPECT_CALL(*this, UniqueOriginCallback(testing::_)).Times(0); |  | 
| 361   host_->EnumerateDevices( |  | 
| 362       true, true, true, url::Origin(), |  | 
| 363       base::Bind(&MediaDevicesDispatcherHostTest::UniqueOriginCallback, |  | 
| 364                  base::Unretained(this))); |  | 
| 365   base::RunLoop().RunUntilIdle(); |  | 
| 366 |  | 
| 367   // Verify that the callback for a valid origin does get called. |  | 
| 368   base::RunLoop run_loop; |  | 
| 369   EXPECT_CALL(*this, ValidOriginCallback(testing::_)) |  | 
| 370       .WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); })); |  | 
| 371   host_->EnumerateDevices( |  | 
| 372       true, true, true, url::Origin(GURL("http://localhost")), |  | 
| 373       base::Bind(&MediaDevicesDispatcherHostTest::ValidOriginCallback, |  | 
| 374                  base::Unretained(this))); |  | 
| 375   run_loop.Run(); |  | 
| 376 } |  | 
| 377 |  | 
| 378 TEST_F(MediaDevicesDispatcherHostTest, GetVideoInputCapabilities) { |  | 
| 379   base::RunLoop run_loop; | 352   base::RunLoop run_loop; | 
| 380   EXPECT_CALL(*this, MockVideoInputCapabilitiesCallback()) | 353   EXPECT_CALL(*this, MockVideoInputCapabilitiesCallback()) | 
| 381       .WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); })); | 354       .WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); })); | 
| 382   host_->GetVideoInputCapabilities( | 355   host_->GetVideoInputCapabilities( | 
| 383       origin_, |  | 
| 384       base::Bind( | 356       base::Bind( | 
| 385           &MediaDevicesDispatcherHostTest::VideoInputCapabilitiesCallback, | 357           &MediaDevicesDispatcherHostTest::VideoInputCapabilitiesCallback, | 
| 386           base::Unretained(this))); | 358           base::Unretained(this))); | 
| 387   run_loop.Run(); | 359   run_loop.Run(); | 
| 388 } | 360 } | 
| 389 | 361 | 
| 390 TEST_F(MediaDevicesDispatcherHostTest, GetVideoInputCapabilitiesUniqueOrigin) { | 362 INSTANTIATE_TEST_CASE_P(, | 
| 391   base::RunLoop run_loop; | 363                         MediaDevicesDispatcherHostTest, | 
| 392   EXPECT_CALL(*this, MockVideoInputCapabilitiesCallback()) | 364                         testing::Values(GURL(), GURL("https://test.com"))); | 
| 393       .WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); })); |  | 
| 394   host_->GetVideoInputCapabilities( |  | 
| 395       url::Origin(), base::Bind(&MediaDevicesDispatcherHostTest:: |  | 
| 396                                     VideoInputCapabilitiesUniqueOriginCallback, |  | 
| 397                                 base::Unretained(this))); |  | 
| 398   run_loop.Run(); |  | 
| 399 } |  | 
| 400 |  | 
| 401 };  // namespace content | 365 };  // namespace content | 
| OLD | NEW | 
|---|