Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / push_messaging / push_messaging_permission_context.cc
blob4c9d7b020cc7c076e623af3051e56bfb5c527455
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/notifications/notification_permission_context.h"
8 #include "chrome/browser/notifications/notification_permission_context_factory.h"
9 #include "chrome/browser/permissions/permission_context_uma_util.h"
10 #include "chrome/browser/permissions/permission_request_id.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/web_contents.h"
15 #include "content/public/browser/web_contents_delegate.h"
17 const ContentSettingsType kPushSettingType =
18 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING;
20 PushMessagingPermissionContext::PushMessagingPermissionContext(Profile* profile)
21 : PermissionContextBase(profile, CONTENT_SETTINGS_TYPE_PUSH_MESSAGING),
22 profile_(profile),
23 weak_factory_ui_thread_(this) {
26 PushMessagingPermissionContext::~PushMessagingPermissionContext() {
29 ContentSetting PushMessagingPermissionContext::GetPermissionStatus(
30 const GURL& requesting_origin,
31 const GURL& embedding_origin) const {
32 #if defined(ENABLE_NOTIFICATIONS)
33 if (requesting_origin != embedding_origin)
34 return CONTENT_SETTING_BLOCK;
36 ContentSetting push_content_setting =
37 profile_->GetHostContentSettingsMap()->GetContentSetting(
38 requesting_origin, embedding_origin, kPushSettingType, std::string());
40 NotificationPermissionContext* notification_context =
41 NotificationPermissionContextFactory::GetForProfile(profile_);
42 DCHECK(notification_context);
44 ContentSetting notifications_permission =
45 notification_context->GetPermissionStatus(requesting_origin,
46 embedding_origin);
48 if (notifications_permission == CONTENT_SETTING_BLOCK ||
49 push_content_setting == CONTENT_SETTING_BLOCK) {
50 return CONTENT_SETTING_BLOCK;
52 if (notifications_permission == CONTENT_SETTING_ASK ||
53 push_content_setting == CONTENT_SETTING_ASK) {
54 return CONTENT_SETTING_ASK;
56 DCHECK_EQ(CONTENT_SETTING_ALLOW, notifications_permission);
57 DCHECK_EQ(CONTENT_SETTING_ALLOW, push_content_setting);
58 return CONTENT_SETTING_ALLOW;
59 #else
60 return CONTENT_SETTING_BLOCK;
61 #endif
64 // Unlike other permissions, push is decided by the following algorithm
65 // - You need to request it from a top level domain
66 // - You need to have notification permission granted.
67 // - You need to not have push permission explicitly blocked.
68 // - If those two things are true it is granted without prompting.
69 // This is done to avoid double prompting for notifications and push.
70 void PushMessagingPermissionContext::DecidePermission(
71 content::WebContents* web_contents,
72 const PermissionRequestID& id,
73 const GURL& requesting_origin,
74 const GURL& embedding_origin,
75 bool user_gesture,
76 const BrowserPermissionCallback& callback) {
77 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
78 #if defined(ENABLE_NOTIFICATIONS)
79 if (requesting_origin != embedding_origin) {
80 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
81 false /* persist */, CONTENT_SETTING_BLOCK);
82 return;
85 NotificationPermissionContext* notification_context =
86 NotificationPermissionContextFactory::GetForProfile(profile_);
87 DCHECK(notification_context);
89 notification_context->RequestPermission(
90 web_contents, id, requesting_origin, user_gesture,
91 base::Bind(&PushMessagingPermissionContext::DecidePushPermission,
92 weak_factory_ui_thread_.GetWeakPtr(), id, requesting_origin,
93 embedding_origin, callback));
94 #else
95 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
96 false /* persist */, CONTENT_SETTING_BLOCK);
97 #endif
100 bool PushMessagingPermissionContext::IsRestrictedToSecureOrigins() const {
101 return true;
104 void PushMessagingPermissionContext::DecidePushPermission(
105 const PermissionRequestID& id,
106 const GURL& requesting_origin,
107 const GURL& embedding_origin,
108 const BrowserPermissionCallback& callback,
109 ContentSetting notification_content_setting) {
110 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
111 ContentSetting push_content_setting =
112 profile_->GetHostContentSettingsMap()
113 ->GetContentSettingAndMaybeUpdateLastUsage(
114 requesting_origin, embedding_origin, kPushSettingType,
115 std::string());
117 if (push_content_setting == CONTENT_SETTING_BLOCK) {
118 DVLOG(1) << "Push permission was explicitly blocked.";
119 PermissionContextUmaUtil::PermissionDenied(kPushSettingType,
120 requesting_origin);
121 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
122 true /* persist */, CONTENT_SETTING_BLOCK);
123 return;
126 if (notification_content_setting != CONTENT_SETTING_ALLOW) {
127 DVLOG(1) << "Notification permission has not been granted.";
128 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
129 false /* persist */, notification_content_setting);
130 return;
133 PermissionContextUmaUtil::PermissionGranted(kPushSettingType,
134 requesting_origin);
135 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
136 true /* persist */, CONTENT_SETTING_ALLOW);