File: network_config_message_handler.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (238 lines) | stat: -rw-r--r-- 8,561 bytes parent folder | download
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2014 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_config_message_handler.h"

#include <string>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
#include "base/values.h"
#include "chromeos/login/login_state.h"
#include "chromeos/network/device_state.h"
#include "chromeos/network/managed_network_configuration_handler.h"
#include "chromeos/network/network_configuration_handler.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h"
#include "chromeos/network/network_util.h"
#include "chromeos/network/onc/onc_signature.h"
#include "chromeos/network/onc/onc_utils.h"
#include "chromeos/network/shill_property_util.h"
#include "components/onc/onc_constants.h"
#include "content/public/browser/web_ui.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;
}

}  // namespace

NetworkConfigMessageHandler::NetworkConfigMessageHandler()
    : weak_ptr_factory_(this) {
}

NetworkConfigMessageHandler::~NetworkConfigMessageHandler() {
}

void NetworkConfigMessageHandler::RegisterMessages() {
  web_ui()->RegisterMessageCallback(
      "networkConfig.getNetworks",
      base::Bind(&NetworkConfigMessageHandler::GetNetworks,
                 base::Unretained(this)));
  web_ui()->RegisterMessageCallback(
      "networkConfig.getProperties",
      base::Bind(&NetworkConfigMessageHandler::GetProperties,
                 base::Unretained(this)));
  web_ui()->RegisterMessageCallback(
      "networkConfig.getManagedProperties",
      base::Bind(&NetworkConfigMessageHandler::GetManagedProperties,
                 base::Unretained(this)));
  web_ui()->RegisterMessageCallback(
      "networkConfig.getShillProperties",
      base::Bind(&NetworkConfigMessageHandler::GetShillProperties,
                 base::Unretained(this)));
}

void NetworkConfigMessageHandler::GetNetworks(
    const base::ListValue* arg_list) const {
  int callback_id = 0;
  const base::DictionaryValue* filter = NULL;
  if (!arg_list->GetInteger(0, &callback_id) ||
      !arg_list->GetDictionary(1, &filter)) {
    NOTREACHED();
  }
  std::string type = ::onc::network_type::kAllTypes;
  bool visible_only = false;
  bool configured_only = false;
  int limit = 1000;
  filter->GetString("type", &type);
  NetworkTypePattern pattern = onc::NetworkTypePatternFromOncType(type);
  filter->GetBoolean("visible", &visible_only);
  filter->GetBoolean("configured", &configured_only);
  filter->GetInteger("limit", &limit);

  base::ListValue return_arg_list;
  return_arg_list.AppendInteger(callback_id);

  scoped_ptr<base::ListValue> network_properties_list =
      chromeos::network_util::TranslateNetworkListToONC(
          pattern, configured_only, visible_only, limit,
          true /* debugging_properties */);

  return_arg_list.Append(network_properties_list.release());

  InvokeCallback(return_arg_list);
}

void NetworkConfigMessageHandler::GetProperties(
    const base::ListValue* arg_list) {
  int callback_id = 0;
  std::string guid;
  if (!arg_list->GetInteger(0, &callback_id) ||
      !arg_list->GetString(1, &guid)) {
    NOTREACHED();
  }
  std::string service_path;
  if (!GetServicePathFromGuid(guid, &service_path)) {
    scoped_ptr<base::DictionaryValue> error_data;
    ErrorCallback(callback_id, "Error.InvalidNetworkGuid", error_data.Pass());
    return;
  }
  NetworkHandler::Get()->managed_network_configuration_handler()->GetProperties(
      service_path,
      base::Bind(&NetworkConfigMessageHandler::GetPropertiesSuccess,
                 weak_ptr_factory_.GetWeakPtr(), callback_id),
      base::Bind(&NetworkConfigMessageHandler::ErrorCallback,
                 weak_ptr_factory_.GetWeakPtr(), callback_id));
}

void NetworkConfigMessageHandler::GetManagedProperties(
    const base::ListValue* arg_list) {
  int callback_id = 0;
  std::string guid;
  if (!arg_list->GetInteger(0, &callback_id) ||
      !arg_list->GetString(1, &guid)) {
    NOTREACHED();
  }
  std::string service_path;
  if (!GetServicePathFromGuid(guid, &service_path)) {
    scoped_ptr<base::DictionaryValue> error_data;
    ErrorCallback(callback_id, "Error.InvalidNetworkGuid", error_data.Pass());
    return;
  }
  NetworkHandler::Get()->managed_network_configuration_handler()->
      GetManagedProperties(
          LoginState::Get()->primary_user_hash(),
          service_path,
          base::Bind(&NetworkConfigMessageHandler::GetPropertiesSuccess,
                     weak_ptr_factory_.GetWeakPtr(), callback_id),
          base::Bind(&NetworkConfigMessageHandler::ErrorCallback,
                     weak_ptr_factory_.GetWeakPtr(), callback_id));
}

void NetworkConfigMessageHandler::GetPropertiesSuccess(
    int callback_id,
    const std::string& service_path,
    const base::DictionaryValue& dictionary) const {
  base::ListValue return_arg_list;
  return_arg_list.AppendInteger(callback_id);

  base::DictionaryValue* network_properties = dictionary.DeepCopy();
  // Set the 'ServicePath' property for debugging.
  network_properties->SetStringWithoutPathExpansion(
      "ServicePath", service_path);

  return_arg_list.Append(network_properties);
  InvokeCallback(return_arg_list);
}

void NetworkConfigMessageHandler::GetShillProperties(
    const base::ListValue* arg_list) {
  int callback_id = 0;
  std::string guid;
  if (!arg_list->GetInteger(0, &callback_id) ||
      !arg_list->GetString(1, &guid)) {
    NOTREACHED();
  }
  std::string service_path;
  if (!GetServicePathFromGuid(guid, &service_path)) {
    scoped_ptr<base::DictionaryValue> error_data;
    ErrorCallback(callback_id, "Error.InvalidNetworkGuid", error_data.Pass());
    return;
  }
  NetworkHandler::Get()->network_configuration_handler()->GetProperties(
      service_path,
      base::Bind(&NetworkConfigMessageHandler::GetShillPropertiesSuccess,
                 weak_ptr_factory_.GetWeakPtr(), callback_id),
      base::Bind(&NetworkConfigMessageHandler::ErrorCallback,
                 weak_ptr_factory_.GetWeakPtr(), callback_id));
}

void NetworkConfigMessageHandler::GetShillPropertiesSuccess(
    int callback_id,
    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);

  // Get the device properties for debugging.
  std::string device;
  dictionary_copy->GetStringWithoutPathExpansion(
      shill::kDeviceProperty, &device);
  const DeviceState* device_state =
      NetworkHandler::Get()->network_state_handler()->GetDeviceState(device);
  if (device_state) {
    base::DictionaryValue* device_dictionary =
        device_state->properties().DeepCopy();
    dictionary_copy->Set(shill::kDeviceProperty, device_dictionary);

    // Convert IPConfig dictionary to a ListValue.
    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);
  }

  base::ListValue return_arg_list;
  return_arg_list.AppendInteger(callback_id);
  return_arg_list.Append(dictionary_copy.release());
  InvokeCallback(return_arg_list);
}

void NetworkConfigMessageHandler::InvokeCallback(
    const base::ListValue& arg_list) const {
  web_ui()->CallJavascriptFunction(
      "networkConfig.chromeCallbackSuccess", arg_list);
}

void NetworkConfigMessageHandler::ErrorCallback(
    int callback_id,
    const std::string& error_name,
    scoped_ptr<base::DictionaryValue> error_data) const {
  LOG(ERROR) << "NetworkConfigMessageHandler Error: " << error_name;
  base::ListValue arg_list;
  arg_list.AppendInteger(callback_id);
  arg_list.AppendString(error_name);
  web_ui()->CallJavascriptFunction(
      "networkConfig.chromeCallbackError", arg_list);
}

}  // namespace chromeos