ReferrerInfoUtils.cpp (1645B)
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/. */ 6 7 #include "mozilla/dom/ReferrerInfoUtils.h" 8 9 #include "ipc/IPCMessageUtilsSpecializations.h" 10 #include "nsSerializationHelper.h" 11 #include "nsString.h" 12 13 namespace IPC { 14 15 void ParamTraits<nsIReferrerInfo*>::Write(MessageWriter* aWriter, 16 nsIReferrerInfo* aParam) { 17 bool isNull = !aParam; 18 WriteParam(aWriter, isNull); 19 if (isNull) { 20 return; 21 } 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; 27 } 28 WriteParam(aWriter, infoString); 29 } 30 31 bool ParamTraits<nsIReferrerInfo*>::Read(MessageReader* aReader, 32 RefPtr<nsIReferrerInfo>* aResult) { 33 bool isNull; 34 if (!ReadParam(aReader, &isNull)) { 35 return false; 36 } 37 if (isNull) { 38 *aResult = nullptr; 39 return true; 40 } 41 nsAutoCString infoString; 42 if (!ReadParam(aReader, &infoString)) { 43 return false; 44 } 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; 52 } 53 54 } // namespace IPC