From 0195319b7538b095b5f0bcfe8138f5081adf203c Mon Sep 17 00:00:00 2001 From: Michaela Laurencin <35157522+mlaurencin@users.noreply.github.com> Date: Fri, 9 Oct 2020 08:26:39 -0700 Subject: [PATCH] fix: notifications successfully never timeout with included flag (#25820) Co-authored-by: mlaurencin --- .../win/windows_toast_notification.cc | 139 +++++++++++++++++- 1 file changed, 132 insertions(+), 7 deletions(-) diff --git a/shell/browser/notifications/win/windows_toast_notification.cc b/shell/browser/notifications/win/windows_toast_notification.cc index fec97c986720..4f0791518515 100644 --- a/shell/browser/notifications/win/windows_toast_notification.cc +++ b/shell/browser/notifications/win/windows_toast_notification.cc @@ -21,6 +21,8 @@ #include "shell/browser/notifications/win/notification_presenter_win.h" #include "shell/browser/win/scoped_hstring.h" #include "shell/common/application_info.h" +#include "ui/base/l10n/l10n_util.h" +#include "ui/strings/grit/ui_strings.h" using ABI::Windows::Data::Xml::Dom::IXmlAttribute; using ABI::Windows::Data::Xml::Dom::IXmlDocument; @@ -245,8 +247,8 @@ HRESULT WindowsToastNotification::SetXmlScenarioReminder(IXmlDocument* doc) { RETURN_IF_FAILED(node_list->Item(0, &root)); // get attributes of root "toast" node - ComPtr attributes; - RETURN_IF_FAILED(root->get_Attributes(&attributes)); + ComPtr toast_attributes; + RETURN_IF_FAILED(root->get_Attributes(&toast_attributes)); ComPtr scenario_attribute; ScopedHString scenario_str(L"scenario"); @@ -265,13 +267,136 @@ HRESULT WindowsToastNotification::SetXmlScenarioReminder(IXmlDocument* doc) { ComPtr scenario_node; RETURN_IF_FAILED(scenario_text.As(&scenario_node)); - ComPtr child_node; - RETURN_IF_FAILED( - scenario_attribute_node->AppendChild(scenario_node.Get(), &child_node)); + ComPtr scenario_backup_node; + RETURN_IF_FAILED(scenario_attribute_node->AppendChild(scenario_node.Get(), + &scenario_backup_node)); ComPtr scenario_attribute_pnode; - return attributes.Get()->SetNamedItem(scenario_attribute_node.Get(), - &scenario_attribute_pnode); + RETURN_IF_FAILED(toast_attributes.Get()->SetNamedItem( + scenario_attribute_node.Get(), &scenario_attribute_pnode)); + + // Create "actions" wrapper + ComPtr actions_wrapper_element; + ScopedHString actions_wrapper_str(L"actions"); + RETURN_IF_FAILED( + doc->CreateElement(actions_wrapper_str, &actions_wrapper_element)); + + ComPtr actions_wrapper_node_tmp; + RETURN_IF_FAILED(actions_wrapper_element.As(&actions_wrapper_node_tmp)); + + // Append actions wrapper node to toast xml + ComPtr actions_wrapper_node; + RETURN_IF_FAILED( + root->AppendChild(actions_wrapper_node_tmp.Get(), &actions_wrapper_node)); + + ComPtr attributes_actions_wrapper; + RETURN_IF_FAILED( + actions_wrapper_node->get_Attributes(&attributes_actions_wrapper)); + + // Add a "Dismiss" button + // Create "action" tag + ComPtr action_element; + ScopedHString action_str(L"action"); + RETURN_IF_FAILED(doc->CreateElement(action_str, &action_element)); + + ComPtr action_node_tmp; + RETURN_IF_FAILED(action_element.As(&action_node_tmp)); + + // Append action node to actions wrapper in toast xml + ComPtr action_node; + RETURN_IF_FAILED( + actions_wrapper_node->AppendChild(action_node_tmp.Get(), &action_node)); + + // Setup attributes for action + ComPtr action_attributes; + RETURN_IF_FAILED(action_node->get_Attributes(&action_attributes)); + + // Create activationType attribute + ComPtr activation_type_attribute; + ScopedHString activation_type_str(L"activationType"); + RETURN_IF_FAILED( + doc->CreateAttribute(activation_type_str, &activation_type_attribute)); + + ComPtr activation_type_attribute_node; + RETURN_IF_FAILED( + activation_type_attribute.As(&activation_type_attribute_node)); + + // Set activationType attribute to system + ScopedHString activation_type_value(L"system"); + if (!activation_type_value.success()) + return E_FAIL; + + ComPtr activation_type_text; + RETURN_IF_FAILED( + doc->CreateTextNode(activation_type_value, &activation_type_text)); + + ComPtr activation_type_node; + RETURN_IF_FAILED(activation_type_text.As(&activation_type_node)); + + ComPtr activation_type_backup_node; + RETURN_IF_FAILED(activation_type_attribute_node->AppendChild( + activation_type_node.Get(), &activation_type_backup_node)); + + // Add activation type to the action attributes + ComPtr activation_type_attribute_pnode; + RETURN_IF_FAILED(action_attributes.Get()->SetNamedItem( + activation_type_attribute_node.Get(), &activation_type_attribute_pnode)); + + // Create arguments attribute + ComPtr arguments_attribute; + ScopedHString arguments_str(L"arguments"); + RETURN_IF_FAILED(doc->CreateAttribute(arguments_str, &arguments_attribute)); + + ComPtr arguments_attribute_node; + RETURN_IF_FAILED(arguments_attribute.As(&arguments_attribute_node)); + + // Set arguments attribute to dismiss + ScopedHString arguments_value(L"dismiss"); + if (!arguments_value.success()) + return E_FAIL; + + ComPtr arguments_text; + RETURN_IF_FAILED(doc->CreateTextNode(arguments_value, &arguments_text)); + + ComPtr arguments_node; + RETURN_IF_FAILED(arguments_text.As(&arguments_node)); + + ComPtr arguments_backup_node; + RETURN_IF_FAILED(arguments_attribute_node->AppendChild( + arguments_node.Get(), &arguments_backup_node)); + + // Add arguments to the action attributes + ComPtr arguments_attribute_pnode; + RETURN_IF_FAILED(action_attributes.Get()->SetNamedItem( + arguments_attribute_node.Get(), &arguments_attribute_pnode)); + + // Create content attribute + ComPtr content_attribute; + ScopedHString content_str(L"content"); + RETURN_IF_FAILED(doc->CreateAttribute(content_str, &content_attribute)); + + ComPtr content_attribute_node; + RETURN_IF_FAILED(content_attribute.As(&content_attribute_node)); + + // Set content attribute to Dismiss + ScopedHString content_value(l10n_util::GetStringUTF16(IDS_APP_CLOSE)); + if (!content_value.success()) + return E_FAIL; + + ComPtr content_text; + RETURN_IF_FAILED(doc->CreateTextNode(content_value, &content_text)); + + ComPtr content_node; + RETURN_IF_FAILED(content_text.As(&content_node)); + + ComPtr content_backup_node; + RETURN_IF_FAILED(content_attribute_node->AppendChild(content_node.Get(), + &content_backup_node)); + + // Add content to the action attributes + ComPtr content_attribute_pnode; + return action_attributes.Get()->SetNamedItem(content_attribute_node.Get(), + &content_attribute_pnode); } HRESULT WindowsToastNotification::SetXmlAudioSilent(IXmlDocument* doc) {