Bug 1224785, Part 2 - Show the site favicon in OS X notifications. r?mstange draft
authorKit Cambridge <kcambridge@mozilla.com>
Thu, 07 Jan 2016 13:16:54 -0700
changeset 330158 d4f030c27f7c771608b025e0f13e57467dd9a0b4
parent 330157 99dfb7b28e9c5eade56199193cf144221421af8b
child 514111 dc76c18eb7defe16f7494263dd6670f6b783176b
push id10690
push userkcambridge@mozilla.com
push dateWed, 10 Feb 2016 20:14:02 +0000
reviewersmstange
bugs1224785
milestone47.0a1
Bug 1224785, Part 2 - Show the site favicon in OS X notifications. r?mstange MozReview-Commit-ID: L7d00xEY6Ka
widget/cocoa/OSXNotificationCenter.h
widget/cocoa/OSXNotificationCenter.mm
--- a/widget/cocoa/OSXNotificationCenter.h
+++ b/widget/cocoa/OSXNotificationCenter.h
@@ -20,23 +20,25 @@ typedef NSInteger NSUserNotificationActi
 #endif
 
 namespace mozilla {
 
 class OSXNotificationInfo;
 
 class OSXNotificationCenter : public nsIAlertsService,
                               public imgINotificationObserver,
-                              public nsITimerCallback
+                              public nsITimerCallback,
+                              public nsIAlertsIconData
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIALERTSSERVICE
   NS_DECL_IMGINOTIFICATIONOBSERVER
   NS_DECL_NSITIMERCALLBACK
+  NS_DECL_NSIALERTSICONDATA
 
   OSXNotificationCenter();
 
   nsresult Init();
   void CloseAlertCocoaString(NSString *aAlertName);
   void OnActivate(NSString *aAlertName, NSUserNotificationActivationType aActivationType,
                   unsigned long long aAdditionalActionIndex);
   void ShowPendingNotification(OSXNotificationInfo *osxni);
--- a/widget/cocoa/OSXNotificationCenter.mm
+++ b/widget/cocoa/OSXNotificationCenter.mm
@@ -213,17 +213,18 @@ OSXNotificationCenter::~OSXNotificationC
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
 
   [GetNotificationCenter() removeAllDeliveredNotifications];
   [mDelegate release];
 
   NS_OBJC_END_TRY_ABORT_BLOCK;
 }
 
-NS_IMPL_ISUPPORTS(OSXNotificationCenter, nsIAlertsService, imgINotificationObserver, nsITimerCallback)
+NS_IMPL_ISUPPORTS(OSXNotificationCenter, nsIAlertsService, nsITimerCallback,
+                  imgINotificationObserver, nsIAlertsIconData)
 
 nsresult OSXNotificationCenter::Init()
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
 
   return (!!NSClassFromString(@"NSUserNotification")) ? NS_OK : NS_ERROR_FAILURE;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
@@ -251,16 +252,25 @@ OSXNotificationCenter::ShowAlertNotifica
   NS_ENSURE_SUCCESS(rv, rv);
   return ShowAlert(alert, aAlertListener);
 }
 
 NS_IMETHODIMP
 OSXNotificationCenter::ShowAlert(nsIAlertNotification* aAlert,
                                  nsIObserver* aAlertListener)
 {
+  return ShowAlertWithIconData(aAlert, aAlertListener, 0, nullptr);
+}
+
+NS_IMETHODIMP
+OSXNotificationCenter::ShowAlertWithIconData(nsIAlertNotification* aAlert,
+                                             nsIObserver* aAlertListener,
+                                             uint32_t aIconSize,
+                                             const uint8_t* aIconData)
+{
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
 
   NS_ENSURE_ARG(aAlert);
 
   Class unClass = NSClassFromString(@"NSUserNotification");
   id<FakeNSUserNotification> notification = [[unClass alloc] init];
 
   nsAutoString title;
@@ -343,16 +353,28 @@ OSXNotificationCenter::ShowAlert(nsIAler
                                                       forKeys:[NSArray arrayWithObjects:@"name", nil]];
 
   nsAutoString cookie;
   rv = aAlert->GetCookie(cookie);
   NS_ENSURE_SUCCESS(rv, rv);
 
   OSXNotificationInfo *osxni = new OSXNotificationInfo(alertName, aAlertListener, cookie);
 
+  // Show the favicon if supported on this version of OS X.
+  if (aIconSize > 0 &&
+      [notification respondsToSelector:@selector(set_identityImage:)] &&
+      [notification respondsToSelector:@selector(set_identityImageHasBorder:)]) {
+
+    NSData *iconData = [NSData dataWithBytes:aIconData length:aIconSize];
+    NSImage *icon = [[[NSImage alloc] initWithData:iconData] autorelease];
+
+    [(NSObject*)notification setValue:icon forKey:@"_identityImage"];
+    [(NSObject*)notification setValue:@(NO) forKey:@"_identityImageHasBorder"];
+  }
+
   nsAutoString imageUrl;
   rv = aAlert->GetImageURL(imageUrl);
   NS_ENSURE_SUCCESS(rv, rv);
 
   bool inPrivateBrowsing;
   rv = aAlert->GetInPrivateBrowsing(&inPrivateBrowsing);
   NS_ENSURE_SUCCESS(rv, rv);