fune/toolkit/components/glean/bindings/Category.cpp
Nika Layzell c15823d075 Bug 1772006 - Part 5: Simplify and move the string searching APIs from ns[T]StringObsolete, r=xpcom-reviewers,necko-reviewers,eeejay,dragana,barret
The biggest set of APIs from ns[T]StringObsolete which are still heavily used
are the string searching APIs. It appears the intention was for these to be
replaced by the `FindInReadable` APIs, however that doesn't appear to have
happened.

In addition, the APIs have some quirks around their handling of mixed character
widths. These APIs generally supported both narrow strings and the native
string type, probably because char16_t string literals weren't available until
c++11. Finally they also used easy-to-confuse unlabeled boolean and integer
optional arguments to control behaviour.

These patches do the following major changes to the searching APIs:

1. The ASCII case-insensitive search method was split out as
   LowerCaseFindASCII, rather than using a boolean. This should be less
   error-prone and more explicit, and allows the method to continue to use
   narrow string literals for all string types (as only ASCII is supported).
2. The other [R]Find methods were restricted to only support arguments with
   matching character types. I considered adding a FindASCII method which would
   use narrow string literals for both wide and narrow strings but it would've
   been the same amount of work as changing all of the literals to unicode
   literals.
   This ends up being the bulk of the changes in the patch.
3. All find methods were re-implemented using std::basic_string_view's find
   algorithm or stl algorithms to reduce code complexity, and avoid the need to
   carry around the logic from nsStringObsolete.cpp.
4. The implementations were moved to nsTStringRepr.cpp.
5. An overload of Find was added to try to catch callers which previously
   called `Find(..., false)` or `Find(..., true)` to set case-sensitivity, due
   to booleans normally implicitly coercing to `index_type`. This should
   probably be removed at some point, but may be useful during the transition.

Differential Revision: https://phabricator.services.mozilla.com/D148300
2022-07-30 00:12:48 +00:00

75 lines
2.6 KiB
C++

/* -*- 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 "mozilla/dom/GleanBinding.h"
#include "mozilla/glean/bindings/Glean.h"
#include "mozilla/glean/bindings/Category.h"
#include "mozilla/glean/bindings/GleanJSMetricsLookup.h"
#include "mozilla/glean/bindings/jog/JOG.h"
namespace mozilla::glean {
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(Category)
NS_IMPL_CYCLE_COLLECTING_ADDREF(Category)
NS_IMPL_CYCLE_COLLECTING_RELEASE(Category)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Category)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
JSObject* Category::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return dom::GleanCategory_Binding::Wrap(aCx, this, aGivenProto);
}
already_AddRefed<nsISupports> Category::NamedGetter(const nsAString& aName,
bool& aFound) {
aFound = false;
nsCString metricName;
metricName.AppendASCII(mName);
metricName.AppendLiteral(".");
AppendUTF16toUTF8(aName, metricName);
Maybe<uint32_t> metricIdx = JOG::GetMetric(metricName);
if (metricIdx.isNothing() && !JOG::AreRuntimeMetricsComprehensive()) {
metricIdx = MetricByNameLookup(metricName);
}
if (metricIdx.isNothing()) {
aFound = false;
return nullptr;
}
aFound = true;
return NewMetricFromId(metricIdx.value());
}
bool Category::NameIsEnumerable(const nsAString& aName) { return false; }
void Category::GetSupportedNames(nsTArray<nsString>& aNames) {
// We don't get dynamic metric names because we don't want to store them.
if (!JOG::AreRuntimeMetricsComprehensive()) {
for (metric_entry_t entry : sMetricByNameLookupEntries) {
const char* identifierBuf = GetMetricIdentifier(entry);
nsDependentCString identifier(identifierBuf);
// We're iterating all metrics,
// so we need to check for the ones in the right category.
//
// We need to ensure that we found _only_ the exact category by checking
// it is followed by a dot.
if (StringBeginsWith(identifier, mName) &&
identifier.CharAt(mName.Length()) == '.') {
const char* metricName = &identifierBuf[mName.Length() + 1];
aNames.AppendElement()->AssignASCII(metricName);
}
}
}
}
} // namespace mozilla::glean