Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/html/MediaError.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/MediaError.h"
8
9
#include <string>
10
#include <unordered_set>
11
12
#include "mozilla/dom/MediaErrorBinding.h"
13
#include "nsContentUtils.h"
14
#include "nsIScriptError.h"
15
#include "jsapi.h"
16
17
namespace mozilla {
18
namespace dom {
19
20
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MediaError, mParent)
21
NS_IMPL_CYCLE_COLLECTING_ADDREF(MediaError)
22
NS_IMPL_CYCLE_COLLECTING_RELEASE(MediaError)
23
24
0
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaError)
25
0
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
26
0
  NS_INTERFACE_MAP_ENTRY(nsISupports)
27
0
NS_INTERFACE_MAP_END
28
29
MediaError::MediaError(HTMLMediaElement* aParent, uint16_t aCode,
30
                       const nsACString& aMessage)
31
  : mParent(aParent)
32
  , mCode(aCode)
33
  , mMessage(aMessage)
34
0
{
35
0
}
36
37
void
38
MediaError::GetMessage(nsAString& aResult) const
39
0
{
40
0
  // When fingerprinting resistance is enabled, only messages in this list
41
0
  // can be returned to content script.
42
0
  static const std::unordered_set<std::string> whitelist = {
43
0
    "404: Not Found"
44
0
    // TODO
45
0
  };
46
0
47
0
  bool shouldBlank = (whitelist.find(mMessage.get()) == whitelist.end());
48
0
49
0
  if (shouldBlank) {
50
0
    // Print a warning message to JavaScript console to alert developers of
51
0
    // a non-whitelisted error message.
52
0
    nsAutoCString message =
53
0
      NS_LITERAL_CSTRING(
54
0
        "This error message will be blank when privacy.resistFingerprinting = true."
55
0
        "  If it is really necessary, please add it to the whitelist in"
56
0
        " MediaError::GetMessage: ") +
57
0
      mMessage;
58
0
    nsIDocument* ownerDoc = mParent->OwnerDoc();
59
0
    AutoJSAPI api;
60
0
    if (api.Init(ownerDoc->GetScopeObject())) {
61
0
      // We prefer this API because it can also print to our debug log and
62
0
      // try server's log viewer.
63
0
      JS_ReportWarningASCII(api.cx(), "%s", message.get());
64
0
    } else {
65
0
      // If failed to use JS_ReportWarningASCII, fall back to
66
0
      // nsContentUtils::ReportToConsoleNonLocalized, which can only print to
67
0
      // JavaScript console.
68
0
      nsContentUtils::ReportToConsoleNonLocalized(
69
0
        NS_ConvertASCIItoUTF16(message),
70
0
        nsIScriptError::warningFlag,
71
0
        NS_LITERAL_CSTRING("MediaError"),
72
0
        ownerDoc
73
0
      );
74
0
    }
75
0
  }
76
0
77
0
  if (!nsContentUtils::IsCallerChrome() &&
78
0
      nsContentUtils::ShouldResistFingerprinting() &&
79
0
      shouldBlank) {
80
0
    aResult.Truncate();
81
0
    return;
82
0
  }
83
0
84
0
  CopyUTF8toUTF16(mMessage, aResult);
85
0
}
86
87
JSObject*
88
MediaError::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
89
0
{
90
0
  return MediaError_Binding::Wrap(aCx, this, aGivenProto);
91
0
}
92
93
} // namespace dom
94
} // namespace mozilla