OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/ui/webui/media/webrtc_logs_ui.h" | |
6 | |
7 #include <vector> | |
8 | |
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/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" | |
32 | |
33 #if defined(OS_CHROMEOS) | |
34 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
35 #endif | |
36 | |
37 using content::WebContents; | |
38 using content::WebUIMessageHandler; | |
39 | |
40 namespace { | |
41 | |
42 content::WebUIDataSource* CreateWebRtcLogsUIHTMLSource() { | |
43 content::WebUIDataSource* source = | |
44 content::WebUIDataSource::Create(chrome::kChromeUIWebRtcLogsHost); | |
45 source->SetUseJsonJSFormatV2(); | |
46 | |
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->AddLocalizedString("disabledHeader", IDS_WEBRTC_LOGS_DISABLED_HEADER); | |
58 source->AddLocalizedString("disabledMessage", | |
59 IDS_WEBRTC_LOGS_DISABLED_MESSAGE); | |
60 source->SetJsonPath("strings.js"); | |
61 source->AddResourcePath("webrtc_logs.js", IDR_WEBRTC_LOGS_JS); | |
62 source->SetDefaultResource(IDR_WEBRTC_LOGS_HTML); | |
63 return source; | |
64 } | |
65 | |
66 //////////////////////////////////////////////////////////////////////////////// | |
67 // | |
68 // WebRtcLogsDOMHandler | |
69 // | |
70 //////////////////////////////////////////////////////////////////////////////// | |
71 | |
72 // The handler for Javascript messages for the chrome://webrtc-logs/ page. | |
73 class WebRtcLogsDOMHandler : public WebUIMessageHandler, | |
74 public WebRtcLogUploadList::Delegate { | |
75 public: | |
76 explicit WebRtcLogsDOMHandler(); | |
77 virtual ~WebRtcLogsDOMHandler(); | |
78 | |
79 // WebUIMessageHandler implementation. | |
80 virtual void RegisterMessages() OVERRIDE; | |
81 | |
82 // WebRtcLogUploadList::Delegate implemenation. | |
83 virtual void OnUploadListAvailable() OVERRIDE; | |
84 | |
85 private: | |
86 // Asynchronously fetches the list of upload WebRTC logs. Called from JS. | |
87 void HandleRequestWebRtcLogs(const ListValue* args); | |
88 | |
89 // Sends the recently uploaded logs list JS. | |
90 void UpdateUI(); | |
91 | |
92 scoped_refptr<WebRtcLogUploadList> upload_list_; | |
James Hawkins
2013/06/26 15:18:53
nit: Document member variables.
Henrik Grunell
2013/06/27 08:16:34
Done.
| |
93 bool list_available_; | |
94 bool js_request_pending_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(WebRtcLogsDOMHandler); | |
97 }; | |
98 | |
99 WebRtcLogsDOMHandler::WebRtcLogsDOMHandler() | |
100 : list_available_(false), js_request_pending_(false) { | |
101 upload_list_ = WebRtcLogUploadList::Create(this); | |
102 } | |
103 | |
104 WebRtcLogsDOMHandler::~WebRtcLogsDOMHandler() { | |
105 upload_list_->ClearDelegate(); | |
106 } | |
107 | |
108 void WebRtcLogsDOMHandler::RegisterMessages() { | |
109 upload_list_->LoadUploadListAsynchronously(); | |
110 | |
111 web_ui()->RegisterMessageCallback("requestWebRtcLogsList", | |
112 base::Bind(&WebRtcLogsDOMHandler::HandleRequestWebRtcLogs, | |
113 base::Unretained(this))); | |
114 } | |
115 | |
116 void WebRtcLogsDOMHandler::HandleRequestWebRtcLogs(const ListValue* args) { | |
117 if (!WebRtcLogsUI::WebRtcLogsUIEnabled() || list_available_) | |
118 UpdateUI(); | |
119 else | |
120 js_request_pending_ = true; | |
121 } | |
122 | |
123 void WebRtcLogsDOMHandler::OnUploadListAvailable() { | |
124 list_available_ = true; | |
125 if (js_request_pending_) | |
126 UpdateUI(); | |
127 } | |
128 | |
129 void WebRtcLogsDOMHandler::UpdateUI() { | |
130 bool webrtc_logs_enabled = WebRtcLogsUI::WebRtcLogsUIEnabled(); | |
131 ListValue upload_list; | |
132 | |
133 if (webrtc_logs_enabled) { | |
134 std::vector<WebRtcLogUploadList::UploadInfo> uploads; | |
135 upload_list_->GetUploads(50, &uploads); | |
136 | |
137 for (std::vector<WebRtcLogUploadList::UploadInfo>::iterator i = | |
138 uploads.begin(); i != uploads.end(); ++i) { | |
139 DictionaryValue* upload = new DictionaryValue(); | |
140 upload->SetString("id", i->id); | |
141 upload->SetString("time", base::TimeFormatFriendlyDateAndTime(i->time)); | |
142 upload_list.Append(upload); | |
143 } | |
144 } | |
145 | |
146 base::FundamentalValue enabled(webrtc_logs_enabled); | |
147 | |
148 const chrome::VersionInfo version_info; | |
149 base::StringValue version(version_info.Version()); | |
150 | |
151 web_ui()->CallJavascriptFunction("updateWebRtcLogsList", enabled, upload_list, | |
152 version); | |
153 } | |
154 | |
155 } // namespace | |
156 | |
157 /////////////////////////////////////////////////////////////////////////////// | |
158 // | |
159 // WebRtcLogsUI | |
160 // | |
161 /////////////////////////////////////////////////////////////////////////////// | |
162 | |
163 WebRtcLogsUI::WebRtcLogsUI(content::WebUI* web_ui) : WebUIController(web_ui) { | |
164 web_ui->AddMessageHandler(new WebRtcLogsDOMHandler()); | |
165 | |
166 // Set up the chrome://webrtc-logs/ source. | |
167 Profile* profile = Profile::FromWebUI(web_ui); | |
168 content::WebUIDataSource::Add(profile, CreateWebRtcLogsUIHTMLSource()); | |
169 } | |
170 | |
171 // static | |
172 bool WebRtcLogsUI::WebRtcLogsUIEnabled() { | |
173 #if defined(GOOGLE_CHROME_BUILD) | |
174 #if defined(OS_CHROMEOS) | |
175 bool reporting_enabled = false; | |
176 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, | |
177 &reporting_enabled); | |
178 return reporting_enabled; | |
179 #elif defined(OS_ANDROID) | |
180 // Android has it's own setings for metrics / crash uploading. | |
181 PrefService* prefs = g_browser_process->local_state(); | |
182 return prefs->GetBoolean(prefs::kCrashReportingEnabled); | |
183 #else | |
184 PrefService* prefs = g_browser_process->local_state(); | |
185 return prefs->GetBoolean(prefs::kMetricsReportingEnabled); | |
186 #endif | |
187 #else | |
188 return false; | |
189 #endif | |
190 } | |
OLD | NEW |