@@ -1278,9 +1278,8 @@ static bool IsCurrentAppPinnedToTaskbarSync(const nsAString& aumid) {
1278
1278
return isPinned;
1279
1279
}
1280
1280
1281
- static nsresult PinCurrentAppToTaskbarWin10 (bool aCheckOnly,
1282
- const nsAString& aAppUserModelId,
1283
- nsAutoString aShortcutPath) {
1281
+ static nsresult ManageShortcutTaskbarPins (bool aCheckOnly, bool aPinType,
1282
+ const nsAString& aShortcutPath) {
1284
1283
// This enum is likely only used for Windows telemetry, INT_MAX is chosen to
1285
1284
// avoid confusion with existing uses.
1286
1285
enum PINNEDLISTMODIFYCALLER { PLMC_INT_MAX = INT_MAX };
@@ -1325,36 +1324,65 @@ static nsresult PinCurrentAppToTaskbarWin10(bool aCheckOnly,
1325
1324
}
1326
1325
};
1327
1326
1327
+ HRESULT hr = CoInitialize (nullptr );
1328
+ if (FAILED (hr)) {
1329
+ return NS_ERROR_FAILURE;
1330
+ }
1331
+ const struct ComUninitializer {
1332
+ ~ComUninitializer () { CoUninitialize (); }
1333
+ } kCUi ;
1334
+
1328
1335
mozilla::UniquePtr<__unaligned ITEMIDLIST, ILFreeDeleter> path (
1329
- ILCreateFromPathW (aShortcutPath.get ()));
1336
+ ILCreateFromPathW (nsString ( aShortcutPath) .get ()));
1330
1337
if (NS_WARN_IF(!path)) {
1331
- return NS_ERROR_FAILURE ;
1338
+ return NS_ERROR_FILE_NOT_FOUND ;
1332
1339
}
1333
1340
1334
1341
IPinnedList3* pinnedList = nullptr ;
1335
- HRESULT hr =
1336
- CoCreateInstance (CLSID_TaskbandPin, nullptr , CLSCTX_INPROC_SERVER,
1337
- IID_IPinnedList3, (void **)&pinnedList);
1342
+ hr = CoCreateInstance (CLSID_TaskbandPin, NULL , CLSCTX_INPROC_SERVER,
1343
+ IID_IPinnedList3, (void **)&pinnedList);
1338
1344
if (FAILED (hr) || !pinnedList) {
1339
1345
return NS_ERROR_NOT_AVAILABLE;
1340
1346
}
1341
1347
1342
1348
if (!aCheckOnly) {
1343
- bool isPinned = false ;
1344
- isPinned = IsCurrentAppPinnedToTaskbarSync (aAppUserModelId);
1345
- if (!isPinned) {
1346
- hr = pinnedList->vtbl ->Modify (pinnedList, nullptr , path.get (),
1347
- PLMC_INT_MAX);
1348
- }
1349
+ hr = pinnedList->vtbl ->Modify (pinnedList, aPinType ? NULL : path.get (),
1350
+ aPinType ? path.get () : NULL , PLMC_INT_MAX);
1349
1351
}
1350
1352
1351
1353
pinnedList->vtbl ->Release (pinnedList);
1352
1354
1353
1355
if (FAILED (hr)) {
1354
- return NS_ERROR_FAILURE;
1355
- } else {
1356
- return NS_OK;
1356
+ return NS_ERROR_FILE_ACCESS_DENIED;
1357
+ }
1358
+ return NS_OK;
1359
+ }
1360
+
1361
+ NS_IMETHODIMP
1362
+ nsWindowsShellService::PinShortcutToTaskbar (const nsAString& aShortcutPath) {
1363
+ const bool pinType = true ; // true means pin
1364
+ const bool runInTestMode = false ;
1365
+ return ManageShortcutTaskbarPins (runInTestMode, pinType, aShortcutPath);
1366
+ }
1367
+
1368
+ NS_IMETHODIMP
1369
+ nsWindowsShellService::UnpinShortcutFromTaskbar (
1370
+ const nsAString& aShortcutPath) {
1371
+ const bool pinType = false ; // false means unpin
1372
+ const bool runInTestMode = false ;
1373
+ return ManageShortcutTaskbarPins (runInTestMode, pinType, aShortcutPath);
1374
+ }
1375
+
1376
+ static nsresult PinCurrentAppToTaskbarWin10 (bool aCheckOnly,
1377
+ const nsAString& aAppUserModelId,
1378
+ nsAutoString aShortcutPath) {
1379
+ // The behavior here is identical if we're only checking or if we try to pin
1380
+ // but the app is already pinned so we update the variable accordingly.
1381
+ if (!aCheckOnly) {
1382
+ aCheckOnly = !IsCurrentAppPinnedToTaskbarSync (aAppUserModelId);
1357
1383
}
1384
+ const bool pinType = true ; // true means pin
1385
+ return ManageShortcutTaskbarPins (aCheckOnly, pinType, aShortcutPath);
1358
1386
}
1359
1387
1360
1388
static nsresult PinCurrentAppToTaskbarImpl (
0 commit comments