tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

HybridSdpParser.cpp (2704B)


      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 #include "sdp/HybridSdpParser.h"
      7 
      8 #include "mozilla/Logging.h"
      9 #include "mozilla/Preferences.h"
     10 #include "sdp/ParsingResultComparer.h"
     11 #include "sdp/RsdparsaSdpParser.h"
     12 #include "sdp/SdpLog.h"
     13 #include "sdp/SdpPref.h"
     14 #include "sdp/SipccSdpParser.h"
     15 
     16 namespace mozilla {
     17 
     18 using mozilla::LogLevel;
     19 
     20 const std::string& HybridSdpParser::ParserName() {
     21  const static std::string PARSER_NAME = "hybrid";
     22  return PARSER_NAME;
     23 }
     24 
     25 HybridSdpParser::HybridSdpParser()
     26    : mStrictSuccess(SdpPref::StrictSuccess()),
     27      mPrimary(SdpPref::Primary()),
     28      mSecondary(SdpPref::Secondary()),
     29      mFailover(SdpPref::Failover()) {
     30  MOZ_ASSERT(!(mSecondary && mFailover),
     31             "Can not have both a secondary and failover parser!");
     32  MOZ_LOG(SdpLog, LogLevel::Info,
     33          ("Primary SDP Parser: %s", mPrimary->Name().c_str()));
     34  mSecondary.apply([](auto& parser) {
     35    MOZ_LOG(SdpLog, LogLevel::Info,
     36            ("Secondary SDP Logger: %s", parser->Name().c_str()));
     37  });
     38  mFailover.apply([](auto& parser) {
     39    MOZ_LOG(SdpLog, LogLevel::Info,
     40            ("Failover SDP Logger: %s", parser->Name().c_str()));
     41  });
     42 }
     43 
     44 auto HybridSdpParser::Parse(const std::string& aText)
     45    -> UniquePtr<SdpParser::Results> {
     46  using Results = UniquePtr<SdpParser::Results>;
     47  using Mode = SdpPref::AlternateParseModes;
     48 
     49  Mode mode = Mode::Never;
     50  auto results = mPrimary->Parse(aText);
     51 
     52  auto successful = [&](Results& aRes) -> bool {
     53    // In strict mode any reported error counts as failure
     54    if (mStrictSuccess) {
     55      return aRes->Ok();
     56    }
     57    return aRes->Sdp() != nullptr;
     58  };
     59  // Pass results on for comparison and return A if it was a success and B
     60  // otherwise.
     61  auto compare = [&](Results&& aResB) -> Results {
     62    ParsingResultComparer::Compare(results, aResB, aText, mode);
     63    return std::move(successful(results) ? results : aResB);
     64  };
     65  // Run secondary parser, if there is one, and update selected results.
     66  mSecondary.apply([&](auto& sec) {
     67    mode = Mode::Parallel;
     68    results = compare(std::move(sec->Parse(aText)));
     69  });
     70  // Run failover parser, if there is one, and update selected results.
     71  mFailover.apply([&](auto& failover) {  // Only run if primary parser failed
     72    mode = Mode::Failover;
     73    if (!successful(results)) {
     74      results = compare(std::move(failover->Parse(aText)));
     75    }
     76  });
     77 
     78  return results;
     79 }
     80 
     81 }  // namespace mozilla