1// Copyright (c) 2011 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/crashes_ui.h" 6 7#include "base/i18n/time_formatting.h" 8#include "base/memory/ref_counted_memory.h" 9#include "base/utf_string_conversions.h" 10#include "base/values.h" 11#include "chrome/browser/browser_process.h" 12#include "chrome/browser/crash_upload_list.h" 13#include "chrome/browser/prefs/pref_service.h" 14#include "chrome/browser/profiles/profile.h" 15#include "chrome/browser/ui/webui/chrome_url_data_manager.h" 16#include "chrome/common/chrome_version_info.h" 17#include "chrome/common/jstemplate_builder.h" 18#include "chrome/common/pref_names.h" 19#include "chrome/common/url_constants.h" 20#include "content/browser/tab_contents/tab_contents.h" 21#include "grit/browser_resources.h" 22#include "grit/chromium_strings.h" 23#include "grit/generated_resources.h" 24#include "grit/theme_resources.h" 25#include "ui/base/l10n/l10n_util.h" 26#include "ui/base/resource/resource_bundle.h" 27 28#if defined(OS_CHROMEOS) 29#include "chrome/browser/chromeos/metrics_cros_settings_provider.h" 30#endif 31 32namespace { 33 34/////////////////////////////////////////////////////////////////////////////// 35// 36// CrashesUIHTMLSource 37// 38/////////////////////////////////////////////////////////////////////////////// 39 40class CrashesUIHTMLSource : public ChromeURLDataManager::DataSource { 41 public: 42 CrashesUIHTMLSource() 43 : DataSource(chrome::kChromeUICrashesHost, MessageLoop::current()) {} 44 45 // Called when the network layer has requested a resource underneath 46 // the path we registered. 47 virtual void StartDataRequest(const std::string& path, 48 bool is_incognito, 49 int request_id); 50 virtual std::string GetMimeType(const std::string&) const { 51 return "text/html"; 52 } 53 54 private: 55 ~CrashesUIHTMLSource() {} 56 57 DISALLOW_COPY_AND_ASSIGN(CrashesUIHTMLSource); 58}; 59 60void CrashesUIHTMLSource::StartDataRequest(const std::string& path, 61 bool is_incognito, 62 int request_id) { 63 DictionaryValue localized_strings; 64 localized_strings.SetString("crashesTitle", 65 l10n_util::GetStringUTF16(IDS_CRASHES_TITLE)); 66 localized_strings.SetString("crashCountFormat", 67 l10n_util::GetStringUTF16(IDS_CRASHES_CRASH_COUNT_BANNER_FORMAT)); 68 localized_strings.SetString("crashHeaderFormat", 69 l10n_util::GetStringUTF16(IDS_CRASHES_CRASH_HEADER_FORMAT)); 70 localized_strings.SetString("crashTimeFormat", 71 l10n_util::GetStringUTF16(IDS_CRASHES_CRASH_TIME_FORMAT)); 72 localized_strings.SetString("bugLinkText", 73 l10n_util::GetStringUTF16(IDS_CRASHES_BUG_LINK_LABEL)); 74 localized_strings.SetString("noCrashesMessage", 75 l10n_util::GetStringUTF16(IDS_CRASHES_NO_CRASHES_MESSAGE)); 76 localized_strings.SetString("disabledHeader", 77 l10n_util::GetStringUTF16(IDS_CRASHES_DISABLED_HEADER)); 78 localized_strings.SetString("disabledMessage", 79 l10n_util::GetStringUTF16(IDS_CRASHES_DISABLED_MESSAGE)); 80 81 ChromeURLDataManager::DataSource::SetFontAndTextDirection(&localized_strings); 82 83 static const base::StringPiece crashes_html( 84 ResourceBundle::GetSharedInstance().GetRawDataResource(IDR_CRASHES_HTML)); 85 std::string full_html = 86 jstemplate_builder::GetI18nTemplateHtml(crashes_html, &localized_strings); 87 jstemplate_builder::AppendJsTemplateSourceHtml(&full_html); 88 89 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); 90 html_bytes->data.resize(full_html.size()); 91 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); 92 93 SendResponse(request_id, html_bytes); 94} 95 96//////////////////////////////////////////////////////////////////////////////// 97// 98// CrashesDOMHandler 99// 100//////////////////////////////////////////////////////////////////////////////// 101 102// The handler for Javascript messages for the chrome://crashes/ page. 103class CrashesDOMHandler : public WebUIMessageHandler, 104 public CrashUploadList::Delegate { 105 public: 106 explicit CrashesDOMHandler(); 107 virtual ~CrashesDOMHandler(); 108 109 // WebUIMessageHandler implementation. 110 virtual WebUIMessageHandler* Attach(WebUI* web_ui); 111 virtual void RegisterMessages(); 112 113 // CrashUploadList::Delegate implemenation. 114 virtual void OnCrashListAvailable(); 115 116 private: 117 // Asynchronously fetches the list of crashes. Called from JS. 118 void HandleRequestCrashes(const ListValue* args); 119 120 // Sends the recent crashes list JS. 121 void UpdateUI(); 122 123 // Returns whether or not crash reporting is enabled. 124 bool CrashReportingEnabled() const; 125 126 scoped_refptr<CrashUploadList> upload_list_; 127 bool list_available_; 128 bool js_request_pending_; 129 130 DISALLOW_COPY_AND_ASSIGN(CrashesDOMHandler); 131}; 132 133CrashesDOMHandler::CrashesDOMHandler() 134 : list_available_(false), js_request_pending_(false) { 135 upload_list_ = CrashUploadList::Create(this); 136} 137 138CrashesDOMHandler::~CrashesDOMHandler() { 139 upload_list_->ClearDelegate(); 140} 141 142WebUIMessageHandler* CrashesDOMHandler::Attach(WebUI* web_ui) { 143 upload_list_->LoadCrashListAsynchronously(); 144 return WebUIMessageHandler::Attach(web_ui); 145} 146 147void CrashesDOMHandler::RegisterMessages() { 148 web_ui_->RegisterMessageCallback("requestCrashList", 149 NewCallback(this, &CrashesDOMHandler::HandleRequestCrashes)); 150} 151 152void CrashesDOMHandler::HandleRequestCrashes(const ListValue* args) { 153 if (!CrashReportingEnabled() || list_available_) 154 UpdateUI(); 155 else 156 js_request_pending_ = true; 157} 158 159void CrashesDOMHandler::OnCrashListAvailable() { 160 list_available_ = true; 161 if (js_request_pending_) 162 UpdateUI(); 163} 164 165void CrashesDOMHandler::UpdateUI() { 166 bool crash_reporting_enabled = CrashReportingEnabled(); 167 ListValue crash_list; 168 169 if (crash_reporting_enabled) { 170 std::vector<CrashUploadList::CrashInfo> crashes; 171 upload_list_->GetUploadedCrashes(50, &crashes); 172 173 for (std::vector<CrashUploadList::CrashInfo>::iterator i = crashes.begin(); 174 i != crashes.end(); ++i) { 175 DictionaryValue* crash = new DictionaryValue(); 176 crash->SetString("id", i->crash_id); 177 crash->SetString("time", 178 base::TimeFormatFriendlyDateAndTime(i->crash_time)); 179 crash_list.Append(crash); 180 } 181 } 182 183 FundamentalValue enabled(crash_reporting_enabled); 184 185 const chrome::VersionInfo version_info; 186 StringValue version(version_info.Version()); 187 188 web_ui_->CallJavascriptFunction("updateCrashList", enabled, crash_list, 189 version); 190} 191 192bool CrashesDOMHandler::CrashReportingEnabled() const { 193#if defined(GOOGLE_CHROME_BUILD) && !defined(OS_CHROMEOS) 194 PrefService* prefs = g_browser_process->local_state(); 195 return prefs->GetBoolean(prefs::kMetricsReportingEnabled); 196#elif defined(GOOGLE_CHROME_BUILD) && defined(OS_CHROMEOS) 197 return chromeos::MetricsCrosSettingsProvider::GetMetricsStatus(); 198#else 199 return false; 200#endif 201} 202 203} // namespace 204 205/////////////////////////////////////////////////////////////////////////////// 206// 207// CrashesUI 208// 209/////////////////////////////////////////////////////////////////////////////// 210 211CrashesUI::CrashesUI(TabContents* contents) : WebUI(contents) { 212 AddMessageHandler((new CrashesDOMHandler())->Attach(this)); 213 214 CrashesUIHTMLSource* html_source = new CrashesUIHTMLSource(); 215 216 // Set up the chrome://crashes/ source. 217 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source); 218} 219 220// static 221RefCountedMemory* CrashesUI::GetFaviconResourceBytes() { 222 return ResourceBundle::GetSharedInstance(). 223 LoadDataResourceBytes(IDR_SAD_FAVICON); 224} 225