shill_ipconfig_client.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chromeos/dbus/shill_ipconfig_client.h"
6
7#include "base/bind.h"
8#include "base/message_loop.h"
9#include "base/stl_util.h"
10#include "base/values.h"
11#include "chromeos/dbus/shill_ipconfig_client_stub.h"
12#include "chromeos/dbus/shill_property_changed_observer.h"
13#include "dbus/bus.h"
14#include "dbus/message.h"
15#include "dbus/object_path.h"
16#include "dbus/object_proxy.h"
17#include "dbus/values_util.h"
18#include "third_party/cros_system_api/dbus/service_constants.h"
19
20namespace chromeos {
21
22namespace {
23
24// The ShillIPConfigClient implementation.
25class ShillIPConfigClientImpl : public ShillIPConfigClient {
26 public:
27  explicit ShillIPConfigClientImpl(dbus::Bus* bus);
28
29  ////////////////////////////////////
30  // ShillIPConfigClient overrides.
31  virtual void AddPropertyChangedObserver(
32      const dbus::ObjectPath& ipconfig_path,
33      ShillPropertyChangedObserver* observer) OVERRIDE {
34    GetHelper(ipconfig_path)->AddPropertyChangedObserver(observer);
35  }
36
37  virtual void RemovePropertyChangedObserver(
38      const dbus::ObjectPath& ipconfig_path,
39      ShillPropertyChangedObserver* observer) OVERRIDE {
40    GetHelper(ipconfig_path)->RemovePropertyChangedObserver(observer);
41  }
42  virtual void Refresh(const dbus::ObjectPath& ipconfig_path,
43                       const VoidDBusMethodCallback& callback) OVERRIDE;
44  virtual void GetProperties(const dbus::ObjectPath& ipconfig_path,
45                             const DictionaryValueCallback& callback) OVERRIDE;
46  virtual base::DictionaryValue* CallGetPropertiesAndBlock(
47      const dbus::ObjectPath& ipconfig_path) OVERRIDE;
48  virtual void SetProperty(const dbus::ObjectPath& ipconfig_path,
49                           const std::string& name,
50                           const base::Value& value,
51                           const VoidDBusMethodCallback& callback) OVERRIDE;
52  virtual void ClearProperty(const dbus::ObjectPath& ipconfig_path,
53                             const std::string& name,
54                             const VoidDBusMethodCallback& callback) OVERRIDE;
55  virtual void Remove(const dbus::ObjectPath& ipconfig_path,
56                      const VoidDBusMethodCallback& callback) OVERRIDE;
57
58 private:
59  typedef std::map<std::string, ShillClientHelper*> HelperMap;
60
61  // Returns the corresponding ShillClientHelper for the profile.
62  ShillClientHelper* GetHelper(const dbus::ObjectPath& ipconfig_path) {
63    HelperMap::iterator it = helpers_.find(ipconfig_path.value());
64    if (it != helpers_.end())
65      return it->second;
66
67    // There is no helper for the profile, create it.
68    dbus::ObjectProxy* object_proxy =
69        bus_->GetObjectProxy(flimflam::kFlimflamServiceName, ipconfig_path);
70    ShillClientHelper* helper = new ShillClientHelper(bus_, object_proxy);
71    helper->MonitorPropertyChanged(flimflam::kFlimflamIPConfigInterface);
72    helpers_.insert(HelperMap::value_type(ipconfig_path.value(), helper));
73    return helper;
74  }
75
76  dbus::Bus* bus_;
77  HelperMap helpers_;
78  STLValueDeleter<HelperMap> helpers_deleter_;
79
80  DISALLOW_COPY_AND_ASSIGN(ShillIPConfigClientImpl);
81};
82
83ShillIPConfigClientImpl::ShillIPConfigClientImpl(dbus::Bus* bus)
84    : bus_(bus),
85      helpers_deleter_(&helpers_) {
86}
87
88void ShillIPConfigClientImpl::GetProperties(
89    const dbus::ObjectPath& ipconfig_path,
90    const DictionaryValueCallback& callback) {
91  dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface,
92                               flimflam::kGetPropertiesFunction);
93  GetHelper(ipconfig_path)->CallDictionaryValueMethod(&method_call, callback);
94}
95
96base::DictionaryValue* ShillIPConfigClientImpl::CallGetPropertiesAndBlock(
97    const dbus::ObjectPath& ipconfig_path) {
98  dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface,
99                               flimflam::kGetPropertiesFunction);
100  return GetHelper(ipconfig_path)->CallDictionaryValueMethodAndBlock(
101      &method_call);
102}
103
104void ShillIPConfigClientImpl::Refresh(
105    const dbus::ObjectPath& ipconfig_path,
106    const VoidDBusMethodCallback& callback) {
107  dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface,
108                               shill::kRefreshFunction);
109  GetHelper(ipconfig_path)->CallVoidMethod(&method_call, callback);
110}
111
112void ShillIPConfigClientImpl::SetProperty(
113    const dbus::ObjectPath& ipconfig_path,
114    const std::string& name,
115    const base::Value& value,
116    const VoidDBusMethodCallback& callback) {
117  dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface,
118                               flimflam::kSetPropertyFunction);
119  dbus::MessageWriter writer(&method_call);
120  writer.AppendString(name);
121  // IPConfig supports writing basic type and string array properties.
122  switch (value.GetType()) {
123    case base::Value::TYPE_LIST: {
124      const base::ListValue* list_value = NULL;
125      value.GetAsList(&list_value);
126      dbus::MessageWriter variant_writer(NULL);
127      writer.OpenVariant("as", &variant_writer);
128      dbus::MessageWriter array_writer(NULL);
129      variant_writer.OpenArray("s", &array_writer);
130      for (base::ListValue::const_iterator it = list_value->begin();
131           it != list_value->end();
132           ++it) {
133        DLOG_IF(ERROR, (*it)->GetType() != base::Value::TYPE_STRING)
134            << "Unexpected type " << (*it)->GetType();
135        std::string str;
136        (*it)->GetAsString(&str);
137        array_writer.AppendString(str);
138      }
139      variant_writer.CloseContainer(&array_writer);
140      writer.CloseContainer(&variant_writer);
141    }
142    case base::Value::TYPE_BOOLEAN:
143    case base::Value::TYPE_INTEGER:
144    case base::Value::TYPE_DOUBLE:
145    case base::Value::TYPE_STRING:
146      dbus::AppendBasicTypeValueDataAsVariant(&writer, value);
147      break;
148    default:
149      DLOG(ERROR) << "Unexpected type " << value.GetType();
150  }
151  GetHelper(ipconfig_path)->CallVoidMethod(&method_call, callback);
152}
153
154void ShillIPConfigClientImpl::ClearProperty(
155    const dbus::ObjectPath& ipconfig_path,
156    const std::string& name,
157    const VoidDBusMethodCallback& callback) {
158  dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface,
159                               flimflam::kClearPropertyFunction);
160  dbus::MessageWriter writer(&method_call);
161  writer.AppendString(name);
162  GetHelper(ipconfig_path)->CallVoidMethod(&method_call, callback);
163}
164
165void ShillIPConfigClientImpl::Remove(
166    const dbus::ObjectPath& ipconfig_path,
167    const VoidDBusMethodCallback& callback) {
168  dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface,
169                               flimflam::kRemoveConfigFunction);
170  GetHelper(ipconfig_path)->CallVoidMethod(&method_call, callback);
171}
172
173}  // namespace
174
175ShillIPConfigClient::ShillIPConfigClient() {}
176
177ShillIPConfigClient::~ShillIPConfigClient() {}
178
179// static
180ShillIPConfigClient* ShillIPConfigClient::Create(
181    DBusClientImplementationType type,
182    dbus::Bus* bus) {
183  if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
184    return new ShillIPConfigClientImpl(bus);
185  DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
186  return new ShillIPConfigClientStub();
187}
188
189}  // namespace chromeos
190