Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/html/PlayPromise.cpp
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#include "mozilla/dom/PlayPromise.h"
8
#include "mozilla/Logging.h"
9
#include "mozilla/Telemetry.h"
10
11
extern mozilla::LazyLogModule gMediaElementLog;
12
13
#define PLAY_PROMISE_LOG(msg, ...)                                             \
14
0
  MOZ_LOG(gMediaElementLog, LogLevel::Debug, (msg, ##__VA_ARGS__))
15
16
namespace mozilla {
17
namespace dom {
18
19
PlayPromise::PlayPromise(nsIGlobalObject* aGlobal)
20
  : Promise(aGlobal)
21
0
{
22
0
}
23
24
PlayPromise::~PlayPromise()
25
0
{
26
0
  if (!mFulfilled && PromiseObj()) {
27
0
    MaybeReject(NS_ERROR_DOM_ABORT_ERR);
28
0
  }
29
0
}
30
31
/* static */
32
already_AddRefed<PlayPromise>
33
PlayPromise::Create(nsIGlobalObject* aGlobal, ErrorResult& aRv)
34
0
{
35
0
  RefPtr<PlayPromise> promise = new PlayPromise(aGlobal);
36
0
  promise->CreateWrapper(nullptr, aRv);
37
0
  return aRv.Failed() ? nullptr : promise.forget();
38
0
}
39
40
void
41
PlayPromise::MaybeResolveWithUndefined()
42
0
{
43
0
  if (mFulfilled) {
44
0
    return;
45
0
  }
46
0
  mFulfilled = true;
47
0
  PLAY_PROMISE_LOG("PlayPromise %p resolved with undefined", this);
48
0
  auto reason = Telemetry::LABELS_MEDIA_PLAY_PROMISE_RESOLUTION::Resolved;
49
0
  Telemetry::AccumulateCategorical(reason);
50
0
  Promise::MaybeResolveWithUndefined();
51
0
}
52
53
using PlayLabel = Telemetry::LABELS_MEDIA_PLAY_PROMISE_RESOLUTION;
54
55
struct PlayPromiseTelemetryResult
56
{
57
  nsresult mValue;
58
  PlayLabel mLabel;
59
  const char* mName;
60
};
61
62
static const PlayPromiseTelemetryResult sPlayPromiseTelemetryResults[] = {
63
  {
64
    NS_ERROR_DOM_MEDIA_NOT_ALLOWED_ERR,
65
    PlayLabel::NotAllowedErr,
66
    "NotAllowedErr",
67
  },
68
  {
69
    NS_ERROR_DOM_MEDIA_NOT_SUPPORTED_ERR,
70
    PlayLabel::SrcNotSupportedErr,
71
    "SrcNotSupportedErr",
72
  },
73
  {
74
    NS_ERROR_DOM_MEDIA_ABORT_ERR,
75
    PlayLabel::PauseAbortErr,
76
    "PauseAbortErr",
77
  },
78
  {
79
    NS_ERROR_DOM_ABORT_ERR,
80
    PlayLabel::AbortErr,
81
    "AbortErr",
82
  },
83
};
84
85
static const PlayPromiseTelemetryResult*
86
FindPlayPromiseTelemetryResult(nsresult aReason)
87
0
{
88
0
  for (const auto& p : sPlayPromiseTelemetryResults) {
89
0
    if (p.mValue == aReason) {
90
0
      return &p;
91
0
    }
92
0
  }
93
0
  return nullptr;
94
0
}
95
96
static PlayLabel
97
ToPlayResultLabel(nsresult aReason)
98
0
{
99
0
  auto p = FindPlayPromiseTelemetryResult(aReason);
100
0
  return p ? p->mLabel : PlayLabel::UnknownErr;
101
0
}
102
103
static const char*
104
ToPlayResultStr(nsresult aReason)
105
0
{
106
0
  auto p = FindPlayPromiseTelemetryResult(aReason);
107
0
  return p ? p->mName : "UnknownErr";
108
0
}
109
110
void
111
PlayPromise::MaybeReject(nsresult aReason)
112
0
{
113
0
  if (mFulfilled) {
114
0
    return;
115
0
  }
116
0
  mFulfilled = true;
117
0
  PLAY_PROMISE_LOG("PlayPromise %p rejected with 0x%x (%s)",
118
0
                   this,
119
0
                   static_cast<uint32_t>(aReason),
120
0
                   ToPlayResultStr(aReason));
121
0
  Telemetry::AccumulateCategorical(ToPlayResultLabel(aReason));
122
0
  Promise::MaybeReject(aReason);
123
0
}
124
125
} // namespace dom
126
} // namespace mozilla