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

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: Removes dependence on basic_types.h and fixes a build error 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 "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 chromeos {
15
16 namespace {
17
18 const char* kSetParametersKeyList[] = {
19 shill::kAddressParameterThirdPartyVpn,
20 shill::kBroadcastAddressParameterThirdPartyVpn,
21 shill::kGatewayParameterThirdPartyVpn,
22 shill::kBypassTunnelForIpParameterThirdPartyVpn,
23 shill::kSubnetPrefixParameterThirdPartyVpn,
24 shill::kMtuParameterThirdPartyVpn,
25 shill::kDomainSearchParameterThirdPartyVpn,
26 shill::kDnsServersParameterThirdPartyVpn};
27
28 // The ShillThirdPartyVpnDriverClient implementation.
29 class ShillThirdPartyVpnDriverClientImpl
30 : public ShillThirdPartyVpnDriverClient {
31 public:
32 ShillThirdPartyVpnDriverClientImpl();
33 ~ShillThirdPartyVpnDriverClientImpl() override;
34
35 // ShillThirdPartyVpnDriverClient overrides
36 void AddShillThirdPartyVpnObserver(
37 const dbus::ObjectPath& object_path,
38 ShillThirdPartyVpnObserver* observer) override;
39
40 void RemoveShillThirdPartyVpnObserver(
41 const dbus::ObjectPath& object_path) override;
42
43 void SetParameters(
44 const dbus::ObjectPath& object_path,
45 const base::DictionaryValue& parameters,
46 const base::Closure& callback,
47 const ShillClientHelper::ErrorCallback& error_callback) override;
48
49 void UpdateConnectionState(
50 const dbus::ObjectPath& object_path,
51 const uint32_t connection_state,
52 const base::Closure& callback,
53 const ShillClientHelper::ErrorCallback& error_callback) override;
54
55 void SendPacket(
56 const dbus::ObjectPath& object_path,
57 const std::string& ip_packet,
58 const base::Closure& callback,
59 const ShillClientHelper::ErrorCallback& error_callback) override;
60
61 protected:
62 void Init(dbus::Bus* bus) override { bus_ = bus; }
63
64 ShillThirdPartyVpnDriverClient::TestInterface* GetTestInterface() override {
65 return NULL;
stevenjb 2014/11/12 18:05:13 s/NULL/nullptr throughout
kaliamoorthi 2014/11/13 12:16:30 Missed it in previous CL. Done.
66 }
67
68 private:
69 class HelperInfo {
pneubeck (no reviews) 2014/11/12 14:21:07 HelperInfo does not seem to require private access
kaliamoorthi 2014/11/13 12:16:30 HelperInfo does not make sense outside the class,
70 public:
71 explicit HelperInfo(dbus::ObjectProxy* object_proxy);
stevenjb 2014/11/12 18:05:13 Blank line between constructor and other methods
kaliamoorthi 2014/11/13 12:16:30 Done.
72 ShillClientHelper* helper() { return &helper_; }
73 ShillThirdPartyVpnObserver* observer() { return observer_; }
74
75 void set_observer(ShillThirdPartyVpnObserver* observer) {
76 observer_ = observer;
77 }
78
79 base::WeakPtr<HelperInfo> GetWeakPtr() {
80 return weak_ptr_factory_.GetWeakPtr();
81 }
82
83 private:
84 ShillClientHelper helper_;
85 ShillThirdPartyVpnObserver* observer_;
86
87 base::WeakPtrFactory<HelperInfo> weak_ptr_factory_;
88 };
89 using HelperMap = std::map<std::string, HelperInfo*>;
90
91 void DeleteHelper(const dbus::ObjectPath& object_path);
pneubeck (no reviews) 2014/11/12 14:21:07 it would improve readability a bit if you'd move t
kaliamoorthi 2014/11/13 12:16:30 Done.
92
93 static void OnPacketReceived(base::WeakPtr<HelperInfo> helper_info,
94 dbus::Signal* signal);
95 static void OnPlatformMessage(base::WeakPtr<HelperInfo> helper_info,
96 dbus::Signal* signal);
97 static void OnSignalConnected(const std::string& interface,
98 const std::string& signal,
99 bool success);
100
101 // Returns or creates the corresponding ShillClientHelper for the object_path.
pneubeck (no reviews) 2014/11/12 14:21:07 references to variables/arguments are usually put
kaliamoorthi 2014/11/13 12:16:30 Done.
102 ShillClientHelper* GetHelper(const dbus::ObjectPath& object_path);
103
104 // Returns the corresponding HelperInfo for the object_path if exists,
105 // nullptr if not.
106 HelperInfo* FindHelperInfo(const dbus::ObjectPath& object_path);
107
108 // Returns or creates the corresponding HelperInfo for the object_path.
109 HelperInfo* GetHelperInfo(const dbus::ObjectPath& object_path);
110
111 dbus::Bus* bus_;
112 HelperMap helpers_;
113 std::set<std::string> valid_keys_;
114
115 DISALLOW_COPY_AND_ASSIGN(ShillThirdPartyVpnDriverClientImpl);
116 };
117
118 ShillThirdPartyVpnDriverClientImpl::HelperInfo::HelperInfo(
119 dbus::ObjectProxy* object_proxy)
120 : helper_(object_proxy), observer_(NULL), weak_ptr_factory_(this) {
121 }
122
123 ShillThirdPartyVpnDriverClientImpl::ShillThirdPartyVpnDriverClientImpl()
124 : bus_(NULL) {
125 for (uint32_t i = 0; i < arraysize(kSetParametersKeyList); ++i) {
126 valid_keys_.insert(kSetParametersKeyList[i]);
127 }
128 }
129
130 ShillThirdPartyVpnDriverClientImpl::~ShillThirdPartyVpnDriverClientImpl() {
131 for (auto& iter : helpers_) {
132 HelperInfo* helper_info = iter.second;
133 bus_->RemoveObjectProxy(
134 shill::kFlimflamServiceName,
135 helper_info->helper()->object_proxy()->object_path(),
136 base::Bind(&base::DoNothing));
137 delete helper_info;
138 }
139 }
140
141 void ShillThirdPartyVpnDriverClientImpl::AddShillThirdPartyVpnObserver(
142 const dbus::ObjectPath& object_path,
143 ShillThirdPartyVpnObserver* observer) {
144 HelperInfo* helper_info = GetHelperInfo(object_path);
145 if (helper_info->observer()) {
146 LOG(ERROR) << "Observer exists for " << object_path.value();
147 return;
148 }
149
150 // TODO(kaliamoorthi): Remove the const_cast.
151 helper_info->set_observer(observer);
152 dbus::ObjectProxy* proxy =
153 const_cast<dbus::ObjectProxy*>(helper_info->helper()->object_proxy());
154
155 proxy->ConnectToSignal(
156 shill::kFlimflamThirdPartyVpnInterface, shill::kOnPlatformMessageFunction,
157 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnPlatformMessage,
158 helper_info->GetWeakPtr()),
159 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnSignalConnected));
160
161 proxy->ConnectToSignal(
162 shill::kFlimflamThirdPartyVpnInterface, shill::kOnPacketReceivedFunction,
163 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnPacketReceived,
164 helper_info->GetWeakPtr()),
165 base::Bind(&ShillThirdPartyVpnDriverClientImpl::OnSignalConnected));
166 }
167
168 void ShillThirdPartyVpnDriverClientImpl::RemoveShillThirdPartyVpnObserver(
169 const dbus::ObjectPath& object_path) {
170 HelperInfo* helper_info = FindHelperInfo(object_path);
171 if (!helper_info) {
172 LOG(ERROR) << "Unknown object_path " << object_path.value();
173 return;
174 }
175
176 CHECK(helper_info->observer());
177 helper_info->set_observer(NULL);
178 DeleteHelper(object_path);
179 }
180
181 void ShillThirdPartyVpnDriverClientImpl::DeleteHelper(
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 bus_->RemoveObjectProxy(shill::kFlimflamServiceName, object_path,
190 base::Bind(&base::DoNothing));
191 helpers_.erase(helpers_.find(object_path.value()));
192 delete helper_info;
193 }
194
195 void ShillThirdPartyVpnDriverClientImpl::SetParameters(
196 const dbus::ObjectPath& object_path,
197 const base::DictionaryValue& parameters,
198 const base::Closure& callback,
199 const ShillClientHelper::ErrorCallback& error_callback) {
200 dbus::MethodCall method_call(shill::kFlimflamThirdPartyVpnInterface,
201 shill::kSetParametersFunction);
202 dbus::MessageWriter writer(&method_call);
203 dbus::MessageWriter array_writer(NULL);
204 writer.OpenArray("{ss}", &array_writer);
205 for (base::DictionaryValue::Iterator it(parameters); !it.IsAtEnd();
206 it.Advance()) {
207 if (valid_keys_.find(it.key()) == valid_keys_.end()) {
208 LOG(WARNING) << "Unknown key " << it.key();
209 continue;
210 }
211 std::string value;
212 if (!it.value().GetAsString(&value)) {
213 LOG(WARNING) << "Non string value " << it.value();
214 continue;
215 }
216 dbus::MessageWriter entry_writer(NULL);
217 array_writer.OpenDictEntry(&entry_writer);
218 entry_writer.AppendString(it.key());
219 entry_writer.AppendString(value);
220 array_writer.CloseContainer(&entry_writer);
221 }
222 writer.CloseContainer(&array_writer);
223 GetHelper(object_path)
224 ->CallVoidMethodWithErrorCallback(&method_call, callback, error_callback);
225 }
226
227 void ShillThirdPartyVpnDriverClientImpl::UpdateConnectionState(
228 const dbus::ObjectPath& object_path,
229 const uint32_t connection_state,
230 const base::Closure& callback,
231 const ShillClientHelper::ErrorCallback& error_callback) {
232 dbus::MethodCall method_call(shill::kFlimflamThirdPartyVpnInterface,
233 shill::kUpdateConnectionStateFunction);
234 dbus::MessageWriter writer(&method_call);
235 writer.AppendUint32(connection_state);
236 GetHelper(object_path)
237 ->CallVoidMethodWithErrorCallback(&method_call, callback, error_callback);
238 }
239
240 void ShillThirdPartyVpnDriverClientImpl::SendPacket(
241 const dbus::ObjectPath& object_path,
242 const std::string& ip_packet,
243 const base::Closure& callback,
244 const ShillClientHelper::ErrorCallback& error_callback) {
245 dbus::MethodCall method_call(shill::kFlimflamThirdPartyVpnInterface,
246 shill::kSendPacketFunction);
247 dbus::MessageWriter writer(&method_call);
248 writer.AppendArrayOfBytes(reinterpret_cast<const uint8_t*>(ip_packet.data()),
249 ip_packet.size());
250 GetHelper(object_path)
251 ->CallVoidMethodWithErrorCallback(&method_call, callback, error_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_t* data = nullptr;
263 size_t length = 0;
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_t platform_message = 0;
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 ShillClientHelper* ShillThirdPartyVpnDriverClientImpl::GetHelper(
291 const dbus::ObjectPath& object_path) {
292 return GetHelperInfo(object_path)->helper();
293 }
294
295 ShillThirdPartyVpnDriverClientImpl::HelperInfo*
296 ShillThirdPartyVpnDriverClientImpl::FindHelperInfo(
297 const dbus::ObjectPath& object_path) {
298 HelperMap::iterator it = helpers_.find(object_path.value());
299 return (it != helpers_.end()) ? it->second : NULL;
300 }
301
302 ShillThirdPartyVpnDriverClientImpl::HelperInfo*
303 ShillThirdPartyVpnDriverClientImpl::GetHelperInfo(
304 const dbus::ObjectPath& object_path) {
305 HelperInfo* helper_info = FindHelperInfo(object_path);
306 if (helper_info)
307 return helper_info;
308
309 // There is no helper for the profile, create it.
310 dbus::ObjectProxy* object_proxy =
311 bus_->GetObjectProxy(shill::kFlimflamServiceName, object_path);
312 helper_info = new HelperInfo(object_proxy);
313 helpers_[object_path.value()] = helper_info;
314 return helper_info;
315 }
316
317 } // namespace
318
319 ShillThirdPartyVpnDriverClient::ShillThirdPartyVpnDriverClient() {
320 }
321
322 ShillThirdPartyVpnDriverClient::~ShillThirdPartyVpnDriverClient() {
323 }
324
325 // static
326 ShillThirdPartyVpnDriverClient* ShillThirdPartyVpnDriverClient::Create() {
327 return new ShillThirdPartyVpnDriverClientImpl();
328 }
329
330 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698