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.
5 #include "chrome/browser/services/gcm/push_messaging_permission_context.h"
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"
17 const ContentSettingsType kPushSettingType
=
18 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
;
22 PushMessagingPermissionContext::PushMessagingPermissionContext(Profile
* profile
)
23 : PermissionContextBase(profile
, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
),
25 weak_factory_ui_thread_(this) {
28 PushMessagingPermissionContext::~PushMessagingPermissionContext() {
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
;
38 ContentSetting push_content_setting
=
39 profile_
->GetHostContentSettingsMap()->GetContentSetting(
40 requesting_origin
, embedding_origin
, kPushSettingType
, std::string());
42 DesktopNotificationService
* notification_service
=
43 DesktopNotificationServiceFactory::GetForProfile(profile_
);
44 DCHECK(notification_service
);
46 ContentSetting notifications_permission
=
47 notification_service
->GetPermissionStatus(requesting_origin
,
50 if (notifications_permission
== CONTENT_SETTING_BLOCK
||
51 push_content_setting
== CONTENT_SETTING_BLOCK
) {
52 return CONTENT_SETTING_BLOCK
;
54 if (notifications_permission
== CONTENT_SETTING_ASK
||
55 push_content_setting
== CONTENT_SETTING_ASK
) {
56 return CONTENT_SETTING_ASK
;
58 DCHECK_EQ(CONTENT_SETTING_ALLOW
, notifications_permission
);
59 DCHECK_EQ(CONTENT_SETTING_ALLOW
, push_content_setting
);
60 return CONTENT_SETTING_ALLOW
;
62 return CONTENT_SETTING_BLOCK
;
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";
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
,
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
);
92 DesktopNotificationService
* notification_service
=
93 DesktopNotificationServiceFactory::GetForProfile(profile_
);
94 DCHECK(notification_service
);
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
));
102 NotifyPermissionSet(id
, requesting_origin
, embedding_origin
, callback
,
103 false /* persist */, CONTENT_SETTING_BLOCK
);
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
,
120 if (push_content_setting
== CONTENT_SETTING_BLOCK
) {
121 DVLOG(1) << "Push permission was explicitly blocked.";
122 PermissionContextUmaUtil::PermissionDenied(kPushSettingType
,
124 NotifyPermissionSet(id
, requesting_origin
, embedding_origin
, callback
,
125 true /* persist */, CONTENT_SETTING_BLOCK
);
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
);
136 PermissionContextUmaUtil::PermissionGranted(kPushSettingType
,
138 NotifyPermissionSet(id
, requesting_origin
, embedding_origin
, callback
,
139 true /* persist */, CONTENT_SETTING_ALLOW
);