tor-browser

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

MediaStreamError.cpp (3924B)


      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 #include "MediaStreamError.h"
      8 
      9 #include "mozilla/dom/MediaStreamErrorBinding.h"
     10 #include "mozilla/dom/Promise.h"
     11 #include "nsContentUtils.h"
     12 
     13 namespace mozilla {
     14 
     15 BaseMediaMgrError::BaseMediaMgrError(Name aName, const nsACString& aMessage,
     16                                     const nsAString& aConstraint)
     17    : mMessage(aMessage), mConstraint(aConstraint), mName(aName) {
     18 #define MAP_MEDIAERR(name, msg) {Name::name, #name, msg}
     19 
     20  static struct {
     21    Name mName;
     22    const char* mNameString;
     23    const char* mMessage;
     24  } map[] = {
     25      MAP_MEDIAERR(AbortError, "The operation was aborted."),
     26      MAP_MEDIAERR(InvalidStateError, "The object is in an invalid state."),
     27      MAP_MEDIAERR(NotAllowedError,
     28                   "The request is not allowed by the user agent "
     29                   "or the platform in the current context."),
     30      MAP_MEDIAERR(NotFoundError, "The object can not be found here."),
     31      MAP_MEDIAERR(NotReadableError, "The I/O read operation failed."),
     32      MAP_MEDIAERR(OverconstrainedError, "Constraints could not be satisfied."),
     33      MAP_MEDIAERR(SecurityError, "The operation is insecure."),
     34      MAP_MEDIAERR(TypeError, ""),
     35  };
     36  for (auto& entry : map) {
     37    if (entry.mName == mName) {
     38      mNameString.AssignASCII(entry.mNameString);
     39      if (mMessage.IsEmpty()) {
     40        mMessage.AssignASCII(entry.mMessage);
     41      }
     42      return;
     43    }
     44  }
     45  MOZ_ASSERT_UNREACHABLE("Unknown error type");
     46 }
     47 
     48 NS_IMPL_ISUPPORTS0(MediaMgrError)
     49 
     50 void MediaMgrError::Reject(dom::Promise* aPromise) const {
     51  switch (mName) {
     52    case Name::AbortError:
     53      aPromise->MaybeRejectWithAbortError(mMessage);
     54      return;
     55    case Name::InvalidStateError:
     56      aPromise->MaybeRejectWithInvalidStateError(mMessage);
     57      return;
     58    case Name::NotAllowedError:
     59      aPromise->MaybeRejectWithNotAllowedError(mMessage);
     60      return;
     61    case Name::NotFoundError:
     62      aPromise->MaybeRejectWithNotFoundError(mMessage);
     63      return;
     64    case Name::NotReadableError:
     65      aPromise->MaybeRejectWithNotReadableError(mMessage);
     66      return;
     67    case Name::OverconstrainedError: {
     68      // TODO: Add OverconstrainedError type.
     69      // https://bugzilla.mozilla.org/show_bug.cgi?id=1453013
     70      nsCOMPtr<nsPIDOMWindowInner> window =
     71          do_QueryInterface(aPromise->GetGlobalObject());
     72      aPromise->MaybeReject(MakeRefPtr<dom::MediaStreamError>(window, *this));
     73      return;
     74    }
     75    case Name::SecurityError:
     76      aPromise->MaybeRejectWithSecurityError(mMessage);
     77      return;
     78    case Name::TypeError:
     79      aPromise->MaybeRejectWithTypeError(mMessage);
     80      return;
     81      // -Wswitch ensures all cases are covered so don't add default:.
     82  }
     83 }
     84 
     85 namespace dom {
     86 
     87 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MediaStreamError, mParent)
     88 NS_IMPL_CYCLE_COLLECTING_ADDREF(MediaStreamError)
     89 NS_IMPL_CYCLE_COLLECTING_RELEASE(MediaStreamError)
     90 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaStreamError)
     91  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
     92  NS_INTERFACE_MAP_ENTRY(nsISupports)
     93  NS_INTERFACE_MAP_ENTRY(MediaStreamError)
     94 NS_INTERFACE_MAP_END
     95 
     96 JSObject* MediaStreamError::WrapObject(JSContext* aCx,
     97                                       JS::Handle<JSObject*> aGivenProto) {
     98  return MediaStreamError_Binding::Wrap(aCx, this, aGivenProto);
     99 }
    100 
    101 void MediaStreamError::GetName(nsAString& aName) const { aName = mNameString; }
    102 
    103 void MediaStreamError::GetMessage(nsAString& aMessage) const {
    104  CopyUTF8toUTF16(mMessage, aMessage);
    105 }
    106 
    107 void MediaStreamError::GetConstraint(nsAString& aConstraint) const {
    108  aConstraint = mConstraint;
    109 }
    110 
    111 }  // namespace dom
    112 }  // namespace mozilla