blob: 21548083238321d7f3da798d297299293b174a1b [file] [log] [blame]
Darin Petkov33af05c2012-02-28 10:10:30 +01001// Copyright (c) 2012 The Chromium OS 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 "shill/vpn_service.h"
6
Darin Petkov02867712012-03-12 14:25:05 +01007#include <algorithm>
Darin Petkov33af05c2012-02-28 10:10:30 +01008
Darin Petkov02867712012-03-12 14:25:05 +01009#include <base/logging.h>
10#include <base/stringprintf.h>
11#include <chromeos/dbus/service_constants.h>
12
13#include "shill/key_value_store.h"
Paul Stewart65512e12012-03-26 18:01:08 -070014#include "shill/manager.h"
Darin Petkov33af05c2012-02-28 10:10:30 +010015#include "shill/technology.h"
16#include "shill/vpn_driver.h"
17
Darin Petkov02867712012-03-12 14:25:05 +010018using base::StringPrintf;
19using std::replace_if;
Darin Petkov33af05c2012-02-28 10:10:30 +010020using std::string;
21
22namespace shill {
23
Darin Petkov79d74c92012-03-07 17:20:32 +010024VPNService::VPNService(ControlInterface *control,
Darin Petkov33af05c2012-02-28 10:10:30 +010025 EventDispatcher *dispatcher,
26 Metrics *metrics,
27 Manager *manager,
28 VPNDriver *driver)
Darin Petkov79d74c92012-03-07 17:20:32 +010029 : Service(control, dispatcher, metrics, manager, Technology::kVPN),
Darin Petkov33af05c2012-02-28 10:10:30 +010030 driver_(driver) {}
31
32VPNService::~VPNService() {}
33
34void VPNService::Connect(Error *error) {
Darin Petkov6aa21872012-03-09 16:10:19 +010035 Service::Connect(error);
Darin Petkov79d74c92012-03-07 17:20:32 +010036 driver_->Connect(this, error);
Darin Petkov33af05c2012-02-28 10:10:30 +010037}
38
Darin Petkov6aa21872012-03-09 16:10:19 +010039void VPNService::Disconnect(Error *error) {
40 Service::Disconnect(error);
41 driver_->Disconnect();
42}
43
Darin Petkov33af05c2012-02-28 10:10:30 +010044string VPNService::GetStorageIdentifier() const {
Darin Petkov02867712012-03-12 14:25:05 +010045 return storage_id_;
46}
47
48// static
49string VPNService::CreateStorageIdentifier(const KeyValueStore &args,
50 Error *error) {
Darin Petkov7f060332012-03-14 11:46:47 +010051 string host = args.LookupString(flimflam::kProviderHostProperty, "");
Darin Petkov02867712012-03-12 14:25:05 +010052 if (host.empty()) {
53 Error::PopulateAndLog(
54 error, Error::kInvalidProperty, "Missing VPN host.");
55 return "";
56 }
Darin Petkov7f060332012-03-14 11:46:47 +010057 string name = args.LookupString(flimflam::kProviderNameProperty, "");
Darin Petkov02867712012-03-12 14:25:05 +010058 if (name.empty()) {
59 Error::PopulateAndLog(
60 error, Error::kNotSupported, "Missing VPN name.");
61 return "";
62 }
63 string id = StringPrintf("vpn_%s_%s", host.c_str(), name.c_str());
64 replace_if(id.begin(), id.end(), &Service::IllegalChar, '_');
65 return id;
Darin Petkov33af05c2012-02-28 10:10:30 +010066}
67
68string VPNService::GetDeviceRpcId(Error *error) {
69 NOTIMPLEMENTED();
70 error->Populate(Error::kNotSupported);
71 return "/";
72}
73
Darin Petkovf3c71d72012-03-21 12:32:15 +010074bool VPNService::Load(StoreInterface *storage) {
75 return Service::Load(storage) &&
76 driver_->Load(storage, GetStorageIdentifier());
77}
78
79bool VPNService::Save(StoreInterface *storage) {
80 return Service::Save(storage) &&
81 driver_->Save(storage, GetStorageIdentifier());
82}
83
Paul Stewart65512e12012-03-26 18:01:08 -070084bool VPNService::Unload() {
85 Service::Unload();
86
87 // VPN services which have been removed from the profile should be
88 // disconnected.
89 driver_->Disconnect();
90
91 // Ask the VPN provider to remove us from its list.
92 manager()->vpn_provider()->RemoveService(this);
93
94 return true;
95}
96
Paul Stewartebd38562012-03-23 13:06:40 -070097void VPNService::InitDriverPropertyStore() {
98 driver_->InitPropertyStore(mutable_store());
99}
100
Darin Petkov33af05c2012-02-28 10:10:30 +0100101} // namespace shill