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

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

Issue 1620203004: Notification action icons prototype. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make it work on Android and clean up. Created 4 years, 11 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 <utility> 7 #include <utility>
8 #include <vector>
8 9
9 #include "base/command_line.h" 10 #include "base/command_line.h"
10 #include "base/metrics/histogram_macros.h" 11 #include "base/metrics/histogram_macros.h"
11 #include "base/metrics/user_metrics_action.h" 12 #include "base/metrics/user_metrics_action.h"
12 #include "base/prefs/pref_service.h" 13 #include "base/prefs/pref_service.h"
13 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
15 #include "build/build_config.h" 16 #include "build/build_config.h"
16 #include "chrome/browser/browser_process.h" 17 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 18 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
(...skipping 13 matching lines...) Expand all
31 #include "components/content_settings/core/browser/host_content_settings_map.h" 32 #include "components/content_settings/core/browser/host_content_settings_map.h"
32 #include "components/content_settings/core/common/content_settings.h" 33 #include "components/content_settings/core/common/content_settings.h"
33 #include "components/content_settings/core/common/content_settings_types.h" 34 #include "components/content_settings/core/common/content_settings_types.h"
34 #include "components/url_formatter/url_formatter.h" 35 #include "components/url_formatter/url_formatter.h"
35 #include "content/public/browser/browser_thread.h" 36 #include "content/public/browser/browser_thread.h"
36 #include "content/public/browser/desktop_notification_delegate.h" 37 #include "content/public/browser/desktop_notification_delegate.h"
37 #include "content/public/browser/notification_event_dispatcher.h" 38 #include "content/public/browser/notification_event_dispatcher.h"
38 #include "content/public/browser/platform_notification_context.h" 39 #include "content/public/browser/platform_notification_context.h"
39 #include "content/public/browser/storage_partition.h" 40 #include "content/public/browser/storage_partition.h"
40 #include "content/public/browser/user_metrics.h" 41 #include "content/public/browser/user_metrics.h"
42 #include "content/public/common/notification_resources.h"
41 #include "content/public/common/platform_notification_data.h" 43 #include "content/public/common/platform_notification_data.h"
42 #include "ui/base/l10n/l10n_util.h" 44 #include "ui/base/l10n/l10n_util.h"
43 #include "ui/base/resource/resource_bundle.h" 45 #include "ui/base/resource/resource_bundle.h"
44 #include "ui/message_center/notification_types.h" 46 #include "ui/message_center/notification_types.h"
45 #include "ui/message_center/notifier_settings.h" 47 #include "ui/message_center/notifier_settings.h"
46 #include "ui/resources/grit/ui_resources.h" 48 #include "ui/resources/grit/ui_resources.h"
47 #include "url/url_constants.h" 49 #include "url/url_constants.h"
48 50
49 #if defined(ENABLE_EXTENSIONS) 51 #if defined(ENABLE_EXTENSIONS)
50 #include "chrome/browser/notifications/notifier_state_tracker.h" 52 #include "chrome/browser/notifications/notifier_state_tracker.h"
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 return blink::WebNotificationPermissionAllowed; 333 return blink::WebNotificationPermissionAllowed;
332 if (setting == CONTENT_SETTING_BLOCK) 334 if (setting == CONTENT_SETTING_BLOCK)
333 return blink::WebNotificationPermissionDenied; 335 return blink::WebNotificationPermissionDenied;
334 336
335 return blink::WebNotificationPermissionDefault; 337 return blink::WebNotificationPermissionDefault;
336 } 338 }
337 339
338 void PlatformNotificationServiceImpl::DisplayNotification( 340 void PlatformNotificationServiceImpl::DisplayNotification(
339 BrowserContext* browser_context, 341 BrowserContext* browser_context,
340 const GURL& origin, 342 const GURL& origin,
341 const SkBitmap& icon,
342 const content::PlatformNotificationData& notification_data, 343 const content::PlatformNotificationData& notification_data,
344 const content::NotificationResources& notification_resources,
343 scoped_ptr<content::DesktopNotificationDelegate> delegate, 345 scoped_ptr<content::DesktopNotificationDelegate> delegate,
344 base::Closure* cancel_callback) { 346 base::Closure* cancel_callback) {
345 DCHECK_CURRENTLY_ON(BrowserThread::UI); 347 DCHECK_CURRENTLY_ON(BrowserThread::UI);
346 348
347 Profile* profile = Profile::FromBrowserContext(browser_context); 349 Profile* profile = Profile::FromBrowserContext(browser_context);
348 DCHECK(profile); 350 DCHECK(profile);
349 DCHECK_EQ(0u, notification_data.actions.size()); 351 DCHECK_EQ(0u, notification_data.actions.size());
352 DCHECK_EQ(0u, notification_resources.action_icons.size());
350 353
351 NotificationObjectProxy* proxy = 354 NotificationObjectProxy* proxy =
352 new NotificationObjectProxy(browser_context, std::move(delegate)); 355 new NotificationObjectProxy(browser_context, std::move(delegate));
353 Notification notification = CreateNotificationFromData( 356 Notification notification = CreateNotificationFromData(
354 profile, origin, icon, notification_data, proxy); 357 profile, origin, notification_data, notification_resources, proxy);
355 358
356 GetNotificationUIManager()->Add(notification, profile); 359 GetNotificationUIManager()->Add(notification, profile);
357 if (cancel_callback) 360 if (cancel_callback)
358 *cancel_callback = 361 *cancel_callback =
359 base::Bind(&CancelNotification, 362 base::Bind(&CancelNotification,
360 notification.delegate_id(), 363 notification.delegate_id(),
361 NotificationUIManager::GetProfileID(profile)); 364 NotificationUIManager::GetProfileID(profile));
362 365
363 HostContentSettingsMapFactory::GetForProfile(profile)->UpdateLastUsage( 366 HostContentSettingsMapFactory::GetForProfile(profile)->UpdateLastUsage(
364 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); 367 origin, origin, CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
365 } 368 }
366 369
367 void PlatformNotificationServiceImpl::DisplayPersistentNotification( 370 void PlatformNotificationServiceImpl::DisplayPersistentNotification(
368 BrowserContext* browser_context, 371 BrowserContext* browser_context,
369 int64_t persistent_notification_id, 372 int64_t persistent_notification_id,
370 const GURL& origin, 373 const GURL& origin,
371 const SkBitmap& icon, 374 const content::PlatformNotificationData& notification_data,
372 const content::PlatformNotificationData& notification_data) { 375 const content::NotificationResources& notification_resources) {
373 DCHECK_CURRENTLY_ON(BrowserThread::UI); 376 DCHECK_CURRENTLY_ON(BrowserThread::UI);
374 377
375 Profile* profile = Profile::FromBrowserContext(browser_context); 378 Profile* profile = Profile::FromBrowserContext(browser_context);
376 DCHECK(profile); 379 DCHECK(profile);
377 380
378 // The notification settings button will be appended after the developer- 381 // The notification settings button will be appended after the developer-
379 // supplied buttons, available in |notification_data.actions|. 382 // supplied buttons, available in |notification_data.actions|.
380 int settings_button_index = notification_data.actions.size(); 383 int settings_button_index = notification_data.actions.size();
381 PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate( 384 PersistentNotificationDelegate* delegate = new PersistentNotificationDelegate(
382 browser_context, persistent_notification_id, origin, 385 browser_context, persistent_notification_id, origin,
383 settings_button_index); 386 settings_button_index);
384 387
385 Notification notification = CreateNotificationFromData( 388 Notification notification = CreateNotificationFromData(
386 profile, origin, icon, notification_data, delegate); 389 profile, origin, notification_data, notification_resources, delegate);
387 390
388 // TODO(peter): Remove this mapping when we have reliable id generation for 391 // TODO(peter): Remove this mapping when we have reliable id generation for
389 // the message_center::Notification objects. 392 // the message_center::Notification objects.
390 persistent_notifications_[persistent_notification_id] = notification.id(); 393 persistent_notifications_[persistent_notification_id] = notification.id();
391 394
392 GetNotificationUIManager()->Add(notification, profile); 395 GetNotificationUIManager()->Add(notification, profile);
393 content::RecordAction( 396 content::RecordAction(
394 base::UserMetricsAction("Notifications.Persistent.Shown")); 397 base::UserMetricsAction("Notifications.Persistent.Shown"));
395 398
396 HostContentSettingsMapFactory::GetForProfile(profile)->UpdateLastUsage( 399 HostContentSettingsMapFactory::GetForProfile(profile)->UpdateLastUsage(
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 #else 457 #else
455 // Android cannot reliably return the notifications that are currently being 458 // Android cannot reliably return the notifications that are currently being
456 // displayed on the platform, see the comment in NotificationUIManagerAndroid. 459 // displayed on the platform, see the comment in NotificationUIManagerAndroid.
457 return false; 460 return false;
458 #endif // !defined(OS_ANDROID) 461 #endif // !defined(OS_ANDROID)
459 } 462 }
460 463
461 Notification PlatformNotificationServiceImpl::CreateNotificationFromData( 464 Notification PlatformNotificationServiceImpl::CreateNotificationFromData(
462 Profile* profile, 465 Profile* profile,
463 const GURL& origin, 466 const GURL& origin,
464 const SkBitmap& icon,
465 const content::PlatformNotificationData& notification_data, 467 const content::PlatformNotificationData& notification_data,
468 const content::NotificationResources& notification_resources,
466 NotificationDelegate* delegate) const { 469 NotificationDelegate* delegate) const {
470 DCHECK_EQ(notification_data.actions.size(),
471 notification_resources.action_icons.size());
472
467 // TODO(peter): Icons for Web Notifications are currently always requested for 473 // TODO(peter): Icons for Web Notifications are currently always requested for
468 // 1x scale, whereas the displays on which they can be displayed can have a 474 // 1x scale, whereas the displays on which they can be displayed can have a
469 // different pixel density. Be smarter about this when the API gets updated 475 // different pixel density. Be smarter about this when the API gets updated
470 // with a way for developers to specify images of different resolutions. 476 // with a way for developers to specify images of different resolutions.
471 Notification notification( 477 Notification notification(
472 message_center::NOTIFICATION_TYPE_SIMPLE, notification_data.title, 478 message_center::NOTIFICATION_TYPE_SIMPLE, notification_data.title,
473 notification_data.body, gfx::Image::CreateFrom1xBitmap(icon), 479 notification_data.body,
480 gfx::Image::CreateFrom1xBitmap(notification_resources.notification_icon),
474 message_center::NotifierId(origin), base::UTF8ToUTF16(origin.host()), 481 message_center::NotifierId(origin), base::UTF8ToUTF16(origin.host()),
475 origin, notification_data.tag, message_center::RichNotificationData(), 482 origin, notification_data.tag, message_center::RichNotificationData(),
476 delegate); 483 delegate);
477 484
478 notification.set_context_message( 485 notification.set_context_message(
479 DisplayNameForContextMessage(profile, origin)); 486 DisplayNameForContextMessage(profile, origin));
480 notification.set_vibration_pattern(notification_data.vibration_pattern); 487 notification.set_vibration_pattern(notification_data.vibration_pattern);
481 notification.set_silent(notification_data.silent); 488 notification.set_silent(notification_data.silent);
482 489
490 // Developer supplied action buttons.
483 std::vector<message_center::ButtonInfo> buttons; 491 std::vector<message_center::ButtonInfo> buttons;
484 492 for (size_t i = 0; i < notification_data.actions.size(); i++) {
485 // Developer supplied buttons. 493 message_center::ButtonInfo button(notification_data.actions[i].title);
486 for (const auto& action : notification_data.actions) 494 button.icon =
487 buttons.push_back(message_center::ButtonInfo(action.title)); 495 gfx::Image::CreateFrom1xBitmap(notification_resources.action_icons[i]);
488 496 buttons.push_back(button);
497 }
489 notification.set_buttons(buttons); 498 notification.set_buttons(buttons);
490 499
491 // On desktop, notifications with require_interaction==true stay on-screen 500 // On desktop, notifications with require_interaction==true stay on-screen
492 // rather than minimizing to the notification center after a timeout. 501 // rather than minimizing to the notification center after a timeout.
493 // On mobile, this is ignored (notifications are minimized at all times). 502 // On mobile, this is ignored (notifications are minimized at all times).
494 if (notification_data.require_interaction) 503 if (notification_data.require_interaction)
495 notification.set_never_timeout(true); 504 notification.set_never_timeout(true);
496 505
497 return notification; 506 return notification;
498 } 507 }
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 extensions::ExtensionRegistry::Get(profile)->GetExtensionById( 555 extensions::ExtensionRegistry::Get(profile)->GetExtensionById(
547 origin.host(), extensions::ExtensionRegistry::EVERYTHING); 556 origin.host(), extensions::ExtensionRegistry::EVERYTHING);
548 DCHECK(extension); 557 DCHECK(extension);
549 558
550 return base::UTF8ToUTF16(extension->name()); 559 return base::UTF8ToUTF16(extension->name());
551 } 560 }
552 #endif 561 #endif
553 562
554 return base::string16(); 563 return base::string16();
555 } 564 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698