1 // Copyright 2015 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
5 #include "chrome/browser/media/webrtc/media_stream_device_permission_context.h"
6
7 #include "base/bind.h"
8 #include "base/macros.h"
9 #include "build/build_config.h"
10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "components/content_settings/core/browser/host_content_settings_map.h"
15 #include "components/content_settings/core/common/content_settings.h"
16 #include "components/content_settings/core/common/content_settings_types.h"
17 #include "components/permissions/permission_request_id.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/content_features.h"
20 #include "content/public/test/mock_render_process_host.h"
21 #include "content/public/test/web_contents_tester.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 #if !defined(OS_ANDROID)
25 #include "components/permissions/permission_request_manager.h"
26 #endif
27
28 namespace {
29 class TestPermissionContext : public MediaStreamDevicePermissionContext {
30 public:
TestPermissionContext(Profile * profile,const ContentSettingsType content_settings_type)31 TestPermissionContext(Profile* profile,
32 const ContentSettingsType content_settings_type)
33 : MediaStreamDevicePermissionContext(profile, content_settings_type) {}
34
~TestPermissionContext()35 ~TestPermissionContext() override {}
36 };
37
38 } // anonymous namespace
39
40 // TODO(raymes): many tests in MediaStreamDevicesControllerTest should be
41 // converted to tests in this file.
42 class MediaStreamDevicePermissionContextTests
43 : public ChromeRenderViewHostTestHarness {
44 protected:
45 MediaStreamDevicePermissionContextTests() = default;
46
TestInsecureQueryingUrl(ContentSettingsType content_settings_type)47 void TestInsecureQueryingUrl(ContentSettingsType content_settings_type) {
48 TestPermissionContext permission_context(profile(), content_settings_type);
49 GURL insecure_url("http://www.example.com");
50 GURL secure_url("https://www.example.com");
51
52 // Check that there is no saved content settings.
53 EXPECT_EQ(CONTENT_SETTING_ASK,
54 HostContentSettingsMapFactory::GetForProfile(profile())
55 ->GetContentSetting(insecure_url.GetOrigin(),
56 insecure_url.GetOrigin(),
57 content_settings_type));
58 EXPECT_EQ(CONTENT_SETTING_ASK,
59 HostContentSettingsMapFactory::GetForProfile(profile())
60 ->GetContentSetting(secure_url.GetOrigin(),
61 insecure_url.GetOrigin(),
62 content_settings_type));
63 EXPECT_EQ(
64 CONTENT_SETTING_ASK,
65 HostContentSettingsMapFactory::GetForProfile(profile())
66 ->GetContentSetting(insecure_url.GetOrigin(),
67 secure_url.GetOrigin(), content_settings_type));
68
69 EXPECT_EQ(CONTENT_SETTING_BLOCK,
70 permission_context
71 .GetPermissionStatus(nullptr /* render_frame_host */,
72 insecure_url, insecure_url)
73 .content_setting);
74
75 EXPECT_EQ(CONTENT_SETTING_BLOCK,
76 permission_context
77 .GetPermissionStatus(nullptr /* render_frame_host */,
78 insecure_url, secure_url)
79 .content_setting);
80 }
81
TestSecureQueryingUrl(ContentSettingsType content_settings_type)82 void TestSecureQueryingUrl(ContentSettingsType content_settings_type) {
83 TestPermissionContext permission_context(profile(), content_settings_type);
84 GURL secure_url("https://www.example.com");
85
86 // Check that there is no saved content settings.
87 EXPECT_EQ(
88 CONTENT_SETTING_ASK,
89 HostContentSettingsMapFactory::GetForProfile(profile())
90 ->GetContentSetting(secure_url.GetOrigin(), secure_url.GetOrigin(),
91 content_settings_type));
92
93 EXPECT_EQ(CONTENT_SETTING_ASK,
94 permission_context
95 .GetPermissionStatus(nullptr /* render_frame_host */,
96 secure_url, secure_url)
97 .content_setting);
98 }
99
100 private:
101 // ChromeRenderViewHostTestHarness:
SetUp()102 void SetUp() override {
103 ChromeRenderViewHostTestHarness::SetUp();
104 #if defined(OS_ANDROID)
105 InfoBarService::CreateForWebContents(web_contents());
106 #else
107 permissions::PermissionRequestManager::CreateForWebContents(web_contents());
108 #endif
109 }
110
111 DISALLOW_COPY_AND_ASSIGN(MediaStreamDevicePermissionContextTests);
112 };
113
114 // MEDIASTREAM_MIC permission status should be ask for insecure origin to
115 // accommodate the usage case of Flash.
TEST_F(MediaStreamDevicePermissionContextTests,TestMicInsecureQueryingUrl)116 TEST_F(MediaStreamDevicePermissionContextTests, TestMicInsecureQueryingUrl) {
117 TestInsecureQueryingUrl(ContentSettingsType::MEDIASTREAM_MIC);
118 }
119
120 // MEDIASTREAM_CAMERA permission status should be ask for insecure origin to
121 // accommodate the usage case of Flash.
TEST_F(MediaStreamDevicePermissionContextTests,TestCameraInsecureQueryingUrl)122 TEST_F(MediaStreamDevicePermissionContextTests, TestCameraInsecureQueryingUrl) {
123 TestInsecureQueryingUrl(ContentSettingsType::MEDIASTREAM_CAMERA);
124 }
125
126 // MEDIASTREAM_MIC permission status should be ask for Secure origin.
TEST_F(MediaStreamDevicePermissionContextTests,TestMicSecureQueryingUrl)127 TEST_F(MediaStreamDevicePermissionContextTests, TestMicSecureQueryingUrl) {
128 TestSecureQueryingUrl(ContentSettingsType::MEDIASTREAM_MIC);
129 }
130
131 // MEDIASTREAM_CAMERA permission status should be ask for Secure origin.
TEST_F(MediaStreamDevicePermissionContextTests,TestCameraSecureQueryingUrl)132 TEST_F(MediaStreamDevicePermissionContextTests, TestCameraSecureQueryingUrl) {
133 TestSecureQueryingUrl(ContentSettingsType::MEDIASTREAM_CAMERA);
134 }
135