• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5
6// This file provides common functions for media actions.
7window.__findMediaElements = function(selector) {
8  // Returns elements matching the selector, otherwise returns the first video
9  // or audio tag element that can be found.
10  // If selector == 'all', returns all media elements.
11  if (selector == 'all') {
12    return document.querySelectorAll('video, audio');
13  } else if (selector) {
14    return document.querySelectorAll(selector);
15  } else {
16    var media = document.getElementsByTagName('video');
17    if (media.length > 0) {
18      return [media[0]];
19    } else {
20      media = document.getElementsByTagName('audio');
21      if (media.length > 0) {
22        return [media[0]];
23      }
24    }
25  }
26  console.error('Could not find any media elements matching: ' + selector);
27  return [];
28};
29
30window.__hasEventCompleted = function(selector, event_name) {
31  // Return true if the event_name fired for media satisfying the selector.
32  var mediaElements = window.__findMediaElements(selector);
33  for (var i = 0; i < mediaElements.length; i++) {
34    if (!mediaElements[i][event_name + '_completed'])
35      return false;
36  }
37  return true;
38};
39
40window.__registerHTML5ErrorEvents = function(element) {
41  // Listens to HTML5 media errors.
42  function onError(e) {
43    window.__error = 'Media error: ' + e.type + ', code:' + e.target.error.code;
44    throw new Error(window.__error);
45  }
46  element.addEventListener('error', onError);
47  element.addEventListener('abort', onError);
48};
49
50window.__registerHTML5EventCompleted = function(element, event_name) {
51  // Logs |even_name| on element when completed.
52  var logEventHappened = function(e) {
53    element[e.type + '_completed'] = true;
54    element.removeEventListener(event_name, logEventHappened);
55  }
56  element.addEventListener(event_name, logEventHappened);
57};
58
59window.__error = null;
60