diff --git a/browser/brave_browser_main_extra_parts.cc b/browser/brave_browser_main_extra_parts.cc index 5edea9036b6..c0c0135db76 100644 --- a/browser/brave_browser_main_extra_parts.cc +++ b/browser/brave_browser_main_extra_parts.cc @@ -51,8 +51,9 @@ void RecordInitialP3AValues() { g_browser_process->local_state()); // Record crash reporting status stats. - const bool crash_reports_enabled = g_browser_process->local_state()-> - GetBoolean(metrics::prefs::kMetricsReportingEnabled); + const bool crash_reports_enabled = + g_browser_process->local_state()->GetBoolean( + metrics::prefs::kMetricsReportingEnabled); UMA_HISTOGRAM_BOOLEAN("Brave.Core.CrashReportsEnabled", crash_reports_enabled); } @@ -105,3 +106,14 @@ void BraveBrowserMainExtraParts::PreMainMessageLoopRun() { #endif // !BUILDFLAG(IS_ANDROID) g_brave_browser_process->process_misc_metrics()->uptime_monitor()->Init(); } + +void BraveBrowserMainExtraParts::PostDestroyThreads() { +#if !BUILDFLAG(IS_ANDROID) + // Based on chrome/browser/metrics/chrome_browser_main_extra_parts_metrics.cc + // The instance needs to delete itself as it uses BrowserCollection observer + // and the collection will go out of scope with the browser process. + if (brave::BraveWindowTracker::HasInstance()) { + brave::BraveWindowTracker::ClearInstance(); + } +#endif // !BUILDFLAG(IS_ANDROID) +} diff --git a/browser/brave_browser_main_extra_parts.h b/browser/brave_browser_main_extra_parts.h index cf3664e3706..b4555c64f11 100644 --- a/browser/brave_browser_main_extra_parts.h +++ b/browser/brave_browser_main_extra_parts.h @@ -22,6 +22,7 @@ class BraveBrowserMainExtraParts : public ChromeBrowserMainExtraParts { void PostBrowserStart() override; void PreMainMessageLoopRun() override; void PreProfileInit() override; + void PostDestroyThreads() override; }; #endif // BRAVE_BROWSER_BRAVE_BROWSER_MAIN_EXTRA_PARTS_H_ diff --git a/browser/p3a/p3a_core_metrics.cc b/browser/p3a/p3a_core_metrics.cc index 5fc6c458318..3cdf6761ac9 100644 --- a/browser/p3a/p3a_core_metrics.cc +++ b/browser/p3a/p3a_core_metrics.cc @@ -1,7 +1,7 @@ -/* Copyright 2019 The Brave Authors. All rights reserved. +/* 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 http://mozilla.org/MPL/2.0/. */ + * You can obtain one at https://mozilla.org/MPL/2.0/. */ #include "brave/browser/p3a/p3a_core_metrics.h" @@ -42,8 +42,7 @@ enum class WindowUsageStats { }; const char* GetPrefNameForProfile(Profile* profile) { - if (profile->IsIncognitoProfile() && - !profile->IsTor()) { + if (profile->IsIncognitoProfile() && !profile->IsTor()) { return kLastTimeIncognitoUsed; } return nullptr; @@ -71,6 +70,16 @@ void BraveWindowTracker::CreateInstance(PrefService* local_state) { g_brave_windows_tracker_instance = new BraveWindowTracker(local_state); } +bool BraveWindowTracker::HasInstance() { + return g_brave_windows_tracker_instance != nullptr; +} + +void BraveWindowTracker::ClearInstance() { + CHECK(g_brave_windows_tracker_instance); + delete g_brave_windows_tracker_instance; + g_brave_windows_tracker_instance = nullptr; +} + void BraveWindowTracker::RegisterPrefs(PrefRegistrySimple* registry) { registry->RegisterTimePref(kLastTimeIncognitoUsed, {}); registry->RegisterBooleanPref(kTorUsed, false); diff --git a/browser/p3a/p3a_core_metrics.h b/browser/p3a/p3a_core_metrics.h index 4be14bc2e7f..ae8973fd408 100644 --- a/browser/p3a/p3a_core_metrics.h +++ b/browser/p3a/p3a_core_metrics.h @@ -1,7 +1,7 @@ -/* Copyright 2019 The Brave Authors. All rights reserved. +/* 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 http://mozilla.org/MPL/2.0/. */ + * 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_ @@ -46,6 +46,8 @@ class BraveWindowTracker : public BrowserCollectionObserver { ~BraveWindowTracker() override; static void CreateInstance(PrefService* local_state); + static bool HasInstance(); + static void ClearInstance(); static void RegisterPrefs(PrefRegistrySimple* registry);