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 // Loads, parses and stores the list of uploaded WebRTC logs. 93 scoped_refptr<WebRtcLogUploadList> upload_list_; 94 95 // Set when |upload_list_| has finished populating the list of logs. 96 bool list_available_; 97 98 // Set when the webpage wants to update the list (on the webpage) but 99 // |upload_list_| hasn't finished populating the list of logs yet. 100 bool js_request_pending_; 101 102 DISALLOW_COPY_AND_ASSIGN(WebRtcLogsDOMHandler); 103 }; 104 105 WebRtcLogsDOMHandler::WebRtcLogsDOMHandler() 106 : list_available_(false), js_request_pending_(false) { 107 upload_list_ = WebRtcLogUploadList::Create(this); 108 } 109 110 WebRtcLogsDOMHandler::~WebRtcLogsDOMHandler() { 111 upload_list_->ClearDelegate(); 112 } 113 114 void WebRtcLogsDOMHandler::RegisterMessages() { 115 upload_list_->LoadUploadListAsynchronously(); 116 117 web_ui()->RegisterMessageCallback("requestWebRtcLogsList", 118 base::Bind(&WebRtcLogsDOMHandler::HandleRequestWebRtcLogs, 119 base::Unretained(this))); 120 } 121 122 void WebRtcLogsDOMHandler::HandleRequestWebRtcLogs(const ListValue* args) { 123 if (!WebRtcLogsUI::WebRtcLogsUIEnabled() || list_available_) 124 UpdateUI(); 125 else 126 js_request_pending_ = true; 127 } 128 129 void WebRtcLogsDOMHandler::OnUploadListAvailable() { 130 list_available_ = true; 131 if (js_request_pending_) 132 UpdateUI(); 133 } 134 135 void WebRtcLogsDOMHandler::UpdateUI() { 136 bool webrtc_logs_enabled = WebRtcLogsUI::WebRtcLogsUIEnabled(); 137 ListValue upload_list; 138 139 if (webrtc_logs_enabled) { 140 std::vector<WebRtcLogUploadList::UploadInfo> uploads; 141 upload_list_->GetUploads(50, &uploads); 142 143 for (std::vector<WebRtcLogUploadList::UploadInfo>::iterator i = 144 uploads.begin(); i != uploads.end(); ++i) { 145 DictionaryValue* upload = new DictionaryValue(); 146 upload->SetString("id", i->id); 147 upload->SetString("time", base::TimeFormatFriendlyDateAndTime(i->time)); 148 upload_list.Append(upload); 149 } 150 } 151 152 base::FundamentalValue enabled(webrtc_logs_enabled); 153 154 const chrome::VersionInfo version_info; 155 base::StringValue version(version_info.Version()); 156 157 web_ui()->CallJavascriptFunction("updateWebRtcLogsList", enabled, upload_list, 158 version); 159 } 160 161 } // namespace 162 163 /////////////////////////////////////////////////////////////////////////////// 164 // 165 // WebRtcLogsUI 166 // 167 /////////////////////////////////////////////////////////////////////////////// 168 169 WebRtcLogsUI::WebRtcLogsUI(content::WebUI* web_ui) : WebUIController(web_ui) { 170 web_ui->AddMessageHandler(new WebRtcLogsDOMHandler()); 171 172 // Set up the chrome://webrtc-logs/ source. 173 Profile* profile = Profile::FromWebUI(web_ui); 174 content::WebUIDataSource::Add(profile, CreateWebRtcLogsUIHTMLSource()); 175 } 176 177 // static 178 bool WebRtcLogsUI::WebRtcLogsUIEnabled() { 179 #if defined(GOOGLE_CHROME_BUILD) 180 #if defined(OS_CHROMEOS) 181 bool reporting_enabled = false; 182 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, 183 &reporting_enabled); 184 return reporting_enabled; 185 #elif defined(OS_ANDROID) 186 // Android has it's own setings for metrics / crash uploading. 187 PrefService* prefs = g_browser_process->local_state(); 188 return prefs->GetBoolean(prefs::kCrashReportingEnabled); 189 #else 190 PrefService* prefs = g_browser_process->local_state(); 191 return prefs->GetBoolean(prefs::kMetricsReportingEnabled); 192 #endif 193 #else 194 return false; 195 #endif 196 } 197