Home | History | Annotate | Download | only in notifications
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/notifications/notification.h"
      6 
      7 #include "base/strings/string_util.h"
      8 #include "chrome/browser/notifications/desktop_notification_service.h"
      9 #include "ui/base/webui/web_ui_util.h"
     10 #include "ui/message_center/message_center_util.h"
     11 
     12 Notification::Notification(const GURL& origin_url,
     13                            const GURL& content_url,
     14                            const base::string16& display_source,
     15                            const base::string16& replace_id,
     16                            NotificationDelegate* delegate)
     17     : message_center::Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
     18                                    delegate->id(),
     19                                    base::string16(),
     20                                    base::string16(),
     21                                    gfx::Image(),
     22                                    display_source,
     23                                    message_center::NotifierId(origin_url),
     24                                    message_center::RichNotificationData(),
     25                                    delegate),
     26       origin_url_(origin_url),
     27       is_html_(true),
     28       content_url_(content_url),
     29       replace_id_(replace_id),
     30       delegate_(delegate) {}
     31 
     32 Notification::Notification(const GURL& origin_url,
     33                            const GURL& icon_url,
     34                            const base::string16& title,
     35                            const base::string16& body,
     36                            blink::WebTextDirection dir,
     37                            const base::string16& display_source,
     38                            const base::string16& replace_id,
     39                            NotificationDelegate* delegate)
     40     : message_center::Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
     41                                    delegate->id(),
     42                                    title,
     43                                    body,
     44                                    gfx::Image(),
     45                                    display_source,
     46                                    message_center::NotifierId(origin_url),
     47                                    message_center::RichNotificationData(),
     48                                    delegate),
     49       origin_url_(origin_url),
     50       icon_url_(icon_url),
     51       is_html_(false),
     52       replace_id_(replace_id),
     53       delegate_(delegate) {
     54   // "Upconvert" the string parameters to a data: URL.
     55   content_url_ = GURL(DesktopNotificationService::CreateDataUrl(
     56       icon_url, title, body, dir));
     57 }
     58 
     59 Notification::Notification(
     60     message_center::NotificationType type,
     61     const GURL& origin_url,
     62     const base::string16& title,
     63     const base::string16& body,
     64     const gfx::Image& icon,
     65     blink::WebTextDirection dir,
     66     const message_center::NotifierId& notifier_id,
     67     const base::string16& display_source,
     68     const base::string16& replace_id,
     69     const message_center::RichNotificationData& rich_notification_data,
     70     NotificationDelegate* delegate)
     71     : message_center::Notification(type,
     72                                    delegate->id(),
     73                                    title,
     74                                    body,
     75                                    icon,
     76                                    display_source,
     77                                    notifier_id,
     78                                    rich_notification_data,
     79                                    delegate),
     80       origin_url_(origin_url),
     81       is_html_(false),
     82       replace_id_(replace_id),
     83       delegate_(delegate) {
     84   // It's important to leave |icon_url_| empty with rich notifications enabled,
     85   // to prevent "Downloading" the data url and overwriting the existing |icon|.
     86   if (!message_center::IsRichNotificationEnabled()) {
     87     // "Upconvert" the string parameters to a data: URL.  Some balloon views
     88     // require content URL to render anything, so this serves as a backup.
     89     if (!icon.IsEmpty())
     90       icon_url_ = GURL(webui::GetBitmapDataUrl(*icon.ToSkBitmap()));
     91     content_url_ = GURL(
     92         DesktopNotificationService::CreateDataUrl(icon_url_, title, body, dir));
     93   }
     94 }
     95 
     96 Notification::Notification(const GURL& origin_url,
     97                            const gfx::Image& icon,
     98                            const base::string16& title,
     99                            const base::string16& body,
    100                            blink::WebTextDirection dir,
    101                            const base::string16& display_source,
    102                            const base::string16& replace_id,
    103                            NotificationDelegate* delegate)
    104     : message_center::Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
    105                                    delegate->id(),
    106                                    title,
    107                                    body,
    108                                    icon,
    109                                    display_source,
    110                                    message_center::NotifierId(origin_url),
    111                                    message_center::RichNotificationData(),
    112                                    delegate),
    113       origin_url_(origin_url),
    114       is_html_(false),
    115       replace_id_(replace_id),
    116       delegate_(delegate) {}
    117 
    118 Notification::Notification(const Notification& notification)
    119     : message_center::Notification(notification),
    120       origin_url_(notification.origin_url()),
    121       icon_url_(notification.icon_url()),
    122       is_html_(notification.is_html()),
    123       content_url_(notification.content_url()),
    124       button_one_icon_url_(notification.button_one_icon_url()),
    125       button_two_icon_url_(notification.button_two_icon_url()),
    126       image_url_(notification.image_url()),
    127       replace_id_(notification.replace_id()),
    128       delegate_(notification.delegate()) {}
    129 
    130 Notification::~Notification() {}
    131 
    132 Notification& Notification::operator=(const Notification& notification) {
    133   message_center::Notification::operator=(notification);
    134   origin_url_ = notification.origin_url();
    135   icon_url_ = notification.icon_url();
    136   is_html_ = notification.is_html();
    137   content_url_ = notification.content_url();
    138   button_one_icon_url_ = notification.button_one_icon_url();
    139   button_two_icon_url_ = notification.button_two_icon_url();
    140   image_url_ = notification.image_url();
    141   replace_id_ = notification.replace_id();
    142   delegate_ = notification.delegate();
    143   return *this;
    144 }
    145