Home | History | Annotate | Download | only in chromeos
      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/chromeos/system_info_ui.h"
      6 
      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/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/webui/jstemplate_builder.h"
     38 #include "ui/webui/web_ui_util.h"
     39 
     40 using content::WebContents;
     41 using content::WebUIMessageHandler;
     42 
     43 namespace chromeos {
     44 
     45 class SystemInfoUIHTMLSource : public content::URLDataSource{
     46  public:
     47   SystemInfoUIHTMLSource();
     48 
     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,
     54       int render_view_id,
     55       const content::URLDataSource::GotDataCallback& callback) OVERRIDE;
     56   virtual std::string GetMimeType(const std::string&) const OVERRIDE {
     57     return "text/html";
     58   }
     59   virtual bool ShouldAddContentSecurityPolicy() const OVERRIDE {
     60     return false;
     61   }
     62 
     63  private:
     64   virtual ~SystemInfoUIHTMLSource() {}
     65 
     66   void SysInfoComplete(scoped_ptr<SystemLogsResponse> response);
     67   void RequestComplete();
     68   void WaitForData();
     69 
     70   // Stored data from StartDataRequest()
     71   std::string path_;
     72   content::URLDataSource::GotDataCallback callback_;
     73 
     74   scoped_ptr<SystemLogsResponse> response_;
     75   base::WeakPtrFactory<SystemInfoUIHTMLSource> weak_ptr_factory_;
     76   DISALLOW_COPY_AND_ASSIGN(SystemInfoUIHTMLSource);
     77 };
     78 
     79 // The handler for Javascript messages related to the "system" view.
     80 class SystemInfoHandler : public WebUIMessageHandler,
     81                           public base::SupportsWeakPtr<SystemInfoHandler> {
     82  public:
     83   SystemInfoHandler();
     84   virtual ~SystemInfoHandler();
     85 
     86   // WebUIMessageHandler implementation.
     87   virtual void RegisterMessages() OVERRIDE;
     88 
     89  private:
     90   DISALLOW_COPY_AND_ASSIGN(SystemInfoHandler);
     91 };
     92 
     93 ////////////////////////////////////////////////////////////////////////////////
     94 //
     95 // SystemInfoUIHTMLSource
     96 //
     97 ////////////////////////////////////////////////////////////////////////////////
     98 
     99 SystemInfoUIHTMLSource::SystemInfoUIHTMLSource() : weak_ptr_factory_(this) {}
    100 
    101 std::string SystemInfoUIHTMLSource::GetSource() const {
    102   return chrome::kChromeUISystemInfoHost;
    103 }
    104 
    105 void SystemInfoUIHTMLSource::StartDataRequest(
    106     const std::string& path,
    107     int render_process_id,
    108     int render_view_id,
    109     const content::URLDataSource::GotDataCallback& callback) {
    110   path_ = path;
    111   callback_ = callback;
    112 
    113   AboutSystemLogsFetcher* fetcher = new AboutSystemLogsFetcher();
    114   fetcher->Fetch(base::Bind(&SystemInfoUIHTMLSource::SysInfoComplete,
    115                             weak_ptr_factory_.GetWeakPtr()));
    116 }
    117 
    118 
    119 void SystemInfoUIHTMLSource::SysInfoComplete(
    120     scoped_ptr<SystemLogsResponse> sys_info) {
    121   response_ = sys_info.Pass();
    122   RequestComplete();
    123 }
    124 
    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("table_title",
    131                     l10n_util::GetStringUTF16(IDS_ABOUT_SYS_TABLE_TITLE));
    132   strings.SetString("expand_all_btn",
    133                     l10n_util::GetStringUTF16(IDS_ABOUT_SYS_EXPAND_ALL));
    134   strings.SetString("collapse_all_btn",
    135                     l10n_util::GetStringUTF16(IDS_ABOUT_SYS_COLLAPSE_ALL));
    136   strings.SetString("expand_btn",
    137                     l10n_util::GetStringUTF16(IDS_ABOUT_SYS_EXPAND));
    138   strings.SetString("collapse_btn",
    139                     l10n_util::GetStringUTF16(IDS_ABOUT_SYS_COLLAPSE));
    140   webui::SetFontAndTextDirection(&strings);
    141   if (response_.get()) {
    142     ListValue* details = new ListValue();
    143     strings.Set("details", details);
    144     for (SystemLogsResponse::const_iterator it = response_->begin();
    145          it != response_->end();
    146          ++it) {
    147       DictionaryValue* val = new DictionaryValue;
    148       val->SetString("stat_name", it->first);
    149       val->SetString("stat_value", it->second);
    150       details->Append(val);
    151     }
    152     strings.SetString("anchor", path_);
    153   }
    154   static const base::StringPiece systeminfo_html(
    155       ResourceBundle::GetSharedInstance().GetRawDataResource(
    156           IDR_ABOUT_SYS_HTML));
    157   std::string full_html = webui::GetTemplatesHtml(
    158       systeminfo_html, &strings, "t" /* template root node id */);
    159   callback_.Run(base::RefCountedString::TakeString(&full_html));
    160 }
    161 
    162 ////////////////////////////////////////////////////////////////////////////////
    163 //
    164 // SystemInfoHandler
    165 //
    166 ////////////////////////////////////////////////////////////////////////////////
    167 SystemInfoHandler::SystemInfoHandler() {
    168 }
    169 
    170 SystemInfoHandler::~SystemInfoHandler() {
    171 }
    172 
    173 void SystemInfoHandler::RegisterMessages() {
    174   // TODO(stevenjb): add message registration, callbacks...
    175 }
    176 
    177 ////////////////////////////////////////////////////////////////////////////////
    178 //
    179 // SystemInfoUI
    180 //
    181 ////////////////////////////////////////////////////////////////////////////////
    182 
    183 SystemInfoUI::SystemInfoUI(content::WebUI* web_ui) : WebUIController(web_ui) {
    184   SystemInfoHandler* handler = new SystemInfoHandler();
    185   web_ui->AddMessageHandler(handler);
    186   SystemInfoUIHTMLSource* html_source = new SystemInfoUIHTMLSource();
    187 
    188   // Set up the chrome://system/ source.
    189   Profile* profile = Profile::FromWebUI(web_ui);
    190   content::URLDataSource::Add(profile, html_source);
    191 }
    192 
    193 }  // namespace chromeos
    194