Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "content/browser/media/media_devices_permission_checker.h" | |
| 6 | |
| 7 #include "base/run_loop.h" | |
| 8 #include "base/test/scoped_feature_list.h" | |
| 9 #include "content/public/browser/render_frame_host.h" | |
| 10 #include "content/public/browser/render_process_host.h" | |
| 11 #include "content/public/browser/web_contents.h" | |
| 12 #include "content/public/browser/web_contents_delegate.h" | |
| 13 #include "content/public/common/content_features.h" | |
| 14 #include "content/public/common/media_stream_request.h" | |
| 15 #include "content/public/test/test_renderer_host.h" | |
| 16 #include "content/test/test_render_view_host.h" | |
| 17 #include "content/test/test_web_contents.h" | |
| 18 #include "third_party/WebKit/public/platform/WebFeaturePolicy.h" | |
| 19 #include "url/origin.h" | |
| 20 | |
| 21 namespace content { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 class TestWebContentsDelegate : public content::WebContentsDelegate { | |
| 26 public: | |
| 27 ~TestWebContentsDelegate() override {} | |
| 28 | |
| 29 bool CheckMediaAccessPermission(WebContents* web_contents, | |
| 30 const GURL& security_origin, | |
| 31 MediaStreamType type) override { | |
| 32 return true; | |
| 33 } | |
| 34 }; | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 class MediaDevicesPermissionCheckerTest : public RenderViewHostImplTestHarness { | |
| 39 public: | |
| 40 MediaDevicesPermissionCheckerTest() | |
| 41 : origin_(GURL("https://www.google.com")), | |
| 42 callback_run_(false), | |
| 43 callback_result_(false) {} | |
| 44 | |
| 45 void SetUp() override { | |
| 46 RenderViewHostImplTestHarness::SetUp(); | |
| 47 NavigateAndCommit(origin_.GetURL()); | |
| 48 contents()->SetDelegate(&delegate_); | |
| 49 } | |
| 50 | |
| 51 protected: | |
| 52 // The header policy should only be set once on page load, so we refresh the | |
| 53 // page to simulate that. | |
| 54 void RefreshPageAndSetHeaderPolicy(blink::WebFeaturePolicyFeature feature, | |
| 55 bool enabled) { | |
| 56 NavigateAndCommit(origin_.GetURL()); | |
| 57 std::vector<url::Origin> whitelist; | |
| 58 if (enabled) | |
| 59 whitelist.push_back(origin_); | |
| 60 RenderFrameHostTester::For(main_rfh()) | |
| 61 ->SimulateFeaturePolicyHeader(feature, whitelist); | |
| 62 } | |
| 63 | |
| 64 bool CheckPermission(MediaDeviceType device_type) { | |
| 65 base::RunLoop run_loop; | |
| 66 quit_closure_ = run_loop.QuitClosure(); | |
| 67 checker_.CheckPermission( | |
| 68 device_type, main_rfh()->GetProcess()->GetID(), | |
| 69 main_rfh()->GetRoutingID(), | |
| 70 base::Bind(&MediaDevicesPermissionCheckerTest::CheckPermissionCallback, | |
| 71 base::Unretained(this))); | |
| 72 run_loop.Run(); | |
| 73 | |
| 74 EXPECT_TRUE(callback_run_); | |
| 75 callback_run_ = false; | |
| 76 return callback_result_; | |
| 77 } | |
| 78 | |
| 79 private: | |
| 80 void CheckPermissionCallback(bool result) { | |
| 81 callback_run_ = true; | |
| 82 callback_result_ = result; | |
| 83 quit_closure_.Run(); | |
| 84 } | |
| 85 | |
| 86 url::Origin origin_; | |
| 87 | |
| 88 base::Closure quit_closure_; | |
| 89 | |
| 90 bool callback_run_; | |
| 91 bool callback_result_; | |
| 92 | |
| 93 MediaDevicesPermissionChecker checker_; | |
| 94 TestWebContentsDelegate delegate_; | |
| 95 }; | |
| 96 | |
| 97 // Basic tests for feature policy checks through the | |
| 98 // MediaDevicesPermissionChecker. These tests are not meant to cover every edge | |
| 99 // case as the FeaturePolicy class itself is tested thoroughly in | |
| 100 // feature_policy_unittest.cc and in | |
| 101 // render_frame_host_feature_policy_unittest.cc. | |
| 102 TEST_F(MediaDevicesPermissionCheckerTest, CheckPermissionWithFeaturePolicy) { | |
| 103 base::test::ScopedFeatureList feature_list; | |
|
Guido Urdaneta
2017/06/08 12:08:22
Can you test this without having ScopedFeatureList
raymes
2017/06/13 04:13:21
Hmm, is that really true? What would be the differ
| |
| 104 feature_list.InitAndEnableFeature(features::kUseFeaturePolicyForPermissions); | |
| 105 // Mic and Camera should be enabled by default for a frame (if permission is | |
| 106 // granted). | |
| 107 EXPECT_TRUE(CheckPermission(MEDIA_DEVICE_TYPE_AUDIO_INPUT)); | |
| 108 EXPECT_TRUE(CheckPermission(MEDIA_DEVICE_TYPE_VIDEO_INPUT)); | |
| 109 | |
| 110 RefreshPageAndSetHeaderPolicy(blink::WebFeaturePolicyFeature::kMicrophone, | |
| 111 /*enabled=*/false); | |
| 112 EXPECT_FALSE(CheckPermission(MEDIA_DEVICE_TYPE_AUDIO_INPUT)); | |
| 113 EXPECT_TRUE(CheckPermission(MEDIA_DEVICE_TYPE_VIDEO_INPUT)); | |
| 114 | |
| 115 RefreshPageAndSetHeaderPolicy(blink::WebFeaturePolicyFeature::kCamera, | |
| 116 /*enabled=*/false); | |
| 117 EXPECT_TRUE(CheckPermission(MEDIA_DEVICE_TYPE_AUDIO_INPUT)); | |
| 118 EXPECT_FALSE(CheckPermission(MEDIA_DEVICE_TYPE_VIDEO_INPUT)); | |
| 119 | |
| 120 // Ensure that the policy is ignored if kUseFeaturePolicyForPermissions is | |
| 121 // disabled. | |
| 122 base::test::ScopedFeatureList empty_feature_list; | |
| 123 empty_feature_list.Init(); | |
| 124 EXPECT_TRUE(CheckPermission(MEDIA_DEVICE_TYPE_AUDIO_INPUT)); | |
| 125 EXPECT_TRUE(CheckPermission(MEDIA_DEVICE_TYPE_VIDEO_INPUT)); | |
| 126 } | |
| 127 | |
| 128 } // namespace | |
| OLD | NEW |