Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "chrome/browser/chromeos/dbus/flimflam_ipconfig_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/chromeos/chromeos_version.h" | |
| 9 #include "dbus/bus.h" | |
| 10 #include "dbus/message.h" | |
| 11 #include "dbus/object_path.h" | |
| 12 #include "dbus/object_proxy.h" | |
| 13 #include "dbus/values_util.h" | |
| 14 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 15 | |
| 16 namespace chromeos { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // The FlimflamIPConfigClient implementation. | |
| 21 class FlimflamIPConfigClientImpl : public FlimflamIPConfigClient { | |
|
stevenjb
2012/03/29 00:28:14
For complex classes like these, it is easier to re
hashimoto
2012/03/29 03:04:46
Done.
satorux1
2012/03/29 17:58:44
I personally prefer not to separate the implementa
| |
| 22 public: | |
| 23 explicit FlimflamIPConfigClientImpl(dbus::Bus* bus) | |
| 24 : proxy_(bus->GetObjectProxy( | |
| 25 flimflam::kFlimflamServiceName, | |
| 26 dbus::ObjectPath(flimflam::kFlimflamServicePath))), | |
| 27 weak_ptr_factory_(this) { | |
| 28 // We are not using dbus::PropertySet to monitor PropertyChanged signal | |
| 29 // because the interface is not "org.freedesktop.DBus.Properties". | |
| 30 proxy_->ConnectToSignal( | |
| 31 flimflam::kFlimflamIPConfigInterface, | |
| 32 flimflam::kMonitorPropertyChanged, | |
| 33 base::Bind(&FlimflamIPConfigClientImpl::OnPropertyChanged, | |
| 34 weak_ptr_factory_.GetWeakPtr()), | |
| 35 base::Bind(&FlimflamIPConfigClientImpl::OnSignalConnected, | |
| 36 weak_ptr_factory_.GetWeakPtr())); | |
| 37 } | |
| 38 | |
| 39 // FlimflamIPConfigClient override. | |
| 40 virtual void SetPropertyChangedHandler( | |
| 41 const PropertyChangedHandler& handler) OVERRIDE { | |
| 42 property_changed_handler_ = handler; | |
| 43 } | |
| 44 | |
| 45 // FlimflamIPConfigClient override. | |
| 46 virtual void ResetPropertyChangedHandler() OVERRIDE { | |
| 47 property_changed_handler_.Reset(); | |
| 48 } | |
| 49 | |
| 50 // FlimflamIPConfigClient override. | |
| 51 virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE { | |
| 52 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, | |
| 53 flimflam::kGetPropertiesFunction); | |
| 54 proxy_->CallMethod( | |
| 55 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 56 base::Bind(&FlimflamIPConfigClientImpl::OnDictionaryValueMethod, | |
| 57 weak_ptr_factory_.GetWeakPtr(), | |
| 58 callback)); | |
| 59 } | |
| 60 | |
| 61 // FlimflamIPConfigClient override. | |
| 62 virtual void SetProperty(const std::string& name, | |
| 63 const base::Value& value, | |
| 64 const VoidCallback& callback) OVERRIDE { | |
| 65 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, | |
| 66 flimflam::kSetPropertyFunction); | |
| 67 dbus::MessageWriter writer(&method_call); | |
| 68 writer.AppendString(name); | |
| 69 // IPConfig supports writing basic type and string array properties. | |
| 70 if (value.GetType() == base::Value::TYPE_LIST) { | |
|
stevenjb
2012/03/29 00:28:14
I think this would be better as a switch(value.Get
hashimoto
2012/03/29 03:04:46
Done.
| |
| 71 const base::ListValue* list_value = NULL; | |
| 72 value.GetAsList(&list_value); | |
| 73 dbus::MessageWriter sub_writer(NULL); | |
| 74 writer.OpenArray("s", &sub_writer); | |
| 75 for (base::ListValue::const_iterator it = list_value->begin(); | |
| 76 it != list_value->end(); | |
| 77 ++it) { | |
| 78 std::string str; | |
| 79 (*it)->GetAsString(&str); | |
| 80 sub_writer.AppendString(str); | |
| 81 } | |
| 82 writer.CloseContainer(&sub_writer); | |
| 83 } else { | |
| 84 dbus::AppendBasicTypeValueData(&writer, value); | |
| 85 } | |
| 86 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 87 base::Bind(&FlimflamIPConfigClientImpl::OnVoidMethod, | |
| 88 weak_ptr_factory_.GetWeakPtr(), | |
| 89 callback)); | |
| 90 } | |
| 91 | |
| 92 // FlimflamIPConfigClient override. | |
| 93 virtual void ClearProperty(const std::string& name, | |
| 94 const VoidCallback& callback) OVERRIDE { | |
| 95 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, | |
| 96 flimflam::kClearPropertyFunction); | |
| 97 dbus::MessageWriter writer(&method_call); | |
| 98 writer.AppendString(name); | |
| 99 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 100 base::Bind(&FlimflamIPConfigClientImpl::OnVoidMethod, | |
| 101 weak_ptr_factory_.GetWeakPtr(), | |
| 102 callback)); | |
| 103 } | |
| 104 | |
| 105 // FlimflamIPConfigClient override. | |
| 106 virtual void Remove(const VoidCallback& callback) OVERRIDE { | |
|
stevenjb
2012/03/29 00:28:14
I think this should be named RemoveConfig.
hashimoto
2012/03/29 03:04:46
The actual method name is "Remove". (http://git.ch
stevenjb
2012/03/29 18:56:16
Well, I think flimflam::kRemoveConfigFunction and
| |
| 107 dbus::MethodCall method_call(flimflam::kFlimflamIPConfigInterface, | |
| 108 flimflam::kRemoveConfigFunction); | |
| 109 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 110 base::Bind(&FlimflamIPConfigClientImpl::OnVoidMethod, | |
| 111 weak_ptr_factory_.GetWeakPtr(), | |
| 112 callback)); | |
| 113 } | |
| 114 | |
| 115 private: | |
| 116 // Handles the result of signal connection setup. | |
| 117 void OnSignalConnected(const std::string& interface, | |
| 118 const std::string& signal, | |
| 119 bool successed) { | |
|
stevenjb
2012/03/29 00:28:14
'success' or 'succeeded'
hashimoto
2012/03/29 03:04:46
Done.
| |
| 120 LOG_IF(ERROR, !successed) << "Connect to " << interface << " " << | |
| 121 signal << " failed."; | |
|
stevenjb
2012/03/29 00:28:14
nit: start second line with <<
hashimoto
2012/03/29 03:04:46
Done.
| |
| 122 } | |
| 123 | |
| 124 // Handles PropertyChanged signal. | |
| 125 virtual void OnPropertyChanged(dbus::Signal* signal) { | |
| 126 dbus::MessageReader reader(signal); | |
| 127 std::string name; | |
| 128 const bool name_popped = reader.PopString(&name); | |
|
stevenjb
2012/03/29 00:28:14
nit: I think I prefer
if (!reader.PopString(&name)
hashimoto
2012/03/29 03:04:46
Done.
| |
| 129 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | |
| 130 if (!name_popped || !value.get()) | |
| 131 return; | |
| 132 | |
| 133 if (!property_changed_handler_.is_null()) | |
| 134 property_changed_handler_.Run(name, *value); | |
| 135 } | |
| 136 | |
| 137 // Handles responses for methods without results. | |
| 138 virtual void OnVoidMethod(const VoidCallback& callback, | |
| 139 dbus::Response* response) { | |
| 140 if (!response) { | |
| 141 callback.Run(FAILURE); | |
| 142 return; | |
| 143 } | |
| 144 callback.Run(SUCCESS); | |
| 145 } | |
| 146 | |
| 147 // Handles responses for methods with DictionaryValue results. | |
| 148 virtual void OnDictionaryValueMethod(const DictionaryValueCallback& callback, | |
| 149 dbus::Response* response) { | |
| 150 if (!response) { | |
| 151 base::DictionaryValue result; | |
| 152 callback.Run(FAILURE, result); | |
|
stevenjb
2012/03/29 00:28:14
callback.Run(FAILURE, base::DictionaryValue())
hashimoto
2012/03/29 03:04:46
I tried it before, but clang said "error: C++98 re
| |
| 153 return; | |
| 154 } | |
| 155 dbus::MessageReader reader(response); | |
| 156 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | |
| 157 base::DictionaryValue* result = NULL; | |
| 158 if (!value.get() || !value->GetAsDictionary(&result)) { | |
| 159 base::DictionaryValue result; | |
| 160 callback.Run(FAILURE, result); | |
|
stevenjb
2012/03/29 00:28:14
callback.Run(FAILURE, base::DictionaryValue())
hashimoto
2012/03/29 03:04:46
ditto.
| |
| 161 return; | |
| 162 } | |
| 163 callback.Run(SUCCESS, *result); | |
| 164 } | |
| 165 | |
| 166 dbus::ObjectProxy* proxy_; | |
| 167 base::WeakPtrFactory<FlimflamIPConfigClientImpl> weak_ptr_factory_; | |
| 168 PropertyChangedHandler property_changed_handler_; | |
| 169 | |
| 170 DISALLOW_COPY_AND_ASSIGN(FlimflamIPConfigClientImpl); | |
| 171 }; | |
| 172 | |
| 173 // A stub implementation of FlimflamIPConfigClient. | |
| 174 class FlimflamIPConfigClientStubImpl : public FlimflamIPConfigClient { | |
| 175 public: | |
| 176 FlimflamIPConfigClientStubImpl() {} | |
| 177 | |
| 178 virtual ~FlimflamIPConfigClientStubImpl() {} | |
| 179 | |
| 180 // FlimflamIPConfigClient override. | |
| 181 virtual void SetPropertyChangedHandler( | |
| 182 const PropertyChangedHandler& handler) OVERRIDE {} | |
| 183 | |
| 184 // FlimflamIPConfigClient override. | |
| 185 virtual void ResetPropertyChangedHandler() OVERRIDE {} | |
| 186 | |
| 187 // FlimflamIPConfigClient override. | |
| 188 virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE { | |
|
stevenjb
2012/03/29 00:28:14
Stub should invoke callback here and elsewhere.
hashimoto
2012/03/29 03:04:46
Done.
| |
| 189 } | |
| 190 | |
| 191 // FlimflamIPConfigClient override. | |
| 192 virtual void SetProperty(const std::string& name, | |
| 193 const base::Value& value, | |
| 194 const VoidCallback& callback) OVERRIDE {} | |
| 195 | |
| 196 // FlimflamIPConfigClient override. | |
| 197 virtual void ClearProperty(const std::string& name, | |
| 198 const VoidCallback& callback) OVERRIDE {} | |
| 199 | |
| 200 // FlimflamIPConfigClient override. | |
| 201 virtual void Remove(const VoidCallback& callback) OVERRIDE {} | |
| 202 | |
| 203 private: | |
| 204 DISALLOW_COPY_AND_ASSIGN(FlimflamIPConfigClientStubImpl); | |
| 205 }; | |
| 206 | |
| 207 } // namespace | |
| 208 | |
| 209 FlimflamIPConfigClient::FlimflamIPConfigClient() {} | |
| 210 | |
| 211 FlimflamIPConfigClient::~FlimflamIPConfigClient() {} | |
| 212 | |
| 213 // static | |
| 214 FlimflamIPConfigClient* FlimflamIPConfigClient::Create(dbus::Bus* bus) { | |
| 215 if (base::chromeos::IsRunningOnChromeOS()) | |
| 216 return new FlimflamIPConfigClientImpl(bus); | |
| 217 else | |
| 218 return new FlimflamIPConfigClientStubImpl(); | |
| 219 } | |
| 220 | |
| 221 } // namespace chromeos | |
| OLD | NEW |