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/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/media/webrtc_log_upload_list.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/common/chrome_version_info.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/common/url_constants.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/browser/web_ui.h"
24 #include "content/public/browser/web_ui_data_source.h"
25 #include "content/public/browser/web_ui_message_handler.h"
26 #include "grit/browser_resources.h"
27 #include "grit/chromium_strings.h"
28 #include "grit/generated_resources.h"
29 #include "grit/theme_resources.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
33 #if defined(OS_CHROMEOS)
34 #include "chrome/browser/chromeos/settings/cros_settings.h"
37 using content::WebContents
;
38 using content::WebUIMessageHandler
;
42 content::WebUIDataSource
* CreateWebRtcLogsUIHTMLSource() {
43 content::WebUIDataSource
* source
=
44 content::WebUIDataSource::Create(chrome::kChromeUIWebRtcLogsHost
);
45 source
->SetUseJsonJSFormatV2();
47 source
->AddLocalizedString("webrtcLogsTitle", IDS_WEBRTC_LOGS_TITLE
);
48 source
->AddLocalizedString("webrtcLogCountFormat",
49 IDS_WEBRTC_LOGS_LOG_COUNT_BANNER_FORMAT
);
50 source
->AddLocalizedString("webrtcLogHeaderFormat",
51 IDS_WEBRTC_LOGS_LOG_HEADER_FORMAT
);
52 source
->AddLocalizedString("webrtcLogTimeFormat",
53 IDS_WEBRTC_LOGS_LOG_TIME_FORMAT
);
54 source
->AddLocalizedString("bugLinkText", IDS_WEBRTC_LOGS_BUG_LINK_LABEL
);
55 source
->AddLocalizedString("noLogsMessage",
56 IDS_WEBRTC_LOGS_NO_LOGS_MESSAGE
);
57 source
->SetJsonPath("strings.js");
58 source
->AddResourcePath("webrtc_logs.js", IDR_WEBRTC_LOGS_JS
);
59 source
->SetDefaultResource(IDR_WEBRTC_LOGS_HTML
);
63 ////////////////////////////////////////////////////////////////////////////////
65 // WebRtcLogsDOMHandler
67 ////////////////////////////////////////////////////////////////////////////////
69 // The handler for Javascript messages for the chrome://webrtc-logs/ page.
70 class WebRtcLogsDOMHandler
: public WebUIMessageHandler
,
71 public WebRtcLogUploadList::Delegate
{
73 explicit WebRtcLogsDOMHandler(Profile
* profile
);
74 virtual ~WebRtcLogsDOMHandler();
76 // WebUIMessageHandler implementation.
77 virtual void RegisterMessages() OVERRIDE
;
79 // WebRtcLogUploadList::Delegate implemenation.
80 virtual void OnUploadListAvailable() OVERRIDE
;
83 // Asynchronously fetches the list of upload WebRTC logs. Called from JS.
84 void HandleRequestWebRtcLogs(const base::ListValue
* args
);
86 // Sends the recently uploaded logs list JS.
89 // Loads, parses and stores the list of uploaded WebRTC logs.
90 scoped_refptr
<WebRtcLogUploadList
> upload_list_
;
92 // Set when |upload_list_| has finished populating the list of logs.
95 // Set when the webpage wants to update the list (on the webpage) but
96 // |upload_list_| hasn't finished populating the list of logs yet.
97 bool js_request_pending_
;
99 DISALLOW_COPY_AND_ASSIGN(WebRtcLogsDOMHandler
);
102 WebRtcLogsDOMHandler::WebRtcLogsDOMHandler(Profile
* profile
)
103 : list_available_(false), js_request_pending_(false) {
104 upload_list_
= WebRtcLogUploadList::Create(this, profile
);
107 WebRtcLogsDOMHandler::~WebRtcLogsDOMHandler() {
108 upload_list_
->ClearDelegate();
111 void WebRtcLogsDOMHandler::RegisterMessages() {
112 upload_list_
->LoadUploadListAsynchronously();
114 web_ui()->RegisterMessageCallback("requestWebRtcLogsList",
115 base::Bind(&WebRtcLogsDOMHandler::HandleRequestWebRtcLogs
,
116 base::Unretained(this)));
119 void WebRtcLogsDOMHandler::HandleRequestWebRtcLogs(
120 const base::ListValue
* args
) {
124 js_request_pending_
= true;
127 void WebRtcLogsDOMHandler::OnUploadListAvailable() {
128 list_available_
= true;
129 if (js_request_pending_
)
133 void WebRtcLogsDOMHandler::UpdateUI() {
134 std::vector
<WebRtcLogUploadList::UploadInfo
> uploads
;
135 upload_list_
->GetUploads(50, &uploads
);
137 base::ListValue upload_list
;
138 for (std::vector
<WebRtcLogUploadList::UploadInfo
>::iterator i
=
139 uploads
.begin(); i
!= uploads
.end(); ++i
) {
140 base::DictionaryValue
* upload
= new base::DictionaryValue();
141 upload
->SetString("id", i
->id
);
142 upload
->SetString("time", base::TimeFormatFriendlyDateAndTime(i
->time
));
143 upload_list
.Append(upload
);
146 const chrome::VersionInfo version_info
;
147 base::StringValue
version(version_info
.Version());
149 web_ui()->CallJavascriptFunction("updateWebRtcLogsList", upload_list
,
155 ///////////////////////////////////////////////////////////////////////////////
159 ///////////////////////////////////////////////////////////////////////////////
161 WebRtcLogsUI::WebRtcLogsUI(content::WebUI
* web_ui
) : WebUIController(web_ui
) {
162 Profile
* profile
= Profile::FromWebUI(web_ui
);
163 web_ui
->AddMessageHandler(new WebRtcLogsDOMHandler(profile
));
165 // Set up the chrome://webrtc-logs/ source.
166 content::WebUIDataSource::Add(profile
, CreateWebRtcLogsUIHTMLSource());