| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
| 9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 10 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 10 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 content_settings->SetContentSettingDefaultScope( | 102 content_settings->SetContentSettingDefaultScope( |
| 103 example_url_, GURL(), CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC, | 103 example_url_, GURL(), CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC, |
| 104 std::string(), mic_setting); | 104 std::string(), mic_setting); |
| 105 content_settings->SetContentSettingDefaultScope( | 105 content_settings->SetContentSettingDefaultScope( |
| 106 example_url_, GURL(), CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA, | 106 example_url_, GURL(), CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA, |
| 107 std::string(), cam_setting); | 107 std::string(), cam_setting); |
| 108 } | 108 } |
| 109 | 109 |
| 110 // Checks whether the devices returned in OnMediaStreamResponse contains a | 110 // Checks whether the devices returned in OnMediaStreamResponse contains a |
| 111 // microphone and/or camera device. | 111 // microphone and/or camera device. |
| 112 bool DevicesContains(bool needs_mic, bool needs_cam) { | 112 bool CheckDevicesListContains(content::MediaStreamType type) { |
| 113 bool has_mic = false; | |
| 114 bool has_cam = false; | |
| 115 for (const auto& device : media_stream_devices_) { | 113 for (const auto& device : media_stream_devices_) { |
| 116 if (device.type == content::MEDIA_DEVICE_AUDIO_CAPTURE) | 114 if (device.type == type) { |
| 117 has_mic = true; | 115 return true; |
| 118 if (device.type == content::MEDIA_DEVICE_VIDEO_CAPTURE) | 116 } |
| 119 has_cam = true; | |
| 120 } | 117 } |
| 121 | 118 return false; |
| 122 return needs_mic == has_mic && needs_cam == has_cam; | |
| 123 } | 119 } |
| 124 | 120 |
| 125 content::WebContents* GetWebContents() { | 121 content::WebContents* GetWebContents() { |
| 126 return browser()->tab_strip_model()->GetActiveWebContents(); | 122 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 127 } | 123 } |
| 128 | 124 |
| 129 // Creates a MediaStreamRequest, asking for those media types, which have a | 125 // Creates a MediaStreamRequest, asking for those media types, which have a |
| 130 // non-empty id string. | 126 // non-empty id string. |
| 131 content::MediaStreamRequest CreateRequestWithType( | 127 content::MediaStreamRequest CreateRequestWithType( |
| 132 const std::string& audio_id, | 128 const std::string& audio_id, |
| (...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 if (test.ExpectMicInfobar() || test.ExpectCamInfobar()) { | 626 if (test.ExpectMicInfobar() || test.ExpectCamInfobar()) { |
| 631 if (test.accept_infobar) | 627 if (test.accept_infobar) |
| 632 controller.PermissionGranted(); | 628 controller.PermissionGranted(); |
| 633 else | 629 else |
| 634 controller.PermissionDenied(); | 630 controller.PermissionDenied(); |
| 635 } | 631 } |
| 636 | 632 |
| 637 // Check the media stream result is expected and the devices returned are | 633 // Check the media stream result is expected and the devices returned are |
| 638 // expected; | 634 // expected; |
| 639 ASSERT_EQ(test.ExpectedMediaStreamResult(), media_stream_result()); | 635 ASSERT_EQ(test.ExpectedMediaStreamResult(), media_stream_result()); |
| 640 ASSERT_TRUE( | 636 ASSERT_EQ(CheckDevicesListContains(content::MEDIA_DEVICE_AUDIO_CAPTURE), |
| 641 DevicesContains(test.ExpectMicAllowed(), test.ExpectCamAllowed())); | 637 test.ExpectMicAllowed()); |
| 638 ASSERT_EQ(CheckDevicesListContains(content::MEDIA_DEVICE_VIDEO_CAPTURE), |
| 639 test.ExpectCamAllowed()); |
| 642 } | 640 } |
| 643 } | 641 } |
| 644 | 642 |
| 645 // Request and allow camera access on WebUI pages without prompting. | 643 // Request and allow camera access on WebUI pages without prompting. |
| 646 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, | 644 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, |
| 647 WebUIRequestAndAllowCam) { | 645 WebUIRequestAndAllowCam) { |
| 648 InitWithUrl(GURL("chrome://test-page")); | 646 InitWithUrl(GURL("chrome://test-page")); |
| 649 MediaStreamDevicesController controller( | 647 MediaStreamDevicesController controller( |
| 650 GetWebContents(), CreateRequest(std::string(), example_video_id()), | 648 GetWebContents(), CreateRequest(std::string(), example_video_id()), |
| 651 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse, | 649 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse, |
| 652 base::Unretained(this))); | 650 base::Unretained(this))); |
| 653 | 651 |
| 654 ASSERT_FALSE(controller.IsAskingForAudio()); | 652 ASSERT_FALSE(controller.IsAskingForAudio()); |
| 655 ASSERT_FALSE(controller.IsAskingForVideo()); | 653 ASSERT_FALSE(controller.IsAskingForVideo()); |
| 656 | 654 |
| 657 ASSERT_EQ(content::MEDIA_DEVICE_OK, media_stream_result()); | 655 ASSERT_EQ(content::MEDIA_DEVICE_OK, media_stream_result()); |
| 658 ASSERT_TRUE(DevicesContains(false, true)); | 656 ASSERT_FALSE(CheckDevicesListContains(content::MEDIA_DEVICE_AUDIO_CAPTURE)); |
| 657 ASSERT_TRUE(CheckDevicesListContains(content::MEDIA_DEVICE_VIDEO_CAPTURE)); |
| 659 } | 658 } |
| 660 | 659 |
| 661 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, | 660 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, |
| 662 ExtensionRequestMicCam) { | 661 ExtensionRequestMicCam) { |
| 663 InitWithUrl(GURL("chrome-extension://test-page")); | 662 InitWithUrl(GURL("chrome-extension://test-page")); |
| 664 // Test that a prompt is required. | 663 // Test that a prompt is required. |
| 665 MediaStreamDevicesController controller( | 664 MediaStreamDevicesController controller( |
| 666 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()), | 665 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()), |
| 667 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse, | 666 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse, |
| 668 base::Unretained(this))); | 667 base::Unretained(this))); |
| 669 ASSERT_TRUE(controller.IsAskingForAudio()); | 668 ASSERT_TRUE(controller.IsAskingForAudio()); |
| 670 ASSERT_TRUE(controller.IsAskingForVideo()); | 669 ASSERT_TRUE(controller.IsAskingForVideo()); |
| 671 | 670 |
| 672 // Accept the prompt. | 671 // Accept the prompt. |
| 673 controller.PermissionGranted(); | 672 controller.PermissionGranted(); |
| 674 ASSERT_EQ(content::MEDIA_DEVICE_OK, media_stream_result()); | 673 ASSERT_EQ(content::MEDIA_DEVICE_OK, media_stream_result()); |
| 675 ASSERT_TRUE(DevicesContains(true, true)); | 674 ASSERT_TRUE(CheckDevicesListContains(content::MEDIA_DEVICE_AUDIO_CAPTURE)); |
| 675 ASSERT_TRUE(CheckDevicesListContains(content::MEDIA_DEVICE_VIDEO_CAPTURE)); |
| 676 | 676 |
| 677 // Check that re-requesting allows without prompting. | 677 // Check that re-requesting allows without prompting. |
| 678 MediaStreamDevicesController controller2( | 678 MediaStreamDevicesController controller2( |
| 679 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()), | 679 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()), |
| 680 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse, | 680 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse, |
| 681 base::Unretained(this))); | 681 base::Unretained(this))); |
| 682 ASSERT_FALSE(controller2.IsAskingForAudio()); | 682 ASSERT_FALSE(controller2.IsAskingForAudio()); |
| 683 ASSERT_FALSE(controller2.IsAskingForVideo()); | 683 ASSERT_FALSE(controller2.IsAskingForVideo()); |
| 684 | 684 |
| 685 ASSERT_EQ(content::MEDIA_DEVICE_OK, media_stream_result()); | 685 ASSERT_EQ(content::MEDIA_DEVICE_OK, media_stream_result()); |
| 686 ASSERT_TRUE(DevicesContains(true, true)); | 686 ASSERT_TRUE(CheckDevicesListContains(content::MEDIA_DEVICE_AUDIO_CAPTURE)); |
| 687 ASSERT_TRUE(CheckDevicesListContains(content::MEDIA_DEVICE_VIDEO_CAPTURE)); |
| 687 } | 688 } |
| 688 | 689 |
| 689 // For Pepper request from insecure origin, even if it's ALLOW, it won't be | 690 // For Pepper request from insecure origin, even if it's ALLOW, it won't be |
| 690 // changed to ASK. | 691 // changed to ASK. |
| 691 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, | 692 IN_PROC_BROWSER_TEST_F(MediaStreamDevicesControllerTest, |
| 692 PepperRequestInsecure) { | 693 PepperRequestInsecure) { |
| 693 InitWithUrl(GURL("http://www.example.com")); | 694 InitWithUrl(GURL("http://www.example.com")); |
| 694 SetContentSettings(CONTENT_SETTING_ALLOW, CONTENT_SETTING_ALLOW); | 695 SetContentSettings(CONTENT_SETTING_ALLOW, CONTENT_SETTING_ALLOW); |
| 695 | 696 |
| 696 MediaStreamDevicesController controller( | 697 MediaStreamDevicesController controller( |
| (...skipping 28 matching lines...) Expand all Loading... |
| 725 MediaStreamDevicesController controller( | 726 MediaStreamDevicesController controller( |
| 726 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()), | 727 GetWebContents(), CreateRequest(example_audio_id(), example_video_id()), |
| 727 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse, | 728 base::Bind(&MediaStreamDevicesControllerTest::OnMediaStreamResponse, |
| 728 base::Unretained(this))); | 729 base::Unretained(this))); |
| 729 | 730 |
| 730 EXPECT_FALSE(controller.IsAllowedForAudio()); | 731 EXPECT_FALSE(controller.IsAllowedForAudio()); |
| 731 EXPECT_FALSE(controller.IsAllowedForVideo()); | 732 EXPECT_FALSE(controller.IsAllowedForVideo()); |
| 732 EXPECT_FALSE(controller.IsAskingForAudio()); | 733 EXPECT_FALSE(controller.IsAskingForAudio()); |
| 733 EXPECT_FALSE(controller.IsAskingForVideo()); | 734 EXPECT_FALSE(controller.IsAskingForVideo()); |
| 734 } | 735 } |
| OLD | NEW |