Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / webui / system_info_ui.cc
blobcb840cb5d821b28e1f2a68afbad2f0dc6e42f886
1 // Copyright 2013 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/system_info_ui.h"
7 #include "base/bind.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/strings/string_piece.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/threading/thread.h"
18 #include "base/time/time.h"
19 #include "base/values.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/feedback/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 "chrome/grit/chromium_strings.h"
26 #include "chrome/grit/generated_resources.h"
27 #include "chrome/grit/locale_settings.h"
28 #include "content/public/browser/url_data_source.h"
29 #include "content/public/browser/web_contents.h"
30 #include "content/public/browser/web_ui.h"
31 #include "content/public/browser/web_ui_message_handler.h"
32 #include "grit/browser_resources.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;
42 using system_logs::SystemLogsResponse;
43 using system_logs::AboutSystemLogsFetcher;
45 class SystemInfoUIHTMLSource : public content::URLDataSource{
46 public:
47 SystemInfoUIHTMLSource();
49 // content::URLDataSource implementation.
50 std::string GetSource() const override;
51 void StartDataRequest(
52 const std::string& path,
53 int render_process_id,
54 int render_frame_id,
55 const content::URLDataSource::GotDataCallback& callback) override;
56 std::string GetMimeType(const std::string&) const override {
57 return "text/html";
59 bool ShouldAddContentSecurityPolicy() const override { return false; }
61 private:
62 ~SystemInfoUIHTMLSource() override {}
64 void SysInfoComplete(scoped_ptr<SystemLogsResponse> response);
65 void RequestComplete();
66 void WaitForData();
68 // Stored data from StartDataRequest()
69 std::string path_;
70 content::URLDataSource::GotDataCallback callback_;
72 scoped_ptr<SystemLogsResponse> response_;
73 base::WeakPtrFactory<SystemInfoUIHTMLSource> weak_ptr_factory_;
74 DISALLOW_COPY_AND_ASSIGN(SystemInfoUIHTMLSource);
77 // The handler for Javascript messages related to the "system" view.
78 class SystemInfoHandler : public WebUIMessageHandler,
79 public base::SupportsWeakPtr<SystemInfoHandler> {
80 public:
81 SystemInfoHandler();
82 ~SystemInfoHandler() override;
84 // WebUIMessageHandler implementation.
85 void RegisterMessages() override;
87 private:
88 DISALLOW_COPY_AND_ASSIGN(SystemInfoHandler);
91 ////////////////////////////////////////////////////////////////////////////////
93 // SystemInfoUIHTMLSource
95 ////////////////////////////////////////////////////////////////////////////////
97 SystemInfoUIHTMLSource::SystemInfoUIHTMLSource() : weak_ptr_factory_(this) {}
99 std::string SystemInfoUIHTMLSource::GetSource() const {
100 return chrome::kChromeUISystemInfoHost;
103 void SystemInfoUIHTMLSource::StartDataRequest(
104 const std::string& path,
105 int render_process_id,
106 int render_frame_id,
107 const content::URLDataSource::GotDataCallback& callback) {
108 path_ = path;
109 callback_ = callback;
111 AboutSystemLogsFetcher* fetcher = new AboutSystemLogsFetcher();
112 fetcher->Fetch(base::Bind(&SystemInfoUIHTMLSource::SysInfoComplete,
113 weak_ptr_factory_.GetWeakPtr()));
117 void SystemInfoUIHTMLSource::SysInfoComplete(
118 scoped_ptr<SystemLogsResponse> sys_info) {
119 response_ = sys_info.Pass();
120 RequestComplete();
123 void SystemInfoUIHTMLSource::RequestComplete() {
124 base::DictionaryValue strings;
125 strings.SetString("title", l10n_util::GetStringUTF16(IDS_ABOUT_SYS_TITLE));
126 strings.SetString("description",
127 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_DESC));
128 strings.SetString("tableTitle",
129 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_TABLE_TITLE));
130 strings.SetString(
131 "logFileTableTitle",
132 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_LOG_FILE_TABLE_TITLE));
133 strings.SetString("expandAllBtn",
134 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_EXPAND_ALL));
135 strings.SetString("collapseAllBtn",
136 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_COLLAPSE_ALL));
137 strings.SetString("expandBtn",
138 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_EXPAND));
139 strings.SetString("collapseBtn",
140 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_COLLAPSE));
141 strings.SetString("parseError",
142 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_PARSE_ERROR));
144 const std::string& app_locale = g_browser_process->GetApplicationLocale();
145 webui::SetLoadTimeDataDefaults(app_locale, &strings);
147 if (response_.get()) {
148 base::ListValue* details = new base::ListValue();
149 strings.Set("details", details);
150 for (SystemLogsResponse::const_iterator it = response_->begin();
151 it != response_->end();
152 ++it) {
153 base::DictionaryValue* val = new base::DictionaryValue;
154 val->SetString("statName", it->first);
155 val->SetString("statValue", it->second);
156 details->Append(val);
159 static const base::StringPiece systeminfo_html(
160 ResourceBundle::GetSharedInstance().GetRawDataResource(
161 IDR_ABOUT_SYS_HTML));
162 std::string full_html = webui::GetI18nTemplateHtml(systeminfo_html, &strings);
163 callback_.Run(base::RefCountedString::TakeString(&full_html));
166 ////////////////////////////////////////////////////////////////////////////////
168 // SystemInfoHandler
170 ////////////////////////////////////////////////////////////////////////////////
171 SystemInfoHandler::SystemInfoHandler() {
174 SystemInfoHandler::~SystemInfoHandler() {
177 void SystemInfoHandler::RegisterMessages() {
178 // TODO(stevenjb): add message registration, callbacks...
181 ////////////////////////////////////////////////////////////////////////////////
183 // SystemInfoUI
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);