blob: 27e219c54566b0d6983e094dccc2bb67f667e955 [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),
Paul Stewart22807992012-04-11 08:48:31 -070030 driver_(driver) {
31 mutable_store()->RegisterString(flimflam::kVPNDomainProperty, &vpn_domain_);
32}
Darin Petkov33af05c2012-02-28 10:10:30 +010033
34VPNService::~VPNService() {}
35
36void VPNService::Connect(Error *error) {
Darin Petkov6aa21872012-03-09 16:10:19 +010037 Service::Connect(error);
Darin Petkov79d74c92012-03-07 17:20:32 +010038 driver_->Connect(this, error);
Darin Petkov33af05c2012-02-28 10:10:30 +010039}
40
Darin Petkov6aa21872012-03-09 16:10:19 +010041void VPNService::Disconnect(Error *error) {
42 Service::Disconnect(error);
43 driver_->Disconnect();
44}
45
Darin Petkov33af05c2012-02-28 10:10:30 +010046string VPNService::GetStorageIdentifier() const {
Darin Petkov02867712012-03-12 14:25:05 +010047 return storage_id_;
48}
49
50// static
51string VPNService::CreateStorageIdentifier(const KeyValueStore &args,
52 Error *error) {
Darin Petkov7f060332012-03-14 11:46:47 +010053 string host = args.LookupString(flimflam::kProviderHostProperty, "");
Darin Petkov02867712012-03-12 14:25:05 +010054 if (host.empty()) {
55 Error::PopulateAndLog(
56 error, Error::kInvalidProperty, "Missing VPN host.");
57 return "";
58 }
Darin Petkov7f060332012-03-14 11:46:47 +010059 string name = args.LookupString(flimflam::kProviderNameProperty, "");
Darin Petkov02867712012-03-12 14:25:05 +010060 if (name.empty()) {
Paul Stewart451aa7f2012-04-11 19:07:58 -070061 name = args.LookupString(flimflam::kNameProperty, "");
62 if (name.empty()) {
63 Error::PopulateAndLog(
64 error, Error::kNotSupported, "Missing VPN name.");
65 return "";
66 }
Darin Petkov02867712012-03-12 14:25:05 +010067 }
68 string id = StringPrintf("vpn_%s_%s", host.c_str(), name.c_str());
69 replace_if(id.begin(), id.end(), &Service::IllegalChar, '_');
70 return id;
Darin Petkov33af05c2012-02-28 10:10:30 +010071}
72
73string VPNService::GetDeviceRpcId(Error *error) {
74 NOTIMPLEMENTED();
75 error->Populate(Error::kNotSupported);
76 return "/";
77}
78
Darin Petkovf3c71d72012-03-21 12:32:15 +010079bool VPNService::Load(StoreInterface *storage) {
80 return Service::Load(storage) &&
81 driver_->Load(storage, GetStorageIdentifier());
82}
83
84bool VPNService::Save(StoreInterface *storage) {
85 return Service::Save(storage) &&
86 driver_->Save(storage, GetStorageIdentifier());
87}
88
Paul Stewart65512e12012-03-26 18:01:08 -070089bool VPNService::Unload() {
90 Service::Unload();
91
92 // VPN services which have been removed from the profile should be
93 // disconnected.
94 driver_->Disconnect();
95
96 // Ask the VPN provider to remove us from its list.
97 manager()->vpn_provider()->RemoveService(this);
98
99 return true;
100}
101
Paul Stewartebd38562012-03-23 13:06:40 -0700102void VPNService::InitDriverPropertyStore() {
103 driver_->InitPropertyStore(mutable_store());
104}
105
Darin Petkov33af05c2012-02-28 10:10:30 +0100106} // namespace shill