Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / push_messaging / push_messaging_permission_context.cc
blob885589e1934a05d35bc4676d92265896a813dc54
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/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;
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 DesktopNotificationService* notification_service =
41 DesktopNotificationServiceFactory::GetForProfile(profile_);
42 DCHECK(notification_service);
44 ContentSetting notifications_permission =
45 notification_service->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 void PushMessagingPermissionContext::CancelPermissionRequest(
65 content::WebContents* web_contents, const PermissionRequestID& id) {
66 // TODO(peter): consider implementing this method.
67 NOTIMPLEMENTED() << "CancelPermission not implemented for push messaging";
70 // Unlike other permissions, push is decided by the following algorithm
71 // - You need to request it from a top level domain
72 // - You need to have notification permission granted.
73 // - You need to not have push permission explicitly blocked.
74 // - If those two things are true it is granted without prompting.
75 // This is done to avoid double prompting for notifications and push.
76 void PushMessagingPermissionContext::DecidePermission(
77 content::WebContents* web_contents,
78 const PermissionRequestID& id,
79 const GURL& requesting_origin,
80 const GURL& embedding_origin,
81 bool user_gesture,
82 const BrowserPermissionCallback& callback) {
83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
84 #if defined(ENABLE_NOTIFICATIONS)
85 if (requesting_origin != embedding_origin) {
86 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
87 false /* persist */, CONTENT_SETTING_BLOCK);
88 return;
90 DesktopNotificationService* notification_service =
91 DesktopNotificationServiceFactory::GetForProfile(profile_);
92 DCHECK(notification_service);
94 notification_service->RequestPermission(
95 web_contents, id, requesting_origin, user_gesture,
96 base::Bind(&PushMessagingPermissionContext::DecidePushPermission,
97 weak_factory_ui_thread_.GetWeakPtr(), id, requesting_origin,
98 embedding_origin, callback));
99 #else
100 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
101 false /* persist */, CONTENT_SETTING_BLOCK);
102 #endif
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 profile_->GetHostContentSettingsMap()
114 ->GetContentSettingAndMaybeUpdateLastUsage(
115 requesting_origin, embedding_origin, kPushSettingType,
116 std::string());
118 if (push_content_setting == CONTENT_SETTING_BLOCK) {
119 DVLOG(1) << "Push permission was explicitly blocked.";
120 PermissionContextUmaUtil::PermissionDenied(kPushSettingType,
121 requesting_origin);
122 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
123 true /* persist */, CONTENT_SETTING_BLOCK);
124 return;
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);
131 return;
134 PermissionContextUmaUtil::PermissionGranted(kPushSettingType,
135 requesting_origin);
136 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
137 true /* persist */, CONTENT_SETTING_ALLOW);