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/push_messaging/push_messaging_permission_context.h"
7 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
8 #include "chrome/browser/notifications/notification_permission_context.h"
9 #include "chrome/browser/notifications/notification_permission_context_factory.h"
10 #include "chrome/browser/permissions/permission_context_uma_util.h"
11 #include "chrome/browser/permissions/permission_request_id.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "components/content_settings/core/browser/host_content_settings_map.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_delegate.h"
18 const ContentSettingsType kPushSettingType
=
19 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
;
21 PushMessagingPermissionContext::PushMessagingPermissionContext(Profile
* profile
)
22 : PermissionContextBase(profile
, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
),
24 weak_factory_ui_thread_(this) {
27 PushMessagingPermissionContext::~PushMessagingPermissionContext() {
30 ContentSetting
PushMessagingPermissionContext::GetPermissionStatus(
31 const GURL
& requesting_origin
,
32 const GURL
& embedding_origin
) const {
33 #if defined(ENABLE_NOTIFICATIONS)
34 if (requesting_origin
!= embedding_origin
)
35 return CONTENT_SETTING_BLOCK
;
37 ContentSetting push_content_setting
=
38 HostContentSettingsMapFactory::GetForProfile(profile_
)->GetContentSetting(
39 requesting_origin
, embedding_origin
, kPushSettingType
, std::string());
41 NotificationPermissionContext
* notification_context
=
42 NotificationPermissionContextFactory::GetForProfile(profile_
);
43 DCHECK(notification_context
);
45 ContentSetting notifications_permission
=
46 notification_context
->GetPermissionStatus(requesting_origin
,
49 if (notifications_permission
== CONTENT_SETTING_BLOCK
||
50 push_content_setting
== CONTENT_SETTING_BLOCK
) {
51 return CONTENT_SETTING_BLOCK
;
53 if (notifications_permission
== CONTENT_SETTING_ASK
||
54 push_content_setting
== CONTENT_SETTING_ASK
) {
55 return CONTENT_SETTING_ASK
;
57 DCHECK_EQ(CONTENT_SETTING_ALLOW
, notifications_permission
);
58 DCHECK_EQ(CONTENT_SETTING_ALLOW
, push_content_setting
);
59 return CONTENT_SETTING_ALLOW
;
61 return CONTENT_SETTING_BLOCK
;
65 // Unlike other permissions, push is decided by the following algorithm
66 // - You need to request it from a top level domain
67 // - You need to have notification permission granted.
68 // - You need to not have push permission explicitly blocked.
69 // - If those two things are true it is granted without prompting.
70 // This is done to avoid double prompting for notifications and push.
71 void PushMessagingPermissionContext::DecidePermission(
72 content::WebContents
* web_contents
,
73 const PermissionRequestID
& id
,
74 const GURL
& requesting_origin
,
75 const GURL
& embedding_origin
,
77 const BrowserPermissionCallback
& callback
) {
78 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
79 #if defined(ENABLE_NOTIFICATIONS)
80 if (requesting_origin
!= embedding_origin
) {
81 NotifyPermissionSet(id
, requesting_origin
, embedding_origin
, callback
,
82 false /* persist */, CONTENT_SETTING_BLOCK
);
86 NotificationPermissionContext
* notification_context
=
87 NotificationPermissionContextFactory::GetForProfile(profile_
);
88 DCHECK(notification_context
);
90 notification_context
->RequestPermission(
91 web_contents
, id
, requesting_origin
, user_gesture
,
92 base::Bind(&PushMessagingPermissionContext::DecidePushPermission
,
93 weak_factory_ui_thread_
.GetWeakPtr(), id
, requesting_origin
,
94 embedding_origin
, callback
));
96 NotifyPermissionSet(id
, requesting_origin
, embedding_origin
, callback
,
97 false /* persist */, CONTENT_SETTING_BLOCK
);
101 bool PushMessagingPermissionContext::IsRestrictedToSecureOrigins() const {
105 void PushMessagingPermissionContext::DecidePushPermission(
106 const PermissionRequestID
& id
,
107 const GURL
& requesting_origin
,
108 const GURL
& embedding_origin
,
109 const BrowserPermissionCallback
& callback
,
110 ContentSetting notification_content_setting
) {
111 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
112 ContentSetting push_content_setting
=
113 HostContentSettingsMapFactory::GetForProfile(profile_
)
114 ->GetContentSettingAndMaybeUpdateLastUsage(
115 requesting_origin
, embedding_origin
, kPushSettingType
,
118 if (push_content_setting
== CONTENT_SETTING_BLOCK
) {
119 DVLOG(1) << "Push permission was explicitly blocked.";
120 PermissionContextUmaUtil::PermissionDenied(kPushSettingType
,
122 NotifyPermissionSet(id
, requesting_origin
, embedding_origin
, callback
,
123 true /* persist */, CONTENT_SETTING_BLOCK
);
127 if (notification_content_setting
!= CONTENT_SETTING_ALLOW
) {
128 DVLOG(1) << "Notification permission has not been granted.";
129 NotifyPermissionSet(id
, requesting_origin
, embedding_origin
, callback
,
130 false /* persist */, notification_content_setting
);
134 PermissionContextUmaUtil::PermissionGranted(kPushSettingType
,
136 NotifyPermissionSet(id
, requesting_origin
, embedding_origin
, callback
,
137 true /* persist */, CONTENT_SETTING_ALLOW
);