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