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/services/gcm/push_messaging_permission_context.h" | 5 #include "chrome/browser/services/gcm/push_messaging_permission_context.h" |
6 | 6 |
| 7 #include "chrome/browser/profiles/profile.h" |
| 8 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 9 #include "content/public/browser/web_contents.h" |
| 10 #include "content/public/browser/web_contents_delegate.h" |
| 11 |
| 12 const ContentSettingsType kPushSettingType = |
| 13 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING; |
| 14 const ContentSettingsType kNotificationSettingType = |
| 15 CONTENT_SETTINGS_TYPE_NOTIFICATIONS; |
| 16 |
7 namespace gcm { | 17 namespace gcm { |
8 | 18 |
9 PushMessagingPermissionContext::PushMessagingPermissionContext(Profile* profile) | 19 PushMessagingPermissionContext::PushMessagingPermissionContext(Profile* profile) |
10 : PermissionContextBase(profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING) { | 20 : PermissionContextBase(profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING), |
| 21 profile_(profile) { |
11 } | 22 } |
12 | 23 |
13 PushMessagingPermissionContext::~PushMessagingPermissionContext() { | 24 PushMessagingPermissionContext::~PushMessagingPermissionContext() { |
14 } | 25 } |
15 | 26 |
| 27 ContentSetting PushMessagingPermissionContext::GetPermissionStatus( |
| 28 const GURL& requesting_origin, |
| 29 const GURL& embedding_origin) const { |
| 30 ContentSetting push_content_setting = |
| 31 profile_->GetHostContentSettingsMap()->GetContentSetting( |
| 32 requesting_origin, embedding_origin, kPushSettingType, std::string()); |
| 33 |
| 34 ContentSetting notifications_content_setting = |
| 35 profile_->GetHostContentSettingsMap()->GetContentSetting( |
| 36 requesting_origin, embedding_origin, kNotificationSettingType, |
| 37 std::string()); |
| 38 |
| 39 if (notifications_content_setting == CONTENT_SETTING_BLOCK || |
| 40 push_content_setting == CONTENT_SETTING_BLOCK) { |
| 41 return CONTENT_SETTING_BLOCK; |
| 42 } |
| 43 if (notifications_content_setting == CONTENT_SETTING_ASK || |
| 44 push_content_setting == CONTENT_SETTING_ASK) { |
| 45 return CONTENT_SETTING_ASK; |
| 46 } |
| 47 DCHECK_EQ(CONTENT_SETTING_ALLOW, notifications_content_setting); |
| 48 DCHECK_EQ(CONTENT_SETTING_ALLOW, push_content_setting); |
| 49 return CONTENT_SETTING_ALLOW; |
| 50 } |
| 51 |
| 52 // Unlike other permissions, push is decided by the following algorithm |
| 53 // - You need to have notification permission granted. |
| 54 // - You need to not have push permission explicitly blocked. |
| 55 // - If those two things are true it is granted without prompting. |
| 56 // This is done to avoid double prompting for notifications and push. |
| 57 void PushMessagingPermissionContext::DecidePermission( |
| 58 content::WebContents* web_contents, |
| 59 const PermissionRequestID& id, |
| 60 const GURL& requesting_origin, |
| 61 const GURL& embedding_origin, |
| 62 bool user_gesture, |
| 63 const BrowserPermissionCallback& callback) { |
| 64 ContentSetting notifications_content_setting = |
| 65 profile_->GetHostContentSettingsMap() |
| 66 ->GetContentSettingAndMaybeUpdateLastUsage( |
| 67 requesting_origin, embedding_origin, kNotificationSettingType, |
| 68 std::string()); |
| 69 |
| 70 if (notifications_content_setting != CONTENT_SETTING_ALLOW) { |
| 71 DVLOG(1) << "Notification permission has not been granted."; |
| 72 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, |
| 73 false /* persist */, false /* granted */); |
| 74 return; |
| 75 } |
| 76 |
| 77 ContentSetting push_content_setting = |
| 78 profile_->GetHostContentSettingsMap() |
| 79 ->GetContentSettingAndMaybeUpdateLastUsage( |
| 80 requesting_origin, embedding_origin, kPushSettingType, |
| 81 std::string()); |
| 82 |
| 83 if (push_content_setting == CONTENT_SETTING_BLOCK) { |
| 84 DVLOG(1) << "Push permission was explicitly blocked."; |
| 85 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, |
| 86 false /* persist */, false /* granted */); |
| 87 return; |
| 88 } |
| 89 |
| 90 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback, |
| 91 true /* persist */, true /* granted */); |
| 92 } |
| 93 |
16 } // namespace gcm | 94 } // namespace gcm |
OLD | NEW |