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