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

Side by Side Diff: chrome/browser/notifications/platform_notification_service_impl.cc

Issue 1207363002: Simplify permission-related code for Web Notifications. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/notifications/platform_notification_service_impl.h" 5 #include "chrome/browser/notifications/platform_notification_service_impl.h"
6 6
7 #include "base/metrics/histogram_macros.h" 7 #include "base/metrics/histogram_macros.h"
8 #include "base/prefs/pref_service.h" 8 #include "base/prefs/pref_service.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/browser_process.h" 10 #include "chrome/browser/browser_process.h"
(...skipping 10 matching lines...) Expand all
21 #include "content/public/browser/desktop_notification_delegate.h" 21 #include "content/public/browser/desktop_notification_delegate.h"
22 #include "content/public/browser/notification_event_dispatcher.h" 22 #include "content/public/browser/notification_event_dispatcher.h"
23 #include "content/public/browser/platform_notification_context.h" 23 #include "content/public/browser/platform_notification_context.h"
24 #include "content/public/browser/storage_partition.h" 24 #include "content/public/browser/storage_partition.h"
25 #include "content/public/common/platform_notification_data.h" 25 #include "content/public/common/platform_notification_data.h"
26 #include "net/base/net_util.h" 26 #include "net/base/net_util.h"
27 #include "ui/message_center/notifier_settings.h" 27 #include "ui/message_center/notifier_settings.h"
28 #include "url/url_constants.h" 28 #include "url/url_constants.h"
29 29
30 #if defined(ENABLE_EXTENSIONS) 30 #if defined(ENABLE_EXTENSIONS)
31 #include "chrome/browser/notifications/desktop_notification_service.h"
32 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
33 #include "extensions/browser/extension_registry.h" 31 #include "extensions/browser/extension_registry.h"
34 #include "extensions/browser/extension_system.h"
35 #include "extensions/browser/info_map.h"
36 #include "extensions/common/constants.h" 32 #include "extensions/common/constants.h"
37 #include "extensions/common/extension_set.h"
38 #include "extensions/common/permissions/api_permission.h"
39 #include "extensions/common/permissions/permissions_data.h"
40 #endif 33 #endif
41 34
42 #if defined(OS_ANDROID) 35 #if defined(OS_ANDROID)
43 #include "base/strings/string_number_conversions.h" 36 #include "base/strings/string_number_conversions.h"
44 #endif 37 #endif
45 38
46 using content::BrowserContext; 39 using content::BrowserContext;
47 using content::BrowserThread; 40 using content::BrowserThread;
48 using content::PlatformNotificationContext; 41 using content::PlatformNotificationContext;
49 using message_center::NotifierId; 42 using message_center::NotifierId;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 blink::WebNotificationPermission 112 blink::WebNotificationPermission
120 PlatformNotificationServiceImpl::CheckPermissionOnUIThread( 113 PlatformNotificationServiceImpl::CheckPermissionOnUIThread(
121 BrowserContext* browser_context, 114 BrowserContext* browser_context,
122 const GURL& origin, 115 const GURL& origin,
123 int render_process_id) { 116 int render_process_id) {
124 DCHECK_CURRENTLY_ON(BrowserThread::UI); 117 DCHECK_CURRENTLY_ON(BrowserThread::UI);
125 118
126 Profile* profile = Profile::FromBrowserContext(browser_context); 119 Profile* profile = Profile::FromBrowserContext(browser_context);
127 DCHECK(profile); 120 DCHECK(profile);
128 121
129 #if defined(ENABLE_EXTENSIONS)
130 // Extensions support an API permission named "notification". This will grant
131 // not only grant permission for using the Chrome App extension API, but also
132 // for the Web Notification API.
133 extensions::ExtensionRegistry* registry =
134 extensions::ExtensionRegistry::Get(browser_context);
135 extensions::ProcessMap* process_map =
136 extensions::ProcessMap::Get(browser_context);
137 extensions::ExtensionSet extensions;
138
139 DesktopNotificationService* desktop_notification_service =
140 DesktopNotificationServiceFactory::GetForProfile(profile);
141 DCHECK(desktop_notification_service);
142
143 // If |origin| is an enabled extension, only select that one. Otherwise select
144 // all extensions whose web content matches |origin|.
145 if (origin.SchemeIs(extensions::kExtensionScheme)) {
146 const extensions::Extension* extension = registry->GetExtensionById(
147 origin.host(), extensions::ExtensionRegistry::ENABLED);
148 if (extension)
149 extensions.Insert(extension);
150 } else {
151 for (const auto& extension : registry->enabled_extensions()) {
152 if (extension->web_extent().MatchesSecurityOrigin(origin))
153 extensions.Insert(extension);
154 }
155 }
156
157 // Check if any of the selected extensions have the "notification" API
158 // permission, are active in |render_process_id| and has not been manually
159 // disabled by the user. If all of that is true, grant permission.
160 for (const auto& extension : extensions) {
161 if (!extension->permissions_data()->HasAPIPermission(
162 extensions::APIPermission::kNotifications))
163 continue;
164
165 if (!process_map->Contains(extension->id(), render_process_id))
166 continue;
167
168 NotifierId notifier_id(NotifierId::APPLICATION, extension->id());
169 if (!desktop_notification_service->IsNotifierEnabled(notifier_id))
170 continue;
171
172 return blink::WebNotificationPermissionAllowed;
173 }
174 #endif
175
176 ContentSetting setting = 122 ContentSetting setting =
177 DesktopNotificationProfileUtil::GetContentSetting(profile, origin); 123 DesktopNotificationProfileUtil::GetContentSetting(profile, origin);
178 124
179 if (setting == CONTENT_SETTING_ALLOW) 125 if (setting == CONTENT_SETTING_ALLOW)
180 return blink::WebNotificationPermissionAllowed; 126 return blink::WebNotificationPermissionAllowed;
181 if (setting == CONTENT_SETTING_BLOCK) 127 if (setting == CONTENT_SETTING_BLOCK)
182 return blink::WebNotificationPermissionDenied; 128 return blink::WebNotificationPermissionDenied;
183 129
184 return blink::WebNotificationPermissionDefault; 130 return blink::WebNotificationPermissionDefault;
185 } 131 }
186 132
187 blink::WebNotificationPermission 133 blink::WebNotificationPermission
188 PlatformNotificationServiceImpl::CheckPermissionOnIOThread( 134 PlatformNotificationServiceImpl::CheckPermissionOnIOThread(
189 content::ResourceContext* resource_context, 135 content::ResourceContext* resource_context,
190 const GURL& origin, 136 const GURL& origin,
191 int render_process_id) { 137 int render_process_id) {
192 DCHECK_CURRENTLY_ON(BrowserThread::IO); 138 DCHECK_CURRENTLY_ON(BrowserThread::IO);
193 139
194 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context); 140 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
195 #if defined(ENABLE_EXTENSIONS)
196 extensions::InfoMap* extension_info_map = io_data->GetExtensionInfoMap();
197 141
198 // We want to see if there is an extension that hasn't been manually disabled
199 // that has the notifications permission and applies to this security origin.
200 // First, get the list of extensions with permission for the origin.
201 extensions::ExtensionSet extensions;
202 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin(
203 origin,
204 render_process_id,
205 extensions::APIPermission::kNotifications,
206 &extensions);
207 for (const auto& extension : extensions) {
208 if (!extension_info_map->AreNotificationsDisabled(extension->id()))
209 return blink::WebNotificationPermissionAllowed;
210 }
211 #endif
212
213 // No enabled extensions exist, so check the normal host content settings.
214 HostContentSettingsMap* host_content_settings_map = 142 HostContentSettingsMap* host_content_settings_map =
215 io_data->GetHostContentSettingsMap(); 143 io_data->GetHostContentSettingsMap();
216 ContentSetting setting = host_content_settings_map->GetContentSetting( 144 ContentSetting setting = host_content_settings_map->GetContentSetting(
217 origin, 145 origin,
218 origin, 146 origin,
219 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 147 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
220 content_settings::ResourceIdentifier()); 148 content_settings::ResourceIdentifier());
221 149
222 if (setting == CONTENT_SETTING_ALLOW) 150 if (setting == CONTENT_SETTING_ALLOW)
223 return blink::WebNotificationPermissionAllowed; 151 return blink::WebNotificationPermissionAllowed;
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 formatted_origin.push_back(':'); 340 formatted_origin.push_back(':');
413 formatted_origin.append(base::UTF8ToUTF16(origin.port())); 341 formatted_origin.append(base::UTF8ToUTF16(origin.port()));
414 } 342 }
415 return formatted_origin; 343 return formatted_origin;
416 } 344 }
417 345
418 // TODO(dewittj): Once file:// URLs are passed in to the origin 346 // TODO(dewittj): Once file:// URLs are passed in to the origin
419 // GURL here, begin returning the path as the display name. 347 // GURL here, begin returning the path as the display name.
420 return net::FormatUrl(origin, languages); 348 return net::FormatUrl(origin, languages);
421 } 349 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698