| 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/notifications/notification_permission_context.h" | 5 #include "chrome/browser/notifications/notification_permission_context.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/test/test_mock_time_task_runner.h" | 9 #include "base/test/test_mock_time_task_runner.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 12 #include "chrome/browser/notifications/desktop_notification_profile_util.h" | 12 #include "chrome/browser/notifications/desktop_notification_profile_util.h" |
| 13 #include "chrome/browser/permissions/permission_manager.h" |
| 14 #include "chrome/browser/permissions/permission_manager_factory.h" |
| 13 #include "chrome/browser/permissions/permission_request_id.h" | 15 #include "chrome/browser/permissions/permission_request_id.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 17 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 16 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
| 17 #include "components/content_settings/core/browser/host_content_settings_map.h" | 19 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 20 #include "content/public/browser/permission_manager.h" |
| 18 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
| 19 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 23 #include "content/public/common/permission_status.mojom.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 23 | 27 |
| 24 namespace { | 28 namespace { |
| 25 | 29 |
| 26 void DoNothing(ContentSetting content_setting) {} | 30 void DoNothing(ContentSetting content_setting) {} |
| 31 void DoNothing2(content::PermissionStatus status) {} |
| 27 | 32 |
| 28 class TestNotificationPermissionContext : public NotificationPermissionContext { | 33 class TestNotificationPermissionContext : public NotificationPermissionContext { |
| 29 public: | 34 public: |
| 30 explicit TestNotificationPermissionContext(Profile* profile) | 35 explicit TestNotificationPermissionContext(Profile* profile) |
| 31 : NotificationPermissionContext(profile), | 36 : NotificationPermissionContext(profile), |
| 32 permission_set_count_(0), | 37 permission_set_count_(0), |
| 33 last_permission_set_persisted_(false), | 38 last_permission_set_persisted_(false), |
| 34 last_permission_set_setting_(CONTENT_SETTING_DEFAULT) {} | 39 last_permission_set_setting_(CONTENT_SETTING_DEFAULT) {} |
| 35 | 40 |
| 36 int permission_set_count() const { return permission_set_count_; } | 41 int permission_set_count() const { return permission_set_count_; } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 63 id, requesting_origin, embedder_origin, callback, persist, | 68 id, requesting_origin, embedder_origin, callback, persist, |
| 64 content_setting); | 69 content_setting); |
| 65 } | 70 } |
| 66 | 71 |
| 67 int permission_set_count_; | 72 int permission_set_count_; |
| 68 bool last_permission_set_persisted_; | 73 bool last_permission_set_persisted_; |
| 69 ContentSetting last_permission_set_setting_; | 74 ContentSetting last_permission_set_setting_; |
| 70 }; | 75 }; |
| 71 | 76 |
| 72 class NotificationPermissionContextTest | 77 class NotificationPermissionContextTest |
| 73 : public ChromeRenderViewHostTestHarness {}; | 78 : public ChromeRenderViewHostTestHarness { |
| 79 public: |
| 80 scoped_refptr<base::TestMockTimeTaskRunner> SwitchToMockTime() { |
| 81 old_task_runner_ = base::MessageLoop::current()->task_runner(); |
| 82 scoped_refptr<base::TestMockTimeTaskRunner> task_runner( |
| 83 new base::TestMockTimeTaskRunner(base::Time::Now(), |
| 84 base::TimeTicks::Now())); |
| 85 base::MessageLoop::current()->SetTaskRunner(task_runner); |
| 86 return task_runner; |
| 87 } |
| 88 |
| 89 void TearDown() override { |
| 90 if (old_task_runner_) { |
| 91 base::MessageLoop::current()->SetTaskRunner(old_task_runner_); |
| 92 old_task_runner_ = nullptr; |
| 93 } |
| 94 ChromeRenderViewHostTestHarness::TearDown(); |
| 95 } |
| 96 |
| 97 private: |
| 98 scoped_refptr<base::SingleThreadTaskRunner> old_task_runner_; |
| 99 }; |
| 74 | 100 |
| 75 } // namespace | 101 } // namespace |
| 76 | 102 |
| 77 // Web Notification permission requests will completely ignore the embedder | 103 // Web Notification permission requests will completely ignore the embedder |
| 78 // origin. See https://crbug.com/416894. | 104 // origin. See https://crbug.com/416894. |
| 79 TEST_F(NotificationPermissionContextTest, IgnoresEmbedderOrigin) { | 105 TEST_F(NotificationPermissionContextTest, IgnoresEmbedderOrigin) { |
| 80 GURL requesting_origin("https://example.com"); | 106 GURL requesting_origin("https://example.com"); |
| 81 GURL embedding_origin("https://chrome.com"); | 107 GURL embedding_origin("https://chrome.com"); |
| 82 GURL different_origin("https://foobar.com"); | 108 GURL different_origin("https://foobar.com"); |
| 83 | 109 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 TEST_F(NotificationPermissionContextTest, TestDenyInIncognitoAfterDelay) { | 146 TEST_F(NotificationPermissionContextTest, TestDenyInIncognitoAfterDelay) { |
| 121 TestNotificationPermissionContext permission_context( | 147 TestNotificationPermissionContext permission_context( |
| 122 profile()->GetOffTheRecordProfile()); | 148 profile()->GetOffTheRecordProfile()); |
| 123 GURL url("https://www.example.com"); | 149 GURL url("https://www.example.com"); |
| 124 NavigateAndCommit(url); | 150 NavigateAndCommit(url); |
| 125 | 151 |
| 126 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | 152 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 127 web_contents()->GetMainFrame()->GetRoutingID(), | 153 web_contents()->GetMainFrame()->GetRoutingID(), |
| 128 -1); | 154 -1); |
| 129 | 155 |
| 130 scoped_refptr<base::SingleThreadTaskRunner> old_task_runner( | 156 scoped_refptr<base::TestMockTimeTaskRunner> task_runner(SwitchToMockTime()); |
| 131 base::MessageLoop::current()->task_runner()); | |
| 132 scoped_refptr<base::TestMockTimeTaskRunner> task_runner( | |
| 133 new base::TestMockTimeTaskRunner(base::Time::Now(), | |
| 134 base::TimeTicks::Now())); | |
| 135 base::MessageLoop::current()->SetTaskRunner(task_runner); | |
| 136 | 157 |
| 137 ASSERT_EQ(0, permission_context.permission_set_count()); | 158 ASSERT_EQ(0, permission_context.permission_set_count()); |
| 138 ASSERT_FALSE(permission_context.last_permission_set_persisted()); | 159 ASSERT_FALSE(permission_context.last_permission_set_persisted()); |
| 139 ASSERT_EQ(CONTENT_SETTING_DEFAULT, | 160 ASSERT_EQ(CONTENT_SETTING_DEFAULT, |
| 140 permission_context.last_permission_set_setting()); | 161 permission_context.last_permission_set_setting()); |
| 141 | 162 |
| 142 permission_context.RequestPermission( | 163 permission_context.RequestPermission( |
| 143 web_contents(), id, url, true /* user_gesture */, base::Bind(&DoNothing)); | 164 web_contents(), id, url, true /* user_gesture */, base::Bind(&DoNothing)); |
| 144 | 165 |
| 145 // Should be blocked after 1-2 seconds, but the timer is reset whenever the | 166 // Should be blocked after 1-2 seconds, but the timer is reset whenever the |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 // But 5*500ms > 2 seconds, so it should now be blocked. | 199 // But 5*500ms > 2 seconds, so it should now be blocked. |
| 179 for (int n = 0; n < 4; n++) | 200 for (int n = 0; n < 4; n++) |
| 180 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); | 201 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); |
| 181 | 202 |
| 182 EXPECT_EQ(1, permission_context.permission_set_count()); | 203 EXPECT_EQ(1, permission_context.permission_set_count()); |
| 183 EXPECT_TRUE(permission_context.last_permission_set_persisted()); | 204 EXPECT_TRUE(permission_context.last_permission_set_persisted()); |
| 184 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 205 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 185 permission_context.last_permission_set_setting()); | 206 permission_context.last_permission_set_setting()); |
| 186 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 207 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 187 permission_context.GetContentSettingFromMap(url, url)); | 208 permission_context.GetContentSettingFromMap(url, url)); |
| 209 } |
| 188 | 210 |
| 189 base::MessageLoop::current()->SetTaskRunner(old_task_runner); | 211 // Tests that navigating cancels incognito permission requests without crashing. |
| 212 TEST_F(NotificationPermissionContextTest, TestCancelledIncognitoRequest) { |
| 213 TestNotificationPermissionContext permission_context( |
| 214 profile()->GetOffTheRecordProfile()); |
| 215 GURL url("https://www.example.com"); |
| 216 NavigateAndCommit(url); |
| 217 |
| 218 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 219 web_contents()->GetMainFrame()->GetRoutingID(), |
| 220 -1); |
| 221 |
| 222 scoped_refptr<base::TestMockTimeTaskRunner> task_runner(SwitchToMockTime()); |
| 223 |
| 224 content::PermissionManager* permission_manager = |
| 225 PermissionManagerFactory::GetForProfile( |
| 226 profile()->GetOffTheRecordProfile()); |
| 227 |
| 228 // Request and cancel the permission via PermissionManager. That way if |
| 229 // https://crbug.com/586944 regresses, then as well as the EXPECT_EQs below |
| 230 // failing, PermissionManager::OnPermissionsRequestResponseStatus will crash. |
| 231 int request_id = permission_manager->RequestPermission( |
| 232 content::PermissionType::NOTIFICATIONS, web_contents()->GetMainFrame(), |
| 233 url.GetOrigin(), true /* user_gesture */, base::Bind(&DoNothing2)); |
| 234 |
| 235 permission_manager->CancelPermissionRequest(request_id); |
| 236 |
| 237 task_runner->FastForwardBy(base::TimeDelta::FromDays(1)); |
| 238 |
| 239 EXPECT_EQ(0, permission_context.permission_set_count()); |
| 240 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 241 permission_context.GetContentSettingFromMap(url, url)); |
| 190 } | 242 } |
| 191 | 243 |
| 192 // Tests how multiple parallel permission requests get auto-denied in incognito. | 244 // Tests how multiple parallel permission requests get auto-denied in incognito. |
| 193 TEST_F(NotificationPermissionContextTest, TestParallelDenyInIncognito) { | 245 TEST_F(NotificationPermissionContextTest, TestParallelDenyInIncognito) { |
| 194 TestNotificationPermissionContext permission_context( | 246 TestNotificationPermissionContext permission_context( |
| 195 profile()->GetOffTheRecordProfile()); | 247 profile()->GetOffTheRecordProfile()); |
| 196 GURL url("https://www.example.com"); | 248 GURL url("https://www.example.com"); |
| 197 NavigateAndCommit(url); | 249 NavigateAndCommit(url); |
| 198 web_contents()->WasShown(); | 250 web_contents()->WasShown(); |
| 199 | 251 |
| 200 const PermissionRequestID id0(web_contents()->GetRenderProcessHost()->GetID(), | 252 const PermissionRequestID id0(web_contents()->GetRenderProcessHost()->GetID(), |
| 201 web_contents()->GetMainFrame()->GetRoutingID(), | 253 web_contents()->GetMainFrame()->GetRoutingID(), |
| 202 0); | 254 0); |
| 203 const PermissionRequestID id1(web_contents()->GetRenderProcessHost()->GetID(), | 255 const PermissionRequestID id1(web_contents()->GetRenderProcessHost()->GetID(), |
| 204 web_contents()->GetMainFrame()->GetRoutingID(), | 256 web_contents()->GetMainFrame()->GetRoutingID(), |
| 205 1); | 257 1); |
| 206 | 258 |
| 207 scoped_refptr<base::SingleThreadTaskRunner> old_task_runner( | 259 scoped_refptr<base::TestMockTimeTaskRunner> task_runner(SwitchToMockTime()); |
| 208 base::MessageLoop::current()->task_runner()); | |
| 209 scoped_refptr<base::TestMockTimeTaskRunner> task_runner( | |
| 210 new base::TestMockTimeTaskRunner(base::Time::Now(), | |
| 211 base::TimeTicks::Now())); | |
| 212 base::MessageLoop::current()->SetTaskRunner(task_runner); | |
| 213 | 260 |
| 214 ASSERT_EQ(0, permission_context.permission_set_count()); | 261 ASSERT_EQ(0, permission_context.permission_set_count()); |
| 215 ASSERT_FALSE(permission_context.last_permission_set_persisted()); | 262 ASSERT_FALSE(permission_context.last_permission_set_persisted()); |
| 216 ASSERT_EQ(CONTENT_SETTING_DEFAULT, | 263 ASSERT_EQ(CONTENT_SETTING_DEFAULT, |
| 217 permission_context.last_permission_set_setting()); | 264 permission_context.last_permission_set_setting()); |
| 218 | 265 |
| 219 permission_context.RequestPermission(web_contents(), id0, url, | 266 permission_context.RequestPermission(web_contents(), id0, url, |
| 220 true /* user_gesture */, | 267 true /* user_gesture */, |
| 221 base::Bind(&DoNothing)); | 268 base::Bind(&DoNothing)); |
| 222 permission_context.RequestPermission(web_contents(), id1, url, | 269 permission_context.RequestPermission(web_contents(), id1, url, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 245 | 292 |
| 246 // After another 2.5 seconds, the second permission request should also have | 293 // After another 2.5 seconds, the second permission request should also have |
| 247 // received a response. | 294 // received a response. |
| 248 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(2500)); | 295 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(2500)); |
| 249 EXPECT_EQ(2, permission_context.permission_set_count()); | 296 EXPECT_EQ(2, permission_context.permission_set_count()); |
| 250 EXPECT_TRUE(permission_context.last_permission_set_persisted()); | 297 EXPECT_TRUE(permission_context.last_permission_set_persisted()); |
| 251 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 298 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 252 permission_context.last_permission_set_setting()); | 299 permission_context.last_permission_set_setting()); |
| 253 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 300 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 254 permission_context.GetContentSettingFromMap(url, url)); | 301 permission_context.GetContentSettingFromMap(url, url)); |
| 255 | |
| 256 base::MessageLoop::current()->SetTaskRunner(old_task_runner); | |
| 257 } | 302 } |
| OLD | NEW |