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" | |
9 #include "base/test/test_mock_time_task_runner.h" | |
10 #include "base/time/time.h" | |
11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | |
8 #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_request_id.h" | |
9 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
10 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
11 #include "components/content_settings/core/browser/host_content_settings_map.h" | 17 #include "components/content_settings/core/browser/host_content_settings_map.h" |
18 #include "content/public/browser/render_process_host.h" | |
19 #include "content/public/browser/web_contents.h" | |
12 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "url/gurl.h" | 22 #include "url/gurl.h" |
15 | 23 |
24 namespace { | |
25 | |
26 void DoNothing(ContentSetting content_setting) {} | |
27 | |
28 class TestNotificationPermissionContext : public NotificationPermissionContext { | |
29 public: | |
30 explicit TestNotificationPermissionContext(Profile* profile) | |
31 : NotificationPermissionContext(profile), | |
32 permission_set_count_(0), | |
33 last_permission_set_persisted_(false), | |
34 last_permission_set_setting_(CONTENT_SETTING_DEFAULT) {} | |
35 | |
36 int permission_set_count() const { return permission_set_count_; } | |
37 bool last_permission_set_persisted() const { | |
38 return last_permission_set_persisted_; | |
39 } | |
40 ContentSetting last_permission_set_setting() const { | |
41 return last_permission_set_setting_; | |
42 } | |
43 | |
44 ContentSetting GetContentSettingFromMap(GURL url_a, GURL url_b) { | |
Peter Beverloo
2016/01/13 01:42:10
const GURL&
johnme
2016/01/13 20:49:26
Done.
| |
45 return HostContentSettingsMapFactory::GetForProfile(profile()) | |
46 ->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), | |
47 content_settings_type(), std::string()); | |
48 } | |
49 | |
50 private: | |
51 // NotificationPermissionContext: | |
52 void NotifyPermissionSet(const PermissionRequestID& id, | |
53 const GURL& requesting_origin, | |
54 const GURL& embedder_origin, | |
55 const BrowserPermissionCallback& callback, | |
56 bool persist, | |
57 ContentSetting content_setting) override { | |
58 permission_set_count_++; | |
59 last_permission_set_persisted_ = persist; | |
60 last_permission_set_setting_ = content_setting; | |
61 NotificationPermissionContext::NotifyPermissionSet( | |
62 id, requesting_origin, embedder_origin, callback, persist, | |
63 content_setting); | |
64 } | |
65 | |
66 int permission_set_count_; | |
67 bool last_permission_set_persisted_; | |
68 ContentSetting last_permission_set_setting_; | |
69 }; | |
70 | |
71 class NotificationPermissionContextTest | |
72 : public ChromeRenderViewHostTestHarness {}; | |
73 | |
74 } // namespace | |
75 | |
16 // Web Notification permission requests will completely ignore the embedder | 76 // Web Notification permission requests will completely ignore the embedder |
17 // origin. See https://crbug.com/416894. | 77 // origin. See https://crbug.com/416894. |
18 TEST(NotificationPermissionContextTest, IgnoresEmbedderOrigin) { | 78 TEST_F(NotificationPermissionContextTest, IgnoresEmbedderOrigin) { |
19 content::TestBrowserThreadBundle thread_bundle; | |
20 TestingProfile profile; | |
21 | |
22 GURL requesting_origin("https://example.com"); | 79 GURL requesting_origin("https://example.com"); |
23 GURL embedding_origin("https://chrome.com"); | 80 GURL embedding_origin("https://chrome.com"); |
24 GURL different_origin("https://foobar.com"); | 81 GURL different_origin("https://foobar.com"); |
25 | 82 |
26 NotificationPermissionContext context(&profile); | 83 NotificationPermissionContext context(profile()); |
27 context.UpdateContentSetting(requesting_origin, | 84 context.UpdateContentSetting(requesting_origin, |
28 embedding_origin, | 85 embedding_origin, |
29 CONTENT_SETTING_ALLOW); | 86 CONTENT_SETTING_ALLOW); |
30 | 87 |
31 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 88 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
32 context.GetPermissionStatus(requesting_origin, embedding_origin)); | 89 context.GetPermissionStatus(requesting_origin, embedding_origin)); |
33 | 90 |
34 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 91 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
35 context.GetPermissionStatus(requesting_origin, different_origin)); | 92 context.GetPermissionStatus(requesting_origin, different_origin)); |
36 | 93 |
37 context.ResetPermission(requesting_origin, embedding_origin); | 94 context.ResetPermission(requesting_origin, embedding_origin); |
38 | 95 |
39 EXPECT_EQ(CONTENT_SETTING_ASK, | 96 EXPECT_EQ(CONTENT_SETTING_ASK, |
40 context.GetPermissionStatus(requesting_origin, embedding_origin)); | 97 context.GetPermissionStatus(requesting_origin, embedding_origin)); |
41 | 98 |
42 EXPECT_EQ(CONTENT_SETTING_ASK, | 99 EXPECT_EQ(CONTENT_SETTING_ASK, |
43 context.GetPermissionStatus(requesting_origin, different_origin)); | 100 context.GetPermissionStatus(requesting_origin, different_origin)); |
44 } | 101 } |
45 | 102 |
46 // Web Notifications do not require a secure origin when requesting permission. | 103 // Web Notifications do not require a secure origin when requesting permission. |
47 // See https://crbug.com/404095. | 104 // See https://crbug.com/404095. |
48 TEST(NotificationPermissionContextTest, NoSecureOriginRequirement) { | 105 TEST_F(NotificationPermissionContextTest, NoSecureOriginRequirement) { |
49 content::TestBrowserThreadBundle thread_bundle; | |
50 TestingProfile profile; | |
51 | |
52 GURL origin("http://example.com"); | 106 GURL origin("http://example.com"); |
53 | 107 |
54 NotificationPermissionContext context(&profile); | 108 NotificationPermissionContext context(profile()); |
55 EXPECT_EQ(CONTENT_SETTING_ASK, | 109 EXPECT_EQ(CONTENT_SETTING_ASK, |
56 context.GetPermissionStatus(origin, origin)); | 110 context.GetPermissionStatus(origin, origin)); |
57 | 111 |
58 context.UpdateContentSetting(origin, origin, CONTENT_SETTING_ALLOW); | 112 context.UpdateContentSetting(origin, origin, CONTENT_SETTING_ALLOW); |
59 | 113 |
60 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 114 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
61 context.GetPermissionStatus(origin, origin)); | 115 context.GetPermissionStatus(origin, origin)); |
62 } | 116 } |
117 | |
118 // Tests auto-denial after a time delay in incognito. | |
119 TEST_F(NotificationPermissionContextTest, TestDenyInIncognitoAfterDelay) { | |
120 TestNotificationPermissionContext permission_context( | |
121 profile()->GetOffTheRecordProfile()); | |
122 GURL url("https://www.example.com"); | |
123 NavigateAndCommit(url); | |
124 | |
125 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | |
126 web_contents()->GetMainFrame()->GetRoutingID(), | |
127 -1); | |
128 | |
129 scoped_refptr<base::SingleThreadTaskRunner> old_task_runner( | |
130 base::MessageLoop::current()->task_runner()); | |
131 scoped_refptr<base::TestMockTimeTaskRunner> task_runner( | |
132 new base::TestMockTimeTaskRunner(base::Time::Now(), | |
133 base::TimeTicks::Now())); | |
134 base::MessageLoop::current()->SetTaskRunner(task_runner); | |
135 | |
136 ASSERT_EQ(0, permission_context.permission_set_count()); | |
137 ASSERT_FALSE(permission_context.last_permission_set_persisted()); | |
138 ASSERT_EQ(CONTENT_SETTING_DEFAULT, | |
139 permission_context.last_permission_set_setting()); | |
140 | |
141 permission_context.RequestPermission( | |
142 web_contents(), id, url, true /* user_gesture */, base::Bind(&DoNothing)); | |
143 | |
144 // Should be blocked after 1-2 seconds, but the timer is reset whenever the | |
145 // tab is not visible, so these 500ms never add up to >= 1 second. | |
146 for (int n = 0; n < 10; n++) { | |
147 web_contents()->WasShown(); | |
148 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); | |
149 web_contents()->WasHidden(); | |
150 } | |
151 | |
152 EXPECT_EQ(0, permission_context.permission_set_count()); | |
153 EXPECT_EQ(CONTENT_SETTING_ASK, | |
154 permission_context.GetContentSettingFromMap(url, url)); | |
155 | |
156 // Time elapsed whilst hidden is not counted. | |
157 // n.b. This line also clears out any old scheduled timer tasks. This is | |
158 // important, because otherwise Timer::Reset (triggered by | |
159 // VisibilityTimerTabHelper::WasShown) may choose to re-use an existing | |
160 // scheduled task, and when it fires Timer::RunScheduledTask will call | |
161 // TimeTicks::Now() (which unlike task_runner->NowTicks(), we can't fake), | |
162 // and miscalculate the remaining delay at which to fire the timer. | |
163 task_runner->FastForwardBy(base::TimeDelta::FromDays(1)); | |
164 | |
165 EXPECT_EQ(0, permission_context.permission_set_count()); | |
166 EXPECT_EQ(CONTENT_SETTING_ASK, | |
167 permission_context.GetContentSettingFromMap(url, url)); | |
168 | |
169 // Should be blocked after 1-2 seconds. So 500ms is not enough. | |
170 web_contents()->WasShown(); | |
171 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); | |
172 | |
173 EXPECT_EQ(0, permission_context.permission_set_count()); | |
174 EXPECT_EQ(CONTENT_SETTING_ASK, | |
175 permission_context.GetContentSettingFromMap(url, url)); | |
176 | |
177 // But 5*500ms > 2 seconds, so it should now be blocked. | |
178 for (int n = 0; n < 4; n++) | |
179 task_runner->FastForwardBy(base::TimeDelta::FromMilliseconds(500)); | |
Peter Beverloo
2016/01/13 01:42:10
This test is testing three things:
(1) Resetti
johnme
2016/01/13 20:49:26
I kept 1,2,3 together, as splitting it up would ca
| |
180 | |
181 EXPECT_EQ(1, permission_context.permission_set_count()); | |
182 EXPECT_TRUE(permission_context.last_permission_set_persisted()); | |
183 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
184 permission_context.last_permission_set_setting()); | |
185 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
186 permission_context.GetContentSettingFromMap(url, url)); | |
187 | |
188 base::MessageLoop::current()->SetTaskRunner(old_task_runner); | |
189 } | |
OLD | NEW |