Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(971)

Side by Side Diff: chromeos/dbus/shill_third_party_vpn_driver_client.cc

Issue 681723003: Add new shill client for VPN (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added unit tests Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 shill {
16
17 // TODO(kaliamoorthi): Move these constants to dbus/service_constants.cc
stevenjb 2014/10/31 16:35:41 The individual constants will be in service_consta
kaliamoorthi 2014/11/03 10:55:38 Done.
18 const char* kSetParametersKeyList[] = {kAddressParameterThirdPartyVpn,
19 kBroadcastAddressParameterThirdPartyVpn,
20 kGatewayParameterThirdPartyVpn,
21 kBypassTunnelForIpParameterThirdPartyVpn,
22 kSubnetPrefixParameterThirdPartyVpn,
23 kMtuParameterThirdPartyVpn,
24 kDomainSearchParameterThirdPartyVpn,
25 kDnsServersParameterThirdPartyVpn};
26
27 } // namespace shill
28
29 namespace chromeos {
30
31 namespace {
32
33 // The ShillThirdPartyVpnDriverClient implementation.
34 class ShillThirdPartyVpnDriverClientImpl
35 : public ShillThirdPartyVpnDriverClient {
36 public:
37 ShillThirdPartyVpnDriverClientImpl();
38 ~ShillThirdPartyVpnDriverClientImpl() override;
39
40 // ShillThirdPartyVpnDriverClient overrides
41 void AddShillThirdPartyVpnObserver(
42 const dbus::ObjectPath& object_path,
43 ShillThirdPartyVpnObserver* observer) override;
44
45 void RemoveShillThirdPartyVpnObserver(
46 const dbus::ObjectPath& object_path) override;
47
48 void SetParameters(const dbus::ObjectPath& object_path,
49 const base::DictionaryValue& parameters,
50 const VoidDBusMethodCallback& callback) override;
51
52 void UpdateConnectionState(const dbus::ObjectPath& object_path,
53 const uint32 connection_state,
54 const VoidDBusMethodCallback& callback) override;
55
56 void SendPacket(const dbus::ObjectPath& object_path,
57 const std::vector<uint8>& ip_packet,
58 const VoidDBusMethodCallback& callback) override;
59
60 protected:
61 void Init(dbus::Bus* bus) override { bus_ = bus; }
62
63 ShillThirdPartyVpnDriverClient::TestInterface* GetTestInterface() override {
64 return NULL;
65 }
66
67 private:
68 class HelperInfo {
69 public:
70 explicit HelperInfo(dbus::ObjectProxy* object_proxy);
71 ShillClientHelper* helper() { return &helper_; }
72 ShillThirdPartyVpnObserver* observer() { return observer_; }
73
74 void set_observer(ShillThirdPartyVpnObserver* observer) {
75 observer_ = observer;
76 }
77
78 base::WeakPtr<HelperInfo> GetWeakPtr() {
79 return weak_ptr_factory_.GetWeakPtr();
80 }
81
82 private:
83 ShillClientHelper helper_;
84 ShillThirdPartyVpnObserver* observer_;
85
86 base::WeakPtrFactory<HelperInfo> weak_ptr_factory_;
87 };
88 typedef std::map<std::string, HelperInfo*> HelperMap;
89
90 void ReleaseHelper(const dbus::ObjectPath& object_path);
91
92 static void OnPacketReceived(base::WeakPtr<HelperInfo> helper_info,
93 dbus::Signal* signal);
94 static void OnPlatformMessage(base::WeakPtr<HelperInfo> helper_info,
95 dbus::Signal* signal);
96 static void OnSignalConnected(const std::string& interface,
97 const std::string& signal,
98 bool success);
99
100 // Returns the corresponding ShillClientHelper for the object_path.
101 ShillClientHelper* GetHelper(const dbus::ObjectPath& object_path) {
102 return GetHelperInfo(object_path)->helper();
103 }
104
105 HelperInfo* FindHelperInfo(const dbus::ObjectPath& object_path) {
106 HelperMap::iterator it = helpers_.find(object_path.value());
107 return (it != helpers_.end()) ? it->second : NULL;
108 }
109
110 // Returns the corresponding HelperInfo for the object_path.
111 HelperInfo* GetHelperInfo(const dbus::ObjectPath& object_path) {
112 HelperInfo* helper_info = FindHelperInfo(object_path);
113 if (helper_info)
114 return helper_info;
115
116 // There is no helper for the profile, create it.
117 dbus::ObjectProxy* object_proxy =
118 bus_->GetObjectProxy(shill::kFlimflamServiceName, object_path);
119 helper_info = new HelperInfo(object_proxy);
120 helpers_.insert(HelperMap::value_type(object_path.value(), helper_info));
121 return helper_info;
122 }
123
124 dbus::Bus* bus_;
125 HelperMap helpers_;
126 std::set<std::string> valid_keys_;
127
128 DISALLOW_COPY_AND_ASSIGN(ShillThirdPartyVpnDriverClientImpl);
129 };
130
131 ShillThirdPartyVpnDriverClientImpl::HelperInfo::HelperInfo(
132 dbus::ObjectProxy* object_proxy)
133 : helper_(object_proxy), observer_(NULL), weak_ptr_factory_(this) {
134 }
135
136 ShillThirdPartyVpnDriverClientImpl::ShillThirdPartyVpnDriverClientImpl()
137 : bus_(NULL) {
138 for (uint32 i = 0; i < arraysize(shill::kSetParametersKeyList); ++i) {
139 valid_keys_.insert(shill::kSetParametersKeyList[i]);
140 }
141 }
142
143 ShillThirdPartyVpnDriverClientImpl::~ShillThirdPartyVpnDriverClientImpl() {
144 for (HelperMap::iterator iter = helpers_.begin(); iter != helpers_.end();
145 ++iter) {
stevenjb 2014/10/31 16:35:41 nit: Use c++11 style iterators here and below: for
kaliamoorthi 2014/11/03 10:55:38 Done.
146 HelperInfo* helper_info = iter->second;
147 bus_->RemoveObjectProxy(
148 shill::kFlimflamServiceName,
149 helper_info->helper()->object_proxy()->object_path(),
150 base::Bind(&base::DoNothing));
151 delete helper_info;
152 }
153 }
154
155 void ShillThirdPartyVpnDriverClientImpl::AddShillThirdPartyVpnObserver(
156 const dbus::ObjectPath& object_path,
157 ShillThirdPartyVpnObserver* observer) {
158 HelperInfo* helper_info = GetHelperInfo(object_path);
159 if (helper_info->observer()) {
160 LOG(ERROR) << "Observer exists for " << object_path.value();
161 return;
162 }
163
164 helper_info->set_observer(observer);
165 dbus::ObjectProxy* proxy =
166 const_cast<dbus::ObjectProxy*>(helper_info->helper()->object_proxy());
167
168 proxy->ConnectToSignal(
169 shill::kFlimflamThirdPartyVpnInterface, shill::kOnPlatformMessageFunction,
170 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnPlatformMessage,
171 helper_info->GetWeakPtr()),
172 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnSignalConnected));
173
174 proxy->ConnectToSignal(
175 shill::kFlimflamThirdPartyVpnInterface, shill::kOnPacketReceivedFunction,
176 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnPacketReceived,
177 helper_info->GetWeakPtr()),
178 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnSignalConnected));
179 }
180
181 void ShillThirdPartyVpnDriverClientImpl::RemoveShillThirdPartyVpnObserver(
182 const dbus::ObjectPath& object_path) {
183 HelperInfo* helper_info = FindHelperInfo(object_path);
184 if (!helper_info) {
185 LOG(ERROR) << "Unknown object_path " << object_path.value();
186 return;
187 }
188
189 CHECK(helper_info->observer());
190 helper_info->set_observer(NULL);
191 ReleaseHelper(object_path);
192 }
193
194 void ShillThirdPartyVpnDriverClientImpl::ReleaseHelper(
195 const dbus::ObjectPath& object_path) {
196 HelperInfo* helper_info = FindHelperInfo(object_path);
197 if (!helper_info) {
198 LOG(ERROR) << "Unknown object_path " << object_path.value();
199 return;
200 }
201
202 bus_->RemoveObjectProxy(shill::kFlimflamServiceName, object_path,
203 base::Bind(&base::DoNothing));
204 helpers_.erase(helpers_.find(object_path.value()));
205 delete helper_info;
206 }
207
208 void ShillThirdPartyVpnDriverClientImpl::SetParameters(
209 const dbus::ObjectPath& object_path,
210 const base::DictionaryValue& parameters,
211 const VoidDBusMethodCallback& callback) {
212 dbus::MethodCall method_call(shill::kFlimflamThirdPartyVpnInterface,
213 shill::kSetParametersFunction);
214 dbus::MessageWriter writer(&method_call);
215 dbus::MessageWriter array_writer(NULL);
216 writer.OpenArray("{ss}", &array_writer);
217 for (base::DictionaryValue::Iterator it(parameters); !it.IsAtEnd();
218 it.Advance()) {
219 std::string value;
220 if (valid_keys_.find(it.key()) == valid_keys_.end() ||
221 !it.value().GetAsString(&value)) {
222 LOG(WARNING) << "Unknown key " << it.key() << " or non string value "
223 << it.value() << ".";
stevenjb 2014/10/31 16:35:41 nit: Maybe have two ifs with separate warnings.
kaliamoorthi 2014/11/03 10:55:38 Done.
224 continue;
225 }
226 dbus::MessageWriter entry_writer(NULL);
227 array_writer.OpenDictEntry(&entry_writer);
228 entry_writer.AppendString(it.key());
229 entry_writer.AppendString(value);
230 array_writer.CloseContainer(&entry_writer);
231 }
232 writer.CloseContainer(&array_writer);
233 GetHelper(object_path)->CallVoidMethod(&method_call, callback);
234 }
235
236 void ShillThirdPartyVpnDriverClientImpl::UpdateConnectionState(
237 const dbus::ObjectPath& object_path,
238 const uint32 connection_state,
239 const VoidDBusMethodCallback& callback) {
240 dbus::MethodCall method_call(shill::kFlimflamThirdPartyVpnInterface,
241 shill::kUpdateConnectionStateFunction);
242 dbus::MessageWriter writer(&method_call);
243 writer.AppendUint32(connection_state);
244 GetHelper(object_path)->CallVoidMethod(&method_call, callback);
245 }
246
247 void ShillThirdPartyVpnDriverClientImpl::SendPacket(
248 const dbus::ObjectPath& object_path,
249 const std::vector<uint8>& ip_packet,
250 const VoidDBusMethodCallback& callback) {
251 dbus::MethodCall method_call(shill::kFlimflamThirdPartyVpnInterface,
252 shill::kSendPacketFunction);
253 dbus::MessageWriter writer(&method_call);
254 writer.AppendArrayOfBytes(ip_packet.data(), ip_packet.size());
255 GetHelper(object_path)->CallVoidMethod(&method_call, callback);
256 }
257
258 // static
259 void ShillThirdPartyVpnDriverClientImpl::OnPacketReceived(
260 base::WeakPtr<HelperInfo> helper_info,
261 dbus::Signal* signal) {
262 if (!helper_info || !helper_info->observer())
263 return;
264
265 dbus::MessageReader reader(signal);
266 const uint8* data;
267 size_t length;
268 if (reader.PopArrayOfBytes(&data, &length))
269 helper_info->observer()->OnPacketReceived(data, length);
270 }
271
272 // static
273 void ShillThirdPartyVpnDriverClientImpl::OnPlatformMessage(
274 base::WeakPtr<HelperInfo> helper_info,
275 dbus::Signal* signal) {
276 if (!helper_info || !helper_info->observer())
277 return;
278
279 dbus::MessageReader reader(signal);
280 uint32 platform_message;
281 if (reader.PopUint32(&platform_message))
282 helper_info->observer()->OnPlatformMessage(platform_message);
283 }
284
285 // static
286 void ShillThirdPartyVpnDriverClientImpl::OnSignalConnected(
287 const std::string& interface,
288 const std::string& signal,
289 bool success) {
290 LOG_IF(ERROR, !success) << "Connect to " << interface << " " << signal
291 << " failed.";
292 }
293
294 } // namespace
295
296 ShillThirdPartyVpnDriverClient::ShillThirdPartyVpnDriverClient() {
297 }
298
299 ShillThirdPartyVpnDriverClient::~ShillThirdPartyVpnDriverClient() {
300 }
301
302 // static
303 ShillThirdPartyVpnDriverClient* ShillThirdPartyVpnDriverClient::Create() {
304 return new ShillThirdPartyVpnDriverClientImpl();
305 }
306
307 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698