IPCMessageUtilsSpecializations.cpp (2017B)
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 "IPCMessageUtilsSpecializations.h" 8 #include "nsGkAtoms.h" 9 10 namespace IPC { 11 12 static const uint16_t kDynamicAtomToken = 0xffff; 13 static const uint16_t kAtomsCount = 14 static_cast<uint16_t>(mozilla::detail::GkAtoms::Atoms::AtomsCount); 15 16 static_assert(static_cast<size_t>( 17 mozilla::detail::GkAtoms::Atoms::AtomsCount) == kAtomsCount, 18 "Number of static atoms must fit in a uint16_t"); 19 20 static_assert(kDynamicAtomToken >= kAtomsCount, 21 "Exceeded supported number of static atoms"); 22 23 /* static */ 24 void ParamTraits<nsAtom*>::Write(MessageWriter* aWriter, const nsAtom* aParam) { 25 MOZ_ASSERT(aParam); 26 27 if (aParam->IsStatic()) { 28 const nsStaticAtom* atom = aParam->AsStatic(); 29 uint16_t index = static_cast<uint16_t>(nsGkAtoms::IndexOf(atom)); 30 MOZ_ASSERT(index < kAtomsCount); 31 WriteParam(aWriter, index); 32 return; 33 } 34 WriteParam(aWriter, kDynamicAtomToken); 35 nsDependentAtomString atomStr(aParam); 36 // nsDependentAtomString is serialized as its base, nsString, but we 37 // can be explicit about it. 38 nsString& str = atomStr; 39 WriteParam(aWriter, str); 40 } 41 42 /* static */ 43 bool ParamTraits<nsAtom*>::Read(MessageReader* aReader, 44 RefPtr<nsAtom>* aResult) { 45 uint16_t token; 46 if (!ReadParam(aReader, &token)) { 47 return false; 48 } 49 if (token != kDynamicAtomToken) { 50 if (token >= kAtomsCount) { 51 return false; 52 } 53 *aResult = nsGkAtoms::GetAtomByIndex(token); 54 return true; 55 } 56 57 nsAutoString str; 58 if (!ReadParam(aReader, static_cast<nsString*>(&str))) { 59 return false; 60 } 61 62 *aResult = NS_Atomize(str); 63 MOZ_ASSERT(*aResult); 64 return true; 65 } 66 67 } // namespace IPC