1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/tracing/chrome_tracing_delegate.h"
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/json/values_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "base/trace_event/named_trigger.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_otr_state.h"
#include "chrome/common/pref_names.h"
#include "components/metrics/metrics_pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/tracing/common/background_tracing_metrics_provider.h"
#include "components/tracing/common/background_tracing_state_manager.h"
#include "components/tracing/common/background_tracing_utils.h"
#include "components/tracing/common/system_profile_metadata_recorder.h"
#include "components/tracing/common/tracing_scenarios_config.h"
#include "components/variations/active_field_trials.h"
#include "components/version_info/version_info.h"
#include "content/public/browser/browser_thread.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/tracing/public/cpp/tracing_features.h"
#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ui/android/tab_model/tab_model.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#else
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#endif
#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/ash_pref_names.h"
#include "chromeos/dbus/constants/dbus_switches.h" // nogncheck
#endif
#if BUILDFLAG(IS_WIN)
#include "base/task/thread_pool.h"
#include "chrome/installer/util/system_tracing_util.h"
#endif
namespace {
using tracing::BackgroundTracingStateManager;
} // namespace
ChromeTracingDelegate::ChromeTracingDelegate() {
// Ensure that this code is called on the UI thread, except for
// tests where a UI thread might not have been initialized at this point.
DCHECK(
content::BrowserThread::CurrentlyOn(content::BrowserThread::UI) ||
!content::BrowserThread::IsThreadInitialized(content::BrowserThread::UI));
#if !BUILDFLAG(IS_ANDROID)
BrowserList::AddObserver(this);
#else
TabModelList::AddObserver(this);
#endif
}
ChromeTracingDelegate::~ChromeTracingDelegate() {
CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
#if !BUILDFLAG(IS_ANDROID)
BrowserList::RemoveObserver(this);
#else
TabModelList::RemoveObserver(this);
#endif
}
#if BUILDFLAG(IS_ANDROID)
void ChromeTracingDelegate::OnTabModelAdded(TabModel* tab_model) {
for (const TabModel* model : TabModelList::models()) {
if (model->GetProfile()->IsOffTheRecord()) {
latest_incognito_launched_ = base::TimeTicks::Now();
base::trace_event::EmitNamedTrigger("incognito-start");
}
}
}
void ChromeTracingDelegate::OnTabModelRemoved(TabModel* tab_model) {
if (!IsOffTheRecordSessionActive()) {
base::trace_event::EmitNamedTrigger("incognito-end");
}
}
#else
void ChromeTracingDelegate::OnBrowserAdded(Browser* browser) {
if (browser->profile()->IsOffTheRecord()) {
latest_incognito_launched_ = base::TimeTicks::Now();
base::trace_event::EmitNamedTrigger("incognito-start");
}
}
void ChromeTracingDelegate::OnBrowserRemoved(Browser* browser) {
if (!IsOffTheRecordSessionActive()) {
base::trace_event::EmitNamedTrigger("incognito-end");
}
}
#endif // BUILDFLAG(IS_ANDROID)
bool ChromeTracingDelegate::IsRecordingAllowed(
bool requires_anonymized_data,
base::TimeTicks session_start) const {
// If the background tracing is specified on the command-line, we allow
// any scenario to be traced and uploaded.
if (!requires_anonymized_data) {
return true;
}
if (IsOffTheRecordSessionActive() ||
session_start <= latest_incognito_launched_) {
UMA_HISTOGRAM_ENUMERATION(
"Tracing.Background.FinalizationDisallowedReason",
TracingFinalizationDisallowedReason::kIncognitoLaunched);
return false;
}
return true;
}
bool ChromeTracingDelegate::ShouldSaveUnuploadedTrace() const {
return true;
}
std::unique_ptr<tracing::BackgroundTracingStateManager>
ChromeTracingDelegate::CreateStateManager() {
return tracing::BackgroundTracingStateManager::CreateInstance(
g_browser_process->local_state());
}
std::string ChromeTracingDelegate::RecordSerializedSystemProfileMetrics()
const {
metrics::SystemProfileProto system_profile_proto;
auto recorder = tracing::BackgroundTracingMetricsProvider::
GetSystemProfileMetricsRecorder();
if (!recorder) {
return std::string();
}
recorder.Run(system_profile_proto);
std::string serialized_system_profile;
system_profile_proto.SerializeToString(&serialized_system_profile);
return serialized_system_profile;
}
tracing::MetadataDataSource::BundleRecorder
ChromeTracingDelegate::CreateSystemProfileMetadataRecorder() const {
return base::BindRepeating(&tracing::RecordSystemProfileMetadata);
}
#if BUILDFLAG(IS_WIN)
void ChromeTracingDelegate::GetSystemTracingState(
base::OnceCallback<void(bool service_supported, bool service_enabled)>
on_tracing_state) {
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock{}},
base::BindOnce([]() -> std::pair<bool, bool> {
return {installer::IsSystemTracingServiceSupported(),
installer::IsSystemTracingServiceRegistered()};
}),
base::BindOnce(
[](base::OnceCallback<void(bool service_supported,
bool service_enabled)> on_tracing_state,
std::pair<bool, bool> state) {
std::move(on_tracing_state).Run(state.first, state.second);
},
std::move(on_tracing_state)));
}
void ChromeTracingDelegate::EnableSystemTracing(
base::OnceCallback<void(bool success)> on_complete) {
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock{}}, base::BindOnce([]() {
return installer::ElevateAndRegisterSystemTracingService();
}),
std::move(on_complete));
}
void ChromeTracingDelegate::DisableSystemTracing(
base::OnceCallback<void(bool success)> on_complete) {
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock{}}, base::BindOnce([]() {
return installer::ElevateAndDeregisterSystemTracingService();
}),
std::move(on_complete));
}
#endif // BUILDFLAG(IS_WIN)
bool ChromeTracingDelegate::IsSystemWideTracingEnabled() {
#if BUILDFLAG(IS_CHROMEOS)
// Always allow system tracing in dev mode images.
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
chromeos::switches::kSystemDevMode)) {
return true;
}
// In non-dev images, honor the pref for system-wide tracing.
PrefService* local_state = g_browser_process->local_state();
DCHECK(local_state);
return local_state->GetBoolean(ash::prefs::kDeviceSystemWideTracingEnabled);
#else
return false;
#endif
}
|