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