content/html/document/src/VideoDocument.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/html/document/src/VideoDocument.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,153 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "MediaDocument.h"
    1.10 +#include "nsGkAtoms.h"
    1.11 +#include "nsNodeInfoManager.h"
    1.12 +#include "nsContentCreatorFunctions.h"
    1.13 +#include "mozilla/dom/HTMLMediaElement.h"
    1.14 +#include "nsIDocumentInlines.h"
    1.15 +#include "nsContentUtils.h"
    1.16 +#include "mozilla/dom/Element.h"
    1.17 +
    1.18 +namespace mozilla {
    1.19 +namespace dom {
    1.20 +
    1.21 +class VideoDocument MOZ_FINAL : public MediaDocument
    1.22 +{
    1.23 +public:
    1.24 +  virtual nsresult StartDocumentLoad(const char*         aCommand,
    1.25 +                                     nsIChannel*         aChannel,
    1.26 +                                     nsILoadGroup*       aLoadGroup,
    1.27 +                                     nsISupports*        aContainer,
    1.28 +                                     nsIStreamListener** aDocListener,
    1.29 +                                     bool                aReset = true,
    1.30 +                                     nsIContentSink*     aSink = nullptr);
    1.31 +  virtual void SetScriptGlobalObject(nsIScriptGlobalObject* aScriptGlobalObject);
    1.32 +
    1.33 +protected:
    1.34 +
    1.35 +  // Sets document <title> to reflect the file name and description.
    1.36 +  void UpdateTitle(nsIChannel* aChannel);
    1.37 +
    1.38 +  nsresult CreateSyntheticVideoDocument(nsIChannel* aChannel,
    1.39 +                                        nsIStreamListener** aListener);
    1.40 +
    1.41 +  nsRefPtr<MediaDocumentStreamListener> mStreamListener;
    1.42 +};
    1.43 +
    1.44 +nsresult
    1.45 +VideoDocument::StartDocumentLoad(const char*         aCommand,
    1.46 +                                 nsIChannel*         aChannel,
    1.47 +                                 nsILoadGroup*       aLoadGroup,
    1.48 +                                 nsISupports*        aContainer,
    1.49 +                                 nsIStreamListener** aDocListener,
    1.50 +                                 bool                aReset,
    1.51 +                                 nsIContentSink*     aSink)
    1.52 +{
    1.53 +  nsresult rv =
    1.54 +    MediaDocument::StartDocumentLoad(aCommand, aChannel, aLoadGroup, aContainer,
    1.55 +                                     aDocListener, aReset, aSink);
    1.56 +  NS_ENSURE_SUCCESS(rv, rv);
    1.57 +
    1.58 +  mStreamListener = new MediaDocumentStreamListener(this);
    1.59 +
    1.60 +  // Create synthetic document
    1.61 +  rv = CreateSyntheticVideoDocument(aChannel,
    1.62 +      getter_AddRefs(mStreamListener->mNextStream));
    1.63 +  NS_ENSURE_SUCCESS(rv, rv);
    1.64 +
    1.65 +  NS_ADDREF(*aDocListener = mStreamListener);
    1.66 +  return rv;
    1.67 +}
    1.68 +
    1.69 +void
    1.70 +VideoDocument::SetScriptGlobalObject(nsIScriptGlobalObject* aScriptGlobalObject)
    1.71 +{
    1.72 +  // Set the script global object on the superclass before doing
    1.73 +  // anything that might require it....
    1.74 +  MediaDocument::SetScriptGlobalObject(aScriptGlobalObject);
    1.75 +
    1.76 +  if (aScriptGlobalObject) {
    1.77 +    if (!nsContentUtils::IsChildOfSameType(this) &&
    1.78 +        GetReadyStateEnum() != nsIDocument::READYSTATE_COMPLETE) {
    1.79 +      LinkStylesheet(NS_LITERAL_STRING("resource://gre/res/TopLevelVideoDocument.css"));
    1.80 +      LinkStylesheet(NS_LITERAL_STRING("chrome://global/skin/media/TopLevelVideoDocument.css"));
    1.81 +    }
    1.82 +    BecomeInteractive();
    1.83 +  }
    1.84 +}
    1.85 +
    1.86 +nsresult
    1.87 +VideoDocument::CreateSyntheticVideoDocument(nsIChannel* aChannel,
    1.88 +                                            nsIStreamListener** aListener)
    1.89 +{
    1.90 +  // make our generic document
    1.91 +  nsresult rv = MediaDocument::CreateSyntheticDocument();
    1.92 +  NS_ENSURE_SUCCESS(rv, rv);
    1.93 +
    1.94 +  Element* body = GetBodyElement();
    1.95 +  if (!body) {
    1.96 +    NS_WARNING("no body on video document!");
    1.97 +    return NS_ERROR_FAILURE;
    1.98 +  }
    1.99 +
   1.100 +  // make content
   1.101 +  nsCOMPtr<nsINodeInfo> nodeInfo;
   1.102 +  nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::video, nullptr,
   1.103 +                                           kNameSpaceID_XHTML,
   1.104 +                                           nsIDOMNode::ELEMENT_NODE);
   1.105 +
   1.106 +  nsRefPtr<HTMLMediaElement> element =
   1.107 +    static_cast<HTMLMediaElement*>(NS_NewHTMLVideoElement(nodeInfo.forget(),
   1.108 +                                                          NOT_FROM_PARSER));
   1.109 +  if (!element)
   1.110 +    return NS_ERROR_OUT_OF_MEMORY;
   1.111 +  element->SetAutoplay(true);
   1.112 +  element->SetControls(true);
   1.113 +  element->LoadWithChannel(aChannel, aListener);
   1.114 +  UpdateTitle(aChannel);
   1.115 +
   1.116 +  if (nsContentUtils::IsChildOfSameType(this)) {
   1.117 +    // Video documents that aren't toplevel should fill their frames and
   1.118 +    // not have margins
   1.119 +    element->SetAttr(kNameSpaceID_None, nsGkAtoms::style,
   1.120 +        NS_LITERAL_STRING("position:absolute; top:0; left:0; width:100%; height:100%"),
   1.121 +        true);
   1.122 +  }
   1.123 +
   1.124 +  return body->AppendChildTo(element, false);
   1.125 +}
   1.126 +
   1.127 +void
   1.128 +VideoDocument::UpdateTitle(nsIChannel* aChannel)
   1.129 +{
   1.130 +  if (!aChannel)
   1.131 +    return;
   1.132 +
   1.133 +  nsAutoString fileName;
   1.134 +  GetFileName(fileName);
   1.135 +  SetTitle(fileName);
   1.136 +}
   1.137 +
   1.138 +} // namespace dom
   1.139 +} // namespace mozilla
   1.140 +
   1.141 +nsresult
   1.142 +NS_NewVideoDocument(nsIDocument** aResult)
   1.143 +{
   1.144 +  mozilla::dom::VideoDocument* doc = new mozilla::dom::VideoDocument();
   1.145 +
   1.146 +  NS_ADDREF(doc);
   1.147 +  nsresult rv = doc->Init();
   1.148 +
   1.149 +  if (NS_FAILED(rv)) {
   1.150 +    NS_RELEASE(doc);
   1.151 +  }
   1.152 +
   1.153 +  *aResult = doc;
   1.154 +
   1.155 +  return rv;
   1.156 +}

mercurial