Skip to content

Commit 8b1ae56

Browse files
authored
Add validations for storage pickers (#5619)
* validate the enums * validate filters and choices * localization files for local tests * tests * ValidatingFileTypeFilterVector * ValidatingFileTypeChoicesMap * ValidateSuggestedFileName * Validate CommitButtonText and SettingsIdentifier * tests for validating the embedded null * revert changes on resources here (will have a separate PR for it) * move vector and map construction to *picker.h * update resource key * add missing copyright headlines; add a localization fallback * resolve comments: fix errors; remove localization fallback; graceful
1 parent 4f83b3c commit 8b1ae56

22 files changed

+855
-18
lines changed

dev/Interop/StoragePickers/FileOpenPicker.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
2828
}
2929
void FileOpenPicker::ViewMode(winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode const& value)
3030
{
31+
PickerCommon::ValidateViewMode(value);
3132
m_viewMode = value;
3233
}
3334
hstring FileOpenPicker::SettingsIdentifier()
@@ -36,6 +37,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
3637
}
3738
void FileOpenPicker::SettingsIdentifier(hstring const& value)
3839
{
40+
PickerCommon::ValidateStringNoEmbeddedNulls(value);
3941
m_settingsIdentifier = value;
4042
}
4143
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId FileOpenPicker::SuggestedStartLocation()
@@ -44,6 +46,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
4446
}
4547
void FileOpenPicker::SuggestedStartLocation(winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId const& value)
4648
{
49+
PickerCommon::ValidateSuggestedStartLocation(value);
4750
m_suggestedStartLocation = value;
4851
}
4952
winrt::hstring FileOpenPicker::CommitButtonText()
@@ -52,6 +55,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
5255
}
5356
void FileOpenPicker::CommitButtonText(winrt::hstring const& value)
5457
{
58+
PickerCommon::ValidateStringNoEmbeddedNulls(value);
5559
m_commitButtonText = value;
5660
}
5761
winrt::Windows::Foundation::Collections::IVector<hstring> FileOpenPicker::FileTypeFilter()

dev/Interop/StoragePickers/FileOpenPicker.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "Microsoft.Windows.Storage.Pickers.FileOpenPicker.g.h"
66
#include "PickerCommon.h"
77
#include "StoragePickersTelemetryHelper.h"
8+
#include <winrt/Windows.Foundation.Collections.h>
9+
#include "FileTypeFilterVector.h"
810

911
namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
1012
{
@@ -35,7 +37,9 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
3537
winrt::hstring m_settingsIdentifier{};
3638
PickerLocationId m_suggestedStartLocation{ PickerLocationId::Unspecified };
3739
winrt::hstring m_commitButtonText{};
38-
winrt::Windows::Foundation::Collections::IVector<hstring> m_fileTypeFilter{ winrt::single_threaded_vector<hstring>() };
40+
41+
winrt::Windows::Foundation::Collections::IVector<hstring> m_fileTypeFilter{ make<FileTypeFilterVector>() };
42+
3943
StoragePickersTelemetryHelper m_telemetryHelper{};
4044

4145
void CaptureParameters(PickerCommon::PickerParameters& parameters);

dev/Interop/StoragePickers/FileSavePicker.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
3333
}
3434
void FileSavePicker::SettingsIdentifier(hstring const& value)
3535
{
36+
PickerCommon::ValidateStringNoEmbeddedNulls(value);
3637
m_settingsIdentifier = value;
3738
}
3839
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId FileSavePicker::SuggestedStartLocation()
@@ -41,6 +42,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
4142
}
4243
void FileSavePicker::SuggestedStartLocation(winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId const& value)
4344
{
45+
PickerCommon::ValidateSuggestedStartLocation(value);
4446
m_suggestedStartLocation = value;
4547
}
4648
hstring FileSavePicker::CommitButtonText()
@@ -49,6 +51,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
4951
}
5052
void FileSavePicker::CommitButtonText(hstring const& value)
5153
{
54+
PickerCommon::ValidateStringNoEmbeddedNulls(value);
5255
m_commitButtonText = value;
5356
}
5457
winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> FileSavePicker::FileTypeChoices()
@@ -86,12 +89,13 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
8689
{
8790
return m_suggestedFileName;
8891
}
92+
8993
void FileSavePicker::SuggestedFileName(hstring const& value)
9094
{
95+
PickerCommon::ValidateSuggestedFileName(value);
9196
m_suggestedFileName = value;
9297
}
9398

94-
9599
void FileSavePicker::CaptureParameters(PickerCommon::PickerParameters& parameters)
96100
{
97101
parameters.HWnd = winrt::Microsoft::UI::GetWindowFromWindowId(m_windowId);

dev/Interop/StoragePickers/FileSavePicker.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "Microsoft.Windows.Storage.Pickers.FileSavePicker.g.h"
66
#include "PickerCommon.h"
77
#include "StoragePickersTelemetryHelper.h"
8+
#include "FileTypeChoicesMap.h"
89

910
namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
1011
{
@@ -41,7 +42,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
4142
hstring m_settingsIdentifier{};
4243
PickerLocationId m_suggestedStartLocation{ PickerLocationId::Unspecified };
4344
hstring m_commitButtonText{};
44-
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>>() };
45+
winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> m_fileTypeChoices{ make<FileTypeChoicesMap>() };
4546
hstring m_defaultFileExtension{};
4647
hstring m_suggestedSaveFilePath{};
4748
hstring m_suggestedFileName{};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) Microsoft Corporation and Contributors.
2+
// Licensed under the MIT License.
3+
4+
#include "pch.h"
5+
#include "FileTypeChoicesMap.h"
6+
#include "FileTypeFilterVector.h"
7+
8+
namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
9+
{
10+
FileTypeChoicesMap::FileTypeChoicesMap()
11+
{
12+
}
13+
14+
bool FileTypeChoicesMap::Insert(hstring const& key, winrt::Windows::Foundation::Collections::IVector<hstring> const& value)
15+
{
16+
// Create a new FileTypeFilterVector and copy all values from the input vector
17+
auto validatingVector = make<FileTypeFilterVector>();
18+
19+
if (value)
20+
{
21+
// Each Append call will validate the extension
22+
for (auto const& item : value)
23+
{
24+
validatingVector.as<winrt::Windows::Foundation::Collections::IVector<hstring>>().Append(item);
25+
}
26+
}
27+
28+
return m_innerMap.Insert(key, validatingVector);
29+
}
30+
31+
winrt::Windows::Foundation::Collections::IVector<hstring> FileTypeChoicesMap::Lookup(hstring const& key) const
32+
{
33+
return m_innerMap.Lookup(key);
34+
}
35+
36+
uint32_t FileTypeChoicesMap::Size() const
37+
{
38+
return m_innerMap.Size();
39+
}
40+
41+
bool FileTypeChoicesMap::HasKey(hstring const& key) const
42+
{
43+
return m_innerMap.HasKey(key);
44+
}
45+
46+
winrt::Windows::Foundation::Collections::IMapView<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> FileTypeChoicesMap::GetView() const
47+
{
48+
return m_innerMap.GetView();
49+
}
50+
51+
void FileTypeChoicesMap::Remove(hstring const& key)
52+
{
53+
m_innerMap.Remove(key);
54+
}
55+
56+
void FileTypeChoicesMap::Clear()
57+
{
58+
m_innerMap.Clear();
59+
}
60+
61+
winrt::Windows::Foundation::Collections::IIterator<winrt::Windows::Foundation::Collections::IKeyValuePair<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>>> FileTypeChoicesMap::First() const
62+
{
63+
return m_innerMap.First();
64+
}
65+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Microsoft Corporation and Contributors.
2+
// Licensed under the MIT License.
3+
4+
#pragma once
5+
#include <winrt/Windows.Foundation.Collections.h>
6+
#include "FileTypeFilterVector.h"
7+
8+
namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
9+
{
10+
struct FileTypeChoicesMap : implements<FileTypeChoicesMap,
11+
winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>>,
12+
winrt::Windows::Foundation::Collections::IIterable<winrt::Windows::Foundation::Collections::IKeyValuePair<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>>>>
13+
{
14+
FileTypeChoicesMap();
15+
16+
// IMap<hstring, IVector<hstring>>
17+
winrt::Windows::Foundation::Collections::IVector<hstring> Lookup(hstring const& key) const;
18+
uint32_t Size() const;
19+
bool HasKey(hstring const& key) const;
20+
winrt::Windows::Foundation::Collections::IMapView<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> GetView() const;
21+
bool Insert(hstring const& key, winrt::Windows::Foundation::Collections::IVector<hstring> const& value);
22+
void Remove(hstring const& key);
23+
void Clear();
24+
25+
// IIterable<IKeyValuePair<hstring, IVector<hstring>>>
26+
winrt::Windows::Foundation::Collections::IIterator<winrt::Windows::Foundation::Collections::IKeyValuePair<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>>> First() const;
27+
28+
private:
29+
winrt::Windows::Foundation::Collections::IMap<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>> m_innerMap{
30+
single_threaded_map<hstring, winrt::Windows::Foundation::Collections::IVector<hstring>>() };
31+
};
32+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) Microsoft Corporation and Contributors.
2+
// Licensed under the MIT License.
3+
4+
#include "pch.h"
5+
#include "FileTypeFilterVector.h"
6+
#include "PickerCommon.h"
7+
8+
namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
9+
{
10+
FileTypeFilterVector::FileTypeFilterVector()
11+
{
12+
}
13+
14+
void FileTypeFilterVector::SetAt(uint32_t index, hstring const& value)
15+
{
16+
PickerCommon::ValidateSingleFileTypeFilterElement(value);
17+
m_innerVector.SetAt(index, value);
18+
}
19+
20+
void FileTypeFilterVector::InsertAt(uint32_t index, hstring const& value)
21+
{
22+
PickerCommon::ValidateSingleFileTypeFilterElement(value);
23+
m_innerVector.InsertAt(index, value);
24+
}
25+
26+
void FileTypeFilterVector::Append(hstring const& value)
27+
{
28+
PickerCommon::ValidateSingleFileTypeFilterElement(value);
29+
m_innerVector.Append(value);
30+
}
31+
32+
void FileTypeFilterVector::ReplaceAll(array_view<hstring const> items)
33+
{
34+
// Validate all items before replacing
35+
for (auto const& item : items)
36+
{
37+
PickerCommon::ValidateSingleFileTypeFilterElement(item);
38+
}
39+
m_innerVector.ReplaceAll(items);
40+
}
41+
42+
hstring FileTypeFilterVector::GetAt(uint32_t index) const
43+
{
44+
return m_innerVector.GetAt(index);
45+
}
46+
47+
uint32_t FileTypeFilterVector::Size() const
48+
{
49+
return m_innerVector.Size();
50+
}
51+
52+
winrt::Windows::Foundation::Collections::IVectorView<hstring> FileTypeFilterVector::GetView() const
53+
{
54+
return m_innerVector.GetView();
55+
}
56+
57+
bool FileTypeFilterVector::IndexOf(hstring const& value, uint32_t& index) const
58+
{
59+
return m_innerVector.IndexOf(value, index);
60+
}
61+
62+
void FileTypeFilterVector::RemoveAt(uint32_t index)
63+
{
64+
m_innerVector.RemoveAt(index);
65+
}
66+
67+
void FileTypeFilterVector::RemoveAtEnd()
68+
{
69+
m_innerVector.RemoveAtEnd();
70+
}
71+
72+
void FileTypeFilterVector::Clear()
73+
{
74+
m_innerVector.Clear();
75+
}
76+
77+
uint32_t FileTypeFilterVector::GetMany(uint32_t startIndex, array_view<hstring> items) const
78+
{
79+
return m_innerVector.GetMany(startIndex, items);
80+
}
81+
82+
winrt::Windows::Foundation::Collections::IIterator<hstring> FileTypeFilterVector::First() const
83+
{
84+
return m_innerVector.First();
85+
}
86+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Microsoft Corporation and Contributors.
2+
// Licensed under the MIT License.
3+
4+
#pragma once
5+
#include <winrt/Windows.Foundation.Collections.h>
6+
7+
namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
8+
{
9+
struct FileTypeFilterVector : implements<FileTypeFilterVector,
10+
winrt::Windows::Foundation::Collections::IVector<hstring>,
11+
winrt::Windows::Foundation::Collections::IIterable<hstring>>
12+
{
13+
FileTypeFilterVector();
14+
15+
// IVector<hstring>
16+
hstring GetAt(uint32_t index) const;
17+
uint32_t Size() const;
18+
winrt::Windows::Foundation::Collections::IVectorView<hstring> GetView() const;
19+
bool IndexOf(hstring const& value, uint32_t& index) const;
20+
void SetAt(uint32_t index, hstring const& value);
21+
void InsertAt(uint32_t index, hstring const& value);
22+
void RemoveAt(uint32_t index);
23+
void Append(hstring const& value);
24+
void RemoveAtEnd();
25+
void Clear();
26+
uint32_t GetMany(uint32_t startIndex, array_view<hstring> items) const;
27+
void ReplaceAll(array_view<hstring const> items);
28+
29+
// IIterable<hstring>
30+
winrt::Windows::Foundation::Collections::IIterator<hstring> First() const;
31+
32+
private:
33+
winrt::Windows::Foundation::Collections::IVector<hstring> m_innerVector{ single_threaded_vector<hstring>() };
34+
};
35+
}

dev/Interop/StoragePickers/FolderPicker.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
2626
}
2727
void FolderPicker::ViewMode(winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode const& value)
2828
{
29+
PickerCommon::ValidateViewMode(value);
2930
m_viewMode = value;
3031
}
3132
hstring FolderPicker::SettingsIdentifier()
@@ -34,6 +35,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
3435
}
3536
void FolderPicker::SettingsIdentifier(hstring const& value)
3637
{
38+
PickerCommon::ValidateStringNoEmbeddedNulls(value);
3739
m_settingsIdentifier = value;
3840
}
3941
winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId FolderPicker::SuggestedStartLocation()
@@ -42,6 +44,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
4244
}
4345
void FolderPicker::SuggestedStartLocation(winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId const& value)
4446
{
47+
PickerCommon::ValidateSuggestedStartLocation(value);
4548
m_suggestedStartLocation = value;
4649
}
4750
hstring FolderPicker::CommitButtonText()
@@ -50,6 +53,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
5053
}
5154
void FolderPicker::CommitButtonText(hstring const& value)
5255
{
56+
PickerCommon::ValidateStringNoEmbeddedNulls(value);
5357
m_commitButtonText = value;
5458
}
5559

dev/Interop/StoragePickers/Microsoft.Windows.Storage.Pickers.idl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ namespace Microsoft.Windows.Storage.Pickers
2323
ComputerFolder,
2424
Desktop,
2525
Downloads,
26-
HomeGroup,
27-
MusicLibrary,
26+
MusicLibrary = 5,
2827
PicturesLibrary,
2928
VideosLibrary,
3029
Objects3D,

0 commit comments

Comments
 (0)