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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
|
// Copyright 2013 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/ui/webui/media/webrtc_logs_ui.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/i18n/time_formatting.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/media/webrtc/webrtc_event_log_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/media_resources.h"
#include "components/prefs/pref_service.h"
#include "components/upload_list/upload_list.h"
#include "components/version_info/version_info.h"
#include "components/webrtc_logging/browser/text_log_list.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "ui/webui/webui_util.h"
using content::WebContents;
using content::WebUIMessageHandler;
namespace {
void CreateAndAddWebRtcLogsUIHTMLSource(Profile* profile) {
content::WebUIDataSource* source = content::WebUIDataSource::CreateAndAdd(
profile, chrome::kChromeUIWebRtcLogsHost);
static constexpr webui::LocalizedString kStrings[] = {
{"webrtcLogsTitle", IDS_WEBRTC_LOGS_TITLE},
{"webrtcTextLogCountFormat",
IDS_WEBRTC_TEXT_LOGS_LOG_COUNT_BANNER_FORMAT},
{"webrtcEventLogCountFormat",
IDS_WEBRTC_EVENT_LOGS_LOG_COUNT_BANNER_FORMAT},
{"webrtcLogHeaderFormat", IDS_WEBRTC_LOGS_LOG_HEADER_FORMAT},
{"webrtcLogLocalFileLabelFormat",
IDS_WEBRTC_LOGS_LOG_LOCAL_FILE_LABEL_FORMAT},
{"noLocalLogFileMessage", IDS_WEBRTC_LOGS_NO_LOCAL_LOG_FILE_MESSAGE},
{"webrtcLogUploadTimeFormat", IDS_WEBRTC_LOGS_LOG_UPLOAD_TIME_FORMAT},
{"webrtcLogFailedUploadTimeFormat",
IDS_WEBRTC_LOGS_LOG_FAILED_UPLOAD_TIME_FORMAT},
{"webrtcLogReportIdFormat", IDS_WEBRTC_LOGS_LOG_REPORT_ID_FORMAT},
{"webrtcEventLogLocalLogIdFormat",
IDS_WEBRTC_LOGS_EVENT_LOG_LOCAL_LOG_ID},
{"bugLinkText", IDS_WEBRTC_LOGS_BUG_LINK_LABEL},
{"webrtcLogNotUploadedMessage", IDS_WEBRTC_LOGS_LOG_NOT_UPLOADED_MESSAGE},
{"webrtcLogPendingMessage", IDS_WEBRTC_LOGS_LOG_PENDING_MESSAGE},
{"webrtcLogActivelyUploadedMessage",
IDS_WEBRTC_LOGS_LOG_ACTIVELY_UPLOADED_MESSAGE},
{"noTextLogsMessage", IDS_WEBRTC_LOGS_NO_TEXT_LOGS_MESSAGE},
{"noEventLogsMessage", IDS_WEBRTC_LOGS_NO_EVENT_LOGS_MESSAGE},
};
source->AddLocalizedStrings(kStrings);
source->UseStringsJs();
source->AddResourcePath("webrtc_logs.css", IDR_MEDIA_WEBRTC_LOGS_CSS);
source->AddResourcePath("webrtc_logs.js", IDR_MEDIA_WEBRTC_LOGS_JS);
source->SetDefaultResource(IDR_MEDIA_WEBRTC_LOGS_HTML);
}
////////////////////////////////////////////////////////////////////////////////
//
// WebRtcLogsDOMHandler
//
////////////////////////////////////////////////////////////////////////////////
// The handler for Javascript messages for the chrome://webrtc-logs/ page.
class WebRtcLogsDOMHandler final : public WebUIMessageHandler {
public:
explicit WebRtcLogsDOMHandler(Profile* profile);
WebRtcLogsDOMHandler(const WebRtcLogsDOMHandler&) = delete;
WebRtcLogsDOMHandler& operator=(const WebRtcLogsDOMHandler&) = delete;
~WebRtcLogsDOMHandler() override;
// WebUIMessageHandler implementation.
void OnJavascriptDisallowed() override;
void RegisterMessages() override;
private:
using WebRtcEventLogManager = webrtc_event_logging::WebRtcEventLogManager;
// Asynchronously fetches the list of upload WebRTC logs. Called from JS.
void HandleRequestWebRtcLogs(const base::Value::List& args);
// Asynchronously load WebRTC text logs.
void LoadWebRtcTextLogs(const std::string& callback_id);
// Callback for when WebRTC text logs have been asynchronously loaded.
void OnWebRtcTextLogsLoaded(const std::string& callback_id);
// Asynchronously load WebRTC event logs.
void LoadWebRtcEventLogs(const std::string& callback_id);
// Callback for when WebRTC event logs have been asynchronously loaded.
void OnWebRtcEventLogsLoaded(
const std::string& callback_id,
const std::vector<UploadList::UploadInfo>& event_logs);
// Update the chrome://webrtc-logs/ page.
void UpdateUI(const std::string& callback_id);
// Update the text/event logs part of the forementioned page.
base::Value::List UpdateUIWithTextLogs() const;
base::Value::List UpdateUIWithEventLogs() const;
// Convert a history entry about a captured WebRTC event log into a
// Value of the type expected by updateWebRtcLogsList().
base::Value EventLogUploadInfoToValue(
const UploadList::UploadInfo& info) const;
// Helpers for `EventLogUploadInfoToValue()`.
base::Value FromPendingLog(const UploadList::UploadInfo& info) const;
base::Value FromActivelyUploadedLog(const UploadList::UploadInfo& info) const;
base::Value FromNotUploadedLog(const UploadList::UploadInfo& info) const;
base::Value FromUploadUnsuccessfulLog(
const UploadList::UploadInfo& info) const;
base::Value FromUploadSuccessfulLog(const UploadList::UploadInfo& info) const;
bool SanityCheckOnUploadInfo(const UploadList::UploadInfo& info) const;
// The directories where the (text/event) logs are stored.
const base::FilePath text_log_dir_;
const base::FilePath event_log_dir_;
// Identifies to WebRtcEventLogManager the profile with which |this| is
// associated. Technically, we should be able to just keep Profile*,
// but avoiding it makes less lifetime assumptions.
// Note that the profile->GetOriginalProfile() is used, to make sure that
// for incognito profiles, the parent profile's event logs are shown,
// as is the behavior for text logs.
const WebRtcEventLogManager::BrowserContextId original_browser_context_id_;
// Loads, parses and stores the list of uploaded text WebRTC logs.
scoped_refptr<UploadList> text_log_upload_list_;
// List of WebRTC logs captured and possibly uploaded to Crash.
std::vector<UploadList::UploadInfo> event_logs_;
// Factory for creating weak references to instances of this class.
base::WeakPtrFactory<WebRtcLogsDOMHandler> weak_ptr_factory_{this};
};
WebRtcLogsDOMHandler::WebRtcLogsDOMHandler(Profile* profile)
: text_log_dir_(
webrtc_logging::TextLogList::
GetWebRtcLogDirectoryForBrowserContextPath(profile->GetPath())),
event_log_dir_(
WebRtcEventLogManager::GetRemoteBoundWebRtcEventLogsDir(profile)),
original_browser_context_id_(webrtc_event_logging::GetBrowserContextId(
profile->GetOriginalProfile())),
text_log_upload_list_(
webrtc_logging::TextLogList::CreateWebRtcLogList(profile)) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
}
WebRtcLogsDOMHandler::~WebRtcLogsDOMHandler() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
text_log_upload_list_->CancelLoadCallback();
}
void WebRtcLogsDOMHandler::RegisterMessages() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
web_ui()->RegisterMessageCallback(
"requestWebRtcLogsList",
base::BindRepeating(&WebRtcLogsDOMHandler::HandleRequestWebRtcLogs,
base::Unretained(this)));
}
void WebRtcLogsDOMHandler::HandleRequestWebRtcLogs(
const base::Value::List& args) {
std::string callback_id = args[0].GetString();
AllowJavascript();
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
LoadWebRtcTextLogs(callback_id);
}
void WebRtcLogsDOMHandler::OnJavascriptDisallowed() {
weak_ptr_factory_.InvalidateWeakPtrs();
}
void WebRtcLogsDOMHandler::LoadWebRtcTextLogs(const std::string& callback_id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
text_log_upload_list_->Load(
base::BindOnce(&WebRtcLogsDOMHandler::OnWebRtcTextLogsLoaded,
weak_ptr_factory_.GetWeakPtr(), callback_id));
}
void WebRtcLogsDOMHandler::OnWebRtcTextLogsLoaded(
const std::string& callback_id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
LoadWebRtcEventLogs(callback_id); // Text logs loaded; on to the event logs.
}
void WebRtcLogsDOMHandler::LoadWebRtcEventLogs(const std::string& callback_id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
WebRtcEventLogManager* manager = WebRtcEventLogManager::GetInstance();
if (manager) {
manager->GetHistory(
original_browser_context_id_,
base::BindOnce(&WebRtcLogsDOMHandler::OnWebRtcEventLogsLoaded,
weak_ptr_factory_.GetWeakPtr(), callback_id));
} else {
OnWebRtcEventLogsLoaded(callback_id, std::vector<UploadList::UploadInfo>());
}
}
void WebRtcLogsDOMHandler::OnWebRtcEventLogsLoaded(
const std::string& callback_id,
const std::vector<UploadList::UploadInfo>& event_logs) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
event_logs_ = event_logs;
// All log histories loaded asynchronously; time to display.
UpdateUI(callback_id);
}
void WebRtcLogsDOMHandler::UpdateUI(const std::string& callback_id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
base::Value::Dict result;
result.Set("textLogs", UpdateUIWithTextLogs());
result.Set("eventLogs", UpdateUIWithEventLogs());
result.Set("version", version_info::GetVersionNumber());
ResolveJavascriptCallback(base::Value(callback_id), result);
}
base::Value::List WebRtcLogsDOMHandler::UpdateUIWithTextLogs() const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
base::Value::List result;
const std::vector<const UploadList::UploadInfo*> uploads =
text_log_upload_list_->GetUploads(50);
for (const auto* upload : uploads) {
base::Value::Dict upload_value;
upload_value.Set("id", upload->upload_id);
std::u16string value_w;
if (!upload->upload_time.is_null()) {
value_w = base::TimeFormatFriendlyDateAndTime(upload->upload_time);
}
upload_value.Set("upload_time", value_w);
std::string value;
if (!upload->local_id.empty()) {
value = text_log_dir_.AppendASCII(upload->local_id)
.AddExtension(FILE_PATH_LITERAL(".gz"))
.AsUTF8Unsafe();
}
upload_value.Set("local_file", value);
// In october 2015, capture time was added to the log list, previously the
// local ID was used as capture time. The local ID has however changed so
// that it might not be a time. We fall back on the local ID if it traslates
// to a time within reasonable bounds, otherwise we fall back on the upload
// time.
// TODO(grunell): Use |capture_time| only.
if (!upload->capture_time.is_null()) {
value_w = base::TimeFormatFriendlyDateAndTime(upload->capture_time);
} else {
// Fall back on local ID as time. We need to check that it's within
// resonable bounds, since the ID may not represent time. Check between
// 2012 when the feature was introduced and now.
double seconds_since_epoch;
if (base::StringToDouble(upload->local_id, &seconds_since_epoch)) {
base::Time capture_time =
base::Time::FromSecondsSinceUnixEpoch(seconds_since_epoch);
static constexpr base::Time::Exploded kLowerLimit = {
.year = 2012, .month = 1, .day_of_month = 1};
base::Time out_time;
bool conversion_success =
base::Time::FromUTCExploded(kLowerLimit, &out_time);
DCHECK(conversion_success);
if (capture_time > out_time && capture_time < base::Time::Now()) {
value_w = base::TimeFormatFriendlyDateAndTime(capture_time);
}
}
}
// If we haven't set |value_w| above, we fall back on the upload time, which
// was already in the variable. In case it's empty set the string to
// inform that the time is unknown.
if (value_w.empty()) {
value_w = std::u16string(u"(unknown time)");
}
upload_value.Set("capture_time", value_w);
result.Append(std::move(upload_value));
}
return result;
}
base::Value::List WebRtcLogsDOMHandler::UpdateUIWithEventLogs() const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
base::Value::List result;
for (const auto& log : event_logs_) {
result.Append(EventLogUploadInfoToValue(log));
}
return result;
}
base::Value WebRtcLogsDOMHandler::EventLogUploadInfoToValue(
const UploadList::UploadInfo& info) const {
switch (info.state) {
case UploadList::UploadInfo::State::Pending:
// TODO(crbug.com/40545136): Display actively-written logs differently
// than fully captured pending logs.
return info.upload_time.is_null() ? FromPendingLog(info)
: FromActivelyUploadedLog(info);
case UploadList::UploadInfo::State::NotUploaded:
return info.upload_time.is_null() ? FromNotUploadedLog(info)
: FromUploadUnsuccessfulLog(info);
case UploadList::UploadInfo::State::Uploaded:
return FromUploadSuccessfulLog(info);
case UploadList::UploadInfo::State::Pending_UserRequested:
NOTREACHED();
}
LOG(ERROR) << "Unrecognized state (" << static_cast<int>(info.state) << ").";
return base::Value();
}
base::Value WebRtcLogsDOMHandler::FromPendingLog(
const UploadList::UploadInfo& info) const {
DCHECK_EQ(info.state, UploadList::UploadInfo::State::Pending);
DCHECK(info.upload_time.is_null());
if (!SanityCheckOnUploadInfo(info)) {
return base::Value();
}
base::Value::Dict log;
log.Set("state", "pending");
log.Set("capture_time",
base::TimeFormatFriendlyDateAndTime(info.capture_time));
log.Set("local_file",
event_log_dir_.AppendASCII(info.local_id).AsUTF8Unsafe());
return base::Value(std::move(log));
}
base::Value WebRtcLogsDOMHandler::FromActivelyUploadedLog(
const UploadList::UploadInfo& info) const {
DCHECK_EQ(info.state, UploadList::UploadInfo::State::Pending);
DCHECK(!info.upload_time.is_null());
if (!SanityCheckOnUploadInfo(info)) {
return base::Value();
}
base::Value::Dict log;
log.Set("state", "actively_uploaded");
log.Set("capture_time",
base::TimeFormatFriendlyDateAndTime(info.capture_time));
log.Set("local_file",
event_log_dir_.AppendASCII(info.local_id).AsUTF8Unsafe());
return base::Value(std::move(log));
}
base::Value WebRtcLogsDOMHandler::FromNotUploadedLog(
const UploadList::UploadInfo& info) const {
DCHECK_EQ(info.state, UploadList::UploadInfo::State::NotUploaded);
DCHECK(info.upload_time.is_null());
if (!SanityCheckOnUploadInfo(info)) {
return base::Value();
}
base::Value::Dict log;
log.Set("state", "not_uploaded");
log.Set("capture_time",
base::TimeFormatFriendlyDateAndTime(info.capture_time));
log.Set("local_id", info.local_id);
return base::Value(std::move(log));
}
base::Value WebRtcLogsDOMHandler::FromUploadUnsuccessfulLog(
const UploadList::UploadInfo& info) const {
DCHECK_EQ(info.state, UploadList::UploadInfo::State::NotUploaded);
DCHECK(!info.upload_time.is_null());
if (!SanityCheckOnUploadInfo(info)) {
return base::Value();
}
if (!info.upload_id.empty()) {
LOG(ERROR) << "Unexpected upload ID.";
return base::Value();
}
base::Value::Dict log;
log.Set("state", "upload_unsuccessful");
log.Set("capture_time",
base::TimeFormatFriendlyDateAndTime(info.capture_time));
log.Set("local_id", info.local_id);
log.Set("upload_time", base::TimeFormatFriendlyDateAndTime(info.upload_time));
return base::Value(std::move(log));
}
base::Value WebRtcLogsDOMHandler::FromUploadSuccessfulLog(
const UploadList::UploadInfo& info) const {
DCHECK_EQ(info.state, UploadList::UploadInfo::State::Uploaded);
DCHECK(!info.upload_time.is_null());
if (!SanityCheckOnUploadInfo(info)) {
return base::Value();
}
if (info.upload_id.empty()) {
LOG(ERROR) << "Unknown upload ID.";
return base::Value();
}
base::Value::Dict log;
log.Set("state", "upload_successful");
log.Set("capture_time",
base::TimeFormatFriendlyDateAndTime(info.capture_time));
log.Set("local_id", info.local_id);
log.Set("upload_id", info.upload_id);
log.Set("upload_time", base::TimeFormatFriendlyDateAndTime(info.upload_time));
return base::Value(std::move(log));
}
bool WebRtcLogsDOMHandler::SanityCheckOnUploadInfo(
const UploadList::UploadInfo& info) const {
if (info.capture_time.is_null()) {
LOG(ERROR) << "Unknown capture time.";
return false;
}
if (info.local_id.empty()) {
LOG(ERROR) << "Unknown local ID.";
return false;
}
return true;
}
} // namespace
///////////////////////////////////////////////////////////////////////////////
//
// WebRtcLogsUI
//
///////////////////////////////////////////////////////////////////////////////
WebRtcLogsUI::WebRtcLogsUI(content::WebUI* web_ui) : WebUIController(web_ui) {
Profile* profile = Profile::FromWebUI(web_ui);
web_ui->AddMessageHandler(std::make_unique<WebRtcLogsDOMHandler>(profile));
// Set up the chrome://webrtc-logs/ source.
CreateAndAddWebRtcLogsUIHTMLSource(profile);
}
|