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

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

Issue 661643002: Adds a context message of the security origin for web notifications. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addresses peter's comments. Created 6 years, 1 month 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/desktop_notification_service.h" 5 #include "chrome/browser/notifications/desktop_notification_service.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/metrics/histogram.h" 8 #include "base/metrics/histogram.h"
9 #include "base/prefs/scoped_user_pref_update.h" 9 #include "base/prefs/scoped_user_pref_update.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 14 matching lines...) Expand all
25 #include "components/content_settings/core/common/permission_request_id.h" 25 #include "components/content_settings/core/common/permission_request_id.h"
26 #include "components/pref_registry/pref_registry_syncable.h" 26 #include "components/pref_registry/pref_registry_syncable.h"
27 #include "content/public/browser/browser_thread.h" 27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/desktop_notification_delegate.h" 28 #include "content/public/browser/desktop_notification_delegate.h"
29 #include "content/public/browser/notification_service.h" 29 #include "content/public/browser/notification_service.h"
30 #include "content/public/browser/render_frame_host.h" 30 #include "content/public/browser/render_frame_host.h"
31 #include "content/public/browser/render_process_host.h" 31 #include "content/public/browser/render_process_host.h"
32 #include "content/public/browser/render_view_host.h" 32 #include "content/public/browser/render_view_host.h"
33 #include "content/public/browser/web_contents.h" 33 #include "content/public/browser/web_contents.h"
34 #include "content/public/common/show_desktop_notification_params.h" 34 #include "content/public/common/show_desktop_notification_params.h"
35 #include "net/base/net_util.h"
35 #include "ui/base/webui/web_ui_util.h" 36 #include "ui/base/webui/web_ui_util.h"
36 #include "ui/message_center/notifier_settings.h" 37 #include "ui/message_center/notifier_settings.h"
37 38
38 #if defined(ENABLE_EXTENSIONS) 39 #if defined(ENABLE_EXTENSIONS)
39 #include "chrome/browser/extensions/api/notifications/notifications_api.h" 40 #include "chrome/browser/extensions/api/notifications/notifications_api.h"
40 #include "chrome/browser/extensions/extension_service.h" 41 #include "chrome/browser/extensions/extension_service.h"
41 #include "extensions/browser/event_router.h" 42 #include "extensions/browser/event_router.h"
42 #include "extensions/browser/extension_registry.h" 43 #include "extensions/browser/extension_registry.h"
43 #include "extensions/browser/extension_system.h" 44 #include "extensions/browser/extension_system.h"
44 #include "extensions/browser/extension_util.h" 45 #include "extensions/browser/extension_util.h"
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 185
185 void DesktopNotificationService::ShowDesktopNotification( 186 void DesktopNotificationService::ShowDesktopNotification(
186 const content::ShowDesktopNotificationHostMsgParams& params, 187 const content::ShowDesktopNotificationHostMsgParams& params,
187 content::RenderFrameHost* render_frame_host, 188 content::RenderFrameHost* render_frame_host,
188 scoped_ptr<content::DesktopNotificationDelegate> delegate, 189 scoped_ptr<content::DesktopNotificationDelegate> delegate,
189 base::Closure* cancel_callback) { 190 base::Closure* cancel_callback) {
190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
191 const GURL& origin = params.origin; 192 const GURL& origin = params.origin;
192 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); 193 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass());
193 194
194 base::string16 display_source = DisplayNameForOriginInProcessId( 195 int process_id = render_frame_host->GetProcess()->GetID();
Peter Beverloo 2014/10/29 16:25:19 nit: You probably have to rebase this, since ShowD
dewittj 2014/11/03 17:18:32 Done.
195 origin, render_frame_host->GetProcess()->GetID()); 196
197 // TODO(dewittj): The origin of file:// URLs always appears to be the empty
198 // URL. We need a better way to display this than returning an empty string.
199 base::string16 display_source =
200 DisplayNameForOriginInProcessId(origin, process_id);
196 201
197 // TODO(peter): Icons for Web Notifications are currently always requested for 202 // TODO(peter): Icons for Web Notifications are currently always requested for
198 // 1x scale, whereas the displays on which they can be displayed can have a 203 // 1x scale, whereas the displays on which they can be displayed can have a
199 // different pixel density. Be smarter about this when the API gets updated 204 // different pixel density. Be smarter about this when the API gets updated
200 // with a way for developers to specify images of different resolutions. 205 // with a way for developers to specify images of different resolutions.
201 Notification notification(origin, params.title, params.body, 206 Notification notification(origin, params.title, params.body,
202 gfx::Image::CreateFrom1xBitmap(params.icon), 207 gfx::Image::CreateFrom1xBitmap(params.icon),
203 display_source, params.replace_id, proxy); 208 display_source, params.replace_id, proxy);
204 209
210 // Web notifications should display their origin
211 notification.set_context_message(display_source);
212
205 // The webkit notification doesn't timeout. 213 // The webkit notification doesn't timeout.
206 notification.set_never_timeout(true); 214 notification.set_never_timeout(true);
207 215
208 g_browser_process->notification_ui_manager()->Add(notification, profile_); 216 g_browser_process->notification_ui_manager()->Add(notification, profile_);
209 if (cancel_callback) 217 if (cancel_callback)
210 *cancel_callback = 218 *cancel_callback =
211 base::Bind(&CancelNotification, 219 base::Bind(&CancelNotification,
212 proxy->id(), 220 proxy->id(),
213 NotificationUIManager::GetProfileID(profile_)); 221 NotificationUIManager::GetProfileID(profile_));
214 222
215 DesktopNotificationProfileUtil::UsePermission(profile_, origin); 223 DesktopNotificationProfileUtil::UsePermission(profile_, origin);
216 } 224 }
217 225
218 base::string16 DesktopNotificationService::DisplayNameForOriginInProcessId( 226 base::string16 DesktopNotificationService::DisplayNameForOriginInProcessId(
219 const GURL& origin, int process_id) { 227 const GURL& origin,
228 int process_id) const {
220 #if defined(ENABLE_EXTENSIONS) 229 #if defined(ENABLE_EXTENSIONS)
221 // If the source is an extension, lookup the display name. 230 // If the source is an extension, lookup the display name.
222 if (origin.SchemeIs(extensions::kExtensionScheme)) { 231 if (origin.SchemeIs(extensions::kExtensionScheme)) {
223 extensions::InfoMap* extension_info_map = 232 base::string16 extension_display_name;
224 extensions::ExtensionSystem::Get(profile_)->info_map(); 233 if (ExtensionDisplayName(origin, process_id, &extension_display_name)) {
225 if (extension_info_map) { 234 return extension_display_name;
226 extensions::ExtensionSet extensions;
227 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin(
228 origin,
229 process_id,
230 extensions::APIPermission::kNotifications,
231 &extensions);
232 for (extensions::ExtensionSet::const_iterator iter = extensions.begin();
233 iter != extensions.end(); ++iter) {
234 NotifierId notifier_id(NotifierId::APPLICATION, (*iter)->id());
235 if (IsNotifierEnabled(notifier_id))
236 return base::UTF8ToUTF16((*iter)->name());
237 }
238 } 235 }
239 } 236 }
240 #endif 237 #endif
241 238
242 return base::UTF8ToUTF16(origin.host()); 239 std::string languages =
240 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages);
241
242 return OriginDisplayName(origin, languages);
243 }
244
245 #if defined(ENABLE_EXTENSIONS)
246 bool DesktopNotificationService::ExtensionDisplayName(
247 const GURL& origin,
248 int process_id,
249 base::string16* out) const {
250 DCHECK(origin.SchemeIs(extensions::kExtensionScheme));
251 DCHECK(out);
252
253 extensions::InfoMap* extension_info_map =
254 extensions::ExtensionSystem::Get(profile_)->info_map();
255 if (extension_info_map) {
256 extensions::ExtensionSet extensions;
257 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin(
258 origin,
259 process_id,
260 extensions::APIPermission::kNotifications,
261 &extensions);
262 for (auto& extension : extensions) {
263 NotifierId notifier_id(NotifierId::APPLICATION, extension->id());
264 if (IsNotifierEnabled(notifier_id)) {
265 *out = base::UTF8ToUTF16(extension->name());
266 return true;
267 }
268 }
269 }
270 return false;
271 }
272 #endif // defined(ENABLE_EXTENSIONS)
273
274 // static
275 base::string16 DesktopNotificationService::OriginDisplayName(
276 const GURL& origin,
277 std::string languages) {
278 if (origin.SchemeIsHTTPOrHTTPS()) {
279 base::string16 formatted_origin =
280 net::IDNToUnicode(origin.host(), languages);
281 if (origin.has_port()) {
282 formatted_origin.push_back(':');
283 formatted_origin.append(base::UTF8ToUTF16(origin.port()));
284 }
285 return formatted_origin;
286 }
287
288 // TODO(dewittj): Once file:// URLs are passed in to the origin
289 // GURL here, begin returning the path as the display name.
290 return net::FormatUrl(origin, languages);
felt 2014/10/30 06:40:14 is it possible for other schemes to send notificat
dewittj 2014/11/03 17:18:32 I'm not aware of anything preventing ftp:// origin
243 } 291 }
244 292
245 bool DesktopNotificationService::IsNotifierEnabled( 293 bool DesktopNotificationService::IsNotifierEnabled(
246 const NotifierId& notifier_id) { 294 const NotifierId& notifier_id) const {
247 switch (notifier_id.type) { 295 switch (notifier_id.type) {
248 case NotifierId::APPLICATION: 296 case NotifierId::APPLICATION:
249 return disabled_extension_ids_.find(notifier_id.id) == 297 return disabled_extension_ids_.find(notifier_id.id) ==
250 disabled_extension_ids_.end(); 298 disabled_extension_ids_.end();
251 case NotifierId::WEB_PAGE: 299 case NotifierId::WEB_PAGE:
252 return DesktopNotificationProfileUtil::GetContentSetting( 300 return DesktopNotificationProfileUtil::GetContentSetting(
253 profile_, notifier_id.url) == CONTENT_SETTING_ALLOW; 301 profile_, notifier_id.url) == CONTENT_SETTING_ALLOW;
254 case NotifierId::SYSTEM_COMPONENT: 302 case NotifierId::SYSTEM_COMPONENT:
255 #if defined(OS_CHROMEOS) 303 #if defined(OS_CHROMEOS)
256 return disabled_system_component_ids_.find(notifier_id.id) == 304 return disabled_system_component_ids_.find(notifier_id.id) ==
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 // Tell the IO thread that this extension's permission for notifications 422 // Tell the IO thread that this extension's permission for notifications
375 // has changed. 423 // has changed.
376 extensions::InfoMap* extension_info_map = 424 extensions::InfoMap* extension_info_map =
377 extensions::ExtensionSystem::Get(profile_)->info_map(); 425 extensions::ExtensionSystem::Get(profile_)->info_map();
378 BrowserThread::PostTask( 426 BrowserThread::PostTask(
379 BrowserThread::IO, FROM_HERE, 427 BrowserThread::IO, FROM_HERE,
380 base::Bind(&extensions::InfoMap::SetNotificationsDisabled, 428 base::Bind(&extensions::InfoMap::SetNotificationsDisabled,
381 extension_info_map, notifier_id.id, !enabled)); 429 extension_info_map, notifier_id.id, !enabled));
382 #endif 430 #endif
383 } 431 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698