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 | |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 5 | #ifndef SHILL_VPN_DRIVER_H_ |
| 6 | #define SHILL_VPN_DRIVER_H_ |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 7 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 8 | #include <string> |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 9 | #include <vector> |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 10 | |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 11 | #include <base/cancelable_callback.h> |
Ben Chan | cc67c52 | 2014-09-03 07:19:18 -0700 | [diff] [blame] | 12 | #include <base/macros.h> |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 13 | #include <base/memory/weak_ptr.h> |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 15 | |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 16 | #include "shill/accessor_interface.h" |
| 17 | #include "shill/key_value_store.h" |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 18 | #include "shill/refptr_types.h" |
| 19 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 20 | namespace shill { |
| 21 | |
| 22 | class Error; |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 23 | class EventDispatcher; |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 24 | class Manager; |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 25 | class PropertyStore; |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 26 | class StoreInterface; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 27 | |
| 28 | class VPNDriver { |
| 29 | public: |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 30 | virtual ~VPNDriver(); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 31 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 32 | virtual bool ClaimInterface(const std::string &link_name, |
| 33 | int interface_index) = 0; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 34 | virtual void Connect(const VPNServiceRefPtr &service, Error *error) = 0; |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 35 | virtual void Disconnect() = 0; |
Paul Stewart | 39964fa | 2012-04-04 09:50:25 -0700 | [diff] [blame] | 36 | virtual std::string GetProviderType() const = 0; |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 37 | |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 38 | // Invoked by VPNService when the underlying connection disconnects. |
| 39 | virtual void OnConnectionDisconnected() = 0; |
| 40 | |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 41 | virtual void InitPropertyStore(PropertyStore *store); |
| 42 | |
| 43 | virtual bool Load(StoreInterface *storage, const std::string &storage_id); |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 44 | virtual bool Save(StoreInterface *storage, |
| 45 | const std::string &storage_id, |
| 46 | bool save_credentials); |
| 47 | virtual void UnloadCredentials(); |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 48 | |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 49 | std::string GetHost() const; |
| 50 | |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 51 | KeyValueStore *args() { return &args_; } |
Paul Stewart | ba8f141 | 2013-09-30 17:52:03 -0700 | [diff] [blame] | 52 | const KeyValueStore *const_args() const { return &args_; } |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 53 | |
| 54 | protected: |
| 55 | struct Property { |
| 56 | enum Flags { |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 57 | kEphemeral = 1 << 0, // Never load or save. |
| 58 | kCredential = 1 << 1, // Save if saving credentials (crypted). |
| 59 | kWriteOnly = 1 << 2, // Never read over RPC. |
Paul Stewart | df4e3c9 | 2013-06-13 17:50:30 -0700 | [diff] [blame] | 60 | kArray = 1 << 3, // Property is an array of strings. |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | const char *property; |
| 64 | int flags; |
| 65 | }; |
| 66 | |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 67 | static const int kDefaultConnectTimeoutSeconds; |
| 68 | |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 69 | VPNDriver(EventDispatcher *dispatcher, |
| 70 | Manager *manager, |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 71 | const Property *properties, |
| 72 | size_t property_count); |
| 73 | |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 74 | EventDispatcher *dispatcher() const { return dispatcher_; } |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 75 | Manager *manager() const { return manager_; } |
| 76 | |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 77 | virtual KeyValueStore GetProvider(Error *error); |
| 78 | |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 79 | // Initializes a callback that will invoke OnConnectTimeout after |
| 80 | // |timeout_seconds|. The timeout will not be restarted if it's already |
| 81 | // scheduled. |
| 82 | void StartConnectTimeout(int timeout_seconds); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 83 | // Cancels the connect timeout callback, if any, previously scheduled through |
| 84 | // StartConnectTimeout. |
| 85 | void StopConnectTimeout(); |
| 86 | // Returns true if a connect timeout is scheduled, false otherwise. |
| 87 | bool IsConnectTimeoutStarted() const; |
| 88 | |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 89 | // Called if a connect timeout scheduled through StartConnectTimeout |
| 90 | // fires. Cancels the timeout callback. |
| 91 | virtual void OnConnectTimeout(); |
| 92 | |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 93 | int connect_timeout_seconds() const { return connect_timeout_seconds_; } |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 94 | |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 95 | private: |
| 96 | friend class VPNDriverTest; |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 97 | |
Paul Stewart | df4e3c9 | 2013-06-13 17:50:30 -0700 | [diff] [blame] | 98 | void ClearMappedStringProperty(const size_t &index, Error *error); |
| 99 | void ClearMappedStringsProperty(const size_t &index, Error *error); |
| 100 | std::string GetMappedStringProperty(const size_t &index, Error *error); |
| 101 | std::vector<std::string> GetMappedStringsProperty( |
| 102 | const size_t &index, Error *error); |
| 103 | bool SetMappedStringProperty( |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 104 | const size_t &index, const std::string &value, Error *error); |
Paul Stewart | df4e3c9 | 2013-06-13 17:50:30 -0700 | [diff] [blame] | 105 | bool SetMappedStringsProperty( |
| 106 | const size_t &index, const std::vector<std::string> &value, Error *error); |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 107 | |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 108 | base::WeakPtrFactory<VPNDriver> weak_ptr_factory_; |
| 109 | EventDispatcher *dispatcher_; |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 110 | Manager *manager_; |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 111 | const Property * const properties_; |
| 112 | const size_t property_count_; |
| 113 | KeyValueStore args_; |
| 114 | |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 115 | base::CancelableClosure connect_timeout_callback_; |
| 116 | int connect_timeout_seconds_; |
| 117 | |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 118 | DISALLOW_COPY_AND_ASSIGN(VPNDriver); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | } // namespace shill |
| 122 | |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 123 | #endif // SHILL_VPN_DRIVER_H_ |