- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / chromeos / diagnostics / diagnostics_ui.cc
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 // Key for devices dictionary in JSON returned from GetNetworkStatus.
26 const char kDevicesKey[] = "devices";
27
28 // JS API callback names.
29 const char kJsApiSetDeviceStatus[] = "diag.DiagPage.setDeviceStatus";
30 const char kJsApiSetTestICMPStatus[] = "diag.DiagPage.setTestICMPStatus";
31
32 ////////////////////////////////////////////////////////////////////////////////
33 // DiagnosticsHandler
34
35 // Class to handle messages from chrome://diagnostics.
36 class DiagnosticsWebUIHandler : public content::WebUIMessageHandler {
37  public:
38   DiagnosticsWebUIHandler()
39       : weak_ptr_factory_(this) {
40   }
41   virtual ~DiagnosticsWebUIHandler() {}
42
43  private:
44   // WebUIMessageHandler implementation.
45   virtual void RegisterMessages() OVERRIDE;
46
47   // Called by JS layer to get network interfaces status.
48   void GetNetworkInterfaces(const base::ListValue* args);
49
50   // Called by JS layer to test ICMP connectivity to a specified host.
51   void TestICMP(const base::ListValue* args);
52
53   // Called when GetNetworkStatus() is complete.
54   // |succeeded|: information was obtained successfully.
55   // |status|: network interfaces information in json. See
56   //      DebugDaemonClient::GetNetworkStatus() for details.
57   void OnGetNetworkStatus(bool succeeded, const std::string& status);
58
59   // Called when TestICMP() is complete.
60   // |succeeded|: information was obtained successfully.
61   // |status|: information about ICMP connectivity in json. See
62   //      DebugDaemonClient::TestICMP() for details.
63   void OnTestICMP(bool succeeded, const std::string& status);
64
65   base::WeakPtrFactory<DiagnosticsWebUIHandler> weak_ptr_factory_;
66   DISALLOW_COPY_AND_ASSIGN(DiagnosticsWebUIHandler);
67 };
68
69 void DiagnosticsWebUIHandler::RegisterMessages() {
70   web_ui()->RegisterMessageCallback(
71       "getNetworkInterfaces",
72       base::Bind(&DiagnosticsWebUIHandler::GetNetworkInterfaces,
73                  weak_ptr_factory_.GetWeakPtr()));
74   web_ui()->RegisterMessageCallback(
75       "testICMP",
76       base::Bind(&DiagnosticsWebUIHandler::TestICMP,
77                  weak_ptr_factory_.GetWeakPtr()));
78 }
79
80 void DiagnosticsWebUIHandler::GetNetworkInterfaces(
81     const base::ListValue* args) {
82   chromeos::DebugDaemonClient* debugd_client =
83       chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
84   DCHECK(debugd_client);
85
86   debugd_client->GetNetworkStatus(
87       base::Bind(&DiagnosticsWebUIHandler::OnGetNetworkStatus,
88                  weak_ptr_factory_.GetWeakPtr()));
89 }
90
91 void DiagnosticsWebUIHandler::TestICMP(const base::ListValue* args) {
92   chromeos::DebugDaemonClient* debugd_client =
93       chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
94   DCHECK(debugd_client);
95   DCHECK(args);
96   DCHECK_EQ(1u, args->GetSize());
97   if (!args || args->GetSize() != 1)
98     return;
99
100   std::string host_address;
101   if (!args->GetString(0, &host_address))
102     return;
103
104   debugd_client->TestICMP(host_address,
105                           base::Bind(&DiagnosticsWebUIHandler::OnTestICMP,
106                                      weak_ptr_factory_.GetWeakPtr()));
107 }
108
109 void DiagnosticsWebUIHandler::OnGetNetworkStatus(
110     bool succeeded, const std::string& status) {
111   if (!succeeded)
112     return;
113   scoped_ptr<Value> parsed_value(base::JSONReader::Read(status));
114   if (parsed_value.get() && parsed_value->IsType(Value::TYPE_DICTIONARY)) {
115     base::DictionaryValue* result =
116         static_cast<DictionaryValue*>(parsed_value.get());
117     base::DictionaryValue* devices_info;
118     if (!result->GetDictionary(std::string(kDevicesKey), &devices_info)) {
119       NOTREACHED() <<
120           "Received improperly formatted result from GetNetworkStatus";
121     }
122     web_ui()->CallJavascriptFunction(kJsApiSetDeviceStatus, *devices_info);
123   }
124 }
125
126 void DiagnosticsWebUIHandler::OnTestICMP(
127     bool succeeded, const std::string& status) {
128   if (!succeeded)
129     return;
130   scoped_ptr<Value> parsed_value(base::JSONReader::Read(status));
131   if (parsed_value.get() && parsed_value->IsType(Value::TYPE_DICTIONARY)) {
132     base::DictionaryValue* result =
133         static_cast<DictionaryValue*>(parsed_value.get());
134     web_ui()->CallJavascriptFunction(kJsApiSetTestICMPStatus, *result);
135   }
136 }
137
138 }  // namespace
139
140 ////////////////////////////////////////////////////////////////////////////////
141 // DiagnosticsUI
142
143 DiagnosticsUI::DiagnosticsUI(content::WebUI* web_ui)
144     : WebUIController(web_ui) {
145   web_ui->AddMessageHandler(new DiagnosticsWebUIHandler());
146
147   content::WebUIDataSource* source =
148       content::WebUIDataSource::Create(chrome::kChromeUIDiagnosticsHost);
149   source->SetJsonPath("strings.js");
150   source->AddResourcePath("main.css", IDR_DIAGNOSTICS_MAIN_CSS);
151   source->AddResourcePath("main.js", IDR_DIAGNOSTICS_MAIN_JS);
152   source->AddResourcePath("fail.png", IDR_DIAGNOSTICS_IMAGES_FAIL);
153   source->AddResourcePath("tick.png", IDR_DIAGNOSTICS_IMAGES_TICK);
154   source->AddResourcePath("warning.png", IDR_DIAGNOSTICS_IMAGES_WARNING);
155   source->AddLocalizedString("diagnostics", IDS_DIAGNOSTICS_DIAGNOSTICS_TITLE);
156   source->AddLocalizedString("refresh", IDS_DIAGNOSTICS_REFRESH);
157   source->AddLocalizedString("choose-adapter", IDS_DIAGNOSTICS_CHOOSE_ADAPTER);
158   source->AddLocalizedString("connectivity",
159                              IDS_DIAGNOSTICS_CONNECTIVITY_TITLE);
160   source->AddLocalizedString("loading", IDS_DIAGNOSTICS_LOADING);
161   source->AddLocalizedString("wifi", IDS_DIAGNOSTICS_ADAPTER_WIFI);
162   source->AddLocalizedString("ethernet1", IDS_DIAGNOSTICS_ADAPTER_ETHERNET1);
163   source->AddLocalizedString("ethernet2", IDS_DIAGNOSTICS_ADAPTER_ETHERNET2);
164   source->AddLocalizedString("3g", IDS_DIAGNOSTICS_ADAPTER_3G);
165   source->AddLocalizedString("testing-hardware",
166                              IDS_DIAGNOSTICS_TESTING_HARDWARE);
167   source->AddLocalizedString("testing-connection-to-router",
168                              IDS_DIAGNOSTICS_TESTING_CONNECTION_TO_ROUTER);
169   source->AddLocalizedString("testing-connection-to-internet",
170                              IDS_DIAGNOSTICS_TESTING_CONNECTION_TO_INTERNET);
171   source->AddLocalizedString("adapter-disabled",
172                              IDS_DIAGNOSTICS_ADAPTER_DISABLED);
173   source->AddLocalizedString("adapter-no-ip",
174                              IDS_DIAGNOSTICS_ADAPTER_NO_IP);
175   source->AddLocalizedString("gateway-not-connected-to-internet",
176                              IDS_DIAGNOSTICS_GATEWAY_NOT_CONNECTED_TO_INTERNET);
177   source->AddLocalizedString("enable-adapter",
178                              IDS_DIAGNOSTICS_ENABLE_ADAPTER);
179   source->AddLocalizedString("fix-no-ip-wifi",
180                              IDS_DIAGNOSTICS_FIX_NO_IP_WIFI);
181   source->AddLocalizedString("fix-no-ip-ethernet",
182                              IDS_DIAGNOSTICS_FIX_NO_IP_ETHERNET);
183   source->AddLocalizedString("fix-no-ip-3g",
184                              IDS_DIAGNOSTICS_FIX_NO_IP_3G);
185   source->AddLocalizedString("fix-gateway-connection",
186                              IDS_DIAGNOSTICS_FIX_GATEWAY_CONNECTION);
187   source->SetDefaultResource(IDR_DIAGNOSTICS_MAIN_HTML);
188
189   Profile* profile = Profile::FromWebUI(web_ui);
190   content::WebUIDataSource::Add(profile, source);
191 }
192
193 }  // namespace chromeos