OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/content_settings/permission_context_base.h" | 5 #include "chrome/browser/content_settings/permission_context_base.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "chrome/browser/content_settings/permission_queue_controller.h" | 9 #include "chrome/browser/content_settings/permission_queue_controller.h" |
10 #include "chrome/browser/infobars/infobar_service.h" | 10 #include "chrome/browser/infobars/infobar_service.h" |
11 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | 11 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
14 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
15 #include "components/content_settings/core/browser/host_content_settings_map.h" | 15 #include "components/content_settings/core/browser/host_content_settings_map.h" |
16 #include "components/content_settings/core/common/content_settings.h" | 16 #include "components/content_settings/core/common/content_settings.h" |
17 #include "components/content_settings/core/common/content_settings_types.h" | 17 #include "components/content_settings/core/common/content_settings_types.h" |
18 #include "components/content_settings/core/common/permission_request_id.h" | 18 #include "components/content_settings/core/common/permission_request_id.h" |
| 19 #include "content/public/browser/render_frame_host.h" |
19 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
20 #include "content/public/test/mock_render_process_host.h" | 21 #include "content/public/test/mock_render_process_host.h" |
21 #include "content/public/test/web_contents_tester.h" | 22 #include "content/public/test/web_contents_tester.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 | 24 |
24 class TestPermissionContext : public PermissionContextBase { | 25 class TestPermissionContext : public PermissionContextBase { |
25 public: | 26 public: |
26 TestPermissionContext(Profile* profile, | 27 TestPermissionContext(Profile* profile, |
27 const ContentSettingsType permission_type) | 28 const ContentSettingsType permission_type) |
28 : PermissionContextBase(profile, permission_type), | 29 : PermissionContextBase(profile, permission_type), |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 } | 102 } |
102 | 103 |
103 void TestAskAndGrant_TestContent() { | 104 void TestAskAndGrant_TestContent() { |
104 TestPermissionContext permission_context( | 105 TestPermissionContext permission_context( |
105 profile(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 106 profile(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
106 GURL url("http://www.google.com"); | 107 GURL url("http://www.google.com"); |
107 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | 108 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
108 | 109 |
109 const PermissionRequestID id( | 110 const PermissionRequestID id( |
110 web_contents()->GetRenderProcessHost()->GetID(), | 111 web_contents()->GetRenderProcessHost()->GetID(), |
111 web_contents()->GetRenderViewHost()->GetRoutingID(), | 112 web_contents()->GetMainFrame()->GetRoutingID(), |
112 -1, GURL()); | 113 -1, GURL()); |
113 permission_context.RequestPermission( | 114 permission_context.RequestPermission( |
114 web_contents(), | 115 web_contents(), |
115 id, url, true, | 116 id, url, true, |
116 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 117 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
117 base::Unretained(&permission_context))); | 118 base::Unretained(&permission_context))); |
118 | 119 |
119 RespondToPermission(&permission_context, id, url, true); | 120 RespondToPermission(&permission_context, id, url, true); |
120 EXPECT_TRUE(permission_context.permission_set()); | 121 EXPECT_TRUE(permission_context.permission_set()); |
121 EXPECT_TRUE(permission_context.permission_granted()); | 122 EXPECT_TRUE(permission_context.permission_granted()); |
122 EXPECT_TRUE(permission_context.tab_context_updated()); | 123 EXPECT_TRUE(permission_context.tab_context_updated()); |
123 | 124 |
124 ContentSetting setting = | 125 ContentSetting setting = |
125 profile()->GetHostContentSettingsMap()->GetContentSetting( | 126 profile()->GetHostContentSettingsMap()->GetContentSetting( |
126 url.GetOrigin(), url.GetOrigin(), | 127 url.GetOrigin(), url.GetOrigin(), |
127 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()); | 128 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()); |
128 EXPECT_EQ(CONTENT_SETTING_ALLOW, setting); | 129 EXPECT_EQ(CONTENT_SETTING_ALLOW, setting); |
129 } | 130 } |
130 | 131 |
131 void TestAskAndDismiss_TestContent() { | 132 void TestAskAndDismiss_TestContent() { |
132 TestPermissionContext permission_context( | 133 TestPermissionContext permission_context( |
133 profile(), CONTENT_SETTINGS_TYPE_MIDI_SYSEX); | 134 profile(), CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
134 GURL url("http://www.google.es"); | 135 GURL url("http://www.google.es"); |
135 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | 136 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
136 | 137 |
137 const PermissionRequestID id( | 138 const PermissionRequestID id( |
138 web_contents()->GetRenderProcessHost()->GetID(), | 139 web_contents()->GetRenderProcessHost()->GetID(), |
139 web_contents()->GetRenderViewHost()->GetRoutingID(), | 140 web_contents()->GetMainFrame()->GetRoutingID(), |
140 -1, GURL()); | 141 -1, GURL()); |
141 permission_context.RequestPermission( | 142 permission_context.RequestPermission( |
142 web_contents(), | 143 web_contents(), |
143 id, url, true, | 144 id, url, true, |
144 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 145 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
145 base::Unretained(&permission_context))); | 146 base::Unretained(&permission_context))); |
146 | 147 |
147 RespondToPermission(&permission_context, id, url, false); | 148 RespondToPermission(&permission_context, id, url, false); |
148 EXPECT_TRUE(permission_context.permission_set()); | 149 EXPECT_TRUE(permission_context.permission_set()); |
149 EXPECT_FALSE(permission_context.permission_granted()); | 150 EXPECT_FALSE(permission_context.permission_granted()); |
150 EXPECT_TRUE(permission_context.tab_context_updated()); | 151 EXPECT_TRUE(permission_context.tab_context_updated()); |
151 | 152 |
152 ContentSetting setting = | 153 ContentSetting setting = |
153 profile()->GetHostContentSettingsMap()->GetContentSetting( | 154 profile()->GetHostContentSettingsMap()->GetContentSetting( |
154 url.GetOrigin(), url.GetOrigin(), | 155 url.GetOrigin(), url.GetOrigin(), |
155 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, std::string()); | 156 CONTENT_SETTINGS_TYPE_MIDI_SYSEX, std::string()); |
156 EXPECT_EQ(CONTENT_SETTING_ASK, setting); | 157 EXPECT_EQ(CONTENT_SETTING_ASK, setting); |
157 } | 158 } |
158 | 159 |
159 void TestRequestPermissionInvalidUrl(ContentSettingsType type) { | 160 void TestRequestPermissionInvalidUrl(ContentSettingsType type) { |
160 TestPermissionContext permission_context(profile(), type); | 161 TestPermissionContext permission_context(profile(), type); |
161 GURL url; | 162 GURL url; |
162 ASSERT_FALSE(url.is_valid()); | 163 ASSERT_FALSE(url.is_valid()); |
163 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | 164 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
164 | 165 |
165 const PermissionRequestID id( | 166 const PermissionRequestID id( |
166 web_contents()->GetRenderProcessHost()->GetID(), | 167 web_contents()->GetRenderProcessHost()->GetID(), |
167 web_contents()->GetRenderViewHost()->GetRoutingID(), | 168 web_contents()->GetMainFrame()->GetRoutingID(), |
168 -1, GURL()); | 169 -1, GURL()); |
169 permission_context.RequestPermission( | 170 permission_context.RequestPermission( |
170 web_contents(), | 171 web_contents(), |
171 id, url, true, | 172 id, url, true, |
172 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 173 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
173 base::Unretained(&permission_context))); | 174 base::Unretained(&permission_context))); |
174 | 175 |
175 EXPECT_TRUE(permission_context.permission_set()); | 176 EXPECT_TRUE(permission_context.permission_set()); |
176 EXPECT_FALSE(permission_context.permission_granted()); | 177 EXPECT_FALSE(permission_context.permission_granted()); |
177 EXPECT_TRUE(permission_context.tab_context_updated()); | 178 EXPECT_TRUE(permission_context.tab_context_updated()); |
178 | 179 |
179 ContentSetting setting = | 180 ContentSetting setting = |
180 profile()->GetHostContentSettingsMap()->GetContentSetting( | 181 profile()->GetHostContentSettingsMap()->GetContentSetting( |
181 url.GetOrigin(), url.GetOrigin(), type, std::string()); | 182 url.GetOrigin(), url.GetOrigin(), type, std::string()); |
182 EXPECT_EQ(CONTENT_SETTING_ASK, setting); | 183 EXPECT_EQ(CONTENT_SETTING_ASK, setting); |
183 } | 184 } |
184 | 185 |
185 void TestGrantAndRevoke_TestContent(ContentSettingsType type, | 186 void TestGrantAndRevoke_TestContent(ContentSettingsType type, |
186 ContentSetting expected_default) { | 187 ContentSetting expected_default) { |
187 TestPermissionContext permission_context(profile(), type); | 188 TestPermissionContext permission_context(profile(), type); |
188 GURL url("https://www.google.com"); | 189 GURL url("https://www.google.com"); |
189 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | 190 content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); |
190 | 191 |
191 const PermissionRequestID id( | 192 const PermissionRequestID id( |
192 web_contents()->GetRenderProcessHost()->GetID(), | 193 web_contents()->GetRenderProcessHost()->GetID(), |
193 web_contents()->GetRenderViewHost()->GetRoutingID(), | 194 web_contents()->GetMainFrame()->GetRoutingID(), |
194 -1, GURL()); | 195 -1, GURL()); |
195 permission_context.RequestPermission( | 196 permission_context.RequestPermission( |
196 web_contents(), | 197 web_contents(), |
197 id, url, true, | 198 id, url, true, |
198 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 199 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
199 base::Unretained(&permission_context))); | 200 base::Unretained(&permission_context))); |
200 | 201 |
201 RespondToPermission(&permission_context, id, url, true); | 202 RespondToPermission(&permission_context, id, url, true); |
202 EXPECT_TRUE(permission_context.permission_set()); | 203 EXPECT_TRUE(permission_context.permission_set()); |
203 EXPECT_TRUE(permission_context.permission_granted()); | 204 EXPECT_TRUE(permission_context.permission_granted()); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 #endif | 290 #endif |
290 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, | 291 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, |
291 CONTENT_SETTING_ASK); | 292 CONTENT_SETTING_ASK); |
292 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, | 293 TestGrantAndRevoke_TestContent(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, |
293 CONTENT_SETTING_ASK); | 294 CONTENT_SETTING_ASK); |
294 #if defined(OS_ANDROID) | 295 #if defined(OS_ANDROID) |
295 TestGrantAndRevoke_TestContent( | 296 TestGrantAndRevoke_TestContent( |
296 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER, CONTENT_SETTING_ASK); | 297 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER, CONTENT_SETTING_ASK); |
297 #endif | 298 #endif |
298 } | 299 } |
OLD | NEW |