Bug 1861467 - [wpt-sync] Update web-platform-tests to eedf737ce39c512d0ca3471f988972e...
[gecko.git] / dom / ipc / ReferrerInfoUtils.cpp
bloba2728e342c032529835486f8d37f48866f310698
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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/ReferrerInfoUtils.h"
9 #include "ipc/IPCMessageUtilsSpecializations.h"
10 #include "nsSerializationHelper.h"
11 #include "nsString.h"
13 namespace IPC {
15 void ParamTraits<nsIReferrerInfo*>::Write(MessageWriter* aWriter,
16 nsIReferrerInfo* aParam) {
17 bool isNull = !aParam;
18 WriteParam(aWriter, isNull);
19 if (isNull) {
20 return;
22 nsAutoCString infoString;
23 nsresult rv = NS_SerializeToString(aParam, infoString);
24 if (NS_FAILED(rv)) {
25 MOZ_CRASH("Unable to serialize referrer info.");
26 return;
28 WriteParam(aWriter, infoString);
31 bool ParamTraits<nsIReferrerInfo*>::Read(MessageReader* aReader,
32 RefPtr<nsIReferrerInfo>* aResult) {
33 bool isNull;
34 if (!ReadParam(aReader, &isNull)) {
35 return false;
37 if (isNull) {
38 *aResult = nullptr;
39 return true;
41 nsAutoCString infoString;
42 if (!ReadParam(aReader, &infoString)) {
43 return false;
45 nsCOMPtr<nsISupports> iSupports;
46 nsresult rv = NS_DeserializeObject(infoString, getter_AddRefs(iSupports));
47 NS_ENSURE_SUCCESS(rv, false);
48 nsCOMPtr<nsIReferrerInfo> referrerInfo = do_QueryInterface(iSupports);
49 NS_ENSURE_TRUE(referrerInfo, false);
50 *aResult = ToRefPtr(std::move(referrerInfo));
51 return true;
54 } // namespace IPC