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/message_center/message_center_util.h" 10#include "ui/webui/web_ui_util.h" 11 12Notification::Notification(const GURL& origin_url, 13 const GURL& content_url, 14 const string16& display_source, 15 const string16& replace_id, 16 NotificationDelegate* delegate) 17 : message_center::Notification(message_center::NOTIFICATION_TYPE_SIMPLE, 18 delegate->id(), 19 EmptyString16(), 20 EmptyString16(), 21 gfx::Image(), 22 display_source, 23 origin_url.spec(), 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 32Notification::Notification(const GURL& origin_url, 33 const GURL& icon_url, 34 const string16& title, 35 const string16& body, 36 WebKit::WebTextDirection dir, 37 const string16& display_source, 38 const 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 origin_url.spec(), 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 59Notification::Notification( 60 message_center::NotificationType type, 61 const GURL& origin_url, 62 const string16& title, 63 const string16& body, 64 const gfx::Image& icon, 65 WebKit::WebTextDirection dir, 66 const string16& display_source, 67 const string16& replace_id, 68 const message_center::RichNotificationData& rich_notification_data, 69 NotificationDelegate* delegate) 70 : message_center::Notification(type, 71 delegate->id(), 72 title, 73 body, 74 icon, 75 display_source, 76 origin_url.spec(), 77 rich_notification_data, 78 delegate), 79 origin_url_(origin_url), 80 is_html_(false), 81 replace_id_(replace_id), 82 delegate_(delegate) { 83 // It's important to leave |icon_url_| empty with rich notifications enabled, 84 // to prevent "Downloading" the data url and overwriting the existing |icon|. 85 if (!message_center::IsRichNotificationEnabled()) { 86 // "Upconvert" the string parameters to a data: URL. Some balloon views 87 // require content URL to render anything, so this serves as a backup. 88 if (!icon.IsEmpty()) 89 icon_url_ = GURL(webui::GetBitmapDataUrl(*icon.ToSkBitmap())); 90 content_url_ = GURL( 91 DesktopNotificationService::CreateDataUrl(icon_url_, title, body, dir)); 92 } 93} 94 95Notification::Notification(const GURL& origin_url, 96 const gfx::Image& icon, 97 const string16& title, 98 const string16& body, 99 WebKit::WebTextDirection dir, 100 const string16& display_source, 101 const string16& replace_id, 102 NotificationDelegate* delegate) 103 : message_center::Notification(message_center::NOTIFICATION_TYPE_SIMPLE, 104 delegate->id(), 105 title, 106 body, 107 icon, 108 display_source, 109 origin_url.spec(), 110 message_center::RichNotificationData(), 111 delegate), 112 origin_url_(origin_url), 113 is_html_(false), 114 replace_id_(replace_id), 115 delegate_(delegate) {} 116 117Notification::Notification(const Notification& notification) 118 : message_center::Notification(notification), 119 origin_url_(notification.origin_url()), 120 icon_url_(notification.icon_url()), 121 is_html_(notification.is_html()), 122 content_url_(notification.content_url()), 123 button_one_icon_url_(notification.button_one_icon_url()), 124 button_two_icon_url_(notification.button_two_icon_url()), 125 image_url_(notification.image_url()), 126 replace_id_(notification.replace_id()), 127 delegate_(notification.delegate()) {} 128 129Notification::~Notification() {} 130 131Notification& Notification::operator=(const Notification& notification) { 132 message_center::Notification::operator=(notification); 133 origin_url_ = notification.origin_url(); 134 icon_url_ = notification.icon_url(); 135 is_html_ = notification.is_html(); 136 content_url_ = notification.content_url(); 137 button_one_icon_url_ = notification.button_one_icon_url(); 138 button_two_icon_url_ = notification.button_two_icon_url(); 139 image_url_ = notification.image_url(); 140 replace_id_ = notification.replace_id(); 141 delegate_ = notification.delegate(); 142 return *this; 143} 144