| 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/push_messaging/push_messaging_permission_context.h" | 5 #include "chrome/browser/push_messaging/push_messaging_permission_context.h" |
| 6 | 6 |
| 7 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 7 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 8 #include "chrome/browser/notifications/notification_permission_context.h" | 8 #include "chrome/browser/notifications/notification_permission_context.h" |
| 9 #include "chrome/browser/notifications/notification_permission_context_factory.h
" | 9 #include "chrome/browser/notifications/notification_permission_context_factory.h
" |
| 10 #include "chrome/browser/permissions/permission_request_id.h" | 10 #include "chrome/browser/permissions/permission_request_id.h" |
| 11 #include "chrome/browser/permissions/permission_uma_util.h" | 11 #include "chrome/browser/permissions/permission_uma_util.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/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 "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/permission_type.h" | 15 #include "content/public/browser/permission_type.h" |
| 16 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/browser/web_contents_delegate.h" | 17 #include "content/public/browser/web_contents_delegate.h" |
| 18 #include "content/public/common/origin_util.h" | 18 #include "content/public/common/origin_util.h" |
| 19 | 19 |
| 20 PushMessagingPermissionContext::PushMessagingPermissionContext(Profile* profile) | 20 PushMessagingPermissionContext::PushMessagingPermissionContext(Profile* profile) |
| 21 : PermissionContextBase(profile, | 21 : PermissionContextBase(profile, |
| 22 content::PermissionType::PUSH_MESSAGING, | 22 content::PermissionType::PUSH_MESSAGING, |
| 23 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING), | 23 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING), |
| 24 profile_(profile), | 24 profile_(profile), |
| 25 weak_factory_ui_thread_(this) {} | 25 weak_factory_ui_thread_(this) {} |
| 26 | 26 |
| 27 PushMessagingPermissionContext::~PushMessagingPermissionContext() {} | 27 PushMessagingPermissionContext::~PushMessagingPermissionContext() {} |
| 28 | 28 |
| 29 ContentSetting PushMessagingPermissionContext::GetPermissionStatus( | 29 ContentSetting PushMessagingPermissionContext::GetPermissionStatus( |
| 30 const GURL& requesting_origin, | 30 const url::Origin& requesting_origin, |
| 31 const GURL& embedding_origin) const { | 31 const url::Origin& embedding_origin) const { |
| 32 // It's possible for this to return CONTENT_SETTING_BLOCK in cases where | 32 // It's possible for this to return CONTENT_SETTING_BLOCK in cases where |
| 33 // HostContentSettingsMap::GetContentSetting returns CONTENT_SETTING_ALLOW. | 33 // HostContentSettingsMap::GetContentSetting returns CONTENT_SETTING_ALLOW. |
| 34 // TODO(johnme): This is likely to break assumptions made elsewhere, so we | 34 // TODO(johnme): This is likely to break assumptions made elsewhere, so we |
| 35 // should try to remove this quirk. | 35 // should try to remove this quirk. |
| 36 #if defined(ENABLE_NOTIFICATIONS) | 36 #if defined(ENABLE_NOTIFICATIONS) |
| 37 if (requesting_origin != embedding_origin) | 37 if (!requesting_origin.IsSameOriginWith(embedding_origin)) |
| 38 return CONTENT_SETTING_BLOCK; | 38 return CONTENT_SETTING_BLOCK; |
| 39 | 39 |
| 40 ContentSetting push_content_setting = | 40 ContentSetting push_content_setting = |
| 41 PermissionContextBase::GetPermissionStatus(requesting_origin, | 41 PermissionContextBase::GetPermissionStatus(requesting_origin, |
| 42 embedding_origin); | 42 embedding_origin); |
| 43 | 43 |
| 44 NotificationPermissionContext* notification_context = | 44 NotificationPermissionContext* notification_context = |
| 45 NotificationPermissionContextFactory::GetForProfile(profile_); | 45 NotificationPermissionContextFactory::GetForProfile(profile_); |
| 46 DCHECK(notification_context); | 46 DCHECK(notification_context); |
| 47 | 47 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 67 | 67 |
| 68 // Unlike other permissions, push is decided by the following algorithm | 68 // Unlike other permissions, push is decided by the following algorithm |
| 69 // - You need to request it from a top level domain | 69 // - You need to request it from a top level domain |
| 70 // - You need to have notification permission granted. | 70 // - You need to have notification permission granted. |
| 71 // - You need to not have push permission explicitly blocked. | 71 // - You need to not have push permission explicitly blocked. |
| 72 // - If those 3 things are true it is granted without prompting. | 72 // - If those 3 things are true it is granted without prompting. |
| 73 // This is done to avoid double prompting for notifications and push. | 73 // This is done to avoid double prompting for notifications and push. |
| 74 void PushMessagingPermissionContext::DecidePermission( | 74 void PushMessagingPermissionContext::DecidePermission( |
| 75 content::WebContents* web_contents, | 75 content::WebContents* web_contents, |
| 76 const PermissionRequestID& id, | 76 const PermissionRequestID& id, |
| 77 const GURL& requesting_origin, | 77 const url::Origin& requesting_origin, |
| 78 const GURL& embedding_origin, | 78 const url::Origin& embedding_origin, |
| 79 const BrowserPermissionCallback& callback) { | 79 const BrowserPermissionCallback& callback) { |
| 80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 81 #if defined(ENABLE_NOTIFICATIONS) | 81 #if defined(ENABLE_NOTIFICATIONS) |
| 82 if (requesting_origin != embedding_origin) { | 82 if (!requesting_origin.IsSameOriginWith(embedding_origin)) { |
| 83 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, | 83 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, |
| 84 false /* persist */, CONTENT_SETTING_BLOCK); | 84 false /* persist */, CONTENT_SETTING_BLOCK); |
| 85 return; | 85 return; |
| 86 } | 86 } |
| 87 | 87 |
| 88 NotificationPermissionContext* notification_context = | 88 NotificationPermissionContext* notification_context = |
| 89 NotificationPermissionContextFactory::GetForProfile(profile_); | 89 NotificationPermissionContextFactory::GetForProfile(profile_); |
| 90 DCHECK(notification_context); | 90 DCHECK(notification_context); |
| 91 | 91 |
| 92 notification_context->RequestPermission( | 92 notification_context->RequestPermission( |
| 93 web_contents, id, requesting_origin, | 93 web_contents, id, requesting_origin, |
| 94 base::Bind(&PushMessagingPermissionContext::DecidePushPermission, | 94 base::Bind(&PushMessagingPermissionContext::DecidePushPermission, |
| 95 weak_factory_ui_thread_.GetWeakPtr(), id, requesting_origin, | 95 weak_factory_ui_thread_.GetWeakPtr(), id, requesting_origin, |
| 96 embedding_origin, callback)); | 96 embedding_origin, callback)); |
| 97 #else | 97 #else |
| 98 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, | 98 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, |
| 99 false /* persist */, CONTENT_SETTING_BLOCK); | 99 false /* persist */, CONTENT_SETTING_BLOCK); |
| 100 #endif | 100 #endif |
| 101 } | 101 } |
| 102 | 102 |
| 103 bool PushMessagingPermissionContext::IsRestrictedToSecureOrigins() const { | 103 bool PushMessagingPermissionContext::IsRestrictedToSecureOrigins() const { |
| 104 return true; | 104 return true; |
| 105 } | 105 } |
| 106 | 106 |
| 107 void PushMessagingPermissionContext::DecidePushPermission( | 107 void PushMessagingPermissionContext::DecidePushPermission( |
| 108 const PermissionRequestID& id, | 108 const PermissionRequestID& id, |
| 109 const GURL& requesting_origin, | 109 const url::Origin& requesting_origin, |
| 110 const GURL& embedding_origin, | 110 const url::Origin& embedding_origin, |
| 111 const BrowserPermissionCallback& callback, | 111 const BrowserPermissionCallback& callback, |
| 112 ContentSetting notification_content_setting) { | 112 ContentSetting notification_content_setting) { |
| 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 114 const GURL requesting_url(requesting_origin.Serialize()); |
| 115 const GURL embedding_url(embedding_origin.Serialize()); |
| 114 ContentSetting push_content_setting = | 116 ContentSetting push_content_setting = |
| 115 HostContentSettingsMapFactory::GetForProfile(profile_) | 117 HostContentSettingsMapFactory::GetForProfile(profile_) |
| 116 ->GetContentSettingAndMaybeUpdateLastUsage( | 118 ->GetContentSettingAndMaybeUpdateLastUsage( |
| 117 requesting_origin, embedding_origin, content_settings_type(), | 119 requesting_url, embedding_url, content_settings_type(), |
| 118 std::string()); | 120 std::string()); |
| 119 | 121 |
| 120 if (push_content_setting == CONTENT_SETTING_BLOCK) { | 122 if (push_content_setting == CONTENT_SETTING_BLOCK) { |
| 121 DVLOG(1) << "Push permission was explicitly blocked."; | 123 DVLOG(1) << "Push permission was explicitly blocked."; |
| 122 PermissionUmaUtil::PermissionDenied(permission_type(), requesting_origin); | 124 PermissionUmaUtil::PermissionDenied(permission_type(), requesting_url); |
| 123 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, | 125 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, |
| 124 true /* persist */, CONTENT_SETTING_BLOCK); | 126 true /* persist */, CONTENT_SETTING_BLOCK); |
| 125 return; | 127 return; |
| 126 } | 128 } |
| 127 | 129 |
| 128 if (notification_content_setting != CONTENT_SETTING_ALLOW) { | 130 if (notification_content_setting != CONTENT_SETTING_ALLOW) { |
| 129 DVLOG(1) << "Notification permission has not been granted."; | 131 DVLOG(1) << "Notification permission has not been granted."; |
| 130 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, | 132 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, |
| 131 false /* persist */, notification_content_setting); | 133 false /* persist */, notification_content_setting); |
| 132 return; | 134 return; |
| 133 } | 135 } |
| 134 | 136 |
| 135 PermissionUmaUtil::PermissionGranted(permission_type(), requesting_origin); | 137 PermissionUmaUtil::PermissionGranted(permission_type(), requesting_url); |
| 136 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, | 138 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, |
| 137 true /* persist */, CONTENT_SETTING_ALLOW); | 139 true /* persist */, CONTENT_SETTING_ALLOW); |
| 138 } | 140 } |
| OLD | NEW |