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/glean/bindings/Uuid.h"
8 
9 #include "Common.h"
10 #include "jsapi.h"
11 #include "mozilla/Components.h"
12 #include "mozilla/ResultVariant.h"
13 #include "mozilla/glean/bindings/ScalarGIFFTMap.h"
14 #include "mozilla/glean/fog_ffi_generated.h"
15 #include "nsIClassInfoImpl.h"
16 #include "nsString.h"
17 
18 namespace mozilla::glean {
19 
20 namespace impl {
21 
Set(const nsACString & aValue) const22 void UuidMetric::Set(const nsACString& aValue) const {
23   auto scalarId = ScalarIdForMetric(mId);
24   if (scalarId) {
25     Telemetry::ScalarSet(scalarId.extract(), NS_ConvertUTF8toUTF16(aValue));
26   }
27   fog_uuid_set(mId, &aValue);
28 }
29 
GenerateAndSet() const30 void UuidMetric::GenerateAndSet() const {
31   // We don't have the generated value to mirror to the scalar,
32   // so calling this function on a mirrored metric is likely an error.
33   (void)NS_WARN_IF(ScalarIdForMetric(mId).isSome());
34   fog_uuid_generate_and_set(mId);
35 }
36 
TestGetValue(const nsACString & aPingName) const37 Result<Maybe<nsCString>, nsCString> UuidMetric::TestGetValue(
38     const nsACString& aPingName) const {
39   nsCString err;
40   if (fog_uuid_test_get_error(mId, &aPingName, &err)) {
41     return Err(err);
42   }
43   if (!fog_uuid_test_has_value(mId, &aPingName)) {
44     return Maybe<nsCString>();
45   }
46   nsCString ret;
47   fog_uuid_test_get_value(mId, &aPingName, &ret);
48   return Some(ret);
49 }
50 
51 }  // namespace impl
52 
53 NS_IMPL_CLASSINFO(GleanUuid, nullptr, 0, {0})
NS_IMPL_ISUPPORTS_CI(GleanUuid,nsIGleanUuid)54 NS_IMPL_ISUPPORTS_CI(GleanUuid, nsIGleanUuid)
55 
56 NS_IMETHODIMP
57 GleanUuid::Set(const nsACString& aValue) {
58   mUuid.Set(aValue);
59   return NS_OK;
60 }
61 
62 NS_IMETHODIMP
GenerateAndSet()63 GleanUuid::GenerateAndSet() {
64   mUuid.GenerateAndSet();
65   return NS_OK;
66 }
67 
68 NS_IMETHODIMP
TestGetValue(const nsACString & aStorageName,JSContext * aCx,JS::MutableHandleValue aResult)69 GleanUuid::TestGetValue(const nsACString& aStorageName, JSContext* aCx,
70                         JS::MutableHandleValue aResult) {
71   auto result = mUuid.TestGetValue(aStorageName);
72   if (result.isErr()) {
73     aResult.set(JS::UndefinedValue());
74     LogToBrowserConsole(nsIScriptError::errorFlag,
75                         NS_ConvertUTF8toUTF16(result.unwrapErr()));
76     return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
77   }
78   auto optresult = result.unwrap();
79   if (optresult.isNothing()) {
80     aResult.set(JS::UndefinedValue());
81   } else {
82     const NS_ConvertUTF8toUTF16 str(optresult.value());
83     aResult.set(
84         JS::StringValue(JS_NewUCStringCopyN(aCx, str.Data(), str.Length())));
85   }
86   return NS_OK;
87 }
88 
89 }  // namespace mozilla::glean
90