tor-browser

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

MediaResult.h (3933B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
      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 MediaResult_h_
      8 #define MediaResult_h_
      9 
     10 #include "mozilla/ErrorNames.h"
     11 #include "mozilla/IntegerPrintfMacros.h"
     12 #include "mozilla/Logging.h"
     13 #include "nsError.h"
     14 #include "nsPrintfCString.h"
     15 #include "nsString.h"  // Required before 'mozilla/ErrorNames.h'!?
     16 
     17 // MediaResult can be used interchangeably with nsresult.
     18 // It allows to store extra information such as where the error occurred.
     19 // While nsresult is typically passed by value; due to its potential size, using
     20 // MediaResult const references is recommended.
     21 namespace mozilla {
     22 
     23 namespace dom {
     24 class Promise;
     25 }
     26 
     27 class CDMProxy;
     28 class ErrorResult;
     29 
     30 class MediaResult {
     31 public:
     32  MediaResult() : mCode(NS_OK) {}
     33  MOZ_IMPLICIT MediaResult(nsresult aResult) : mCode(aResult) {}
     34  MediaResult(nsresult aResult, const nsACString& aMessage,
     35              Maybe<int32_t> aPlatformErrorCode = Nothing())
     36      : mCode(aResult),
     37        mMessage(aMessage),
     38        mPlatformErrorCode(aPlatformErrorCode) {}
     39  MediaResult(nsresult aResult, const char* aMessage,
     40              Maybe<int32_t> aPlatformErrorCode = Nothing())
     41      : mCode(aResult),
     42        mMessage(aMessage),
     43        mPlatformErrorCode(aPlatformErrorCode) {}
     44  MediaResult(nsresult aResult, CDMProxy* aCDMProxy)
     45      : mCode(aResult), mCDMProxy(aCDMProxy) {
     46    MOZ_ASSERT(aResult == NS_ERROR_DOM_MEDIA_CDM_PROXY_NOT_SUPPORTED_ERR);
     47  }
     48  static MediaResult Logged(nsresult aResult, const char* aMessage,
     49                            const LogModule* aLogModule) {
     50    MOZ_LOG(aLogModule, LogLevel::Warning, ("%s", aMessage));
     51    return MediaResult(aResult, aMessage);
     52  }
     53  static MediaResult Logged(nsresult aResult, const nsCString& aMessage,
     54                            const LogModule* aLogModule) {
     55    MOZ_LOG(aLogModule, LogLevel::Warning, ("%s", aMessage.get()));
     56    return MediaResult(aResult, aMessage);
     57  }
     58  MediaResult(const MediaResult& aOther) = default;
     59  MediaResult(MediaResult&& aOther) = default;
     60  MediaResult& operator=(const MediaResult& aOther) = default;
     61  MediaResult& operator=(MediaResult&& aOther) = default;
     62 
     63  nsresult Code() const { return mCode; }
     64  nsCString ErrorName() const {
     65    nsCString name;
     66    GetErrorName(mCode, name);
     67    return name;
     68  }
     69 
     70  const nsCString& Message() const { return mMessage; }
     71 
     72  // Interoperations with nsresult.
     73  bool operator==(nsresult aResult) const { return aResult == mCode; }
     74  bool operator!=(nsresult aResult) const { return aResult != mCode; }
     75  operator nsresult() const { return mCode; }
     76 
     77  nsCString Description() const {
     78    if (NS_SUCCEEDED(mCode)) {
     79      return nsCString();
     80    }
     81    return nsPrintfCString("%s (0x%08" PRIx32 ")%s%s", ErrorName().get(),
     82                           static_cast<uint32_t>(mCode),
     83                           mMessage.IsEmpty() ? "" : " - ", mMessage.get());
     84  }
     85 
     86  CDMProxy* GetCDMProxy() const { return mCDMProxy; }
     87  Maybe<int32_t> GetPlatformErrorCode() const { return mPlatformErrorCode; }
     88 
     89  void ThrowTo(ErrorResult& aRv) const;
     90  void RejectTo(dom::Promise* aPromise) const;
     91 
     92 private:
     93  nsresult mCode;
     94  nsCString mMessage;
     95  // A platform error code, which is useful for debug. Eg. HResult on Windows.
     96  Maybe<int32_t> mPlatformErrorCode = Nothing();
     97  // It's used when the error is NS_ERROR_DOM_MEDIA_CDM_PROXY_NOT_SUPPORTED_ERR.
     98  CDMProxy* mCDMProxy = nullptr;
     99 };
    100 
    101 #ifdef _MSC_VER
    102 #  define RESULT_DETAIL(arg, ...) \
    103    nsPrintfCString("%s: " arg, __FUNCSIG__, ##__VA_ARGS__)
    104 #else
    105 #  define RESULT_DETAIL(arg, ...) \
    106    nsPrintfCString("%s: " arg, __PRETTY_FUNCTION__, ##__VA_ARGS__)
    107 #endif
    108 
    109 }  // namespace mozilla
    110 #endif  // MediaResult_h_