Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1 | // 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 Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 7 | #include <algorithm> |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 8 | |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 9 | #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 Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 14 | #include "shill/manager.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 15 | #include "shill/technology.h" |
| 16 | #include "shill/vpn_driver.h" |
| 17 | |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 18 | using base::StringPrintf; |
| 19 | using std::replace_if; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 20 | using std::string; |
| 21 | |
| 22 | namespace shill { |
| 23 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 24 | VPNService::VPNService(ControlInterface *control, |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 25 | EventDispatcher *dispatcher, |
| 26 | Metrics *metrics, |
| 27 | Manager *manager, |
| 28 | VPNDriver *driver) |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 29 | : Service(control, dispatcher, metrics, manager, Technology::kVPN), |
Paul Stewart | 2280799 | 2012-04-11 08:48:31 -0700 | [diff] [blame] | 30 | driver_(driver) { |
| 31 | mutable_store()->RegisterString(flimflam::kVPNDomainProperty, &vpn_domain_); |
| 32 | } |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 33 | |
| 34 | VPNService::~VPNService() {} |
| 35 | |
| 36 | void VPNService::Connect(Error *error) { |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 37 | Service::Connect(error); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 38 | driver_->Connect(this, error); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 39 | } |
| 40 | |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 41 | void VPNService::Disconnect(Error *error) { |
| 42 | Service::Disconnect(error); |
| 43 | driver_->Disconnect(); |
| 44 | } |
| 45 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 46 | string VPNService::GetStorageIdentifier() const { |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 47 | return storage_id_; |
| 48 | } |
| 49 | |
| 50 | // static |
| 51 | string VPNService::CreateStorageIdentifier(const KeyValueStore &args, |
| 52 | Error *error) { |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 53 | string host = args.LookupString(flimflam::kProviderHostProperty, ""); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 54 | if (host.empty()) { |
| 55 | Error::PopulateAndLog( |
| 56 | error, Error::kInvalidProperty, "Missing VPN host."); |
| 57 | return ""; |
| 58 | } |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 59 | string name = args.LookupString(flimflam::kProviderNameProperty, ""); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 60 | if (name.empty()) { |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame^] | 61 | name = args.LookupString(flimflam::kNameProperty, ""); |
| 62 | if (name.empty()) { |
| 63 | Error::PopulateAndLog( |
| 64 | error, Error::kNotSupported, "Missing VPN name."); |
| 65 | return ""; |
| 66 | } |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 67 | } |
| 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 Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | string VPNService::GetDeviceRpcId(Error *error) { |
| 74 | NOTIMPLEMENTED(); |
| 75 | error->Populate(Error::kNotSupported); |
| 76 | return "/"; |
| 77 | } |
| 78 | |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 79 | bool VPNService::Load(StoreInterface *storage) { |
| 80 | return Service::Load(storage) && |
| 81 | driver_->Load(storage, GetStorageIdentifier()); |
| 82 | } |
| 83 | |
| 84 | bool VPNService::Save(StoreInterface *storage) { |
| 85 | return Service::Save(storage) && |
| 86 | driver_->Save(storage, GetStorageIdentifier()); |
| 87 | } |
| 88 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 89 | bool 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 Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 102 | void VPNService::InitDriverPropertyStore() { |
| 103 | driver_->InitPropertyStore(mutable_store()); |
| 104 | } |
| 105 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 106 | } // namespace shill |