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