1 // Copyright 2014 The Chromium 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.
5 #ifndef EXTENSIONS_BROWSER_API_VPN_PROVIDER_VPN_SERVICE_H_
6 #define EXTENSIONS_BROWSER_API_VPN_PROVIDER_VPN_SERVICE_H_
12 #include "base/callback.h"
13 #include "base/macros.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "chromeos/network/network_configuration_observer.h"
17 #include "chromeos/network/network_state_handler_observer.h"
18 #include "components/keyed_service/core/keyed_service.h"
19 #include "extensions/browser/extension_registry_observer.h"
20 #include "extensions/common/api/vpn_provider.h"
24 class DictionaryValue
;
33 } // namespace content
35 namespace extensions
{
38 class ExtensionRegistry
;
40 } // namespace extensions
44 class NetworkConfigurationHandler
;
45 class NetworkProfileHandler
;
46 class NetworkStateHandler
;
47 class ShillThirdPartyVpnDriverClient
;
49 // The class manages the VPN configurations.
50 class VpnService
: public KeyedService
,
51 public NetworkConfigurationObserver
,
52 public NetworkStateHandlerObserver
,
53 public extensions::ExtensionRegistryObserver
{
55 using SuccessCallback
= base::Closure
;
56 using FailureCallback
=
57 base::Callback
<void(const std::string
& error_name
,
58 const std::string
& error_message
)>;
60 VpnService(content::BrowserContext
* browser_context
,
61 const std::string
& userid_hash
,
62 extensions::ExtensionRegistry
* extension_registry
,
63 extensions::EventRouter
* event_router
,
64 ShillThirdPartyVpnDriverClient
* shill_client
,
65 NetworkConfigurationHandler
* network_configuration_handler
,
66 NetworkProfileHandler
* network_profile_handler
,
67 NetworkStateHandler
* network_state_handler
);
68 ~VpnService() override
;
70 // NetworkConfigurationObserver:
71 void OnConfigurationCreated(const std::string
& service_path
,
72 const std::string
& profile_path
,
73 const base::DictionaryValue
& properties
,
74 Source source
) override
;
75 void OnConfigurationRemoved(const std::string
& service_path
,
76 const std::string
& guid
,
77 Source source
) override
;
78 void OnPropertiesSet(const std::string
& service_path
,
79 const std::string
& guid
,
80 const base::DictionaryValue
& set_properties
,
81 Source source
) override
;
82 void OnConfigurationProfileChanged(const std::string
& service_path
,
83 const std::string
& profile_path
,
84 Source source
) override
;
86 // NetworkStateHandlerObserver:
87 void NetworkListChanged() override
;
89 // ExtensionRegistryObserver:
90 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
91 const extensions::Extension
* extension
,
92 extensions::UninstallReason reason
) override
;
94 // Creates a new VPN configuration with |configuration_name| as the name and
95 // attaches it to the extension with id |extension_id|.
96 // Calls |success| or |failure| based on the outcome.
97 void CreateConfiguration(const std::string
& extension_id
,
98 const std::string
& extension_name
,
99 const std::string
& configuration_name
,
100 const SuccessCallback
& success
,
101 const FailureCallback
& failure
);
103 // Destroys the VPN configuration with the name |configuration_name| after
104 // verifying that it belongs to the extension with id |extension_id|.
105 // Calls |success| or |failure| based on the outcome.
106 void DestroyConfiguration(const std::string
& extension_id
,
107 const std::string
& configuration_name
,
108 const SuccessCallback
& success
,
109 const FailureCallback
& failure
);
111 // Set |parameters| for the active VPN configuration after verifying that it
112 // belongs to the extension with id |extension_id|.
113 // Calls |success| or |failure| based on the outcome.
114 void SetParameters(const std::string
& extension_id
,
115 const base::DictionaryValue
& parameters
,
116 const SuccessCallback
& success
,
117 const FailureCallback
& failure
);
119 // Sends an IP packet contained in |data| to the active VPN configuration
120 // after verifying that it belongs to the extension with id |extension_id|.
121 // Calls |success| or |failure| based on the outcome.
122 void SendPacket(const std::string
& extension_id
,
123 const std::vector
<char>& data
,
124 const SuccessCallback
& success
,
125 const FailureCallback
& failure
);
127 // Notifies connection state |state| to the active VPN configuration after
128 // verifying that it belongs to the extension with id |extension_id|.
129 // Calls |success| or |failure| based on the outcome.
130 void NotifyConnectionStateChanged(
131 const std::string
& extension_id
,
132 extensions::core_api::vpn_provider::VpnConnectionState state
,
133 const SuccessCallback
& success
,
134 const FailureCallback
& failure
);
137 class VpnConfiguration
;
139 using StringToConfigurationMap
= std::map
<std::string
, VpnConfiguration
*>;
141 // Callback used to indicate that configuration was successfully created.
142 void OnCreateConfigurationSuccess(const SuccessCallback
& callback
,
143 VpnConfiguration
* configuration
,
144 const std::string
& service_path
);
146 // Callback used to indicate that configuration creation failed.
147 void OnCreateConfigurationFailure(
148 const FailureCallback
& callback
,
149 VpnConfiguration
* configuration
,
150 const std::string
& error_name
,
151 scoped_ptr
<base::DictionaryValue
> error_data
);
153 // Callback used to indicate that removing a configuration succeeded.
154 void OnRemoveConfigurationSuccess(const SuccessCallback
& callback
);
156 // Callback used to indicate that removing a configuration failed.
157 void OnRemoveConfigurationFailure(
158 const FailureCallback
& callback
,
159 const std::string
& error_name
,
160 scoped_ptr
<base::DictionaryValue
> error_data
);
162 // Callback used to indicate that GetProperties was successful.
163 void OnGetPropertiesSuccess(const std::string
& service_path
,
164 const base::DictionaryValue
& dictionary
);
166 // Callback used to indicate that GetProperties failed.
167 void OnGetPropertiesFailure(const std::string
& error_name
,
168 scoped_ptr
<base::DictionaryValue
> error_data
);
170 // Creates and adds the configuration to the internal store.
171 VpnConfiguration
* CreateConfigurationInternal(
172 const std::string
& extension_id
,
173 const std::string
& configuration_name
,
174 const std::string
& key
);
176 // Removes configuration from the internal store and destroys it.
177 void DestroyConfigurationInternal(VpnConfiguration
* configuration
);
179 // Verifies if |active_configuration_| exists and if the extension with id
180 // |extension_id| is authorized to access it.
181 bool DoesActiveConfigurationExistAndIsAccessAuthorized(
182 const std::string
& extension_id
);
184 // Send an event with name |event_name| and arguments |event_args| to the
185 // extension with id |extension_id|.
186 void SendSignalToExtension(const std::string
& extension_id
,
187 const std::string
& event_name
,
188 scoped_ptr
<base::ListValue
> event_args
);
190 // Set the active configuration.
191 void SetActiveConfiguration(VpnConfiguration
* configuration
);
193 content::BrowserContext
* browser_context_
;
194 std::string userid_hash_
;
196 extensions::ExtensionRegistry
* extension_registry_
;
197 extensions::EventRouter
* event_router_
;
198 ShillThirdPartyVpnDriverClient
* shill_client_
;
199 NetworkConfigurationHandler
* network_configuration_handler_
;
200 NetworkProfileHandler
* network_profile_handler_
;
201 NetworkStateHandler
* network_state_handler_
;
203 VpnConfiguration
* active_configuration_
;
205 // Key map owns the VpnConfigurations.
206 StringToConfigurationMap key_to_configuration_map_
;
208 // Service path does not own the VpnConfigurations.
209 StringToConfigurationMap service_path_to_configuration_map_
;
211 base::WeakPtrFactory
<VpnService
> weak_factory_
;
213 DISALLOW_COPY_AND_ASSIGN(VpnService
);
216 } // namespace chromeos
218 #endif // EXTENSIONS_BROWSER_API_VPN_PROVIDER_VPN_SERVICE_H_