Bug 1943650 - Command-line --help output misformatted after --dbus-service. r=emilio
[gecko.git] / toolkit / components / alerts / AlertNotificationIPCSerializer.h
blobbdec9a8f476e434f9597437f7c4a2ac0b7a9a19d
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef mozilla_AlertNotificationIPCSerializer_h__
6 #define mozilla_AlertNotificationIPCSerializer_h__
8 #include "nsComponentManagerUtils.h"
9 #include "nsCOMPtr.h"
10 #include "nsIAlertsService.h"
11 #include "nsIPrincipal.h"
12 #include "nsString.h"
14 #include "ipc/IPCMessageUtils.h"
16 #include "mozilla/dom/PermissionMessageUtils.h"
18 namespace mozilla {
19 namespace ipc {
21 template <>
22 struct IPDLParamTraits<nsIAlertNotification*> {
23 static void Write(IPC::MessageWriter* aWriter, IProtocol* aActor,
24 nsIAlertNotification* aParam) {
25 bool isNull = !aParam;
26 if (isNull) {
27 WriteIPDLParam(aWriter, aActor, isNull);
28 return;
31 nsString name, imageURL, title, text, cookie, dir, lang, data;
32 bool textClickable, inPrivateBrowsing, requireInteraction, silent;
33 nsCOMPtr<nsIPrincipal> principal;
34 nsTArray<uint32_t> vibrate;
36 if (NS_WARN_IF(NS_FAILED(aParam->GetName(name))) ||
37 NS_WARN_IF(NS_FAILED(aParam->GetImageURL(imageURL))) ||
38 NS_WARN_IF(NS_FAILED(aParam->GetTitle(title))) ||
39 NS_WARN_IF(NS_FAILED(aParam->GetText(text))) ||
40 NS_WARN_IF(NS_FAILED(aParam->GetTextClickable(&textClickable))) ||
41 NS_WARN_IF(NS_FAILED(aParam->GetCookie(cookie))) ||
42 NS_WARN_IF(NS_FAILED(aParam->GetDir(dir))) ||
43 NS_WARN_IF(NS_FAILED(aParam->GetLang(lang))) ||
44 NS_WARN_IF(NS_FAILED(aParam->GetData(data))) ||
45 NS_WARN_IF(
46 NS_FAILED(aParam->GetPrincipal(getter_AddRefs(principal)))) ||
47 NS_WARN_IF(
48 NS_FAILED(aParam->GetInPrivateBrowsing(&inPrivateBrowsing))) ||
49 NS_WARN_IF(
50 NS_FAILED(aParam->GetRequireInteraction(&requireInteraction))) ||
51 NS_WARN_IF(NS_FAILED(aParam->GetSilent(&silent))) ||
52 NS_WARN_IF(NS_FAILED(aParam->GetVibrate(vibrate)))) {
53 // Write a `null` object if any getter returns an error. Otherwise, the
54 // receiver will try to deserialize an incomplete object and crash.
55 WriteIPDLParam(aWriter, aActor, /* isNull */ true);
56 return;
59 WriteIPDLParam(aWriter, aActor, isNull);
60 WriteIPDLParam(aWriter, aActor, name);
61 WriteIPDLParam(aWriter, aActor, imageURL);
62 WriteIPDLParam(aWriter, aActor, title);
63 WriteIPDLParam(aWriter, aActor, text);
64 WriteIPDLParam(aWriter, aActor, textClickable);
65 WriteIPDLParam(aWriter, aActor, cookie);
66 WriteIPDLParam(aWriter, aActor, dir);
67 WriteIPDLParam(aWriter, aActor, lang);
68 WriteIPDLParam(aWriter, aActor, data);
69 WriteIPDLParam(aWriter, aActor, principal);
70 WriteIPDLParam(aWriter, aActor, inPrivateBrowsing);
71 WriteIPDLParam(aWriter, aActor, requireInteraction);
72 WriteIPDLParam(aWriter, aActor, silent);
73 WriteIPDLParam(aWriter, aActor, vibrate);
76 static bool Read(IPC::MessageReader* aReader, IProtocol* aActor,
77 RefPtr<nsIAlertNotification>* aResult) {
78 bool isNull;
79 NS_ENSURE_TRUE(ReadIPDLParam(aReader, aActor, &isNull), false);
80 if (isNull) {
81 *aResult = nullptr;
82 return true;
85 nsString name, imageURL, title, text, cookie, dir, lang, data;
86 bool textClickable, inPrivateBrowsing, requireInteraction, silent;
87 nsCOMPtr<nsIPrincipal> principal;
88 nsTArray<uint32_t> vibrate;
90 if (!ReadIPDLParam(aReader, aActor, &name) ||
91 !ReadIPDLParam(aReader, aActor, &imageURL) ||
92 !ReadIPDLParam(aReader, aActor, &title) ||
93 !ReadIPDLParam(aReader, aActor, &text) ||
94 !ReadIPDLParam(aReader, aActor, &textClickable) ||
95 !ReadIPDLParam(aReader, aActor, &cookie) ||
96 !ReadIPDLParam(aReader, aActor, &dir) ||
97 !ReadIPDLParam(aReader, aActor, &lang) ||
98 !ReadIPDLParam(aReader, aActor, &data) ||
99 !ReadIPDLParam(aReader, aActor, &principal) ||
100 !ReadIPDLParam(aReader, aActor, &inPrivateBrowsing) ||
101 !ReadIPDLParam(aReader, aActor, &requireInteraction) ||
102 !ReadIPDLParam(aReader, aActor, &silent) ||
103 !ReadIPDLParam(aReader, aActor, &vibrate)) {
104 return false;
107 nsCOMPtr<nsIAlertNotification> alert =
108 do_CreateInstance(ALERT_NOTIFICATION_CONTRACTID);
109 if (NS_WARN_IF(!alert)) {
110 *aResult = nullptr;
111 return true;
113 nsresult rv = alert->Init(
114 name, imageURL, title, text, textClickable, cookie, dir, lang, data,
115 principal, inPrivateBrowsing, requireInteraction, silent, vibrate);
116 if (NS_WARN_IF(NS_FAILED(rv))) {
117 *aResult = nullptr;
118 return true;
120 *aResult = ToRefPtr(std::move(alert));
121 return true;
125 } // namespace ipc
126 } // namespace mozilla
128 #endif /* mozilla_AlertNotificationIPCSerializer_h__ */