| 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/permissions/permission_context_base.h" | 5 #include "chrome/browser/permissions/permission_context_base.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 | 82 |
| 83 ContentSetting GetContentSettingFromMap(const GURL& url_a, | 83 ContentSetting GetContentSettingFromMap(const GURL& url_a, |
| 84 const GURL& url_b) { | 84 const GURL& url_b) { |
| 85 return HostContentSettingsMapFactory::GetForProfile(profile()) | 85 return HostContentSettingsMapFactory::GetForProfile(profile()) |
| 86 ->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), | 86 ->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), |
| 87 content_settings_type(), std::string()); | 87 content_settings_type(), std::string()); |
| 88 } | 88 } |
| 89 | 89 |
| 90 protected: | 90 protected: |
| 91 void UpdateTabContext(const PermissionRequestID& id, | 91 void UpdateTabContext(const PermissionRequestID& id, |
| 92 const GURL& requesting_origin, | 92 const url::Origin& requesting_origin, |
| 93 bool allowed) override { | 93 bool allowed) override { |
| 94 tab_context_updated_ = true; | 94 tab_context_updated_ = true; |
| 95 } | 95 } |
| 96 | 96 |
| 97 bool IsRestrictedToSecureOrigins() const override { | 97 bool IsRestrictedToSecureOrigins() const override { |
| 98 return false; | 98 return false; |
| 99 } | 99 } |
| 100 | 100 |
| 101 private: | 101 private: |
| 102 std::vector<ContentSetting> decisions_; | 102 std::vector<ContentSetting> decisions_; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 default: | 137 default: |
| 138 NOTREACHED(); | 138 NOTREACHED(); |
| 139 } | 139 } |
| 140 #endif | 140 #endif |
| 141 } | 141 } |
| 142 | 142 |
| 143 void TestAskAndGrant_TestContent() { | 143 void TestAskAndGrant_TestContent() { |
| 144 TestPermissionContext permission_context( | 144 TestPermissionContext permission_context( |
| 145 profile(), content::PermissionType::NOTIFICATIONS, | 145 profile(), content::PermissionType::NOTIFICATIONS, |
| 146 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 146 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 147 GURL url("http://www.google.com"); | 147 const GURL url("http://www.google.com"); |
| 148 NavigateAndCommit(url); | 148 NavigateAndCommit(url); |
| 149 | 149 |
| 150 const PermissionRequestID id( | 150 const PermissionRequestID id( |
| 151 web_contents()->GetRenderProcessHost()->GetID(), | 151 web_contents()->GetRenderProcessHost()->GetID(), |
| 152 web_contents()->GetMainFrame()->GetRoutingID(), | 152 web_contents()->GetMainFrame()->GetRoutingID(), |
| 153 -1); | 153 -1); |
| 154 const url::Origin origin(url); |
| 154 permission_context.RequestPermission( | 155 permission_context.RequestPermission( |
| 155 web_contents(), id, url, | 156 web_contents(), id, origin, |
| 156 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 157 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 157 base::Unretained(&permission_context))); | 158 base::Unretained(&permission_context))); |
| 158 | 159 |
| 159 RespondToPermission(&permission_context, id, url, CONTENT_SETTING_ALLOW); | 160 RespondToPermission(&permission_context, id, url, CONTENT_SETTING_ALLOW); |
| 160 EXPECT_EQ(1u, permission_context.decisions().size()); | 161 EXPECT_EQ(1u, permission_context.decisions().size()); |
| 161 EXPECT_EQ(CONTENT_SETTING_ALLOW, permission_context.decisions()[0]); | 162 EXPECT_EQ(CONTENT_SETTING_ALLOW, permission_context.decisions()[0]); |
| 162 EXPECT_TRUE(permission_context.tab_context_updated()); | 163 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 163 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 164 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 164 permission_context.GetContentSettingFromMap(url, url)); | 165 permission_context.GetContentSettingFromMap(url, url)); |
| 165 } | 166 } |
| 166 | 167 |
| 167 void TestAskAndDismiss_TestContent() { | 168 void TestAskAndDismiss_TestContent() { |
| 168 TestPermissionContext permission_context( | 169 TestPermissionContext permission_context( |
| 169 profile(), content::PermissionType::MIDI_SYSEX, | 170 profile(), content::PermissionType::MIDI_SYSEX, |
| 170 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); | 171 CONTENT_SETTINGS_TYPE_MIDI_SYSEX); |
| 171 GURL url("http://www.google.es"); | 172 const GURL url("http://www.google.es"); |
| 172 NavigateAndCommit(url); | 173 NavigateAndCommit(url); |
| 173 | 174 |
| 174 const PermissionRequestID id( | 175 const PermissionRequestID id( |
| 175 web_contents()->GetRenderProcessHost()->GetID(), | 176 web_contents()->GetRenderProcessHost()->GetID(), |
| 176 web_contents()->GetMainFrame()->GetRoutingID(), | 177 web_contents()->GetMainFrame()->GetRoutingID(), |
| 177 -1); | 178 -1); |
| 179 const url::Origin origin(url); |
| 178 permission_context.RequestPermission( | 180 permission_context.RequestPermission( |
| 179 web_contents(), id, url, | 181 web_contents(), id, origin, |
| 180 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 182 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 181 base::Unretained(&permission_context))); | 183 base::Unretained(&permission_context))); |
| 182 | 184 |
| 183 RespondToPermission(&permission_context, id, url, CONTENT_SETTING_ASK); | 185 RespondToPermission(&permission_context, id, url, CONTENT_SETTING_ASK); |
| 184 EXPECT_EQ(1u, permission_context.decisions().size()); | 186 EXPECT_EQ(1u, permission_context.decisions().size()); |
| 185 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]); | 187 EXPECT_EQ(CONTENT_SETTING_ASK, permission_context.decisions()[0]); |
| 186 EXPECT_TRUE(permission_context.tab_context_updated()); | 188 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 187 EXPECT_EQ(CONTENT_SETTING_ASK, | 189 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 188 permission_context.GetContentSettingFromMap(url, url)); | 190 permission_context.GetContentSettingFromMap(url, url)); |
| 189 } | 191 } |
| 190 | 192 |
| 191 void TestRequestPermissionInvalidUrl( | 193 void TestRequestPermissionInvalidUrl( |
| 192 content::PermissionType permission_type, | 194 content::PermissionType permission_type, |
| 193 ContentSettingsType content_settings_type) { | 195 ContentSettingsType content_settings_type) { |
| 194 TestPermissionContext permission_context(profile(), permission_type, | 196 TestPermissionContext permission_context(profile(), permission_type, |
| 195 content_settings_type); | 197 content_settings_type); |
| 196 GURL url; | 198 const GURL url; |
| 197 ASSERT_FALSE(url.is_valid()); | 199 ASSERT_FALSE(url.is_valid()); |
| 198 NavigateAndCommit(url); | 200 NavigateAndCommit(url); |
| 199 | 201 |
| 200 const PermissionRequestID id( | 202 const PermissionRequestID id( |
| 201 web_contents()->GetRenderProcessHost()->GetID(), | 203 web_contents()->GetRenderProcessHost()->GetID(), |
| 202 web_contents()->GetMainFrame()->GetRoutingID(), | 204 web_contents()->GetMainFrame()->GetRoutingID(), |
| 203 -1); | 205 -1); |
| 206 const url::Origin origin(url); |
| 204 permission_context.RequestPermission( | 207 permission_context.RequestPermission( |
| 205 web_contents(), id, url, | 208 web_contents(), id, origin, |
| 206 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 209 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 207 base::Unretained(&permission_context))); | 210 base::Unretained(&permission_context))); |
| 208 | 211 |
| 209 EXPECT_EQ(1u, permission_context.decisions().size()); | 212 EXPECT_EQ(1u, permission_context.decisions().size()); |
| 210 EXPECT_EQ(CONTENT_SETTING_BLOCK, permission_context.decisions()[0]); | 213 EXPECT_EQ(CONTENT_SETTING_BLOCK, permission_context.decisions()[0]); |
| 211 EXPECT_TRUE(permission_context.tab_context_updated()); | 214 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 212 EXPECT_EQ(CONTENT_SETTING_ASK, | 215 EXPECT_EQ(CONTENT_SETTING_ASK, |
| 213 permission_context.GetContentSettingFromMap(url, url)); | 216 permission_context.GetContentSettingFromMap(url, url)); |
| 214 } | 217 } |
| 215 | 218 |
| 216 void TestGrantAndRevoke_TestContent(content::PermissionType permission_type, | 219 void TestGrantAndRevoke_TestContent(content::PermissionType permission_type, |
| 217 ContentSettingsType content_settings_type, | 220 ContentSettingsType content_settings_type, |
| 218 ContentSetting expected_default) { | 221 ContentSetting expected_default) { |
| 219 TestPermissionContext permission_context(profile(), permission_type, | 222 TestPermissionContext permission_context(profile(), permission_type, |
| 220 content_settings_type); | 223 content_settings_type); |
| 221 GURL url("https://www.google.com"); | 224 const GURL url("https://www.google.com"); |
| 222 NavigateAndCommit(url); | 225 NavigateAndCommit(url); |
| 223 | 226 |
| 224 const PermissionRequestID id( | 227 const PermissionRequestID id( |
| 225 web_contents()->GetRenderProcessHost()->GetID(), | 228 web_contents()->GetRenderProcessHost()->GetID(), |
| 226 web_contents()->GetMainFrame()->GetRoutingID(), | 229 web_contents()->GetMainFrame()->GetRoutingID(), |
| 227 -1); | 230 -1); |
| 231 const url::Origin origin(url); |
| 228 permission_context.RequestPermission( | 232 permission_context.RequestPermission( |
| 229 web_contents(), id, url, | 233 web_contents(), id, origin, |
| 230 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 234 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 231 base::Unretained(&permission_context))); | 235 base::Unretained(&permission_context))); |
| 232 | 236 |
| 233 RespondToPermission(&permission_context, id, url, CONTENT_SETTING_ALLOW); | 237 RespondToPermission(&permission_context, id, url, CONTENT_SETTING_ALLOW); |
| 234 EXPECT_EQ(1u, permission_context.decisions().size()); | 238 EXPECT_EQ(1u, permission_context.decisions().size()); |
| 235 EXPECT_EQ(CONTENT_SETTING_ALLOW, permission_context.decisions()[0]); | 239 EXPECT_EQ(CONTENT_SETTING_ALLOW, permission_context.decisions()[0]); |
| 236 EXPECT_TRUE(permission_context.tab_context_updated()); | 240 EXPECT_TRUE(permission_context.tab_context_updated()); |
| 237 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 241 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 238 permission_context.GetContentSettingFromMap(url, url)); | 242 permission_context.GetContentSettingFromMap(url, url)); |
| 239 | 243 |
| 240 // Try to reset permission. | 244 // Try to reset permission. |
| 241 permission_context.ResetPermission(url.GetOrigin(), url.GetOrigin()); | 245 permission_context.ResetPermission(origin, origin); |
| 242 ContentSetting setting_after_reset = | 246 ContentSetting setting_after_reset = |
| 243 permission_context.GetContentSettingFromMap(url, url); | 247 permission_context.GetContentSettingFromMap(url, url); |
| 244 ContentSetting default_setting = | 248 ContentSetting default_setting = |
| 245 HostContentSettingsMapFactory::GetForProfile(profile()) | 249 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 246 ->GetDefaultContentSetting(content_settings_type, nullptr); | 250 ->GetDefaultContentSetting(content_settings_type, nullptr); |
| 247 EXPECT_EQ(default_setting, setting_after_reset); | 251 EXPECT_EQ(default_setting, setting_after_reset); |
| 248 } | 252 } |
| 249 | 253 |
| 250 void TestGlobalPermissionsKillSwitch( | 254 void TestGlobalPermissionsKillSwitch( |
| 251 content::PermissionType permission_type, | 255 content::PermissionType permission_type, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 265 kPermissionsKillSwitchTestGroup); | 269 kPermissionsKillSwitchTestGroup); |
| 266 EXPECT_TRUE(permission_context.IsPermissionKillSwitchOn()); | 270 EXPECT_TRUE(permission_context.IsPermissionKillSwitchOn()); |
| 267 } | 271 } |
| 268 | 272 |
| 269 // Don't call this more than once in the same test, as it persists data to | 273 // Don't call this more than once in the same test, as it persists data to |
| 270 // HostContentSettingsMap. | 274 // HostContentSettingsMap. |
| 271 void TestParallelRequests(ContentSetting response) { | 275 void TestParallelRequests(ContentSetting response) { |
| 272 TestPermissionContext permission_context( | 276 TestPermissionContext permission_context( |
| 273 profile(), content::PermissionType::NOTIFICATIONS, | 277 profile(), content::PermissionType::NOTIFICATIONS, |
| 274 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 278 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 275 GURL url("http://www.google.com"); | 279 const GURL url("http://www.google.com"); |
| 276 NavigateAndCommit(url); | 280 NavigateAndCommit(url); |
| 277 | 281 |
| 278 const PermissionRequestID id0( | 282 const PermissionRequestID id0( |
| 279 web_contents()->GetRenderProcessHost()->GetID(), | 283 web_contents()->GetRenderProcessHost()->GetID(), |
| 280 web_contents()->GetMainFrame()->GetRoutingID(), 0); | 284 web_contents()->GetMainFrame()->GetRoutingID(), 0); |
| 281 const PermissionRequestID id1( | 285 const PermissionRequestID id1( |
| 282 web_contents()->GetRenderProcessHost()->GetID(), | 286 web_contents()->GetRenderProcessHost()->GetID(), |
| 283 web_contents()->GetMainFrame()->GetRoutingID(), 1); | 287 web_contents()->GetMainFrame()->GetRoutingID(), 1); |
| 284 | 288 |
| 289 const url::Origin origin(url); |
| 285 permission_context.RequestPermission( | 290 permission_context.RequestPermission( |
| 286 web_contents(), id0, url, | 291 web_contents(), id0, origin, |
| 287 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 292 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 288 base::Unretained(&permission_context))); | 293 base::Unretained(&permission_context))); |
| 289 permission_context.RequestPermission( | 294 permission_context.RequestPermission( |
| 290 web_contents(), id1, url, | 295 web_contents(), id1, origin, |
| 291 base::Bind(&TestPermissionContext::TrackPermissionDecision, | 296 base::Bind(&TestPermissionContext::TrackPermissionDecision, |
| 292 base::Unretained(&permission_context))); | 297 base::Unretained(&permission_context))); |
| 293 | 298 |
| 294 EXPECT_EQ(0u, permission_context.decisions().size()); | 299 EXPECT_EQ(0u, permission_context.decisions().size()); |
| 295 | 300 |
| 296 RespondToPermission(&permission_context, id0, url, response); | 301 RespondToPermission(&permission_context, id0, url, response); |
| 297 | 302 |
| 298 EXPECT_EQ(2u, permission_context.decisions().size()); | 303 EXPECT_EQ(2u, permission_context.decisions().size()); |
| 299 EXPECT_EQ(response, permission_context.decisions()[0]); | 304 EXPECT_EQ(response, permission_context.decisions()[0]); |
| 300 EXPECT_EQ(response, permission_context.decisions()[1]); | 305 EXPECT_EQ(response, permission_context.decisions()[1]); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 TestParallelRequests(CONTENT_SETTING_ALLOW); | 421 TestParallelRequests(CONTENT_SETTING_ALLOW); |
| 417 } | 422 } |
| 418 | 423 |
| 419 TEST_F(PermissionContextBaseTests, TestParallelRequestsBlocked) { | 424 TEST_F(PermissionContextBaseTests, TestParallelRequestsBlocked) { |
| 420 TestParallelRequests(CONTENT_SETTING_BLOCK); | 425 TestParallelRequests(CONTENT_SETTING_BLOCK); |
| 421 } | 426 } |
| 422 | 427 |
| 423 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { | 428 TEST_F(PermissionContextBaseTests, TestParallelRequestsDismissed) { |
| 424 TestParallelRequests(CONTENT_SETTING_ASK); | 429 TestParallelRequests(CONTENT_SETTING_ASK); |
| 425 } | 430 } |
| OLD | NEW |