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"
10 #include "base/bind_helpers.h"
11 #include "base/i18n/time_formatting.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "chrome/browser/crash_upload_list.h"
16 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/common/url_constants.h"
19 #include "chrome/grit/chromium_strings.h"
20 #include "chrome/grit/generated_resources.h"
21 #include "components/version_info/version_info.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/theme_resources.h"
28 #include "ui/base/resource/resource_bundle.h"
30 #if defined(OS_CHROMEOS)
31 #include "chromeos/dbus/dbus_thread_manager.h"
32 #include "chromeos/dbus/debug_daemon_client.h"
35 using content::WebContents
;
36 using content::WebUIMessageHandler
;
40 content::WebUIDataSource
* CreateCrashesUIHTMLSource() {
41 content::WebUIDataSource
* source
=
42 content::WebUIDataSource::Create(chrome::kChromeUICrashesHost
);
44 source
->AddLocalizedString("shortProductName", IDS_SHORT_PRODUCT_NAME
);
45 source
->AddLocalizedString("crashesTitle", IDS_CRASHES_TITLE
);
46 source
->AddLocalizedString("crashCountFormat",
47 IDS_CRASHES_CRASH_COUNT_BANNER_FORMAT
);
48 source
->AddLocalizedString("crashHeaderFormat",
49 IDS_CRASHES_CRASH_HEADER_FORMAT
);
50 source
->AddLocalizedString("crashTimeFormat", IDS_CRASHES_CRASH_TIME_FORMAT
);
51 source
->AddLocalizedString("bugLinkText", IDS_CRASHES_BUG_LINK_LABEL
);
52 source
->AddLocalizedString("noCrashesMessage",
53 IDS_CRASHES_NO_CRASHES_MESSAGE
);
54 source
->AddLocalizedString("disabledHeader", IDS_CRASHES_DISABLED_HEADER
);
55 source
->AddLocalizedString("disabledMessage", IDS_CRASHES_DISABLED_MESSAGE
);
56 source
->AddLocalizedString("uploadCrashesLinkText",
57 IDS_CRASHES_UPLOAD_MESSAGE
);
58 source
->SetJsonPath("strings.js");
59 source
->AddResourcePath("crashes.js", IDR_CRASHES_JS
);
60 source
->SetDefaultResource(IDR_CRASHES_HTML
);
64 ////////////////////////////////////////////////////////////////////////////////
68 ////////////////////////////////////////////////////////////////////////////////
70 // The handler for Javascript messages for the chrome://crashes/ page.
71 class CrashesDOMHandler
: public WebUIMessageHandler
,
72 public CrashUploadList::Delegate
{
75 ~CrashesDOMHandler() override
;
77 // WebUIMessageHandler implementation.
78 void RegisterMessages() override
;
80 // CrashUploadList::Delegate implemenation.
81 void OnUploadListAvailable() override
;
84 // Asynchronously fetches the list of crashes. Called from JS.
85 void HandleRequestCrashes(const base::ListValue
* args
);
87 #if defined(OS_CHROMEOS)
88 // Asynchronously triggers crash uploading. Called from JS.
89 void HandleRequestUploads(const base::ListValue
* args
);
92 // Sends the recent crashes list JS.
95 scoped_refptr
<CrashUploadList
> upload_list_
;
99 DISALLOW_COPY_AND_ASSIGN(CrashesDOMHandler
);
102 CrashesDOMHandler::CrashesDOMHandler()
103 : list_available_(false), first_load_(true) {
104 upload_list_
= CreateCrashUploadList(this);
107 CrashesDOMHandler::~CrashesDOMHandler() {
108 upload_list_
->ClearDelegate();
111 void CrashesDOMHandler::RegisterMessages() {
112 upload_list_
->LoadUploadListAsynchronously();
113 web_ui()->RegisterMessageCallback("requestCrashList",
114 base::Bind(&CrashesDOMHandler::HandleRequestCrashes
,
115 base::Unretained(this)));
117 #if defined(OS_CHROMEOS)
118 web_ui()->RegisterMessageCallback("requestCrashUpload",
119 base::Bind(&CrashesDOMHandler::HandleRequestUploads
,
120 base::Unretained(this)));
124 void CrashesDOMHandler::HandleRequestCrashes(const base::ListValue
* args
) {
130 list_available_
= false;
131 upload_list_
->LoadUploadListAsynchronously();
135 #if defined(OS_CHROMEOS)
136 void CrashesDOMHandler::HandleRequestUploads(const base::ListValue
* args
) {
137 chromeos::DebugDaemonClient
* debugd_client
=
138 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
139 DCHECK(debugd_client
);
141 debugd_client
->UploadCrashes();
145 void CrashesDOMHandler::OnUploadListAvailable() {
146 list_available_
= true;
151 void CrashesDOMHandler::UpdateUI() {
152 bool crash_reporting_enabled
=
153 ChromeMetricsServiceAccessor::IsCrashReportingEnabled();
154 base::ListValue crash_list
;
155 bool system_crash_reporter
= false;
157 #if defined(OS_CHROMEOS)
158 // Chrome OS has a system crash reporter.
159 system_crash_reporter
= true;
162 if (crash_reporting_enabled
) {
163 std::vector
<CrashUploadList::UploadInfo
> crashes
;
164 upload_list_
->GetUploads(50, &crashes
);
166 for (std::vector
<CrashUploadList::UploadInfo
>::iterator i
= crashes
.begin();
167 i
!= crashes
.end(); ++i
) {
168 base::DictionaryValue
* crash
= new base::DictionaryValue();
169 crash
->SetString("id", i
->id
);
170 crash
->SetString("time", base::TimeFormatFriendlyDateAndTime(i
->time
));
171 crash
->SetString("local_id", i
->local_id
);
172 crash_list
.Append(crash
);
176 base::FundamentalValue
enabled(crash_reporting_enabled
);
177 base::FundamentalValue
dynamic_backend(system_crash_reporter
);
179 base::StringValue
version(version_info::GetVersionNumber());
181 web_ui()->CallJavascriptFunction("updateCrashList", enabled
, dynamic_backend
,
182 crash_list
, version
);
187 ///////////////////////////////////////////////////////////////////////////////
191 ///////////////////////////////////////////////////////////////////////////////
193 CrashesUI::CrashesUI(content::WebUI
* web_ui
) : WebUIController(web_ui
) {
194 web_ui
->AddMessageHandler(new CrashesDOMHandler());
196 // Set up the chrome://crashes/ source.
197 Profile
* profile
= Profile::FromWebUI(web_ui
);
198 content::WebUIDataSource::Add(profile
, CreateCrashesUIHTMLSource());
202 base::RefCountedMemory
* CrashesUI::GetFaviconResourceBytes(
203 ui::ScaleFactor scale_factor
) {
204 return ResourceBundle::GetSharedInstance().
205 LoadDataResourceBytesForScale(IDR_SAD_FAVICON
, scale_factor
);