Line data Source code
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 :
8 : #include "Telemetry.h"
9 : #include "TelemetryCommon.h"
10 : #include "WebrtcTelemetry.h"
11 : #include "jsapi.h"
12 : #include "nsPrintfCString.h"
13 : #include "nsTHashtable.h"
14 :
15 : void
16 0 : WebrtcTelemetry::RecordIceCandidateMask(const uint32_t iceCandidateBitmask,
17 : const bool success)
18 : {
19 0 : WebrtcIceCandidateType *entry = mWebrtcIceCandidates.GetEntry(iceCandidateBitmask);
20 0 : if (!entry) {
21 0 : entry = mWebrtcIceCandidates.PutEntry(iceCandidateBitmask);
22 0 : if (MOZ_UNLIKELY(!entry))
23 0 : return;
24 : }
25 :
26 0 : if (success) {
27 0 : entry->mData.webrtc.successCount++;
28 : } else {
29 0 : entry->mData.webrtc.failureCount++;
30 : }
31 : }
32 :
33 : bool
34 0 : ReflectIceEntry(const WebrtcTelemetry::WebrtcIceCandidateType *entry,
35 : const WebrtcTelemetry::WebrtcIceCandidateStats *stat, JSContext *cx,
36 : JS::Handle<JSObject*> obj)
37 : {
38 0 : if ((stat->successCount == 0) && (stat->failureCount == 0))
39 0 : return true;
40 :
41 0 : const uint32_t &bitmask = entry->GetKey();
42 :
43 0 : JS::Rooted<JSObject*> statsObj(cx, JS_NewPlainObject(cx));
44 0 : if (!statsObj)
45 0 : return false;
46 0 : if (!JS_DefineProperty(cx, obj,
47 0 : nsPrintfCString("%" PRIu32, bitmask).BeginReading(),
48 : statsObj, JSPROP_ENUMERATE)) {
49 0 : return false;
50 : }
51 0 : if (stat->successCount && !JS_DefineProperty(cx, statsObj, "successCount",
52 0 : stat->successCount,
53 : JSPROP_ENUMERATE)) {
54 0 : return false;
55 : }
56 0 : if (stat->failureCount && !JS_DefineProperty(cx, statsObj, "failureCount",
57 0 : stat->failureCount,
58 : JSPROP_ENUMERATE)) {
59 0 : return false;
60 : }
61 0 : return true;
62 : }
63 :
64 : bool
65 0 : ReflectIceWebrtc(WebrtcTelemetry::WebrtcIceCandidateType *entry, JSContext *cx,
66 : JS::Handle<JSObject*> obj)
67 : {
68 0 : return ReflectIceEntry(entry, &entry->mData.webrtc, cx, obj);
69 : }
70 :
71 : bool
72 0 : WebrtcTelemetry::AddIceInfo(JSContext *cx, JS::Handle<JSObject*> iceObj)
73 : {
74 0 : JS::Rooted<JSObject*> statsObj(cx, JS_NewPlainObject(cx));
75 0 : if (!statsObj)
76 0 : return false;
77 :
78 0 : if (!mWebrtcIceCandidates.ReflectIntoJS(ReflectIceWebrtc, cx, statsObj)) {
79 0 : return false;
80 : }
81 :
82 0 : return JS_DefineProperty(cx, iceObj, "webrtc",
83 0 : statsObj, JSPROP_ENUMERATE);
84 : }
85 :
86 : bool
87 0 : WebrtcTelemetry::GetWebrtcStats(JSContext *cx, JS::MutableHandle<JS::Value> ret)
88 : {
89 0 : JS::Rooted<JSObject*> root_obj(cx, JS_NewPlainObject(cx));
90 0 : if (!root_obj)
91 0 : return false;
92 0 : ret.setObject(*root_obj);
93 :
94 0 : JS::Rooted<JSObject*> ice_obj(cx, JS_NewPlainObject(cx));
95 0 : if (!ice_obj)
96 0 : return false;
97 0 : JS_DefineProperty(cx, root_obj, "IceCandidatesStats", ice_obj,
98 0 : JSPROP_ENUMERATE);
99 :
100 0 : if (!AddIceInfo(cx, ice_obj))
101 0 : return false;
102 :
103 0 : return true;
104 : }
105 :
106 : size_t
107 0 : WebrtcTelemetry::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
108 : {
109 0 : return mWebrtcIceCandidates.ShallowSizeOfExcludingThis(aMallocSizeOf);
110 : }
|