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" | |
| 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(const dbus::ObjectPath& object_path, | |
| 45 const base::DictionaryValue& parameters, | |
| 46 const VoidDBusMethodCallback& callback) override; | |
| 47 | |
| 48 void UpdateConnectionState(const dbus::ObjectPath& object_path, | |
| 49 const uint32 connection_state, | |
| 50 const VoidDBusMethodCallback& callback) override; | |
| 51 | |
| 52 void SendPacket(const dbus::ObjectPath& object_path, | |
| 53 const std::vector<uint8>& ip_packet, | |
| 54 const VoidDBusMethodCallback& callback) override; | |
| 55 | |
| 56 protected: | |
| 57 void Init(dbus::Bus* bus) override { bus_ = bus; } | |
| 58 | |
| 59 ShillThirdPartyVpnDriverClient::TestInterface* GetTestInterface() override { | |
| 60 return NULL; | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 class HelperInfo { | |
| 65 public: | |
| 66 explicit HelperInfo(dbus::ObjectProxy* object_proxy); | |
| 67 ShillClientHelper* helper() { return &helper_; } | |
| 68 ShillThirdPartyVpnObserver* observer() { return observer_; } | |
| 69 | |
| 70 void set_observer(ShillThirdPartyVpnObserver* observer) { | |
| 71 observer_ = observer; | |
| 72 } | |
| 73 | |
| 74 base::WeakPtr<HelperInfo> GetWeakPtr() { | |
| 75 return weak_ptr_factory_.GetWeakPtr(); | |
| 76 } | |
| 77 | |
| 78 private: | |
| 79 ShillClientHelper helper_; | |
| 80 ShillThirdPartyVpnObserver* observer_; | |
| 81 | |
| 82 base::WeakPtrFactory<HelperInfo> weak_ptr_factory_; | |
| 83 }; | |
| 84 typedef std::map<std::string, HelperInfo*> HelperMap; | |
| 85 | |
| 86 void ReleaseHelper(const dbus::ObjectPath& object_path); | |
| 87 | |
| 88 static void OnPacketReceived(base::WeakPtr<HelperInfo> helper_info, | |
| 89 dbus::Signal* signal); | |
| 90 static void OnPlatformMessage(base::WeakPtr<HelperInfo> helper_info, | |
| 91 dbus::Signal* signal); | |
| 92 static void OnSignalConnected(const std::string& interface, | |
| 93 const std::string& signal, | |
| 94 bool success); | |
| 95 | |
| 96 // Returns the corresponding ShillClientHelper for the object_path. | |
| 97 ShillClientHelper* GetHelper(const dbus::ObjectPath& object_path) { | |
| 98 return GetHelperInfo(object_path)->helper(); | |
| 99 } | |
| 100 | |
| 101 HelperInfo* FindHelperInfo(const dbus::ObjectPath& object_path) { | |
| 102 HelperMap::iterator it = helpers_.find(object_path.value()); | |
| 103 return (it != helpers_.end()) ? it->second : NULL; | |
| 104 } | |
| 105 | |
| 106 // Returns the corresponding HelperInfo for the object_path. | |
| 107 HelperInfo* GetHelperInfo(const dbus::ObjectPath& object_path) { | |
| 108 HelperInfo* helper_info = FindHelperInfo(object_path); | |
| 109 if (helper_info) | |
| 110 return helper_info; | |
| 111 | |
| 112 // There is no helper for the profile, create it. | |
| 113 dbus::ObjectProxy* object_proxy = | |
| 114 bus_->GetObjectProxy(shill::kFlimflamServiceName, object_path); | |
| 115 helper_info = new HelperInfo(object_proxy); | |
| 116 helpers_.insert(HelperMap::value_type(object_path.value(), helper_info)); | |
| 117 return helper_info; | |
| 118 } | |
| 119 | |
| 120 dbus::Bus* bus_; | |
| 121 HelperMap helpers_; | |
| 122 std::set<std::string> valid_keys_; | |
| 123 | |
| 124 DISALLOW_COPY_AND_ASSIGN(ShillThirdPartyVpnDriverClientImpl); | |
| 125 }; | |
| 126 | |
| 127 ShillThirdPartyVpnDriverClientImpl::HelperInfo::HelperInfo( | |
| 128 dbus::ObjectProxy* object_proxy) | |
| 129 : helper_(object_proxy), observer_(NULL), weak_ptr_factory_(this) { | |
| 130 } | |
| 131 | |
| 132 ShillThirdPartyVpnDriverClientImpl::ShillThirdPartyVpnDriverClientImpl() | |
| 133 : bus_(NULL) { | |
| 134 for (uint32 i = 0; i < arraysize(kSetParametersKeyList); ++i) { | |
| 135 valid_keys_.insert(kSetParametersKeyList[i]); | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 ShillThirdPartyVpnDriverClientImpl::~ShillThirdPartyVpnDriverClientImpl() { | |
| 140 for (auto& iter : helpers_) { | |
| 141 HelperInfo* helper_info = iter.second; | |
| 142 bus_->RemoveObjectProxy( | |
| 143 shill::kFlimflamServiceName, | |
| 144 helper_info->helper()->object_proxy()->object_path(), | |
| 145 base::Bind(&base::DoNothing)); | |
| 146 delete helper_info; | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 void ShillThirdPartyVpnDriverClientImpl::AddShillThirdPartyVpnObserver( | |
| 151 const dbus::ObjectPath& object_path, | |
| 152 ShillThirdPartyVpnObserver* observer) { | |
| 153 HelperInfo* helper_info = GetHelperInfo(object_path); | |
| 154 if (helper_info->observer()) { | |
| 155 LOG(ERROR) << "Observer exists for " << object_path.value(); | |
| 156 return; | |
| 157 } | |
| 158 | |
| 159 helper_info->set_observer(observer); | |
| 160 dbus::ObjectProxy* proxy = | |
| 161 const_cast<dbus::ObjectProxy*>(helper_info->helper()->object_proxy()); | |
| 162 | |
| 163 proxy->ConnectToSignal( | |
| 164 shill::kFlimflamThirdPartyVpnInterface, shill::kOnPlatformMessageFunction, | |
| 165 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnPlatformMessage, | |
| 166 helper_info->GetWeakPtr()), | |
| 167 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnSignalConnected)); | |
| 168 | |
| 169 proxy->ConnectToSignal( | |
| 170 shill::kFlimflamThirdPartyVpnInterface, shill::kOnPacketReceivedFunction, | |
| 171 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnPacketReceived, | |
| 172 helper_info->GetWeakPtr()), | |
| 173 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnSignalConnected)); | |
| 174 } | |
| 175 | |
| 176 void ShillThirdPartyVpnDriverClientImpl::RemoveShillThirdPartyVpnObserver( | |
| 177 const dbus::ObjectPath& object_path) { | |
| 178 HelperInfo* helper_info = FindHelperInfo(object_path); | |
| 179 if (!helper_info) { | |
| 180 LOG(ERROR) << "Unknown object_path " << object_path.value(); | |
| 181 return; | |
| 182 } | |
| 183 | |
| 184 CHECK(helper_info->observer()); | |
| 185 helper_info->set_observer(NULL); | |
| 186 ReleaseHelper(object_path); | |
| 187 } | |
| 188 | |
| 189 void ShillThirdPartyVpnDriverClientImpl::ReleaseHelper( | |
| 190 const dbus::ObjectPath& object_path) { | |
| 191 HelperInfo* helper_info = FindHelperInfo(object_path); | |
| 192 if (!helper_info) { | |
| 193 LOG(ERROR) << "Unknown object_path " << object_path.value(); | |
| 194 return; | |
| 195 } | |
| 196 | |
| 197 bus_->RemoveObjectProxy(shill::kFlimflamServiceName, object_path, | |
| 198 base::Bind(&base::DoNothing)); | |
| 199 helpers_.erase(helpers_.find(object_path.value())); | |
| 200 delete helper_info; | |
| 201 } | |
| 202 | |
| 203 void ShillThirdPartyVpnDriverClientImpl::SetParameters( | |
| 204 const dbus::ObjectPath& object_path, | |
| 205 const base::DictionaryValue& parameters, | |
| 206 const VoidDBusMethodCallback& callback) { | |
| 207 dbus::MethodCall method_call(shill::kFlimflamThirdPartyVpnInterface, | |
| 208 shill::kSetParametersFunction); | |
| 209 dbus::MessageWriter writer(&method_call); | |
| 210 dbus::MessageWriter array_writer(NULL); | |
| 211 writer.OpenArray("{ss}", &array_writer); | |
| 212 for (base::DictionaryValue::Iterator it(parameters); !it.IsAtEnd(); | |
| 213 it.Advance()) { | |
| 214 std::string value; | |
| 215 if (valid_keys_.find(it.key()) == valid_keys_.end()) { | |
| 216 LOG(WARNING) << "Unknown key " << it.key(); | |
| 217 continue; | |
| 218 } else if (!it.value().GetAsString(&value)) { | |
|
stevenjb
2014/11/03 16:55:23
no else after continue or return
kaliamoorthi
2014/11/04 12:58:59
Done.
| |
| 219 LOG(WARNING) << "Non string value " << it.value(); | |
| 220 continue; | |
| 221 } | |
| 222 dbus::MessageWriter entry_writer(NULL); | |
| 223 array_writer.OpenDictEntry(&entry_writer); | |
| 224 entry_writer.AppendString(it.key()); | |
| 225 entry_writer.AppendString(value); | |
| 226 array_writer.CloseContainer(&entry_writer); | |
| 227 } | |
| 228 writer.CloseContainer(&array_writer); | |
| 229 GetHelper(object_path)->CallVoidMethod(&method_call, callback); | |
| 230 } | |
| 231 | |
| 232 void ShillThirdPartyVpnDriverClientImpl::UpdateConnectionState( | |
| 233 const dbus::ObjectPath& object_path, | |
| 234 const uint32 connection_state, | |
| 235 const VoidDBusMethodCallback& callback) { | |
| 236 dbus::MethodCall method_call(shill::kFlimflamThirdPartyVpnInterface, | |
| 237 shill::kUpdateConnectionStateFunction); | |
| 238 dbus::MessageWriter writer(&method_call); | |
| 239 writer.AppendUint32(connection_state); | |
| 240 GetHelper(object_path)->CallVoidMethod(&method_call, callback); | |
| 241 } | |
| 242 | |
| 243 void ShillThirdPartyVpnDriverClientImpl::SendPacket( | |
| 244 const dbus::ObjectPath& object_path, | |
| 245 const std::vector<uint8>& ip_packet, | |
| 246 const VoidDBusMethodCallback& callback) { | |
| 247 dbus::MethodCall method_call(shill::kFlimflamThirdPartyVpnInterface, | |
| 248 shill::kSendPacketFunction); | |
| 249 dbus::MessageWriter writer(&method_call); | |
| 250 writer.AppendArrayOfBytes(ip_packet.data(), ip_packet.size()); | |
| 251 GetHelper(object_path)->CallVoidMethod(&method_call, callback); | |
| 252 } | |
| 253 | |
| 254 // static | |
| 255 void ShillThirdPartyVpnDriverClientImpl::OnPacketReceived( | |
| 256 base::WeakPtr<HelperInfo> helper_info, | |
| 257 dbus::Signal* signal) { | |
| 258 if (!helper_info || !helper_info->observer()) | |
| 259 return; | |
| 260 | |
| 261 dbus::MessageReader reader(signal); | |
| 262 const uint8* data; | |
| 263 size_t length; | |
| 264 if (reader.PopArrayOfBytes(&data, &length)) | |
| 265 helper_info->observer()->OnPacketReceived(data, length); | |
| 266 } | |
| 267 | |
| 268 // static | |
| 269 void ShillThirdPartyVpnDriverClientImpl::OnPlatformMessage( | |
| 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 uint32 platform_message; | |
| 277 if (reader.PopUint32(&platform_message)) | |
| 278 helper_info->observer()->OnPlatformMessage(platform_message); | |
| 279 } | |
| 280 | |
| 281 // static | |
| 282 void ShillThirdPartyVpnDriverClientImpl::OnSignalConnected( | |
| 283 const std::string& interface, | |
| 284 const std::string& signal, | |
| 285 bool success) { | |
| 286 LOG_IF(ERROR, !success) << "Connect to " << interface << " " << signal | |
| 287 << " failed."; | |
| 288 } | |
| 289 | |
| 290 } // namespace | |
| 291 | |
| 292 ShillThirdPartyVpnDriverClient::ShillThirdPartyVpnDriverClient() { | |
| 293 } | |
| 294 | |
| 295 ShillThirdPartyVpnDriverClient::~ShillThirdPartyVpnDriverClient() { | |
| 296 } | |
| 297 | |
| 298 // static | |
| 299 ShillThirdPartyVpnDriverClient* ShillThirdPartyVpnDriverClient::Create() { | |
| 300 return new ShillThirdPartyVpnDriverClientImpl(); | |
| 301 } | |
| 302 | |
| 303 } // namespace chromeos | |
| OLD | NEW |