diff options
author | miguelg <miguelg@chromium.org> | 2015-08-20 02:00:30 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-08-20 09:01:21 +0000 |
commit | 53a6dde573f020d57b4fff5923f272f98da66e27 (patch) | |
tree | d305d9922fd6896ae4b4d90c0f292a7b58b8c28f /ui | |
parent | 731d73a31fddd700ac7fb2c832a3011f8753f4f8 (diff) | |
download | chromium_src-53a6dde573f020d57b4fff5923f272f98da66e27.zip chromium_src-53a6dde573f020d57b4fff5923f272f98da66e27.tar.gz chromium_src-53a6dde573f020d57b4fff5923f272f98da66e27.tar.bz2 |
Elide origins displayed on web notifications.
WebPush displays notifications including the origin in the context message
field. When such origin is too long it is now elided in a secure acceptable
way.
BUG=509834
Review URL: https://codereview.chromium.org/1292003004
Cr-Commit-Position: refs/heads/master@{#344455}
Diffstat (limited to 'ui')
20 files changed, 457 insertions, 647 deletions
diff --git a/ui/message_center/BUILD.gn b/ui/message_center/BUILD.gn index 8d129a6..752e881 100644 --- a/ui/message_center/BUILD.gn +++ b/ui/message_center/BUILD.gn @@ -11,6 +11,7 @@ component("message_center") { "//base", "//base:i18n", "//base/third_party/dynamic_annotations", + "//components/url_formatter", "//skia", "//ui/accessibility", "//ui/base", diff --git a/ui/message_center/DEPS b/ui/message_center/DEPS index 6d0196ae..e8b12b4 100644 --- a/ui/message_center/DEPS +++ b/ui/message_center/DEPS @@ -1,4 +1,5 @@ include_rules = [ + "+components/url_formatter", "+skia/ext", "+third_party/skia", "+ui/accessibility", diff --git a/ui/message_center/cocoa/notification_controller.mm b/ui/message_center/cocoa/notification_controller.mm index 1b4e96a..2d1207a 100644 --- a/ui/message_center/cocoa/notification_controller.mm +++ b/ui/message_center/cocoa/notification_controller.mm @@ -10,6 +10,7 @@ #include "base/strings/string_util.h" #include "base/strings/sys_string_conversions.h" #include "base/strings/utf_string_conversions.h" +#include "components/url_formatter/elide_url.h" #include "skia/ext/skia_utils_mac.h" #import "ui/base/cocoa/hover_image_button.h" #include "ui/base/l10n/l10n_util_mac.h" @@ -22,7 +23,7 @@ #include "ui/message_center/notification.h" #include "ui/resources/grit/ui_resources.h" #include "ui/strings/grit/ui_strings.h" - +#include "url/gurl.h" @interface MCNotificationProgressBar : NSProgressIndicator @end @@ -380,11 +381,11 @@ // Set the title and recalculate the frame. size_t actualTitleLines = 0; - [title_ setString:base::SysUTF16ToNSString( - [self wrapText:notification_->title() - forFont:[title_ font] - maxNumberOfLines:message_center::kMaxTitleLines - actualLines:&actualTitleLines])]; + [title_ setString:base::SysUTF16ToNSString([self + wrapText:notification_->title() + forFont:[title_ font] + maxNumberOfLines:message_center::kMaxTitleLines + actualLines:&actualTitleLines])]; [title_ sizeToFit]; NSRect titleFrame = [title_ frame]; titleFrame.origin.y = NSMaxY(rootFrame) - titlePadding - NSHeight(titleFrame); @@ -396,7 +397,9 @@ messageLineLimit -= (actualTitleLines - 1) * 2; if (!notification_->image().IsEmpty()) { messageLineLimit /= 2; - if (!notification_->context_message().empty()) + + if (!notification_->context_message().empty() && + !notification_->UseOriginAsContextMessage()) messageLineLimit -= message_center::kContextMessageLineLimit; } if (messageLineLimit < 0) @@ -428,14 +431,26 @@ } // Set the context message and recalculate the frame. - [contextMessage_ setString:base::SysUTF16ToNSString( - [self wrapText:notification_->context_message() + base::string16 message; + if (notification->UseOriginAsContextMessage()) { + gfx::FontList font_list((gfx::Font([message_ font]))); + message = + url_formatter::ElideHost(notification->origin_url(), font_list, + message_center::kContextMessageViewWidth); + } else { + message = notification->context_message(); + } + + base::string16 elided = + [self wrapText:message forFont:[contextMessage_ font] - maxNumberOfLines:message_center::kContextMessageLineLimit])]; + maxNumberOfLines:message_center::kContextMessageLineLimit]; + [contextMessage_ setString:base::SysUTF16ToNSString(elided)]; [contextMessage_ sizeToFit]; NSRect contextMessageFrame = [contextMessage_ frame]; - if (notification_->context_message().empty()) { + if (notification->context_message().empty() && + !notification->UseOriginAsContextMessage()) { [contextMessage_ setHidden:YES]; contextMessageFrame.origin.y = messageFrame.origin.y; contextMessageFrame.size.height = 0; diff --git a/ui/message_center/cocoa/notification_controller_unittest.mm b/ui/message_center/cocoa/notification_controller_unittest.mm index 41573e1..5b5999c 100644 --- a/ui/message_center/cocoa/notification_controller_unittest.mm +++ b/ui/message_center/cocoa/notification_controller_unittest.mm @@ -117,14 +117,10 @@ class NotificationControllerTest : public ui::CocoaTest { TEST_F(NotificationControllerTest, BasicLayout) { scoped_ptr<message_center::Notification> notification( new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "", + message_center::NOTIFICATION_TYPE_SIMPLE, "", ASCIIToUTF16("Added to circles"), - ASCIIToUTF16("Jonathan and 5 others"), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), + ASCIIToUTF16("Jonathan and 5 others"), gfx::Image(), base::string16(), + GURL(), DummyNotifierId(), message_center::RichNotificationData(), NULL)); gfx::Image testIcon([TestIcon() retain]); notification->set_icon(testIcon); @@ -147,18 +143,14 @@ TEST_F(NotificationControllerTest, BasicLayout) { TEST_F(NotificationControllerTest, OverflowText) { scoped_ptr<message_center::Notification> notification( new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "", + message_center::NOTIFICATION_TYPE_SIMPLE, "", ASCIIToUTF16("This is a much longer title that should wrap " "multiple lines."), ASCIIToUTF16("And even the message is long. This sure is a wordy " "notification. Are you really going to read this " "entire thing?"), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + gfx::Image(), base::string16(), GURL(), DummyNotifierId(), + message_center::RichNotificationData(), NULL)); base::scoped_nsobject<MCNotificationController> controller( [[MCNotificationController alloc] initWithNotification:notification.get() messageCenter:NULL]); @@ -171,15 +163,9 @@ TEST_F(NotificationControllerTest, OverflowText) { TEST_F(NotificationControllerTest, Close) { scoped_ptr<message_center::Notification> notification( new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "an_id", - base::string16(), - base::string16(), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + message_center::NOTIFICATION_TYPE_SIMPLE, "an_id", base::string16(), + base::string16(), gfx::Image(), base::string16(), GURL(), + DummyNotifierId(), message_center::RichNotificationData(), NULL)); MockMessageCenter message_center; base::scoped_nsobject<MCNotificationController> controller( @@ -197,16 +183,12 @@ TEST_F(NotificationControllerTest, Close) { TEST_F(NotificationControllerTest, Update) { scoped_ptr<message_center::Notification> notification( new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "", + message_center::NOTIFICATION_TYPE_SIMPLE, "", ASCIIToUTF16("A simple title"), ASCIIToUTF16("This message isn't too long and should fit in the" "default bounds."), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + gfx::Image(), base::string16(), GURL(), DummyNotifierId(), + message_center::RichNotificationData(), NULL)); base::scoped_nsobject<MCNotificationController> controller( [[MCNotificationController alloc] initWithNotification:notification.get() messageCenter:NULL]); @@ -238,15 +220,9 @@ TEST_F(NotificationControllerTest, Buttons) { scoped_ptr<message_center::Notification> notification( new message_center::Notification( - message_center::NOTIFICATION_TYPE_BASE_FORMAT, - "an_id", - base::string16(), - base::string16(), - gfx::Image(), - base::string16(), - DummyNotifierId(), - optional, - NULL)); + message_center::NOTIFICATION_TYPE_BASE_FORMAT, "an_id", + base::string16(), base::string16(), gfx::Image(), base::string16(), + GURL(), DummyNotifierId(), optional, NULL)); MockMessageCenter message_center; base::scoped_nsobject<MCNotificationController> controller( @@ -263,14 +239,9 @@ TEST_F(NotificationControllerTest, Buttons) { TEST_F(NotificationControllerTest, Image) { scoped_ptr<message_center::Notification> notification( new message_center::Notification( - message_center::NOTIFICATION_TYPE_BASE_FORMAT, - "an_id", - base::string16(), - base::string16(), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), + message_center::NOTIFICATION_TYPE_BASE_FORMAT, "an_id", + base::string16(), base::string16(), gfx::Image(), base::string16(), + GURL(), DummyNotifierId(), message_center::RichNotificationData(), NULL)); NSImage* image = [NSImage imageNamed:NSImageNameFolder]; notification->set_image(gfx::Image([image retain])); @@ -306,15 +277,10 @@ TEST_F(NotificationControllerTest, List) { scoped_ptr<message_center::Notification> notification( new message_center::Notification( - message_center::NOTIFICATION_TYPE_BASE_FORMAT, - "an_id", + message_center::NOTIFICATION_TYPE_BASE_FORMAT, "an_id", UTF8ToUTF16("Notification Title"), - UTF8ToUTF16("Notification Message - should be hidden"), - gfx::Image(), - base::string16(), - DummyNotifierId(), - optional, - NULL)); + UTF8ToUTF16("Notification Message - should be hidden"), gfx::Image(), + base::string16(), GURL(), DummyNotifierId(), optional, NULL)); MockMessageCenter message_center; base::scoped_nsobject<MCNotificationController> controller( @@ -337,15 +303,9 @@ TEST_F(NotificationControllerTest, NoMessage) { scoped_ptr<message_center::Notification> notification( new message_center::Notification( - message_center::NOTIFICATION_TYPE_BASE_FORMAT, - "an_id", - UTF8ToUTF16("Notification Title"), - UTF8ToUTF16(""), - gfx::Image(), - base::string16(), - DummyNotifierId(), - optional, - NULL)); + message_center::NOTIFICATION_TYPE_BASE_FORMAT, "an_id", + UTF8ToUTF16("Notification Title"), UTF8ToUTF16(""), gfx::Image(), + base::string16(), GURL(), DummyNotifierId(), optional, NULL)); MockMessageCenter message_center; base::scoped_nsobject<MCNotificationController> controller( @@ -363,15 +323,10 @@ TEST_F(NotificationControllerTest, MessageSize) { std::string id("id"); NotifierId notifier_id(NotifierId::APPLICATION, "notifier"); scoped_ptr<Notification> notification(new Notification( - NOTIFICATION_TYPE_BASE_FORMAT, - id, - base::UTF8ToUTF16(""), + NOTIFICATION_TYPE_BASE_FORMAT, id, base::UTF8ToUTF16(""), ASCIIToUTF16("And\neven\nthe\nmessage is long.\nThis sure is wordy"), - gfx::Image(), - base::string16() /* display_source */, - notifier_id, - data, - NULL /* delegate */)); + gfx::Image(), base::string16() /* display_source */, GURL(), notifier_id, + data, NULL /* delegate */)); base::scoped_nsobject<MCNotificationController> controller( [[MCNotificationController alloc] initWithNotification:notification.get() @@ -422,7 +377,7 @@ TEST_F(NotificationControllerTest, MessageSize) { EXPECT_EQ(1u, compute_message_lines()); // Same as above, but context message takes away from message lines. - notification->set_context_message(base::UTF8ToUTF16("foo")); + notification->set_context_message(UTF8ToUTF16("foo")); notification->set_title(ASCIIToUTF16("")); [controller updateNotification:notification.get()]; EXPECT_EQ(1u, compute_message_lines()); diff --git a/ui/message_center/cocoa/popup_collection_unittest.mm b/ui/message_center/cocoa/popup_collection_unittest.mm index 80c3874..274d375 100644 --- a/ui/message_center/cocoa/popup_collection_unittest.mm +++ b/ui/message_center/cocoa/popup_collection_unittest.mm @@ -49,45 +49,30 @@ class PopupCollectionTest : public ui::CocoaTest { void AddThreeNotifications() { scoped_ptr<message_center::Notification> notification; notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "1", - ASCIIToUTF16("One"), + message_center::NOTIFICATION_TYPE_SIMPLE, "1", ASCIIToUTF16("One"), ASCIIToUTF16("This is the first notification to" " be displayed"), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + gfx::Image(), base::string16(), GURL(), DummyNotifierId(), + message_center::RichNotificationData(), NULL)); center_->AddNotification(notification.Pass()); notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "2", - ASCIIToUTF16("Two"), - ASCIIToUTF16("This is the second notification."), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + message_center::NOTIFICATION_TYPE_SIMPLE, "2", ASCIIToUTF16("Two"), + ASCIIToUTF16("This is the second notification."), gfx::Image(), + base::string16(), GURL(), DummyNotifierId(), + message_center::RichNotificationData(), NULL)); center_->AddNotification(notification.Pass()); notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "3", - ASCIIToUTF16("Three"), + message_center::NOTIFICATION_TYPE_SIMPLE, "3", ASCIIToUTF16("Three"), ASCIIToUTF16("This is the third notification " "that has a much longer body " "than the other notifications. It " "may not fit on the screen if we " "set the screen size too small or " "if the notification is way too big"), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + gfx::Image(), base::string16(), GURL(), DummyNotifierId(), + message_center::RichNotificationData(), NULL)); center_->AddNotification(notification.Pass()); WaitForAnimationEnded(); } @@ -134,15 +119,10 @@ TEST_F(PopupCollectionTest, AttemptFourOneOffscreen) { scoped_ptr<message_center::Notification> notification; notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "4", - ASCIIToUTF16("Four"), - ASCIIToUTF16("This is the fourth notification."), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + message_center::NOTIFICATION_TYPE_SIMPLE, "4", ASCIIToUTF16("Four"), + ASCIIToUTF16("This is the fourth notification."), gfx::Image(), + base::string16(), GURL(), DummyNotifierId(), + message_center::RichNotificationData(), NULL)); center_->AddNotification(notification.Pass()); WaitForAnimationEnded(); @@ -183,15 +163,9 @@ TEST_F(PopupCollectionTest, LayoutSpacing) { optional.priority = message_center::HIGH_PRIORITY; scoped_ptr<message_center::Notification> notification; notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "4", - ASCIIToUTF16("Four"), - ASCIIToUTF16("This is the fourth notification."), - gfx::Image(), - base::string16(), - DummyNotifierId(), - optional, - NULL)); + message_center::NOTIFICATION_TYPE_SIMPLE, "4", ASCIIToUTF16("Four"), + ASCIIToUTF16("This is the fourth notification."), gfx::Image(), + base::string16(), GURL(), DummyNotifierId(), optional, NULL)); center_->AddNotification(notification.Pass()); WaitForAnimationEnded(); EXPECT_TRUE(CheckSpacingBetween([popups objectAtIndex:2], @@ -220,38 +194,28 @@ TEST_F(PopupCollectionTest, TinyScreen) { EXPECT_EQ(0u, [[collection_ popups] count]); scoped_ptr<message_center::Notification> notification; notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "1", - ASCIIToUTF16("One"), + message_center::NOTIFICATION_TYPE_SIMPLE, "1", ASCIIToUTF16("One"), ASCIIToUTF16("This is the first notification to" - " be displayed"), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + " be displayed"), + gfx::Image(), base::string16(), GURL(), DummyNotifierId(), + message_center::RichNotificationData(), NULL)); center_->AddNotification(notification.Pass()); WaitForAnimationEnded(); EXPECT_EQ(1u, [[collection_ popups] count]); // Now give the notification a longer message so that it no longer fits. notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "1", - ASCIIToUTF16("One"), + message_center::NOTIFICATION_TYPE_SIMPLE, "1", ASCIIToUTF16("One"), ASCIIToUTF16("This is now a very very very very " - "very very very very very very very " - "very very very very very very very " - "very very very very very very very " - "very very very very very very very " - "very very very very very very very " - "very very very very very very very " - "long notification."), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + "very very very very very very very " + "very very very very very very very " + "very very very very very very very " + "very very very very very very very " + "very very very very very very very " + "very very very very very very very " + "long notification."), + gfx::Image(), base::string16(), GURL(), DummyNotifierId(), + message_center::RichNotificationData(), NULL)); center_->UpdateNotification("1", notification.Pass()); WaitForAnimationEnded(); EXPECT_EQ(0u, [[collection_ popups] count]); @@ -283,19 +247,15 @@ TEST_F(PopupCollectionTest, UpdateIconAndBody) { NSRect old_frame = [[controller view] frame]; scoped_ptr<message_center::Notification> notification; notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "1", + message_center::NOTIFICATION_TYPE_SIMPLE, "1", ASCIIToUTF16("One is going to get a much longer " - "title than it previously had."), + "title than it previously had."), ASCIIToUTF16("This is the first notification to " - "be displayed, but it will also be " - "updated to have a significantly " - "longer body"), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + "be displayed, but it will also be " + "updated to have a significantly " + "longer body"), + gfx::Image(), base::string16(), GURL(), DummyNotifierId(), + message_center::RichNotificationData(), NULL)); center_->AddNotification(notification.Pass()); WaitForAnimationEnded(); EXPECT_GT(NSHeight([[controller view] frame]), NSHeight(old_frame)); @@ -314,15 +274,10 @@ TEST_F(PopupCollectionTest, UpdateIconAndBody) { TEST_F(PopupCollectionTest, UpdatePriority) { scoped_ptr<message_center::Notification> notification; notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "1", - ASCIIToUTF16("One"), - ASCIIToUTF16("This notification should not yet toast."), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + message_center::NOTIFICATION_TYPE_SIMPLE, "1", ASCIIToUTF16("One"), + ASCIIToUTF16("This notification should not yet toast."), gfx::Image(), + base::string16(), GURL(), DummyNotifierId(), + message_center::RichNotificationData(), NULL)); notification->set_priority(-1); center_->AddNotification(notification.Pass()); @@ -332,15 +287,10 @@ TEST_F(PopupCollectionTest, UpdatePriority) { // Raise priority -1 to 1. Notification should display. notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "1", - ASCIIToUTF16("One"), - ASCIIToUTF16("This notification should now toast"), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + message_center::NOTIFICATION_TYPE_SIMPLE, "1", ASCIIToUTF16("One"), + ASCIIToUTF16("This notification should now toast"), gfx::Image(), + base::string16(), GURL(), DummyNotifierId(), + message_center::RichNotificationData(), NULL)); notification->set_priority(1); center_->UpdateNotification("1", notification.Pass()); @@ -352,16 +302,11 @@ TEST_F(PopupCollectionTest, CloseCollectionBeforeNewPopupAnimationEnds) { // Add a notification and don't wait for the animation to finish. scoped_ptr<message_center::Notification> notification; notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "1", - ASCIIToUTF16("One"), + message_center::NOTIFICATION_TYPE_SIMPLE, "1", ASCIIToUTF16("One"), ASCIIToUTF16("This is the first notification to" " be displayed"), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + gfx::Image(), base::string16(), GURL(), DummyNotifierId(), + message_center::RichNotificationData(), NULL)); center_->AddNotification(notification.Pass()); // Release the popup collection before the animation ends. No crash should @@ -386,15 +331,9 @@ TEST_F(PopupCollectionTest, CloseCollectionBeforeUpdatePopupAnimationEnds) { // Update a notification and don't wait for the animation to finish. scoped_ptr<message_center::Notification> notification; notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "1", - ASCIIToUTF16("One"), - ASCIIToUTF16("New message."), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + message_center::NOTIFICATION_TYPE_SIMPLE, "1", ASCIIToUTF16("One"), + ASCIIToUTF16("New message."), gfx::Image(), base::string16(), GURL(), + DummyNotifierId(), message_center::RichNotificationData(), NULL)); center_->UpdateNotification("1", notification.Pass()); // Release the popup collection before the animation ends. No crash should diff --git a/ui/message_center/cocoa/popup_controller_unittest.mm b/ui/message_center/cocoa/popup_controller_unittest.mm index 73e142b..1094795 100644 --- a/ui/message_center/cocoa/popup_controller_unittest.mm +++ b/ui/message_center/cocoa/popup_controller_unittest.mm @@ -21,15 +21,11 @@ class PopupControllerTest : public ui::CocoaTest { TEST_F(PopupControllerTest, Creation) { scoped_ptr<message_center::Notification> notification( new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "", + message_center::NOTIFICATION_TYPE_SIMPLE, "", ASCIIToUTF16("Added to circles"), - ASCIIToUTF16("Jonathan and 5 others"), - gfx::Image(), - base::string16(), - message_center::NotifierId(), - message_center::RichNotificationData(), - NULL)); + ASCIIToUTF16("Jonathan and 5 others"), gfx::Image(), base::string16(), + GURL(), message_center::NotifierId(), + message_center::RichNotificationData(), NULL)); base::scoped_nsobject<MCPopupController> controller( [[MCPopupController alloc] initWithNotification:notification.get() diff --git a/ui/message_center/cocoa/tray_view_controller_unittest.mm b/ui/message_center/cocoa/tray_view_controller_unittest.mm index f24d673..c8ed157 100644 --- a/ui/message_center/cocoa/tray_view_controller_unittest.mm +++ b/ui/message_center/cocoa/tray_view_controller_unittest.mm @@ -72,15 +72,10 @@ TEST_F(TrayViewControllerTest, AddRemoveOne) { EXPECT_EQ(0u, [[view subviews] count]); scoped_ptr<message_center::Notification> notification_data; notification_data.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "1", + message_center::NOTIFICATION_TYPE_SIMPLE, "1", ASCIIToUTF16("First notification"), - ASCIIToUTF16("This is a simple test."), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + ASCIIToUTF16("This is a simple test."), gfx::Image(), base::string16(), + GURL(), DummyNotifierId(), message_center::RichNotificationData(), NULL)); center_->AddNotification(notification_data.Pass()); [tray_ onMessageCenterTrayChanged]; ASSERT_EQ(1u, [[view subviews] count]); @@ -109,37 +104,22 @@ TEST_F(TrayViewControllerTest, AddThreeClearAll) { EXPECT_EQ(0u, [[view subviews] count]); scoped_ptr<message_center::Notification> notification; notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "1", + message_center::NOTIFICATION_TYPE_SIMPLE, "1", ASCIIToUTF16("First notification"), - ASCIIToUTF16("This is a simple test."), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + ASCIIToUTF16("This is a simple test."), gfx::Image(), base::string16(), + GURL(), DummyNotifierId(), message_center::RichNotificationData(), NULL)); center_->AddNotification(notification.Pass()); notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "2", + message_center::NOTIFICATION_TYPE_SIMPLE, "2", ASCIIToUTF16("Second notification"), - ASCIIToUTF16("This is a simple test."), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + ASCIIToUTF16("This is a simple test."), gfx::Image(), base::string16(), + GURL(), DummyNotifierId(), message_center::RichNotificationData(), NULL)); center_->AddNotification(notification.Pass()); notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "3", + message_center::NOTIFICATION_TYPE_SIMPLE, "3", ASCIIToUTF16("Third notification"), - ASCIIToUTF16("This is a simple test."), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + ASCIIToUTF16("This is a simple test."), gfx::Image(), base::string16(), + GURL(), DummyNotifierId(), message_center::RichNotificationData(), NULL)); center_->AddNotification(notification.Pass()); [tray_ onMessageCenterTrayChanged]; ASSERT_EQ(3u, [[view subviews] count]); @@ -167,15 +147,10 @@ TEST_F(TrayViewControllerTest, NoClearAllWhenNoNotifications) { // Add a notification. scoped_ptr<message_center::Notification> notification; notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "1", + message_center::NOTIFICATION_TYPE_SIMPLE, "1", ASCIIToUTF16("First notification"), - ASCIIToUTF16("This is a simple test."), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + ASCIIToUTF16("This is a simple test."), gfx::Image(), base::string16(), + GURL(), DummyNotifierId(), message_center::RichNotificationData(), NULL)); center_->AddNotification(notification.Pass()); [tray_ onMessageCenterTrayChanged]; @@ -186,15 +161,10 @@ TEST_F(TrayViewControllerTest, NoClearAllWhenNoNotifications) { // Adding a second notification should keep things still visible. notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "2", + message_center::NOTIFICATION_TYPE_SIMPLE, "2", ASCIIToUTF16("Second notification"), - ASCIIToUTF16("This is a simple test."), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + ASCIIToUTF16("This is a simple test."), gfx::Image(), base::string16(), + GURL(), DummyNotifierId(), message_center::RichNotificationData(), NULL)); center_->AddNotification(notification.Pass()); [tray_ onMessageCenterTrayChanged]; EXPECT_FALSE([[tray_ clearAllButton] isHidden]); @@ -262,15 +232,10 @@ TEST_F(TrayViewControllerTest, EmptyCenter) { scoped_ptr<message_center::Notification> notification; notification.reset(new message_center::Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - "1", + message_center::NOTIFICATION_TYPE_SIMPLE, "1", ASCIIToUTF16("First notification"), - ASCIIToUTF16("This is a simple test."), - gfx::Image(), - base::string16(), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL)); + ASCIIToUTF16("This is a simple test."), gfx::Image(), base::string16(), + GURL(), DummyNotifierId(), message_center::RichNotificationData(), NULL)); center_->AddNotification(notification.Pass()); [tray_ onMessageCenterTrayChanged]; diff --git a/ui/message_center/message_center.gyp b/ui/message_center/message_center.gyp index 245d8ba..149fa4b 100644 --- a/ui/message_center/message_center.gyp +++ b/ui/message_center/message_center.gyp @@ -15,6 +15,7 @@ '../../base/base.gyp:base', '../../base/base.gyp:base_i18n', '../../base/third_party/dynamic_annotations/dynamic_annotations.gyp:dynamic_annotations', + '../../components/url_formatter/url_formatter.gyp:url_formatter', '../../skia/skia.gyp:skia', '../../url/url.gyp:url_lib', '../base/ui_base.gyp:ui_base', diff --git a/ui/message_center/message_center_impl_unittest.cc b/ui/message_center/message_center_impl_unittest.cc index ccf8633..af48555 100644 --- a/ui/message_center/message_center_impl_unittest.cc +++ b/ui/message_center/message_center_impl_unittest.cc @@ -81,15 +81,11 @@ class MessageCenterImplTest : public testing::Test, RichNotificationData optional_fields; optional_fields.buttons.push_back(ButtonInfo(UTF8ToUTF16("foo"))); optional_fields.buttons.push_back(ButtonInfo(UTF8ToUTF16("foo"))); - return new Notification(type, - id, - UTF8ToUTF16("title"), - UTF8ToUTF16(id), + return new Notification(type, id, UTF8ToUTF16("title"), UTF8ToUTF16(id), gfx::Image() /* icon */, - base::string16() /* display_source */, + base::string16() /* display_source */, GURL(), NotifierId(NotifierId::APPLICATION, notifier_id), - optional_fields, - NULL); + optional_fields, NULL); } @@ -351,26 +347,16 @@ TEST_F(MessageCenterImplTest, NotificationBlocker) { ToggledNotificationBlocker blocker1(message_center()); ToggledNotificationBlocker blocker2(message_center()); - message_center()->AddNotification(scoped_ptr<Notification>(new Notification( - NOTIFICATION_TYPE_SIMPLE, - "id1", - UTF8ToUTF16("title"), - UTF8ToUTF16("message"), - gfx::Image() /* icon */, - base::string16() /* display_source */, - notifier_id, - RichNotificationData(), - NULL))); - message_center()->AddNotification(scoped_ptr<Notification>(new Notification( - NOTIFICATION_TYPE_SIMPLE, - "id2", - UTF8ToUTF16("title"), - UTF8ToUTF16("message"), - gfx::Image() /* icon */, - base::string16() /* display_source */, - notifier_id, - RichNotificationData(), - NULL))); + message_center()->AddNotification(scoped_ptr<Notification>( + new Notification(NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title"), + UTF8ToUTF16("message"), gfx::Image() /* icon */, + base::string16() /* display_source */, GURL(), + notifier_id, RichNotificationData(), NULL))); + message_center()->AddNotification(scoped_ptr<Notification>( + new Notification(NOTIFICATION_TYPE_SIMPLE, "id2", UTF8ToUTF16("title"), + UTF8ToUTF16("message"), gfx::Image() /* icon */, + base::string16() /* display_source */, GURL(), + notifier_id, RichNotificationData(), NULL))); EXPECT_EQ(2u, message_center()->GetPopupNotifications().size()); EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size()); @@ -407,31 +393,21 @@ TEST_F(MessageCenterImplTest, NotificationsDuringBlocked) { NotifierId notifier_id(NotifierId::APPLICATION, "app1"); ToggledNotificationBlocker blocker(message_center()); - message_center()->AddNotification(scoped_ptr<Notification>(new Notification( - NOTIFICATION_TYPE_SIMPLE, - "id1", - UTF8ToUTF16("title"), - UTF8ToUTF16("message"), - gfx::Image() /* icon */, - base::string16() /* display_source */, - notifier_id, - RichNotificationData(), - NULL))); + message_center()->AddNotification(scoped_ptr<Notification>( + new Notification(NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title"), + UTF8ToUTF16("message"), gfx::Image() /* icon */, + base::string16() /* display_source */, GURL(), + notifier_id, RichNotificationData(), NULL))); EXPECT_EQ(1u, message_center()->GetPopupNotifications().size()); EXPECT_EQ(1u, message_center()->GetVisibleNotifications().size()); // Create a notification during blocked. Still no popups. blocker.SetNotificationsEnabled(false); - message_center()->AddNotification(scoped_ptr<Notification>(new Notification( - NOTIFICATION_TYPE_SIMPLE, - "id2", - UTF8ToUTF16("title"), - UTF8ToUTF16("message"), - gfx::Image() /* icon */, - base::string16() /* display_source */, - notifier_id, - RichNotificationData(), - NULL))); + message_center()->AddNotification(scoped_ptr<Notification>( + new Notification(NOTIFICATION_TYPE_SIMPLE, "id2", UTF8ToUTF16("title"), + UTF8ToUTF16("message"), gfx::Image() /* icon */, + base::string16() /* display_source */, GURL(), + notifier_id, RichNotificationData(), NULL))); EXPECT_TRUE(message_center()->GetPopupNotifications().empty()); EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size()); @@ -451,26 +427,16 @@ TEST_F(MessageCenterImplTest, NotificationBlockerAllowsPopups) { NotifierId notifier_id2(NotifierId::APPLICATION, "app2"); PopupNotificationBlocker blocker(message_center(), notifier_id2); - message_center()->AddNotification(scoped_ptr<Notification>(new Notification( - NOTIFICATION_TYPE_SIMPLE, - "id1", - UTF8ToUTF16("title"), - UTF8ToUTF16("message"), - gfx::Image() /* icon */, - base::string16() /* display_source */, - notifier_id1, - RichNotificationData(), - NULL))); - message_center()->AddNotification(scoped_ptr<Notification>(new Notification( - NOTIFICATION_TYPE_SIMPLE, - "id2", - UTF8ToUTF16("title"), - UTF8ToUTF16("message"), - gfx::Image() /* icon */, - base::string16() /* display_source */, - notifier_id2, - RichNotificationData(), - NULL))); + message_center()->AddNotification(scoped_ptr<Notification>( + new Notification(NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title"), + UTF8ToUTF16("message"), gfx::Image() /* icon */, + base::string16() /* display_source */, GURL(), + notifier_id1, RichNotificationData(), NULL))); + message_center()->AddNotification(scoped_ptr<Notification>( + new Notification(NOTIFICATION_TYPE_SIMPLE, "id2", UTF8ToUTF16("title"), + UTF8ToUTF16("message"), gfx::Image() /* icon */, + base::string16() /* display_source */, GURL(), + notifier_id2, RichNotificationData(), NULL))); // "id1" is closed but "id2" is still visible as a popup. blocker.SetNotificationsEnabled(false); @@ -480,26 +446,16 @@ TEST_F(MessageCenterImplTest, NotificationBlockerAllowsPopups) { EXPECT_TRUE(PopupNotificationsContain(popups, "id2")); EXPECT_EQ(2u, message_center()->GetVisibleNotifications().size()); - message_center()->AddNotification(scoped_ptr<Notification>(new Notification( - NOTIFICATION_TYPE_SIMPLE, - "id3", - UTF8ToUTF16("title"), - UTF8ToUTF16("message"), - gfx::Image() /* icon */, - base::string16() /* display_source */, - notifier_id1, - RichNotificationData(), - NULL))); - message_center()->AddNotification(scoped_ptr<Notification>(new Notification( - NOTIFICATION_TYPE_SIMPLE, - "id4", - UTF8ToUTF16("title"), - UTF8ToUTF16("message"), - gfx::Image() /* icon */, - base::string16() /* display_source */, - notifier_id2, - RichNotificationData(), - NULL))); + message_center()->AddNotification(scoped_ptr<Notification>( + new Notification(NOTIFICATION_TYPE_SIMPLE, "id3", UTF8ToUTF16("title"), + UTF8ToUTF16("message"), gfx::Image() /* icon */, + base::string16() /* display_source */, GURL(), + notifier_id1, RichNotificationData(), NULL))); + message_center()->AddNotification(scoped_ptr<Notification>( + new Notification(NOTIFICATION_TYPE_SIMPLE, "id4", UTF8ToUTF16("title"), + UTF8ToUTF16("message"), gfx::Image() /* icon */, + base::string16() /* display_source */, GURL(), + notifier_id2, RichNotificationData(), NULL))); popups = message_center()->GetPopupNotifications(); EXPECT_EQ(2u, popups.size()); EXPECT_TRUE(PopupNotificationsContain(popups, "id2")); @@ -523,26 +479,16 @@ TEST_F(MessageCenterImplTest, TotalNotificationBlocker) { NotifierId notifier_id2(NotifierId::APPLICATION, "app2"); TotalNotificationBlocker blocker(message_center(), notifier_id2); - message_center()->AddNotification(scoped_ptr<Notification>(new Notification( - NOTIFICATION_TYPE_SIMPLE, - "id1", - UTF8ToUTF16("title"), - UTF8ToUTF16("message"), - gfx::Image() /* icon */, - base::string16() /* display_source */, - notifier_id1, - RichNotificationData(), - NULL))); - message_center()->AddNotification(scoped_ptr<Notification>(new Notification( - NOTIFICATION_TYPE_SIMPLE, - "id2", - UTF8ToUTF16("title"), - UTF8ToUTF16("message"), - gfx::Image() /* icon */, - base::string16() /* display_source */, - notifier_id2, - RichNotificationData(), - NULL))); + message_center()->AddNotification(scoped_ptr<Notification>( + new Notification(NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title"), + UTF8ToUTF16("message"), gfx::Image() /* icon */, + base::string16() /* display_source */, GURL(), + notifier_id1, RichNotificationData(), NULL))); + message_center()->AddNotification(scoped_ptr<Notification>( + new Notification(NOTIFICATION_TYPE_SIMPLE, "id2", UTF8ToUTF16("title"), + UTF8ToUTF16("message"), gfx::Image() /* icon */, + base::string16() /* display_source */, GURL(), + notifier_id2, RichNotificationData(), NULL))); // "id1" becomes invisible while "id2" is still visible. blocker.SetNotificationsEnabled(false); @@ -552,26 +498,16 @@ TEST_F(MessageCenterImplTest, TotalNotificationBlocker) { EXPECT_FALSE(NotificationsContain(notifications, "id1")); EXPECT_TRUE(NotificationsContain(notifications, "id2")); - message_center()->AddNotification(scoped_ptr<Notification>(new Notification( - NOTIFICATION_TYPE_SIMPLE, - "id3", - UTF8ToUTF16("title"), - UTF8ToUTF16("message"), - gfx::Image() /* icon */, - base::string16() /* display_source */, - notifier_id1, - RichNotificationData(), - NULL))); - message_center()->AddNotification(scoped_ptr<Notification>(new Notification( - NOTIFICATION_TYPE_SIMPLE, - "id4", - UTF8ToUTF16("title"), - UTF8ToUTF16("message"), - gfx::Image() /* icon */, - base::string16() /* display_source */, - notifier_id2, - RichNotificationData(), - NULL))); + message_center()->AddNotification(scoped_ptr<Notification>( + new Notification(NOTIFICATION_TYPE_SIMPLE, "id3", UTF8ToUTF16("title"), + UTF8ToUTF16("message"), gfx::Image() /* icon */, + base::string16() /* display_source */, GURL(), + notifier_id1, RichNotificationData(), NULL))); + message_center()->AddNotification(scoped_ptr<Notification>( + new Notification(NOTIFICATION_TYPE_SIMPLE, "id4", UTF8ToUTF16("title"), + UTF8ToUTF16("message"), gfx::Image() /* icon */, + base::string16() /* display_source */, GURL(), + notifier_id2, RichNotificationData(), NULL))); EXPECT_EQ(2u, message_center()->NotificationCount()); notifications = message_center()->GetVisibleNotifications(); EXPECT_FALSE(NotificationsContain(notifications, "id1")); diff --git a/ui/message_center/message_center_style.h b/ui/message_center/message_center_style.h index 6622b3f..51ab665 100644 --- a/ui/message_center/message_center_style.h +++ b/ui/message_center/message_center_style.h @@ -61,6 +61,11 @@ const int kIconToTextPadding = 16; // H space between icon & title/message. const int kTextTopPadding = 12; // V space between text elements. const int kIconBottomPadding = 16; // Minimum non-zero V space between icon // and frame. +// H space between the context message and the end of the card. +const int kTextRightPadding = 23; +const int kTextLeftPadding = kNotificationIconSize + kIconToTextPadding; +const int kContextMessageViewWidth = + kNotificationWidth - kTextLeftPadding - kTextRightPadding; // Text sizes. const int kTitleFontSize = 14; // For title only. diff --git a/ui/message_center/message_center_tray_unittest.cc b/ui/message_center/message_center_tray_unittest.cc index b86c6f2..1562156 100644 --- a/ui/message_center/message_center_tray_unittest.cc +++ b/ui/message_center/message_center_tray_unittest.cc @@ -69,16 +69,12 @@ class MessageCenterTrayTest : public testing::Test { } void AddNotification(const std::string& id) { - scoped_ptr<Notification> notification( - new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - id, - ASCIIToUTF16("Test Web Notification"), - ASCIIToUTF16("Notification message body."), - gfx::Image(), - ASCIIToUTF16("www.test.org"), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL /* delegate */)); + scoped_ptr<Notification> notification(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, id, + ASCIIToUTF16("Test Web Notification"), + ASCIIToUTF16("Notification message body."), gfx::Image(), + ASCIIToUTF16("www.test.org"), GURL(), DummyNotifierId(), + message_center::RichNotificationData(), NULL /* delegate */)); message_center_->AddNotification(notification.Pass()); } scoped_ptr<MockDelegate> delegate_; @@ -174,16 +170,13 @@ TEST_F(MessageCenterTrayTest, MessageCenterReopenPopupsForSystemPriority) { ASSERT_FALSE(message_center_tray_->popups_visible()); ASSERT_FALSE(message_center_tray_->message_center_visible()); - scoped_ptr<Notification> notification( - new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - "MessageCenterReopnPopupsForSystemPriority", - ASCIIToUTF16("Test Web Notification"), - ASCIIToUTF16("Notification message body."), - gfx::Image(), - ASCIIToUTF16("www.test.org"), - DummyNotifierId(), - message_center::RichNotificationData(), - NULL /* delegate */)); + scoped_ptr<Notification> notification(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, + "MessageCenterReopnPopupsForSystemPriority", + ASCIIToUTF16("Test Web Notification"), + ASCIIToUTF16("Notification message body."), gfx::Image(), + ASCIIToUTF16("www.test.org"), GURL(), DummyNotifierId(), + message_center::RichNotificationData(), NULL /* delegate */)); notification->SetSystemPriority(); message_center_->AddNotification(notification.Pass()); @@ -249,16 +242,12 @@ TEST_F(MessageCenterTrayTest, ContextMenuTest) { NotifierId notifier_id = DummyNotifierId(); NotifierId notifier_id2(NotifierId::APPLICATION, "sample-app"); - scoped_ptr<Notification> notification( - new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - id2, - ASCIIToUTF16("Test Web Notification"), - ASCIIToUTF16("Notification message body."), - gfx::Image(), - base::string16() /* empty display source */, - notifier_id2, - message_center::RichNotificationData(), - NULL /* delegate */)); + scoped_ptr<Notification> notification(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, id2, + ASCIIToUTF16("Test Web Notification"), + ASCIIToUTF16("Notification message body."), gfx::Image(), + base::string16() /* empty display source */, GURL(), notifier_id2, + message_center::RichNotificationData(), NULL /* delegate */)); message_center_->AddNotification(notification.Pass()); AddNotification(id3); diff --git a/ui/message_center/notification.cc b/ui/message_center/notification.cc index 6633a01..af5bc1f 100644 --- a/ui/message_center/notification.cc +++ b/ui/message_center/notification.cc @@ -28,6 +28,7 @@ RichNotificationData::RichNotificationData() : priority(DEFAULT_PRIORITY), never_timeout(false), timestamp(base::Time::Now()), + context_message(base::string16()), progress(0), should_make_spoken_feedback_for_popup_updates(true), clickable(true), @@ -57,6 +58,7 @@ Notification::Notification(NotificationType type, const base::string16& message, const gfx::Image& icon, const base::string16& display_source, + const GURL& origin_url, const NotifierId& notifier_id, const RichNotificationData& optional_fields, NotificationDelegate* delegate) @@ -66,6 +68,7 @@ Notification::Notification(NotificationType type, message_(message), icon_(icon), display_source_(display_source), + origin_url_(origin_url), notifier_id_(notifier_id), serial_number_(g_next_serial_number_++), optional_fields_(optional_fields), @@ -80,13 +83,13 @@ Notification::Notification(const std::string& id, const Notification& other) message_(other.message_), icon_(other.icon_), display_source_(other.display_source_), + origin_url_(other.origin_url_), notifier_id_(other.notifier_id_), serial_number_(other.serial_number_), optional_fields_(other.optional_fields_), shown_as_popup_(other.shown_as_popup_), is_read_(other.is_read_), - delegate_(other.delegate_) { -} + delegate_(other.delegate_) {} Notification::Notification(const Notification& other) : type_(other.type_), @@ -95,6 +98,7 @@ Notification::Notification(const Notification& other) message_(other.message_), icon_(other.icon_), display_source_(other.display_source_), + origin_url_(other.origin_url_), notifier_id_(other.notifier_id_), serial_number_(other.serial_number_), optional_fields_(other.optional_fields_), @@ -109,6 +113,7 @@ Notification& Notification::operator=(const Notification& other) { message_ = other.message_; icon_ = other.icon_; display_source_ = other.display_source_; + origin_url_ = other.origin_url_; notifier_id_ = other.notifier_id_; serial_number_ = other.serial_number_; optional_fields_ = other.optional_fields_; @@ -144,6 +149,11 @@ void Notification::SetSystemPriority() { optional_fields_.never_timeout = true; } +bool Notification::UseOriginAsContextMessage() const { + return optional_fields_.context_message.empty() && origin_url_.is_valid() && + origin_url_.SchemeIsHTTPOrHTTPS(); +} + // static scoped_ptr<Notification> Notification::CreateSystemNotification( const std::string& notification_id, @@ -152,17 +162,12 @@ scoped_ptr<Notification> Notification::CreateSystemNotification( const gfx::Image& icon, const std::string& system_component_id, const base::Closure& click_callback) { - scoped_ptr<Notification> notification( - new Notification( - NOTIFICATION_TYPE_SIMPLE, - notification_id, - title, - message, - icon, - base::string16() /* display_source */, - NotifierId(NotifierId::SYSTEM_COMPONENT, system_component_id), - RichNotificationData(), - new HandleNotificationClickedDelegate(click_callback))); + scoped_ptr<Notification> notification(new Notification( + NOTIFICATION_TYPE_SIMPLE, notification_id, title, message, icon, + base::string16() /* display_source */, GURL(), + NotifierId(NotifierId::SYSTEM_COMPONENT, system_component_id), + RichNotificationData(), + new HandleNotificationClickedDelegate(click_callback))); notification->SetSystemPriority(); return notification.Pass(); } diff --git a/ui/message_center/notification.h b/ui/message_center/notification.h index 122cd67..3b61862 100644 --- a/ui/message_center/notification.h +++ b/ui/message_center/notification.h @@ -16,6 +16,7 @@ #include "ui/message_center/notification_delegate.h" #include "ui/message_center/notification_types.h" #include "ui/message_center/notifier_settings.h" +#include "url/gurl.h" namespace message_center { @@ -62,6 +63,7 @@ class MESSAGE_CENTER_EXPORT Notification { const base::string16& message, const gfx::Image& icon, const base::string16& display_source, + const GURL& origin_url, const NotifierId& notifier_id, const RichNotificationData& optional_fields, NotificationDelegate* delegate); @@ -92,6 +94,11 @@ class MESSAGE_CENTER_EXPORT Notification { const base::string16& message() const { return message_; } void set_message(const base::string16& message) { message_ = message; } + // The origin URL of the script which requested the notification. + // Can be empty if the notification is requested by an extension or + // Chrome app. + const GURL& origin_url() const { return origin_url_; } + // A display string for the source of the notification. const base::string16& display_source() const { return display_source_; } @@ -123,13 +130,17 @@ class MESSAGE_CENTER_EXPORT Notification { optional_fields_.timestamp = timestamp; } - const base::string16& context_message() const { + const base::string16 context_message() const { return optional_fields_.context_message; } + void set_context_message(const base::string16& context_message) { optional_fields_.context_message = context_message; } + // Decides if the notification origin should be used as a context message + bool UseOriginAsContextMessage() const; + const std::vector<NotificationItem>& items() const { return optional_fields_.items; } @@ -233,6 +244,10 @@ class MESSAGE_CENTER_EXPORT Notification { base::string16 display_source_; private: + // The origin URL of the script which requested the notification. + // Can be empty if requested through a chrome app or extension or if + // it's a system notification. + GURL origin_url_; NotifierId notifier_id_; unsigned serial_number_; RichNotificationData optional_fields_; diff --git a/ui/message_center/notification_list_unittest.cc b/ui/message_center/notification_list_unittest.cc index fe8c1f4..a3e84cd 100644 --- a/ui/message_center/notification_list_unittest.cc +++ b/ui/message_center/notification_list_unittest.cc @@ -53,14 +53,11 @@ class NotificationListTest : public testing::Test { std::string* id_out) { *id_out = base::StringPrintf(kIdFormat, counter_); scoped_ptr<Notification> notification(new Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - *id_out, + message_center::NOTIFICATION_TYPE_SIMPLE, *id_out, UTF8ToUTF16(base::StringPrintf(kTitleFormat, counter_)), - UTF8ToUTF16(base::StringPrintf(kMessageFormat, counter_)), - gfx::Image(), - UTF8ToUTF16(kDisplaySource), - NotifierId(NotifierId::APPLICATION, kExtensionId), - optional_fields, + UTF8ToUTF16(base::StringPrintf(kMessageFormat, counter_)), gfx::Image(), + UTF8ToUTF16(kDisplaySource), GURL(), + NotifierId(NotifierId::APPLICATION, kExtensionId), optional_fields, NULL)); return notification.Pass(); } @@ -186,15 +183,11 @@ TEST_F(NotificationListTest, UpdateNotification) { std::string replaced = id0 + "_replaced"; EXPECT_EQ(1u, notification_list()->NotificationCount(blockers())); scoped_ptr<Notification> notification( - new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - replaced, - UTF8ToUTF16("newtitle"), - UTF8ToUTF16("newbody"), - gfx::Image(), - UTF8ToUTF16(kDisplaySource), + new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, replaced, + UTF8ToUTF16("newtitle"), UTF8ToUTF16("newbody"), + gfx::Image(), UTF8ToUTF16(kDisplaySource), GURL(), NotifierId(NotifierId::APPLICATION, kExtensionId), - message_center::RichNotificationData(), - NULL)); + message_center::RichNotificationData(), NULL)); notification_list()->UpdateNotificationMessage(id0, notification.Pass()); EXPECT_EQ(1u, notification_list()->NotificationCount(blockers())); const NotificationList::Notifications notifications = @@ -209,66 +202,35 @@ TEST_F(NotificationListTest, GetNotificationsByNotifierId) { NotifierId id1(NotifierId::APPLICATION, "ext1"); NotifierId id2(GURL("http://example.com")); NotifierId id3(NotifierId::SYSTEM_COMPONENT, "system-notifier"); - scoped_ptr<Notification> notification( - new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - "id0", - UTF8ToUTF16("title0"), - UTF8ToUTF16("message0"), - gfx::Image(), - UTF8ToUTF16("source0"), - id0, - message_center::RichNotificationData(), - NULL)); + scoped_ptr<Notification> notification(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, "id0", UTF8ToUTF16("title0"), + UTF8ToUTF16("message0"), gfx::Image(), UTF8ToUTF16("source0"), GURL(), + id0, message_center::RichNotificationData(), NULL)); notification_list()->AddNotification(notification.Pass()); - notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - "id1", - UTF8ToUTF16("title1"), - UTF8ToUTF16("message1"), - gfx::Image(), - UTF8ToUTF16("source0"), - id0, - message_center::RichNotificationData(), - NULL)); + notification.reset(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title1"), + UTF8ToUTF16("message1"), gfx::Image(), UTF8ToUTF16("source0"), GURL(), + id0, message_center::RichNotificationData(), NULL)); notification_list()->AddNotification(notification.Pass()); - notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - "id2", - UTF8ToUTF16("title1"), - UTF8ToUTF16("message1"), - gfx::Image(), - UTF8ToUTF16("source1"), - id0, - message_center::RichNotificationData(), - NULL)); + notification.reset(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, "id2", UTF8ToUTF16("title1"), + UTF8ToUTF16("message1"), gfx::Image(), UTF8ToUTF16("source1"), GURL(), + id0, message_center::RichNotificationData(), NULL)); notification_list()->AddNotification(notification.Pass()); - notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - "id3", - UTF8ToUTF16("title1"), - UTF8ToUTF16("message1"), - gfx::Image(), - UTF8ToUTF16("source2"), - id1, - message_center::RichNotificationData(), - NULL)); + notification.reset(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, "id3", UTF8ToUTF16("title1"), + UTF8ToUTF16("message1"), gfx::Image(), UTF8ToUTF16("source2"), GURL(), + id1, message_center::RichNotificationData(), NULL)); notification_list()->AddNotification(notification.Pass()); - notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - "id4", - UTF8ToUTF16("title1"), - UTF8ToUTF16("message1"), - gfx::Image(), - UTF8ToUTF16("source2"), - id2, - message_center::RichNotificationData(), - NULL)); + notification.reset(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, "id4", UTF8ToUTF16("title1"), + UTF8ToUTF16("message1"), gfx::Image(), UTF8ToUTF16("source2"), GURL(), + id2, message_center::RichNotificationData(), NULL)); notification_list()->AddNotification(notification.Pass()); - notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - "id5", - UTF8ToUTF16("title1"), - UTF8ToUTF16("message1"), - gfx::Image(), - UTF8ToUTF16("source2"), - id3, - message_center::RichNotificationData(), - NULL)); + notification.reset(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, "id5", UTF8ToUTF16("title1"), + UTF8ToUTF16("message1"), gfx::Image(), UTF8ToUTF16("source2"), GURL(), + id3, message_center::RichNotificationData(), NULL)); notification_list()->AddNotification(notification.Pass()); NotificationList::Notifications by_notifier_id = @@ -418,16 +380,11 @@ TEST_F(NotificationListTest, PriorityPromotion) { EXPECT_EQ(0u, GetPopupCounts()); message_center::RichNotificationData optional; optional.priority = 1; - scoped_ptr<Notification> notification( - new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - replaced, - UTF8ToUTF16("newtitle"), - UTF8ToUTF16("newbody"), - gfx::Image(), - UTF8ToUTF16(kDisplaySource), - NotifierId(NotifierId::APPLICATION, kExtensionId), - optional, - NULL)); + scoped_ptr<Notification> notification(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, replaced, + UTF8ToUTF16("newtitle"), UTF8ToUTF16("newbody"), gfx::Image(), + UTF8ToUTF16(kDisplaySource), GURL(), + NotifierId(NotifierId::APPLICATION, kExtensionId), optional, NULL)); notification_list()->UpdateNotificationMessage(id0, notification.Pass()); EXPECT_EQ(1u, notification_list()->NotificationCount(blockers())); EXPECT_EQ(1u, GetPopupCounts()); @@ -449,47 +406,30 @@ TEST_F(NotificationListTest, PriorityPromotionWithPopups) { // id0 promoted to LOW->DEFAULT, it'll appear as toast (popup). message_center::RichNotificationData priority; priority.priority = DEFAULT_PRIORITY; - scoped_ptr<Notification> notification( - new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - id0, - UTF8ToUTF16("newtitle"), - UTF8ToUTF16("newbody"), - gfx::Image(), - UTF8ToUTF16(kDisplaySource), - NotifierId(NotifierId::APPLICATION, kExtensionId), - priority, - NULL)); + scoped_ptr<Notification> notification(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, id0, UTF8ToUTF16("newtitle"), + UTF8ToUTF16("newbody"), gfx::Image(), UTF8ToUTF16(kDisplaySource), GURL(), + NotifierId(NotifierId::APPLICATION, kExtensionId), priority, NULL)); notification_list()->UpdateNotificationMessage(id0, notification.Pass()); EXPECT_EQ(1u, GetPopupCounts()); notification_list()->MarkSinglePopupAsShown(id0, true); EXPECT_EQ(0u, GetPopupCounts()); // update with no promotion change for id0, it won't appear as a toast. - notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - id0, - UTF8ToUTF16("newtitle2"), - UTF8ToUTF16("newbody2"), - gfx::Image(), - UTF8ToUTF16(kDisplaySource), - NotifierId(NotifierId::APPLICATION, - kExtensionId), - priority, - NULL)); + notification.reset(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, id0, UTF8ToUTF16("newtitle2"), + UTF8ToUTF16("newbody2"), gfx::Image(), UTF8ToUTF16(kDisplaySource), + GURL(), NotifierId(NotifierId::APPLICATION, kExtensionId), priority, + NULL)); notification_list()->UpdateNotificationMessage(id0, notification.Pass()); EXPECT_EQ(0u, GetPopupCounts()); // id1 promoted to DEFAULT->HIGH, it'll appear as toast (popup). priority.priority = HIGH_PRIORITY; - notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - id1, - UTF8ToUTF16("newtitle"), - UTF8ToUTF16("newbody"), - gfx::Image(), - UTF8ToUTF16(kDisplaySource), - NotifierId(NotifierId::APPLICATION, - kExtensionId), - priority, - NULL)); + notification.reset(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, id1, UTF8ToUTF16("newtitle"), + UTF8ToUTF16("newbody"), gfx::Image(), UTF8ToUTF16(kDisplaySource), GURL(), + NotifierId(NotifierId::APPLICATION, kExtensionId), priority, NULL)); notification_list()->UpdateNotificationMessage(id1, notification.Pass()); EXPECT_EQ(1u, GetPopupCounts()); notification_list()->MarkSinglePopupAsShown(id1, true); @@ -497,16 +437,11 @@ TEST_F(NotificationListTest, PriorityPromotionWithPopups) { // id1 promoted to HIGH->MAX, it'll appear as toast again. priority.priority = MAX_PRIORITY; - notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - id1, - UTF8ToUTF16("newtitle2"), - UTF8ToUTF16("newbody2"), - gfx::Image(), - UTF8ToUTF16(kDisplaySource), - NotifierId(NotifierId::APPLICATION, - kExtensionId), - priority, - NULL)); + notification.reset(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, id1, UTF8ToUTF16("newtitle2"), + UTF8ToUTF16("newbody2"), gfx::Image(), UTF8ToUTF16(kDisplaySource), + GURL(), NotifierId(NotifierId::APPLICATION, kExtensionId), priority, + NULL)); notification_list()->UpdateNotificationMessage(id1, notification.Pass()); EXPECT_EQ(1u, GetPopupCounts()); notification_list()->MarkSinglePopupAsShown(id1, true); @@ -514,32 +449,23 @@ TEST_F(NotificationListTest, PriorityPromotionWithPopups) { // id1 demoted to MAX->DEFAULT, no appearing as toast. priority.priority = DEFAULT_PRIORITY; - notification.reset(new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - id1, - UTF8ToUTF16("newtitle3"), - UTF8ToUTF16("newbody3"), - gfx::Image(), - UTF8ToUTF16(kDisplaySource), - NotifierId(NotifierId::APPLICATION, - kExtensionId), - priority, - NULL)); + notification.reset(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, id1, UTF8ToUTF16("newtitle3"), + UTF8ToUTF16("newbody3"), gfx::Image(), UTF8ToUTF16(kDisplaySource), + GURL(), NotifierId(NotifierId::APPLICATION, kExtensionId), priority, + NULL)); notification_list()->UpdateNotificationMessage(id1, notification.Pass()); EXPECT_EQ(0u, GetPopupCounts()); } TEST_F(NotificationListTest, WebNotificationUpdatePromotion) { std::string notification_id = "replaced-web-notification"; - scoped_ptr<Notification> original_notification( - new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - notification_id, - UTF8ToUTF16("Web Notification"), - UTF8ToUTF16("Notification contents"), - gfx::Image(), - UTF8ToUTF16(kDisplaySource), - NotifierId(GURL("https://example.com/")), - message_center::RichNotificationData(), - NULL)); + scoped_ptr<Notification> original_notification(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, notification_id, + UTF8ToUTF16("Web Notification"), UTF8ToUTF16("Notification contents"), + gfx::Image(), UTF8ToUTF16(kDisplaySource), GURL(), + NotifierId(GURL("https://example.com/")), + message_center::RichNotificationData(), NULL)); EXPECT_EQ(0u, GetPopupCounts()); notification_list()->AddNotification(original_notification.Pass()); @@ -548,16 +474,13 @@ TEST_F(NotificationListTest, WebNotificationUpdatePromotion) { notification_list()->MarkSinglePopupAsShown(notification_id, true); EXPECT_EQ(0u, GetPopupCounts()); - scoped_ptr<Notification> replaced_notification( - new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - notification_id, - UTF8ToUTF16("Web Notification Replacement"), - UTF8ToUTF16("New notification contents"), - gfx::Image(), - UTF8ToUTF16(kDisplaySource), - NotifierId(GURL("https://example.com/")), - message_center::RichNotificationData(), - NULL)); + scoped_ptr<Notification> replaced_notification(new Notification( + message_center::NOTIFICATION_TYPE_SIMPLE, notification_id, + UTF8ToUTF16("Web Notification Replacement"), + UTF8ToUTF16("New notification contents"), gfx::Image(), + UTF8ToUTF16(kDisplaySource), GURL(), + NotifierId(GURL("https://example.com/")), + message_center::RichNotificationData(), NULL)); // Web Notifications will be re-shown as popups even if their priority didn't // change, to match the behavior of the Web Notification API. @@ -659,15 +582,11 @@ TEST_F(NotificationListTest, UpdateAfterMarkedAsShown) { const std::string replaced("test-replaced-id"); scoped_ptr<Notification> notification( - new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, - replaced, - UTF8ToUTF16("newtitle"), - UTF8ToUTF16("newbody"), - gfx::Image(), - UTF8ToUTF16(kDisplaySource), + new Notification(message_center::NOTIFICATION_TYPE_SIMPLE, replaced, + UTF8ToUTF16("newtitle"), UTF8ToUTF16("newbody"), + gfx::Image(), UTF8ToUTF16(kDisplaySource), GURL(), NotifierId(NotifierId::APPLICATION, kExtensionId), - message_center::RichNotificationData(), - NULL)); + message_center::RichNotificationData(), NULL)); notification_list()->UpdateNotificationMessage(id1, notification.Pass()); n1 = GetNotification(id1); EXPECT_TRUE(n1 == NULL); @@ -705,15 +624,9 @@ TEST_F(NotificationListTest, UnreadCountNoNegative) { // Updates the notification and verifies unread_count doesn't change. scoped_ptr<Notification> updated_notification(new Notification( - message_center::NOTIFICATION_TYPE_SIMPLE, - id, - UTF8ToUTF16("updated"), - UTF8ToUTF16("updated"), - gfx::Image(), - base::string16(), - NotifierId(), - RichNotificationData(), - NULL)); + message_center::NOTIFICATION_TYPE_SIMPLE, id, UTF8ToUTF16("updated"), + UTF8ToUTF16("updated"), gfx::Image(), base::string16(), GURL(), + NotifierId(), RichNotificationData(), NULL)); notification_list()->AddNotification(updated_notification.Pass()); EXPECT_EQ(1u, notification_list()->UnreadCount(blockers())); } @@ -740,15 +653,9 @@ TEST_F(NotificationListTest, TestHasNotificationOfType) { id, message_center::NOTIFICATION_TYPE_PROGRESS)); scoped_ptr<Notification> updated_notification(new Notification( - message_center::NOTIFICATION_TYPE_PROGRESS, - id, - UTF8ToUTF16("updated"), - UTF8ToUTF16("updated"), - gfx::Image(), - base::string16(), - NotifierId(), - RichNotificationData(), - NULL)); + message_center::NOTIFICATION_TYPE_PROGRESS, id, UTF8ToUTF16("updated"), + UTF8ToUTF16("updated"), gfx::Image(), base::string16(), GURL(), + NotifierId(), RichNotificationData(), NULL)); notification_list()->AddNotification(updated_notification.Pass()); EXPECT_FALSE(notification_list()->HasNotificationOfType( diff --git a/ui/message_center/views/constants.h b/ui/message_center/views/constants.h index 5bd55a4..a8cc80d 100644 --- a/ui/message_center/views/constants.h +++ b/ui/message_center/views/constants.h @@ -21,9 +21,7 @@ const SkColor kContextTextBackgroundColor = SK_ColorWHITE; const int kIconSize = message_center::kNotificationIconSize; const int kLegacyIconSize = 40; -const int kTextLeftPadding = kIconSize + message_center::kIconToTextPadding; const int kTextBottomPadding = 12; -const int kTextRightPadding = 23; const int kItemTitleToMessagePadding = 3; const int kButtonVecticalPadding = 0; const int kButtonTitleTopPadding = 0; diff --git a/ui/message_center/views/message_center_view_unittest.cc b/ui/message_center/views/message_center_view_unittest.cc index 273c01e..0953f53 100644 --- a/ui/message_center/views/message_center_view_unittest.cc +++ b/ui/message_center/views/message_center_view_unittest.cc @@ -129,15 +129,12 @@ MessageCenterViewTest::~MessageCenterViewTest() { void MessageCenterViewTest::SetUp() { // Create a dummy notification. - Notification notification(NOTIFICATION_TYPE_SIMPLE, - std::string("notification id"), - base::UTF8ToUTF16("title"), - base::UTF8ToUTF16("message"), - gfx::Image(), - base::UTF8ToUTF16("display source"), - NotifierId(NotifierId::APPLICATION, "extension_id"), - message_center::RichNotificationData(), - NULL); + Notification notification( + NOTIFICATION_TYPE_SIMPLE, std::string("notification id"), + base::UTF8ToUTF16("title"), base::UTF8ToUTF16("message"), gfx::Image(), + base::UTF8ToUTF16("display source"), GURL(), + NotifierId(NotifierId::APPLICATION, "extension_id"), + message_center::RichNotificationData(), NULL); // ...and a list for it. NotificationList::Notifications notifications; diff --git a/ui/message_center/views/message_popup_collection_unittest.cc b/ui/message_center/views/message_popup_collection_unittest.cc index 74934dd..3c67e3c 100644 --- a/ui/message_center/views/message_popup_collection_unittest.cc +++ b/ui/message_center/views/message_popup_collection_unittest.cc @@ -94,16 +94,11 @@ class MessagePopupCollectionTest : public views::ViewsTestBase { std::string AddNotification() { std::string id = base::IntToString(id_++); - scoped_ptr<Notification> notification( - new Notification(NOTIFICATION_TYPE_BASE_FORMAT, - id, - base::UTF8ToUTF16("test title"), - base::UTF8ToUTF16("test message"), - gfx::Image(), - base::string16() /* display_source */, - NotifierId(), - message_center::RichNotificationData(), - NULL /* delegate */)); + scoped_ptr<Notification> notification(new Notification( + NOTIFICATION_TYPE_BASE_FORMAT, id, base::UTF8ToUTF16("test title"), + base::UTF8ToUTF16("test message"), gfx::Image(), + base::string16() /* display_source */, GURL(), NotifierId(), + message_center::RichNotificationData(), NULL /* delegate */)); MessageCenter::Get()->AddNotification(notification.Pass()); return id; } diff --git a/ui/message_center/views/notification_view.cc b/ui/message_center/views/notification_view.cc index 27c8acc..fdff875 100644 --- a/ui/message_center/views/notification_view.cc +++ b/ui/message_center/views/notification_view.cc @@ -8,6 +8,7 @@ #include "base/stl_util.h" #include "base/strings/string_util.h" #include "base/strings/utf_string_conversions.h" +#include "components/url_formatter/elide_url.h" #include "third_party/skia/include/core/SkPaint.h" #include "third_party/skia/include/core/SkPath.h" #include "ui/base/cursor/cursor.h" @@ -41,6 +42,7 @@ #include "ui/views/painter.h" #include "ui/views/view_targeter.h" #include "ui/views/widget/widget.h" +#include "url/gurl.h" namespace { @@ -285,11 +287,11 @@ views::View* NotificationView::TargetForRect(views::View* root, void NotificationView::CreateOrUpdateViews(const Notification& notification) { CreateOrUpdateTitleView(notification); CreateOrUpdateMessageView(notification); - CreateOrUpdateContextMessageView(notification); CreateOrUpdateProgressBarView(notification); CreateOrUpdateListItemViews(notification); CreateOrUpdateIconView(notification); CreateOrUpdateImageView(notification); + CreateOrUpdateContextMessageView(notification); CreateOrUpdateActionButtonViews(notification); } @@ -553,9 +555,23 @@ void NotificationView::CreateOrUpdateMessageView( message_view_->SetVisible(!notification.items().size()); } +base::string16 NotificationView::FormatContextMessage( + const Notification& notification) const { + if (notification.UseOriginAsContextMessage()) { + const GURL url = notification.origin_url(); + DCHECK(url.is_valid()); + return url_formatter::ElideHost(url, views::Label().font_list(), + kContextMessageViewWidth); + } + + return gfx::TruncateString(notification.context_message(), + kContextMessageCharacterLimit, gfx::WORD_BREAK); +} + void NotificationView::CreateOrUpdateContextMessageView( const Notification& notification) { - if (notification.context_message().empty()) { + if (notification.context_message().empty() && + !notification.UseOriginAsContextMessage()) { if (context_message_view_) { // Deletion will also remove |context_message_view_| from its parent. delete context_message_view_; @@ -566,12 +582,11 @@ void NotificationView::CreateOrUpdateContextMessageView( DCHECK(top_view_ != NULL); - base::string16 text = gfx::TruncateString(notification.context_message(), - kContextMessageCharacterLimit, - gfx::WORD_BREAK); + base::string16 message = FormatContextMessage(notification); + if (!context_message_view_) { int padding = kMessageLineHeight - views::Label().font_list().GetHeight(); - context_message_view_ = new BoundedLabel(text); + context_message_view_ = new BoundedLabel(message); context_message_view_->SetLineLimit( message_center::kContextMessageLineLimit); context_message_view_->SetLineHeight(kMessageLineHeight); @@ -580,7 +595,7 @@ void NotificationView::CreateOrUpdateContextMessageView( context_message_view_->SetBorder(MakeTextBorder(padding, 4, 0)); top_view_->AddChildView(context_message_view_); } else { - context_message_view_->SetText(text); + context_message_view_->SetText(message); } } diff --git a/ui/message_center/views/notification_view.h b/ui/message_center/views/notification_view.h index 95bf7c2..c7612cc 100644 --- a/ui/message_center/views/notification_view.h +++ b/ui/message_center/views/notification_view.h @@ -11,6 +11,8 @@ #include "ui/message_center/views/message_view.h" #include "ui/views/view_targeter_delegate.h" +class GURL; + namespace views { class ProgressBar; } @@ -73,6 +75,7 @@ class MESSAGE_CENTER_EXPORT NotificationView private: FRIEND_TEST_ALL_PREFIXES(NotificationViewTest, CreateOrUpdateTest); + FRIEND_TEST_ALL_PREFIXES(NotificationViewTest, FormatContextMessageTest); FRIEND_TEST_ALL_PREFIXES(NotificationViewTest, TestLineLimits); FRIEND_TEST_ALL_PREFIXES(NotificationViewTest, TestIconSizing); FRIEND_TEST_ALL_PREFIXES(NotificationViewTest, TestImageSizing); @@ -99,6 +102,12 @@ class MESSAGE_CENTER_EXPORT NotificationView int GetMessageLineLimit(int title_lines, int width) const; int GetMessageHeight(int width, int limit) const; + // Formats the context message to be displayed based on |context| + // so it shows as much information as possible + // given the space available in the ContextMessage section of the + // notification. + base::string16 FormatContextMessage(const Notification& notification) const; + MessageCenterController* controller_; // Weak, lives longer then views. // Describes whether the view should display a hand pointer or not. diff --git a/ui/message_center/views/notification_view_unittest.cc b/ui/message_center/views/notification_view_unittest.cc index 054dfbd..bf6dc2e 100644 --- a/ui/message_center/views/notification_view_unittest.cc +++ b/ui/message_center/views/notification_view_unittest.cc @@ -165,16 +165,11 @@ void NotificationViewTest::SetUp() { // Create a dummy notification. SkBitmap bitmap; data_.reset(new RichNotificationData()); - notification_.reset( - new Notification(NOTIFICATION_TYPE_BASE_FORMAT, - std::string("notification id"), - base::UTF8ToUTF16("title"), - base::UTF8ToUTF16("message"), - CreateTestImage(80, 80), - base::UTF8ToUTF16("display source"), - NotifierId(NotifierId::APPLICATION, "extension_id"), - *data_, - NULL)); + notification_.reset(new Notification( + NOTIFICATION_TYPE_BASE_FORMAT, std::string("notification id"), + base::UTF8ToUTF16("title"), base::UTF8ToUTF16("message"), + CreateTestImage(80, 80), base::UTF8ToUTF16("display source"), GURL(), + NotifierId(NotifierId::APPLICATION, "extension_id"), *data_, NULL)); notification_->set_small_image(CreateTestImage(16, 16)); notification_->set_image(CreateTestImage(320, 240)); @@ -267,7 +262,7 @@ TEST_F(NotificationViewTest, TestLineLimits) { EXPECT_EQ(2, notification_view()->GetMessageLineLimit(1, 360)); EXPECT_EQ(1, notification_view()->GetMessageLineLimit(2, 360)); - notification()->set_context_message(base::UTF8ToUTF16("foo")); + notification()->set_context_message(base::ASCIIToUTF16("foo")); notification_view()->CreateOrUpdateViews(*notification()); EXPECT_TRUE(notification_view()->context_message_view_ != NULL); @@ -461,4 +456,75 @@ TEST_F(NotificationViewTest, ViewOrderingTest) { CheckVerticalOrderInNotification(); } +TEST_F(NotificationViewTest, FormatContextMessageTest) { + const std::string kRegularContextText = "Context Text"; + const std::string kVeryLongContextText = + "VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY" + "VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY" + "VERY VERY VERY VERY Long Long Long Long Long Long Long Long context"; + + const std::string kVeryLongElidedContextText = + "VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERYVERY VERY " + "VERY VERY VERY VERY VERY VERY VERY VERY VERY\xE2\x80\xA6"; + + const std::string kChromeUrl = "chrome://settings"; + const std::string kUrlContext = "http://chromium.org/hello"; + const std::string kHostContext = "chromium.org"; + const std::string kLongUrlContext = + "https://" + "veryveryveryveryveyryveryveryveryveryveyryveryvery.veryveryveyrylong." + "chromium.org/hello"; + + Notification notification1( + NOTIFICATION_TYPE_BASE_FORMAT, std::string(""), base::UTF8ToUTF16(""), + base::UTF8ToUTF16(""), CreateTestImage(80, 80), base::UTF8ToUTF16(""), + GURL(), message_center::NotifierId(GURL()), *data(), NULL); + notification1.set_context_message(base::ASCIIToUTF16(kRegularContextText)); + + base::string16 result = + notification_view()->FormatContextMessage(notification1); + EXPECT_EQ(kRegularContextText, base::UTF16ToUTF8(result)); + + notification1.set_context_message(base::ASCIIToUTF16(kVeryLongContextText)); + result = notification_view()->FormatContextMessage(notification1); + EXPECT_EQ(kVeryLongElidedContextText, base::UTF16ToUTF8(result)); + + Notification notification2( + NOTIFICATION_TYPE_BASE_FORMAT, std::string(""), base::UTF8ToUTF16(""), + base::UTF8ToUTF16(""), CreateTestImage(80, 80), base::UTF8ToUTF16(""), + GURL(kUrlContext), message_center::NotifierId(GURL()), *data(), NULL); + notification2.set_context_message(base::ASCIIToUTF16("")); + + result = notification_view()->FormatContextMessage(notification2); + EXPECT_EQ(kHostContext, base::UTF16ToUTF8(result)); + + // Non http url and empty context message should yield an empty context + // message. + Notification notification3( + NOTIFICATION_TYPE_BASE_FORMAT, std::string(""), base::UTF8ToUTF16(""), + base::UTF8ToUTF16(""), CreateTestImage(80, 80), base::UTF8ToUTF16(""), + GURL(kChromeUrl), message_center::NotifierId(GURL()), *data(), NULL); + notification3.set_context_message(base::ASCIIToUTF16("")); + result = notification_view()->FormatContextMessage(notification3); + EXPECT_TRUE(result.empty()); + + // Long http url should be elided + Notification notification4( + NOTIFICATION_TYPE_BASE_FORMAT, std::string(""), base::UTF8ToUTF16(""), + base::UTF8ToUTF16(""), CreateTestImage(80, 80), base::UTF8ToUTF16(""), + GURL(kLongUrlContext), message_center::NotifierId(GURL()), *data(), NULL); + notification4.set_context_message(base::ASCIIToUTF16("")); + result = notification_view()->FormatContextMessage(notification4); + + // Different platforms elide at different lengths so we do + // some generic checking here. + // The url has been elided (it starts with an ellipsis) + // The end of the domainsuffix is shown + // the url piece is not shown + EXPECT_TRUE(base::UTF16ToUTF8(result).find( + ".veryveryveyrylong.chromium.org") != std::string::npos); + EXPECT_TRUE(base::UTF16ToUTF8(result).find("\xE2\x80\xA6") == 0); + EXPECT_TRUE(base::UTF16ToUTF8(result).find("hello") == std::string::npos); +} + } // namespace message_center |