| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/media/midi_permission_context.h" | |
| 6 | |
| 7 #include "base/bind.h" | 5 #include "base/bind.h" |
| 8 #include "base/macros.h" | 6 #include "base/macros.h" |
| 9 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 8 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 9 #include "chrome/browser/media/midi_permission_context.h" |
| 11 #include "chrome/browser/permissions/permission_request_id.h" | 10 #include "chrome/browser/permissions/permission_request_id.h" |
| 12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 11 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 13 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
| 14 #include "components/content_settings/core/browser/host_content_settings_map.h" | 13 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 15 #include "components/content_settings/core/common/content_settings.h" | 14 #include "components/content_settings/core/common/content_settings.h" |
| 16 #include "components/content_settings/core/common/content_settings_types.h" | 15 #include "components/content_settings/core/common/content_settings_types.h" |
| 17 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 18 #include "content/public/test/mock_render_process_host.h" | 17 #include "content/public/test/mock_render_process_host.h" |
| 19 #include "content/public/test/web_contents_tester.h" | 18 #include "content/public/test/web_contents_tester.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 20 |
| 22 #if defined(OS_ANDROID) | 21 #if defined(OS_ANDROID) |
| 23 #include "chrome/browser/infobars/infobar_service.h" | 22 #include "chrome/browser/infobars/infobar_service.h" |
| 24 #else | 23 #else |
| 25 #include "chrome/browser/permissions/permission_request_manager.h" | 24 #include "chrome/browser/permissions/permission_request_manager.h" |
| 26 #endif | 25 #endif |
| 27 | 26 |
| 28 namespace { | 27 namespace { |
| 29 | 28 |
| 30 class TestPermissionContext : public MidiPermissionContext { | 29 class TestPermissionContext : public MidiPermissionContext { |
| 31 public: | 30 public: |
| 32 explicit TestPermissionContext(Profile* profile) | 31 explicit TestPermissionContext(Profile* profile) |
| 33 : MidiPermissionContext(profile), | 32 : MidiPermissionContext(profile), |
| 34 permission_set_(false), | 33 permission_set_(false), |
| 35 permission_granted_(false), | 34 permission_granted_(false), |
| 36 tab_context_updated_(false) {} | 35 tab_context_updated_(false) {} |
| 37 | 36 |
| 38 ~TestPermissionContext() override {} | 37 ~TestPermissionContext() override {} |
| 39 | 38 |
| 40 bool permission_granted() { | 39 bool permission_granted() { return permission_granted_; } |
| 41 return permission_granted_; | |
| 42 } | |
| 43 | 40 |
| 44 bool permission_set() { | 41 bool permission_set() { return permission_set_; } |
| 45 return permission_set_; | |
| 46 } | |
| 47 | 42 |
| 48 bool tab_context_updated() { | 43 bool tab_context_updated() { return tab_context_updated_; } |
| 49 return tab_context_updated_; | |
| 50 } | |
| 51 | 44 |
| 52 void TrackPermissionDecision(ContentSetting content_setting) { | 45 void TrackPermissionDecision(ContentSetting content_setting) { |
| 53 permission_set_ = true; | 46 permission_set_ = true; |
| 54 permission_granted_ = content_setting == CONTENT_SETTING_ALLOW; | 47 permission_granted_ = content_setting == CONTENT_SETTING_ALLOW; |
| 55 } | 48 } |
| 56 | 49 |
| 57 protected: | 50 protected: |
| 58 void UpdateTabContext(const PermissionRequestID& id, | 51 void UpdateTabContext(const PermissionRequestID& id, |
| 59 const GURL& requesting_origin, | 52 const GURL& requesting_origin, |
| 60 bool allowed) override { | 53 bool allowed) override { |
| 61 tab_context_updated_ = true; | 54 tab_context_updated_ = true; |
| 62 } | 55 } |
| 63 | 56 |
| 64 private: | 57 private: |
| 65 bool permission_set_; | 58 bool permission_set_; |
| 66 bool permission_granted_; | 59 bool permission_granted_; |
| 67 bool tab_context_updated_; | 60 bool tab_context_updated_; |
| 68 }; | 61 }; |
| 69 | 62 |
| 70 } // anonymous namespace | 63 } // anonymous namespace |
| 71 | 64 |
| 72 class MidiPermissionContextTests : public ChromeRenderViewHostTestHarness { | 65 class MidiPermissionContextTests : public ChromeRenderViewHostTestHarness { |
| 73 protected: | 66 protected: |
| 74 MidiPermissionContextTests() = default; | 67 MidiPermissionContextTests() = default; |
| 75 | 68 |
| 76 private: | 69 private: |
| 77 // ChromeRenderViewHostTestHarness: | 70 // ChromeRenderViewHostTestHarness: |
| 78 void SetUp() override { | 71 void SetUp() override { |
| 79 ChromeRenderViewHostTestHarness::SetUp(); | 72 ChromeRenderViewHostTestHarness::SetUp(); |
| 80 #if defined(OS_ANDROID) | 73 #if defined(OS_ANDROID) |
| 81 InfoBarService::CreateForWebContents(web_contents()); | 74 InfoBarService::CreateForWebContents(web_contents()); |
| 82 #else | 75 #else |
| 83 PermissionRequestManager::CreateForWebContents(web_contents()); | 76 PermissionRequestManager::CreateForWebContents(web_contents()); |
| 84 #endif | 77 #endif |
| 85 } | 78 } |
| 86 | 79 |
| 87 DISALLOW_COPY_AND_ASSIGN(MidiPermissionContextTests); | 80 DISALLOW_COPY_AND_ASSIGN(MidiPermissionContextTests); |
| 88 }; | 81 }; |
| 89 | 82 |
| 90 // Web MIDI permission should be denied for insecure origin. | 83 // Web MIDI permission should be denied for insecure origin. |
| 91 TEST_F(MidiPermissionContextTests, TestInsecureRequestingUrl) { | 84 TEST_F(MidiPermissionContextTests, TestInsecureRequestingUrl) { |
| 92 TestPermissionContext permission_context(profile()); | 85 TestPermissionContext permission_context(profile()); |
| 93 GURL url("http://www.example.com"); | 86 GURL url("http://www.example.com"); |
| 94 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | 87 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
| 95 | 88 |
| 96 const PermissionRequestID id( | 89 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 97 web_contents()->GetRenderProcessHost()->GetID(), | 90 web_contents()->GetMainFrame()->GetRoutingID(), |
| 98 web_contents()->GetMainFrame()->GetRoutingID(), | 91 -1); |
| 99 -1); | |
| 100 permission_context.RequestPermission( | 92 permission_context.RequestPermission( |
| 101 web_contents(), | 93 web_contents(), id, url, true, |
| 102 id, url, true, | |
| 103 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 94 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 104 base::Unretained(&permission_context))); | 95 base::Unretained(&permission_context))); |
| 105 | 96 |
| 106 EXPECT_TRUE(permission_context.permission_set()); | 97 EXPECT_TRUE(permission_context.permission_set()); |
| 107 EXPECT_FALSE(permission_context.permission_granted()); | 98 EXPECT_FALSE(permission_context.permission_granted()); |
| 108 EXPECT_TRUE(permission_context.tab_context_updated()); | 99 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 109 | 100 |
| 110 ContentSetting setting = | 101 ContentSetting setting = |
| 111 HostContentSettingsMapFactory::GetForProfile(profile()) | 102 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 112 ->GetContentSetting(url.GetOrigin(), | 103 ->GetContentSetting(url.GetOrigin(), url.GetOrigin(), |
| 113 url.GetOrigin(), | 104 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, std::string()); |
| 114 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, | |
| 115 std::string()); | |
| 116 EXPECT_EQ(CONTENT_SETTING_ASK, setting); | 105 EXPECT_EQ(CONTENT_SETTING_ASK, setting); |
| 117 } | 106 } |
| 118 | 107 |
| 119 // Web MIDI permission status should be denied for insecure origin. | 108 // Web MIDI permission status should be denied for insecure origin. |
| 120 TEST_F(MidiPermissionContextTests, TestInsecureQueryingUrl) { | 109 TEST_F(MidiPermissionContextTests, TestInsecureQueryingUrl) { |
| 121 TestPermissionContext permission_context(profile()); | 110 TestPermissionContext permission_context(profile()); |
| 122 GURL insecure_url("http://www.example.com"); | 111 GURL insecure_url("http://www.example.com"); |
| 123 GURL secure_url("https://www.example.com"); | 112 GURL secure_url("https://www.example.com"); |
| 124 | 113 |
| 125 // Check that there is no saved content settings. | 114 // Check that there is no saved content settings. |
| 126 EXPECT_EQ(CONTENT_SETTING_ASK, | 115 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 116 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 117 ->GetContentSetting( |
| 118 insecure_url.GetOrigin(), insecure_url.GetOrigin(), |
| 119 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, std::string())); |
| 120 EXPECT_EQ( |
| 121 CONTENT_SETTING_ASK, |
| 127 HostContentSettingsMapFactory::GetForProfile(profile()) | 122 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 128 ->GetContentSetting(insecure_url.GetOrigin(), | 123 ->GetContentSetting(secure_url.GetOrigin(), insecure_url.GetOrigin(), |
| 129 insecure_url.GetOrigin(), | 124 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, std::string())); |
| 130 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, | 125 EXPECT_EQ( |
| 131 std::string())); | 126 CONTENT_SETTING_ASK, |
| 132 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 133 HostContentSettingsMapFactory::GetForProfile(profile()) | 127 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 134 ->GetContentSetting(secure_url.GetOrigin(), | 128 ->GetContentSetting(insecure_url.GetOrigin(), secure_url.GetOrigin(), |
| 135 insecure_url.GetOrigin(), | 129 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, std::string())); |
| 136 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, | |
| 137 std::string())); | |
| 138 EXPECT_EQ(CONTENT_SETTING_ASK, | |
| 139 HostContentSettingsMapFactory::GetForProfile(profile()) | |
| 140 ->GetContentSetting(insecure_url.GetOrigin(), | |
| 141 secure_url.GetOrigin(), | |
| 142 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, | |
| 143 std::string())); | |
| 144 | 130 |
| 145 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 131 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 146 permission_context | 132 permission_context |
| 147 .GetPermissionStatus(nullptr /* render_frame_host */, | 133 .GetPermissionStatus(nullptr /* render_frame_host */, |
| 148 insecure_url, insecure_url) | 134 insecure_url, insecure_url) |
| 149 .content_setting); | 135 .content_setting); |
| 150 | 136 |
| 151 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 137 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 152 permission_context | 138 permission_context |
| 153 .GetPermissionStatus(nullptr /* render_frame_host */, | 139 .GetPermissionStatus(nullptr /* render_frame_host */, |
| 154 insecure_url, secure_url) | 140 insecure_url, secure_url) |
| 155 .content_setting); | 141 .content_setting); |
| 156 } | 142 } |
| OLD | NEW |