summaryrefslogtreecommitdiffstats
path: root/chromeos/network/shill_property_handler.cc
blob: 77813675fd4472968d56cc92fb211305a5c80ef1 (plain)
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
// 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/shill_property_handler.h"

#include "base/bind.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/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/shill_device_client.h"
#include "chromeos/dbus/shill_ipconfig_client.h"
#include "chromeos/dbus/shill_manager_client.h"
#include "chromeos/dbus/shill_service_client.h"
#include "chromeos/network/network_event_log.h"
#include "chromeos/network/shill_service_observer.h"
#include "dbus/object_path.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace {

const char kLogModule[] = "ShillPropertyHandler";

// Limit the number of services we observe. Since they are listed in priority
// order, it should be reasonable to ignore services past this.
const size_t kMaxObservedServices = 100;

const base::ListValue* GetListValue(const std::string& key,
                                    const base::Value& value) {
  const base::ListValue* vlist = NULL;
  if (!value.GetAsList(&vlist)) {
    LOG(ERROR) << "Error parsing key as list: " << key;
    return NULL;
  }
  return vlist;
}

}  // namespace

namespace chromeos {
namespace internal {

ShillPropertyHandler::ShillPropertyHandler(Listener* listener)
    : listener_(listener),
      shill_manager_(DBusThreadManager::Get()->GetShillManagerClient()),
      weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
}

ShillPropertyHandler::~ShillPropertyHandler() {
  // Delete network service observers.
  STLDeleteContainerPairSecondPointers(
      observed_networks_.begin(), observed_networks_.end());
  CHECK(shill_manager_ == DBusThreadManager::Get()->GetShillManagerClient());
  shill_manager_->RemovePropertyChangedObserver(this);
}

void ShillPropertyHandler::Init() {
  shill_manager_->GetProperties(
      base::Bind(&ShillPropertyHandler::ManagerPropertiesCallback,
                 weak_ptr_factory_.GetWeakPtr()));
  shill_manager_->AddPropertyChangedObserver(this);
}

void ShillPropertyHandler::SetTechnologyEnabled(
    const std::string& technology,
    bool enabled,
    const network_handler::ErrorCallback& error_callback) {
  if (enabled) {
    shill_manager_->EnableTechnology(
        technology,
        base::Bind(&base::DoNothing),
        base::Bind(&network_handler::ShillErrorCallbackFunction,
                   kLogModule, technology, error_callback));
  } else {
    shill_manager_->DisableTechnology(
        technology,
        base::Bind(&base::DoNothing),
        base::Bind(&network_handler::ShillErrorCallbackFunction,
                   kLogModule, technology, error_callback));
  }
}

void ShillPropertyHandler::RequestScan() const {
  shill_manager_->RequestScan(
      "",
      base::Bind(&base::DoNothing),
      base::Bind(&network_handler::ShillErrorCallbackFunction,
                 kLogModule, "", network_handler::ErrorCallback()));
}

void ShillPropertyHandler::RequestProperties(ManagedState::ManagedType type,
                                             const std::string& path) {
  if (pending_updates_[type].find(path) != pending_updates_[type].end())
    return;  // Update already requested.

  pending_updates_[type].insert(path);
  if (type == ManagedState::MANAGED_TYPE_NETWORK) {
    DBusThreadManager::Get()->GetShillServiceClient()->GetProperties(
        dbus::ObjectPath(path),
        base::Bind(&ShillPropertyHandler::GetPropertiesCallback,
                   weak_ptr_factory_.GetWeakPtr(), type, path));
  } else if (type == ManagedState::MANAGED_TYPE_DEVICE) {
    DBusThreadManager::Get()->GetShillDeviceClient()->GetProperties(
        dbus::ObjectPath(path),
        base::Bind(&ShillPropertyHandler::GetPropertiesCallback,
                   weak_ptr_factory_.GetWeakPtr(), type, path));
  } else {
    NOTREACHED();
  }
}

void ShillPropertyHandler::OnPropertyChanged(const std::string& key,
                                             const base::Value& value) {
  if (ManagerPropertyChanged(key, value))
    listener_->ManagerPropertyChanged();
}

//------------------------------------------------------------------------------
// Private methods

void ShillPropertyHandler::ManagerPropertiesCallback(
    DBusMethodCallStatus call_status,
    const base::DictionaryValue& properties) {
  if (call_status != DBUS_METHOD_CALL_SUCCESS) {
    LOG(ERROR) << "Failed to get Manager properties:" << call_status;
    return;
  }
  bool notify = false;
  bool update_service_list = false;
  for (base::DictionaryValue::Iterator iter(properties);
       iter.HasNext(); iter.Advance()) {
    // Defer updating Services until all other properties have been updated.
    if (iter.key() == flimflam::kServicesProperty)
      update_service_list = true;
    else
      notify |= ManagerPropertyChanged(iter.key(), iter.value());
  }
  // Now update the service list which can safely assume other properties have
  // been initially set.
  if (update_service_list) {
    const base::Value* value = NULL;
    if (properties.GetWithoutPathExpansion(flimflam::kServicesProperty, &value))
      notify |= ManagerPropertyChanged(flimflam::kServicesProperty, *value);
  }
  if (notify)
    listener_->ManagerPropertyChanged();
}

bool ShillPropertyHandler::ManagerPropertyChanged(const std::string& key,
                                                  const base::Value& value) {
  bool notify_manager_changed = false;
  if (key == flimflam::kServicesProperty) {
    const base::ListValue* vlist = GetListValue(key, value);
    if (vlist)
      UpdateManagedList(ManagedState::MANAGED_TYPE_NETWORK, *vlist);
  } else if (key == flimflam::kServiceWatchListProperty) {
    const base::ListValue* vlist = GetListValue(key, value);
    if (vlist)
      UpdateObservedNetworkServices(*vlist);
  } else if (key == flimflam::kDevicesProperty) {
    const ListValue* vlist = GetListValue(key, value);
    if (vlist)
      UpdateManagedList(ManagedState::MANAGED_TYPE_DEVICE, *vlist);
  } else if (key == flimflam::kAvailableTechnologiesProperty) {
    const base::ListValue* vlist = GetListValue(key, value);
    if (vlist) {
      listener_->UpdateAvailableTechnologies(*vlist);
      notify_manager_changed = true;
    }
  } else if (key == flimflam::kEnabledTechnologiesProperty) {
    const base::ListValue* vlist = GetListValue(key, value);
    if (vlist) {
      listener_->UpdateEnabledTechnologies(*vlist);
      notify_manager_changed = true;
    }
  }
  return notify_manager_changed;
}

void ShillPropertyHandler::UpdateManagedList(ManagedState::ManagedType type,
                                             const base::ListValue& entries) {
  listener_->UpdateManagedList(type, entries);
  // Do not send a ManagerPropertyChanged notification to the Listener if
  // RequestProperties has been called (ManagedStateListChanged will be
  // called when the update requests have completed). If no requests
  // have been made, call ManagedStateListChanged to indicate that the
  // order of the list has changed.
  if (pending_updates_[type].size() == 0)
    listener_->ManagedStateListChanged(type);
}

void ShillPropertyHandler::UpdateObservedNetworkServices(
    const base::ListValue& entries) {
  // Watch all networks in the watch list.
  ShillServiceObserverMap new_observed;
  for (base::ListValue::const_iterator iter1 = entries.begin();
       iter1 != entries.end(); ++iter1) {
    std::string path;
    (*iter1)->GetAsString(&path);
    if (path.empty())
      continue;
    ShillServiceObserverMap::iterator iter2 = observed_networks_.find(path);
    if (iter2 != observed_networks_.end()) {
      new_observed[path] = iter2->second;
    } else {
      // Request an update for the network.
      RequestProperties(ManagedState::MANAGED_TYPE_NETWORK, path);
      // Create an observer for future updates.
      new_observed[path] = new ShillServiceObserver(
          path, base::Bind(
              &ShillPropertyHandler::NetworkServicePropertyChangedCallback,
              weak_ptr_factory_.GetWeakPtr()));
      network_event_log::AddEntry(kLogModule, "StartObserving", path);
    }
    observed_networks_.erase(path);
    // Limit the number of observed services.
    if (new_observed.size() >= kMaxObservedServices)
      break;
  }
  // Delete network service observers still in observed_networks_.
  for (ShillServiceObserverMap::iterator iter =  observed_networks_.begin();
       iter != observed_networks_.end(); ++iter) {
    network_event_log::AddEntry(kLogModule, "StopObserving", iter->first);
    delete iter->second;
  }
  observed_networks_.swap(new_observed);
}

void ShillPropertyHandler::GetPropertiesCallback(
    ManagedState::ManagedType type,
    const std::string& path,
    DBusMethodCallStatus call_status,
    const base::DictionaryValue& properties) {
  VLOG(2) << "GetPropertiesCallback: " << type << " : " << path;
  pending_updates_[type].erase(path);
  if (call_status != DBUS_METHOD_CALL_SUCCESS) {
    LOG(ERROR) << "Failed to get properties for: " << path
               << ": " << call_status;
    return;
  }
  listener_->UpdateManagedStateProperties(type, path, properties);
  // Notify the listener only when all updates for that type have completed.
  if (pending_updates_[type].size() == 0)
    listener_->ManagedStateListChanged(type);
}

void ShillPropertyHandler::NetworkServicePropertyChangedCallback(
    const std::string& path,
    const std::string& key,
    const base::Value& value) {
  if (key == shill::kIPConfigProperty) {
    // Handle IPConfig here and call listener_->UpdateNetworkServiceIPAddress
    // when the request completes.
    std::string ip_config_path;
    value.GetAsString(&ip_config_path);
    DCHECK(!ip_config_path.empty());
    DBusThreadManager::Get()->GetShillIPConfigClient()->GetProperties(
        dbus::ObjectPath(ip_config_path),
        base::Bind(&ShillPropertyHandler::GetIPConfigCallback,
                   weak_ptr_factory_.GetWeakPtr(), path));
  } else {
    listener_->UpdateNetworkServiceProperty(path, key, value);
  }
}

void ShillPropertyHandler::GetIPConfigCallback(
    const std::string& service_path,
    DBusMethodCallStatus call_status,
    const base::DictionaryValue& properties)  {
  if (call_status != DBUS_METHOD_CALL_SUCCESS) {
    LOG(ERROR) << "Failed to get IP properties for: " << service_path;
    return;
  }
  std::string ip_address;
  if (!properties.GetStringWithoutPathExpansion(flimflam::kAddressProperty,
                                                &ip_address)) {
    LOG(ERROR) << "Failed to get IP Address property for: " << service_path;
    return;
  }
  listener_->UpdateNetworkServiceIPAddress(service_path, ip_address);
}

}  // namespace internal
}  // namespace chromeos