blob: 210f1ac60b75dddfadec039ac383c0b52d0b1a98 [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>
Alex Vakulenko8a532292014-06-16 17:18:44 -07009#include <vector>
Paul Stewartca6abd42012-03-01 15:45:29 -080010
Darin Petkov602303f2012-06-06 12:15:59 +020011#include <base/cancelable_callback.h>
Ben Chancc67c522014-09-03 07:19:18 -070012#include <base/macros.h>
Darin Petkov602303f2012-06-06 12:15:59 +020013#include <base/memory/weak_ptr.h>
Darin Petkov0e9735d2012-04-24 12:33:45 +020014#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkov33af05c2012-02-28 10:10:30 +010015
Darin Petkovb451d6e2012-04-23 11:56:41 +020016#include "shill/accessor_interface.h"
17#include "shill/key_value_store.h"
Darin Petkov79d74c92012-03-07 17:20:32 +010018#include "shill/refptr_types.h"
19
Darin Petkov33af05c2012-02-28 10:10:30 +010020namespace shill {
21
22class Error;
Darin Petkov602303f2012-06-06 12:15:59 +020023class EventDispatcher;
Darin Petkov0e9735d2012-04-24 12:33:45 +020024class Manager;
Paul Stewartebd38562012-03-23 13:06:40 -070025class PropertyStore;
Darin Petkovf3c71d72012-03-21 12:32:15 +010026class StoreInterface;
Darin Petkov33af05c2012-02-28 10:10:30 +010027
28class VPNDriver {
29 public:
Darin Petkovb451d6e2012-04-23 11:56:41 +020030 virtual ~VPNDriver();
Darin Petkov33af05c2012-02-28 10:10:30 +010031
Paul Stewartca6abd42012-03-01 15:45:29 -080032 virtual bool ClaimInterface(const std::string &link_name,
33 int interface_index) = 0;
Darin Petkov79d74c92012-03-07 17:20:32 +010034 virtual void Connect(const VPNServiceRefPtr &service, Error *error) = 0;
Darin Petkov6aa21872012-03-09 16:10:19 +010035 virtual void Disconnect() = 0;
Paul Stewart39964fa2012-04-04 09:50:25 -070036 virtual std::string GetProviderType() const = 0;
Darin Petkovb451d6e2012-04-23 11:56:41 +020037
Darin Petkova42afe32013-02-05 16:53:52 +010038 // Invoked by VPNService when the underlying connection disconnects.
39 virtual void OnConnectionDisconnected() = 0;
40
Darin Petkovb451d6e2012-04-23 11:56:41 +020041 virtual void InitPropertyStore(PropertyStore *store);
42
43 virtual bool Load(StoreInterface *storage, const std::string &storage_id);
Darin Petkovcb715292012-04-25 13:04:37 +020044 virtual bool Save(StoreInterface *storage,
45 const std::string &storage_id,
46 bool save_credentials);
47 virtual void UnloadCredentials();
Darin Petkovb451d6e2012-04-23 11:56:41 +020048
Darin Petkov9c6e9812013-03-26 13:49:07 +010049 std::string GetHost() const;
50
Darin Petkovb451d6e2012-04-23 11:56:41 +020051 KeyValueStore *args() { return &args_; }
Paul Stewartba8f1412013-09-30 17:52:03 -070052 const KeyValueStore *const_args() const { return &args_; }
Darin Petkovb451d6e2012-04-23 11:56:41 +020053
54 protected:
55 struct Property {
56 enum Flags {
Darin Petkovcb715292012-04-25 13:04:37 +020057 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 Stewartdf4e3c92013-06-13 17:50:30 -070060 kArray = 1 << 3, // Property is an array of strings.
Darin Petkovb451d6e2012-04-23 11:56:41 +020061 };
62
63 const char *property;
64 int flags;
65 };
66
Darin Petkov0cd0d1e2013-02-11 12:49:10 +010067 static const int kDefaultConnectTimeoutSeconds;
68
Darin Petkov602303f2012-06-06 12:15:59 +020069 VPNDriver(EventDispatcher *dispatcher,
70 Manager *manager,
Darin Petkov0e9735d2012-04-24 12:33:45 +020071 const Property *properties,
72 size_t property_count);
73
Darin Petkov602303f2012-06-06 12:15:59 +020074 EventDispatcher *dispatcher() const { return dispatcher_; }
Darin Petkov0e9735d2012-04-24 12:33:45 +020075 Manager *manager() const { return manager_; }
76
Darin Petkovb536a742012-04-26 11:31:28 +020077 virtual KeyValueStore GetProvider(Error *error);
78
Darin Petkov0cd0d1e2013-02-11 12:49:10 +010079 // 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 Petkov602303f2012-06-06 12:15:59 +020083 // 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 Petkova42afe32013-02-05 16:53:52 +010089 // Called if a connect timeout scheduled through StartConnectTimeout
90 // fires. Cancels the timeout callback.
91 virtual void OnConnectTimeout();
92
Darin Petkov0cd0d1e2013-02-11 12:49:10 +010093 int connect_timeout_seconds() const { return connect_timeout_seconds_; }
Darin Petkov602303f2012-06-06 12:15:59 +020094
Darin Petkov0cd0d1e2013-02-11 12:49:10 +010095 private:
96 friend class VPNDriverTest;
Darin Petkov602303f2012-06-06 12:15:59 +020097
Paul Stewartdf4e3c92013-06-13 17:50:30 -070098 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 Petkovb451d6e2012-04-23 11:56:41 +0200104 const size_t &index, const std::string &value, Error *error);
Paul Stewartdf4e3c92013-06-13 17:50:30 -0700105 bool SetMappedStringsProperty(
106 const size_t &index, const std::vector<std::string> &value, Error *error);
Darin Petkovb451d6e2012-04-23 11:56:41 +0200107
Darin Petkov602303f2012-06-06 12:15:59 +0200108 base::WeakPtrFactory<VPNDriver> weak_ptr_factory_;
109 EventDispatcher *dispatcher_;
Darin Petkov0e9735d2012-04-24 12:33:45 +0200110 Manager *manager_;
Darin Petkovb451d6e2012-04-23 11:56:41 +0200111 const Property * const properties_;
112 const size_t property_count_;
113 KeyValueStore args_;
114
Darin Petkov602303f2012-06-06 12:15:59 +0200115 base::CancelableClosure connect_timeout_callback_;
116 int connect_timeout_seconds_;
117
Darin Petkovb451d6e2012-04-23 11:56:41 +0200118 DISALLOW_COPY_AND_ASSIGN(VPNDriver);
Darin Petkov33af05c2012-02-28 10:10:30 +0100119};
120
121} // namespace shill
122
Darin Petkova42afe32013-02-05 16:53:52 +0100123#endif // SHILL_VPN_DRIVER_H_