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