Home | History | Annotate | Download | only in diagnostics
      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/diagnostics/diagnostics_ui.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/json/json_reader.h"
      9 #include "base/memory/weak_ptr.h"
     10 #include "base/values.h"
     11 #include "chrome/browser/profiles/profile.h"
     12 #include "chrome/common/url_constants.h"
     13 #include "chromeos/dbus/debug_daemon_client.h"
     14 #include "chromeos/dbus/dbus_thread_manager.h"
     15 #include "content/public/browser/web_ui.h"
     16 #include "content/public/browser/web_ui_data_source.h"
     17 #include "content/public/browser/web_ui_message_handler.h"
     18 #include "grit/browser_resources.h"
     19 #include "grit/generated_resources.h"
     20 
     21 namespace chromeos {
     22 
     23 namespace {
     24 
     25 // JS API callback names.
     26 const char kJsApiSetNetifStatus[] = "diag.DiagPage.setNetifStatus";
     27 const char kJsApiSetTestICMPStatus[] = "diag.DiagPage.setTestICMPStatus";
     28 
     29 ////////////////////////////////////////////////////////////////////////////////
     30 // DiagnosticsHandler
     31 
     32 // Class to handle messages from chrome://diagnostics.
     33 class DiagnosticsWebUIHandler : public content::WebUIMessageHandler {
     34  public:
     35   DiagnosticsWebUIHandler()
     36       : weak_ptr_factory_(this) {
     37   }
     38   virtual ~DiagnosticsWebUIHandler() {}
     39 
     40  private:
     41   // WebUIMessageHandler implementation.
     42   virtual void RegisterMessages() OVERRIDE;
     43 
     44   // Called by JS layer to get network interfaces status.
     45   void GetNetworkInterfaces(const base::ListValue* args);
     46 
     47   // Called by JS layer to test ICMP connectivity to a specified host.
     48   void TestICMP(const base::ListValue* args);
     49 
     50   // Called when GetNetworkInterfaces() is complete.
     51   // |succeeded|: information was obtained successfully.
     52   // |status|: network interfaces information in json. See
     53   //      DebugDaemonClient::GetNetworkInterfaces() for details.
     54   void OnGetNetworkInterfaces(bool succeeded, const std::string& status);
     55 
     56   // Called when TestICMP() is complete.
     57   // |succeeded|: information was obtained successfully.
     58   // |status|: information about ICMP connectivity in json. See
     59   //      DebugDaemonClient::TestICMP() for details.
     60   void OnTestICMP(bool succeeded, const std::string& status);
     61 
     62   base::WeakPtrFactory<DiagnosticsWebUIHandler> weak_ptr_factory_;
     63   DISALLOW_COPY_AND_ASSIGN(DiagnosticsWebUIHandler);
     64 };
     65 
     66 void DiagnosticsWebUIHandler::RegisterMessages() {
     67   web_ui()->RegisterMessageCallback(
     68       "getNetworkInterfaces",
     69       base::Bind(&DiagnosticsWebUIHandler::GetNetworkInterfaces,
     70                  weak_ptr_factory_.GetWeakPtr()));
     71   web_ui()->RegisterMessageCallback(
     72       "testICMP",
     73       base::Bind(&DiagnosticsWebUIHandler::TestICMP,
     74                  weak_ptr_factory_.GetWeakPtr()));
     75 }
     76 
     77 void DiagnosticsWebUIHandler::GetNetworkInterfaces(
     78     const base::ListValue* args) {
     79   chromeos::DebugDaemonClient* debugd_client =
     80       chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
     81   DCHECK(debugd_client);
     82 
     83   debugd_client->GetNetworkInterfaces(
     84       base::Bind(&DiagnosticsWebUIHandler::OnGetNetworkInterfaces,
     85                  weak_ptr_factory_.GetWeakPtr()));
     86 }
     87 
     88 void DiagnosticsWebUIHandler::TestICMP(const base::ListValue* args) {
     89   chromeos::DebugDaemonClient* debugd_client =
     90       chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
     91   DCHECK(debugd_client);
     92   DCHECK(args);
     93   DCHECK_EQ(1u, args->GetSize());
     94   if (!args || args->GetSize() != 1)
     95     return;
     96 
     97   std::string host_address;
     98   if (!args->GetString(0, &host_address))
     99     return;
    100 
    101   debugd_client->TestICMP(host_address,
    102                           base::Bind(&DiagnosticsWebUIHandler::OnTestICMP,
    103                                      weak_ptr_factory_.GetWeakPtr()));
    104 }
    105 
    106 void DiagnosticsWebUIHandler::OnGetNetworkInterfaces(
    107     bool succeeded, const std::string& status) {
    108   if (!succeeded)
    109     return;
    110   scoped_ptr<Value> parsed_value(base::JSONReader::Read(status));
    111   if (parsed_value.get() && parsed_value->IsType(Value::TYPE_DICTIONARY)) {
    112     base::DictionaryValue* result =
    113         static_cast<DictionaryValue*>(parsed_value.get());
    114     web_ui()->CallJavascriptFunction(kJsApiSetNetifStatus, *result);
    115   }
    116 }
    117 
    118 void DiagnosticsWebUIHandler::OnTestICMP(
    119     bool succeeded, const std::string& status) {
    120   if (!succeeded)
    121     return;
    122   scoped_ptr<Value> parsed_value(base::JSONReader::Read(status));
    123   if (parsed_value.get() && parsed_value->IsType(Value::TYPE_DICTIONARY)) {
    124     base::DictionaryValue* result =
    125         static_cast<DictionaryValue*>(parsed_value.get());
    126     web_ui()->CallJavascriptFunction(kJsApiSetTestICMPStatus, *result);
    127   }
    128 }
    129 
    130 }  // namespace
    131 
    132 ////////////////////////////////////////////////////////////////////////////////
    133 // DiagnosticsUI
    134 
    135 DiagnosticsUI::DiagnosticsUI(content::WebUI* web_ui)
    136     : WebUIController(web_ui) {
    137   web_ui->AddMessageHandler(new DiagnosticsWebUIHandler());
    138 
    139   content::WebUIDataSource* source =
    140       content::WebUIDataSource::Create(chrome::kChromeUIDiagnosticsHost);
    141   source->SetJsonPath("strings.js");
    142   source->AddResourcePath("main.css", IDR_DIAGNOSTICS_MAIN_CSS);
    143   source->AddResourcePath("main.js", IDR_DIAGNOSTICS_MAIN_JS);
    144   source->AddResourcePath("fail.png", IDR_DIAGNOSTICS_IMAGES_FAIL);
    145   source->AddResourcePath("tick.png", IDR_DIAGNOSTICS_IMAGES_TICK);
    146   source->AddResourcePath("warning.png", IDR_DIAGNOSTICS_IMAGES_WARNING);
    147   source->AddLocalizedString("diagnostics", IDS_DIAGNOSTICS_DIAGNOSTICS_TITLE);
    148   source->AddLocalizedString("refresh", IDS_DIAGNOSTICS_REFRESH);
    149   source->AddLocalizedString("choose-adapter", IDS_DIAGNOSTICS_CHOOSE_ADAPTER);
    150   source->AddLocalizedString("connectivity",
    151                              IDS_DIAGNOSTICS_CONNECTIVITY_TITLE);
    152   source->AddLocalizedString("loading", IDS_DIAGNOSTICS_LOADING);
    153   source->AddLocalizedString("wlan0", IDS_DIAGNOSTICS_ADAPTER_WLAN0);
    154   source->AddLocalizedString("eth0", IDS_DIAGNOSTICS_ADAPTER_ETH0);
    155   source->AddLocalizedString("eth1", IDS_DIAGNOSTICS_ADAPTER_ETH1);
    156   source->AddLocalizedString("wwan0", IDS_DIAGNOSTICS_ADAPTER_WWAN0);
    157   source->AddLocalizedString("testing-hardware",
    158                              IDS_DIAGNOSTICS_TESTING_HARDWARE);
    159   source->AddLocalizedString("testing-connection-to-router",
    160                              IDS_DIAGNOSTICS_TESTING_CONNECTION_TO_ROUTER);
    161   source->AddLocalizedString("testing-connection-to-internet",
    162                              IDS_DIAGNOSTICS_TESTING_CONNECTION_TO_INTERNET);
    163   source->AddLocalizedString("adapter-disabled",
    164                              IDS_DIAGNOSTICS_ADAPTER_DISABLED);
    165   source->AddLocalizedString("adapter-no-ip",
    166                              IDS_DIAGNOSTICS_ADAPTER_NO_IP);
    167   source->AddLocalizedString("gateway-not-connected-to-internet",
    168                              IDS_DIAGNOSTICS_GATEWAY_NOT_CONNECTED_TO_INTERNET);
    169   source->AddLocalizedString("enable-adapter",
    170                              IDS_DIAGNOSTICS_ENABLE_ADAPTER);
    171   source->AddLocalizedString("fix-no-ip-wifi",
    172                              IDS_DIAGNOSTICS_FIX_NO_IP_WIFI);
    173   source->AddLocalizedString("fix-no-ip-ethernet",
    174                              IDS_DIAGNOSTICS_FIX_NO_IP_ETHERNET);
    175   source->AddLocalizedString("fix-no-ip-3g",
    176                              IDS_DIAGNOSTICS_FIX_NO_IP_3G);
    177   source->AddLocalizedString("fix-gateway-connection",
    178                              IDS_DIAGNOSTICS_FIX_GATEWAY_CONNECTION);
    179   source->SetDefaultResource(IDR_DIAGNOSTICS_MAIN_HTML);
    180 
    181   Profile* profile = Profile::FromWebUI(web_ui);
    182   content::WebUIDataSource::Add(profile, source);
    183 }
    184 
    185 }  // namespace chromeos
    186