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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
// Copyright (c) 2012 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 "chromeos/network/network_state_handler.h"
#include "base/format_macros.h"
#include "base/stl_util.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "base/values.h"
#include "chromeos/network/device_state.h"
#include "chromeos/network/managed_state.h"
#include "chromeos/network/network_event_log.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler_observer.h"
#include "chromeos/network/shill_property_handler.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace {
const char kLogModule[] = "NetworkPropertyHandler";
// Returns true if |network->type()| == |match_type|, or it matches one of the
// following special match types:
// * kMatchTypeDefault matches any network (i.e. the first instance)
// * kMatchTypeNonVirtual matches non virtual networks
// * kMatchTypeWireless matches wireless networks
bool NetworkStateMatchesType(const chromeos::NetworkState* network,
const std::string& match_type) {
const std::string& type = network->type();
return ((match_type == chromeos::NetworkStateHandler::kMatchTypeDefault) ||
(match_type == type) ||
(match_type == chromeos::NetworkStateHandler::kMatchTypeNonVirtual &&
type != flimflam::kTypeVPN) ||
(match_type == chromeos::NetworkStateHandler::kMatchTypeWireless &&
type != flimflam::kTypeEthernet && type != flimflam::kTypeVPN));
}
} // namespace
namespace chromeos {
const char NetworkStateHandler::kMatchTypeDefault[] = "default";
const char NetworkStateHandler::kMatchTypeWireless[] = "wireless";
const char NetworkStateHandler::kMatchTypeNonVirtual[] = "non-virtual";
static NetworkStateHandler* g_network_state_handler = NULL;
NetworkStateHandler::NetworkStateHandler() {
}
NetworkStateHandler::~NetworkStateHandler() {
STLDeleteContainerPointers(network_list_.begin(), network_list_.end());
STLDeleteContainerPointers(device_list_.begin(), device_list_.end());
}
void NetworkStateHandler::InitShillPropertyHandler() {
shill_property_handler_.reset(new internal::ShillPropertyHandler(this));
shill_property_handler_->Init();
}
// static
void NetworkStateHandler::Initialize() {
CHECK(!g_network_state_handler);
g_network_state_handler = new NetworkStateHandler();
g_network_state_handler->InitShillPropertyHandler();
}
// static
void NetworkStateHandler::Shutdown() {
CHECK(g_network_state_handler);
delete g_network_state_handler;
g_network_state_handler = NULL;
}
// static
NetworkStateHandler* NetworkStateHandler::Get() {
CHECK(g_network_state_handler)
<< "NetworkStateHandler::Get() called before Initialize()";
return g_network_state_handler;
}
void NetworkStateHandler::AddObserver(NetworkStateHandlerObserver* observer) {
observers_.AddObserver(observer);
}
void NetworkStateHandler::RemoveObserver(
NetworkStateHandlerObserver* observer) {
observers_.RemoveObserver(observer);
}
bool NetworkStateHandler::TechnologyAvailable(
const std::string& technology) const {
return available_technologies_.find(technology) !=
available_technologies_.end();
}
bool NetworkStateHandler::TechnologyEnabled(
const std::string& technology) const {
return enabled_technologies_.find(technology) != enabled_technologies_.end();
}
void NetworkStateHandler::SetTechnologyEnabled(
const std::string& technology,
bool enabled,
const network_handler::ErrorCallback& error_callback) {
shill_property_handler_->SetTechnologyEnabled(
technology, enabled, error_callback);
}
const DeviceState* NetworkStateHandler::GetDeviceState(
const std::string& device_path) const {
return GetModifiableDeviceState(device_path);
}
const DeviceState* NetworkStateHandler::GetDeviceStateByType(
const std::string& type) const {
for (ManagedStateList::const_iterator iter = device_list_.begin();
iter != device_list_.end(); ++iter) {
ManagedState* device = *iter;
if (device->type() == type)
return device->AsDeviceState();
}
return NULL;
}
const NetworkState* NetworkStateHandler::GetNetworkState(
const std::string& service_path) const {
return GetModifiableNetworkState(service_path);
}
const NetworkState* NetworkStateHandler::DefaultNetwork() const {
if (network_list_.empty())
return NULL;
const NetworkState* network = network_list_.front()->AsNetworkState();
DCHECK(network);
if (!network->IsConnectedState())
return NULL;
return network;
}
const NetworkState* NetworkStateHandler::ConnectedNetworkByType(
const std::string& type) const {
for (ManagedStateList::const_iterator iter = network_list_.begin();
iter != network_list_.end(); ++iter) {
const NetworkState* network = (*iter)->AsNetworkState();
DCHECK(network);
if (!network->IsConnectedState())
break; // Connected networks are listed first.
if (NetworkStateMatchesType(network, type))
return network;
}
return NULL;
}
const NetworkState* NetworkStateHandler::ConnectingNetworkByType(
const std::string& type) const {
for (ManagedStateList::const_iterator iter = network_list_.begin();
iter != network_list_.end(); ++iter) {
const NetworkState* network = (*iter)->AsNetworkState();
DCHECK(network);
if (network->IsConnectedState())
continue;
if (!network->IsConnectingState())
break; // Connected and connecting networks are listed first.
if (NetworkStateMatchesType(network, type))
return network;
}
return NULL;
}
std::string NetworkStateHandler::HardwareAddressForType(
const std::string& type) const {
std::string result;
const NetworkState* network = ConnectedNetworkByType(type);
if (network) {
const DeviceState* device = GetDeviceState(network->device_path());
if (device)
result = device->mac_address();
}
StringToUpperASCII(&result);
return result;
}
std::string NetworkStateHandler::FormattedHardwareAddressForType(
const std::string& type) const {
std::string address = HardwareAddressForType(type);
if (address.size() % 2 != 0)
return address;
std::string result;
for (size_t i = 0; i < address.size(); ++i) {
if ((i != 0) && (i % 2 == 0))
result.push_back(':');
result.push_back(address[i]);
}
return result;
}
void NetworkStateHandler::GetNetworkList(NetworkStateList* list) const {
DCHECK(list);
NetworkStateList result;
list->clear();
for (ManagedStateList::const_iterator iter = network_list_.begin();
iter != network_list_.end(); ++iter) {
const NetworkState* network = (*iter)->AsNetworkState();
DCHECK(network);
list->push_back(network);
}
}
bool NetworkStateHandler::RequestWifiScan() const {
if (!TechnologyEnabled(flimflam::kTypeWifi))
return false;
shill_property_handler_->RequestScan();
return true;
}
//------------------------------------------------------------------------------
// ShillPropertyHandler::Delegate overrides
void NetworkStateHandler::UpdateManagedList(ManagedState::ManagedType type,
const base::ListValue& entries) {
ManagedStateList* managed_list = GetManagedList(type);
VLOG(2) << "UpdateManagedList: " << type;
// Create a map of existing entries.
std::map<std::string, ManagedState*> managed_map;
for (ManagedStateList::iterator iter = managed_list->begin();
iter != managed_list->end(); ++iter) {
ManagedState* managed = *iter;
managed_map[managed->path()] = managed;
}
// Clear the list (pointers are owned by managed_map).
managed_list->clear();
// Updates managed_list and request updates for new entries.
for (base::ListValue::const_iterator iter = entries.begin();
iter != entries.end(); ++iter) {
std::string path;
(*iter)->GetAsString(&path);
DCHECK(!path.empty());
std::map<std::string, ManagedState*>::iterator found =
managed_map.find(path);
bool request_properties = false;
ManagedState* managed;
bool is_observing = shill_property_handler_->IsObservingNetwork(path);
if (found == managed_map.end()) {
request_properties = true;
managed = ManagedState::Create(type, path);
managed_list->push_back(managed);
} else {
managed = found->second;
managed_list->push_back(managed);
managed_map.erase(found);
if (!managed->is_observed() && is_observing)
request_properties = true;
}
if (is_observing)
managed->set_is_observed(true);
if (request_properties)
shill_property_handler_->RequestProperties(type, path);
}
// Delete any remaning entries in managed_map.
STLDeleteContainerPairSecondPointers(managed_map.begin(), managed_map.end());
}
void NetworkStateHandler::UpdateAvailableTechnologies(
const base::ListValue& technologies) {
available_technologies_.clear();
network_event_log::AddEntry(
kLogModule, "AvailableTechnologiesChanged",
StringPrintf("Size: %"PRIuS, technologies.GetSize()));
for (base::ListValue::const_iterator iter = technologies.begin();
iter != technologies.end(); ++iter) {
std::string technology;
(*iter)->GetAsString(&technology);
DCHECK(!technology.empty());
available_technologies_.insert(technology);
}
}
void NetworkStateHandler::UpdateEnabledTechnologies(
const base::ListValue& technologies) {
bool wifi_was_enabled = TechnologyEnabled(flimflam::kTypeWifi);
enabled_technologies_.clear();
network_event_log::AddEntry(
kLogModule, "EnabledTechnologiesChanged",
StringPrintf("Size: %"PRIuS, technologies.GetSize()));
for (base::ListValue::const_iterator iter = technologies.begin();
iter != technologies.end(); ++iter) {
std::string technology;
(*iter)->GetAsString(&technology);
DCHECK(!technology.empty());
enabled_technologies_.insert(technology);
}
if (!wifi_was_enabled && TechnologyEnabled(flimflam::kTypeWifi))
RequestWifiScan();
}
void NetworkStateHandler::UpdateManagedStateProperties(
ManagedState::ManagedType type,
const std::string& path,
const base::DictionaryValue& properties) {
ManagedState* managed = GetModifiableManagedState(GetManagedList(type), path);
if (!managed) {
LOG(ERROR) << "GetPropertiesCallback: " << path << " Not found!";
return;
}
bool network_property_updated = false;
for (base::DictionaryValue::Iterator iter(properties);
iter.HasNext(); iter.Advance()) {
if (type == ManagedState::MANAGED_TYPE_NETWORK) {
if (ParseNetworkServiceProperty(
managed->AsNetworkState(), iter.key(), iter.value())) {
network_property_updated = true;
}
} else {
managed->PropertyChanged(iter.key(), iter.value());
}
}
// Notify observers.
if (network_property_updated) {
NetworkState* network = managed->AsNetworkState();
DCHECK(network);
FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_,
NetworkPropertiesUpdated(network));
}
network_event_log::AddEntry(
kLogModule, "PropertiesReceived",
StringPrintf("%s (%s)", path.c_str(), managed->name().c_str()));
}
void NetworkStateHandler::UpdateNetworkServiceProperty(
const std::string& service_path,
const std::string& key,
const base::Value& value) {
NetworkState* network = GetModifiableNetworkState(service_path);
if (!network)
return;
if (!ParseNetworkServiceProperty(network, key, value))
return;
std::string detail = network->name() + "." + key;
std::string vstr;
if (value.GetAsString(&vstr))
detail += " = " + vstr;
network_event_log::AddEntry(kLogModule, "NetworkPropertiesUpdated", detail);
FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_,
NetworkPropertiesUpdated(network));
}
void NetworkStateHandler::UpdateNetworkServiceIPAddress(
const std::string& service_path,
const std::string& ip_address) {
NetworkState* network = GetModifiableNetworkState(service_path);
if (!network)
return;
std::string detail = network->name() + ".IPAddress = " + ip_address;
network_event_log::AddEntry(kLogModule, "NetworkIPChanged", detail);
network->set_ip_address(ip_address);
FOR_EACH_OBSERVER(
NetworkStateHandlerObserver, observers_,
NetworkPropertiesUpdated(network));
}
void NetworkStateHandler::ManagerPropertyChanged() {
FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_,
NetworkManagerChanged());
}
void NetworkStateHandler::ManagedStateListChanged(
ManagedState::ManagedType type) {
if (type == ManagedState::MANAGED_TYPE_NETWORK) {
// Notify observers that the list of networks has changed.
NetworkStateList network_list;
GetNetworkList(&network_list);
network_event_log::AddEntry(
kLogModule, "NetworkListChanged",
StringPrintf("Size: %"PRIuS, network_list_.size()));
FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_,
NetworkListChanged(network_list));
// The list order may have changed, so check if the default network changed.
if (CheckDefaultNetworkChanged())
OnDefaultNetworkChanged();
} else if (type == ManagedState::MANAGED_TYPE_DEVICE) {
network_event_log::AddEntry(
kLogModule, "DeviceListChanged",
StringPrintf("Size: %"PRIuS, device_list_.size()));
FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_,
DeviceListChanged());
} else {
NOTREACHED();
}
}
//------------------------------------------------------------------------------
// Private methods
DeviceState* NetworkStateHandler::GetModifiableDeviceState(
const std::string& device_path) const {
ManagedState* managed = GetModifiableManagedState(&device_list_, device_path);
if (!managed)
return NULL;
return managed->AsDeviceState();
}
NetworkState* NetworkStateHandler::GetModifiableNetworkState(
const std::string& service_path) const {
ManagedState* managed =
GetModifiableManagedState(&network_list_, service_path);
if (!managed)
return NULL;
return managed->AsNetworkState();
}
ManagedState* NetworkStateHandler::GetModifiableManagedState(
const ManagedStateList* managed_list,
const std::string& path) const {
for (ManagedStateList::const_iterator iter = managed_list->begin();
iter != managed_list->end(); ++iter) {
ManagedState* managed = *iter;
if (managed->path() == path)
return managed;
}
return NULL;
}
NetworkStateHandler::ManagedStateList* NetworkStateHandler::GetManagedList(
ManagedState::ManagedType type) {
switch (type) {
case ManagedState::MANAGED_TYPE_NETWORK:
return &network_list_;
case ManagedState::MANAGED_TYPE_DEVICE:
return &device_list_;
}
NOTREACHED();
return NULL;
}
bool NetworkStateHandler::ParseNetworkServiceProperty(
NetworkState* network,
const std::string& key,
const base::Value& value) {
DCHECK(network);
std::string prev_connection_state = network->connection_state();
if (!network->PropertyChanged(key, value))
return false;
if (key == flimflam::kStateProperty &&
network->connection_state() != prev_connection_state) {
OnNetworkConnectionStateChanged(network);
}
return true;
}
void NetworkStateHandler::OnNetworkConnectionStateChanged(
NetworkState* network) {
std::string desc = StringPrintf(
"%s: %s", network->path().c_str(), network->connection_state().c_str());
network_event_log::AddEntry(
kLogModule, "NetworkConnectionStateChanged", desc);
FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_,
NetworkConnectionStateChanged(network));
if (CheckDefaultNetworkChanged() ||
network->path() == default_network_path_) {
OnDefaultNetworkChanged();
}
}
bool NetworkStateHandler::CheckDefaultNetworkChanged() {
std::string new_default_network_path;
const NetworkState* new_default_network = DefaultNetwork();
if (new_default_network)
new_default_network_path = new_default_network->path();
if (new_default_network_path == default_network_path_)
return false;
default_network_path_ = new_default_network_path;
return true;
}
void NetworkStateHandler::OnDefaultNetworkChanged() {
const NetworkState* default_network = DefaultNetwork();
network_event_log::AddEntry(
kLogModule, "DefaultNetworkChanged",
default_network ? default_network->path() : "Null");
FOR_EACH_OBSERVER(NetworkStateHandlerObserver, observers_,
DefaultNetworkChanged(default_network));
}
} // namespace chromeos
|