Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / webui / crashes_ui.cc
blob64fbfff09c11f4b1f07979ecb5926048ca6d20d6
1 // Copyright (c) 2012 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/crashes_ui.h"
7 #include <vector>
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/crash_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"
35 #endif
37 using content::WebContents;
38 using content::WebUIMessageHandler;
40 namespace {
42 content::WebUIDataSource* CreateCrashesUIHTMLSource() {
43 content::WebUIDataSource* source =
44 content::WebUIDataSource::Create(chrome::kChromeUICrashesHost);
45 source->SetUseJsonJSFormatV2();
47 source->AddLocalizedString("crashesTitle", IDS_CRASHES_TITLE);
48 source->AddLocalizedString("crashCountFormat",
49 IDS_CRASHES_CRASH_COUNT_BANNER_FORMAT);
50 source->AddLocalizedString("crashHeaderFormat",
51 IDS_CRASHES_CRASH_HEADER_FORMAT);
52 source->AddLocalizedString("crashTimeFormat", IDS_CRASHES_CRASH_TIME_FORMAT);
53 source->AddLocalizedString("bugLinkText", IDS_CRASHES_BUG_LINK_LABEL);
54 source->AddLocalizedString("noCrashesMessage",
55 IDS_CRASHES_NO_CRASHES_MESSAGE);
56 source->AddLocalizedString("disabledHeader", IDS_CRASHES_DISABLED_HEADER);
57 source->AddLocalizedString("disabledMessage", IDS_CRASHES_DISABLED_MESSAGE);
58 source->SetJsonPath("strings.js");
59 source->AddResourcePath("crashes.js", IDR_CRASHES_JS);
60 source->SetDefaultResource(IDR_CRASHES_HTML);
61 return source;
64 ////////////////////////////////////////////////////////////////////////////////
66 // CrashesDOMHandler
68 ////////////////////////////////////////////////////////////////////////////////
70 // The handler for Javascript messages for the chrome://crashes/ page.
71 class CrashesDOMHandler : public WebUIMessageHandler,
72 public CrashUploadList::Delegate {
73 public:
74 explicit CrashesDOMHandler();
75 virtual ~CrashesDOMHandler();
77 // WebUIMessageHandler implementation.
78 virtual void RegisterMessages() OVERRIDE;
80 // CrashUploadList::Delegate implemenation.
81 virtual void OnUploadListAvailable() OVERRIDE;
83 private:
84 // Asynchronously fetches the list of crashes. Called from JS.
85 void HandleRequestCrashes(const base::ListValue* args);
87 // Sends the recent crashes list JS.
88 void UpdateUI();
90 scoped_refptr<CrashUploadList> upload_list_;
91 bool list_available_;
92 bool first_load_;
94 DISALLOW_COPY_AND_ASSIGN(CrashesDOMHandler);
97 CrashesDOMHandler::CrashesDOMHandler()
98 : list_available_(false), first_load_(true) {
99 upload_list_ = CrashUploadList::Create(this);
102 CrashesDOMHandler::~CrashesDOMHandler() {
103 upload_list_->ClearDelegate();
106 void CrashesDOMHandler::RegisterMessages() {
107 upload_list_->LoadUploadListAsynchronously();
108 web_ui()->RegisterMessageCallback("requestCrashList",
109 base::Bind(&CrashesDOMHandler::HandleRequestCrashes,
110 base::Unretained(this)));
113 void CrashesDOMHandler::HandleRequestCrashes(const base::ListValue* args) {
114 if (first_load_) {
115 first_load_ = false;
116 if (list_available_)
117 UpdateUI();
118 } else {
119 list_available_ = false;
120 upload_list_->LoadUploadListAsynchronously();
124 void CrashesDOMHandler::OnUploadListAvailable() {
125 list_available_ = true;
126 if (!first_load_)
127 UpdateUI();
130 void CrashesDOMHandler::UpdateUI() {
131 bool crash_reporting_enabled = CrashesUI::CrashReportingUIEnabled();
132 base::ListValue crash_list;
134 if (crash_reporting_enabled) {
135 std::vector<CrashUploadList::UploadInfo> crashes;
136 upload_list_->GetUploads(50, &crashes);
138 for (std::vector<CrashUploadList::UploadInfo>::iterator i = crashes.begin();
139 i != crashes.end(); ++i) {
140 base::DictionaryValue* crash = new base::DictionaryValue();
141 crash->SetString("id", i->id);
142 crash->SetString("time", base::TimeFormatFriendlyDateAndTime(i->time));
143 crash_list.Append(crash);
147 base::FundamentalValue enabled(crash_reporting_enabled);
149 const chrome::VersionInfo version_info;
150 base::StringValue version(version_info.Version());
152 web_ui()->CallJavascriptFunction("updateCrashList", enabled, crash_list,
153 version);
156 } // namespace
158 ///////////////////////////////////////////////////////////////////////////////
160 // CrashesUI
162 ///////////////////////////////////////////////////////////////////////////////
164 CrashesUI::CrashesUI(content::WebUI* web_ui) : WebUIController(web_ui) {
165 web_ui->AddMessageHandler(new CrashesDOMHandler());
167 // Set up the chrome://crashes/ source.
168 Profile* profile = Profile::FromWebUI(web_ui);
169 content::WebUIDataSource::Add(profile, CreateCrashesUIHTMLSource());
172 // static
173 base::RefCountedMemory* CrashesUI::GetFaviconResourceBytes(
174 ui::ScaleFactor scale_factor) {
175 return ResourceBundle::GetSharedInstance().
176 LoadDataResourceBytesForScale(IDR_SAD_FAVICON, scale_factor);
179 // static
180 bool CrashesUI::CrashReportingUIEnabled() {
181 #if defined(GOOGLE_CHROME_BUILD)
182 #if defined(OS_CHROMEOS)
183 bool reporting_enabled = false;
184 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref,
185 &reporting_enabled);
186 return reporting_enabled;
187 #elif defined(OS_ANDROID)
188 // Android has it's own setings for metrics / crash uploading.
189 PrefService* prefs = g_browser_process->local_state();
190 return prefs->GetBoolean(prefs::kCrashReportingEnabled);
191 #else
192 PrefService* prefs = g_browser_process->local_state();
193 return prefs->GetBoolean(prefs::kMetricsReportingEnabled);
194 #endif
195 #else
196 return false;
197 #endif