1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/media/webrtc_logs_ui.h"
10 #include "base/bind_helpers.h"
11 #include "base/i18n/time_formatting.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/values.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/media/webrtc_log_list.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/common/url_constants.h"
21 #include "chrome/grit/generated_resources.h"
22 #include "components/upload_list/upload_list.h"
23 #include "components/version_info/version_info.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/browser/web_ui.h"
26 #include "content/public/browser/web_ui_data_source.h"
27 #include "content/public/browser/web_ui_message_handler.h"
28 #include "grit/browser_resources.h"
30 #if defined(OS_CHROMEOS)
31 #include "chrome/browser/chromeos/settings/cros_settings.h"
34 using content::WebContents
;
35 using content::WebUIMessageHandler
;
39 content::WebUIDataSource
* CreateWebRtcLogsUIHTMLSource() {
40 content::WebUIDataSource
* source
=
41 content::WebUIDataSource::Create(chrome::kChromeUIWebRtcLogsHost
);
43 source
->AddLocalizedString("webrtcLogsTitle", IDS_WEBRTC_LOGS_TITLE
);
44 source
->AddLocalizedString("webrtcLogCountFormat",
45 IDS_WEBRTC_LOGS_LOG_COUNT_BANNER_FORMAT
);
46 source
->AddLocalizedString("webrtcLogHeaderFormat",
47 IDS_WEBRTC_LOGS_LOG_HEADER_FORMAT
);
48 source
->AddLocalizedString("webrtcLogLocalFileLabelFormat",
49 IDS_WEBRTC_LOGS_LOG_LOCAL_FILE_LABEL_FORMAT
);
50 source
->AddLocalizedString("webrtcLogLocalFileFormat",
51 IDS_WEBRTC_LOGS_LOG_LOCAL_FILE_FORMAT
);
52 source
->AddLocalizedString("noLocalLogFileMessage",
53 IDS_WEBRTC_LOGS_NO_LOCAL_LOG_FILE_MESSAGE
);
54 source
->AddLocalizedString("webrtcLogUploadTimeFormat",
55 IDS_WEBRTC_LOGS_LOG_UPLOAD_TIME_FORMAT
);
56 source
->AddLocalizedString("webrtcLogReportIdFormat",
57 IDS_WEBRTC_LOGS_LOG_REPORT_ID_FORMAT
);
58 source
->AddLocalizedString("bugLinkText", IDS_WEBRTC_LOGS_BUG_LINK_LABEL
);
59 source
->AddLocalizedString("webrtcLogNotUploadedMessage",
60 IDS_WEBRTC_LOGS_LOG_NOT_UPLOADED_MESSAGE
);
61 source
->AddLocalizedString("noLogsMessage",
62 IDS_WEBRTC_LOGS_NO_LOGS_MESSAGE
);
63 source
->SetJsonPath("strings.js");
64 source
->AddResourcePath("webrtc_logs.js", IDR_WEBRTC_LOGS_JS
);
65 source
->SetDefaultResource(IDR_WEBRTC_LOGS_HTML
);
69 ////////////////////////////////////////////////////////////////////////////////
71 // WebRtcLogsDOMHandler
73 ////////////////////////////////////////////////////////////////////////////////
75 // The handler for Javascript messages for the chrome://webrtc-logs/ page.
76 class WebRtcLogsDOMHandler
: public WebUIMessageHandler
,
77 public UploadList::Delegate
{
79 explicit WebRtcLogsDOMHandler(Profile
* profile
);
80 ~WebRtcLogsDOMHandler() override
;
82 // WebUIMessageHandler implementation.
83 void RegisterMessages() override
;
85 // UploadList::Delegate implemenation.
86 void OnUploadListAvailable() override
;
89 // Asynchronously fetches the list of upload WebRTC logs. Called from JS.
90 void HandleRequestWebRtcLogs(const base::ListValue
* args
);
92 // Sends the recently uploaded logs list JS.
95 // Loads, parses and stores the list of uploaded WebRTC logs.
96 scoped_refptr
<UploadList
> upload_list_
;
98 // The directory where the logs are stored.
99 base::FilePath log_dir_
;
101 // Set when |upload_list_| has finished populating the list of logs.
102 bool list_available_
;
104 // Set when the webpage wants to update the list (on the webpage) but
105 // |upload_list_| hasn't finished populating the list of logs yet.
106 bool js_request_pending_
;
108 DISALLOW_COPY_AND_ASSIGN(WebRtcLogsDOMHandler
);
111 WebRtcLogsDOMHandler::WebRtcLogsDOMHandler(Profile
* profile
)
113 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile
->GetPath())),
114 list_available_(false),
115 js_request_pending_(false) {
116 upload_list_
= WebRtcLogList::CreateWebRtcLogList(this, profile
);
119 WebRtcLogsDOMHandler::~WebRtcLogsDOMHandler() {
120 upload_list_
->ClearDelegate();
123 void WebRtcLogsDOMHandler::RegisterMessages() {
124 upload_list_
->LoadUploadListAsynchronously();
126 web_ui()->RegisterMessageCallback("requestWebRtcLogsList",
127 base::Bind(&WebRtcLogsDOMHandler::HandleRequestWebRtcLogs
,
128 base::Unretained(this)));
131 void WebRtcLogsDOMHandler::HandleRequestWebRtcLogs(
132 const base::ListValue
* args
) {
136 js_request_pending_
= true;
139 void WebRtcLogsDOMHandler::OnUploadListAvailable() {
140 list_available_
= true;
141 if (js_request_pending_
)
145 void WebRtcLogsDOMHandler::UpdateUI() {
146 std::vector
<UploadList::UploadInfo
> uploads
;
147 upload_list_
->GetUploads(50, &uploads
);
149 base::ListValue upload_list
;
150 for (std::vector
<UploadList::UploadInfo
>::iterator i
= uploads
.begin();
153 base::DictionaryValue
* upload
= new base::DictionaryValue();
154 upload
->SetString("id", i
->id
);
156 base::string16 value_w
;
157 if (!i
->time
.is_null())
158 value_w
= base::TimeFormatFriendlyDateAndTime(i
->time
);
159 upload
->SetString("upload_time", value_w
);
162 double seconds_since_epoch
;
163 if (base::StringToDouble(i
->local_id
, &seconds_since_epoch
)) {
164 base::Time capture_time
= base::Time::FromDoubleT(seconds_since_epoch
);
165 value_w
= base::TimeFormatFriendlyDateAndTime(capture_time
);
167 upload
->SetString("capture_time", value_w
);
169 base::FilePath::StringType value
;
170 if (!i
->local_id
.empty())
171 value
= log_dir_
.AppendASCII(i
->local_id
)
172 .AddExtension(FILE_PATH_LITERAL(".gz")).value();
173 upload
->SetString("local_file", value
);
175 upload_list
.Append(upload
);
178 base::StringValue
version(version_info::GetVersionNumber());
180 web_ui()->CallJavascriptFunction("updateWebRtcLogsList", upload_list
,
186 ///////////////////////////////////////////////////////////////////////////////
190 ///////////////////////////////////////////////////////////////////////////////
192 WebRtcLogsUI::WebRtcLogsUI(content::WebUI
* web_ui
) : WebUIController(web_ui
) {
193 Profile
* profile
= Profile::FromWebUI(web_ui
);
194 web_ui
->AddMessageHandler(new WebRtcLogsDOMHandler(profile
));
196 // Set up the chrome://webrtc-logs/ source.
197 content::WebUIDataSource::Add(profile
, CreateWebRtcLogsUIHTMLSource());