• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/ui/webui/crashes_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/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/chrome_version_info.h"
19 #include "chrome/common/url_constants.h"
20 #include "chrome/grit/chromium_strings.h"
21 #include "chrome/grit/generated_resources.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"
29 
30 #if defined(OS_CHROMEOS)
31 #include "chromeos/dbus/dbus_thread_manager.h"
32 #include "chromeos/dbus/debug_daemon_client.h"
33 #endif
34 
35 using content::WebContents;
36 using content::WebUIMessageHandler;
37 
38 namespace {
39 
CreateCrashesUIHTMLSource()40 content::WebUIDataSource* CreateCrashesUIHTMLSource() {
41   content::WebUIDataSource* source =
42       content::WebUIDataSource::Create(chrome::kChromeUICrashesHost);
43   source->SetUseJsonJSFormatV2();
44 
45   source->AddLocalizedString("shortProductName", IDS_SHORT_PRODUCT_NAME);
46   source->AddLocalizedString("crashesTitle", IDS_CRASHES_TITLE);
47   source->AddLocalizedString("crashCountFormat",
48                              IDS_CRASHES_CRASH_COUNT_BANNER_FORMAT);
49   source->AddLocalizedString("crashHeaderFormat",
50                              IDS_CRASHES_CRASH_HEADER_FORMAT);
51   source->AddLocalizedString("crashTimeFormat", IDS_CRASHES_CRASH_TIME_FORMAT);
52   source->AddLocalizedString("bugLinkText", IDS_CRASHES_BUG_LINK_LABEL);
53   source->AddLocalizedString("noCrashesMessage",
54                              IDS_CRASHES_NO_CRASHES_MESSAGE);
55   source->AddLocalizedString("disabledHeader", IDS_CRASHES_DISABLED_HEADER);
56   source->AddLocalizedString("disabledMessage", IDS_CRASHES_DISABLED_MESSAGE);
57   source->AddLocalizedString("uploadCrashesLinkText",
58                              IDS_CRASHES_UPLOAD_MESSAGE);
59   source->SetJsonPath("strings.js");
60   source->AddResourcePath("crashes.js", IDR_CRASHES_JS);
61   source->SetDefaultResource(IDR_CRASHES_HTML);
62   return source;
63 }
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 //
67 // CrashesDOMHandler
68 //
69 ////////////////////////////////////////////////////////////////////////////////
70 
71 // The handler for Javascript messages for the chrome://crashes/ page.
72 class CrashesDOMHandler : public WebUIMessageHandler,
73                           public CrashUploadList::Delegate {
74  public:
75   CrashesDOMHandler();
76   virtual ~CrashesDOMHandler();
77 
78   // WebUIMessageHandler implementation.
79   virtual void RegisterMessages() OVERRIDE;
80 
81   // CrashUploadList::Delegate implemenation.
82   virtual void OnUploadListAvailable() OVERRIDE;
83 
84  private:
85   // Asynchronously fetches the list of crashes. Called from JS.
86   void HandleRequestCrashes(const base::ListValue* args);
87 
88 #if defined(OS_CHROMEOS)
89   // Asynchronously triggers crash uploading. Called from JS.
90   void HandleRequestUploads(const base::ListValue* args);
91 #endif
92 
93   // Sends the recent crashes list JS.
94   void UpdateUI();
95 
96   scoped_refptr<CrashUploadList> upload_list_;
97   bool list_available_;
98   bool first_load_;
99 
100   DISALLOW_COPY_AND_ASSIGN(CrashesDOMHandler);
101 };
102 
CrashesDOMHandler()103 CrashesDOMHandler::CrashesDOMHandler()
104     : list_available_(false), first_load_(true) {
105   upload_list_ = CrashUploadList::Create(this);
106 }
107 
~CrashesDOMHandler()108 CrashesDOMHandler::~CrashesDOMHandler() {
109   upload_list_->ClearDelegate();
110 }
111 
RegisterMessages()112 void CrashesDOMHandler::RegisterMessages() {
113   upload_list_->LoadUploadListAsynchronously();
114   web_ui()->RegisterMessageCallback("requestCrashList",
115       base::Bind(&CrashesDOMHandler::HandleRequestCrashes,
116                  base::Unretained(this)));
117 
118 #if defined(OS_CHROMEOS)
119   web_ui()->RegisterMessageCallback("requestCrashUpload",
120       base::Bind(&CrashesDOMHandler::HandleRequestUploads,
121                  base::Unretained(this)));
122 #endif
123 }
124 
HandleRequestCrashes(const base::ListValue * args)125 void CrashesDOMHandler::HandleRequestCrashes(const base::ListValue* args) {
126   if (first_load_) {
127     first_load_ = false;
128     if (list_available_)
129       UpdateUI();
130   } else {
131     list_available_ = false;
132     upload_list_->LoadUploadListAsynchronously();
133   }
134 }
135 
136 #if defined(OS_CHROMEOS)
HandleRequestUploads(const base::ListValue * args)137 void CrashesDOMHandler::HandleRequestUploads(const base::ListValue* args) {
138   chromeos::DebugDaemonClient* debugd_client =
139       chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
140   DCHECK(debugd_client);
141 
142   debugd_client->UploadCrashes();
143 }
144 #endif
145 
OnUploadListAvailable()146 void CrashesDOMHandler::OnUploadListAvailable() {
147   list_available_ = true;
148   if (!first_load_)
149     UpdateUI();
150 }
151 
UpdateUI()152 void CrashesDOMHandler::UpdateUI() {
153   bool crash_reporting_enabled =
154       ChromeMetricsServiceAccessor::IsCrashReportingEnabled();
155   base::ListValue crash_list;
156   bool system_crash_reporter = false;
157 
158 #if defined(OS_CHROMEOS)
159   // Chrome OS has a system crash reporter.
160   system_crash_reporter = true;
161 #endif
162 
163   if (crash_reporting_enabled) {
164     std::vector<CrashUploadList::UploadInfo> crashes;
165     upload_list_->GetUploads(50, &crashes);
166 
167     for (std::vector<CrashUploadList::UploadInfo>::iterator i = crashes.begin();
168          i != crashes.end(); ++i) {
169       base::DictionaryValue* crash = new base::DictionaryValue();
170       crash->SetString("id", i->id);
171       crash->SetString("time", base::TimeFormatFriendlyDateAndTime(i->time));
172       crash->SetString("local_id", i->local_id);
173       crash_list.Append(crash);
174     }
175   }
176 
177   base::FundamentalValue enabled(crash_reporting_enabled);
178   base::FundamentalValue dynamic_backend(system_crash_reporter);
179 
180   const chrome::VersionInfo version_info;
181   base::StringValue version(version_info.Version());
182 
183   web_ui()->CallJavascriptFunction("updateCrashList", enabled, dynamic_backend,
184                                    crash_list, version);
185 }
186 
187 }  // namespace
188 
189 ///////////////////////////////////////////////////////////////////////////////
190 //
191 // CrashesUI
192 //
193 ///////////////////////////////////////////////////////////////////////////////
194 
CrashesUI(content::WebUI * web_ui)195 CrashesUI::CrashesUI(content::WebUI* web_ui) : WebUIController(web_ui) {
196   web_ui->AddMessageHandler(new CrashesDOMHandler());
197 
198   // Set up the chrome://crashes/ source.
199   Profile* profile = Profile::FromWebUI(web_ui);
200   content::WebUIDataSource::Add(profile, CreateCrashesUIHTMLSource());
201 }
202 
203 // static
GetFaviconResourceBytes(ui::ScaleFactor scale_factor)204 base::RefCountedMemory* CrashesUI::GetFaviconResourceBytes(
205       ui::ScaleFactor scale_factor) {
206   return ResourceBundle::GetSharedInstance().
207       LoadDataResourceBytesForScale(IDR_SAD_FAVICON, scale_factor);
208 }
209