Commit f139811a authored by thomasanderson's avatar thomasanderson Committed by Commit bot

Linux native notifications: Add attribution

This CL adds the origin url to the notification body (if one exists),
similar to how Chrome notifications do currently.

This is one of the requirements specified in [1] (Google only).

[1] https://docs.google.com/a/google.com/document/d/1cqXPpIyCf1RaEYx9XCPjZbO6U5oDeyslajznnG9wd_U/edit?usp=sharing

BUG=676220
R=peter@chromium.org,thestig@chromium.org
TBR=mukai@chromium.org

Review-Url: https://codereview.chromium.org/2876603004
Cr-Commit-Position: refs/heads/master@{#472160}
parent d760627f
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/shell_integration_linux.h" #include "chrome/browser/shell_integration_linux.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "components/url_formatter/elide_url.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h" #include "content/public/browser/notification_service.h"
#include "dbus/bus.h" #include "dbus/bus.h"
...@@ -136,6 +137,7 @@ int NotificationPriorityToFdoUrgency(int priority) { ...@@ -136,6 +137,7 @@ int NotificationPriorityToFdoUrgency(int priority) {
// the image does not need to be resized, or the image is empty, // the image does not need to be resized, or the image is empty,
// returns |image| directly. // returns |image| directly.
gfx::Image ResizeImageToFdoMaxSize(const gfx::Image& image) { gfx::Image ResizeImageToFdoMaxSize(const gfx::Image& image) {
DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (image.IsEmpty()) if (image.IsEmpty())
return image; return image;
int width = image.Width(); int width = image.Width();
...@@ -519,12 +521,32 @@ class NotificationPlatformBridgeLinuxImpl ...@@ -519,12 +521,32 @@ class NotificationPlatformBridgeLinuxImpl
if (base::ContainsKey(capabilities_, kCapabilityBody)) { if (base::ContainsKey(capabilities_, kCapabilityBody)) {
const bool body_markup = const bool body_markup =
base::ContainsKey(capabilities_, kCapabilityBodyMarkup); base::ContainsKey(capabilities_, kCapabilityBodyMarkup);
std::string message = base::UTF16ToUTF8(notification->message());
if (body_markup) { if (notification->UseOriginAsContextMessage()) {
base::ReplaceSubstringsAfterOffset(&message, 0, "&", "&"); std::string url_display_text = net::EscapeForHTML(
base::ReplaceSubstringsAfterOffset(&message, 0, "<", "&lt;"); base::UTF16ToUTF8(url_formatter::FormatUrlForSecurityDisplay(
base::ReplaceSubstringsAfterOffset(&message, 0, ">", "&gt;"); notification->origin_url(),
url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS)));
if (base::ContainsKey(capabilities_, kCapabilityBodyHyperlinks)) {
body << "<a href=\""
<< net::EscapePath(notification->origin_url().spec()) << "\">"
<< url_display_text << "</a>";
} else {
body << url_display_text;
}
} else if (!notification->context_message().empty()) {
std::string context =
base::UTF16ToUTF8(notification->context_message());
if (body_markup)
context = net::EscapeForHTML(context);
body << context;
} }
std::string message = base::UTF16ToUTF8(notification->message());
if (body_markup)
message = net::EscapeForHTML(message);
if (body.tellp())
body << "\n";
body << message; body << message;
if (notification->type() == message_center::NOTIFICATION_TYPE_MULTIPLE) { if (notification->type() == message_center::NOTIFICATION_TYPE_MULTIPLE) {
......
...@@ -61,11 +61,21 @@ class NotificationBuilder { ...@@ -61,11 +61,21 @@ class NotificationBuilder {
return *this; return *this;
} }
NotificationBuilder& SetMessage(const base::string16& message) {
notification_.set_message(message);
return *this;
}
NotificationBuilder& SetNeverTimeout(bool never_timeout) { NotificationBuilder& SetNeverTimeout(bool never_timeout) {
notification_.set_never_timeout(never_timeout); notification_.set_never_timeout(never_timeout);
return *this; return *this;
} }
NotificationBuilder& SetOriginUrl(const GURL& origin_url) {
notification_.set_origin_url(origin_url);
return *this;
}
NotificationBuilder& SetProgress(int progress) { NotificationBuilder& SetProgress(int progress) {
notification_.set_progress(progress); notification_.set_progress(progress);
return *this; return *this;
...@@ -218,8 +228,8 @@ class NotificationPlatformBridgeLinuxTest : public testing::Test { ...@@ -218,8 +228,8 @@ class NotificationPlatformBridgeLinuxTest : public testing::Test {
EXPECT_CALL(*mock_notification_proxy_.get(), EXPECT_CALL(*mock_notification_proxy_.get(),
MockCallMethodAndBlock(Calls("GetCapabilities"), _)) MockCallMethodAndBlock(Calls("GetCapabilities"), _))
.WillOnce( .WillOnce(OnGetCapabilities(std::vector<std::string>{
OnGetCapabilities(std::vector<std::string>{"body", "body-images"})); "body", "body-hyperlinks", "body-images", "body-markup"}));
EXPECT_CALL(*mock_notification_proxy_.get(), EXPECT_CALL(*mock_notification_proxy_.get(),
MockCallMethodAndBlock(Calls("GetServerInformation"), _)) MockCallMethodAndBlock(Calls("GetServerInformation"), _))
...@@ -311,7 +321,7 @@ TEST_F(NotificationPlatformBridgeLinuxTest, NotificationListItemsInBody) { ...@@ -311,7 +321,7 @@ TEST_F(NotificationPlatformBridgeLinuxTest, NotificationListItemsInBody) {
MockCallMethodAndBlock(Calls("Notify"), _)) MockCallMethodAndBlock(Calls("Notify"), _))
.WillOnce(OnNotify( .WillOnce(OnNotify(
[](const NotificationRequest& request) { [](const NotificationRequest& request) {
EXPECT_EQ("abc - 123\ndef - 456", request.body); EXPECT_EQ("<b>abc</b> 123\n<b>def</b> 456", request.body);
}, },
1)); 1));
...@@ -390,3 +400,24 @@ TEST_F(NotificationPlatformBridgeLinuxTest, NotificationImages) { ...@@ -390,3 +400,24 @@ TEST_F(NotificationPlatformBridgeLinuxTest, NotificationImages) {
.SetImage(original_image) .SetImage(original_image)
.GetResult()); .GetResult());
} }
TEST_F(NotificationPlatformBridgeLinuxTest, NotificationAttribution) {
EXPECT_CALL(*mock_notification_proxy_.get(),
MockCallMethodAndBlock(Calls("Notify"), _))
.WillOnce(OnNotify(
[](const NotificationRequest& request) {
EXPECT_EQ(
"<a href=\"https%3A//google.com/"
"search%3Fq=test&ie=UTF8\">google.com</a>\nBody text",
request.body);
},
1));
CreateNotificationBridgeLinux();
notification_bridge_linux_->Display(
NotificationCommon::PERSISTENT, "", "", false,
NotificationBuilder("")
.SetMessage(base::ASCIIToUTF16("Body text"))
.SetOriginUrl(GURL("https://google.com/search?q=test&ie=UTF8"))
.GetResult());
}
...@@ -129,6 +129,7 @@ class MESSAGE_CENTER_EXPORT Notification { ...@@ -129,6 +129,7 @@ class MESSAGE_CENTER_EXPORT Notification {
// Can be empty if the notification is requested by an extension or // Can be empty if the notification is requested by an extension or
// Chrome app. // Chrome app.
const GURL& origin_url() const { return origin_url_; } const GURL& origin_url() const { return origin_url_; }
void set_origin_url(const GURL& origin_url) { origin_url_ = origin_url; }
// A display string for the source of the notification. // A display string for the source of the notification.
const base::string16& display_source() const { return display_source_; } const base::string16& display_source() const { return display_source_; }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment