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