Bug 1941046 - Part 4: Send a callback request for impression and clicks of MARS Top...
[gecko.git] / dom / quota / NotifyUtilsCommon.cpp
blobcca80d1b4868763bc84af51db98cabd452fea530
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/. */
7 #include "mozilla/dom/quota/NotifyUtilsCommon.h"
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"
17 namespace mozilla::dom::quota {
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);
28 nsCOMPtr<nsISupports> subject;
29 if (subjectGetter) {
30 subject = subjectGetter();
33 observerService->NotifyObservers(subject, topic, u"");
36 MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(
37 NS_NewRunnableFunction("dom::quota::NotifyObserversOnMainThread",
38 std::move(mainThreadFunction))));
41 } // namespace mozilla::dom::quota