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

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: Rebase 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"
11 #include "chrome/browser/browser_process.h" 11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/notifications/desktop_notification_profile_util.h" 12 #include "chrome/browser/notifications/desktop_notification_profile_util.h"
13 #include "chrome/browser/notifications/notification.h" 13 #include "chrome/browser/notifications/notification.h"
14 #include "chrome/browser/notifications/notification_object_proxy.h" 14 #include "chrome/browser/notifications/notification_object_proxy.h"
15 #include "chrome/browser/notifications/notification_ui_manager.h" 15 #include "chrome/browser/notifications/notification_ui_manager.h"
16 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h" 17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/common/pref_names.h" 18 #include "chrome/common/pref_names.h"
19 #include "components/content_settings/core/common/permission_request_id.h" 19 #include "components/content_settings/core/common/permission_request_id.h"
20 #include "components/pref_registry/pref_registry_syncable.h" 20 #include "components/pref_registry/pref_registry_syncable.h"
21 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/desktop_notification_delegate.h" 22 #include "content/public/browser/desktop_notification_delegate.h"
23 #include "content/public/common/show_desktop_notification_params.h" 23 #include "content/public/common/show_desktop_notification_params.h"
24 #include "net/base/net_util.h"
24 #include "ui/base/webui/web_ui_util.h" 25 #include "ui/base/webui/web_ui_util.h"
25 #include "ui/message_center/notifier_settings.h" 26 #include "ui/message_center/notifier_settings.h"
26 27
27 #if defined(ENABLE_EXTENSIONS) 28 #if defined(ENABLE_EXTENSIONS)
28 #include "chrome/browser/extensions/api/notifications/notifications_api.h" 29 #include "chrome/browser/extensions/api/notifications/notifications_api.h"
29 #include "chrome/browser/extensions/extension_service.h" 30 #include "chrome/browser/extensions/extension_service.h"
30 #include "extensions/browser/event_router.h" 31 #include "extensions/browser/event_router.h"
31 #include "extensions/browser/extension_registry.h" 32 #include "extensions/browser/extension_registry.h"
32 #include "extensions/browser/extension_system.h" 33 #include "extensions/browser/extension_system.h"
33 #include "extensions/browser/extension_util.h" 34 #include "extensions/browser/extension_util.h"
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 171
171 void DesktopNotificationService::ShowDesktopNotification( 172 void DesktopNotificationService::ShowDesktopNotification(
172 const content::ShowDesktopNotificationHostMsgParams& params, 173 const content::ShowDesktopNotificationHostMsgParams& params,
173 int render_process_id, 174 int render_process_id,
174 scoped_ptr<content::DesktopNotificationDelegate> delegate, 175 scoped_ptr<content::DesktopNotificationDelegate> delegate,
175 base::Closure* cancel_callback) { 176 base::Closure* cancel_callback) {
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
177 const GURL& origin = params.origin; 178 const GURL& origin = params.origin;
178 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass()); 179 NotificationObjectProxy* proxy = new NotificationObjectProxy(delegate.Pass());
179 180
181 // TODO(dewittj): The origin of file:// URLs always appears to be the empty
182 // URL. We need a better way to display this than returning an empty string.
felt 2014/11/04 00:50:16 Could you have content/renderer/notification_provi
dewittj 2015/01/09 21:59:40 This appears to be a broader issue than just notif
felt 2015/01/17 01:40:15 Can you write a test to verify that it's impossibl
180 base::string16 display_source = DisplayNameForOriginInProcessId( 183 base::string16 display_source = DisplayNameForOriginInProcessId(
181 origin, render_process_id); 184 origin, render_process_id);
182 185
183 // TODO(peter): Icons for Web Notifications are currently always requested for 186 // TODO(peter): Icons for Web Notifications are currently always requested for
184 // 1x scale, whereas the displays on which they can be displayed can have a 187 // 1x scale, whereas the displays on which they can be displayed can have a
185 // different pixel density. Be smarter about this when the API gets updated 188 // different pixel density. Be smarter about this when the API gets updated
186 // with a way for developers to specify images of different resolutions. 189 // with a way for developers to specify images of different resolutions.
187 Notification notification(origin, params.title, params.body, 190 Notification notification(origin, params.title, params.body,
188 gfx::Image::CreateFrom1xBitmap(params.icon), 191 gfx::Image::CreateFrom1xBitmap(params.icon),
189 display_source, params.replace_id, proxy); 192 display_source, params.replace_id, proxy);
190 193
194 // Web notifications should display their origin
195 notification.set_context_message(display_source);
196
191 // The webkit notification doesn't timeout. 197 // The webkit notification doesn't timeout.
192 notification.set_never_timeout(true); 198 notification.set_never_timeout(true);
193 199
194 g_browser_process->notification_ui_manager()->Add(notification, profile_); 200 g_browser_process->notification_ui_manager()->Add(notification, profile_);
195 if (cancel_callback) 201 if (cancel_callback)
196 *cancel_callback = 202 *cancel_callback =
197 base::Bind(&CancelNotification, 203 base::Bind(&CancelNotification,
198 proxy->id(), 204 proxy->id(),
199 NotificationUIManager::GetProfileID(profile_)); 205 NotificationUIManager::GetProfileID(profile_));
200 206
201 DesktopNotificationProfileUtil::UsePermission(profile_, origin); 207 DesktopNotificationProfileUtil::UsePermission(profile_, origin);
202 } 208 }
203 209
204 base::string16 DesktopNotificationService::DisplayNameForOriginInProcessId( 210 base::string16 DesktopNotificationService::DisplayNameForOriginInProcessId(
205 const GURL& origin, int process_id) { 211 const GURL& origin,
212 int process_id) const {
206 #if defined(ENABLE_EXTENSIONS) 213 #if defined(ENABLE_EXTENSIONS)
207 // If the source is an extension, lookup the display name. 214 // If the source is an extension, lookup the display name.
208 if (origin.SchemeIs(extensions::kExtensionScheme)) { 215 if (origin.SchemeIs(extensions::kExtensionScheme)) {
209 extensions::InfoMap* extension_info_map = 216 base::string16 extension_display_name;
210 extensions::ExtensionSystem::Get(profile_)->info_map(); 217 if (ExtensionDisplayName(origin, process_id, &extension_display_name)) {
211 if (extension_info_map) { 218 return extension_display_name;
212 extensions::ExtensionSet extensions;
213 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin(
214 origin,
215 process_id,
216 extensions::APIPermission::kNotifications,
217 &extensions);
218 for (extensions::ExtensionSet::const_iterator iter = extensions.begin();
219 iter != extensions.end(); ++iter) {
220 NotifierId notifier_id(NotifierId::APPLICATION, (*iter)->id());
221 if (IsNotifierEnabled(notifier_id))
222 return base::UTF8ToUTF16((*iter)->name());
223 }
224 } 219 }
225 } 220 }
226 #endif 221 #endif
227 222
228 return base::UTF8ToUTF16(origin.host()); 223 std::string languages =
224 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages);
225
226 return OriginDisplayName(origin, languages);
227 }
228
229 #if defined(ENABLE_EXTENSIONS)
230 bool DesktopNotificationService::ExtensionDisplayName(
231 const GURL& origin,
232 int process_id,
233 base::string16* out) const {
234 DCHECK(origin.SchemeIs(extensions::kExtensionScheme));
235 DCHECK(out);
236
237 extensions::InfoMap* extension_info_map =
238 extensions::ExtensionSystem::Get(profile_)->info_map();
239 if (extension_info_map) {
240 extensions::ExtensionSet extensions;
241 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin(
242 origin,
243 process_id,
244 extensions::APIPermission::kNotifications,
245 &extensions);
246 for (auto& extension : extensions) {
247 NotifierId notifier_id(NotifierId::APPLICATION, extension->id());
248 if (IsNotifierEnabled(notifier_id)) {
249 *out = base::UTF8ToUTF16(extension->name());
250 return true;
251 }
252 }
253 }
254 return false;
255 }
256 #endif // defined(ENABLE_EXTENSIONS)
257
258 // static
259 base::string16 DesktopNotificationService::OriginDisplayName(
260 const GURL& origin,
261 const std::string& languages) {
262 if (origin.SchemeIsHTTPOrHTTPS()) {
263 base::string16 formatted_origin =
264 net::IDNToUnicode(origin.host(), languages);
265 if (origin.has_port()) {
266 formatted_origin.push_back(':');
267 formatted_origin.append(base::UTF8ToUTF16(origin.port()));
268 }
269 return formatted_origin;
270 }
271
272 // TODO(dewittj): Once file:// URLs are passed in to the origin
273 // GURL here, begin returning the path as the display name.
274 return net::FormatUrl(origin, languages);
229 } 275 }
230 276
231 bool DesktopNotificationService::IsNotifierEnabled( 277 bool DesktopNotificationService::IsNotifierEnabled(
232 const NotifierId& notifier_id) { 278 const NotifierId& notifier_id) const {
233 switch (notifier_id.type) { 279 switch (notifier_id.type) {
234 case NotifierId::APPLICATION: 280 case NotifierId::APPLICATION:
235 return disabled_extension_ids_.find(notifier_id.id) == 281 return disabled_extension_ids_.find(notifier_id.id) ==
236 disabled_extension_ids_.end(); 282 disabled_extension_ids_.end();
237 case NotifierId::WEB_PAGE: 283 case NotifierId::WEB_PAGE:
238 return DesktopNotificationProfileUtil::GetContentSetting( 284 return DesktopNotificationProfileUtil::GetContentSetting(
239 profile_, notifier_id.url) == CONTENT_SETTING_ALLOW; 285 profile_, notifier_id.url) == CONTENT_SETTING_ALLOW;
240 case NotifierId::SYSTEM_COMPONENT: 286 case NotifierId::SYSTEM_COMPONENT:
241 #if defined(OS_CHROMEOS) 287 #if defined(OS_CHROMEOS)
242 return disabled_system_component_ids_.find(notifier_id.id) == 288 return disabled_system_component_ids_.find(notifier_id.id) ==
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 // Tell the IO thread that this extension's permission for notifications 406 // Tell the IO thread that this extension's permission for notifications
361 // has changed. 407 // has changed.
362 extensions::InfoMap* extension_info_map = 408 extensions::InfoMap* extension_info_map =
363 extensions::ExtensionSystem::Get(profile_)->info_map(); 409 extensions::ExtensionSystem::Get(profile_)->info_map();
364 BrowserThread::PostTask( 410 BrowserThread::PostTask(
365 BrowserThread::IO, FROM_HERE, 411 BrowserThread::IO, FROM_HERE,
366 base::Bind(&extensions::InfoMap::SetNotificationsDisabled, 412 base::Bind(&extensions::InfoMap::SetNotificationsDisabled,
367 extension_info_map, notifier_id.id, !enabled)); 413 extension_info_map, notifier_id.id, !enabled));
368 #endif 414 #endif
369 } 415 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698