Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/tracing/tracing_ui.h" |
| 6 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 9 | #include <memory> |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 10 | #include <optional> |
[email protected] | b12387e6 | 2014-06-27 20:52:06 | [diff] [blame] | 11 | #include <set> |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 12 | #include <string> |
Oystein Eftevaag | 2386e05 | 2019-10-02 21:08:09 | [diff] [blame] | 13 | #include <utility> |
[email protected] | b12387e6 | 2014-06-27 20:52:06 | [diff] [blame] | 14 | #include <vector> |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 15 | |
[email protected] | 4a0a8f9 | 2013-11-21 06:10:34 | [diff] [blame] | 16 | #include "base/base64.h" |
simonhatch | 6160a4b | 2015-07-23 13:30:44 | [diff] [blame] | 17 | #include "base/command_line.h" |
[email protected] | b12387e6 | 2014-06-27 20:52:06 | [diff] [blame] | 18 | #include "base/format_macros.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 19 | #include "base/functional/bind.h" |
| 20 | #include "base/functional/callback_helpers.h" |
[email protected] | 4a0a8f9 | 2013-11-21 06:10:34 | [diff] [blame] | 21 | #include "base/json/json_reader.h" |
| 22 | #include "base/json/json_writer.h" |
Ehsan Chiniforooshan | c77b44e | 2017-10-10 18:12:10 | [diff] [blame] | 23 | #include "base/memory/ref_counted_memory.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 24 | #include "base/strings/string_number_conversions.h" |
[email protected] | b12387e6 | 2014-06-27 20:52:06 | [diff] [blame] | 25 | #include "base/strings/string_split.h" |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 26 | #include "base/strings/string_util.h" |
Etienne Pierre-doray | 764aab09 | 2025-07-14 18:59:38 | [diff] [blame] | 27 | #include "base/trace_event/trace_config.h" |
ssid | 3e76561 | 2015-01-28 04:03:42 | [diff] [blame] | 28 | #include "base/trace_event/trace_event.h" |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 29 | #include "base/values.h" |
[email protected] | 9786502 | 2014-06-13 23:18:04 | [diff] [blame] | 30 | #include "content/browser/tracing/grit/tracing_resources.h" |
[email protected] | 06d5b6e | 2014-01-14 14:55:58 | [diff] [blame] | 31 | #include "content/browser/tracing/tracing_controller_impl.h" |
[email protected] | b12387e6 | 2014-06-27 20:52:06 | [diff] [blame] | 32 | #include "content/public/browser/browser_context.h" |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 33 | #include "content/public/browser/browser_thread.h" |
oysteine | c563c58 | 2015-02-13 19:19:52 | [diff] [blame] | 34 | #include "content/public/browser/content_browser_client.h" |
jam | b84299e | 2016-04-12 16:58:59 | [diff] [blame] | 35 | #include "content/public/browser/storage_partition.h" |
[email protected] | 4a0a8f9 | 2013-11-21 06:10:34 | [diff] [blame] | 36 | #include "content/public/browser/tracing_controller.h" |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 37 | #include "content/public/browser/web_contents.h" |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 38 | #include "content/public/browser/web_ui.h" |
| 39 | #include "content/public/browser/web_ui_data_source.h" |
[email protected] | b12387e6 | 2014-06-27 20:52:06 | [diff] [blame] | 40 | #include "content/public/common/content_client.h" |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 41 | #include "content/public/common/url_constants.h" |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 42 | #include "services/tracing/public/cpp/perfetto/perfetto_config.h" |
Sami Kyostila | ab4673c | 2020-07-30 18:32:33 | [diff] [blame] | 43 | #include "services/tracing/public/cpp/perfetto/perfetto_session.h" |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 44 | #include "third_party/perfetto/include/perfetto/tracing/tracing.h" |
| 45 | #include "third_party/perfetto/protos/perfetto/common/trace_stats.gen.h" |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 46 | |
| 47 | namespace content { |
| 48 | namespace { |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 49 | constexpr char kStreamFormat[] = "stream_format"; |
| 50 | constexpr char kStreamFormatProtobuf[] = "protobuf"; |
| 51 | constexpr char kStreamFormatJSON[] = "json"; |
Sami Kyostila | ab4673c | 2020-07-30 18:32:33 | [diff] [blame] | 52 | perfetto::TracingSession* g_tracing_session = nullptr; |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 53 | |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame] | 54 | void OnGotCategories(WebUIDataSource::GotDataCallback callback, |
Lei Zhang | fe109ac6 | 2022-12-15 17:38:49 | [diff] [blame] | 55 | const std::set<std::string>& category_set) { |
| 56 | base::Value::List category_list; |
| 57 | for (const std::string& category : category_set) { |
| 58 | category_list.Append(category); |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 59 | } |
| 60 | |
Lei Zhang | fe109ac6 | 2022-12-15 17:38:49 | [diff] [blame] | 61 | auto res = base::MakeRefCounted<base::RefCountedString>(); |
danakj | 529a3eba | 2024-04-18 20:14:56 | [diff] [blame] | 62 | base::JSONWriter::Write(category_list, &res->as_string()); |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame] | 63 | std::move(callback).Run(res); |
[email protected] | 4a0a8f9 | 2013-11-21 06:10:34 | [diff] [blame] | 64 | } |
| 65 | |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame] | 66 | void OnRecordingEnabledAck(WebUIDataSource::GotDataCallback callback); |
[email protected] | b83f397 | 2014-01-08 12:16:57 | [diff] [blame] | 67 | |
[email protected] | 5186ccb | 2014-01-16 16:12:56 | [diff] [blame] | 68 | bool BeginRecording(const std::string& data64, |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame] | 69 | WebUIDataSource::GotDataCallback callback) { |
zhenw | d601ddc5 | 2015-06-02 21:46:34 | [diff] [blame] | 70 | base::trace_event::TraceConfig trace_config("", ""); |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 71 | std::string stream_format; |
| 72 | if (!TracingUI::GetTracingOptions(data64, trace_config, stream_format)) |
[email protected] | b83f397 | 2014-01-08 12:16:57 | [diff] [blame] | 73 | return false; |
[email protected] | 4a0a8f9 | 2013-11-21 06:10:34 | [diff] [blame] | 74 | |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 75 | // TODO(skyostil): Migrate all use cases from TracingController to Perfetto. |
| 76 | if (stream_format == kStreamFormatProtobuf) { |
Lei Zhang | fe109ac6 | 2022-12-15 17:38:49 | [diff] [blame] | 77 | delete g_tracing_session; |
Lukasz Gniadzik | 34d355d | 2021-03-15 10:38:59 | [diff] [blame] | 78 | g_tracing_session = |
| 79 | perfetto::Tracing::NewTrace(perfetto::BackendType::kCustomBackend) |
| 80 | .release(); |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 81 | g_tracing_session->Setup(tracing::GetDefaultPerfettoConfig(trace_config)); |
Sami Kyostila | f88c15920 | 2020-07-09 17:00:56 | [diff] [blame] | 82 | |
Sami Kyostila | b8f89f6 | 2020-07-31 10:06:31 | [diff] [blame] | 83 | auto shared_callback = base::MakeRefCounted< |
| 84 | base::RefCountedData<WebUIDataSource::GotDataCallback>>( |
| 85 | std::move(callback)); |
Sami Kyostila | f88c15920 | 2020-07-09 17:00:56 | [diff] [blame] | 86 | g_tracing_session->SetOnStartCallback([shared_callback] { |
Sami Kyostila | b8f89f6 | 2020-07-31 10:06:31 | [diff] [blame] | 87 | OnRecordingEnabledAck(std::move(shared_callback->data)); |
Sami Kyostila | f88c15920 | 2020-07-09 17:00:56 | [diff] [blame] | 88 | }); |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 89 | g_tracing_session->Start(); |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 90 | return true; |
| 91 | } |
zhenw | 873bdff | 2015-11-11 22:16:55 | [diff] [blame] | 92 | return TracingController::GetInstance()->StartTracing( |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame] | 93 | trace_config, |
| 94 | base::BindOnce(&OnRecordingEnabledAck, std::move(callback))); |
[email protected] | 4a0a8f9 | 2013-11-21 06:10:34 | [diff] [blame] | 95 | } |
| 96 | |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame] | 97 | void OnTraceBufferUsageResult(WebUIDataSource::GotDataCallback callback, |
yurys | d57ba6fe6 | 2014-11-17 10:57:56 | [diff] [blame] | 98 | float percent_full, |
| 99 | size_t approximate_event_count) { |
Brett Wilson | 5ed06e7 | 2017-12-01 01:25:11 | [diff] [blame] | 100 | std::string str = base::NumberToString(percent_full); |
Jongmok Kim | c549108 | 2022-10-19 09:17:59 | [diff] [blame] | 101 | std::move(callback).Run( |
| 102 | base::MakeRefCounted<base::RefCountedString>(std::move(str))); |
[email protected] | 4a0a8f9 | 2013-11-21 06:10:34 | [diff] [blame] | 103 | } |
| 104 | |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 105 | bool GetTraceBufferUsage(WebUIDataSource::GotDataCallback callback) { |
| 106 | if (g_tracing_session) { |
| 107 | // |callback| is move-only, so in order to pass it through a copied lambda |
| 108 | // we need to temporarily move it on the heap. |
Sami Kyostila | b8f89f6 | 2020-07-31 10:06:31 | [diff] [blame] | 109 | auto shared_callback = base::MakeRefCounted< |
| 110 | base::RefCountedData<WebUIDataSource::GotDataCallback>>( |
| 111 | std::move(callback)); |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 112 | g_tracing_session->GetTraceStats( |
| 113 | [shared_callback]( |
| 114 | perfetto::TracingSession::GetTraceStatsCallbackArgs args) { |
| 115 | std::string usage; |
| 116 | perfetto::protos::gen::TraceStats trace_stats; |
| 117 | if (args.success && |
| 118 | trace_stats.ParseFromArray(args.trace_stats_data.data(), |
Sami Kyostila | ab4673c | 2020-07-30 18:32:33 | [diff] [blame] | 119 | args.trace_stats_data.size())) { |
| 120 | double percent_full = tracing::GetTraceBufferUsage(trace_stats); |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 121 | usage = base::NumberToString(percent_full); |
| 122 | } |
Sami Kyostila | b8f89f6 | 2020-07-31 10:06:31 | [diff] [blame] | 123 | std::move(shared_callback->data) |
Jongmok Kim | c549108 | 2022-10-19 09:17:59 | [diff] [blame] | 124 | .Run(base::MakeRefCounted<base::RefCountedString>( |
| 125 | std::move(usage))); |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 126 | }); |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | return TracingController::GetInstance()->GetTraceBufferUsage( |
| 131 | base::BindOnce(OnTraceBufferUsageResult, std::move(callback))); |
| 132 | } |
| 133 | |
| 134 | void ReadProtobufTraceData( |
| 135 | scoped_refptr<TracingController::TraceDataEndpoint> endpoint, |
| 136 | perfetto::TracingSession::ReadTraceCallbackArgs args) { |
| 137 | if (args.size) { |
| 138 | auto data_string = std::make_unique<std::string>(args.data, args.size); |
| 139 | endpoint->ReceiveTraceChunk(std::move(data_string)); |
| 140 | } |
| 141 | if (!args.has_more) |
| 142 | endpoint->ReceivedTraceFinalContents(); |
| 143 | } |
| 144 | |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame] | 145 | void TracingCallbackWrapperBase64(WebUIDataSource::GotDataCallback callback, |
| 146 | std::unique_ptr<std::string> data) { |
simonhatch | a13b0a5 | 2015-06-23 20:23:08 | [diff] [blame] | 147 | base::RefCountedString* data_base64 = new base::RefCountedString(); |
danakj | 529a3eba | 2024-04-18 20:14:56 | [diff] [blame] | 148 | data_base64->as_string() = base::Base64Encode(*data); |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame] | 149 | std::move(callback).Run(data_base64); |
simonhatch | a13b0a5 | 2015-06-23 20:23:08 | [diff] [blame] | 150 | } |
| 151 | |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 152 | bool EndRecording(WebUIDataSource::GotDataCallback callback) { |
| 153 | if (!TracingController::GetInstance()->IsTracing() && !g_tracing_session) |
| 154 | return false; |
| 155 | |
| 156 | scoped_refptr<TracingController::TraceDataEndpoint> data_endpoint = |
| 157 | TracingControllerImpl::CreateCompressedStringEndpoint( |
| 158 | TracingControllerImpl::CreateCallbackEndpoint(base::BindOnce( |
| 159 | TracingCallbackWrapperBase64, std::move(callback))), |
| 160 | false /* compress_with_background_priority */); |
| 161 | |
| 162 | if (g_tracing_session) { |
| 163 | perfetto::TracingSession* session = g_tracing_session; |
| 164 | g_tracing_session = nullptr; |
| 165 | session->SetOnStopCallback([session, data_endpoint] { |
| 166 | session->ReadTrace( |
| 167 | [session, data_endpoint]( |
| 168 | perfetto::TracingSession::ReadTraceCallbackArgs args) { |
| 169 | ReadProtobufTraceData(data_endpoint, args); |
| 170 | if (!args.has_more) |
| 171 | delete session; |
| 172 | }); |
| 173 | }); |
| 174 | session->Stop(); |
| 175 | return true; |
| 176 | } |
| 177 | return TracingController::GetInstance()->StopTracing(data_endpoint); |
| 178 | } |
| 179 | |
| 180 | void OnRecordingEnabledAck(WebUIDataSource::GotDataCallback callback) { |
| 181 | std::move(callback).Run(base::MakeRefCounted<base::RefCountedString>()); |
| 182 | } |
| 183 | |
[email protected] | e4f45cd4 | 2014-01-06 20:55:06 | [diff] [blame] | 184 | bool OnBeginJSONRequest(const std::string& path, |
danakj | 88bb36b | 2020-01-09 15:35:00 | [diff] [blame] | 185 | WebUIDataSource::GotDataCallback callback) { |
[email protected] | 4a0a8f9 | 2013-11-21 06:10:34 | [diff] [blame] | 186 | if (path == "json/categories") { |
[email protected] | e4f45cd4 | 2014-01-06 20:55:06 | [diff] [blame] | 187 | return TracingController::GetInstance()->GetCategories( |
danakj | 88bb36b | 2020-01-09 15:35:00 | [diff] [blame] | 188 | base::BindOnce(OnGotCategories, std::move(callback))); |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 189 | } |
[email protected] | b83f397 | 2014-01-08 12:16:57 | [diff] [blame] | 190 | |
thestig | 418c505 | 2016-04-11 19:39:19 | [diff] [blame] | 191 | const char kBeginRecordingPath[] = "json/begin_recording?"; |
| 192 | if (base::StartsWith(path, kBeginRecordingPath, |
brettw | 9550931 | 2015-07-16 23:57:33 | [diff] [blame] | 193 | base::CompareCase::SENSITIVE)) { |
thestig | 418c505 | 2016-04-11 19:39:19 | [diff] [blame] | 194 | std::string data = path.substr(strlen(kBeginRecordingPath)); |
danakj | 88bb36b | 2020-01-09 15:35:00 | [diff] [blame] | 195 | return BeginRecording(data, std::move(callback)); |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 196 | } |
[email protected] | 4a0a8f9 | 2013-11-21 06:10:34 | [diff] [blame] | 197 | if (path == "json/get_buffer_percent_full") { |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 198 | return GetTraceBufferUsage(std::move(callback)); |
yurys | d57ba6fe6 | 2014-11-17 10:57:56 | [diff] [blame] | 199 | } |
simonhatch | a13b0a5 | 2015-06-23 20:23:08 | [diff] [blame] | 200 | if (path == "json/end_recording_compressed") { |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 201 | return EndRecording(std::move(callback)); |
simonhatch | a13b0a5 | 2015-06-23 20:23:08 | [diff] [blame] | 202 | } |
[email protected] | b83f397 | 2014-01-08 12:16:57 | [diff] [blame] | 203 | |
[email protected] | e4f45cd4 | 2014-01-06 20:55:06 | [diff] [blame] | 204 | LOG(ERROR) << "Unhandled request to " << path; |
| 205 | return false; |
| 206 | } |
| 207 | |
dpapad | 28dccc44 | 2019-04-09 22:06:40 | [diff] [blame] | 208 | bool OnShouldHandleRequest(const std::string& path) { |
| 209 | return base::StartsWith(path, "json/", base::CompareCase::SENSITIVE); |
| 210 | } |
| 211 | |
| 212 | void OnTracingRequest(const std::string& path, |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame] | 213 | WebUIDataSource::GotDataCallback callback) { |
dpapad | 28dccc44 | 2019-04-09 22:06:40 | [diff] [blame] | 214 | DCHECK(OnShouldHandleRequest(path)); |
danakj | 88bb36b | 2020-01-09 15:35:00 | [diff] [blame] | 215 | // OnBeginJSONRequest() only runs |callback| if it returns true. But it needs |
| 216 | // to take ownership of |callback| even though it won't call |callback| |
| 217 | // sometimes, as it binds |callback| into other callbacks before it makes that |
| 218 | // decision. So we must give it one copy and keep one ourselves. |
danakj | 6989c6c | 2021-05-13 22:20:31 | [diff] [blame] | 219 | auto split_callback = base::SplitOnceCallback(std::move(callback)); |
| 220 | if (!OnBeginJSONRequest(path, std::move(split_callback.first))) { |
dpapad | 28dccc44 | 2019-04-09 22:06:40 | [diff] [blame] | 221 | std::string error("##ERROR##"); |
danakj | 6989c6c | 2021-05-13 22:20:31 | [diff] [blame] | 222 | std::move(split_callback.second) |
Jongmok Kim | c549108 | 2022-10-19 09:17:59 | [diff] [blame] | 223 | .Run(base::MakeRefCounted<base::RefCountedString>(std::move(error))); |
[email protected] | e4f45cd4 | 2014-01-06 20:55:06 | [diff] [blame] | 224 | } |
[email protected] | 3dfd28c9 | 2013-03-24 05:36:56 | [diff] [blame] | 225 | } |
| 226 | |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 227 | } // namespace |
| 228 | |
| 229 | |
| 230 | //////////////////////////////////////////////////////////////////////////////// |
| 231 | // |
| 232 | // TracingUI |
| 233 | // |
| 234 | //////////////////////////////////////////////////////////////////////////////// |
| 235 | |
Etienne Pierre-doray | a7c28de | 2024-03-28 22:35:03 | [diff] [blame] | 236 | TracingUI::TracingUI(WebUI* web_ui) : WebUIController(web_ui) { |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 237 | // Set up the chrome://tracing/ source. |
Juyoung Kim | aa699c8 | 2022-02-08 01:55:29 | [diff] [blame] | 238 | WebUIDataSource* source = WebUIDataSource::CreateAndAdd( |
| 239 | web_ui->GetWebContents()->GetBrowserContext(), kChromeUITracingHost); |
Jun Kokatsu | 2e4a490 | 2020-08-05 20:52:16 | [diff] [blame] | 240 | source->DisableTrustedTypesCSP(); |
Dan Beam | e9f4007f | 2019-08-17 00:59:10 | [diff] [blame] | 241 | source->UseStringsJs(); |
dpapad | 57c03ad0 | 2020-11-24 18:51:29 | [diff] [blame] | 242 | source->SetDefaultResource(IDR_TRACING_ABOUT_TRACING_HTML); |
| 243 | source->AddResourcePath("tracing.js", IDR_TRACING_ABOUT_TRACING_JS); |
| 244 | |
dpapad | 28dccc44 | 2019-04-09 22:06:40 | [diff] [blame] | 245 | source->SetRequestFilter(base::BindRepeating(OnShouldHandleRequest), |
| 246 | base::BindRepeating(OnTracingRequest)); |
[email protected] | 06d5b6e | 2014-01-14 14:55:58 | [diff] [blame] | 247 | } |
| 248 | |
Oystein Eftevaag | 2386e05 | 2019-10-02 21:08:09 | [diff] [blame] | 249 | TracingUI::~TracingUI() = default; |
[email protected] | b12387e6 | 2014-06-27 20:52:06 | [diff] [blame] | 250 | |
erikchen | 3e164f7 | 2017-06-22 21:15:44 | [diff] [blame] | 251 | // static |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 252 | bool TracingUI::GetTracingOptions(const std::string& data64, |
| 253 | base::trace_event::TraceConfig& trace_config, |
| 254 | std::string& out_stream_format) { |
erikchen | 3e164f7 | 2017-06-22 21:15:44 | [diff] [blame] | 255 | std::string data; |
| 256 | if (!base::Base64Decode(data64, &data)) { |
| 257 | LOG(ERROR) << "Options were not base64 encoded."; |
| 258 | return false; |
| 259 | } |
| 260 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 261 | std::optional<base::Value> options = base::JSONReader::Read(data); |
Scott Haseley | 02ebe05 | 2021-09-16 01:43:00 | [diff] [blame] | 262 | if (!options) { |
erikchen | 3e164f7 | 2017-06-22 21:15:44 | [diff] [blame] | 263 | LOG(ERROR) << "Options were not valid JSON"; |
| 264 | return false; |
| 265 | } |
Avi Drissman | 89545b1 | 2023-02-02 05:13:08 | [diff] [blame] | 266 | base::Value::Dict* options_dict = options->GetIfDict(); |
| 267 | if (!options_dict) { |
erikchen | 3e164f7 | 2017-06-22 21:15:44 | [diff] [blame] | 268 | LOG(ERROR) << "Options must be dict"; |
| 269 | return false; |
| 270 | } |
| 271 | |
Scott Haseley | 02ebe05 | 2021-09-16 01:43:00 | [diff] [blame] | 272 | if (const std::string* stream_format = |
Avi Drissman | 89545b1 | 2023-02-02 05:13:08 | [diff] [blame] | 273 | options_dict->FindString(kStreamFormat)) { |
Scott Haseley | 02ebe05 | 2021-09-16 01:43:00 | [diff] [blame] | 274 | out_stream_format = *stream_format; |
Sami Kyostila | b3f9041 | 2020-07-09 11:47:54 | [diff] [blame] | 275 | } else { |
| 276 | out_stream_format = kStreamFormatJSON; |
erikchen | 3e164f7 | 2017-06-22 21:15:44 | [diff] [blame] | 277 | } |
| 278 | |
Avi Drissman | 89545b1 | 2023-02-02 05:13:08 | [diff] [blame] | 279 | // New-style options dictionary. |
| 280 | trace_config = base::trace_event::TraceConfig(*options_dict); |
erikchen | 3e164f7 | 2017-06-22 21:15:44 | [diff] [blame] | 281 | return true; |
| 282 | } |
| 283 | |
[email protected] | bcb6ee23d | 2013-02-03 04:06:40 | [diff] [blame] | 284 | } // namespace content |