Commit 0d633b3b authored by Xi Cheng's avatar Xi Cheng Committed by Commit Bot

Simplify class NotificationTemplateBuilder to a single public function

Bug: 888276
Change-Id: Ibd3e557ad55ff48daf1e3347ec27ff6fd235a47f
Reviewed-on: https://chromium-review.googlesource.com/c/1311837
Commit-Queue: Xi Cheng <chengx@chromium.org>
Reviewed-by: default avatarGreg Thompson <grt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#605223}
parent 159af3ae
...@@ -146,13 +146,13 @@ class NotificationPlatformBridgeWinImpl ...@@ -146,13 +146,13 @@ class NotificationPlatformBridgeWinImpl
image_retainer_->GetCleanupTask()); image_retainer_->GetCleanupTask());
} }
// Obtain an IToastNotification interface from a given XML (provided by the // Obtain an IToastNotification interface from a given XML as in
// NotificationTemplateBuilder). This function is only used when displaying // |xml_template|. This function is only used when displaying notification in
// notification in production code, which explains why the UMA metrics record // production code, which explains why the UMA metrics record within are
// within are classified with the display path. // classified with the display path.
mswr::ComPtr<winui::Notifications::IToastNotification> GetToastNotification( mswr::ComPtr<winui::Notifications::IToastNotification> GetToastNotification(
const message_center::Notification& notification, const message_center::Notification& notification,
const NotificationTemplateBuilder& notification_template_builder, const base::string16& xml_template,
const std::string& profile_id, const std::string& profile_id,
bool incognito) { bool incognito) {
ScopedHString ref_class_name = ScopedHString ref_class_name =
...@@ -176,9 +176,7 @@ class NotificationPlatformBridgeWinImpl ...@@ -176,9 +176,7 @@ class NotificationPlatformBridgeWinImpl
return nullptr; return nullptr;
} }
base::string16 notification_template = ScopedHString ref_template = ScopedHString::Create(xml_template);
notification_template_builder.GetNotificationTemplate();
ScopedHString ref_template = ScopedHString::Create(notification_template);
hr = document_io->LoadXml(ref_template.get()); hr = document_io->LoadXml(ref_template.get());
if (FAILED(hr)) { if (FAILED(hr)) {
LogDisplayHistogram(DisplayStatus::LOAD_XML_FAILED); LogDisplayHistogram(DisplayStatus::LOAD_XML_FAILED);
...@@ -350,11 +348,10 @@ class NotificationPlatformBridgeWinImpl ...@@ -350,11 +348,10 @@ class NotificationPlatformBridgeWinImpl
NotificationLaunchId launch_id(notification_type, notification->id(), NotificationLaunchId launch_id(notification_type, notification->id(),
profile_id, incognito, profile_id, incognito,
notification->origin_url()); notification->origin_url());
std::unique_ptr<NotificationTemplateBuilder> notification_template = base::string16 xml_template = BuildNotificationTemplate(
NotificationTemplateBuilder::Build(image_retainer_.get(), launch_id, image_retainer_.get(), launch_id, *notification);
*notification);
mswr::ComPtr<winui::Notifications::IToastNotification> toast = mswr::ComPtr<winui::Notifications::IToastNotification> toast =
GetToastNotification(*notification, *notification_template, profile_id, GetToastNotification(*notification, xml_template, profile_id,
incognito); incognito);
if (!toast) if (!toast)
return; return;
...@@ -906,9 +903,9 @@ void NotificationPlatformBridgeWin::SetNotifierForTesting( ...@@ -906,9 +903,9 @@ void NotificationPlatformBridgeWin::SetNotifierForTesting(
mswr::ComPtr<winui::Notifications::IToastNotification> mswr::ComPtr<winui::Notifications::IToastNotification>
NotificationPlatformBridgeWin::GetToastNotificationForTesting( NotificationPlatformBridgeWin::GetToastNotificationForTesting(
const message_center::Notification& notification, const message_center::Notification& notification,
const NotificationTemplateBuilder& notification_template_builder, const base::string16& xml_template,
const std::string& profile_id, const std::string& profile_id,
bool incognito) { bool incognito) {
return impl_->GetToastNotification( return impl_->GetToastNotification(notification, xml_template, profile_id,
notification, notification_template_builder, profile_id, incognito); incognito);
} }
...@@ -80,7 +80,7 @@ class NotificationPlatformBridgeWin : public NotificationPlatformBridge { ...@@ -80,7 +80,7 @@ class NotificationPlatformBridgeWin : public NotificationPlatformBridge {
Microsoft::WRL::ComPtr<ABI::Windows::UI::Notifications::IToastNotification> Microsoft::WRL::ComPtr<ABI::Windows::UI::Notifications::IToastNotification>
GetToastNotificationForTesting( GetToastNotificationForTesting(
const message_center::Notification& notification, const message_center::Notification& notification,
const NotificationTemplateBuilder& notification_template_builder, const base::string16& xml_template,
const std::string& profile_id, const std::string& profile_id,
bool incognito); bool incognito);
......
...@@ -73,12 +73,11 @@ class NotificationPlatformBridgeWinTest : public testing::Test { ...@@ -73,12 +73,11 @@ class NotificationPlatformBridgeWinTest : public testing::Test {
message_center::RichNotificationData(), nullptr /* delegate */); message_center::RichNotificationData(), nullptr /* delegate */);
notification->set_renotify(renotify); notification->set_renotify(renotify);
MockNotificationImageRetainer image_retainer; MockNotificationImageRetainer image_retainer;
std::unique_ptr<NotificationTemplateBuilder> builder = base::string16 xml_template =
NotificationTemplateBuilder::Build(&image_retainer, launch_id, BuildNotificationTemplate(&image_retainer, launch_id, *notification);
*notification);
mswr::ComPtr<winui::Notifications::IToastNotification> toast = mswr::ComPtr<winui::Notifications::IToastNotification> toast =
bridge->GetToastNotificationForTesting(*notification, *builder, bridge->GetToastNotificationForTesting(*notification, xml_template,
profile_id, incognito); profile_id, incognito);
if (!toast) { if (!toast) {
LOG(ERROR) << "GetToastNotificationForTesting failed"; LOG(ERROR) << "GetToastNotificationForTesting failed";
......
...@@ -5,26 +5,12 @@ ...@@ -5,26 +5,12 @@
#ifndef CHROME_BROWSER_NOTIFICATIONS_WIN_NOTIFICATION_TEMPLATE_BUILDER_H_ #ifndef CHROME_BROWSER_NOTIFICATIONS_WIN_NOTIFICATION_TEMPLATE_BUILDER_H_
#define CHROME_BROWSER_NOTIFICATIONS_WIN_NOTIFICATION_TEMPLATE_BUILDER_H_ #define CHROME_BROWSER_NOTIFICATIONS_WIN_NOTIFICATION_TEMPLATE_BUILDER_H_
#include <memory>
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "chrome/browser/notifications/notification_common.h"
class GURL;
class NotificationLaunchId; class NotificationLaunchId;
class XmlWriter;
namespace gfx {
class Image;
}
namespace message_center { namespace message_center {
struct ButtonInfo;
class Notification; class Notification;
struct NotificationItem;
} // namespace message_center } // namespace message_center
class NotificationImageRetainer; class NotificationImageRetainer;
...@@ -35,115 +21,16 @@ extern const char kNotificationToastElement[]; ...@@ -35,115 +21,16 @@ extern const char kNotificationToastElement[];
// The Notification Launch attribute name in the toast XML. // The Notification Launch attribute name in the toast XML.
extern const char kNotificationLaunchAttribute[]; extern const char kNotificationLaunchAttribute[];
// Builds XML-based notification templates for displaying a given notification // Builds XML-based notification template for displaying a given |notification|
// in the Windows Action Center. // in the Windows Action Center.
//
// https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-adaptive-interactive-toasts // https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-adaptive-interactive-toasts
// base::string16 BuildNotificationTemplate(
// libXml was preferred (over WinXml, which the samples tend to use) because it NotificationImageRetainer* image_retainer,
// is used frequently in Chrome, is nicer to use and has already been vetted. const NotificationLaunchId& launch_id,
class NotificationTemplateBuilder { const message_center::Notification& notification);
public:
// Builds the notification template for the given |notification|. // Sets the label of the context menu for testing. The caller owns |label| and
static std::unique_ptr<NotificationTemplateBuilder> Build( // is responsible for resetting the override back to nullptr.
NotificationImageRetainer* image_retainer, void SetContextMenuLabelForTesting(const char* label);
const NotificationLaunchId& launch_id,
const message_center::Notification& notification);
// Set label for the context menu item in testing. The caller owns |label| and
// is responsible for resetting the override back to nullptr.
static void OverrideContextMenuLabelForTesting(const char* label);
~NotificationTemplateBuilder();
// Gets the XML template that was created by this builder.
base::string16 GetNotificationTemplate() const;
private:
// The different types of text nodes to output.
enum class TextType { NORMAL, ATTRIBUTION };
NotificationTemplateBuilder();
// Formats the |origin| for display in the notification template.
std::string FormatOrigin(const GURL& origin) const;
// Writes the <toast> element with a given |launch_attribute|.
// Also closes the |xml_writer_| for writing as the toast is now complete.
void StartToastElement(const NotificationLaunchId& launch_id,
const message_center::Notification& notification);
void EndToastElement();
// Writes the <visual> element.
void StartVisualElement();
void EndVisualElement();
// Writes the <binding> element with the given |template_name|.
void StartBindingElement(const std::string& template_name);
void EndBindingElement();
// Writes the <text> element with the given |content|. If |text_type| is
// ATTRIBUTION then |content| is treated as the source that the notification
// is attributed to.
void WriteTextElement(const std::string& content, TextType text_type);
// Writes the <text> element containing the list entries.
void WriteItems(const std::vector<message_center::NotificationItem>& items);
// Writes the <image> element for the notification icon.
void WriteIconElement(NotificationImageRetainer* image_retainer,
const message_center::Notification& notification);
// Writes the <image> element for showing a large image within the
// notification body.
void WriteLargeImageElement(NotificationImageRetainer* image_retainer,
const message_center::Notification& notification);
// A helper for constructing image xml.
void WriteImageElement(NotificationImageRetainer* image_retainer,
const gfx::Image& image,
const std::string& placement,
const std::string& hint_crop);
// Adds a progress bar to the notification XML.
void WriteProgressElement(const message_center::Notification& notification);
// Writes the <actions> element.
void StartActionsElement();
void EndActionsElement();
// Writes the <audio silent="true"> element.
void WriteAudioSilentElement();
// Fills in the details for the actions (the buttons the notification
// contains).
void AddActions(NotificationImageRetainer* image_retainer,
const message_center::Notification& notification,
const NotificationLaunchId& launch_id);
void WriteActionElement(NotificationImageRetainer* image_retainer,
const message_center::ButtonInfo& button,
int index,
NotificationLaunchId copied_launch_id);
// Adds context menu actions to the notification sent by |origin|.
void AddContextMenu(NotificationLaunchId copied_launch_id);
void WriteContextMenuElement(const std::string& content,
const std::string& arguments);
// Ensures that every reminder has at least one button, as the Action Center
// does not respect the Reminder setting on notifications with no buttons, so
// we must add a Dismiss button to the notification for those cases. For more
// details, see issue https://crbug.com/781792.
void EnsureReminderHasButton(const message_center::Notification& notification,
NotificationLaunchId copied_launch_id);
// Label to override context menu items in tests.
static const char* context_menu_label_override_;
// The XML writer to which the template will be written.
std::unique_ptr<XmlWriter> xml_writer_;
DISALLOW_COPY_AND_ASSIGN(NotificationTemplateBuilder);
};
#endif // CHROME_BROWSER_NOTIFICATIONS_WIN_NOTIFICATION_TEMPLATE_BUILDER_H_ #endif // CHROME_BROWSER_NOTIFICATIONS_WIN_NOTIFICATION_TEMPLATE_BUILDER_H_
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