Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit b638c59

Browse files
committed
Bug 1685213 - Part 2: Implement shortcut location classifier. r=bytesized
Differential Revision: https://phabricator.services.mozilla.com/D106344
1 parent 37c29fd commit b638c59

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

browser/components/shell/nsIWindowsShellService.idl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,19 @@ interface nsIWindowsShellService : nsISupports
6969
* These cases should be rare.
7070
*/
7171
boolean isCurrentAppPinnedToTaskbar();
72+
73+
/*
74+
* Determine where a given shortcut likely appears in the shell.
75+
*
76+
* Returns one of:
77+
* - "StartMenu", Current User or All Users Start Menu, including pins
78+
* - "Desktop", Current User or All Users Desktop
79+
* - "Taskbar", Taskbar Pins
80+
* - "" otherwise
81+
*
82+
* NOTE: This tries to avoid I/O, so paths are compared directly as
83+
* strings, which may not be accurate in all cases. It is intended
84+
* for noncritical telemetry use.
85+
*/
86+
AString classifyShortcut(in AString aPath);
7287
};

browser/components/shell/nsWindowsShellService.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ PSSTDAPI PropVariantToString(REFPROPVARIANT propvar, PWSTR psz, UINT cch);
4343

4444
#include <objbase.h>
4545
#include <shlobj.h>
46+
#include <knownfolders.h>
4647
#include "WinUtils.h"
4748
#include "mozilla/widget/WinTaskbar.h"
4849

@@ -974,6 +975,58 @@ nsWindowsShellService::IsCurrentAppPinnedToTaskbar(/* out */ bool* aIsPinned) {
974975
return NS_OK;
975976
}
976977

978+
NS_IMETHODIMP
979+
nsWindowsShellService::ClassifyShortcut(const nsAString& aPath,
980+
nsAString& aResult) {
981+
aResult.Truncate();
982+
983+
nsAutoString shortcutPath(PromiseFlatString(aPath));
984+
985+
// NOTE: On Windows 7, Start Menu pin shortcuts are stored under
986+
// "<FOLDERID_User Pinned>\StartMenu", but on Windows 10 they are just normal
987+
// Start Menu shortcuts. These both map to "StartMenu" for consistency,
988+
// rather than having a separate "StartMenuPins" which would only apply on
989+
// Win7.
990+
struct {
991+
KNOWNFOLDERID folderId;
992+
const char16_t* postfix;
993+
const char16_t* classification;
994+
} folders[] = {{FOLDERID_CommonStartMenu, u"\\", u"StartMenu"},
995+
{FOLDERID_StartMenu, u"\\", u"StartMenu"},
996+
{FOLDERID_PublicDesktop, u"\\", u"Desktop"},
997+
{FOLDERID_Desktop, u"\\", u"Desktop"},
998+
{FOLDERID_UserPinned, u"\\TaskBar\\", u"Taskbar"},
999+
{FOLDERID_UserPinned, u"\\StartMenu\\", u"StartMenu"}};
1000+
1001+
for (int i = 0; i < ArrayLength(folders); ++i) {
1002+
nsAutoString knownPath;
1003+
1004+
// These flags are chosen to avoid I/O, see bug 1363398.
1005+
DWORD flags =
1006+
KF_FLAG_SIMPLE_IDLIST | KF_FLAG_DONT_VERIFY | KF_FLAG_NO_ALIAS;
1007+
PWSTR rawPath = nullptr;
1008+
1009+
if (FAILED(SHGetKnownFolderPath(folders[i].folderId, flags, nullptr,
1010+
&rawPath))) {
1011+
continue;
1012+
}
1013+
1014+
knownPath = nsDependentString(rawPath);
1015+
CoTaskMemFree(rawPath);
1016+
1017+
knownPath.Append(folders[i].postfix);
1018+
// Check if the shortcut path starts with the shell folder path.
1019+
if (wcsnicmp(shortcutPath.get(), knownPath.get(), knownPath.Length()) ==
1020+
0) {
1021+
aResult.Assign(folders[i].classification);
1022+
return NS_OK;
1023+
}
1024+
}
1025+
1026+
// Nothing found, aResult is already "".
1027+
return NS_OK;
1028+
}
1029+
9771030
nsWindowsShellService::nsWindowsShellService() {}
9781031

9791032
nsWindowsShellService::~nsWindowsShellService() {}

0 commit comments

Comments
 (0)