• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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/chromeos/system_info_ui.h"
6 
7 #include "base/callback.h"
8 #include "base/memory/weak_ptr.h"
9 #include "base/message_loop.h"
10 #include "base/path_service.h"
11 #include "base/string_piece.h"
12 #include "base/string_util.h"
13 #include "base/threading/thread.h"
14 #include "base/time.h"
15 #include "base/utf_string_conversions.h"
16 #include "base/values.h"
17 #include "chrome/browser/chromeos/cros/cros_library.h"
18 #include "chrome/browser/chromeos/cros/syslogs_library.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/jstemplate_builder.h"
23 #include "chrome/common/url_constants.h"
24 #include "content/browser/browser_thread.h"
25 #include "content/browser/tab_contents/tab_contents.h"
26 #include "grit/browser_resources.h"
27 #include "grit/chromium_strings.h"
28 #include "grit/generated_resources.h"
29 #include "grit/locale_settings.h"
30 #include "net/base/directory_lister.h"
31 #include "net/base/escape.h"
32 #include "ui/base/l10n/l10n_util.h"
33 #include "ui/base/resource/resource_bundle.h"
34 
35 class SystemInfoUIHTMLSource : public ChromeURLDataManager::DataSource {
36  public:
37   SystemInfoUIHTMLSource();
38 
39   // Called when the network layer has requested a resource underneath
40   // the path we registered.
41   virtual void StartDataRequest(const std::string& path,
42                                 bool is_incognito,
43                                 int request_id);
GetMimeType(const std::string &) const44   virtual std::string GetMimeType(const std::string&) const {
45     return "text/html";
46   }
47 
48  private:
~SystemInfoUIHTMLSource()49   ~SystemInfoUIHTMLSource() {}
50 
51   void SyslogsComplete(chromeos::LogDictionaryType* sys_info,
52                        std::string* ignored_content);
53 
54   CancelableRequestConsumer consumer_;
55 
56   // Stored data from StartDataRequest()
57   std::string path_;
58   int request_id_;
59 
60   DISALLOW_COPY_AND_ASSIGN(SystemInfoUIHTMLSource);
61 };
62 
63 // The handler for Javascript messages related to the "system" view.
64 class SystemInfoHandler : public WebUIMessageHandler,
65                           public base::SupportsWeakPtr<SystemInfoHandler> {
66  public:
67   SystemInfoHandler();
68   virtual ~SystemInfoHandler();
69 
70   // WebUIMessageHandler implementation.
71   virtual WebUIMessageHandler* Attach(WebUI* web_ui);
72   virtual void RegisterMessages();
73 
74  private:
75   DISALLOW_COPY_AND_ASSIGN(SystemInfoHandler);
76 };
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 //
80 // SystemInfoUIHTMLSource
81 //
82 ////////////////////////////////////////////////////////////////////////////////
83 
SystemInfoUIHTMLSource()84 SystemInfoUIHTMLSource::SystemInfoUIHTMLSource()
85     : DataSource(chrome::kChromeUISystemInfoHost, MessageLoop::current()),
86       request_id_(0) {
87 }
88 
StartDataRequest(const std::string & path,bool is_incognito,int request_id)89 void SystemInfoUIHTMLSource::StartDataRequest(const std::string& path,
90                                               bool is_incognito,
91                                               int request_id) {
92   path_ = path;
93   request_id_ = request_id;
94 
95   chromeos::SyslogsLibrary* syslogs_lib =
96       chromeos::CrosLibrary::Get()->GetSyslogsLibrary();
97   if (syslogs_lib) {
98     syslogs_lib->RequestSyslogs(
99         false, false,
100         &consumer_,
101         NewCallback(this, &SystemInfoUIHTMLSource::SyslogsComplete));
102   }
103 }
104 
SyslogsComplete(chromeos::LogDictionaryType * sys_info,std::string * ignored_content)105 void SystemInfoUIHTMLSource::SyslogsComplete(
106     chromeos::LogDictionaryType* sys_info,
107     std::string* ignored_content) {
108   DCHECK(!ignored_content);
109 
110   DictionaryValue strings;
111   strings.SetString("title", l10n_util::GetStringUTF16(IDS_ABOUT_SYS_TITLE));
112   strings.SetString("description",
113                     l10n_util::GetStringUTF16(IDS_ABOUT_SYS_DESC));
114   strings.SetString("table_title",
115                     l10n_util::GetStringUTF16(IDS_ABOUT_SYS_TABLE_TITLE));
116   strings.SetString("expand_all_btn",
117                     l10n_util::GetStringUTF16(IDS_ABOUT_SYS_EXPAND_ALL));
118   strings.SetString("collapse_all_btn",
119                     l10n_util::GetStringUTF16(IDS_ABOUT_SYS_COLLAPSE_ALL));
120   strings.SetString("expand_btn",
121                     l10n_util::GetStringUTF16(IDS_ABOUT_SYS_EXPAND));
122   strings.SetString("collapse_btn",
123                     l10n_util::GetStringUTF16(IDS_ABOUT_SYS_COLLAPSE));
124   SetFontAndTextDirection(&strings);
125 
126   if (sys_info) {
127      ListValue* details = new ListValue();
128      strings.Set("details", details);
129      chromeos::LogDictionaryType::iterator it;
130      for (it = sys_info->begin(); it != sys_info->end(); ++it) {
131        DictionaryValue* val = new DictionaryValue;
132        val->SetString("stat_name", it->first);
133        val->SetString("stat_value", it->second);
134        details->Append(val);
135      }
136      strings.SetString("anchor", path_);
137      delete sys_info;
138   }
139   static const base::StringPiece systeminfo_html(
140       ResourceBundle::GetSharedInstance().GetRawDataResource(
141           IDR_ABOUT_SYS_HTML));
142   const std::string full_html = jstemplate_builder::GetTemplatesHtml(
143       systeminfo_html, &strings, "t" /* template root node id */);
144 
145   scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
146   html_bytes->data.resize(full_html.size());
147   std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin());
148 
149   SendResponse(request_id_, html_bytes);
150 }
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 //
154 // SystemInfoHandler
155 //
156 ////////////////////////////////////////////////////////////////////////////////
SystemInfoHandler()157 SystemInfoHandler::SystemInfoHandler() {
158 }
159 
~SystemInfoHandler()160 SystemInfoHandler::~SystemInfoHandler() {
161 }
162 
Attach(WebUI * web_ui)163 WebUIMessageHandler* SystemInfoHandler::Attach(WebUI* web_ui) {
164   // TODO(stevenjb): customize handler attach if needed...
165   return WebUIMessageHandler::Attach(web_ui);
166 }
167 
RegisterMessages()168 void SystemInfoHandler::RegisterMessages() {
169   // TODO(stevenjb): add message registration, callbacks...
170 }
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 //
174 // SystemInfoUI
175 //
176 ////////////////////////////////////////////////////////////////////////////////
177 
SystemInfoUI(TabContents * contents)178 SystemInfoUI::SystemInfoUI(TabContents* contents) : WebUI(contents) {
179   SystemInfoHandler* handler = new SystemInfoHandler();
180   AddMessageHandler((handler)->Attach(this));
181   SystemInfoUIHTMLSource* html_source = new SystemInfoUIHTMLSource();
182 
183   // Set up the chrome://system/ source.
184   contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source);
185 }
186