Line data Source code
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/. */
4 :
5 : #ifndef mozilla_AlertNotificationIPCSerializer_h__
6 : #define mozilla_AlertNotificationIPCSerializer_h__
7 :
8 : #include "nsComponentManagerUtils.h"
9 : #include "nsCOMPtr.h"
10 : #include "nsIAlertsService.h"
11 : #include "nsIPrincipal.h"
12 : #include "nsString.h"
13 :
14 : #include "ipc/IPCMessageUtils.h"
15 :
16 : #include "mozilla/dom/PermissionMessageUtils.h"
17 :
18 : typedef nsIAlertNotification* AlertNotificationType;
19 :
20 : namespace IPC {
21 :
22 : template <>
23 : struct ParamTraits<AlertNotificationType>
24 : {
25 : typedef AlertNotificationType paramType;
26 :
27 0 : static void Write(Message* aMsg, const paramType& aParam)
28 : {
29 0 : bool isNull = !aParam;
30 0 : if (isNull) {
31 0 : WriteParam(aMsg, isNull);
32 0 : return;
33 : }
34 :
35 0 : nsString name, imageURL, title, text, cookie, dir, lang, data;
36 : bool textClickable, inPrivateBrowsing, requireInteraction;
37 0 : nsCOMPtr<nsIPrincipal> principal;
38 :
39 0 : if (NS_WARN_IF(NS_FAILED(aParam->GetName(name))) ||
40 0 : NS_WARN_IF(NS_FAILED(aParam->GetImageURL(imageURL))) ||
41 0 : NS_WARN_IF(NS_FAILED(aParam->GetTitle(title))) ||
42 0 : NS_WARN_IF(NS_FAILED(aParam->GetText(text))) ||
43 0 : NS_WARN_IF(NS_FAILED(aParam->GetTextClickable(&textClickable))) ||
44 0 : NS_WARN_IF(NS_FAILED(aParam->GetCookie(cookie))) ||
45 0 : NS_WARN_IF(NS_FAILED(aParam->GetDir(dir))) ||
46 0 : NS_WARN_IF(NS_FAILED(aParam->GetLang(lang))) ||
47 0 : NS_WARN_IF(NS_FAILED(aParam->GetData(data))) ||
48 0 : NS_WARN_IF(NS_FAILED(aParam->GetPrincipal(getter_AddRefs(principal)))) ||
49 0 : NS_WARN_IF(NS_FAILED(aParam->GetInPrivateBrowsing(&inPrivateBrowsing))) ||
50 0 : NS_WARN_IF(NS_FAILED(aParam->GetRequireInteraction(&requireInteraction)))) {
51 :
52 : // Write a `null` object if any getter returns an error. Otherwise, the
53 : // receiver will try to deserialize an incomplete object and crash.
54 0 : WriteParam(aMsg, /* isNull */ true);
55 0 : return;
56 : }
57 :
58 0 : WriteParam(aMsg, isNull);
59 0 : WriteParam(aMsg, name);
60 0 : WriteParam(aMsg, imageURL);
61 0 : WriteParam(aMsg, title);
62 0 : WriteParam(aMsg, text);
63 0 : WriteParam(aMsg, textClickable);
64 0 : WriteParam(aMsg, cookie);
65 0 : WriteParam(aMsg, dir);
66 0 : WriteParam(aMsg, lang);
67 0 : WriteParam(aMsg, data);
68 0 : WriteParam(aMsg, IPC::Principal(principal));
69 0 : WriteParam(aMsg, inPrivateBrowsing);
70 0 : WriteParam(aMsg, requireInteraction);
71 : }
72 :
73 0 : static bool Read(const Message* aMsg, PickleIterator* aIter, paramType* aResult)
74 : {
75 : bool isNull;
76 0 : NS_ENSURE_TRUE(ReadParam(aMsg, aIter, &isNull), false);
77 0 : if (isNull) {
78 0 : *aResult = nullptr;
79 0 : return true;
80 : }
81 :
82 0 : nsString name, imageURL, title, text, cookie, dir, lang, data;
83 : bool textClickable, inPrivateBrowsing, requireInteraction;
84 0 : IPC::Principal principal;
85 :
86 0 : if (!ReadParam(aMsg, aIter, &name) ||
87 0 : !ReadParam(aMsg, aIter, &imageURL) ||
88 0 : !ReadParam(aMsg, aIter, &title) ||
89 0 : !ReadParam(aMsg, aIter, &text) ||
90 0 : !ReadParam(aMsg, aIter, &textClickable) ||
91 0 : !ReadParam(aMsg, aIter, &cookie) ||
92 0 : !ReadParam(aMsg, aIter, &dir) ||
93 0 : !ReadParam(aMsg, aIter, &lang) ||
94 0 : !ReadParam(aMsg, aIter, &data) ||
95 0 : !ReadParam(aMsg, aIter, &principal) ||
96 0 : !ReadParam(aMsg, aIter, &inPrivateBrowsing) ||
97 0 : !ReadParam(aMsg, aIter, &requireInteraction)) {
98 :
99 0 : return false;
100 : }
101 :
102 : nsCOMPtr<nsIAlertNotification> alert =
103 0 : do_CreateInstance(ALERT_NOTIFICATION_CONTRACTID);
104 0 : if (NS_WARN_IF(!alert)) {
105 0 : *aResult = nullptr;
106 0 : return true;
107 : }
108 0 : nsresult rv = alert->Init(name, imageURL, title, text, textClickable,
109 : cookie, dir, lang, data, principal,
110 0 : inPrivateBrowsing, requireInteraction);
111 0 : if (NS_WARN_IF(NS_FAILED(rv))) {
112 0 : *aResult = nullptr;
113 0 : return true;
114 : }
115 0 : alert.forget(aResult);
116 0 : return true;
117 : }
118 : };
119 :
120 : } // namespace IPC
121 :
122 : #endif /* mozilla_AlertNotificationIPCSerializer_h__ */
|