Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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_third_party_vpn_driver_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/macros.h" | |
|
pneubeck (no reviews)
2014/11/10 16:44:07
nit: not required if included in the header (see c
kaliamoorthi
2014/11/11 14:58:33
Done.
| |
| 9 #include "chromeos/dbus/shill_third_party_vpn_observer.h" | |
| 10 #include "dbus/bus.h" | |
| 11 #include "dbus/message.h" | |
| 12 #include "dbus/object_proxy.h" | |
| 13 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 14 | |
| 15 namespace chromeos { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 const char* kSetParametersKeyList[] = { | |
| 20 shill::kAddressParameterThirdPartyVpn, | |
| 21 shill::kBroadcastAddressParameterThirdPartyVpn, | |
| 22 shill::kGatewayParameterThirdPartyVpn, | |
| 23 shill::kBypassTunnelForIpParameterThirdPartyVpn, | |
| 24 shill::kSubnetPrefixParameterThirdPartyVpn, | |
| 25 shill::kMtuParameterThirdPartyVpn, | |
| 26 shill::kDomainSearchParameterThirdPartyVpn, | |
| 27 shill::kDnsServersParameterThirdPartyVpn}; | |
| 28 | |
| 29 // The ShillThirdPartyVpnDriverClient implementation. | |
| 30 class ShillThirdPartyVpnDriverClientImpl | |
| 31 : public ShillThirdPartyVpnDriverClient { | |
| 32 public: | |
| 33 ShillThirdPartyVpnDriverClientImpl(); | |
| 34 ~ShillThirdPartyVpnDriverClientImpl() override; | |
| 35 | |
| 36 // ShillThirdPartyVpnDriverClient overrides | |
| 37 void AddShillThirdPartyVpnObserver( | |
| 38 const dbus::ObjectPath& object_path, | |
| 39 ShillThirdPartyVpnObserver* observer) override; | |
| 40 | |
| 41 void RemoveShillThirdPartyVpnObserver( | |
| 42 const dbus::ObjectPath& object_path) override; | |
| 43 | |
| 44 void SetParameters( | |
| 45 const dbus::ObjectPath& object_path, | |
| 46 const base::DictionaryValue& parameters, | |
| 47 const base::Closure& callback, | |
| 48 const ShillClientHelper::ErrorCallback& error_callback) override; | |
| 49 | |
| 50 void UpdateConnectionState( | |
| 51 const dbus::ObjectPath& object_path, | |
| 52 const uint32 connection_state, | |
| 53 const base::Closure& callback, | |
| 54 const ShillClientHelper::ErrorCallback& error_callback) override; | |
| 55 | |
| 56 void SendPacket( | |
| 57 const dbus::ObjectPath& object_path, | |
| 58 const std::string& ip_packet, | |
| 59 const base::Closure& callback, | |
| 60 const ShillClientHelper::ErrorCallback& error_callback) override; | |
| 61 | |
| 62 protected: | |
| 63 void Init(dbus::Bus* bus) override { bus_ = bus; } | |
| 64 | |
| 65 ShillThirdPartyVpnDriverClient::TestInterface* GetTestInterface() override { | |
| 66 return NULL; | |
|
pneubeck (no reviews)
2014/11/10 16:44:07
nit: NULL -> nullptr , also everywhere else where
kaliamoorthi
2014/11/11 14:58:33
Done.
pneubeck (no reviews)
2014/11/12 14:21:07
not done?
please run s/NULL/nullptr/ over your who
| |
| 67 } | |
| 68 | |
| 69 private: | |
| 70 class HelperInfo { | |
| 71 public: | |
| 72 explicit HelperInfo(dbus::ObjectProxy* object_proxy); | |
| 73 ShillClientHelper* helper() { return &helper_; } | |
| 74 ShillThirdPartyVpnObserver* observer() { return observer_; } | |
| 75 | |
| 76 void set_observer(ShillThirdPartyVpnObserver* observer) { | |
| 77 observer_ = observer; | |
| 78 } | |
| 79 | |
| 80 base::WeakPtr<HelperInfo> GetWeakPtr() { | |
| 81 return weak_ptr_factory_.GetWeakPtr(); | |
| 82 } | |
| 83 | |
| 84 private: | |
| 85 ShillClientHelper helper_; | |
| 86 ShillThirdPartyVpnObserver* observer_; | |
| 87 | |
| 88 base::WeakPtrFactory<HelperInfo> weak_ptr_factory_; | |
| 89 }; | |
| 90 typedef std::map<std::string, HelperInfo*> HelperMap; | |
|
pneubeck (no reviews)
2014/11/10 16:44:08
typedef -> using
kaliamoorthi
2014/11/11 14:58:33
Done.
| |
| 91 | |
| 92 void ReleaseHelper(const dbus::ObjectPath& object_path); | |
|
pneubeck (no reviews)
2014/11/10 16:44:07
please make it consistent whether you define funct
pneubeck (no reviews)
2014/11/10 16:44:08
'Release' is misleading. This term is usually used
kaliamoorthi
2014/11/11 14:58:33
Done.
kaliamoorthi
2014/11/11 14:58:33
Done.
| |
| 93 | |
| 94 static void OnPacketReceived(base::WeakPtr<HelperInfo> helper_info, | |
| 95 dbus::Signal* signal); | |
| 96 static void OnPlatformMessage(base::WeakPtr<HelperInfo> helper_info, | |
| 97 dbus::Signal* signal); | |
| 98 static void OnSignalConnected(const std::string& interface, | |
| 99 const std::string& signal, | |
| 100 bool success); | |
| 101 | |
| 102 // Returns the corresponding ShillClientHelper for the object_path. | |
|
pneubeck (no reviews)
2014/11/10 16:44:07
'Returns or creates'
kaliamoorthi
2014/11/11 14:58:33
Done.
| |
| 103 ShillClientHelper* GetHelper(const dbus::ObjectPath& object_path) { | |
| 104 return GetHelperInfo(object_path)->helper(); | |
| 105 } | |
| 106 | |
| 107 HelperInfo* FindHelperInfo(const dbus::ObjectPath& object_path) { | |
| 108 HelperMap::iterator it = helpers_.find(object_path.value()); | |
| 109 return (it != helpers_.end()) ? it->second : NULL; | |
| 110 } | |
| 111 | |
| 112 // Returns the corresponding HelperInfo for the object_path. | |
|
pneubeck (no reviews)
2014/11/10 16:44:07
'Returns or creates'
kaliamoorthi
2014/11/11 14:58:33
Done.
| |
| 113 HelperInfo* GetHelperInfo(const dbus::ObjectPath& object_path) { | |
| 114 HelperInfo* helper_info = FindHelperInfo(object_path); | |
| 115 if (helper_info) | |
| 116 return helper_info; | |
| 117 | |
| 118 // There is no helper for the profile, create it. | |
| 119 dbus::ObjectProxy* object_proxy = | |
| 120 bus_->GetObjectProxy(shill::kFlimflamServiceName, object_path); | |
| 121 helper_info = new HelperInfo(object_proxy); | |
| 122 helpers_.insert(HelperMap::value_type(object_path.value(), helper_info)); | |
|
pneubeck (no reviews)
2014/11/10 16:44:08
nit: using operator[] seems easier to read
kaliamoorthi
2014/11/11 14:58:33
Done.
| |
| 123 return helper_info; | |
| 124 } | |
| 125 | |
| 126 dbus::Bus* bus_; | |
| 127 HelperMap helpers_; | |
| 128 std::set<std::string> valid_keys_; | |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(ShillThirdPartyVpnDriverClientImpl); | |
| 131 }; | |
| 132 | |
| 133 ShillThirdPartyVpnDriverClientImpl::HelperInfo::HelperInfo( | |
| 134 dbus::ObjectProxy* object_proxy) | |
| 135 : helper_(object_proxy), observer_(NULL), weak_ptr_factory_(this) { | |
| 136 } | |
| 137 | |
| 138 ShillThirdPartyVpnDriverClientImpl::ShillThirdPartyVpnDriverClientImpl() | |
| 139 : bus_(NULL) { | |
| 140 for (uint32 i = 0; i < arraysize(kSetParametersKeyList); ++i) { | |
|
pneubeck (no reviews)
2014/11/10 16:44:07
nit: braces are not required
kaliamoorthi
2014/11/11 14:58:33
I guess this is a matter of style. I prefer it thi
| |
| 141 valid_keys_.insert(kSetParametersKeyList[i]); | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 ShillThirdPartyVpnDriverClientImpl::~ShillThirdPartyVpnDriverClientImpl() { | |
| 146 for (auto& iter : helpers_) { | |
| 147 HelperInfo* helper_info = iter.second; | |
| 148 bus_->RemoveObjectProxy( | |
| 149 shill::kFlimflamServiceName, | |
| 150 helper_info->helper()->object_proxy()->object_path(), | |
| 151 base::Bind(&base::DoNothing)); | |
| 152 delete helper_info; | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 void ShillThirdPartyVpnDriverClientImpl::AddShillThirdPartyVpnObserver( | |
| 157 const dbus::ObjectPath& object_path, | |
| 158 ShillThirdPartyVpnObserver* observer) { | |
| 159 HelperInfo* helper_info = GetHelperInfo(object_path); | |
| 160 if (helper_info->observer()) { | |
| 161 LOG(ERROR) << "Observer exists for " << object_path.value(); | |
| 162 return; | |
| 163 } | |
| 164 | |
| 165 helper_info->set_observer(observer); | |
| 166 dbus::ObjectProxy* proxy = | |
| 167 const_cast<dbus::ObjectProxy*>(helper_info->helper()->object_proxy()); | |
|
pneubeck (no reviews)
2014/11/10 16:44:07
please try to get rid of the const_cast.
e.g. make
kaliamoorthi
2014/11/11 14:58:33
I agree that this needs to be done. However, I am
| |
| 168 | |
| 169 proxy->ConnectToSignal( | |
| 170 shill::kFlimflamThirdPartyVpnInterface, shill::kOnPlatformMessageFunction, | |
| 171 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnPlatformMessage, | |
| 172 helper_info->GetWeakPtr()), | |
| 173 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnSignalConnected)); | |
| 174 | |
| 175 proxy->ConnectToSignal( | |
| 176 shill::kFlimflamThirdPartyVpnInterface, shill::kOnPacketReceivedFunction, | |
| 177 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnPacketReceived, | |
| 178 helper_info->GetWeakPtr()), | |
| 179 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnSignalConnected)); | |
| 180 } | |
| 181 | |
| 182 void ShillThirdPartyVpnDriverClientImpl::RemoveShillThirdPartyVpnObserver( | |
| 183 const dbus::ObjectPath& object_path) { | |
| 184 HelperInfo* helper_info = FindHelperInfo(object_path); | |
| 185 if (!helper_info) { | |
| 186 LOG(ERROR) << "Unknown object_path " << object_path.value(); | |
| 187 return; | |
| 188 } | |
| 189 | |
| 190 CHECK(helper_info->observer()); | |
| 191 helper_info->set_observer(NULL); | |
| 192 ReleaseHelper(object_path); | |
|
pneubeck (no reviews)
2014/11/10 16:44:08
potential BUG!
ReleaseHelper will remove the help
kaliamoorthi
2014/11/11 14:58:33
We discussed this. But thanks for the thorough rev
pneubeck (no reviews)
2014/11/12 14:21:07
Consider that in the case that I explained, the ca
pneubeck (no reviews)
2014/11/13 14:06:11
Note that as long as this is not clarified this CL
| |
| 193 } | |
| 194 | |
| 195 void ShillThirdPartyVpnDriverClientImpl::ReleaseHelper( | |
| 196 const dbus::ObjectPath& object_path) { | |
| 197 HelperInfo* helper_info = FindHelperInfo(object_path); | |
| 198 if (!helper_info) { | |
| 199 LOG(ERROR) << "Unknown object_path " << object_path.value(); | |
| 200 return; | |
| 201 } | |
| 202 | |
| 203 bus_->RemoveObjectProxy(shill::kFlimflamServiceName, object_path, | |
| 204 base::Bind(&base::DoNothing)); | |
| 205 helpers_.erase(helpers_.find(object_path.value())); | |
| 206 delete helper_info; | |
| 207 } | |
| 208 | |
| 209 void ShillThirdPartyVpnDriverClientImpl::SetParameters( | |
| 210 const dbus::ObjectPath& object_path, | |
| 211 const base::DictionaryValue& parameters, | |
| 212 const base::Closure& callback, | |
| 213 const ShillClientHelper::ErrorCallback& error_callback) { | |
| 214 dbus::MethodCall method_call(shill::kFlimflamThirdPartyVpnInterface, | |
| 215 shill::kSetParametersFunction); | |
| 216 dbus::MessageWriter writer(&method_call); | |
| 217 dbus::MessageWriter array_writer(NULL); | |
| 218 writer.OpenArray("{ss}", &array_writer); | |
| 219 for (base::DictionaryValue::Iterator it(parameters); !it.IsAtEnd(); | |
| 220 it.Advance()) { | |
| 221 std::string value; | |
|
pneubeck (no reviews)
2014/11/10 16:44:07
nit: move to first usage
kaliamoorthi
2014/11/11 14:58:33
Done.
| |
| 222 if (valid_keys_.find(it.key()) == valid_keys_.end()) { | |
| 223 LOG(WARNING) << "Unknown key " << it.key(); | |
| 224 continue; | |
| 225 } | |
| 226 if (!it.value().GetAsString(&value)) { | |
| 227 LOG(WARNING) << "Non string value " << it.value(); | |
| 228 continue; | |
| 229 } | |
| 230 dbus::MessageWriter entry_writer(NULL); | |
| 231 array_writer.OpenDictEntry(&entry_writer); | |
| 232 entry_writer.AppendString(it.key()); | |
| 233 entry_writer.AppendString(value); | |
| 234 array_writer.CloseContainer(&entry_writer); | |
| 235 } | |
| 236 writer.CloseContainer(&array_writer); | |
| 237 GetHelper(object_path) | |
| 238 ->CallVoidMethodWithErrorCallback(&method_call, callback, error_callback); | |
|
pneubeck (no reviews)
2014/11/10 16:44:07
here you're creating a helper that you only delete
kaliamoorthi
2014/11/11 14:58:33
We discussed this. To many object creation an dele
| |
| 239 } | |
| 240 | |
| 241 void ShillThirdPartyVpnDriverClientImpl::UpdateConnectionState( | |
| 242 const dbus::ObjectPath& object_path, | |
| 243 const uint32 connection_state, | |
| 244 const base::Closure& callback, | |
| 245 const ShillClientHelper::ErrorCallback& error_callback) { | |
| 246 dbus::MethodCall method_call(shill::kFlimflamThirdPartyVpnInterface, | |
| 247 shill::kUpdateConnectionStateFunction); | |
| 248 dbus::MessageWriter writer(&method_call); | |
| 249 writer.AppendUint32(connection_state); | |
| 250 GetHelper(object_path) | |
| 251 ->CallVoidMethodWithErrorCallback(&method_call, callback, error_callback); | |
| 252 } | |
| 253 | |
| 254 void ShillThirdPartyVpnDriverClientImpl::SendPacket( | |
| 255 const dbus::ObjectPath& object_path, | |
| 256 const std::string& ip_packet, | |
| 257 const base::Closure& callback, | |
| 258 const ShillClientHelper::ErrorCallback& error_callback) { | |
| 259 dbus::MethodCall method_call(shill::kFlimflamThirdPartyVpnInterface, | |
| 260 shill::kSendPacketFunction); | |
| 261 dbus::MessageWriter writer(&method_call); | |
| 262 writer.AppendArrayOfBytes(reinterpret_cast<const uint8*>(ip_packet.data()), | |
| 263 ip_packet.size()); | |
| 264 GetHelper(object_path) | |
| 265 ->CallVoidMethodWithErrorCallback(&method_call, callback, error_callback); | |
| 266 } | |
| 267 | |
| 268 // static | |
| 269 void ShillThirdPartyVpnDriverClientImpl::OnPacketReceived( | |
| 270 base::WeakPtr<HelperInfo> helper_info, | |
| 271 dbus::Signal* signal) { | |
| 272 if (!helper_info || !helper_info->observer()) | |
| 273 return; | |
| 274 | |
| 275 dbus::MessageReader reader(signal); | |
| 276 const uint8* data; | |
|
pneubeck (no reviews)
2014/11/10 16:44:07
please initialize all primitives explicitly, just
kaliamoorthi
2014/11/11 14:58:33
Done.
| |
| 277 size_t length; | |
|
pneubeck (no reviews)
2014/11/10 16:44:08
ditto: initialize
kaliamoorthi
2014/11/11 14:58:33
Done.
| |
| 278 if (reader.PopArrayOfBytes(&data, &length)) | |
| 279 helper_info->observer()->OnPacketReceived(data, length); | |
| 280 } | |
| 281 | |
| 282 // static | |
| 283 void ShillThirdPartyVpnDriverClientImpl::OnPlatformMessage( | |
| 284 base::WeakPtr<HelperInfo> helper_info, | |
| 285 dbus::Signal* signal) { | |
| 286 if (!helper_info || !helper_info->observer()) | |
| 287 return; | |
| 288 | |
| 289 dbus::MessageReader reader(signal); | |
| 290 uint32 platform_message; | |
|
pneubeck (no reviews)
2014/11/10 16:44:07
ditto: initialize
kaliamoorthi
2014/11/11 14:58:33
Done.
| |
| 291 if (reader.PopUint32(&platform_message)) | |
| 292 helper_info->observer()->OnPlatformMessage(platform_message); | |
| 293 } | |
| 294 | |
| 295 // static | |
| 296 void ShillThirdPartyVpnDriverClientImpl::OnSignalConnected( | |
| 297 const std::string& interface, | |
| 298 const std::string& signal, | |
| 299 bool success) { | |
| 300 LOG_IF(ERROR, !success) << "Connect to " << interface << " " << signal | |
| 301 << " failed."; | |
| 302 } | |
| 303 | |
| 304 } // namespace | |
| 305 | |
| 306 ShillThirdPartyVpnDriverClient::ShillThirdPartyVpnDriverClient() { | |
| 307 } | |
| 308 | |
| 309 ShillThirdPartyVpnDriverClient::~ShillThirdPartyVpnDriverClient() { | |
| 310 } | |
| 311 | |
| 312 // static | |
| 313 ShillThirdPartyVpnDriverClient* ShillThirdPartyVpnDriverClient::Create() { | |
| 314 return new ShillThirdPartyVpnDriverClientImpl(); | |
| 315 } | |
| 316 | |
| 317 } // namespace chromeos | |
| OLD | NEW |