Files
brave-core/components/p3a/histograms_braveizer.h
Claudio DeSouza c4d376cf62 [cr135] ScopedHistogramSampleObserver using string_view
This change affects the ctor args and callback arg list.

Chromium change:
https://chromium.googlesource.com/chromium/src/+/8952859c39099ebf8ca13ae6b1901af7a482249c

commit 8952859c39099ebf8ca13ae6b1901af7a482249c
Author: Roger McFarlane <rogerm@chromium.org>
Date:   Thu Feb 20 12:50:27 2025 -0800

    Use bounded ranges (string_view) for histogram names.

    This CL changes the histogram creation APIs to take a durable
    string view (i.e., a pointer+size pair referring to memory that
    the programmer has annotated that it will not be freed) instead
    of just an implicit pointer. The HistogramBase class internally
    represents the name's length and the histogram's flags using
    16 bits in order to not grow the in-memory size of Histogram
    objects. The name of the histogram is subsequently exposed by
    HistogramBase as a string_view.

    This CL updates consumers of the histogram name's to use a
    string_view of the name. This removes many string length
    calculations and string copy operations. For histograms
    allocated in shared memory, it also avoids potential out-of-
    bounds reads if the underlying string data is modified or
    corrupted such that it no longer has a trailing NUL char
    at the end of the string.

    Lastly, this CL updates a number of call-sites where the
    histogram names were being copied into short-lived string
    objects for use as search keys into various containers to
    perform the functionality without making any unnecessary
    copies.

    Low-Coverage-Reason: TRIVIAL_CHANGE Use of string_view instead of string/char* in some error/logging paths have low coverage.
    AX-Relnotes: n/a
    Bug: 393394360, 40818143
2025-03-18 20:02:13 -04:00

48 lines
1.5 KiB
C++

/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* 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 https://mozilla.org/MPL/2.0/. */
#ifndef BRAVE_COMPONENTS_P3A_HISTOGRAMS_BRAVEIZER_H_
#define BRAVE_COMPONENTS_P3A_HISTOGRAMS_BRAVEIZER_H_
#include <memory>
#include <vector>
#include "base/memory/ref_counted.h"
#include "base/metrics/histogram_base.h"
#include "base/metrics/statistics_recorder.h"
namespace p3a {
class HistogramsBraveizer
: public base::RefCountedThreadSafe<HistogramsBraveizer> {
public:
static scoped_refptr<p3a::HistogramsBraveizer> Create();
HistogramsBraveizer();
HistogramsBraveizer(const HistogramsBraveizer&) = delete;
HistogramsBraveizer& operator=(const HistogramsBraveizer&) = delete;
private:
friend class base::RefCountedThreadSafe<HistogramsBraveizer>;
~HistogramsBraveizer();
// Set callbacks for existing Chromium histograms that will be bravetized,
// i.e. reemitted using a different name and custom buckets.
void InitCallbacks();
void DoHistogramBravetization(std::string_view histogram_name,
uint64_t name_hash,
base::HistogramBase::Sample32 sample);
std::vector<
std::unique_ptr<base::StatisticsRecorder::ScopedHistogramSampleObserver>>
histogram_sample_callbacks_;
};
} // namespace p3a
#endif // BRAVE_COMPONENTS_P3A_HISTOGRAMS_BRAVEIZER_H_