vpn_driver.h revision 8a5322984f2d81bcbfd8d44c59747a11bd9b904b
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_H_
6#define SHILL_VPN_DRIVER_H_
7
8#include <string>
9#include <vector>
10
11#include <base/basictypes.h>
12#include <base/cancelable_callback.h>
13#include <base/memory/weak_ptr.h>
14#include <gtest/gtest_prod.h>  // for FRIEND_TEST
15
16#include "shill/accessor_interface.h"
17#include "shill/key_value_store.h"
18#include "shill/refptr_types.h"
19
20namespace shill {
21
22class Error;
23class EventDispatcher;
24class Manager;
25class PropertyStore;
26class StoreInterface;
27
28class VPNDriver {
29 public:
30  virtual ~VPNDriver();
31
32  virtual bool ClaimInterface(const std::string &link_name,
33                              int interface_index) = 0;
34  virtual void Connect(const VPNServiceRefPtr &service, Error *error) = 0;
35  virtual void Disconnect() = 0;
36  virtual std::string GetProviderType() const = 0;
37
38  // Invoked by VPNService when the underlying connection disconnects.
39  virtual void OnConnectionDisconnected() = 0;
40
41  virtual void InitPropertyStore(PropertyStore *store);
42
43  virtual bool Load(StoreInterface *storage, const std::string &storage_id);
44  virtual bool Save(StoreInterface *storage,
45                    const std::string &storage_id,
46                    bool save_credentials);
47  virtual void UnloadCredentials();
48
49  std::string GetHost() const;
50
51  KeyValueStore *args() { return &args_; }
52  const KeyValueStore *const_args() const { return &args_; }
53
54 protected:
55  struct Property {
56    enum Flags {
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.
60      kArray = 1 << 3,       // Property is an array of strings.
61    };
62
63    const char *property;
64    int flags;
65  };
66
67  static const int kDefaultConnectTimeoutSeconds;
68
69  VPNDriver(EventDispatcher *dispatcher,
70            Manager *manager,
71            const Property *properties,
72            size_t property_count);
73
74  EventDispatcher *dispatcher() const { return dispatcher_; }
75  Manager *manager() const { return manager_; }
76
77  virtual KeyValueStore GetProvider(Error *error);
78
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);
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
89  // Called if a connect timeout scheduled through StartConnectTimeout
90  // fires. Cancels the timeout callback.
91  virtual void OnConnectTimeout();
92
93  int connect_timeout_seconds() const { return connect_timeout_seconds_; }
94
95 private:
96  friend class VPNDriverTest;
97
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(
104      const size_t &index, const std::string &value, Error *error);
105  bool SetMappedStringsProperty(
106      const size_t &index, const std::vector<std::string> &value, Error *error);
107
108  base::WeakPtrFactory<VPNDriver> weak_ptr_factory_;
109  EventDispatcher *dispatcher_;
110  Manager *manager_;
111  const Property * const properties_;
112  const size_t property_count_;
113  KeyValueStore args_;
114
115  base::CancelableClosure connect_timeout_callback_;
116  int connect_timeout_seconds_;
117
118  DISALLOW_COPY_AND_ASSIGN(VPNDriver);
119};
120
121}  // namespace shill
122
123#endif  // SHILL_VPN_DRIVER_H_
124