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/chromeos/system_info_ui.h"
8 #include "base/bind_helpers.h"
9 #include "base/memory/ref_counted_memory.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/path_service.h"
14 #include "base/strings/string_piece.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "base/threading/thread.h"
19 #include "base/time/time.h"
20 #include "base/values.h"
21 #include "chrome/browser/chromeos/system_logs/about_system_logs_fetcher.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/common/chrome_paths.h"
24 #include "chrome/common/url_constants.h"
25 #include "content/public/browser/url_data_source.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/browser/web_ui.h"
28 #include "content/public/browser/web_ui_message_handler.h"
29 #include "grit/browser_resources.h"
30 #include "grit/chromium_strings.h"
31 #include "grit/generated_resources.h"
32 #include "grit/locale_settings.h"
33 #include "net/base/directory_lister.h"
34 #include "net/base/escape.h"
35 #include "ui/base/l10n/l10n_util.h"
36 #include "ui/base/resource/resource_bundle.h"
37 #include "ui/base/webui/jstemplate_builder.h"
38 #include "ui/base/webui/web_ui_util.h"
40 using content::WebContents
;
41 using content::WebUIMessageHandler
;
45 class SystemInfoUIHTMLSource
: public content::URLDataSource
{
47 SystemInfoUIHTMLSource();
49 // content::URLDataSource implementation.
50 virtual std::string
GetSource() const OVERRIDE
;
51 virtual void StartDataRequest(
52 const std::string
& path
,
53 int render_process_id
,
55 const content::URLDataSource::GotDataCallback
& callback
) OVERRIDE
;
56 virtual std::string
GetMimeType(const std::string
&) const OVERRIDE
{
59 virtual bool ShouldAddContentSecurityPolicy() const OVERRIDE
{
64 virtual ~SystemInfoUIHTMLSource() {}
66 void SysInfoComplete(scoped_ptr
<SystemLogsResponse
> response
);
67 void RequestComplete();
70 // Stored data from StartDataRequest()
72 content::URLDataSource::GotDataCallback callback_
;
74 scoped_ptr
<SystemLogsResponse
> response_
;
75 base::WeakPtrFactory
<SystemInfoUIHTMLSource
> weak_ptr_factory_
;
76 DISALLOW_COPY_AND_ASSIGN(SystemInfoUIHTMLSource
);
79 // The handler for Javascript messages related to the "system" view.
80 class SystemInfoHandler
: public WebUIMessageHandler
,
81 public base::SupportsWeakPtr
<SystemInfoHandler
> {
84 virtual ~SystemInfoHandler();
86 // WebUIMessageHandler implementation.
87 virtual void RegisterMessages() OVERRIDE
;
90 DISALLOW_COPY_AND_ASSIGN(SystemInfoHandler
);
93 ////////////////////////////////////////////////////////////////////////////////
95 // SystemInfoUIHTMLSource
97 ////////////////////////////////////////////////////////////////////////////////
99 SystemInfoUIHTMLSource::SystemInfoUIHTMLSource() : weak_ptr_factory_(this) {}
101 std::string
SystemInfoUIHTMLSource::GetSource() const {
102 return chrome::kChromeUISystemInfoHost
;
105 void SystemInfoUIHTMLSource::StartDataRequest(
106 const std::string
& path
,
107 int render_process_id
,
109 const content::URLDataSource::GotDataCallback
& callback
) {
111 callback_
= callback
;
113 AboutSystemLogsFetcher
* fetcher
= new AboutSystemLogsFetcher();
114 fetcher
->Fetch(base::Bind(&SystemInfoUIHTMLSource::SysInfoComplete
,
115 weak_ptr_factory_
.GetWeakPtr()));
119 void SystemInfoUIHTMLSource::SysInfoComplete(
120 scoped_ptr
<SystemLogsResponse
> sys_info
) {
121 response_
= sys_info
.Pass();
125 void SystemInfoUIHTMLSource::RequestComplete() {
126 DictionaryValue strings
;
127 strings
.SetString("title", l10n_util::GetStringUTF16(IDS_ABOUT_SYS_TITLE
));
128 strings
.SetString("description",
129 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_DESC
));
130 strings
.SetString("tableTitle",
131 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_TABLE_TITLE
));
134 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_LOG_FILE_TABLE_TITLE
));
135 strings
.SetString("expandAllBtn",
136 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_EXPAND_ALL
));
137 strings
.SetString("collapseAllBtn",
138 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_COLLAPSE_ALL
));
139 strings
.SetString("expandBtn",
140 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_EXPAND
));
141 strings
.SetString("collapseBtn",
142 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_COLLAPSE
));
143 strings
.SetString("parseError",
144 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_PARSE_ERROR
));
145 webui::SetFontAndTextDirection(&strings
);
146 if (response_
.get()) {
147 ListValue
* details
= new ListValue();
148 strings
.Set("details", details
);
149 for (SystemLogsResponse::const_iterator it
= response_
->begin();
150 it
!= response_
->end();
152 DictionaryValue
* val
= new DictionaryValue
;
153 val
->SetString("statName", it
->first
);
154 val
->SetString("statValue", it
->second
);
155 details
->Append(val
);
158 static const base::StringPiece
systeminfo_html(
159 ResourceBundle::GetSharedInstance().GetRawDataResource(
160 IDR_ABOUT_SYS_HTML
));
161 std::string full_html
= webui::GetTemplatesHtml(
162 systeminfo_html
, &strings
, "t" /* template root node id */);
163 callback_
.Run(base::RefCountedString::TakeString(&full_html
));
166 ////////////////////////////////////////////////////////////////////////////////
170 ////////////////////////////////////////////////////////////////////////////////
171 SystemInfoHandler::SystemInfoHandler() {
174 SystemInfoHandler::~SystemInfoHandler() {
177 void SystemInfoHandler::RegisterMessages() {
178 // TODO(stevenjb): add message registration, callbacks...
181 ////////////////////////////////////////////////////////////////////////////////
185 ////////////////////////////////////////////////////////////////////////////////
187 SystemInfoUI::SystemInfoUI(content::WebUI
* web_ui
) : WebUIController(web_ui
) {
188 SystemInfoHandler
* handler
= new SystemInfoHandler();
189 web_ui
->AddMessageHandler(handler
);
190 SystemInfoUIHTMLSource
* html_source
= new SystemInfoUIHTMLSource();
192 // Set up the chrome://system/ source.
193 Profile
* profile
= Profile::FromWebUI(web_ui
);
194 content::URLDataSource::Add(profile
, html_source
);
197 } // namespace chromeos