summaryrefslogtreecommitdiff
path: root/toolkit/components/telemetry/WebrtcTelemetry.cpp
blob: 29c22be23581a312f3d50d632633af868ccbc116 (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
/* -*- 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 "Telemetry.h"
#include "TelemetryCommon.h"
#include "WebrtcTelemetry.h"
#include "jsapi.h"
#include "nsPrintfCString.h"
#include "nsTHashtable.h"

using mozilla::Telemetry::Common::AutoHashtable;

void
WebrtcTelemetry::RecordIceCandidateMask(const uint32_t iceCandidateBitmask,
                                        const bool success)
{
  WebrtcIceCandidateType *entry = mWebrtcIceCandidates.GetEntry(iceCandidateBitmask);
  if (!entry) {
    entry = mWebrtcIceCandidates.PutEntry(iceCandidateBitmask);
    if (MOZ_UNLIKELY(!entry))
      return;
  }

  if (success) {
    entry->mData.webrtc.successCount++;
  } else {
    entry->mData.webrtc.failureCount++;
  }
}

bool
ReflectIceEntry(const WebrtcTelemetry::WebrtcIceCandidateType *entry,
                const WebrtcTelemetry::WebrtcIceCandidateStats *stat, JSContext *cx,
                JS::Handle<JSObject*> obj)
{
  if ((stat->successCount == 0) && (stat->failureCount == 0))
    return true;

  const uint32_t &bitmask = entry->GetKey();

  JS::Rooted<JSObject*> statsObj(cx, JS_NewPlainObject(cx));
  if (!statsObj)
    return false;
  if (!JS_DefineProperty(cx, obj,
                         nsPrintfCString("%lu", bitmask).BeginReading(),
                         statsObj, JSPROP_ENUMERATE)) {
    return false;
  }
  if (stat->successCount && !JS_DefineProperty(cx, statsObj, "successCount",
                                               stat->successCount,
                                               JSPROP_ENUMERATE)) {
    return false;
  }
  if (stat->failureCount && !JS_DefineProperty(cx, statsObj, "failureCount",
                                               stat->failureCount,
                                               JSPROP_ENUMERATE)) {
    return false;
  }
  return true;
}

bool
ReflectIceWebrtc(WebrtcTelemetry::WebrtcIceCandidateType *entry, JSContext *cx,
                 JS::Handle<JSObject*> obj)
{
  return ReflectIceEntry(entry, &entry->mData.webrtc, cx, obj);
}

bool
WebrtcTelemetry::AddIceInfo(JSContext *cx, JS::Handle<JSObject*> iceObj)
{
  JS::Rooted<JSObject*> statsObj(cx, JS_NewPlainObject(cx));
  if (!statsObj)
    return false;

  if (!mWebrtcIceCandidates.ReflectIntoJS(ReflectIceWebrtc, cx, statsObj)) {
    return false;
  }

  return JS_DefineProperty(cx, iceObj, "webrtc",
                           statsObj, JSPROP_ENUMERATE);
}

bool
WebrtcTelemetry::GetWebrtcStats(JSContext *cx, JS::MutableHandle<JS::Value> ret)
{
  JS::Rooted<JSObject*> root_obj(cx, JS_NewPlainObject(cx));
  if (!root_obj)
    return false;
  ret.setObject(*root_obj);

  JS::Rooted<JSObject*> ice_obj(cx, JS_NewPlainObject(cx));
  if (!ice_obj)
    return false;
  JS_DefineProperty(cx, root_obj, "IceCandidatesStats", ice_obj,
                    JSPROP_ENUMERATE);

  if (!AddIceInfo(cx, ice_obj))
    return false;

  return true;
}

size_t
WebrtcTelemetry::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
{
  return mWebrtcIceCandidates.ShallowSizeOfExcludingThis(aMallocSizeOf);
}