Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(233)

Side by Side Diff: chrome/browser/push_messaging/push_messaging_permission_context.cc

Issue 2149883002: Use the same codepath for NOTIFICATIONS and PUSH_MESSAGING permissions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/push_messaging/push_messaging_permission_context.h"
6
7 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
8 #include "chrome/browser/permissions/permission_manager.h"
9 #include "chrome/browser/permissions/permission_request_id.h"
10 #include "chrome/browser/permissions/permission_uma_util.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "components/content_settings/core/browser/host_content_settings_map.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/permission_type.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_delegate.h"
17 #include "content/public/common/origin_util.h"
18
19 PushMessagingPermissionContext::PushMessagingPermissionContext(Profile* profile)
20 : PermissionContextBase(profile,
21 content::PermissionType::PUSH_MESSAGING,
22 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING),
23 profile_(profile),
24 weak_factory_ui_thread_(this) {}
25
26 PushMessagingPermissionContext::~PushMessagingPermissionContext() {}
27
28 ContentSetting PushMessagingPermissionContext::GetPermissionStatus(
29 const GURL& requesting_origin,
30 const GURL& embedding_origin) const {
31 // It's possible for this to return CONTENT_SETTING_BLOCK in cases where
32 // HostContentSettingsMap::GetContentSetting returns CONTENT_SETTING_ALLOW.
33 // TODO(johnme): This is likely to break assumptions made elsewhere, so we
34 // should try to remove this quirk.
35 #if defined(ENABLE_NOTIFICATIONS)
36 if (requesting_origin != embedding_origin)
37 return CONTENT_SETTING_BLOCK;
38
39 ContentSetting push_content_setting =
40 PermissionContextBase::GetPermissionStatus(requesting_origin,
41 embedding_origin);
42
43 blink::mojom::PermissionStatus notifications_permission =
44 PermissionManager::Get(profile_)->GetPermissionStatus(
45 content::PermissionType::NOTIFICATIONS, requesting_origin,
46 embedding_origin);
47
48 if (notifications_permission == blink::mojom::PermissionStatus::DENIED ||
49 push_content_setting == CONTENT_SETTING_BLOCK) {
50 return CONTENT_SETTING_BLOCK;
51 }
52 if (notifications_permission == blink::mojom::PermissionStatus::ASK)
53 return CONTENT_SETTING_ASK;
54
55 DCHECK(push_content_setting == CONTENT_SETTING_ALLOW ||
56 push_content_setting == CONTENT_SETTING_ASK);
57
58 // If the notifications permission has already been granted,
59 // and the push permission isn't explicitly blocked, then grant
60 // allow permission.
61 return CONTENT_SETTING_ALLOW;
62 #else
63 return CONTENT_SETTING_BLOCK;
64 #endif
65 }
66
67 // Unlike other permissions, push is decided by the following algorithm
68 // - You need to request it from a top level domain
69 // - You need to have notification permission granted.
70 // - You need to not have push permission explicitly blocked.
71 // - If those 3 things are true it is granted without prompting.
72 // This is done to avoid double prompting for notifications and push.
73 void PushMessagingPermissionContext::DecidePermission(
74 content::WebContents* web_contents,
75 const PermissionRequestID& id,
76 const GURL& requesting_origin,
77 const GURL& embedding_origin,
78 const BrowserPermissionCallback& callback) {
79 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
80 #if defined(ENABLE_NOTIFICATIONS)
81 if (requesting_origin != embedding_origin) {
82 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
83 false /* persist */, CONTENT_SETTING_BLOCK);
84 return;
85 }
86
87 PermissionManager::Get(profile_)->RequestPermission(
88 content::PermissionType::NOTIFICATIONS, web_contents->GetMainFrame(),
89 requesting_origin,
90 base::Bind(&PushMessagingPermissionContext::DecidePushPermission,
91 weak_factory_ui_thread_.GetWeakPtr(), id, requesting_origin,
92 embedding_origin, callback));
93 #else
94 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
95 false /* persist */, CONTENT_SETTING_BLOCK);
96 #endif
97 }
98
99 bool PushMessagingPermissionContext::IsRestrictedToSecureOrigins() const {
100 return true;
101 }
102
103 void PushMessagingPermissionContext::DecidePushPermission(
104 const PermissionRequestID& id,
105 const GURL& requesting_origin,
106 const GURL& embedding_origin,
107 const BrowserPermissionCallback& callback,
108 blink::mojom::PermissionStatus notification_status) {
109 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
110 DCHECK_NE(notification_status, blink::mojom::PermissionStatus::ASK);
111
112 ContentSetting push_content_setting =
113 HostContentSettingsMapFactory::GetForProfile(profile_)
114 ->GetContentSettingAndMaybeUpdateLastUsage(
115 requesting_origin, embedding_origin, content_settings_type(),
116 std::string());
117
118 if (push_content_setting == CONTENT_SETTING_BLOCK) {
119 DVLOG(1) << "Push permission was explicitly blocked.";
120 PermissionUmaUtil::PermissionDenied(permission_type(), requesting_origin);
121 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
122 true /* persist */, CONTENT_SETTING_BLOCK);
123 return;
124 }
125
126 if (notification_status == blink::mojom::PermissionStatus::DENIED) {
127 DVLOG(1) << "Notification permission has not been granted.";
128 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
129 false /* persist */, CONTENT_SETTING_BLOCK);
130 return;
131 }
132
133 PermissionUmaUtil::PermissionGranted(permission_type(), requesting_origin);
134 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
135 true /* persist */, CONTENT_SETTING_ALLOW);
136 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698