# Global Metrics

path: .metrics.cyclomatic.sum
old: 17.0
new: 110.0
path: .metrics.cyclomatic.average
old: 1.7
new: 4.230769230769231
path: .metrics.cognitive.sum
old: 11.0
new: 129.0
path: .metrics.cognitive.average
old: 1.8333333333333333
new: 4.961538461538462
path: .metrics.mi.mi_original
old: 45.49388938765101
new: -10.128022226593473
path: .metrics.mi.mi_visual_studio
old: 26.604613676988897
new: 0.0
path: .metrics.mi.mi_sei
old: 15.346597394620602
new: -57.28446461696397
path: .metrics.halstead.vocabulary
old: 100.0
new: 310.0
path: .metrics.halstead.volume
old: 3082.749272055472
new: 19175.78024702041
path: .metrics.halstead.estimated_program_length
old: 588.3688686812759
new: 2413.971585934454
path: .metrics.halstead.time
old: 4927.129178413447
new: 59713.74889798685
path: .metrics.halstead.effort
old: 88688.32521144205
new: 1074847.4801637633
path: .metrics.halstead.n2
old: 78.0
new: 277.0
path: .metrics.halstead.N1
old: 260.0
new: 1376.0
path: .metrics.halstead.difficulty
old: 28.76923076923077
new: 56.05234657039711
path: .metrics.halstead.level
old: 0.034759358288770054
new: 0.01784046629955238
path: .metrics.halstead.bugs
old: 0.6629127639069036
new: 3.497652349169644
path: .metrics.halstead.n1
old: 22.0
new: 33.0
path: .metrics.halstead.purity_ratio
old: 1.268036354916543
new: 1.0418522166311843
path: .metrics.halstead.N2
old: 204.0
new: 941.0
path: .metrics.halstead.length
old: 464.0
new: 2317.0
path: .metrics.nargs.sum
old: 11.0
new: 21.0
path: .metrics.nargs.average
old: 1.8333333333333333
new: 0.8076923076923077
path: .metrics.loc.sloc
old: 138.0
new: 635.0
path: .metrics.loc.blank
old: 18.0
new: 112.0
path: .metrics.loc.cloc
old: 14.0
new: 54.0
path: .metrics.loc.ploc
old: 106.0
new: 469.0
path: .metrics.loc.lloc
old: 23.0
new: 248.0
path: .metrics.nom.functions
old: 6.0
new: 21.0
path: .metrics.nom.closures
old: 0.0
new: 5.0
path: .metrics.nom.total
old: 6.0
new: 26.0
path: .metrics.nexits.sum
old: 6.0
new: 52.0
path: .metrics.nexits.average
old: 1.0
new: 2.0

# Spaces Data

## Minimal test - lines (37, 634)

path: .spaces[0].spaces[0].metrics.nargs.sum
old: 3.0
new: 21.0
path: .spaces[0].spaces[0].metrics.nargs.average
old: 1.5
new: 0.8076923076923077
path: .spaces[0].spaces[0].metrics.cyclomatic.sum
old: 5.0
new: 108.0
path: .spaces[0].spaces[0].metrics.cyclomatic.average
old: 1.25
new: 4.5
path: .spaces[0].spaces[0].metrics.halstead.difficulty
old: 10.235294117647058
new: 59.16796875
path: .spaces[0].spaces[0].metrics.halstead.estimated_program_length
old: 112.50641830990963
new: 2214.465005938829
path: .spaces[0].spaces[0].metrics.halstead.level
old: 0.09770114942528736
new: 0.016901036508879645
path: .spaces[0].spaces[0].metrics.halstead.purity_ratio
old: 1.6305278015928932
new: 0.96701528643617
path: .spaces[0].spaces[0].metrics.halstead.time
old: 190.60431316177
new: 61536.593408506626
path: .spaces[0].spaces[0].metrics.halstead.length
old: 69.0
new: 2290.0
path: .spaces[0].spaces[0].metrics.halstead.N1
old: 40.0
new: 1372.0
path: .spaces[0].spaces[0].metrics.halstead.bugs
old: 0.07582554922199064
new: 3.568475469961448
path: .spaces[0].spaces[0].metrics.halstead.n2
old: 17.0
new: 256.0
path: .spaces[0].spaces[0].metrics.halstead.N2
old: 29.0
new: 918.0
path: .spaces[0].spaces[0].metrics.halstead.volume
old: 335.20068866380245
new: 18720.579812926553
path: .spaces[0].spaces[0].metrics.halstead.n1
old: 12.0
new: 33.0
path: .spaces[0].spaces[0].metrics.halstead.vocabulary
old: 29.0
new: 289.0
path: .spaces[0].spaces[0].metrics.halstead.effort
old: 3430.87763691186
new: 1107658.6813531192
path: .spaces[0].spaces[0].metrics.loc.cloc
old: 0.0
new: 46.0
path: .spaces[0].spaces[0].metrics.loc.lloc
old: 2.0
new: 248.0
path: .spaces[0].spaces[0].metrics.loc.ploc
old: 22.0
new: 444.0
path: .spaces[0].spaces[0].metrics.loc.sloc
old: 26.0
new: 598.0
path: .spaces[0].spaces[0].metrics.loc.blank
old: 4.0
new: 108.0
path: .spaces[0].spaces[0].metrics.nexits.average
old: 0.0
new: 2.0
path: .spaces[0].spaces[0].metrics.nexits.sum
old: 0.0
new: 52.0
path: .spaces[0].spaces[0].metrics.nom.functions
old: 2.0
new: 21.0
path: .spaces[0].spaces[0].metrics.nom.closures
old: 0.0
new: 5.0
path: .spaces[0].spaces[0].metrics.nom.total
old: 2.0
new: 26.0
path: .spaces[0].spaces[0].metrics.mi.mi_sei
old: 50.080693791522506
new: -56.240489951468504
path: .spaces[0].spaces[0].metrics.mi.mi_visual_studio
old: 50.77908952279127
new: 0.0
path: .spaces[0].spaces[0].metrics.mi.mi_original
old: 86.83224308397305
new: -8.57053957081655
path: .spaces[0].spaces[0].metrics.cognitive.average
old: 0.5
new: 4.961538461538462
path: .spaces[0].spaces[0].metrics.cognitive.sum
old: 1.0
new: 129.0

### Code

    namespace widget {

    // defined in WinTaskbar.cpp
    extern const wchar_t* gMozillaJumpListIDGeneric;

    Atomic JumpListBuilder::sBuildingList(false);
    const char kPrefTaskbarEnabled[] = "browser.taskbar.lists.enabled";

    NS_IMPL_ISUPPORTS(JumpListBuilder, nsIJumpListBuilder, nsIObserver)
    #define TOPIC_PROFILE_BEFORE_CHANGE "profile-before-change"
    #define TOPIC_CLEAR_PRIVATE_DATA "clear-private-data"

    namespace detail {

    class DoneCommitListBuildCallback final : public nsIRunnable {
      NS_DECL_THREADSAFE_ISUPPORTS

     public:
      DoneCommitListBuildCallback(nsIJumpListCommittedCallback* aCallback,
                                  JumpListBuilder* aBuilder)
          : mCallback(aCallback), mBuilder(aBuilder), mResult(false) {}

      NS_IMETHOD Run() override {
        MOZ_ASSERT(NS_IsMainThread());
        if (mCallback) {
          Unused << mCallback->Done(mResult);
        }
        // Ensure we are releasing on the main thread.
        Destroy();
        return NS_OK;
      }

      void SetResult(bool aResult) { mResult = aResult; }

     private:
      ~DoneCommitListBuildCallback() {
        // Destructor does not always call on the main thread.
        MOZ_ASSERT(!mCallback);
        MOZ_ASSERT(!mBuilder);
      }

      void Destroy() {
        MOZ_ASSERT(NS_IsMainThread());
        mCallback = nullptr;
        mBuilder = nullptr;
      }

      // These two references MUST be released on the main thread.
      RefPtr mCallback;
      RefPtr mBuilder;
      bool mResult;
    };

    NS_IMPL_ISUPPORTS(DoneCommitListBuildCallback, nsIRunnable);

    }  // namespace detail

    JumpListBuilder::JumpListBuilder()
        : mMaxItems(0), mHasCommit(false), mMonitor("JumpListBuilderMonitor") {
      MOZ_ASSERT(NS_IsMainThread());

      // Instantiate mJumpListMgr in the multithreaded apartment so that proxied
      // calls on that object do not need to interact with the main thread's message
      // pump.
      mscom::EnsureMTA([this]() {
        RefPtr jumpListMgr;
        HRESULT hr = ::CoCreateInstance(
            CLSID_DestinationList, nullptr, CLSCTX_INPROC_SERVER,
            IID_ICustomDestinationList, getter_AddRefs(jumpListMgr));
        if (FAILED(hr)) {
          return;
        }

        // Since we are accessing mJumpListMgr across different threads
        // (ie, different apartments), mJumpListMgr must be an agile reference.
        mJumpListMgr = jumpListMgr;
      });

      if (!mJumpListMgr) {
        return;
      }

      // Make a lazy thread for any IO
      mIOThread = new LazyIdleThread(DEFAULT_THREAD_TIMEOUT_MS, "Jump List"_ns,
                                     LazyIdleThread::ManualShutdown);
      Preferences::AddStrongObserver(this, kPrefTaskbarEnabled);

      nsCOMPtr observerService =
          do_GetService("@mozilla.org/observer-service;1");
      if (observerService) {
        observerService->AddObserver(this, TOPIC_PROFILE_BEFORE_CHANGE, false);
        observerService->AddObserver(this, TOPIC_CLEAR_PRIVATE_DATA, false);
      }

      RefPtr jumpListMgr = mJumpListMgr;
      if (!jumpListMgr) {
        return;
      }

      // GetAppUserModelID can only be called once we're back on the main thread.
      nsString modelId;
      if (mozilla::widget::WinTaskbar::GetAppUserModelID(modelId)) {
        jumpListMgr->SetAppID(modelId.get());
      }
    }

    JumpListBuilder::~JumpListBuilder() {
      Preferences::RemoveObserver(this, kPrefTaskbarEnabled);
    }

    NS_IMETHODIMP JumpListBuilder::GetAvailable(int16_t* aAvailable) {
      *aAvailable = false;

      ReentrantMonitorAutoEnter lock(mMonitor);
      if (mJumpListMgr) *aAvailable = true;

      return NS_OK;
    }

    NS_IMETHODIMP JumpListBuilder::GetIsListCommitted(bool* aCommit) {
      *aCommit = mHasCommit;

      return NS_OK;
    }

    NS_IMETHODIMP JumpListBuilder::GetMaxListItems(int16_t* aMaxItems) {
      ReentrantMonitorAutoEnter lock(mMonitor);
      if (!mJumpListMgr) return NS_ERROR_NOT_AVAILABLE;

      *aMaxItems = 0;

      if (sBuildingList) {
        *aMaxItems = mMaxItems;
        return NS_OK;
      }

      RefPtr jumpListMgr = mJumpListMgr;
      if (!jumpListMgr) {
        return NS_ERROR_UNEXPECTED;
      }

      IObjectArray* objArray;
      if (SUCCEEDED(jumpListMgr->BeginList(&mMaxItems, IID_PPV_ARGS(&objArray)))) {
        *aMaxItems = mMaxItems;

        if (objArray) objArray->Release();

        jumpListMgr->AbortList();
      }

      return NS_OK;
    }

    NS_IMETHODIMP JumpListBuilder::InitListBuild(JSContext* aCx,
                                                 Promise** aPromise) {
      ReentrantMonitorAutoEnter lock(mMonitor);
      if (!mJumpListMgr) {
        return NS_ERROR_NOT_AVAILABLE;
      }

      nsIGlobalObject* globalObject = xpc::CurrentNativeGlobal(aCx);
      if (NS_WARN_IF(!globalObject)) {
        return NS_ERROR_FAILURE;
      }

      ErrorResult result;
      RefPtr promise = Promise::Create(globalObject, result);
      if (NS_WARN_IF(result.Failed())) {
        return result.StealNSResult();
      }

      nsCOMPtr runnable =
          NewRunnableMethod>>(
              "InitListBuild", this, &JumpListBuilder::DoInitListBuild, promise);
      nsresult rv = mIOThread->Dispatch(runnable, NS_DISPATCH_NORMAL);
      if (NS_WARN_IF(NS_FAILED(rv))) {
        return rv;
      }

      promise.forget(aPromise);
      return NS_OK;
    }

    void JumpListBuilder::DoInitListBuild(RefPtr&& aPromise) {
      // Since we're invoking COM interfaces to talk to the shell on a background
      // thread, we need to be running inside a multithreaded apartment.
      mscom::MTARegion mta;
      MOZ_ASSERT(mta.IsValid());

      ReentrantMonitorAutoEnter lock(mMonitor);
      MOZ_ASSERT(mJumpListMgr);

      if (sBuildingList) {
        AbortListBuild();
      }

      HRESULT hr = E_UNEXPECTED;
      auto errorHandler = MakeScopeExit([&aPromise, &hr]() {
        if (SUCCEEDED(hr)) {
          return;
        }

        NS_DispatchToMainThread(NS_NewRunnableFunction(
            "InitListBuildReject", [promise = std::move(aPromise)]() {
              promise->MaybeReject(NS_ERROR_FAILURE);
            }));
      });

      RefPtr jumpListMgr = mJumpListMgr;
      if (!jumpListMgr) {
        return;
      }

      nsTArray urisToRemove;
      RefPtr objArray;
      hr = jumpListMgr->BeginList(
          &mMaxItems,
          IID_PPV_ARGS(static_cast(getter_AddRefs(objArray))));
      if (FAILED(hr)) {
        return;
      }

      // The returned objArray of removed items are for manually removed items.
      // This does not return items which are removed because they were previously
      // part of the jump list but are no longer part of the jump list.
      sBuildingList = true;
      RemoveIconCacheAndGetJumplistShortcutURIs(objArray, urisToRemove);

      NS_DispatchToMainThread(NS_NewRunnableFunction(
          "InitListBuildResolve", [urisToRemove = std::move(urisToRemove),
                                   promise = std::move(aPromise)]() {
            promise->MaybeResolve(urisToRemove);
          }));
    }

    // Ensures that we have no old ICO files left in the jump list cache
    nsresult JumpListBuilder::RemoveIconCacheForAllItems() {
      // Construct the path of our jump list cache
      nsCOMPtr jumpListCacheDir;
      nsresult rv =
          NS_GetSpecialDirectory("ProfLDS", getter_AddRefs(jumpListCacheDir));
      NS_ENSURE_SUCCESS(rv, rv);
      rv = jumpListCacheDir->AppendNative(
          nsDependentCString(mozilla::widget::FaviconHelper::kJumpListCacheDir));
      NS_ENSURE_SUCCESS(rv, rv);

      nsCOMPtr entries;
      rv = jumpListCacheDir->GetDirectoryEntries(getter_AddRefs(entries));
      NS_ENSURE_SUCCESS(rv, rv);

      // Loop through each directory entry and remove all ICO files found
      do {
        nsCOMPtr currFile;
        if (NS_FAILED(entries->GetNextFile(getter_AddRefs(currFile))) || !currFile)
          break;

        nsAutoString path;
        if (NS_FAILED(currFile->GetPath(path))) continue;

        if (StringTail(path, 4).LowerCaseEqualsASCII(".ico")) {
          // Check if the cached ICO file exists
          bool exists;
          if (NS_FAILED(currFile->Exists(&exists)) || !exists) continue;

          // We found an ICO file that exists, so we should remove it
          currFile->Remove(false);
        }
      } while (true);

      return NS_OK;
    }

    NS_IMETHODIMP JumpListBuilder::AddListToBuild(int16_t aCatType, nsIArray* items,
                                                  const nsAString& catName,
                                                  bool* _retval) {
      nsresult rv;

      *_retval = false;

      ReentrantMonitorAutoEnter lock(mMonitor);
      if (!mJumpListMgr) return NS_ERROR_NOT_AVAILABLE;

      RefPtr jumpListMgr = mJumpListMgr;
      if (!jumpListMgr) {
        return NS_ERROR_UNEXPECTED;
      }

      switch (aCatType) {
        case nsIJumpListBuilder::JUMPLIST_CATEGORY_TASKS: {
          NS_ENSURE_ARG_POINTER(items);

          HRESULT hr;
          RefPtr collection;
          hr = CoCreateInstance(CLSID_EnumerableObjectCollection, nullptr,
                                CLSCTX_INPROC_SERVER, IID_IObjectCollection,
                                getter_AddRefs(collection));
          if (FAILED(hr)) return NS_ERROR_UNEXPECTED;

          // Build the list
          uint32_t length;
          items->GetLength(&length);
          for (uint32_t i = 0; i < length; ++i) {
            nsCOMPtr item = do_QueryElementAt(items, i);
            if (!item) continue;
            // Check for separators
            if (IsSeparator(item)) {
              RefPtr link;
              rv = JumpListSeparator::GetSeparator(link);
              if (NS_FAILED(rv)) return rv;
              collection->AddObject(link);
              continue;
            }
            // These should all be ShellLinks
            RefPtr link;
            rv = JumpListShortcut::GetShellLink(item, link, mIOThread);
            if (NS_FAILED(rv)) return rv;
            collection->AddObject(link);
          }

          // We need IObjectArray to submit
          RefPtr pArray;
          hr = collection->QueryInterface(IID_IObjectArray, getter_AddRefs(pArray));
          if (FAILED(hr)) return NS_ERROR_UNEXPECTED;

          // Add the tasks
          hr = jumpListMgr->AddUserTasks(pArray);
          if (SUCCEEDED(hr)) *_retval = true;
          return NS_OK;
        } break;
        case nsIJumpListBuilder::JUMPLIST_CATEGORY_RECENT: {
          if (SUCCEEDED(jumpListMgr->AppendKnownCategory(KDC_RECENT)))
            *_retval = true;
          return NS_OK;
        } break;
        case nsIJumpListBuilder::JUMPLIST_CATEGORY_FREQUENT: {
          if (SUCCEEDED(jumpListMgr->AppendKnownCategory(KDC_FREQUENT)))
            *_retval = true;
          return NS_OK;
        } break;
        case nsIJumpListBuilder::JUMPLIST_CATEGORY_CUSTOMLIST: {
          NS_ENSURE_ARG_POINTER(items);

          if (catName.IsEmpty()) return NS_ERROR_INVALID_ARG;

          HRESULT hr;
          RefPtr collection;
          hr = CoCreateInstance(CLSID_EnumerableObjectCollection, nullptr,
                                CLSCTX_INPROC_SERVER, IID_IObjectCollection,
                                getter_AddRefs(collection));
          if (FAILED(hr)) return NS_ERROR_UNEXPECTED;

          uint32_t length;
          items->GetLength(&length);
          for (uint32_t i = 0; i < length; ++i) {
            nsCOMPtr item = do_QueryElementAt(items, i);
            if (!item) continue;
            int16_t type;
            if (NS_FAILED(item->GetType(&type))) continue;
            switch (type) {
              case nsIJumpListItem::JUMPLIST_ITEM_SEPARATOR: {
                RefPtr shellItem;
                rv = JumpListSeparator::GetSeparator(shellItem);
                if (NS_FAILED(rv)) return rv;
                collection->AddObject(shellItem);
              } break;
              case nsIJumpListItem::JUMPLIST_ITEM_LINK: {
                RefPtr shellItem;
                rv = JumpListLink::GetShellItem(item, shellItem);
                if (NS_FAILED(rv)) return rv;
                collection->AddObject(shellItem);
              } break;
              case nsIJumpListItem::JUMPLIST_ITEM_SHORTCUT: {
                RefPtr shellItem;
                rv = JumpListShortcut::GetShellLink(item, shellItem, mIOThread);
                if (NS_FAILED(rv)) return rv;
                collection->AddObject(shellItem);
              } break;
            }
          }

          // We need IObjectArray to submit
          RefPtr pArray;
          hr = collection->QueryInterface(IID_IObjectArray, (LPVOID*)&pArray);
          if (FAILED(hr)) return NS_ERROR_UNEXPECTED;

          // Add the tasks
          hr = jumpListMgr->AppendCategory(
              reinterpret_cast(catName.BeginReading()), pArray);
          if (SUCCEEDED(hr)) *_retval = true;

          // Get rid of the old icons
          nsCOMPtr event =
              new mozilla::widget::AsyncDeleteAllFaviconsFromDisk(true);
          mIOThread->Dispatch(event, NS_DISPATCH_NORMAL);

          return NS_OK;
        } break;
      }
      return NS_OK;
    }

    NS_IMETHODIMP JumpListBuilder::AbortListBuild() {
      ReentrantMonitorAutoEnter lock(mMonitor);
      if (!mJumpListMgr) return NS_ERROR_NOT_AVAILABLE;

      RefPtr jumpListMgr = mJumpListMgr;
      if (!jumpListMgr) {
        return NS_ERROR_UNEXPECTED;
      }

      jumpListMgr->AbortList();
      sBuildingList = false;

      return NS_OK;
    }

    NS_IMETHODIMP JumpListBuilder::CommitListBuild(
        nsIJumpListCommittedCallback* aCallback) {
      ReentrantMonitorAutoEnter lock(mMonitor);
      if (!mJumpListMgr) return NS_ERROR_NOT_AVAILABLE;

      // Also holds a strong reference to this to prevent use-after-free.
      RefPtr callback =
          new detail::DoneCommitListBuildCallback(aCallback, this);

      // The builder has a strong reference in the callback already, so we do not
      // need to do it for this runnable again.
      RefPtr event =
          NewNonOwningRunnableMethod>(
              "JumpListBuilder::DoCommitListBuild", this,
              &JumpListBuilder::DoCommitListBuild, std::move(callback));
      Unused << mIOThread->Dispatch(event, NS_DISPATCH_NORMAL);

      return NS_OK;
    }

    void JumpListBuilder::DoCommitListBuild(
        RefPtr aCallback) {
      // Since we're invoking COM interfaces to talk to the shell on a background
      // thread, we need to be running inside a multithreaded apartment.
      mscom::MTARegion mta;
      MOZ_ASSERT(mta.IsValid());

      ReentrantMonitorAutoEnter lock(mMonitor);
      MOZ_ASSERT(mJumpListMgr);
      MOZ_ASSERT(aCallback);

      HRESULT hr = E_UNEXPECTED;
      auto onExit = MakeScopeExit([&hr, &aCallback]() {
        // XXX We might want some specific error data here.
        aCallback->SetResult(SUCCEEDED(hr));
        Unused << NS_DispatchToMainThread(aCallback);
      });

      RefPtr jumpListMgr = mJumpListMgr;
      if (!jumpListMgr) {
        return;
      }

      hr = jumpListMgr->CommitList();
      sBuildingList = false;

      if (SUCCEEDED(hr)) {
        mHasCommit = true;
      }
    }

    NS_IMETHODIMP JumpListBuilder::DeleteActiveList(bool* _retval) {
      *_retval = false;

      ReentrantMonitorAutoEnter lock(mMonitor);
      if (!mJumpListMgr) return NS_ERROR_NOT_AVAILABLE;

      if (sBuildingList) {
        AbortListBuild();
      }

      nsAutoString uid;
      if (!WinTaskbar::GetAppUserModelID(uid)) return NS_OK;

      RefPtr jumpListMgr = mJumpListMgr;
      if (!jumpListMgr) {
        return NS_ERROR_UNEXPECTED;
      }

      if (SUCCEEDED(jumpListMgr->DeleteList(uid.get()))) {
        *_retval = true;
      }

      return NS_OK;
    }

    /* internal */

    bool JumpListBuilder::IsSeparator(nsCOMPtr& item) {
      int16_t type;
      item->GetType(&type);
      if (NS_FAILED(item->GetType(&type))) return false;

      if (type == nsIJumpListItem::JUMPLIST_ITEM_SEPARATOR) return true;
      return false;
    }

    // RemoveIconCacheAndGetJumplistShortcutURIs - does multiple things to
    // avoid unnecessary extra XPCOM incantations. For each object in the input
    // array,  if it's an IShellLinkW, this deletes the cached icon and adds the
    // url param to a list of URLs to be removed from the places database.
    void JumpListBuilder::RemoveIconCacheAndGetJumplistShortcutURIs(
        IObjectArray* aObjArray, nsTArray& aURISpecs) {
      MOZ_ASSERT(!NS_IsMainThread());

      // Early return here just in case some versions of Windows don't populate this
      if (!aObjArray) {
        return;
      }

      uint32_t count = 0;
      aObjArray->GetCount(&count);

      for (uint32_t idx = 0; idx < count; idx++) {
        RefPtr pLink;

        if (FAILED(aObjArray->GetAt(idx, IID_IShellLinkW,
                                    static_cast(getter_AddRefs(pLink))))) {
          continue;
        }

        wchar_t buf[MAX_PATH];
        HRESULT hres = pLink->GetArguments(buf, MAX_PATH);
        if (SUCCEEDED(hres)) {
          LPWSTR* arglist;
          int32_t numArgs;

          arglist = ::CommandLineToArgvW(buf, &numArgs);
          if (arglist && numArgs > 0) {
            nsString spec(arglist[0]);
            aURISpecs.AppendElement(std::move(spec));
            ::LocalFree(arglist);
          }
        }

        int iconIdx = 0;
        hres = pLink->GetIconLocation(buf, MAX_PATH, &iconIdx);
        if (SUCCEEDED(hres)) {
          nsDependentString spec(buf);
          DeleteIconFromDisk(spec);
        }
      }
    }

    void JumpListBuilder::DeleteIconFromDisk(const nsAString& aPath) {
      MOZ_ASSERT(!NS_IsMainThread());

      // Check that we aren't deleting some arbitrary file that is not an icon
      if (StringTail(aPath, 4).LowerCaseEqualsASCII(".ico")) {
        // Construct the parent path of the passed in path
        nsCOMPtr icoFile;
        nsresult rv = NS_NewLocalFile(aPath, true, getter_AddRefs(icoFile));
        if (NS_WARN_IF(NS_FAILED(rv))) {
          return;
        }

        icoFile->Remove(false);
      }
    }

    NS_IMETHODIMP JumpListBuilder::Observe(nsISupports* aSubject,
                                           const char* aTopic,
                                           const char16_t* aData) {
      NS_ENSURE_ARG_POINTER(aTopic);
      if (strcmp(aTopic, TOPIC_PROFILE_BEFORE_CHANGE) == 0) {
        nsCOMPtr observerService =
            do_GetService("@mozilla.org/observer-service;1");
        if (observerService) {
          observerService->RemoveObserver(this, TOPIC_PROFILE_BEFORE_CHANGE);
        }
        mIOThread->Shutdown();
        // Clear out mJumpListMgr, as MSCOM services won't be available soon.
        ReentrantMonitorAutoEnter lock(mMonitor);
        mJumpListMgr = nullptr;
      } else if (strcmp(aTopic, "nsPref:changed") == 0 &&
                 nsDependentString(aData).EqualsASCII(kPrefTaskbarEnabled)) {
        bool enabled = Preferences::GetBool(kPrefTaskbarEnabled, true);
        if (!enabled) {
          nsCOMPtr event =
              new mozilla::widget::AsyncDeleteAllFaviconsFromDisk();
          mIOThread->Dispatch(event, NS_DISPATCH_NORMAL);
        }
      } else if (strcmp(aTopic, TOPIC_CLEAR_PRIVATE_DATA) == 0) {
        // Delete JumpListCache icons from Disk, if any.
        nsCOMPtr event =
            new mozilla::widget::AsyncDeleteAllFaviconsFromDisk(false);
        mIOThread->Dispatch(event, NS_DISPATCH_NORMAL);
      }
      return NS_OK;
    }

    }  // namespace widget

## Minimal test - lines (36, 635)

path: .spaces[0].metrics.loc.ploc
old: 102.0
new: 446.0
path: .spaces[0].metrics.loc.cloc
old: 6.0
new: 47.0
path: .spaces[0].metrics.loc.blank
old: 16.0
new: 107.0
path: .spaces[0].metrics.loc.lloc
old: 23.0
new: 248.0
path: .spaces[0].metrics.loc.sloc
old: 124.0
new: 600.0
path: .spaces[0].metrics.mi.mi_sei
old: 11.236740664820765
new: -56.38158897229136
path: .spaces[0].metrics.mi.mi_visual_studio
old: 27.80594149774381
new: 0.0
path: .spaces[0].metrics.mi.mi_original
old: 47.548159961141906
new: -8.862338210928172
path: .spaces[0].metrics.cyclomatic.average
old: 1.7777777777777777
new: 4.36
path: .spaces[0].metrics.cyclomatic.sum
old: 16.0
new: 109.0
path: .spaces[0].metrics.nom.total
old: 6.0
new: 26.0
path: .spaces[0].metrics.nom.closures
old: 0.0
new: 5.0
path: .spaces[0].metrics.nom.functions
old: 6.0
new: 21.0
path: .spaces[0].metrics.cognitive.average
old: 1.8333333333333333
new: 4.961538461538462
path: .spaces[0].metrics.cognitive.sum
old: 11.0
new: 129.0
path: .spaces[0].metrics.halstead.effort
old: 90053.81149634879
new: 1106189.2218168245
path: .spaces[0].metrics.halstead.length
old: 460.0
new: 2292.0
path: .spaces[0].metrics.halstead.volume
old: 3029.082750331732
new: 18748.35163431423
path: .spaces[0].metrics.halstead.N1
old: 260.0
new: 1373.0
path: .spaces[0].metrics.halstead.purity_ratio
old: 1.2121892275360062
new: 0.9702925458471447
path: .spaces[0].metrics.halstead.estimated_program_length
old: 557.6070446665628
new: 2223.910515081656
path: .spaces[0].metrics.halstead.n1
old: 22.0
new: 33.0
path: .spaces[0].metrics.halstead.bugs
old: 0.6696997619337162
new: 3.5653187271237443
path: .spaces[0].metrics.halstead.N2
old: 200.0
new: 919.0
path: .spaces[0].metrics.halstead.vocabulary
old: 96.0
new: 290.0
path: .spaces[0].metrics.halstead.n2
old: 74.0
new: 257.0
path: .spaces[0].metrics.halstead.difficulty
old: 29.72972972972973
new: 59.00194552529183
path: .spaces[0].metrics.halstead.time
old: 5002.989527574933
new: 61454.95676760135
path: .spaces[0].metrics.halstead.level
old: 0.03363636363636364
new: 0.016948593662413032
path: .spaces[0].metrics.nargs.average
old: 1.8333333333333333
new: 0.8076923076923077
path: .spaces[0].metrics.nargs.sum
old: 11.0
new: 21.0
path: .spaces[0].metrics.nexits.sum
old: 6.0
new: 52.0
path: .spaces[0].metrics.nexits.average
old: 1.0
new: 2.0

### Code

    namespace mozilla {
    namespace widget {

    // defined in WinTaskbar.cpp
    extern const wchar_t* gMozillaJumpListIDGeneric;

    Atomic JumpListBuilder::sBuildingList(false);
    const char kPrefTaskbarEnabled[] = "browser.taskbar.lists.enabled";

    NS_IMPL_ISUPPORTS(JumpListBuilder, nsIJumpListBuilder, nsIObserver)
    #define TOPIC_PROFILE_BEFORE_CHANGE "profile-before-change"
    #define TOPIC_CLEAR_PRIVATE_DATA "clear-private-data"

    namespace detail {

    class DoneCommitListBuildCallback final : public nsIRunnable {
      NS_DECL_THREADSAFE_ISUPPORTS

     public:
      DoneCommitListBuildCallback(nsIJumpListCommittedCallback* aCallback,
                                  JumpListBuilder* aBuilder)
          : mCallback(aCallback), mBuilder(aBuilder), mResult(false) {}

      NS_IMETHOD Run() override {
        MOZ_ASSERT(NS_IsMainThread());
        if (mCallback) {
          Unused << mCallback->Done(mResult);
        }
        // Ensure we are releasing on the main thread.
        Destroy();
        return NS_OK;
      }

      void SetResult(bool aResult) { mResult = aResult; }

     private:
      ~DoneCommitListBuildCallback() {
        // Destructor does not always call on the main thread.
        MOZ_ASSERT(!mCallback);
        MOZ_ASSERT(!mBuilder);
      }

      void Destroy() {
        MOZ_ASSERT(NS_IsMainThread());
        mCallback = nullptr;
        mBuilder = nullptr;
      }

      // These two references MUST be released on the main thread.
      RefPtr mCallback;
      RefPtr mBuilder;
      bool mResult;
    };

    NS_IMPL_ISUPPORTS(DoneCommitListBuildCallback, nsIRunnable);

    }  // namespace detail

    JumpListBuilder::JumpListBuilder()
        : mMaxItems(0), mHasCommit(false), mMonitor("JumpListBuilderMonitor") {
      MOZ_ASSERT(NS_IsMainThread());

      // Instantiate mJumpListMgr in the multithreaded apartment so that proxied
      // calls on that object do not need to interact with the main thread's message
      // pump.
      mscom::EnsureMTA([this]() {
        RefPtr jumpListMgr;
        HRESULT hr = ::CoCreateInstance(
            CLSID_DestinationList, nullptr, CLSCTX_INPROC_SERVER,
            IID_ICustomDestinationList, getter_AddRefs(jumpListMgr));
        if (FAILED(hr)) {
          return;
        }

        // Since we are accessing mJumpListMgr across different threads
        // (ie, different apartments), mJumpListMgr must be an agile reference.
        mJumpListMgr = jumpListMgr;
      });

      if (!mJumpListMgr) {
        return;
      }

      // Make a lazy thread for any IO
      mIOThread = new LazyIdleThread(DEFAULT_THREAD_TIMEOUT_MS, "Jump List"_ns,
                                     LazyIdleThread::ManualShutdown);
      Preferences::AddStrongObserver(this, kPrefTaskbarEnabled);

      nsCOMPtr observerService =
          do_GetService("@mozilla.org/observer-service;1");
      if (observerService) {
        observerService->AddObserver(this, TOPIC_PROFILE_BEFORE_CHANGE, false);
        observerService->AddObserver(this, TOPIC_CLEAR_PRIVATE_DATA, false);
      }

      RefPtr jumpListMgr = mJumpListMgr;
      if (!jumpListMgr) {
        return;
      }

      // GetAppUserModelID can only be called once we're back on the main thread.
      nsString modelId;
      if (mozilla::widget::WinTaskbar::GetAppUserModelID(modelId)) {
        jumpListMgr->SetAppID(modelId.get());
      }
    }

    JumpListBuilder::~JumpListBuilder() {
      Preferences::RemoveObserver(this, kPrefTaskbarEnabled);
    }

    NS_IMETHODIMP JumpListBuilder::GetAvailable(int16_t* aAvailable) {
      *aAvailable = false;

      ReentrantMonitorAutoEnter lock(mMonitor);
      if (mJumpListMgr) *aAvailable = true;

      return NS_OK;
    }

    NS_IMETHODIMP JumpListBuilder::GetIsListCommitted(bool* aCommit) {
      *aCommit = mHasCommit;

      return NS_OK;
    }

    NS_IMETHODIMP JumpListBuilder::GetMaxListItems(int16_t* aMaxItems) {
      ReentrantMonitorAutoEnter lock(mMonitor);
      if (!mJumpListMgr) return NS_ERROR_NOT_AVAILABLE;

      *aMaxItems = 0;

      if (sBuildingList) {
        *aMaxItems = mMaxItems;
        return NS_OK;
      }

      RefPtr jumpListMgr = mJumpListMgr;
      if (!jumpListMgr) {
        return NS_ERROR_UNEXPECTED;
      }

      IObjectArray* objArray;
      if (SUCCEEDED(jumpListMgr->BeginList(&mMaxItems, IID_PPV_ARGS(&objArray)))) {
        *aMaxItems = mMaxItems;

        if (objArray) objArray->Release();

        jumpListMgr->AbortList();
      }

      return NS_OK;
    }

    NS_IMETHODIMP JumpListBuilder::InitListBuild(JSContext* aCx,
                                                 Promise** aPromise) {
      ReentrantMonitorAutoEnter lock(mMonitor);
      if (!mJumpListMgr) {
        return NS_ERROR_NOT_AVAILABLE;
      }

      nsIGlobalObject* globalObject = xpc::CurrentNativeGlobal(aCx);
      if (NS_WARN_IF(!globalObject)) {
        return NS_ERROR_FAILURE;
      }

      ErrorResult result;
      RefPtr promise = Promise::Create(globalObject, result);
      if (NS_WARN_IF(result.Failed())) {
        return result.StealNSResult();
      }

      nsCOMPtr runnable =
          NewRunnableMethod>>(
              "InitListBuild", this, &JumpListBuilder::DoInitListBuild, promise);
      nsresult rv = mIOThread->Dispatch(runnable, NS_DISPATCH_NORMAL);
      if (NS_WARN_IF(NS_FAILED(rv))) {
        return rv;
      }

      promise.forget(aPromise);
      return NS_OK;
    }

    void JumpListBuilder::DoInitListBuild(RefPtr&& aPromise) {
      // Since we're invoking COM interfaces to talk to the shell on a background
      // thread, we need to be running inside a multithreaded apartment.
      mscom::MTARegion mta;
      MOZ_ASSERT(mta.IsValid());

      ReentrantMonitorAutoEnter lock(mMonitor);
      MOZ_ASSERT(mJumpListMgr);

      if (sBuildingList) {
        AbortListBuild();
      }

      HRESULT hr = E_UNEXPECTED;
      auto errorHandler = MakeScopeExit([&aPromise, &hr]() {
        if (SUCCEEDED(hr)) {
          return;
        }

        NS_DispatchToMainThread(NS_NewRunnableFunction(
            "InitListBuildReject", [promise = std::move(aPromise)]() {
              promise->MaybeReject(NS_ERROR_FAILURE);
            }));
      });

      RefPtr jumpListMgr = mJumpListMgr;
      if (!jumpListMgr) {
        return;
      }

      nsTArray urisToRemove;
      RefPtr objArray;
      hr = jumpListMgr->BeginList(
          &mMaxItems,
          IID_PPV_ARGS(static_cast(getter_AddRefs(objArray))));
      if (FAILED(hr)) {
        return;
      }

      // The returned objArray of removed items are for manually removed items.
      // This does not return items which are removed because they were previously
      // part of the jump list but are no longer part of the jump list.
      sBuildingList = true;
      RemoveIconCacheAndGetJumplistShortcutURIs(objArray, urisToRemove);

      NS_DispatchToMainThread(NS_NewRunnableFunction(
          "InitListBuildResolve", [urisToRemove = std::move(urisToRemove),
                                   promise = std::move(aPromise)]() {
            promise->MaybeResolve(urisToRemove);
          }));
    }

    // Ensures that we have no old ICO files left in the jump list cache
    nsresult JumpListBuilder::RemoveIconCacheForAllItems() {
      // Construct the path of our jump list cache
      nsCOMPtr jumpListCacheDir;
      nsresult rv =
          NS_GetSpecialDirectory("ProfLDS", getter_AddRefs(jumpListCacheDir));
      NS_ENSURE_SUCCESS(rv, rv);
      rv = jumpListCacheDir->AppendNative(
          nsDependentCString(mozilla::widget::FaviconHelper::kJumpListCacheDir));
      NS_ENSURE_SUCCESS(rv, rv);

      nsCOMPtr entries;
      rv = jumpListCacheDir->GetDirectoryEntries(getter_AddRefs(entries));
      NS_ENSURE_SUCCESS(rv, rv);

      // Loop through each directory entry and remove all ICO files found
      do {
        nsCOMPtr currFile;
        if (NS_FAILED(entries->GetNextFile(getter_AddRefs(currFile))) || !currFile)
          break;

        nsAutoString path;
        if (NS_FAILED(currFile->GetPath(path))) continue;

        if (StringTail(path, 4).LowerCaseEqualsASCII(".ico")) {
          // Check if the cached ICO file exists
          bool exists;
          if (NS_FAILED(currFile->Exists(&exists)) || !exists) continue;

          // We found an ICO file that exists, so we should remove it
          currFile->Remove(false);
        }
      } while (true);

      return NS_OK;
    }

    NS_IMETHODIMP JumpListBuilder::AddListToBuild(int16_t aCatType, nsIArray* items,
                                                  const nsAString& catName,
                                                  bool* _retval) {
      nsresult rv;

      *_retval = false;

      ReentrantMonitorAutoEnter lock(mMonitor);
      if (!mJumpListMgr) return NS_ERROR_NOT_AVAILABLE;

      RefPtr jumpListMgr = mJumpListMgr;
      if (!jumpListMgr) {
        return NS_ERROR_UNEXPECTED;
      }

      switch (aCatType) {
        case nsIJumpListBuilder::JUMPLIST_CATEGORY_TASKS: {
          NS_ENSURE_ARG_POINTER(items);

          HRESULT hr;
          RefPtr collection;
          hr = CoCreateInstance(CLSID_EnumerableObjectCollection, nullptr,
                                CLSCTX_INPROC_SERVER, IID_IObjectCollection,
                                getter_AddRefs(collection));
          if (FAILED(hr)) return NS_ERROR_UNEXPECTED;

          // Build the list
          uint32_t length;
          items->GetLength(&length);
          for (uint32_t i = 0; i < length; ++i) {
            nsCOMPtr item = do_QueryElementAt(items, i);
            if (!item) continue;
            // Check for separators
            if (IsSeparator(item)) {
              RefPtr link;
              rv = JumpListSeparator::GetSeparator(link);
              if (NS_FAILED(rv)) return rv;
              collection->AddObject(link);
              continue;
            }
            // These should all be ShellLinks
            RefPtr link;
            rv = JumpListShortcut::GetShellLink(item, link, mIOThread);
            if (NS_FAILED(rv)) return rv;
            collection->AddObject(link);
          }

          // We need IObjectArray to submit
          RefPtr pArray;
          hr = collection->QueryInterface(IID_IObjectArray, getter_AddRefs(pArray));
          if (FAILED(hr)) return NS_ERROR_UNEXPECTED;

          // Add the tasks
          hr = jumpListMgr->AddUserTasks(pArray);
          if (SUCCEEDED(hr)) *_retval = true;
          return NS_OK;
        } break;
        case nsIJumpListBuilder::JUMPLIST_CATEGORY_RECENT: {
          if (SUCCEEDED(jumpListMgr->AppendKnownCategory(KDC_RECENT)))
            *_retval = true;
          return NS_OK;
        } break;
        case nsIJumpListBuilder::JUMPLIST_CATEGORY_FREQUENT: {
          if (SUCCEEDED(jumpListMgr->AppendKnownCategory(KDC_FREQUENT)))
            *_retval = true;
          return NS_OK;
        } break;
        case nsIJumpListBuilder::JUMPLIST_CATEGORY_CUSTOMLIST: {
          NS_ENSURE_ARG_POINTER(items);

          if (catName.IsEmpty()) return NS_ERROR_INVALID_ARG;

          HRESULT hr;
          RefPtr collection;
          hr = CoCreateInstance(CLSID_EnumerableObjectCollection, nullptr,
                                CLSCTX_INPROC_SERVER, IID_IObjectCollection,
                                getter_AddRefs(collection));
          if (FAILED(hr)) return NS_ERROR_UNEXPECTED;

          uint32_t length;
          items->GetLength(&length);
          for (uint32_t i = 0; i < length; ++i) {
            nsCOMPtr item = do_QueryElementAt(items, i);
            if (!item) continue;
            int16_t type;
            if (NS_FAILED(item->GetType(&type))) continue;
            switch (type) {
              case nsIJumpListItem::JUMPLIST_ITEM_SEPARATOR: {
                RefPtr shellItem;
                rv = JumpListSeparator::GetSeparator(shellItem);
                if (NS_FAILED(rv)) return rv;
                collection->AddObject(shellItem);
              } break;
              case nsIJumpListItem::JUMPLIST_ITEM_LINK: {
                RefPtr shellItem;
                rv = JumpListLink::GetShellItem(item, shellItem);
                if (NS_FAILED(rv)) return rv;
                collection->AddObject(shellItem);
              } break;
              case nsIJumpListItem::JUMPLIST_ITEM_SHORTCUT: {
                RefPtr shellItem;
                rv = JumpListShortcut::GetShellLink(item, shellItem, mIOThread);
                if (NS_FAILED(rv)) return rv;
                collection->AddObject(shellItem);
              } break;
            }
          }

          // We need IObjectArray to submit
          RefPtr pArray;
          hr = collection->QueryInterface(IID_IObjectArray, (LPVOID*)&pArray);
          if (FAILED(hr)) return NS_ERROR_UNEXPECTED;

          // Add the tasks
          hr = jumpListMgr->AppendCategory(
              reinterpret_cast(catName.BeginReading()), pArray);
          if (SUCCEEDED(hr)) *_retval = true;

          // Get rid of the old icons
          nsCOMPtr event =
              new mozilla::widget::AsyncDeleteAllFaviconsFromDisk(true);
          mIOThread->Dispatch(event, NS_DISPATCH_NORMAL);

          return NS_OK;
        } break;
      }
      return NS_OK;
    }

    NS_IMETHODIMP JumpListBuilder::AbortListBuild() {
      ReentrantMonitorAutoEnter lock(mMonitor);
      if (!mJumpListMgr) return NS_ERROR_NOT_AVAILABLE;

      RefPtr jumpListMgr = mJumpListMgr;
      if (!jumpListMgr) {
        return NS_ERROR_UNEXPECTED;
      }

      jumpListMgr->AbortList();
      sBuildingList = false;

      return NS_OK;
    }

    NS_IMETHODIMP JumpListBuilder::CommitListBuild(
        nsIJumpListCommittedCallback* aCallback) {
      ReentrantMonitorAutoEnter lock(mMonitor);
      if (!mJumpListMgr) return NS_ERROR_NOT_AVAILABLE;

      // Also holds a strong reference to this to prevent use-after-free.
      RefPtr callback =
          new detail::DoneCommitListBuildCallback(aCallback, this);

      // The builder has a strong reference in the callback already, so we do not
      // need to do it for this runnable again.
      RefPtr event =
          NewNonOwningRunnableMethod>(
              "JumpListBuilder::DoCommitListBuild", this,
              &JumpListBuilder::DoCommitListBuild, std::move(callback));
      Unused << mIOThread->Dispatch(event, NS_DISPATCH_NORMAL);

      return NS_OK;
    }

    void JumpListBuilder::DoCommitListBuild(
        RefPtr aCallback) {
      // Since we're invoking COM interfaces to talk to the shell on a background
      // thread, we need to be running inside a multithreaded apartment.
      mscom::MTARegion mta;
      MOZ_ASSERT(mta.IsValid());

      ReentrantMonitorAutoEnter lock(mMonitor);
      MOZ_ASSERT(mJumpListMgr);
      MOZ_ASSERT(aCallback);

      HRESULT hr = E_UNEXPECTED;
      auto onExit = MakeScopeExit([&hr, &aCallback]() {
        // XXX We might want some specific error data here.
        aCallback->SetResult(SUCCEEDED(hr));
        Unused << NS_DispatchToMainThread(aCallback);
      });

      RefPtr jumpListMgr = mJumpListMgr;
      if (!jumpListMgr) {
        return;
      }

      hr = jumpListMgr->CommitList();
      sBuildingList = false;

      if (SUCCEEDED(hr)) {
        mHasCommit = true;
      }
    }

    NS_IMETHODIMP JumpListBuilder::DeleteActiveList(bool* _retval) {
      *_retval = false;

      ReentrantMonitorAutoEnter lock(mMonitor);
      if (!mJumpListMgr) return NS_ERROR_NOT_AVAILABLE;

      if (sBuildingList) {
        AbortListBuild();
      }

      nsAutoString uid;
      if (!WinTaskbar::GetAppUserModelID(uid)) return NS_OK;

      RefPtr jumpListMgr = mJumpListMgr;
      if (!jumpListMgr) {
        return NS_ERROR_UNEXPECTED;
      }

      if (SUCCEEDED(jumpListMgr->DeleteList(uid.get()))) {
        *_retval = true;
      }

      return NS_OK;
    }

    /* internal */

    bool JumpListBuilder::IsSeparator(nsCOMPtr& item) {
      int16_t type;
      item->GetType(&type);
      if (NS_FAILED(item->GetType(&type))) return false;

      if (type == nsIJumpListItem::JUMPLIST_ITEM_SEPARATOR) return true;
      return false;
    }

    // RemoveIconCacheAndGetJumplistShortcutURIs - does multiple things to
    // avoid unnecessary extra XPCOM incantations. For each object in the input
    // array,  if it's an IShellLinkW, this deletes the cached icon and adds the
    // url param to a list of URLs to be removed from the places database.
    void JumpListBuilder::RemoveIconCacheAndGetJumplistShortcutURIs(
        IObjectArray* aObjArray, nsTArray& aURISpecs) {
      MOZ_ASSERT(!NS_IsMainThread());

      // Early return here just in case some versions of Windows don't populate this
      if (!aObjArray) {
        return;
      }

      uint32_t count = 0;
      aObjArray->GetCount(&count);

      for (uint32_t idx = 0; idx < count; idx++) {
        RefPtr pLink;

        if (FAILED(aObjArray->GetAt(idx, IID_IShellLinkW,
                                    static_cast(getter_AddRefs(pLink))))) {
          continue;
        }

        wchar_t buf[MAX_PATH];
        HRESULT hres = pLink->GetArguments(buf, MAX_PATH);
        if (SUCCEEDED(hres)) {
          LPWSTR* arglist;
          int32_t numArgs;

          arglist = ::CommandLineToArgvW(buf, &numArgs);
          if (arglist && numArgs > 0) {
            nsString spec(arglist[0]);
            aURISpecs.AppendElement(std::move(spec));
            ::LocalFree(arglist);
          }
        }

        int iconIdx = 0;
        hres = pLink->GetIconLocation(buf, MAX_PATH, &iconIdx);
        if (SUCCEEDED(hres)) {
          nsDependentString spec(buf);
          DeleteIconFromDisk(spec);
        }
      }
    }

    void JumpListBuilder::DeleteIconFromDisk(const nsAString& aPath) {
      MOZ_ASSERT(!NS_IsMainThread());

      // Check that we aren't deleting some arbitrary file that is not an icon
      if (StringTail(aPath, 4).LowerCaseEqualsASCII(".ico")) {
        // Construct the parent path of the passed in path
        nsCOMPtr icoFile;
        nsresult rv = NS_NewLocalFile(aPath, true, getter_AddRefs(icoFile));
        if (NS_WARN_IF(NS_FAILED(rv))) {
          return;
        }

        icoFile->Remove(false);
      }
    }

    NS_IMETHODIMP JumpListBuilder::Observe(nsISupports* aSubject,
                                           const char* aTopic,
                                           const char16_t* aData) {
      NS_ENSURE_ARG_POINTER(aTopic);
      if (strcmp(aTopic, TOPIC_PROFILE_BEFORE_CHANGE) == 0) {
        nsCOMPtr observerService =
            do_GetService("@mozilla.org/observer-service;1");
        if (observerService) {
          observerService->RemoveObserver(this, TOPIC_PROFILE_BEFORE_CHANGE);
        }
        mIOThread->Shutdown();
        // Clear out mJumpListMgr, as MSCOM services won't be available soon.
        ReentrantMonitorAutoEnter lock(mMonitor);
        mJumpListMgr = nullptr;
      } else if (strcmp(aTopic, "nsPref:changed") == 0 &&
                 nsDependentString(aData).EqualsASCII(kPrefTaskbarEnabled)) {
        bool enabled = Preferences::GetBool(kPrefTaskbarEnabled, true);
        if (!enabled) {
          nsCOMPtr event =
              new mozilla::widget::AsyncDeleteAllFaviconsFromDisk();
          mIOThread->Dispatch(event, NS_DISPATCH_NORMAL);
        }
      } else if (strcmp(aTopic, TOPIC_CLEAR_PRIVATE_DATA) == 0) {
        // Delete JumpListCache icons from Disk, if any.
        nsCOMPtr event =
            new mozilla::widget::AsyncDeleteAllFaviconsFromDisk(false);
        mIOThread->Dispatch(event, NS_DISPATCH_NORMAL);
      }
      return NS_OK;
    }

    }  // namespace widget
    }  // namespace mozilla

## Minimal test - lines (49, 92)

path: .spaces[0].spaces[0].spaces[0].metrics.cyclomatic.sum
old: 4.0
new: 7.0
path: .spaces[0].spaces[0].spaces[0].metrics.cyclomatic.average
old: 1.3333333333333333
new: 1.1666666666666667
path: .spaces[0].spaces[0].spaces[0].metrics.nargs.sum
old: 3.0
new: 1.0
path: .spaces[0].spaces[0].spaces[0].metrics.nargs.average
old: 1.5
new: 0.25
path: .spaces[0].spaces[0].spaces[0].metrics.halstead.n1
old: 12.0
new: 16.0
path: .spaces[0].spaces[0].spaces[0].metrics.halstead.N2
old: 29.0
new: 53.0
path: .spaces[0].spaces[0].spaces[0].metrics.halstead.level
old: 0.09770114942528736
new: 0.06132075471698114
path: .spaces[0].spaces[0].spaces[0].metrics.halstead.effort
old: 3331.431908305719
new: 10816.159162798382
path: .spaces[0].spaces[0].spaces[0].metrics.halstead.difficulty
old: 10.235294117647058
new: 16.307692307692307
path: .spaces[0].spaces[0].spaces[0].metrics.halstead.length
old: 67.0
new: 123.0
path: .spaces[0].spaces[0].spaces[0].metrics.halstead.estimated_program_length
old: 112.50641830990963
new: 186.2114326716684
path: .spaces[0].spaces[0].spaces[0].metrics.halstead.n2
old: 17.0
new: 26.0
path: .spaces[0].spaces[0].spaces[0].metrics.halstead.vocabulary
old: 29.0
new: 42.0
path: .spaces[0].spaces[0].spaces[0].metrics.halstead.N1
old: 38.0
new: 70.0
path: .spaces[0].spaces[0].spaces[0].metrics.halstead.volume
old: 325.4847266735473
new: 663.2550430017876
path: .spaces[0].spaces[0].spaces[0].metrics.halstead.time
old: 185.0795504614288
new: 600.8977312665768
path: .spaces[0].spaces[0].spaces[0].metrics.halstead.purity_ratio
old: 1.679200273282233
new: 1.5139140867615315
path: .spaces[0].spaces[0].spaces[0].metrics.halstead.bugs
old: 0.07435315006223539
new: 0.16302747101569456
path: .spaces[0].spaces[0].spaces[0].metrics.mi.mi_sei
old: 55.52293836125831
new: 51.88429764297507
path: .spaces[0].spaces[0].spaces[0].metrics.mi.mi_original
old: 90.67509571187436
new: 74.30089802893522
path: .spaces[0].spaces[0].spaces[0].metrics.mi.mi_visual_studio
old: 53.026371761330026
new: 43.450817560780834
path: .spaces[0].spaces[0].spaces[0].metrics.nexits.average
old: 0.0
new: 0.25
path: .spaces[0].spaces[0].spaces[0].metrics.nexits.sum
old: 0.0
new: 1.0
path: .spaces[0].spaces[0].spaces[0].metrics.nom.total
old: 2.0
new: 4.0
path: .spaces[0].spaces[0].spaces[0].metrics.nom.functions
old: 2.0
new: 4.0
path: .spaces[0].spaces[0].spaces[0].metrics.loc.blank
old: 2.0
new: 9.0
path: .spaces[0].spaces[0].spaces[0].metrics.loc.sloc
old: 21.0
new: 44.0
path: .spaces[0].spaces[0].spaces[0].metrics.loc.cloc
old: 0.0
new: 3.0
path: .spaces[0].spaces[0].spaces[0].metrics.loc.ploc
old: 19.0
new: 32.0
path: .spaces[0].spaces[0].spaces[0].metrics.loc.lloc
old: 2.0
new: 12.0
path: .spaces[0].spaces[0].spaces[0].metrics.cognitive.average
old: 0.5
new: 0.25

### Code

    namespace detail {

    class DoneCommitListBuildCallback final : public nsIRunnable {
      NS_DECL_THREADSAFE_ISUPPORTS

     public:
      DoneCommitListBuildCallback(nsIJumpListCommittedCallback* aCallback,
                                  JumpListBuilder* aBuilder)
          : mCallback(aCallback), mBuilder(aBuilder), mResult(false) {}

      NS_IMETHOD Run() override {
        MOZ_ASSERT(NS_IsMainThread());
        if (mCallback) {
          Unused << mCallback->Done(mResult);
        }
        // Ensure we are releasing on the main thread.
        Destroy();
        return NS_OK;
      }

      void SetResult(bool aResult) { mResult = aResult; }

     private:
      ~DoneCommitListBuildCallback() {
        // Destructor does not always call on the main thread.
        MOZ_ASSERT(!mCallback);
        MOZ_ASSERT(!mBuilder);
      }

      void Destroy() {
        MOZ_ASSERT(NS_IsMainThread());
        mCallback = nullptr;
        mBuilder = nullptr;
      }

      // These two references MUST be released on the main thread.
      RefPtr mCallback;
      RefPtr mBuilder;
      bool mResult;
    };

    NS_IMPL_ISUPPORTS(DoneCommitListBuildCallback, nsIRunnable);

    }  // namespace detail

## Minimal test - lines (51, 88)

path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.nargs.sum
old: 2.0
new: 1.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.nargs.average
old: 2.0
new: 0.25
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.halstead.purity_ratio
old: 1.7241892855119398
new: 1.526658772081647
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.halstead.vocabulary
old: 15.0
new: 40.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.halstead.difficulty
old: 3.25
new: 16.333333333333332
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.halstead.n2
old: 10.0
new: 24.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.halstead.time
old: 18.340680851606656
new: 550.5238951489039
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.halstead.level
old: 0.3076923076923077
new: 0.06122448979591837
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.halstead.N1
old: 13.0
new: 65.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.halstead.bugs
old: 0.01592223579483085
new: 0.15378401596318086
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.halstead.estimated_program_length
old: 44.82892142331043
new: 174.03910001730776
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.halstead.n1
old: 5.0
new: 16.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.halstead.volume
old: 101.57915548582147
new: 606.6998028171594
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.halstead.length
old: 26.0
new: 114.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.halstead.N2
old: 13.0
new: 49.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.halstead.effort
old: 330.1322553289198
new: 9909.43011268027
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.nexits.sum
old: 0.0
new: 1.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.nexits.average
old: 0.0
new: 0.25
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.mi.mi_sei
old: 98.48916987523094
new: 57.61388743111988
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.mi.mi_original
old: 120.668746390484
new: 77.36932684074768
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.mi.mi_visual_studio
old: 70.56651835700818
new: 45.24522037470624
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.cognitive.average
old: 0.0
new: 0.25
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.cognitive.sum
old: 0.0
new: 1.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.loc.blank
old: 0.0
new: 6.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.loc.sloc
old: 5.0
new: 38.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.loc.cloc
old: 0.0
new: 3.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.loc.ploc
old: 5.0
new: 29.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.loc.lloc
old: 0.0
new: 11.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.nom.functions
old: 1.0
new: 4.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.nom.total
old: 1.0
new: 4.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.cyclomatic.sum
old: 1.0
new: 6.0
path: .spaces[0].spaces[0].spaces[0].spaces[0].metrics.cyclomatic.average
old: 1.0
new: 1.2

### Code

    class DoneCommitListBuildCallback final : public nsIRunnable {
      NS_DECL_THREADSAFE_ISUPPORTS

     public:
      DoneCommitListBuildCallback(nsIJumpListCommittedCallback* aCallback,
                                  JumpListBuilder* aBuilder)
          : mCallback(aCallback), mBuilder(aBuilder), mResult(false) {}

      NS_IMETHOD Run() override {
        MOZ_ASSERT(NS_IsMainThread());
        if (mCallback) {
          Unused << mCallback->Done(mResult);
        }
        // Ensure we are releasing on the main thread.
        Destroy();
        return NS_OK;
      }

      void SetResult(bool aResult) { mResult = aResult; }

     private:
      ~DoneCommitListBuildCallback() {
        // Destructor does not always call on the main thread.
        MOZ_ASSERT(!mCallback);
        MOZ_ASSERT(!mBuilder);
      }

      void Destroy() {
        MOZ_ASSERT(NS_IsMainThread());
        mCallback = nullptr;
        mBuilder = nullptr;
      }

      // These two references MUST be released on the main thread.
      RefPtr mCallback;
      RefPtr mBuilder;
      bool mResult;
    };