Пример #1
0
already_AddRefed<DOMMediaStream>
TVTuner::CreateSimulatedMediaStream()
{
  nsCOMPtr<nsPIDOMWindowInner> domWin = GetOwner();
  if (NS_WARN_IF(!domWin)) {
    return nullptr;
  }

  nsIDocument* doc = domWin->GetExtantDoc();
  if (NS_WARN_IF(!doc)) {
    return nullptr;
  }

  ErrorResult error;
  ElementCreationOptionsOrString options;

  options.SetAsString();
  RefPtr<Element> element = doc->CreateElement(VIDEO_TAG, options, error);
  if (NS_WARN_IF(error.Failed())) {
    error.SuppressException();
    return nullptr;
  }

  nsCOMPtr<nsIContent> content(do_QueryInterface(element));
  if (NS_WARN_IF(!content)) {
    return nullptr;
  }

  HTMLMediaElement* mediaElement = static_cast<HTMLMediaElement*>(content.get());
  if (NS_WARN_IF(!mediaElement)) {
    return nullptr;
  }

  mediaElement->SetAutoplay(true, error);
  if (NS_WARN_IF(error.Failed())) {
    error.SuppressException();
    return nullptr;
  }

  mediaElement->SetLoop(true, error);
  if (NS_WARN_IF(error.Failed())) {
    error.SuppressException();
    return nullptr;
  }

  nsCOMPtr<nsITVSimulatorService> simService(do_QueryInterface(mTVService));
  if (NS_WARN_IF(!simService)) {
    return nullptr;
  }

  if (NS_WARN_IF(!mCurrentSource)) {
    return nullptr;
  }

  RefPtr<TVChannel> currentChannel = mCurrentSource->GetCurrentChannel();
  if (NS_WARN_IF(!currentChannel)) {
    return nullptr;
  }

  nsString currentChannelNumber;
  currentChannel->GetNumber(currentChannelNumber);
  if (currentChannelNumber.IsEmpty()) {
    return nullptr;
  }

  nsString currentVideoBlobUrl;
  nsresult rv = simService->GetSimulatorVideoBlobURL(mId,
                                                     ToTVSourceTypeStr(mCurrentSource->Type()),
                                                     currentChannelNumber,
                                                     domWin,
                                                     currentVideoBlobUrl);
  if (NS_WARN_IF(NS_FAILED(rv) || currentVideoBlobUrl.IsEmpty())) {
    return nullptr;
  }

  mediaElement->SetSrc(currentVideoBlobUrl, error);
  if (NS_WARN_IF(error.Failed())) {
    error.SuppressException();
    return nullptr;
  }

  // See Media Capture from DOM Elements spec.
  // http://www.w3.org/TR/mediacapture-fromelement/
  RefPtr<DOMMediaStream> stream = mediaElement->MozCaptureStream(error);
  if (NS_WARN_IF(error.Failed())) {
    error.SuppressException();
    return nullptr;
  }

  return stream.forget();
}
Пример #2
0
nsresult
NS_NewDOMDocument(nsIDOMDocument** aInstancePtrResult,
                  const nsAString& aNamespaceURI,
                  const nsAString& aQualifiedName,
                  nsIDOMDocumentType* aDoctype,
                  nsIURI* aDocumentURI,
                  nsIURI* aBaseURI,
                  nsIPrincipal* aPrincipal,
                  bool aLoadedAsData,
                  nsIGlobalObject* aEventObject,
                  DocumentFlavor aFlavor,
                  StyleBackendType aStyleBackend)
{
  // Note: can't require that aDocumentURI/aBaseURI/aPrincipal be non-null,
  // since at least one caller (XMLHttpRequest) doesn't have decent args to
  // pass in.

  nsresult rv;

  *aInstancePtrResult = nullptr;

  nsCOMPtr<nsIDocument> d;
  bool isHTML = false;
  bool isXHTML = false;
  if (aFlavor == DocumentFlavorSVG) {
    rv = NS_NewSVGDocument(getter_AddRefs(d));
  } else if (aFlavor == DocumentFlavorHTML) {
    rv = NS_NewHTMLDocument(getter_AddRefs(d));
    isHTML = true;
  } else if (aFlavor == DocumentFlavorPlain) {
    rv = NS_NewXMLDocument(getter_AddRefs(d), aLoadedAsData, true);
  } else if (aDoctype) {
    MOZ_ASSERT(aFlavor == DocumentFlavorLegacyGuess);
    nsAutoString publicId, name;
    aDoctype->GetPublicId(publicId);
    if (publicId.IsEmpty()) {
      aDoctype->GetName(name);
    }
    if (name.EqualsLiteral("html") ||
        publicId.EqualsLiteral("-//W3C//DTD HTML 4.01//EN") ||
        publicId.EqualsLiteral("-//W3C//DTD HTML 4.01 Frameset//EN") ||
        publicId.EqualsLiteral("-//W3C//DTD HTML 4.01 Transitional//EN") ||
        publicId.EqualsLiteral("-//W3C//DTD HTML 4.0//EN") ||
        publicId.EqualsLiteral("-//W3C//DTD HTML 4.0 Frameset//EN") ||
        publicId.EqualsLiteral("-//W3C//DTD HTML 4.0 Transitional//EN")) {
      rv = NS_NewHTMLDocument(getter_AddRefs(d));
      isHTML = true;
    } else if (publicId.EqualsLiteral("-//W3C//DTD XHTML 1.0 Strict//EN") ||
               publicId.EqualsLiteral("-//W3C//DTD XHTML 1.0 Transitional//EN") ||
               publicId.EqualsLiteral("-//W3C//DTD XHTML 1.0 Frameset//EN")) {
      rv = NS_NewHTMLDocument(getter_AddRefs(d));
      isHTML = true;
      isXHTML = true;
    }
    else if (publicId.EqualsLiteral("-//W3C//DTD SVG 1.1//EN")) {
      rv = NS_NewSVGDocument(getter_AddRefs(d));
    }
    // XXX Add support for XUL documents.
    else {
      rv = NS_NewXMLDocument(getter_AddRefs(d));
    }
  } else {
    MOZ_ASSERT(aFlavor == DocumentFlavorLegacyGuess);
    rv = NS_NewXMLDocument(getter_AddRefs(d));
  }

  if (NS_FAILED(rv)) {
    return rv;
  }

  // If we were passed an explicit style backend for this document set it
  // immediately after creation, before any content is inserted.
  if (aStyleBackend != StyleBackendType::None) {
    d->SetStyleBackendType(aStyleBackend);
  }

  if (isHTML) {
    nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(d);
    NS_ASSERTION(htmlDoc, "HTML Document doesn't implement nsIHTMLDocument?");
    htmlDoc->SetCompatibilityMode(eCompatibility_FullStandards);
    htmlDoc->SetIsXHTML(isXHTML);
  }
  nsDocument* doc = static_cast<nsDocument*>(d.get());
  doc->SetLoadedAsData(aLoadedAsData);
  doc->nsDocument::SetDocumentURI(aDocumentURI);
  // Must set the principal first, since SetBaseURI checks it.
  doc->SetPrincipal(aPrincipal);
  doc->SetBaseURI(aBaseURI);

  // We need to set the script handling object after we set the principal such
  // that the doc group is assigned correctly.
  if (nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aEventObject)) {
    d->SetScriptHandlingObject(sgo);
  } else if (aEventObject){
    d->SetScopeObject(aEventObject);
  }

  // XMLDocuments and documents "created in memory" get to be UTF-8 by default,
  // unlike the legacy HTML mess
  doc->SetDocumentCharacterSet(UTF_8_ENCODING);

  if (aDoctype) {
    nsCOMPtr<nsINode> doctypeAsNode = do_QueryInterface(aDoctype);
    ErrorResult result;
    d->AppendChild(*doctypeAsNode, result);
    if (NS_WARN_IF(result.Failed())) {
      return result.StealNSResult();
    }
  }

  if (!aQualifiedName.IsEmpty()) {
    ErrorResult result;
    ElementCreationOptionsOrString options;
    options.SetAsString();

    nsCOMPtr<Element> root =
      doc->CreateElementNS(aNamespaceURI, aQualifiedName, options, result);
    if (NS_WARN_IF(result.Failed())) {
      return result.StealNSResult();
    }

    d->AppendChild(*root, result);
    if (NS_WARN_IF(result.Failed())) {
      return result.StealNSResult();
    }
  }

  *aInstancePtrResult = doc;
  NS_ADDREF(*aInstancePtrResult);

  return NS_OK;
}