OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/chromeos/network_ui.h" | 5 #include "chrome/browser/ui/webui/chromeos/network_ui.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
| 9 #include "base/memory/weak_ptr.h" |
9 #include "base/values.h" | 10 #include "base/values.h" |
10 #include "chrome/browser/ui/webui/chromeos/network_config_message_handler.h" | 11 #include "chrome/browser/extensions/tab_helper.h" |
11 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
12 #include "chrome/grit/generated_resources.h" | 13 #include "chrome/grit/generated_resources.h" |
| 14 #include "chromeos/device_event_log.h" |
| 15 #include "chromeos/network/device_state.h" |
| 16 #include "chromeos/network/network_configuration_handler.h" |
| 17 #include "chromeos/network/network_state.h" |
| 18 #include "chromeos/network/network_state_handler.h" |
13 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
14 #include "content/public/browser/web_ui.h" | 20 #include "content/public/browser/web_ui.h" |
15 #include "content/public/browser/web_ui_data_source.h" | 21 #include "content/public/browser/web_ui_data_source.h" |
| 22 #include "content/public/browser/web_ui_message_handler.h" |
16 #include "grit/browser_resources.h" | 23 #include "grit/browser_resources.h" |
| 24 #include "third_party/cros_system_api/dbus/service_constants.h" |
17 | 25 |
18 namespace chromeos { | 26 namespace chromeos { |
19 | 27 |
| 28 namespace { |
| 29 |
| 30 bool GetServicePathFromGuid(const std::string& guid, |
| 31 std::string* service_path) { |
| 32 const NetworkState* network = |
| 33 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid( |
| 34 guid); |
| 35 if (!network) |
| 36 return false; |
| 37 *service_path = network->path(); |
| 38 return true; |
| 39 } |
| 40 |
| 41 void SetDeviceProperties(base::DictionaryValue* dictionary) { |
| 42 std::string device; |
| 43 dictionary->GetStringWithoutPathExpansion(shill::kDeviceProperty, &device); |
| 44 const DeviceState* device_state = |
| 45 NetworkHandler::Get()->network_state_handler()->GetDeviceState(device); |
| 46 if (!device_state) |
| 47 return; |
| 48 |
| 49 scoped_ptr<base::DictionaryValue> device_dictionary( |
| 50 device_state->properties().DeepCopy()); |
| 51 |
| 52 if (!device_state->ip_configs().empty()) { |
| 53 // Convert IPConfig dictionary to a ListValue. |
| 54 scoped_ptr<base::ListValue> ip_configs(new base::ListValue); |
| 55 for (base::DictionaryValue::Iterator iter(device_state->ip_configs()); |
| 56 !iter.IsAtEnd(); iter.Advance()) { |
| 57 ip_configs->Append(iter.value().DeepCopy()); |
| 58 } |
| 59 device_dictionary->SetWithoutPathExpansion(shill::kIPConfigsProperty, |
| 60 ip_configs.release()); |
| 61 } |
| 62 if (!device_dictionary->empty()) |
| 63 dictionary->Set(shill::kDeviceProperty, device_dictionary.release()); |
| 64 } |
| 65 |
| 66 class NetworkConfigMessageHandler : public content::WebUIMessageHandler { |
| 67 public: |
| 68 NetworkConfigMessageHandler() : weak_ptr_factory_(this) {} |
| 69 ~NetworkConfigMessageHandler() override {} |
| 70 |
| 71 // WebUIMessageHandler implementation. |
| 72 void RegisterMessages() override { |
| 73 web_ui()->RegisterMessageCallback( |
| 74 "getShillProperties", |
| 75 base::Bind(&NetworkConfigMessageHandler::GetShillProperties, |
| 76 base::Unretained(this))); |
| 77 } |
| 78 |
| 79 private: |
| 80 void GetShillProperties(const base::ListValue* arg_list) { |
| 81 std::string guid; |
| 82 if (!arg_list->GetString(0, &guid)) { |
| 83 NOTREACHED(); |
| 84 ErrorCallback(guid, "Missing GUID in arg list", nullptr); |
| 85 return; |
| 86 } |
| 87 std::string service_path; |
| 88 if (!GetServicePathFromGuid(guid, &service_path)) { |
| 89 ErrorCallback(guid, "Error.InvalidNetworkGuid", nullptr); |
| 90 return; |
| 91 } |
| 92 NetworkHandler::Get()->network_configuration_handler()->GetProperties( |
| 93 service_path, |
| 94 base::Bind(&NetworkConfigMessageHandler::GetShillPropertiesSuccess, |
| 95 weak_ptr_factory_.GetWeakPtr()), |
| 96 base::Bind(&NetworkConfigMessageHandler::ErrorCallback, |
| 97 weak_ptr_factory_.GetWeakPtr(), guid)); |
| 98 } |
| 99 |
| 100 void GetShillPropertiesSuccess( |
| 101 const std::string& service_path, |
| 102 const base::DictionaryValue& dictionary) const { |
| 103 scoped_ptr<base::DictionaryValue> dictionary_copy(dictionary.DeepCopy()); |
| 104 |
| 105 // Set the 'ServicePath' property for debugging. |
| 106 dictionary_copy->SetStringWithoutPathExpansion("ServicePath", service_path); |
| 107 // Set the device properties for debugging. |
| 108 SetDeviceProperties(dictionary_copy.get()); |
| 109 |
| 110 base::ListValue return_arg_list; |
| 111 return_arg_list.Append(dictionary_copy.release()); |
| 112 web_ui()->CallJavascriptFunction("NetworkUI.getShillPropertiesResult", |
| 113 return_arg_list); |
| 114 } |
| 115 |
| 116 void ErrorCallback( |
| 117 const std::string& guid, |
| 118 const std::string& error_name, |
| 119 scoped_ptr<base::DictionaryValue> /* error_data */) const { |
| 120 NET_LOG(ERROR) << "Shill Error: " << error_name << " guid=" << guid; |
| 121 base::ListValue return_arg_list; |
| 122 scoped_ptr<base::DictionaryValue> dictionary; |
| 123 dictionary->SetStringWithoutPathExpansion(shill::kGuidProperty, guid); |
| 124 dictionary->SetStringWithoutPathExpansion("ShillError", error_name); |
| 125 return_arg_list.Append(dictionary.release()); |
| 126 web_ui()->CallJavascriptFunction("NetworkUI.getShillPropertiesResult", |
| 127 return_arg_list); |
| 128 } |
| 129 |
| 130 base::WeakPtrFactory<NetworkConfigMessageHandler> weak_ptr_factory_; |
| 131 |
| 132 DISALLOW_COPY_AND_ASSIGN(NetworkConfigMessageHandler); |
| 133 }; |
| 134 |
| 135 } // namespace |
| 136 |
20 NetworkUI::NetworkUI(content::WebUI* web_ui) | 137 NetworkUI::NetworkUI(content::WebUI* web_ui) |
21 : content::WebUIController(web_ui) { | 138 : content::WebUIController(web_ui) { |
22 web_ui->AddMessageHandler(new NetworkConfigMessageHandler()); | 139 web_ui->AddMessageHandler(new NetworkConfigMessageHandler()); |
23 | 140 |
| 141 // Enable extension API calls in the WebUI. |
| 142 extensions::TabHelper::CreateForWebContents(web_ui->GetWebContents()); |
| 143 |
24 content::WebUIDataSource* html = | 144 content::WebUIDataSource* html = |
25 content::WebUIDataSource::Create(chrome::kChromeUINetworkHost); | 145 content::WebUIDataSource::Create(chrome::kChromeUINetworkHost); |
26 | 146 |
27 html->AddLocalizedString("titleText", IDS_NETWORK_UI_TITLE); | 147 html->AddLocalizedString("titleText", IDS_NETWORK_UI_TITLE); |
28 html->AddLocalizedString("autoRefreshText", IDS_NETWORK_UI_AUTO_REFRESH); | 148 html->AddLocalizedString("autoRefreshText", IDS_NETWORK_UI_AUTO_REFRESH); |
29 html->AddLocalizedString("deviceLogLinkText", IDS_DEVICE_LOG_LINK_TEXT); | 149 html->AddLocalizedString("deviceLogLinkText", IDS_DEVICE_LOG_LINK_TEXT); |
30 html->AddLocalizedString("networkRefreshText", IDS_NETWORK_UI_REFRESH); | 150 html->AddLocalizedString("networkRefreshText", IDS_NETWORK_UI_REFRESH); |
31 html->AddLocalizedString("clickToExpandText", IDS_NETWORK_UI_EXPAND); | 151 html->AddLocalizedString("clickToExpandText", IDS_NETWORK_UI_EXPAND); |
32 html->AddLocalizedString("propertyFormatText", | 152 html->AddLocalizedString("propertyFormatText", |
33 IDS_NETWORK_UI_PROPERTY_FORMAT); | 153 IDS_NETWORK_UI_PROPERTY_FORMAT); |
34 | 154 |
35 html->AddLocalizedString("normalFormatOption", IDS_NETWORK_UI_FORMAT_NORMAL); | 155 html->AddLocalizedString("normalFormatOption", IDS_NETWORK_UI_FORMAT_NORMAL); |
36 html->AddLocalizedString("managedFormatOption", | 156 html->AddLocalizedString("managedFormatOption", |
37 IDS_NETWORK_UI_FORMAT_MANAGED); | 157 IDS_NETWORK_UI_FORMAT_MANAGED); |
38 html->AddLocalizedString("shillFormatOption", IDS_NETWORK_UI_FORMAT_SHILL); | 158 html->AddLocalizedString("shillFormatOption", IDS_NETWORK_UI_FORMAT_SHILL); |
39 | 159 |
40 html->AddLocalizedString("visibleNetworksLabel", | 160 html->AddLocalizedString("visibleNetworksLabel", |
41 IDS_NETWORK_UI_VISIBLE_NETWORKS); | 161 IDS_NETWORK_UI_VISIBLE_NETWORKS); |
42 html->AddLocalizedString("favoriteNetworksLabel", | 162 html->AddLocalizedString("favoriteNetworksLabel", |
43 IDS_NETWORK_UI_FAVORITE_NETWORKS); | 163 IDS_NETWORK_UI_FAVORITE_NETWORKS); |
44 | 164 |
45 html->SetJsonPath("strings.js"); | 165 html->SetJsonPath("strings.js"); |
46 html->AddResourcePath("network_config.js", IDR_NETWORK_CONFIG_JS); | |
47 html->AddResourcePath("network_ui.css", IDR_NETWORK_UI_CSS); | 166 html->AddResourcePath("network_ui.css", IDR_NETWORK_UI_CSS); |
48 html->AddResourcePath("network_ui.js", IDR_NETWORK_UI_JS); | 167 html->AddResourcePath("network_ui.js", IDR_NETWORK_UI_JS); |
49 html->SetDefaultResource(IDR_NETWORK_UI_HTML); | 168 html->SetDefaultResource(IDR_NETWORK_UI_HTML); |
50 | 169 |
51 content::WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(), | 170 content::WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(), |
52 html); | 171 html); |
53 } | 172 } |
54 | 173 |
55 NetworkUI::~NetworkUI() { | 174 NetworkUI::~NetworkUI() { |
56 } | 175 } |
57 | 176 |
58 } // namespace chromeos | 177 } // namespace chromeos |
OLD | NEW |