Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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_template_builder.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "base/strings/string_util.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "components/url_formatter/elide_url.h" | |
| 11 #include "third_party/libxml/chromium/libxml_utils.h" | |
| 12 #include "ui/message_center/notification.h" | |
| 13 #include "url/origin.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // Constants used for the XML element names and their attributes. | |
| 18 const char kToastElement[] = "toast"; | |
| 19 const char kToastElementLaunchAttribute[] = "launch"; | |
| 20 const char kVisualElement[] = "visual"; | |
| 21 const char kBindingElement[] = "binding"; | |
| 22 const char kBindingElementTemplateAttribute[] = "template"; | |
| 23 const char kTextElement[] = "text"; | |
| 24 const char kTextElementIdAttribute[] = "id"; | |
| 25 | |
| 26 // Name of the template used for default Chrome notifications. | |
| 27 // https://msdn.microsoft.com/library/1a437614-4259-426b-8e3f-ca57368b2e7a | |
| 28 const char kDefaultTemplate[] = "ToastText04"; | |
| 29 | |
| 30 // The XML version header that has to be stripped from the output. | |
| 31 const char kXmlVersionHeader[] = "<?xml version=\"1.0\"?>\n"; | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 // static | |
| 36 std::unique_ptr<NotificationTemplateBuilder> NotificationTemplateBuilder::Build( | |
| 37 const std::string& notification_id, | |
| 38 const message_center::Notification& notification) { | |
| 39 std::unique_ptr<NotificationTemplateBuilder> builder = | |
| 40 base::WrapUnique(new NotificationTemplateBuilder); | |
| 41 | |
| 42 // TODO(finnur): Can we set <toast scenario="reminder"> for notifications | |
| 43 // that have set the never_timeout() flag? | |
| 44 builder->StartToastElement(notification_id); | |
| 45 builder->StartVisualElement(); | |
| 46 | |
| 47 // TODO(finnur): Set the correct binding template based on the |notification|. | |
| 48 builder->StartBindingElement(kDefaultTemplate); | |
| 49 | |
| 50 // Content for the ToastText04 toast template. | |
| 51 // https://msdn.microsoft.com/library/1a437614-4259-426b-8e3f-ca57368b2e7a#Toa stText04 | |
| 52 builder->WriteTextElement("1", base::UTF16ToUTF8(notification.title())); | |
| 53 builder->WriteTextElement("2", base::UTF16ToUTF8(notification.message())); | |
| 54 builder->WriteTextElement("3", | |
| 55 builder->FormatOrigin(notification.origin_url())); | |
| 56 | |
| 57 builder->EndBindingElement(); | |
| 58 | |
| 59 builder->EndVisualElement(); | |
| 60 builder->EndToastElement(); | |
|
Peter Beverloo
2017/06/05 17:06:25
An alternative would be to use comments:
builder-
Finnur
2017/06/06 11:00:09
EndToastElement(), you mean?
I don't know... Seem
Peter Beverloo
2017/06/16 15:40:21
OK, we'll leave as is, this is super simple to cha
| |
| 61 | |
| 62 return builder; | |
| 63 } | |
| 64 | |
| 65 NotificationTemplateBuilder::NotificationTemplateBuilder() | |
| 66 : xml_writer_(base::MakeUnique<XmlWriter>()) { | |
| 67 xml_writer_->StartWriting(); | |
| 68 } | |
| 69 | |
| 70 NotificationTemplateBuilder::~NotificationTemplateBuilder() = default; | |
| 71 | |
| 72 std::string NotificationTemplateBuilder::GetNotificationTemplate() { | |
| 73 std::string template_xml = xml_writer_->GetWrittenString(); | |
| 74 DCHECK(base::StartsWith(template_xml, kXmlVersionHeader, | |
| 75 base::CompareCase::SENSITIVE)); | |
| 76 | |
| 77 // The |kXmlVersionHeader| is automatically appended by libxml, but the toast | |
| 78 // systen in the Windows Action Center expects it to be absent. | |
| 79 return template_xml.substr(sizeof(kXmlVersionHeader) - 1); | |
|
Peter Beverloo
2017/06/05 17:06:25
:(
| |
| 80 } | |
| 81 | |
| 82 std::string NotificationTemplateBuilder::FormatOrigin( | |
| 83 const GURL& origin) const { | |
| 84 base::string16 origin_string = url_formatter::FormatOriginForSecurityDisplay( | |
| 85 url::Origin(origin), url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS); | |
| 86 DCHECK(origin_string.size()); | |
| 87 | |
| 88 return base::UTF16ToUTF8(origin_string); | |
| 89 } | |
| 90 | |
| 91 void NotificationTemplateBuilder::StartToastElement( | |
| 92 const std::string& notification_id) { | |
| 93 xml_writer_->StartElement(kToastElement); | |
| 94 xml_writer_->AddAttribute(kToastElementLaunchAttribute, notification_id); | |
| 95 } | |
| 96 | |
| 97 void NotificationTemplateBuilder::EndToastElement() { | |
| 98 xml_writer_->EndElement(); | |
| 99 xml_writer_->StopWriting(); | |
| 100 } | |
| 101 | |
| 102 void NotificationTemplateBuilder::StartVisualElement() { | |
| 103 xml_writer_->StartElement(kVisualElement); | |
| 104 } | |
| 105 | |
| 106 void NotificationTemplateBuilder::EndVisualElement() { | |
| 107 xml_writer_->EndElement(); | |
| 108 } | |
| 109 | |
| 110 void NotificationTemplateBuilder::StartBindingElement( | |
| 111 const std::string& template_name) { | |
| 112 xml_writer_->StartElement(kBindingElement); | |
| 113 xml_writer_->AddAttribute(kBindingElementTemplateAttribute, template_name); | |
| 114 } | |
| 115 | |
| 116 void NotificationTemplateBuilder::EndBindingElement() { | |
| 117 xml_writer_->EndElement(); | |
| 118 } | |
| 119 | |
| 120 void NotificationTemplateBuilder::WriteTextElement(const std::string& id, | |
| 121 const std::string& content) { | |
| 122 xml_writer_->StartElement(kTextElement); | |
| 123 xml_writer_->AddAttribute(kTextElementIdAttribute, id); | |
| 124 xml_writer_->AppendElementContent(content); | |
| 125 xml_writer_->EndElement(); | |
| 126 } | |
| OLD | NEW |