Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / media / webrtc_logs_ui.cc
blob37ab6fe51bbe42a3d425011c20c71c08df84d05d
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"
7 #include <vector>
9 #include "base/bind.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"
32 #endif
34 using content::WebContents;
35 using content::WebUIMessageHandler;
37 namespace {
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("noLocalLogFileMessage",
51 IDS_WEBRTC_LOGS_NO_LOCAL_LOG_FILE_MESSAGE);
52 source->AddLocalizedString("webrtcLogUploadTimeFormat",
53 IDS_WEBRTC_LOGS_LOG_UPLOAD_TIME_FORMAT);
54 source->AddLocalizedString("webrtcLogReportIdFormat",
55 IDS_WEBRTC_LOGS_LOG_REPORT_ID_FORMAT);
56 source->AddLocalizedString("bugLinkText", IDS_WEBRTC_LOGS_BUG_LINK_LABEL);
57 source->AddLocalizedString("webrtcLogNotUploadedMessage",
58 IDS_WEBRTC_LOGS_LOG_NOT_UPLOADED_MESSAGE);
59 source->AddLocalizedString("noLogsMessage",
60 IDS_WEBRTC_LOGS_NO_LOGS_MESSAGE);
61 source->SetJsonPath("strings.js");
62 source->AddResourcePath("webrtc_logs.js", IDR_WEBRTC_LOGS_JS);
63 source->SetDefaultResource(IDR_WEBRTC_LOGS_HTML);
64 return source;
67 ////////////////////////////////////////////////////////////////////////////////
69 // WebRtcLogsDOMHandler
71 ////////////////////////////////////////////////////////////////////////////////
73 // The handler for Javascript messages for the chrome://webrtc-logs/ page.
74 class WebRtcLogsDOMHandler : public WebUIMessageHandler,
75 public UploadList::Delegate {
76 public:
77 explicit WebRtcLogsDOMHandler(Profile* profile);
78 ~WebRtcLogsDOMHandler() override;
80 // WebUIMessageHandler implementation.
81 void RegisterMessages() override;
83 // UploadList::Delegate implemenation.
84 void OnUploadListAvailable() override;
86 private:
87 // Asynchronously fetches the list of upload WebRTC logs. Called from JS.
88 void HandleRequestWebRtcLogs(const base::ListValue* args);
90 // Sends the recently uploaded logs list JS.
91 void UpdateUI();
93 // Loads, parses and stores the list of uploaded WebRTC logs.
94 scoped_refptr<UploadList> upload_list_;
96 // The directory where the logs are stored.
97 base::FilePath log_dir_;
99 // Set when |upload_list_| has finished populating the list of logs.
100 bool list_available_;
102 // Set when the webpage wants to update the list (on the webpage) but
103 // |upload_list_| hasn't finished populating the list of logs yet.
104 bool js_request_pending_;
106 DISALLOW_COPY_AND_ASSIGN(WebRtcLogsDOMHandler);
109 WebRtcLogsDOMHandler::WebRtcLogsDOMHandler(Profile* profile)
110 : log_dir_(
111 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile->GetPath())),
112 list_available_(false),
113 js_request_pending_(false) {
114 upload_list_ = WebRtcLogList::CreateWebRtcLogList(this, profile);
117 WebRtcLogsDOMHandler::~WebRtcLogsDOMHandler() {
118 upload_list_->ClearDelegate();
121 void WebRtcLogsDOMHandler::RegisterMessages() {
122 upload_list_->LoadUploadListAsynchronously();
124 web_ui()->RegisterMessageCallback("requestWebRtcLogsList",
125 base::Bind(&WebRtcLogsDOMHandler::HandleRequestWebRtcLogs,
126 base::Unretained(this)));
129 void WebRtcLogsDOMHandler::HandleRequestWebRtcLogs(
130 const base::ListValue* args) {
131 if (list_available_)
132 UpdateUI();
133 else
134 js_request_pending_ = true;
137 void WebRtcLogsDOMHandler::OnUploadListAvailable() {
138 list_available_ = true;
139 if (js_request_pending_)
140 UpdateUI();
143 void WebRtcLogsDOMHandler::UpdateUI() {
144 std::vector<UploadList::UploadInfo> uploads;
145 upload_list_->GetUploads(50, &uploads);
147 base::ListValue upload_list;
148 for (std::vector<UploadList::UploadInfo>::iterator i = uploads.begin();
149 i != uploads.end();
150 ++i) {
151 base::DictionaryValue* upload = new base::DictionaryValue();
152 upload->SetString("id", i->id);
154 base::string16 value_w;
155 if (!i->time.is_null())
156 value_w = base::TimeFormatFriendlyDateAndTime(i->time);
157 upload->SetString("upload_time", value_w);
159 value_w.clear();
160 double seconds_since_epoch;
161 if (base::StringToDouble(i->local_id, &seconds_since_epoch)) {
162 base::Time capture_time = base::Time::FromDoubleT(seconds_since_epoch);
163 value_w = base::TimeFormatFriendlyDateAndTime(capture_time);
165 upload->SetString("capture_time", value_w);
167 base::FilePath::StringType value;
168 if (!i->local_id.empty())
169 value = log_dir_.AppendASCII(i->local_id)
170 .AddExtension(FILE_PATH_LITERAL(".gz")).value();
171 upload->SetString("local_file", value);
173 upload_list.Append(upload);
176 base::StringValue version(version_info::GetVersionNumber());
178 web_ui()->CallJavascriptFunction("updateWebRtcLogsList", upload_list,
179 version);
182 } // namespace
184 ///////////////////////////////////////////////////////////////////////////////
186 // WebRtcLogsUI
188 ///////////////////////////////////////////////////////////////////////////////
190 WebRtcLogsUI::WebRtcLogsUI(content::WebUI* web_ui) : WebUIController(web_ui) {
191 Profile* profile = Profile::FromWebUI(web_ui);
192 web_ui->AddMessageHandler(new WebRtcLogsDOMHandler(profile));
194 // Set up the chrome://webrtc-logs/ source.
195 content::WebUIDataSource::Add(profile, CreateWebRtcLogsUIHTMLSource());