| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/services/gcm/push_messaging_permission_context.h" | |
| 6 | |
| 7 #include "chrome/browser/content_settings/permission_context_uma_util.h" | |
| 8 #include "chrome/browser/notifications/desktop_notification_service.h" | |
| 9 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
| 12 #include "components/content_settings/core/common/permission_request_id.h" | |
| 13 #include "content/public/browser/browser_thread.h" | |
| 14 #include "content/public/browser/web_contents.h" | |
| 15 #include "content/public/browser/web_contents_delegate.h" | |
| 16 | |
| 17 const ContentSettingsType kPushSettingType = | |
| 18 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING; | |
| 19 | |
| 20 namespace gcm { | |
| 21 | |
| 22 PushMessagingPermissionContext::PushMessagingPermissionContext(Profile* profile) | |
| 23 : PermissionContextBase(profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING), | |
| 24 profile_(profile), | |
| 25 weak_factory_ui_thread_(this) { | |
| 26 } | |
| 27 | |
| 28 PushMessagingPermissionContext::~PushMessagingPermissionContext() { | |
| 29 } | |
| 30 | |
| 31 ContentSetting PushMessagingPermissionContext::GetPermissionStatus( | |
| 32 const GURL& requesting_origin, | |
| 33 const GURL& embedding_origin) const { | |
| 34 #if defined(ENABLE_NOTIFICATIONS) | |
| 35 if (requesting_origin != embedding_origin) | |
| 36 return CONTENT_SETTING_BLOCK; | |
| 37 | |
| 38 ContentSetting push_content_setting = | |
| 39 profile_->GetHostContentSettingsMap()->GetContentSetting( | |
| 40 requesting_origin, embedding_origin, kPushSettingType, std::string()); | |
| 41 | |
| 42 DesktopNotificationService* notification_service = | |
| 43 DesktopNotificationServiceFactory::GetForProfile(profile_); | |
| 44 DCHECK(notification_service); | |
| 45 | |
| 46 ContentSetting notifications_permission = | |
| 47 notification_service->GetPermissionStatus(requesting_origin, | |
| 48 embedding_origin); | |
| 49 | |
| 50 if (notifications_permission == CONTENT_SETTING_BLOCK || | |
| 51 push_content_setting == CONTENT_SETTING_BLOCK) { | |
| 52 return CONTENT_SETTING_BLOCK; | |
| 53 } | |
| 54 if (notifications_permission == CONTENT_SETTING_ASK || | |
| 55 push_content_setting == CONTENT_SETTING_ASK) { | |
| 56 return CONTENT_SETTING_ASK; | |
| 57 } | |
| 58 DCHECK_EQ(CONTENT_SETTING_ALLOW, notifications_permission); | |
| 59 DCHECK_EQ(CONTENT_SETTING_ALLOW, push_content_setting); | |
| 60 return CONTENT_SETTING_ALLOW; | |
| 61 #else | |
| 62 return CONTENT_SETTING_BLOCK; | |
| 63 #endif | |
| 64 } | |
| 65 | |
| 66 void PushMessagingPermissionContext::CancelPermissionRequest( | |
| 67 content::WebContents* web_contents, const PermissionRequestID& id) { | |
| 68 // TODO(peter): consider implementing this method. | |
| 69 NOTIMPLEMENTED() << "CancelPermission not implemented for push messaging"; | |
| 70 } | |
| 71 | |
| 72 // Unlike other permissions, push is decided by the following algorithm | |
| 73 // - You need to request it from a top level domain | |
| 74 // - You need to have notification permission granted. | |
| 75 // - You need to not have push permission explicitly blocked. | |
| 76 // - If those two things are true it is granted without prompting. | |
| 77 // This is done to avoid double prompting for notifications and push. | |
| 78 void PushMessagingPermissionContext::DecidePermission( | |
| 79 content::WebContents* web_contents, | |
| 80 const PermissionRequestID& id, | |
| 81 const GURL& requesting_origin, | |
| 82 const GURL& embedding_origin, | |
| 83 bool user_gesture, | |
| 84 const BrowserPermissionCallback& callback) { | |
| 85 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 86 #if defined(ENABLE_NOTIFICATIONS) | |
| 87 if (requesting_origin != embedding_origin) { | |
| 88 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, | |
| 89 false /* persist */, CONTENT_SETTING_BLOCK); | |
| 90 return; | |
| 91 } | |
| 92 DesktopNotificationService* notification_service = | |
| 93 DesktopNotificationServiceFactory::GetForProfile(profile_); | |
| 94 DCHECK(notification_service); | |
| 95 | |
| 96 notification_service->RequestPermission( | |
| 97 web_contents, id, requesting_origin, user_gesture, | |
| 98 base::Bind(&PushMessagingPermissionContext::DecidePushPermission, | |
| 99 weak_factory_ui_thread_.GetWeakPtr(), id, requesting_origin, | |
| 100 embedding_origin, callback)); | |
| 101 #else | |
| 102 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, | |
| 103 false /* persist */, CONTENT_SETTING_BLOCK); | |
| 104 #endif | |
| 105 } | |
| 106 | |
| 107 void PushMessagingPermissionContext::DecidePushPermission( | |
| 108 const PermissionRequestID& id, | |
| 109 const GURL& requesting_origin, | |
| 110 const GURL& embedding_origin, | |
| 111 const BrowserPermissionCallback& callback, | |
| 112 ContentSetting notification_content_setting) { | |
| 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 114 ContentSetting push_content_setting = | |
| 115 profile_->GetHostContentSettingsMap() | |
| 116 ->GetContentSettingAndMaybeUpdateLastUsage( | |
| 117 requesting_origin, embedding_origin, kPushSettingType, | |
| 118 std::string()); | |
| 119 | |
| 120 if (push_content_setting == CONTENT_SETTING_BLOCK) { | |
| 121 DVLOG(1) << "Push permission was explicitly blocked."; | |
| 122 PermissionContextUmaUtil::PermissionDenied(kPushSettingType, | |
| 123 requesting_origin); | |
| 124 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, | |
| 125 true /* persist */, CONTENT_SETTING_BLOCK); | |
| 126 return; | |
| 127 } | |
| 128 | |
| 129 if (notification_content_setting != CONTENT_SETTING_ALLOW) { | |
| 130 DVLOG(1) << "Notification permission has not been granted."; | |
| 131 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, | |
| 132 false /* persist */, notification_content_setting); | |
| 133 return; | |
| 134 } | |
| 135 | |
| 136 PermissionContextUmaUtil::PermissionGranted(kPushSettingType, | |
| 137 requesting_origin); | |
| 138 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, | |
| 139 true /* persist */, CONTENT_SETTING_ALLOW); | |
| 140 } | |
| 141 } // namespace gcm | |
| 142 | |
| OLD | NEW |