Backed out changeset 1d9301697aa0 (bug 1887752) for causing failures on browser_all_f...
[gecko.git] / dom / html / HTMLAudioElement.cpp
blob766df668b853965d8e57207a47b3d902977556d1
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"
9 #include "nsError.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"
17 #include <algorithm>
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());
30 element->Init();
31 return element;
34 namespace mozilla::dom {
36 nsresult HTMLAudioElement::Clone(mozilla::dom::NodeInfo* aNodeInfo,
37 nsINode** aResult) const {
38 *aResult = nullptr;
39 RefPtr<mozilla::dom::NodeInfo> ni(aNodeInfo);
40 auto* nim = ni->NodeInfoManager();
41 HTMLAudioElement* it = new (nim) HTMLAudioElement(ni.forget());
42 it->Init();
43 nsCOMPtr<nsINode> kungFuDeathGrip = it;
44 nsresult rv = const_cast<HTMLAudioElement*>(this)->CopyInnerTo(it);
45 if (NS_SUCCEEDED(rv)) {
46 kungFuDeathGrip.swap(*aResult);
48 return rv;
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,
67 ErrorResult& aRv) {
68 nsCOMPtr<nsPIDOMWindowInner> win = do_QueryInterface(aGlobal.GetAsSupports());
69 Document* doc;
70 if (!win || !(doc = win->GetExtantDoc())) {
71 aRv.Throw(NS_ERROR_FAILURE);
72 return nullptr;
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);
81 if (aRv.Failed()) {
82 return nullptr;
85 if (aSrc.WasPassed()) {
86 audio->SetSrc(aSrc.Value(), aRv);
89 return audio.forget();
92 nsresult HTMLAudioElement::SetAcceptHeader(nsIHttpChannel* aChannel) {
93 nsAutoCString value(
94 "audio/webm,"
95 "audio/ogg,"
96 "audio/wav,"
97 "audio/*;q=0.9,"
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