SdpPref.h (2530B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=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 file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef _SDPPREF_H_ 8 #define _SDPPREF_H_ 9 10 #include <string> 11 #include <unordered_map> 12 13 #include "mozilla/Maybe.h" 14 #include "mozilla/Preferences.h" 15 16 namespace mozilla { 17 18 class SdpParser; 19 20 // Interprets about:config SDP parsing preferences 21 class SdpPref { 22 private: 23 static constexpr const char PRIMARY_PREF[] = 24 "media.peerconnection.sdp.parser"; 25 static constexpr const char ALTERNATE_PREF[] = 26 "media.peerconnection.sdp.alternate_parse_mode"; 27 static constexpr const char STRICT_SUCCESS_PREF[] = 28 "media.peerconnection.sdp.strict_success"; 29 static constexpr const char DEFAULT[] = "default"; 30 31 public: 32 // Supported Parsers 33 enum class Parsers { 34 Sipcc, 35 WebRtcSdp, 36 }; 37 38 // How is the alternate used 39 enum class AlternateParseModes { 40 Parallel, // Alternate is always run, if A succedes it is used, otherwise B 41 // is used 42 Failover, // Alternate is only run on failure of the primary to parse 43 Never, // Alternate is never run; this is effectively a kill switch 44 }; 45 46 private: 47 // Finds the mapping between a pref string and pref value, if none exists the 48 // default is used 49 template <class T> 50 static auto Pref(const std::string& aPrefName, 51 const std::unordered_map<std::string, T>& aMap) -> T { 52 MOZ_ASSERT(aMap.find(DEFAULT) != aMap.end()); 53 54 nsCString value; 55 if (NS_FAILED(Preferences::GetCString(aPrefName.c_str(), value))) { 56 return aMap.at(DEFAULT); 57 } 58 const auto found = aMap.find(value.get()); 59 if (found != aMap.end()) { 60 return found->second; 61 } 62 return aMap.at(DEFAULT); 63 } 64 // The value of the parser pref 65 static auto Parser() -> Parsers; 66 67 // The value of the alternate parse mode pref 68 static auto AlternateParseMode() -> AlternateParseModes; 69 70 public: 71 // Do non-fatal parsing errors count as failure 72 static auto StrictSuccess() -> bool; 73 // Functions to create the primary, secondary and failover parsers. 74 75 // Reads about:config to choose the primary Parser 76 static auto Primary() -> UniquePtr<SdpParser>; 77 static auto Secondary() -> Maybe<UniquePtr<SdpParser>>; 78 static auto Failover() -> Maybe<UniquePtr<SdpParser>>; 79 }; 80 81 } // namespace mozilla 82 83 #endif