1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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/. */
7 #include "mozilla/dom/HTMLAudioElement.h"
8 #include "mozilla/dom/HTMLAudioElementBinding.h"
10 #include "nsGenericHTMLElement.h"
11 #include "nsGkAtoms.h"
12 #include "mozilla/dom/Document.h"
13 #include "jsfriendapi.h"
14 #include "nsContentUtils.h"
15 #include "nsJSUtils.h"
16 #include "AudioSampleFormat.h"
18 #include "nsComponentManagerUtils.h"
19 #include "nsIHttpChannel.h"
20 #include "mozilla/dom/TimeRanges.h"
21 #include "AudioStream.h"
23 nsGenericHTMLElement
* NS_NewHTMLAudioElement(
24 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
,
25 mozilla::dom::FromParser aFromParser
) {
26 RefPtr
<mozilla::dom::NodeInfo
> nodeInfo(aNodeInfo
);
27 auto* nim
= nodeInfo
->NodeInfoManager();
28 mozilla::dom::HTMLAudioElement
* element
=
29 new (nim
) mozilla::dom::HTMLAudioElement(nodeInfo
.forget());
34 namespace mozilla::dom
{
36 nsresult
HTMLAudioElement::Clone(mozilla::dom::NodeInfo
* aNodeInfo
,
37 nsINode
** aResult
) const {
39 RefPtr
<mozilla::dom::NodeInfo
> ni(aNodeInfo
);
40 auto* nim
= ni
->NodeInfoManager();
41 HTMLAudioElement
* it
= new (nim
) HTMLAudioElement(ni
.forget());
43 nsCOMPtr
<nsINode
> kungFuDeathGrip
= it
;
44 nsresult rv
= const_cast<HTMLAudioElement
*>(this)->CopyInnerTo(it
);
45 if (NS_SUCCEEDED(rv
)) {
46 kungFuDeathGrip
.swap(*aResult
);
51 HTMLAudioElement::HTMLAudioElement(already_AddRefed
<NodeInfo
>&& aNodeInfo
)
52 : HTMLMediaElement(std::move(aNodeInfo
)) {
53 DecoderDoctorLogger::LogConstruction(this);
56 HTMLAudioElement::~HTMLAudioElement() {
57 DecoderDoctorLogger::LogDestruction(this);
60 bool HTMLAudioElement::IsInteractiveHTMLContent() const {
61 return HasAttr(nsGkAtoms::controls
) ||
62 HTMLMediaElement::IsInteractiveHTMLContent();
65 already_AddRefed
<HTMLAudioElement
> HTMLAudioElement::Audio(
66 const GlobalObject
& aGlobal
, const Optional
<nsAString
>& aSrc
,
68 nsCOMPtr
<nsPIDOMWindowInner
> win
= do_QueryInterface(aGlobal
.GetAsSupports());
70 if (!win
|| !(doc
= win
->GetExtantDoc())) {
71 aRv
.Throw(NS_ERROR_FAILURE
);
75 RefPtr
<mozilla::dom::NodeInfo
> nodeInfo
= doc
->NodeInfoManager()->GetNodeInfo(
76 nsGkAtoms::audio
, nullptr, kNameSpaceID_XHTML
, ELEMENT_NODE
);
78 RefPtr
<HTMLAudioElement
> audio
=
79 static_cast<HTMLAudioElement
*>(NS_NewHTMLAudioElement(nodeInfo
.forget()));
80 audio
->SetHTMLAttr(nsGkAtoms::preload
, u
"auto"_ns
, aRv
);
85 if (aSrc
.WasPassed()) {
86 audio
->SetSrc(aSrc
.Value(), aRv
);
89 return audio
.forget();
92 nsresult
HTMLAudioElement::SetAcceptHeader(nsIHttpChannel
* aChannel
) {
98 "application/ogg;q=0.7,"
99 "video/*;q=0.6,*/*;q=0.5");
101 return aChannel
->SetRequestHeader("Accept"_ns
, value
, false);
104 JSObject
* HTMLAudioElement::WrapNode(JSContext
* aCx
,
105 JS::Handle
<JSObject
*> aGivenProto
) {
106 return HTMLAudioElement_Binding::Wrap(aCx
, this, aGivenProto
);
109 } // namespace mozilla::dom