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 "chromeos/dbus/shill_third_party_vpn_observer.h" | |
| 9 #include "dbus/bus.h" | |
| 10 #include "dbus/message.h" | |
| 11 #include "dbus/object_proxy.h" | |
| 12 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 13 | |
| 14 namespace shill { | |
| 15 | |
| 16 // TODO(kaliamoorthi): Move these constants to dbus/service_constants.h | |
| 17 const char kFlimflamThirdPartyVpnInterface[] = | |
| 18 "org.chromium.flimflam.ThirdPartyVpn"; | |
| 19 const char kSetParametersFunction[] = "SetParameters"; | |
| 20 const char kSendPacketFunction[] = "SendPacket"; | |
| 21 const char kUpdateConnectionStateFunction[] = "UpdateConnectionState"; | |
| 22 const char kOnPacketReceivedFunction[] = "OnPacketReceived"; | |
| 23 const char kOnPlatformMessageFunction[] = "OnPlatformMessage"; | |
| 24 const char* kSetParametersKeyList[] = {"address", | |
| 25 "broadcast_address", | |
| 26 "gateway", | |
| 27 "bypass_tunnel_for_ip", | |
| 28 "subnet_prefix", | |
| 29 "mtu", | |
| 30 "domain_search", | |
| 31 "dns_servers"}; | |
| 32 } // namespace shill | |
| 33 | |
| 34 namespace chromeos { | |
| 35 | |
| 36 namespace { | |
| 37 | |
| 38 // The ShillThirdPartyVpnDriverClient implementation. | |
| 39 class ShillThirdPartyVpnDriverClientImpl | |
| 40 : public ShillThirdPartyVpnDriverClient { | |
| 41 public: | |
| 42 ShillThirdPartyVpnDriverClientImpl(); | |
| 43 ~ShillThirdPartyVpnDriverClientImpl() override; | |
| 44 | |
|
stevenjb
2014/10/30 20:55:36
// ShillThirdPartyVpnDriverClient
kaliamoorthi
2014/10/31 11:03:48
Done.
| |
| 45 void AddShillThirdPartyVpnObserver( | |
| 46 const dbus::ObjectPath& object_path, | |
| 47 ShillThirdPartyVpnObserver* observer) override; | |
| 48 | |
| 49 void RemoveShillThirdPartyVpnObserver( | |
| 50 const dbus::ObjectPath& object_path) override; | |
| 51 | |
| 52 void SetParameters(const dbus::ObjectPath& object_path, | |
| 53 const base::DictionaryValue& parameters, | |
| 54 const VoidDBusMethodCallback& callback) override; | |
| 55 | |
| 56 void UpdateConnectionState(const dbus::ObjectPath& object_path, | |
| 57 const uint32 connection_state, | |
| 58 const VoidDBusMethodCallback& callback) override; | |
| 59 | |
| 60 void SendPacket(const dbus::ObjectPath& object_path, | |
| 61 const std::vector<uint8>& ip_packet, | |
| 62 const VoidDBusMethodCallback& callback) override; | |
| 63 | |
| 64 protected: | |
| 65 void Init(dbus::Bus* bus) override { bus_ = bus; } | |
| 66 | |
| 67 ShillThirdPartyVpnDriverClient::TestInterface* GetTestInterface() override { | |
| 68 return NULL; | |
| 69 } | |
| 70 | |
| 71 private: | |
| 72 struct HelperInfo { | |
| 73 explicit HelperInfo(dbus::ObjectProxy* object_proxy); | |
| 74 ShillClientHelper helper; | |
| 75 ShillThirdPartyVpnObserver* observer; | |
| 76 }; | |
| 77 typedef std::map<std::string, HelperInfo*> HelperMap; | |
| 78 | |
| 79 void ReleaseHelper(const dbus::ObjectPath& object_path); | |
| 80 | |
| 81 static void OnPacketReceived(HelperInfo* helper_info, dbus::Signal* signal); | |
| 82 static void OnPlatformMessage(HelperInfo* helper_info, dbus::Signal* signal); | |
| 83 static void OnSignalConnected(const std::string& interface, | |
| 84 const std::string& signal, | |
| 85 bool success); | |
| 86 | |
| 87 // Returns the corresponding ShillClientHelper for the profile. | |
| 88 ShillClientHelper* GetHelper(const dbus::ObjectPath& service_path) { | |
| 89 HelperMap::iterator it = helpers_.find(service_path.value()); | |
| 90 if (it != helpers_.end()) | |
| 91 return &it->second->helper; | |
| 92 | |
| 93 // There is no helper for the profile, create it. | |
| 94 dbus::ObjectProxy* object_proxy = | |
| 95 bus_->GetObjectProxy(shill::kFlimflamServiceName, service_path); | |
| 96 HelperInfo* helper_info = new HelperInfo(object_proxy); | |
| 97 helpers_.insert(HelperMap::value_type(service_path.value(), helper_info)); | |
| 98 return &helper_info->helper; | |
| 99 } | |
| 100 | |
| 101 dbus::Bus* bus_; | |
| 102 HelperMap helpers_; | |
| 103 std::set<std::string> valid_keys_; | |
| 104 | |
| 105 DISALLOW_COPY_AND_ASSIGN(ShillThirdPartyVpnDriverClientImpl); | |
| 106 }; | |
| 107 | |
| 108 ShillThirdPartyVpnDriverClientImpl::HelperInfo::HelperInfo( | |
| 109 dbus::ObjectProxy* object_proxy) | |
| 110 : helper(object_proxy), observer(NULL) { | |
| 111 } | |
| 112 | |
| 113 ShillThirdPartyVpnDriverClientImpl::ShillThirdPartyVpnDriverClientImpl() | |
| 114 : bus_(NULL) { | |
| 115 for (uint32 i = 0; i < sizeof(shill::kSetParametersKeyList) / | |
| 116 sizeof(shill::kSetParametersKeyList[0]); | |
|
stevenjb
2014/10/30 20:55:36
Ugh, clang format did this? Maybe use a tmp for si
kaliamoorthi
2014/10/31 11:03:47
Done.
| |
| 117 ++i) { | |
| 118 valid_keys_.insert(shill::kSetParametersKeyList[i]); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 ShillThirdPartyVpnDriverClientImpl::~ShillThirdPartyVpnDriverClientImpl() { | |
| 123 for (HelperMap::iterator iter = helpers_.begin(); iter != helpers_.end(); | |
| 124 ++iter) { | |
| 125 HelperInfo* helper_info = iter->second; | |
| 126 bus_->RemoveObjectProxy(shill::kFlimflamServiceName, | |
| 127 helper_info->helper.object_proxy()->object_path(), | |
| 128 base::Bind(&base::DoNothing)); | |
| 129 delete helper_info; | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 void ShillThirdPartyVpnDriverClientImpl::AddShillThirdPartyVpnObserver( | |
| 134 const dbus::ObjectPath& object_path, | |
| 135 ShillThirdPartyVpnObserver* observer) { | |
| 136 bus_->AssertOnOriginThread(); | |
| 137 HelperMap::iterator it = helpers_.find(object_path.value()); | |
| 138 if (it == helpers_.end()) { | |
| 139 LOG(ERROR) << "Unknown object_path " << object_path.value(); | |
| 140 return; | |
| 141 } | |
|
stevenjb
2014/10/30 20:55:36
This pattern is used in several places, consider w
kaliamoorthi
2014/10/31 11:03:48
Done.
| |
| 142 | |
| 143 HelperInfo* helper_info = it->second; | |
| 144 CHECK(helper_info->observer == NULL); | |
| 145 helper_info->observer = observer; | |
| 146 dbus::ObjectProxy* proxy = | |
| 147 const_cast<dbus::ObjectProxy*>(helper_info->helper.object_proxy()); | |
| 148 | |
| 149 proxy->ConnectToSignal( | |
| 150 shill::kFlimflamThirdPartyVpnInterface, shill::kOnPlatformMessageFunction, | |
| 151 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnPlatformMessage, | |
| 152 helper_info), | |
| 153 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnSignalConnected)); | |
| 154 | |
| 155 proxy->ConnectToSignal( | |
| 156 shill::kFlimflamThirdPartyVpnInterface, shill::kOnPacketReceivedFunction, | |
| 157 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnPacketReceived, | |
| 158 helper_info), | |
| 159 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnSignalConnected)); | |
| 160 } | |
| 161 | |
| 162 void ShillThirdPartyVpnDriverClientImpl::RemoveShillThirdPartyVpnObserver( | |
| 163 const dbus::ObjectPath& object_path) { | |
| 164 bus_->AssertOnOriginThread(); | |
| 165 HelperMap::iterator it = helpers_.find(object_path.value()); | |
| 166 if (it == helpers_.end()) { | |
| 167 LOG(ERROR) << "Unknown object_path " << object_path.value(); | |
| 168 return; | |
| 169 } | |
| 170 | |
| 171 HelperInfo* helper_info = it->second; | |
| 172 CHECK(helper_info->observer); | |
| 173 helper_info->observer = NULL; | |
| 174 ReleaseHelper(object_path); | |
| 175 } | |
| 176 | |
| 177 void ShillThirdPartyVpnDriverClientImpl::ReleaseHelper( | |
| 178 const dbus::ObjectPath& object_path) { | |
| 179 HelperMap::iterator it = helpers_.find(object_path.value()); | |
| 180 if (it == helpers_.end()) { | |
| 181 LOG(ERROR) << "Unknown object_path " << object_path.value(); | |
| 182 return; | |
| 183 } | |
| 184 | |
| 185 HelperInfo* helper_info = it->second; | |
| 186 bus_->RemoveObjectProxy(shill::kFlimflamServiceName, object_path, | |
| 187 base::Bind(&base::DoNothing)); | |
| 188 helpers_.erase(it); | |
| 189 delete helper_info; | |
| 190 } | |
| 191 | |
| 192 void ShillThirdPartyVpnDriverClientImpl::SetParameters( | |
| 193 const dbus::ObjectPath& object_path, | |
| 194 const base::DictionaryValue& parameters, | |
| 195 const VoidDBusMethodCallback& callback) { | |
| 196 dbus::MethodCall method_call(shill::kFlimflamThirdPartyVpnInterface, | |
| 197 shill::kSetParametersFunction); | |
| 198 dbus::MessageWriter writer(&method_call); | |
| 199 dbus::MessageWriter array_writer(NULL); | |
| 200 writer.OpenArray("{ss}", &array_writer); | |
| 201 for (base::DictionaryValue::Iterator it(parameters); !it.IsAtEnd(); | |
| 202 it.Advance()) { | |
| 203 std::string value; | |
| 204 if (valid_keys_.find(it.key()) != valid_keys_.end() && | |
| 205 it.value().GetAsString(&value)) { | |
|
stevenjb
2014/10/30 20:55:36
Invert logic, LOG(WARNING), and continue
kaliamoorthi
2014/10/31 11:03:47
Done.
| |
| 206 dbus::MessageWriter entry_writer(NULL); | |
| 207 array_writer.OpenDictEntry(&entry_writer); | |
| 208 entry_writer.AppendString(it.key()); | |
| 209 entry_writer.AppendString(value); | |
| 210 array_writer.CloseContainer(&entry_writer); | |
| 211 } | |
| 212 } | |
| 213 writer.CloseContainer(&array_writer); | |
| 214 GetHelper(object_path)->CallVoidMethod(&method_call, callback); | |
| 215 } | |
| 216 | |
| 217 void ShillThirdPartyVpnDriverClientImpl::UpdateConnectionState( | |
| 218 const dbus::ObjectPath& object_path, | |
| 219 const uint32 connection_state, | |
| 220 const VoidDBusMethodCallback& callback) { | |
| 221 dbus::MethodCall method_call(shill::kFlimflamThirdPartyVpnInterface, | |
| 222 shill::kUpdateConnectionStateFunction); | |
| 223 dbus::MessageWriter writer(&method_call); | |
| 224 writer.AppendUint32(connection_state); | |
| 225 GetHelper(object_path)->CallVoidMethod(&method_call, callback); | |
| 226 } | |
| 227 | |
| 228 void ShillThirdPartyVpnDriverClientImpl::SendPacket( | |
| 229 const dbus::ObjectPath& object_path, | |
| 230 const std::vector<uint8>& ip_packet, | |
| 231 const VoidDBusMethodCallback& callback) { | |
| 232 dbus::MethodCall method_call(shill::kFlimflamThirdPartyVpnInterface, | |
| 233 shill::kSendPacketFunction); | |
| 234 dbus::MessageWriter writer(&method_call); | |
| 235 writer.AppendArrayOfBytes(ip_packet.data(), ip_packet.size()); | |
| 236 GetHelper(object_path)->CallVoidMethod(&method_call, callback); | |
| 237 } | |
| 238 | |
| 239 // static | |
| 240 void ShillThirdPartyVpnDriverClientImpl::OnPacketReceived( | |
| 241 ShillThirdPartyVpnDriverClientImpl::HelperInfo* helper_info, | |
| 242 dbus::Signal* signal) { | |
| 243 if (!helper_info->observer) | |
| 244 return; | |
| 245 | |
| 246 dbus::MessageReader reader(signal); | |
| 247 const uint8* data; | |
| 248 size_t length; | |
| 249 if (reader.PopArrayOfBytes(&data, &length)) | |
| 250 helper_info->observer->OnPacketReceived(data, length); | |
| 251 } | |
| 252 | |
| 253 // static | |
| 254 void ShillThirdPartyVpnDriverClientImpl::OnPlatformMessage( | |
| 255 ShillThirdPartyVpnDriverClientImpl::HelperInfo* helper_info, | |
| 256 dbus::Signal* signal) { | |
| 257 if (!helper_info->observer) | |
| 258 return; | |
| 259 | |
| 260 dbus::MessageReader reader(signal); | |
| 261 uint32 platform_message; | |
| 262 if (reader.PopUint32(&platform_message)) | |
| 263 helper_info->observer->OnPlatformMessage(platform_message); | |
| 264 } | |
| 265 | |
| 266 // static | |
| 267 void ShillThirdPartyVpnDriverClientImpl::OnSignalConnected( | |
| 268 const std::string& interface, | |
| 269 const std::string& signal, | |
| 270 bool success) { | |
| 271 LOG_IF(ERROR, !success) << "Connect to " << interface << " " << signal | |
| 272 << " failed."; | |
| 273 } | |
| 274 | |
| 275 } // namespace | |
| 276 | |
| 277 ShillThirdPartyVpnDriverClient::ShillThirdPartyVpnDriverClient() { | |
| 278 } | |
| 279 | |
| 280 ShillThirdPartyVpnDriverClient::~ShillThirdPartyVpnDriverClient() { | |
| 281 } | |
| 282 | |
| 283 // static | |
| 284 ShillThirdPartyVpnDriverClient* ShillThirdPartyVpnDriverClient::Create() { | |
| 285 return new ShillThirdPartyVpnDriverClientImpl(); | |
| 286 } | |
| 287 | |
| 288 } // namespace chromeos | |
| OLD | NEW |