blob: dfccf8c9a847d18af4271f3f32405780a0810ece [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
Darin Petkova42afe32013-02-05 16:53:52 +01005#ifndef SHILL_VPN_DRIVER_H_
6#define SHILL_VPN_DRIVER_H_
Darin Petkov33af05c2012-02-28 10:10:30 +01007
Paul Stewartca6abd42012-03-01 15:45:29 -08008#include <string>
9
Darin Petkov33af05c2012-02-28 10:10:30 +010010#include <base/basictypes.h>
Darin Petkov602303f2012-06-06 12:15:59 +020011#include <base/cancelable_callback.h>
12#include <base/memory/weak_ptr.h>
Darin Petkov0e9735d2012-04-24 12:33:45 +020013#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkov33af05c2012-02-28 10:10:30 +010014
Darin Petkovb451d6e2012-04-23 11:56:41 +020015#include "shill/accessor_interface.h"
16#include "shill/key_value_store.h"
Darin Petkov79d74c92012-03-07 17:20:32 +010017#include "shill/refptr_types.h"
18
Darin Petkov33af05c2012-02-28 10:10:30 +010019namespace shill {
20
21class Error;
Darin Petkov602303f2012-06-06 12:15:59 +020022class EventDispatcher;
Darin Petkov0e9735d2012-04-24 12:33:45 +020023class Manager;
Paul Stewartebd38562012-03-23 13:06:40 -070024class PropertyStore;
Darin Petkovf3c71d72012-03-21 12:32:15 +010025class StoreInterface;
Darin Petkov33af05c2012-02-28 10:10:30 +010026
27class VPNDriver {
28 public:
Darin Petkovb451d6e2012-04-23 11:56:41 +020029 virtual ~VPNDriver();
Darin Petkov33af05c2012-02-28 10:10:30 +010030
Paul Stewartca6abd42012-03-01 15:45:29 -080031 virtual bool ClaimInterface(const std::string &link_name,
32 int interface_index) = 0;
Darin Petkov79d74c92012-03-07 17:20:32 +010033 virtual void Connect(const VPNServiceRefPtr &service, Error *error) = 0;
Darin Petkov6aa21872012-03-09 16:10:19 +010034 virtual void Disconnect() = 0;
Paul Stewart39964fa2012-04-04 09:50:25 -070035 virtual std::string GetProviderType() const = 0;
Darin Petkovb451d6e2012-04-23 11:56:41 +020036
Darin Petkova42afe32013-02-05 16:53:52 +010037 // Invoked by VPNService when the underlying connection disconnects.
38 virtual void OnConnectionDisconnected() = 0;
39
Darin Petkovb451d6e2012-04-23 11:56:41 +020040 virtual void InitPropertyStore(PropertyStore *store);
41
42 virtual bool Load(StoreInterface *storage, const std::string &storage_id);
Darin Petkovcb715292012-04-25 13:04:37 +020043 virtual bool Save(StoreInterface *storage,
44 const std::string &storage_id,
45 bool save_credentials);
46 virtual void UnloadCredentials();
Darin Petkovb451d6e2012-04-23 11:56:41 +020047
Darin Petkov9c6e9812013-03-26 13:49:07 +010048 std::string GetHost() const;
49
Darin Petkovb451d6e2012-04-23 11:56:41 +020050 KeyValueStore *args() { return &args_; }
51
52 protected:
53 struct Property {
54 enum Flags {
Darin Petkovcb715292012-04-25 13:04:37 +020055 kEphemeral = 1 << 0, // Never load or save.
56 kCredential = 1 << 1, // Save if saving credentials (crypted).
57 kWriteOnly = 1 << 2, // Never read over RPC.
Darin Petkovb451d6e2012-04-23 11:56:41 +020058 };
59
60 const char *property;
61 int flags;
62 };
63
Darin Petkov0cd0d1e2013-02-11 12:49:10 +010064 static const int kDefaultConnectTimeoutSeconds;
65
Darin Petkov602303f2012-06-06 12:15:59 +020066 VPNDriver(EventDispatcher *dispatcher,
67 Manager *manager,
Darin Petkov0e9735d2012-04-24 12:33:45 +020068 const Property *properties,
69 size_t property_count);
70
Darin Petkov602303f2012-06-06 12:15:59 +020071 EventDispatcher *dispatcher() const { return dispatcher_; }
Darin Petkov0e9735d2012-04-24 12:33:45 +020072 Manager *manager() const { return manager_; }
73
Darin Petkovb536a742012-04-26 11:31:28 +020074 virtual KeyValueStore GetProvider(Error *error);
75
Darin Petkov0cd0d1e2013-02-11 12:49:10 +010076 // Initializes a callback that will invoke OnConnectTimeout after
77 // |timeout_seconds|. The timeout will not be restarted if it's already
78 // scheduled.
79 void StartConnectTimeout(int timeout_seconds);
Darin Petkov602303f2012-06-06 12:15:59 +020080 // Cancels the connect timeout callback, if any, previously scheduled through
81 // StartConnectTimeout.
82 void StopConnectTimeout();
83 // Returns true if a connect timeout is scheduled, false otherwise.
84 bool IsConnectTimeoutStarted() const;
85
Darin Petkova42afe32013-02-05 16:53:52 +010086 // Called if a connect timeout scheduled through StartConnectTimeout
87 // fires. Cancels the timeout callback.
88 virtual void OnConnectTimeout();
89
Darin Petkov0cd0d1e2013-02-11 12:49:10 +010090 int connect_timeout_seconds() const { return connect_timeout_seconds_; }
Darin Petkov602303f2012-06-06 12:15:59 +020091
Darin Petkov0cd0d1e2013-02-11 12:49:10 +010092 private:
93 friend class VPNDriverTest;
Darin Petkov602303f2012-06-06 12:15:59 +020094
Darin Petkovb451d6e2012-04-23 11:56:41 +020095 void ClearMappedProperty(const size_t &index, Error *error);
96 std::string GetMappedProperty(const size_t &index, Error *error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -070097 bool SetMappedProperty(
Darin Petkovb451d6e2012-04-23 11:56:41 +020098 const size_t &index, const std::string &value, Error *error);
99
Darin Petkov602303f2012-06-06 12:15:59 +0200100 base::WeakPtrFactory<VPNDriver> weak_ptr_factory_;
101 EventDispatcher *dispatcher_;
Darin Petkov0e9735d2012-04-24 12:33:45 +0200102 Manager *manager_;
Darin Petkovb451d6e2012-04-23 11:56:41 +0200103 const Property * const properties_;
104 const size_t property_count_;
105 KeyValueStore args_;
106
Darin Petkov602303f2012-06-06 12:15:59 +0200107 base::CancelableClosure connect_timeout_callback_;
108 int connect_timeout_seconds_;
109
Darin Petkovb451d6e2012-04-23 11:56:41 +0200110 DISALLOW_COPY_AND_ASSIGN(VPNDriver);
Darin Petkov33af05c2012-02-28 10:10:30 +0100111};
112
113} // namespace shill
114
Darin Petkova42afe32013-02-05 16:53:52 +0100115#endif // SHILL_VPN_DRIVER_H_