Files
brave-core/browser/p3a/p3a_core_metrics.h
T
Max Karolinskiy d99ea245ce [cr149] Fixes BraveWindowTracker dangling pointer.
BraveWindowTracker is initialized in
BraveBrowserMainExtraParts::PreMainMessageLoopRun. It uses
base::ScopedObservation<BrowserCollection, BrowserCollectionObserver>,
which obseves browser collection which goes out of scope with
GlobalFeatures. BraveWindowTracker needs to stop observing before then.

Added HasInstance/ClearInstance to BraveWindowTracker modeling this
behavior on upstream's ChromeBrowserMainExtraPartsMetrics and
metrics::TabStatsTracker.

[33240:46492:0417/173936.000:ERROR:base\allocator\partition_alloc_support.cc:683] A freed allocation is still referenced by a dangling pointer at exit, or at test end. Leaked raw_ptr/raw_ref could cause PartitionAlloc's quarantine memory bloat.

Memory was released on:
        brave_browser_tests!base::allocator::`anonymous namespace'::DanglingRawPtrDetected [0x7ff7130f09ee+3ee] (base\allocator\partition_alloc_support.cc:438)
        brave_browser_tests!GlobalFeatures::~GlobalFeatures [0x7ff711aec13d+cd] (chrome\browser\global_features.cc:96)
        brave_browser_tests!BraveGlobalFeatures::~BraveGlobalFeatures [0x7ff710d19780+10] (brave\browser\brave_global_features.cc:12)
        brave_browser_tests!BrowserProcessImpl::~BrowserProcessImpl [0x7ff711b8ccb8+138] (chrome\browser\browser_process_impl.cc:565)
        brave_browser_tests!BraveBrowserProcessImpl::~BraveBrowserProcessImpl [0x7ff711b46128+3c8] (brave\browser\brave_browser_process_impl.cc:142)
        brave_browser_tests!BraveBrowserProcessImpl::~BraveBrowserProcessImpl [0x7ff711b47fa0+10] (brave\browser\brave_browser_process_impl.cc:142)
        brave_browser_tests!ChromeBrowserMainParts_ChromiumImpl::PostDestroyThreads [0x7ff711b52658+168] (chrome\browser\chrome_browser_main.cc:2291)
        brave_browser_tests!content::BrowserMainLoop::ShutdownThreadsAndCleanUp [0x7ff70d2638c7+7b7] (content\browser\browser_main_loop.cc:1282)
        brave_browser_tests!content::BrowserMainRunnerImpl::Shutdown [0x7ff70d265b86+106] (content\browser\browser_main_runner_impl.cc:177)

ERROR:base\allocator\partition_alloc_support.cc:700] Dangling reference from:
ERROR:base\allocator\partition_alloc_support.cc:701]      brave_browser_tests!partition_alloc::internal::base::debug::CollectStackTrace [0x7ff713271b82+12] (base\allocator\partition_allocator\src\partition_alloc\partition_alloc_base\debug\stack_trace_win.cc:103)
        brave_browser_tests!base::internal::InstanceTracer::TraceImpl [0x7ff7132712ee+1fe] (base\allocator\partition_allocator\src\partition_alloc\pointers\instance_tracer.cc:66)
        brave_browser_tests!base::ScopedObservation<brave_rewards::RewardsService,brave_rewards::RewardsServiceObserver,0>::Observe [0x7ff707af12d8+128] (base\scoped_observation.h:117)
        brave_browser_tests!brave::BraveWindowTracker::BraveWindowTracker [0x7ff7119c682d+11d] (brave\browser\p3a\p3a_core_metrics.cc:60)
        brave_browser_tests!brave::BraveWindowTracker::CreateInstance [0x7ff7119c6de1+21] (brave\browser\p3a\p3a_core_metrics.cc:71)
        brave_browser_tests!BraveBrowserMainExtraParts::PreMainMessageLoopRun [0x7ff711a00915+185] (brave\browser\brave_browser_main_extra_parts.cc:104)
        brave_browser_tests!ChromeBrowserMainParts_ChromiumImpl::PreMainMessageLoopRun [0x7ff711b4fd60+60] (chrome\browser\chrome_browser_main.cc:1465)
2026-05-22 16:25:01 -04:00

71 lines
2.3 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_BROWSER_P3A_P3A_CORE_METRICS_H_
#define BRAVE_BROWSER_P3A_P3A_CORE_METRICS_H_
// The class below can be used on desktop only
// because BrowserCollectionObserver is available on desktop only
// Brave.Core.LastTimeIncognitoUsed and
// Brave.Core.TorEverUsed don't work on Android
#include "build/build_config.h"
#if BUILDFLAG(IS_ANDROID)
#error This file should only be included on desktop.
#endif
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "base/timer/timer.h"
#include "chrome/browser/ui/browser_window/public/browser_collection_observer.h"
class BrowserWindowInterface;
class BrowserCollection;
class PrefService;
class PrefRegistrySimple;
namespace brave {
// BraveWindowTracker is under !OS_ANDROID guard because
// BrowserCollectionObserver should only be only on desktop
// Brave.Uptime.BrowserOpenMinutes and Brave.Core.LastTimeIncognitoUsed
// don't work on Android
#if !BUILDFLAG(IS_ANDROID)
// Periodically records P3A stats (extracted from Local State) regarding the
// time when incognito windows were used.
// Used as a leaking singletone.
class BraveWindowTracker : public BrowserCollectionObserver {
public:
explicit BraveWindowTracker(PrefService* local_state);
BraveWindowTracker(const BraveWindowTracker&) = delete;
BraveWindowTracker& operator=(const BraveWindowTracker&) = delete;
~BraveWindowTracker() override;
static void CreateInstance(PrefService* local_state);
static bool HasInstance();
static void ClearInstance();
static void RegisterPrefs(PrefRegistrySimple* registry);
private:
// BrowserCollectionObserver:
void OnBrowserCreated(BrowserWindowInterface* browser) override;
void OnBrowserActivated(BrowserWindowInterface* browser) override;
void UpdateP3AValues() const;
base::RepeatingTimer timer_;
raw_ptr<PrefService, DanglingUntriaged> local_state_ = nullptr;
base::ScopedObservation<BrowserCollection, BrowserCollectionObserver>
browser_collection_observation_{this};
};
#endif // !BUILDFLAG(IS_ANDROID)
} // namespace brave
#endif // BRAVE_BROWSER_P3A_P3A_CORE_METRICS_H_