|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "MediaDocument.h" |
|
7 #include "nsGkAtoms.h" |
|
8 #include "nsNodeInfoManager.h" |
|
9 #include "nsContentCreatorFunctions.h" |
|
10 #include "mozilla/dom/HTMLMediaElement.h" |
|
11 #include "nsIDocumentInlines.h" |
|
12 #include "nsContentUtils.h" |
|
13 #include "mozilla/dom/Element.h" |
|
14 |
|
15 namespace mozilla { |
|
16 namespace dom { |
|
17 |
|
18 class VideoDocument MOZ_FINAL : public MediaDocument |
|
19 { |
|
20 public: |
|
21 virtual nsresult StartDocumentLoad(const char* aCommand, |
|
22 nsIChannel* aChannel, |
|
23 nsILoadGroup* aLoadGroup, |
|
24 nsISupports* aContainer, |
|
25 nsIStreamListener** aDocListener, |
|
26 bool aReset = true, |
|
27 nsIContentSink* aSink = nullptr); |
|
28 virtual void SetScriptGlobalObject(nsIScriptGlobalObject* aScriptGlobalObject); |
|
29 |
|
30 protected: |
|
31 |
|
32 // Sets document <title> to reflect the file name and description. |
|
33 void UpdateTitle(nsIChannel* aChannel); |
|
34 |
|
35 nsresult CreateSyntheticVideoDocument(nsIChannel* aChannel, |
|
36 nsIStreamListener** aListener); |
|
37 |
|
38 nsRefPtr<MediaDocumentStreamListener> mStreamListener; |
|
39 }; |
|
40 |
|
41 nsresult |
|
42 VideoDocument::StartDocumentLoad(const char* aCommand, |
|
43 nsIChannel* aChannel, |
|
44 nsILoadGroup* aLoadGroup, |
|
45 nsISupports* aContainer, |
|
46 nsIStreamListener** aDocListener, |
|
47 bool aReset, |
|
48 nsIContentSink* aSink) |
|
49 { |
|
50 nsresult rv = |
|
51 MediaDocument::StartDocumentLoad(aCommand, aChannel, aLoadGroup, aContainer, |
|
52 aDocListener, aReset, aSink); |
|
53 NS_ENSURE_SUCCESS(rv, rv); |
|
54 |
|
55 mStreamListener = new MediaDocumentStreamListener(this); |
|
56 |
|
57 // Create synthetic document |
|
58 rv = CreateSyntheticVideoDocument(aChannel, |
|
59 getter_AddRefs(mStreamListener->mNextStream)); |
|
60 NS_ENSURE_SUCCESS(rv, rv); |
|
61 |
|
62 NS_ADDREF(*aDocListener = mStreamListener); |
|
63 return rv; |
|
64 } |
|
65 |
|
66 void |
|
67 VideoDocument::SetScriptGlobalObject(nsIScriptGlobalObject* aScriptGlobalObject) |
|
68 { |
|
69 // Set the script global object on the superclass before doing |
|
70 // anything that might require it.... |
|
71 MediaDocument::SetScriptGlobalObject(aScriptGlobalObject); |
|
72 |
|
73 if (aScriptGlobalObject) { |
|
74 if (!nsContentUtils::IsChildOfSameType(this) && |
|
75 GetReadyStateEnum() != nsIDocument::READYSTATE_COMPLETE) { |
|
76 LinkStylesheet(NS_LITERAL_STRING("resource://gre/res/TopLevelVideoDocument.css")); |
|
77 LinkStylesheet(NS_LITERAL_STRING("chrome://global/skin/media/TopLevelVideoDocument.css")); |
|
78 } |
|
79 BecomeInteractive(); |
|
80 } |
|
81 } |
|
82 |
|
83 nsresult |
|
84 VideoDocument::CreateSyntheticVideoDocument(nsIChannel* aChannel, |
|
85 nsIStreamListener** aListener) |
|
86 { |
|
87 // make our generic document |
|
88 nsresult rv = MediaDocument::CreateSyntheticDocument(); |
|
89 NS_ENSURE_SUCCESS(rv, rv); |
|
90 |
|
91 Element* body = GetBodyElement(); |
|
92 if (!body) { |
|
93 NS_WARNING("no body on video document!"); |
|
94 return NS_ERROR_FAILURE; |
|
95 } |
|
96 |
|
97 // make content |
|
98 nsCOMPtr<nsINodeInfo> nodeInfo; |
|
99 nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::video, nullptr, |
|
100 kNameSpaceID_XHTML, |
|
101 nsIDOMNode::ELEMENT_NODE); |
|
102 |
|
103 nsRefPtr<HTMLMediaElement> element = |
|
104 static_cast<HTMLMediaElement*>(NS_NewHTMLVideoElement(nodeInfo.forget(), |
|
105 NOT_FROM_PARSER)); |
|
106 if (!element) |
|
107 return NS_ERROR_OUT_OF_MEMORY; |
|
108 element->SetAutoplay(true); |
|
109 element->SetControls(true); |
|
110 element->LoadWithChannel(aChannel, aListener); |
|
111 UpdateTitle(aChannel); |
|
112 |
|
113 if (nsContentUtils::IsChildOfSameType(this)) { |
|
114 // Video documents that aren't toplevel should fill their frames and |
|
115 // not have margins |
|
116 element->SetAttr(kNameSpaceID_None, nsGkAtoms::style, |
|
117 NS_LITERAL_STRING("position:absolute; top:0; left:0; width:100%; height:100%"), |
|
118 true); |
|
119 } |
|
120 |
|
121 return body->AppendChildTo(element, false); |
|
122 } |
|
123 |
|
124 void |
|
125 VideoDocument::UpdateTitle(nsIChannel* aChannel) |
|
126 { |
|
127 if (!aChannel) |
|
128 return; |
|
129 |
|
130 nsAutoString fileName; |
|
131 GetFileName(fileName); |
|
132 SetTitle(fileName); |
|
133 } |
|
134 |
|
135 } // namespace dom |
|
136 } // namespace mozilla |
|
137 |
|
138 nsresult |
|
139 NS_NewVideoDocument(nsIDocument** aResult) |
|
140 { |
|
141 mozilla::dom::VideoDocument* doc = new mozilla::dom::VideoDocument(); |
|
142 |
|
143 NS_ADDREF(doc); |
|
144 nsresult rv = doc->Init(); |
|
145 |
|
146 if (NS_FAILED(rv)) { |
|
147 NS_RELEASE(doc); |
|
148 } |
|
149 |
|
150 *aResult = doc; |
|
151 |
|
152 return rv; |
|
153 } |