summaryrefslogtreecommitdiffstats
path: root/dom/html/HTMLTrackElement.cpp
blob: 5363d3e39936afad863f35743a4bf2a661a6323c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/HTMLTrackElement.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/HTMLMediaElement.h"
#include "mozilla/dom/WebVTTListener.h"
#include "mozilla/LoadInfo.h"
#include "mozilla/StaticPrefs_media.h"
#include "mozilla/dom/HTMLTrackElementBinding.h"
#include "mozilla/dom/HTMLUnknownElement.h"
#include "nsAttrValueInlines.h"
#include "nsCOMPtr.h"
#include "nsContentPolicyUtils.h"
#include "nsContentUtils.h"
#include "nsCycleCollectionParticipant.h"
#include "nsGenericHTMLElement.h"
#include "nsGkAtoms.h"
#include "nsIContentPolicy.h"
#include "mozilla/dom/Document.h"
#include "nsILoadGroup.h"
#include "nsIObserver.h"
#include "nsIObserverService.h"
#include "nsIScriptError.h"
#include "nsISupportsImpl.h"
#include "nsISupportsPrimitives.h"
#include "nsNetUtil.h"
#include "nsStyleConsts.h"
#include "nsThreadUtils.h"
#include "nsVideoFrame.h"

extern mozilla::LazyLogModule gTextTrackLog;
#define LOG(msg, ...)                       \
  MOZ_LOG(gTextTrackLog, LogLevel::Verbose, \
          ("TextTrackElement=%p, " msg, this, ##__VA_ARGS__))

// Replace the usual NS_IMPL_NS_NEW_HTML_ELEMENT(Track) so
// we can return an UnknownElement instead when pref'd off.
nsGenericHTMLElement* NS_NewHTMLTrackElement(
    already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
    mozilla::dom::FromParser aFromParser) {
  RefPtr<mozilla::dom::NodeInfo> nodeInfo(aNodeInfo);
  auto* nim = nodeInfo->NodeInfoManager();
  return new (nim) mozilla::dom::HTMLTrackElement(nodeInfo.forget());
}

namespace mozilla::dom {

// Map html attribute string values to TextTrackKind enums.
static constexpr nsAttrValue::EnumTable kKindTable[] = {
    {"subtitles", static_cast<int16_t>(TextTrackKind::Subtitles)},
    {"captions", static_cast<int16_t>(TextTrackKind::Captions)},
    {"descriptions", static_cast<int16_t>(TextTrackKind::Descriptions)},
    {"chapters", static_cast<int16_t>(TextTrackKind::Chapters)},
    {"metadata", static_cast<int16_t>(TextTrackKind::Metadata)},
    {nullptr, 0}};

// Invalid values are treated as "metadata" in ParseAttribute, but if no value
// at all is specified, it's treated as "subtitles" in GetKind
static constexpr const nsAttrValue::EnumTable* kKindTableInvalidValueDefault =
    &kKindTable[4];

class WindowDestroyObserver final : public nsIObserver {
  NS_DECL_ISUPPORTS

 public:
  explicit WindowDestroyObserver(HTMLTrackElement* aElement, uint64_t aWinID)
      : mTrackElement(aElement), mInnerID(aWinID) {
    RegisterWindowDestroyObserver();
  }
  void RegisterWindowDestroyObserver() {
    nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
    if (obs) {
      obs->AddObserver(this, "inner-window-destroyed", false);
    }
  }
  void UnRegisterWindowDestroyObserver() {
    nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
    if (obs) {
      obs->RemoveObserver(this, "inner-window-destroyed");
    }
    mTrackElement = nullptr;
  }
  NS_IMETHODIMP Observe(nsISupports* aSubject, const char* aTopic,
                        const char16_t* aData) override {
    MOZ_ASSERT(NS_IsMainThread());
    if (strcmp(aTopic, "inner-window-destroyed") == 0) {
      nsCOMPtr<nsISupportsPRUint64> wrapper = do_QueryInterface(aSubject);
      NS_ENSURE_TRUE(wrapper, NS_ERROR_FAILURE);
      uint64_t innerID;
      nsresult rv = wrapper->GetData(&innerID);
      NS_ENSURE_SUCCESS(rv, rv);
      if (innerID == mInnerID) {
        if (mTrackElement) {
          mTrackElement->CancelChannelAndListener();
        }
        UnRegisterWindowDestroyObserver();
      }
    }
    return NS_OK;
  }

 private:
  ~WindowDestroyObserver() = default;

  HTMLTrackElement* mTrackElement;
  uint64_t mInnerID;
};
NS_IMPL_ISUPPORTS(WindowDestroyObserver, nsIObserver);

/** HTMLTrackElement */
HTMLTrackElement::HTMLTrackElement(
    already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
    : nsGenericHTMLElement(std::move(aNodeInfo)),
      mLoadResourceDispatched(false),
      mWindowDestroyObserver(nullptr) {
  nsISupports* parentObject = OwnerDoc()->GetParentObject();
  NS_ENSURE_TRUE_VOID(parentObject);
  nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(parentObject);
  if (window) {
    mWindowDestroyObserver =
        new WindowDestroyObserver(this, window->WindowID());
  }
}

HTMLTrackElement::~HTMLTrackElement() {
  if (mWindowDestroyObserver) {
    mWindowDestroyObserver->UnRegisterWindowDestroyObserver();
  }
  CancelChannelAndListener();
}

NS_IMPL_ELEMENT_CLONE(HTMLTrackElement)

NS_IMPL_CYCLE_COLLECTION_INHERITED(HTMLTrackElement, nsGenericHTMLElement,
                                   mTrack, mMediaParent, mListener)

NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(HTMLTrackElement,
                                               nsGenericHTMLElement)

void HTMLTrackElement::GetKind(DOMString& aKind) const {
  GetEnumAttr(nsGkAtoms::kind, kKindTable[0].tag, aKind);
}

void HTMLTrackElement::OnChannelRedirect(nsIChannel* aChannel,
                                         nsIChannel* aNewChannel,
                                         uint32_t aFlags) {
  NS_ASSERTION(aChannel == mChannel, "Channels should match!");
  mChannel = aNewChannel;
}

JSObject* HTMLTrackElement::WrapNode(JSContext* aCx,
                                     JS::Handle<JSObject*> aGivenProto) {
  return HTMLTrackElement_Binding::Wrap(aCx, this, aGivenProto);
}

TextTrack* HTMLTrackElement::GetTrack() {
  if (!mTrack) {
    CreateTextTrack();
  }
  return mTrack;
}

void HTMLTrackElement::CreateTextTrack() {
  nsISupports* parentObject = OwnerDoc()->GetParentObject();
  nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(parentObject);
  if (!parentObject) {
    nsContentUtils::ReportToConsole(
        nsIScriptError::errorFlag, "Media"_ns, OwnerDoc(),
        nsContentUtils::eDOM_PROPERTIES,
        "Using track element in non-window context");
    return;
  }

  nsString label, srcLang;
  GetSrclang(srcLang);
  GetLabel(label);

  TextTrackKind kind;
  if (const nsAttrValue* value = GetParsedAttr(nsGkAtoms::kind)) {
    kind = static_cast<TextTrackKind>(value->GetEnumValue());
  } else {
    kind = TextTrackKind::Subtitles;
  }

  MOZ_ASSERT(!mTrack, "No need to recreate a text track!");
  mTrack =
      new TextTrack(window, kind, label, srcLang, TextTrackMode::Disabled,
                    TextTrackReadyState::NotLoaded, TextTrackSource::Track);
  mTrack->SetTrackElement(this);
}

bool HTMLTrackElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
                                      const nsAString& aValue,
                                      nsIPrincipal* aMaybeScriptedPrincipal,
                                      nsAttrValue& aResult) {
  if (aNamespaceID == kNameSpaceID_None && aAttribute == nsGkAtoms::kind) {
    // Case-insensitive lookup, with the first element as the default.
    return aResult.ParseEnumValue(aValue, kKindTable, false,
                                  kKindTableInvalidValueDefault);
  }

  // Otherwise call the generic implementation.
  return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
                                              aMaybeScriptedPrincipal, aResult);
}

void HTMLTrackElement::SetSrc(const nsAString& aSrc, ErrorResult& aError) {
  LOG("Set src=%s", NS_ConvertUTF16toUTF8(aSrc).get());

  nsAutoString src;
  if (GetAttr(nsGkAtoms::src, src) && src == aSrc) {
    LOG("No need to reload for same src url");
    return;
  }

  SetHTMLAttr(nsGkAtoms::src, aSrc, aError);
  SetReadyState(TextTrackReadyState::NotLoaded);
  if (!mMediaParent) {
    return;
  }

  // Stop WebVTTListener.
  mListener = nullptr;
  if (mChannel) {
    mChannel->CancelWithReason(NS_BINDING_ABORTED,
                               "HTMLTrackElement::SetSrc"_ns);
    mChannel = nullptr;
  }

  MaybeDispatchLoadResource();
}

void HTMLTrackElement::MaybeClearAllCues() {
  // Empty track's cue list whenever the track element's `src` attribute set,
  // changed, or removed,
  // https://html.spec.whatwg.org/multipage/media.html#sourcing-out-of-band-text-tracks:attr-track-src
  if (!mTrack) {
    return;
  }
  mTrack->ClearAllCues();
}

// This function will run partial steps from `start-the-track-processing-model`
// and finish the rest of steps in `LoadResource()` during the stable state.
// https://html.spec.whatwg.org/multipage/media.html#start-the-track-processing-model
void HTMLTrackElement::MaybeDispatchLoadResource() {
  MOZ_ASSERT(mTrack, "Should have already created text track!");

  // step2, if the text track's text track mode is not set to one of hidden or
  // showing, then return.
  if (mTrack->Mode() == TextTrackMode::Disabled) {
    LOG("Do not load resource for disable track");
    return;
  }

  // step3, if the text track's track element does not have a media element as a
  // parent, return.
  if (!mMediaParent) {
    LOG("Do not load resource for track without media element");
    return;
  }

  if (ReadyState() == TextTrackReadyState::Loaded) {
    LOG("Has already loaded resource");
    return;
  }

  // step5, await a stable state and run the rest of steps.
  if (!mLoadResourceDispatched) {
    RefPtr<WebVTTListener> listener = new WebVTTListener(this);
    RefPtr<Runnable> r = NewRunnableMethod<RefPtr<WebVTTListener>>(
        "dom::HTMLTrackElement::LoadResource", this,
        &HTMLTrackElement::LoadResource, std::move(listener));
    nsContentUtils::RunInStableState(r.forget());
    mLoadResourceDispatched = true;
  }
}

void HTMLTrackElement::LoadResource(RefPtr<WebVTTListener>&& aWebVTTListener) {
  LOG("LoadResource");
  mLoadResourceDispatched = false;

  nsAutoString src;
  if (!GetAttr(nsGkAtoms::src, src) || src.IsEmpty()) {
    LOG("Fail to load because no src");
    SetReadyState(TextTrackReadyState::FailedToLoad);
    return;
  }

  nsCOMPtr<nsIURI> uri;
  nsresult rv = NewURIFromString(src, getter_AddRefs(uri));
  NS_ENSURE_TRUE_VOID(NS_SUCCEEDED(rv));
  LOG("Trying to load from src=%s", NS_ConvertUTF16toUTF8(src).get());

  CancelChannelAndListener();

  // According to
  // https://www.w3.org/TR/html5/embedded-content-0.html#sourcing-out-of-band-text-tracks
  //
  // "8: If the track element's parent is a media element then let CORS mode
  // be the state of the parent media element's crossorigin content attribute.
  // Otherwise, let CORS mode be No CORS."
  //
  CORSMode corsMode =
      mMediaParent ? AttrValueToCORSMode(
                         mMediaParent->GetParsedAttr(nsGkAtoms::crossorigin))
                   : CORS_NONE;

  // Determine the security flag based on corsMode.
  nsSecurityFlags secFlags;
  if (CORS_NONE == corsMode) {
    // Same-origin is required for track element.
    secFlags = nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_INHERITS_SEC_CONTEXT;
  } else {
    secFlags = nsILoadInfo::SEC_REQUIRE_CORS_INHERITS_SEC_CONTEXT;
    if (CORS_ANONYMOUS == corsMode) {
      secFlags |= nsILoadInfo::SEC_COOKIES_SAME_ORIGIN;
    } else if (CORS_USE_CREDENTIALS == corsMode) {
      secFlags |= nsILoadInfo::SEC_COOKIES_INCLUDE;
    } else {
      NS_WARNING("Unknown CORS mode.");
      secFlags = nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_INHERITS_SEC_CONTEXT;
    }
  }

  mListener = std::move(aWebVTTListener);
  // This will do 6. Set the text track readiness state to loading.
  rv = mListener->LoadResource();
  NS_ENSURE_TRUE_VOID(NS_SUCCEEDED(rv));

  Document* doc = OwnerDoc();
  if (!doc) {
    return;
  }

  // 9. End the synchronous section, continuing the remaining steps in parallel.
  nsCOMPtr<nsIRunnable> runnable = NS_NewRunnableFunction(
      "dom::HTMLTrackElement::LoadResource",
      [self = RefPtr<HTMLTrackElement>(this), this, uri, secFlags]() {
        if (!mListener) {
          // Shutdown got called, abort.
          return;
        }
        nsCOMPtr<nsIChannel> channel;
        nsCOMPtr<nsILoadGroup> loadGroup = OwnerDoc()->GetDocumentLoadGroup();
        nsresult rv = NS_NewChannel(getter_AddRefs(channel), uri,
                                    static_cast<Element*>(this), secFlags,
                                    nsIContentPolicy::TYPE_INTERNAL_TRACK,
                                    nullptr,  // PerformanceStorage
                                    loadGroup);

        if (NS_FAILED(rv)) {
          LOG("create channel failed.");
          SetReadyState(TextTrackReadyState::FailedToLoad);
          return;
        }

        channel->SetNotificationCallbacks(mListener);

        LOG("opening webvtt channel");
        rv = channel->AsyncOpen(mListener);

        if (NS_FAILED(rv)) {
          SetReadyState(TextTrackReadyState::FailedToLoad);
          return;
        }
        mChannel = channel;
      });
  doc->Dispatch(runnable.forget());
}

nsresult HTMLTrackElement::BindToTree(BindContext& aContext, nsINode& aParent) {
  nsresult rv = nsGenericHTMLElement::BindToTree(aContext, aParent);
  NS_ENSURE_SUCCESS(rv, rv);

  LOG("Track Element bound to tree.");
  auto* parent = HTMLMediaElement::FromNode(aParent);
  if (!parent) {
    return NS_OK;
  }

  // Store our parent so we can look up its frame for display.
  if (!mMediaParent) {
    mMediaParent = parent;

    // TODO: separate notification for 'alternate' tracks?
    mMediaParent->NotifyAddedSource();
    LOG("Track element sent notification to parent.");

    // We may already have a TextTrack at this point if GetTrack() has already
    // been called. This happens, for instance, if script tries to get the
    // TextTrack before its mTrackElement has been bound to the DOM tree.
    if (!mTrack) {
      CreateTextTrack();
    }
    // As `CreateTextTrack()` might fail, so we have to check it again.
    if (mTrack) {
      LOG("Add text track to media parent");
      mMediaParent->AddTextTrack(mTrack);
    }
    MaybeDispatchLoadResource();
  }

  return NS_OK;
}

void HTMLTrackElement::UnbindFromTree(bool aNullParent) {
  if (mMediaParent && aNullParent) {
    // mTrack can be null if HTMLTrackElement::LoadResource has never been
    // called.
    if (mTrack) {
      mMediaParent->RemoveTextTrack(mTrack);
      mMediaParent->UpdateReadyState();
    }
    mMediaParent = nullptr;
  }

  nsGenericHTMLElement::UnbindFromTree(aNullParent);
}

TextTrackReadyState HTMLTrackElement::ReadyState() const {
  if (!mTrack) {
    return TextTrackReadyState::NotLoaded;
  }

  return mTrack->ReadyState();
}

void HTMLTrackElement::SetReadyState(TextTrackReadyState aReadyState) {
  if (ReadyState() == aReadyState) {
    return;
  }

  if (mTrack) {
    switch (aReadyState) {
      case TextTrackReadyState::Loaded:
        LOG("dispatch 'load' event");
        DispatchTrackRunnable(u"load"_ns);
        break;
      case TextTrackReadyState::FailedToLoad:
        LOG("dispatch 'error' event");
        DispatchTrackRunnable(u"error"_ns);
        break;
      default:
        break;
    }
    mTrack->SetReadyState(aReadyState);
  }
}

void HTMLTrackElement::DispatchTrackRunnable(const nsString& aEventName) {
  Document* doc = OwnerDoc();
  if (!doc) {
    return;
  }
  nsCOMPtr<nsIRunnable> runnable = NewRunnableMethod<const nsString>(
      "dom::HTMLTrackElement::DispatchTrustedEvent", this,
      &HTMLTrackElement::DispatchTrustedEvent, aEventName);
  doc->Dispatch(runnable.forget());
}

void HTMLTrackElement::DispatchTrustedEvent(const nsAString& aName) {
  Document* doc = OwnerDoc();
  if (!doc) {
    return;
  }
  nsContentUtils::DispatchTrustedEvent(doc, this, aName, CanBubble::eNo,
                                       Cancelable::eNo);
}

void HTMLTrackElement::CancelChannelAndListener() {
  if (mChannel) {
    mChannel->CancelWithReason(NS_BINDING_ABORTED,
                               "HTMLTrackElement::CancelChannelAndListener"_ns);
    mChannel->SetNotificationCallbacks(nullptr);
    mChannel = nullptr;
  }

  if (mListener) {
    mListener->Cancel();
    mListener = nullptr;
  }
}

void HTMLTrackElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
                                    const nsAttrValue* aValue,
                                    const nsAttrValue* aOldValue,
                                    nsIPrincipal* aMaybeScriptedPrincipal,
                                    bool aNotify) {
  if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::src) {
    MaybeClearAllCues();
    // In spec, `start the track processing model` step10, while fetching is
    // ongoing, if the track URL changes, then we have to set the `FailedToLoad`
    // state.
    // https://html.spec.whatwg.org/multipage/media.html#sourcing-out-of-band-text-tracks:text-track-failed-to-load-3
    if (ReadyState() == TextTrackReadyState::Loading && aValue != aOldValue) {
      SetReadyState(TextTrackReadyState::FailedToLoad);
    }
  }
  return nsGenericHTMLElement::AfterSetAttr(
      aNameSpaceID, aName, aValue, aOldValue, aMaybeScriptedPrincipal, aNotify);
}

void HTMLTrackElement::DispatchTestEvent(const nsAString& aName) {
  if (!StaticPrefs::media_webvtt_testing_events()) {
    return;
  }
  DispatchTrustedEvent(aName);
}

#undef LOG

}  // namespace mozilla::dom