blob: f539d0ad49edd530fc531b015d27134560b976f9 [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
5#ifndef SHILL_VPN_DRIVER_
6#define SHILL_VPN_DRIVER_
7
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;
Darin Petkov5eb05422012-05-11 15:45:25 +020035 virtual void OnConnectionDisconnected() = 0;
Paul Stewart39964fa2012-04-04 09:50:25 -070036 virtual std::string GetProviderType() const = 0;
Darin Petkovb451d6e2012-04-23 11:56:41 +020037
38 virtual void InitPropertyStore(PropertyStore *store);
39
40 virtual bool Load(StoreInterface *storage, const std::string &storage_id);
Darin Petkovcb715292012-04-25 13:04:37 +020041 virtual bool Save(StoreInterface *storage,
42 const std::string &storage_id,
43 bool save_credentials);
44 virtual void UnloadCredentials();
Darin Petkovb451d6e2012-04-23 11:56:41 +020045
46 KeyValueStore *args() { return &args_; }
47
48 protected:
49 struct Property {
50 enum Flags {
Darin Petkovcb715292012-04-25 13:04:37 +020051 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 Petkovb451d6e2012-04-23 11:56:41 +020054 };
55
56 const char *property;
57 int flags;
58 };
59
Darin Petkov602303f2012-06-06 12:15:59 +020060 VPNDriver(EventDispatcher *dispatcher,
61 Manager *manager,
Darin Petkov0e9735d2012-04-24 12:33:45 +020062 const Property *properties,
63 size_t property_count);
64
Darin Petkov602303f2012-06-06 12:15:59 +020065 EventDispatcher *dispatcher() const { return dispatcher_; }
Darin Petkov0e9735d2012-04-24 12:33:45 +020066 Manager *manager() const { return manager_; }
67
Darin Petkovb536a742012-04-26 11:31:28 +020068 virtual KeyValueStore GetProvider(Error *error);
69
Darin Petkov602303f2012-06-06 12:15:59 +020070 // 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 Petkovb451d6e2012-04-23 11:56:41 +020079 private:
Darin Petkov602303f2012-06-06 12:15:59 +020080 FRIEND_TEST(VPNDriverTest, ConnectTimeout);
81
82 static const int kDefaultConnectTimeoutSeconds;
83
Darin Petkovb451d6e2012-04-23 11:56:41 +020084 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 Petkov602303f2012-06-06 12:15:59 +020089 // 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 Petkov0e9735d2012-04-24 12:33:45 +020095 Manager *manager_;
Darin Petkovb451d6e2012-04-23 11:56:41 +020096 const Property * const properties_;
97 const size_t property_count_;
98 KeyValueStore args_;
99
Darin Petkov602303f2012-06-06 12:15:59 +0200100 base::CancelableClosure connect_timeout_callback_;
101 int connect_timeout_seconds_;
102
Darin Petkovb451d6e2012-04-23 11:56:41 +0200103 DISALLOW_COPY_AND_ASSIGN(VPNDriver);
Darin Petkov33af05c2012-02-28 10:10:30 +0100104};
105
106} // namespace shill
107
108#endif // SHILL_VPN_DRIVER_