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_PROVIDER_H_
18 #define SHILL_VPN_VPN_PROVIDER_H_
19 
20 #include <string>
21 #include <vector>
22 
23 #include <base/macros.h>
24 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
25 
26 #include "shill/provider_interface.h"
27 #include "shill/refptr_types.h"
28 
29 namespace shill {
30 
31 class ControlInterface;
32 class Error;
33 class EventDispatcher;
34 class KeyValueStore;
35 class Manager;
36 class Metrics;
37 class StoreInterface;
38 
39 class VPNProvider : public ProviderInterface {
40  public:
41   VPNProvider(ControlInterface* control_interface,
42               EventDispatcher* dispatcher,
43               Metrics* metrics,
44               Manager* manager);
45   ~VPNProvider() override;
46 
47   // Called by Manager as a part of the Provider interface.  The attributes
48   // used for matching services for the VPN provider are the ProviderType,
49   // ProviderHost mode and Name parameters.
50   void CreateServicesFromProfile(const ProfileRefPtr& profile) override;
51   ServiceRefPtr FindSimilarService(
52       const KeyValueStore& args, Error* error) const override;
53   ServiceRefPtr GetService(const KeyValueStore& args, Error* error) override;
54   ServiceRefPtr CreateTemporaryService(
55       const KeyValueStore& args, Error* error) override;
56   ServiceRefPtr CreateTemporaryServiceFromProfile(
57       const ProfileRefPtr& profile,
58       const std::string& entry_name,
59       Error* error) override;
60   void Start() override;
61   void Stop() override;
62 
63   // Offers an unclaimed interface to VPN services.  Returns true if this
64   // device has been accepted by a service.
65   virtual bool OnDeviceInfoAvailable(const std::string& link_name,
66                                      int interface_index);
67 
68   // Clean up a VPN services that has been unloaded and will be deregistered.
69   // This removes the VPN provider's reference to this service in its
70   // services_ vector.
71   void RemoveService(VPNServiceRefPtr service);
72 
73   // Returns true if any of the managed VPN services is connecting or connected.
74   virtual bool HasActiveService() const;
75 
76   // Disconnect any other active VPN services.
77   virtual void DisconnectAll();
78 
79  private:
80   friend class VPNProviderTest;
81   FRIEND_TEST(VPNProviderTest, CreateService);
82   FRIEND_TEST(VPNProviderTest, OnDeviceInfoAvailable);
83   FRIEND_TEST(VPNProviderTest, RemoveService);
84   FRIEND_TEST(VPNServiceTest, Unload);
85 
86   // Create a service of type |type| and storage identifier |storage_id|
87   // and initial parameters |args|.  Returns a service reference pointer
88   // to the newly created service, or populates |error| with an the error
89   // that caused this to fail.
90   VPNServiceRefPtr CreateServiceInner(const std::string& type,
91                                       const std::string& name,
92                                       const std::string& storage_id,
93                                       Error* error);
94 
95   // Calls CreateServiceInner above, and on success registers and adds this
96   // service to the provider's list.
97   VPNServiceRefPtr CreateService(const std::string& type,
98                                  const std::string& name,
99                                  const std::string& storage_id,
100                                  Error* error);
101 
102   // Finds a service of type |type| with its Name property set to |name| and its
103   // Provider.Host property set to |host|.
104   VPNServiceRefPtr FindService(const std::string& type,
105                                const std::string& name,
106                                const std::string& host) const;
107 
108   // Populates |type_ptr|, |name_ptr| and |host_ptr| with the appropriate
109   // values from |args|.  Returns True on success, otherwise if any of
110   // these arguments are not available, |error| is populated and False is
111   // returned.
112   static bool GetServiceParametersFromArgs(const KeyValueStore& args,
113                                            std::string* type_ptr,
114                                            std::string* name_ptr,
115                                            std::string* host_ptr,
116                                            Error* error);
117   // Populates |vpn_type_ptr|, |name_ptr| and |host_ptr| with the appropriate
118   // values from profile storgae.  Returns True on success, otherwise if any of
119   // these arguments are not available, |error| is populated and False is
120   // returned.
121   static bool GetServiceParametersFromStorage(const StoreInterface* storage,
122                                               const std::string& entry_name,
123                                               std::string* vpn_type_ptr,
124                                               std::string* name_ptr,
125                                               std::string* host_ptr,
126                                               Error* error);
127 
128   ControlInterface* control_interface_;
129   EventDispatcher* dispatcher_;
130   Metrics* metrics_;
131   Manager* manager_;
132   std::vector<VPNServiceRefPtr> services_;
133 
134   DISALLOW_COPY_AND_ASSIGN(VPNProvider);
135 };
136 
137 }  // namespace shill
138 
139 #endif  // SHILL_VPN_VPN_PROVIDER_H_
140