Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding/Decoding AppNotificationBuilder arguments #2805

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions dev/AppNotifications/AppNotificationActivatedEventArgs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#include "pch.h"
#include "AppNotificationActivatedEventArgs.h"
#include "Microsoft.Windows.AppNotifications.AppNotificationActivatedEventArgs.g.cpp"
#include "AppNotificationBuilder/AppNotificationBuilderUtility.h"

namespace winrt::Microsoft::Windows::AppNotifications::implementation
{
winrt::Windows::Foundation::Collections::IMap<hstring, hstring> AppNotificationActivatedEventArgs::DecodeArguments(std::wstring arguments)
pmpurifoy marked this conversation as resolved.
Show resolved Hide resolved
pmpurifoy marked this conversation as resolved.
Show resolved Hide resolved
{
auto result{ winrt::single_threaded_map<winrt::hstring, winrt::hstring>() };

std::vector<std::wstring> pairs{};
size_t pos{ 0 };

// Separate the key/value pairs by ';' as the delimiter
while ((pos = arguments.find(L';')) != std::wstring::npos) {
pairs.push_back(arguments.substr(0, pos));
arguments.erase(0, pos + 1);
}

// Need to push back final string
pairs.push_back(arguments);

for (auto pair : pairs)
{
// Get the key/value individual values separated by '='
pos = pair.find(L'=');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use tokenizers here too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There can only be one '=' per token. Find is simpler/better here since we aren't expecting more than 1 delimiter.

if (pos == std::wstring::npos)
{
result.Insert(Decode(pair).c_str(), L"");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So is it expected to return empty string as the value for a pair? Can we get away with not putting in anything here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Result is a pair of <hstring, hstring. As fas I know there is anything else we can put in this to indicate no value. insert() insists on an hstring won't accept a nullptr.

And since this is part of the IDL, we can really change it to an optional<>

Copy link
Contributor Author

@pmpurifoy pmpurifoy Aug 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you can use optional in midl 3, but I think it's fine to have an empty "" value. Developers doing AddArgument(L"Key", L""), is valid. The toolkit allows this behavior as well. In the future, we can add AddArgument(key) for a more explicit "I want an empty value", but even then we would store it as { L"key", L"" }.

}
else
{
result.Insert(Decode(pair.substr(0, pos)).c_str(), Decode(pair.substr(pos + 1)).c_str());
pmpurifoy marked this conversation as resolved.
Show resolved Hide resolved
}
}
return result;
}
}
13 changes: 9 additions & 4 deletions dev/AppNotifications/AppNotificationActivatedEventArgs.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#pragma once
Expand All @@ -10,12 +10,17 @@ namespace winrt::Microsoft::Windows::AppNotifications::implementation
{
AppNotificationActivatedEventArgs() = default;

AppNotificationActivatedEventArgs(winrt::hstring const& arguments, winrt::Windows::Foundation::Collections::IMap<winrt::hstring, winrt::hstring> const& userInput) : m_arguments(arguments), m_userInput(userInput) {};
winrt::hstring Argument() { return m_arguments; };
AppNotificationActivatedEventArgs(winrt::hstring const& argument, winrt::Windows::Foundation::Collections::IMap<winrt::hstring, winrt::hstring> const& userInput)
: m_argument(argument), m_userInput(userInput), m_arguments(DecodeArguments(argument.c_str())) {};
winrt::hstring Argument() { return m_argument; };
winrt::Windows::Foundation::Collections::IMap<hstring, hstring> UserInput() { return m_userInput; };
winrt::Windows::Foundation::Collections::IMap<hstring, hstring> Arguments() { return m_arguments; };

private:
winrt::hstring m_arguments;
winrt::Windows::Foundation::Collections::IMap<hstring, hstring> DecodeArguments(std::wstring arguments);

winrt::hstring m_argument;
winrt::Windows::Foundation::Collections::IMap<winrt::hstring, winrt::hstring> m_userInput;
winrt::Windows::Foundation::Collections::IMap<winrt::hstring, winrt::hstring> m_arguments{ nullptr };
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation
{
THROW_HR_IF_MSG(E_INVALIDARG, key.empty(), "You must provide a key when adding an argument");

m_arguments.Insert(key, value);
m_arguments.Insert(Encode(key.c_str()), Encode(value.c_str()));
return *this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#pragma once
#include "pch.h"
#include "winrt/Microsoft.Windows.AppNotifications.Builder.h"
#include <algorithm>
#include <regex>
#include <map>

constexpr size_t c_maxAppNotificationPayload{ 5120 };
constexpr uint8_t c_maxTextElements{ 3 };
Expand All @@ -14,6 +16,12 @@ namespace AppNotificationBuilder
using namespace winrt::Microsoft::Windows::AppNotifications::Builder;
}

inline std::map<wchar_t, PCWSTR> GetCharacterEncodings()
{
static std::map<wchar_t, PCWSTR> encodings = { { L'%', L"%25"}, { L';', L"%3B"}, { L'=', L"%3D"}};
return encodings;
}

inline PCWSTR GetWinSoundEventString(AppNotificationBuilder::AppNotificationSoundEvent soundEvent)
{
switch (soundEvent)
Expand Down Expand Up @@ -70,3 +78,38 @@ inline PCWSTR GetWinSoundEventString(AppNotificationBuilder::AppNotificationSoun
return L"ms-winsoundevent:Notification.Default";
}
}

inline std::wstring Encode(std::wstring const& value)
{
std::wstring encodedValue{};

auto encodings{ GetCharacterEncodings() };
for (auto ch : value)
{
if(encodings.find(ch) != encodings.end())
{
encodedValue.append(encodings[ch]);
}
else
{
encodedValue.push_back(ch);
}
}
return encodedValue;
}

// Decoding process based off the Windows Community Toolkit:
// https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/rel/7.1.0/Microsoft.Toolkit.Uwp.Notifications/Toasts/ToastArguments.cs#L389inline
inline std::wstring Decode(std::wstring const& value)
{
std::wstring result{ value };

// Need to unescape special characters
for (auto encoding : GetCharacterEncodings())
{
std::wstring specialCharacter{ encoding.first };
result = std::regex_replace(result, std::wregex(encoding.second), specialCharacter);
}

return result;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "AppNotificationButton.h"
#include "Microsoft.Windows.AppNotifications.Builder.AppNotificationButton.g.cpp"
#include <IsWindowsVersion.h>
#include "AppNotificationBuilderUtility.h"

namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation
{
Expand All @@ -25,7 +26,7 @@ namespace winrt::Microsoft::Windows::AppNotifications::Builder::implementation
THROW_HR_IF_MSG(E_INVALIDARG, key.empty(), "You must provide a key when adding an argument");
THROW_HR_IF_MSG(E_INVALIDARG, m_protocolUri, "You cannot add an argument after calling SetInvokeUri");

m_arguments.Insert(key, value);
m_arguments.Insert(Encode(key.c_str()), Encode(value.c_str()));
return *this;
}

Expand Down
6 changes: 5 additions & 1 deletion dev/AppNotifications/AppNotifications.idl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "..\AppLifecycle\AppLifecycle.idl";

namespace Microsoft.Windows.AppNotifications
{
[contractversion(2)]
[contractversion(3)]
apicontract AppNotificationsContract {}

// Event args for the Notification Activation
Expand All @@ -16,6 +16,10 @@ namespace Microsoft.Windows.AppNotifications

// The data from the input elements of a Notification like a TextBox
Windows.Foundation.Collections.IMap<String, String> UserInput{ get; };

// Arguments from the invoked button built from AppNotificationBuilder
[contract(AppNotificationsContract, 3)]
Windows.Foundation.Collections.IMap<String, String> Arguments{ get; };
}

// Notification Progress Data
Expand Down
1 change: 1 addition & 0 deletions dev/AppNotifications/AppNotifications.vcxitems
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<ClInclude Include="$(MSBuildThisFileDirectory)ShellLocalization.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MSBuildThisFileDirectory)AppNotificationActivatedEventArgs.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)AppNotificationManager.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)AppNotification.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)AppNotificationProgressData.cpp" />
Expand Down
Loading