Skip to content

Add validations for storage pickers #5619

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions dev/Interop/StoragePickers/FileOpenPicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
}
void FileOpenPicker::ViewMode(winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode const& value)
{
PickerCommon::ValidateViewMode(value);
m_viewMode = value;
}
hstring FileOpenPicker::SettingsIdentifier()
Expand All @@ -36,6 +37,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
}
void FileOpenPicker::SettingsIdentifier(hstring const& value)
{
PickerCommon::ValidateStringNoEmbeddedNulls(value);
m_settingsIdentifier = value;
}
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId FileOpenPicker::SuggestedStartLocation()
Expand All @@ -44,6 +46,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
}
void FileOpenPicker::SuggestedStartLocation(winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId const& value)
{
PickerCommon::ValidateSuggestedStartLocation(value);
m_suggestedStartLocation = value;
}
winrt::hstring FileOpenPicker::CommitButtonText()
Expand All @@ -52,6 +55,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
}
void FileOpenPicker::CommitButtonText(winrt::hstring const& value)
{
PickerCommon::ValidateStringNoEmbeddedNulls(value);
m_commitButtonText = value;
}
winrt::Windows::Foundation::Collections::IVector<hstring> FileOpenPicker::FileTypeFilter()
Expand Down
6 changes: 5 additions & 1 deletion dev/Interop/StoragePickers/FileOpenPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "Microsoft.Windows.Storage.Pickers.FileOpenPicker.g.h"
#include "PickerCommon.h"
#include "StoragePickersTelemetryHelper.h"
#include <winrt/Windows.Foundation.Collections.h>
#include "FileTypeFilterVector.h"

namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
{
Expand Down Expand Up @@ -35,7 +37,9 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
winrt::hstring m_settingsIdentifier{};
PickerLocationId m_suggestedStartLocation{ PickerLocationId::Unspecified };
winrt::hstring m_commitButtonText{};
winrt::Windows::Foundation::Collections::IVector<hstring> m_fileTypeFilter{ winrt::single_threaded_vector<hstring>() };

winrt::Windows::Foundation::Collections::IVector<hstring> m_fileTypeFilter{ make<FileTypeFilterVector>() };

StoragePickersTelemetryHelper m_telemetryHelper{};

void CaptureParameters(PickerCommon::PickerParameters& parameters);
Expand Down
6 changes: 5 additions & 1 deletion dev/Interop/StoragePickers/FileSavePicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
}
void FileSavePicker::SettingsIdentifier(hstring const& value)
{
PickerCommon::ValidateStringNoEmbeddedNulls(value);
m_settingsIdentifier = value;
}
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId FileSavePicker::SuggestedStartLocation()
Expand All @@ -41,6 +42,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
}
void FileSavePicker::SuggestedStartLocation(winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId const& value)
{
PickerCommon::ValidateSuggestedStartLocation(value);
m_suggestedStartLocation = value;
}
hstring FileSavePicker::CommitButtonText()
Expand All @@ -49,6 +51,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
}
void FileSavePicker::CommitButtonText(hstring const& value)
{
PickerCommon::ValidateStringNoEmbeddedNulls(value);
m_commitButtonText = value;
}
winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> FileSavePicker::FileTypeChoices()
Expand Down Expand Up @@ -86,12 +89,13 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
{
return m_suggestedFileName;
}

void FileSavePicker::SuggestedFileName(hstring const& value)
{
PickerCommon::ValidateSuggestedFileName(value);
m_suggestedFileName = value;
}


void FileSavePicker::CaptureParameters(PickerCommon::PickerParameters& parameters)
{
parameters.HWnd = winrt::Microsoft::UI::GetWindowFromWindowId(m_windowId);
Expand Down
3 changes: 2 additions & 1 deletion dev/Interop/StoragePickers/FileSavePicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Microsoft.Windows.Storage.Pickers.FileSavePicker.g.h"
#include "PickerCommon.h"
#include "StoragePickersTelemetryHelper.h"
#include "FileTypeChoicesMap.h"

namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
{
Expand Down Expand Up @@ -41,7 +42,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
hstring m_settingsIdentifier{};
PickerLocationId m_suggestedStartLocation{ PickerLocationId::Unspecified };
hstring m_commitButtonText{};
winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> m_fileTypeChoices{ winrt::single_threaded_map<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>>() };
winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> m_fileTypeChoices{ make<FileTypeChoicesMap>() };
hstring m_defaultFileExtension{};
hstring m_suggestedSaveFilePath{};
hstring m_suggestedFileName{};
Expand Down
65 changes: 65 additions & 0 deletions dev/Interop/StoragePickers/FileTypeChoicesMap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#include "pch.h"
#include "FileTypeChoicesMap.h"
#include "FileTypeFilterVector.h"

namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
{
FileTypeChoicesMap::FileTypeChoicesMap()
{
}

bool FileTypeChoicesMap::Insert(hstring const& key, winrt::Windows::Foundation::Collections::IVector<hstring> const& value)
{
// Create a new FileTypeFilterVector and copy all values from the input vector
auto validatingVector = make<FileTypeFilterVector>();

if (value)
{
// Each Append call will validate the extension
for (auto const& item : value)
{
validatingVector.as<winrt::Windows::Foundation::Collections::IVector<hstring>>().Append(item);
}
}

return m_innerMap.Insert(key, validatingVector);
}

winrt::Windows::Foundation::Collections::IVector<hstring> FileTypeChoicesMap::Lookup(hstring const& key) const
{
return m_innerMap.Lookup(key);
}

uint32_t FileTypeChoicesMap::Size() const
{
return m_innerMap.Size();
}

bool FileTypeChoicesMap::HasKey(hstring const& key) const
{
return m_innerMap.HasKey(key);
}

winrt::Windows::Foundation::Collections::IMapView<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> FileTypeChoicesMap::GetView() const
{
return m_innerMap.GetView();
}

void FileTypeChoicesMap::Remove(hstring const& key)
{
m_innerMap.Remove(key);
}

void FileTypeChoicesMap::Clear()
{
m_innerMap.Clear();
}

winrt::Windows::Foundation::Collections::IIterator<winrt::Windows::Foundation::Collections::IKeyValuePair<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>>> FileTypeChoicesMap::First() const
{
return m_innerMap.First();
}
}
32 changes: 32 additions & 0 deletions dev/Interop/StoragePickers/FileTypeChoicesMap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#pragma once
#include <winrt/Windows.Foundation.Collections.h>
#include "FileTypeFilterVector.h"

namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
{
struct FileTypeChoicesMap : implements<FileTypeChoicesMap,
winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>>,
winrt::Windows::Foundation::Collections::IIterable<winrt::Windows::Foundation::Collections::IKeyValuePair<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>>>>
{
FileTypeChoicesMap();

// IMap<hstring, IVector<hstring>>
winrt::Windows::Foundation::Collections::IVector<hstring> Lookup(hstring const& key) const;
uint32_t Size() const;
bool HasKey(hstring const& key) const;
winrt::Windows::Foundation::Collections::IMapView<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> GetView() const;
bool Insert(hstring const& key, winrt::Windows::Foundation::Collections::IVector<hstring> const& value);
void Remove(hstring const& key);
void Clear();

// IIterable<IKeyValuePair<hstring, IVector<hstring>>>
winrt::Windows::Foundation::Collections::IIterator<winrt::Windows::Foundation::Collections::IKeyValuePair<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>>> First() const;

private:
winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> m_innerMap{
single_threaded_map<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>>() };
};
}
86 changes: 86 additions & 0 deletions dev/Interop/StoragePickers/FileTypeFilterVector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#include "pch.h"
#include "FileTypeFilterVector.h"
#include "PickerCommon.h"

namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
{
FileTypeFilterVector::FileTypeFilterVector()
{
}

void FileTypeFilterVector::SetAt(uint32_t index, hstring const& value)
{
PickerCommon::ValidateSingleFileTypeFilterElement(value);
m_innerVector.SetAt(index, value);
}

void FileTypeFilterVector::InsertAt(uint32_t index, hstring const& value)
{
PickerCommon::ValidateSingleFileTypeFilterElement(value);
m_innerVector.InsertAt(index, value);
}

void FileTypeFilterVector::Append(hstring const& value)
{
PickerCommon::ValidateSingleFileTypeFilterElement(value);
m_innerVector.Append(value);
}

void FileTypeFilterVector::ReplaceAll(array_view<hstring const> items)
{
// Validate all items before replacing
for (auto const& item : items)
{
PickerCommon::ValidateSingleFileTypeFilterElement(item);
}
m_innerVector.ReplaceAll(items);
}

hstring FileTypeFilterVector::GetAt(uint32_t index) const
{
return m_innerVector.GetAt(index);
}

uint32_t FileTypeFilterVector::Size() const
{
return m_innerVector.Size();
}

winrt::Windows::Foundation::Collections::IVectorView<hstring> FileTypeFilterVector::GetView() const
{
return m_innerVector.GetView();
}

bool FileTypeFilterVector::IndexOf(hstring const& value, uint32_t& index) const
{
return m_innerVector.IndexOf(value, index);
}

void FileTypeFilterVector::RemoveAt(uint32_t index)
{
m_innerVector.RemoveAt(index);
}

void FileTypeFilterVector::RemoveAtEnd()
{
m_innerVector.RemoveAtEnd();
}

void FileTypeFilterVector::Clear()
{
m_innerVector.Clear();
}

uint32_t FileTypeFilterVector::GetMany(uint32_t startIndex, array_view<hstring> items) const
{
return m_innerVector.GetMany(startIndex, items);
}

winrt::Windows::Foundation::Collections::IIterator<hstring> FileTypeFilterVector::First() const
{
return m_innerVector.First();
}
}
35 changes: 35 additions & 0 deletions dev/Interop/StoragePickers/FileTypeFilterVector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#pragma once
#include <winrt/Windows.Foundation.Collections.h>

namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
{
struct FileTypeFilterVector : implements<FileTypeFilterVector,
winrt::Windows::Foundation::Collections::IVector<hstring>,
winrt::Windows::Foundation::Collections::IIterable<hstring>>
{
FileTypeFilterVector();

// IVector<hstring>
hstring GetAt(uint32_t index) const;
uint32_t Size() const;
winrt::Windows::Foundation::Collections::IVectorView<hstring> GetView() const;
bool IndexOf(hstring const& value, uint32_t& index) const;
void SetAt(uint32_t index, hstring const& value);
void InsertAt(uint32_t index, hstring const& value);
void RemoveAt(uint32_t index);
void Append(hstring const& value);
void RemoveAtEnd();
void Clear();
uint32_t GetMany(uint32_t startIndex, array_view<hstring> items) const;
void ReplaceAll(array_view<hstring const> items);

// IIterable<hstring>
winrt::Windows::Foundation::Collections::IIterator<hstring> First() const;

private:
winrt::Windows::Foundation::Collections::IVector<hstring> m_innerVector{ single_threaded_vector<hstring>() };
};
}
4 changes: 4 additions & 0 deletions dev/Interop/StoragePickers/FolderPicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
}
void FolderPicker::ViewMode(winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode const& value)
{
PickerCommon::ValidateViewMode(value);
m_viewMode = value;
}
hstring FolderPicker::SettingsIdentifier()
Expand All @@ -34,6 +35,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
}
void FolderPicker::SettingsIdentifier(hstring const& value)
{
PickerCommon::ValidateStringNoEmbeddedNulls(value);
m_settingsIdentifier = value;
}
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId FolderPicker::SuggestedStartLocation()
Expand All @@ -42,6 +44,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
}
void FolderPicker::SuggestedStartLocation(winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId const& value)
{
PickerCommon::ValidateSuggestedStartLocation(value);
m_suggestedStartLocation = value;
}
hstring FolderPicker::CommitButtonText()
Expand All @@ -50,6 +53,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
}
void FolderPicker::CommitButtonText(hstring const& value)
{
PickerCommon::ValidateStringNoEmbeddedNulls(value);
m_commitButtonText = value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ namespace Microsoft.Windows.Storage.Pickers
ComputerFolder,
Desktop,
Downloads,
HomeGroup,
MusicLibrary,
MusicLibrary = 5,
PicturesLibrary,
VideosLibrary,
Objects3D,
Expand Down
Loading