tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

NotifyUtilsCommon.cpp (1424B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "mozilla/dom/quota/NotifyUtilsCommon.h"
      8 
      9 #include "mozilla/Services.h"
     10 #include "mozilla/dom/quota/QuotaCommon.h"
     11 #include "mozilla/dom/quota/ResultExtensions.h"
     12 #include "nsCOMPtr.h"
     13 #include "nsError.h"
     14 #include "nsIObserverService.h"
     15 #include "nsThreadUtils.h"
     16 
     17 namespace mozilla::dom::quota {
     18 
     19 void NotifyObserversOnMainThread(
     20    const char* aTopic,
     21    std::function<already_AddRefed<nsISupports>()>&& aSubjectGetter) {
     22  auto mainThreadFunction = [topic = aTopic,
     23                             subjectGetter = std::move(aSubjectGetter)]() {
     24    nsCOMPtr<nsIObserverService> observerService =
     25        mozilla::services::GetObserverService();
     26    QM_TRY(MOZ_TO_RESULT(observerService), QM_VOID);
     27 
     28    nsCOMPtr<nsISupports> subject;
     29    if (subjectGetter) {
     30      subject = subjectGetter();
     31    }
     32 
     33    observerService->NotifyObservers(subject, topic, u"");
     34  };
     35 
     36  MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(
     37      NS_NewRunnableFunction("dom::quota::NotifyObserversOnMainThread",
     38                             std::move(mainThreadFunction))));
     39 }
     40 
     41 }  // namespace mozilla::dom::quota