1//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#ifndef SHILL_VPN_VPN_DRIVER_H_
18#define SHILL_VPN_VPN_DRIVER_H_
19
20#include <string>
21#include <vector>
22
23#include <base/cancelable_callback.h>
24#include <base/macros.h>
25#include <base/memory/weak_ptr.h>
26#include <gtest/gtest_prod.h>  // for FRIEND_TEST
27
28#include "shill/accessor_interface.h"
29#include "shill/key_value_store.h"
30#include "shill/refptr_types.h"
31
32namespace shill {
33
34class Error;
35class EventDispatcher;
36class Manager;
37class PropertyStore;
38class StoreInterface;
39
40class VPNDriver {
41 public:
42  virtual ~VPNDriver();
43
44  virtual bool ClaimInterface(const std::string& link_name,
45                              int interface_index) = 0;
46  virtual void Connect(const VPNServiceRefPtr& service, Error* error) = 0;
47  virtual void Disconnect() = 0;
48  virtual std::string GetProviderType() const = 0;
49
50  // Invoked by VPNService when the underlying connection disconnects.
51  virtual void OnConnectionDisconnected() = 0;
52
53  virtual void InitPropertyStore(PropertyStore* store);
54
55  virtual bool Load(StoreInterface* storage, const std::string& storage_id);
56  virtual bool Save(StoreInterface* storage,
57                    const std::string& storage_id,
58                    bool save_credentials);
59  virtual void UnloadCredentials();
60
61  std::string GetHost() const;
62
63  KeyValueStore* args() { return &args_; }
64  const KeyValueStore* const_args() const { return &args_; }
65
66 protected:
67  struct Property {
68    enum Flags {
69      kEphemeral = 1 << 0,   // Never load or save.
70      kCredential = 1 << 1,  // Save if saving credentials (crypted).
71      kWriteOnly = 1 << 2,   // Never read over RPC.
72      kArray = 1 << 3,       // Property is an array of strings.
73    };
74
75    const char* property;
76    int flags;
77  };
78
79  static const int kDefaultConnectTimeoutSeconds;
80
81  VPNDriver(EventDispatcher* dispatcher,
82            Manager* manager,
83            const Property* properties,
84            size_t property_count);
85
86  EventDispatcher* dispatcher() const { return dispatcher_; }
87  Manager* manager() const { return manager_; }
88
89  virtual KeyValueStore GetProvider(Error* error);
90
91  // Initializes a callback that will invoke OnConnectTimeout after
92  // |timeout_seconds|. The timeout will not be restarted if it's already
93  // scheduled.
94  void StartConnectTimeout(int timeout_seconds);
95  // Cancels the connect timeout callback, if any, previously scheduled through
96  // StartConnectTimeout.
97  void StopConnectTimeout();
98  // Returns true if a connect timeout is scheduled, false otherwise.
99  bool IsConnectTimeoutStarted() const;
100
101  // Called if a connect timeout scheduled through StartConnectTimeout
102  // fires. Cancels the timeout callback.
103  virtual void OnConnectTimeout();
104
105  int connect_timeout_seconds() const { return connect_timeout_seconds_; }
106
107 private:
108  friend class VPNDriverTest;
109
110  void ClearMappedStringProperty(const size_t& index, Error* error);
111  void ClearMappedStringsProperty(const size_t& index, Error* error);
112  std::string GetMappedStringProperty(const size_t& index, Error* error);
113  std::vector<std::string> GetMappedStringsProperty(
114      const size_t& index, Error* error);
115  bool SetMappedStringProperty(
116      const size_t& index, const std::string& value, Error* error);
117  bool SetMappedStringsProperty(
118      const size_t& index, const std::vector<std::string>& value, Error* error);
119
120  base::WeakPtrFactory<VPNDriver> weak_ptr_factory_;
121  EventDispatcher* dispatcher_;
122  Manager* manager_;
123  const Property* const properties_;
124  const size_t property_count_;
125  KeyValueStore args_;
126
127  base::CancelableClosure connect_timeout_callback_;
128  int connect_timeout_seconds_;
129
130  DISALLOW_COPY_AND_ASSIGN(VPNDriver);
131};
132
133}  // namespace shill
134
135#endif  // SHILL_VPN_VPN_DRIVER_H_
136