JSActorProtocolUtils.h (3048B)
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 #ifndef mozilla_dom_JSActorProtocolUtils_h 8 #define mozilla_dom_JSActorProtocolUtils_h 9 10 #include "mozilla/Assertions.h" // MOZ_ASSERT 11 #include "mozilla/ErrorResult.h" // ErrorResult 12 13 namespace mozilla { 14 15 namespace dom { 16 17 class JSActorProtocolUtils { 18 public: 19 template <typename ProtoT, typename ActorInfoT> 20 static void FromIPCShared(ProtoT& aProto, const ActorInfoT& aInfo) { 21 aProto->mRemoteTypes = aInfo.remoteTypes().Clone(); 22 23 aProto->mChild.mESModuleURI = aInfo.url(); 24 25 aProto->mLoadInDevToolsLoader = aInfo.loadInDevToolsLoader(); 26 27 aProto->mChild.mObservers = aInfo.observers().Clone(); 28 } 29 30 template <typename ProtoT, typename ActorInfoT> 31 static void ToIPCShared(ActorInfoT& aInfo, const ProtoT& aProto) { 32 aInfo.name() = aProto->mName; 33 34 aInfo.remoteTypes() = aProto->mRemoteTypes.Clone(); 35 36 aInfo.url() = aProto->mChild.mESModuleURI; 37 38 aInfo.loadInDevToolsLoader() = aProto->mLoadInDevToolsLoader; 39 40 aInfo.observers() = aProto->mChild.mObservers.Clone(); 41 } 42 43 template <typename ProtoT, typename ActorOptionsT> 44 static bool FromWebIDLOptionsShared(ProtoT& aProto, 45 const ActorOptionsT& aOptions, 46 ErrorResult& aRv) { 47 if (aOptions.mRemoteTypes.WasPassed()) { 48 MOZ_ASSERT(aOptions.mRemoteTypes.Value().Length()); 49 aProto->mRemoteTypes = aOptions.mRemoteTypes.Value(); 50 } 51 52 if (aOptions.mParent.WasPassed()) { 53 const auto& parentOptions = aOptions.mParent.Value(); 54 55 if (parentOptions.mEsModuleURI.WasPassed()) { 56 aProto->mParent.mESModuleURI.emplace( 57 parentOptions.mEsModuleURI.Value()); 58 } else { 59 aRv.ThrowNotSupportedError( 60 "Either moduleURI or esModuleURI is required."); 61 return false; 62 } 63 } 64 if (aOptions.mChild.WasPassed()) { 65 const auto& childOptions = aOptions.mChild.Value(); 66 67 if (childOptions.mEsModuleURI.WasPassed()) { 68 aProto->mChild.mESModuleURI.emplace(childOptions.mEsModuleURI.Value()); 69 } else { 70 aRv.ThrowNotSupportedError( 71 "Either moduleURI or esModuleURI is required."); 72 return false; 73 } 74 } 75 76 if (!aOptions.mChild.WasPassed() && !aOptions.mParent.WasPassed()) { 77 aRv.ThrowNotSupportedError( 78 "No point registering an actor with neither child nor parent " 79 "specifications."); 80 return false; 81 } 82 83 if (aOptions.mChild.WasPassed() && 84 aOptions.mChild.Value().mObservers.WasPassed()) { 85 aProto->mChild.mObservers = aOptions.mChild.Value().mObservers.Value(); 86 } 87 88 return true; 89 } 90 }; 91 92 } // namespace dom 93 } // namespace mozilla 94 95 #endif // mozilla_dom_JSActorProtocolUtils_h