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 StringCallback
= base::Callback
<void(const std::string
& result
)>;
57 using FailureCallback
=
58 base::Callback
<void(const std::string
& error_name
,
59 const std::string
& error_message
)>;
61 VpnService(content::BrowserContext
* browser_context
,
62 const std::string
& userid_hash
,
63 extensions::ExtensionRegistry
* extension_registry
,
64 extensions::EventRouter
* event_router
,
65 ShillThirdPartyVpnDriverClient
* shill_client
,
66 NetworkConfigurationHandler
* network_configuration_handler
,
67 NetworkProfileHandler
* network_profile_handler
,
68 NetworkStateHandler
* network_state_handler
);
69 ~VpnService() override
;
71 void SendShowAddDialogToExtension(const std::string
& extension_id
);
73 void SendShowConfigureDialogToExtension(const std::string
& extension_id
,
74 const std::string
& configuration_id
);
76 void SendPlatformError(const std::string
& extension_id
,
77 const std::string
& configuration_id
,
78 const std::string
& error_message
);
80 // NetworkConfigurationObserver:
81 void OnConfigurationCreated(const std::string
& service_path
,
82 const std::string
& profile_path
,
83 const base::DictionaryValue
& properties
,
84 Source source
) override
;
85 void OnConfigurationRemoved(const std::string
& service_path
,
86 const std::string
& guid
,
87 Source source
) override
;
88 void OnPropertiesSet(const std::string
& service_path
,
89 const std::string
& guid
,
90 const base::DictionaryValue
& set_properties
,
91 Source source
) override
;
92 void OnConfigurationProfileChanged(const std::string
& service_path
,
93 const std::string
& profile_path
,
94 Source source
) override
;
96 // NetworkStateHandlerObserver:
97 void NetworkListChanged() override
;
99 // ExtensionRegistryObserver:
100 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
101 const extensions::Extension
* extension
,
102 extensions::UninstallReason reason
) override
;
103 void OnExtensionUnloaded(
104 content::BrowserContext
* browser_context
,
105 const extensions::Extension
* extension
,
106 extensions::UnloadedExtensionInfo::Reason reason
) override
;
108 // Creates a new VPN configuration with |configuration_name| as the name and
109 // attaches it to the extension with id |extension_id|.
110 // Calls |success| or |failure| based on the outcome.
111 void CreateConfiguration(const std::string
& extension_id
,
112 const std::string
& extension_name
,
113 const std::string
& configuration_name
,
114 const SuccessCallback
& success
,
115 const FailureCallback
& failure
);
117 // Destroys the VPN configuration with |configuration_id| after verifying that
118 // it belongs to the extension with id |extension_id|.
119 // Calls |success| or |failure| based on the outcome.
120 void DestroyConfiguration(const std::string
& extension_id
,
121 const std::string
& configuration_id
,
122 const SuccessCallback
& success
,
123 const FailureCallback
& failure
);
125 // Set |parameters| for the active VPN configuration after verifying that it
126 // belongs to the extension with id |extension_id|.
127 // Calls |success| or |failure| based on the outcome.
128 void SetParameters(const std::string
& extension_id
,
129 const base::DictionaryValue
& parameters
,
130 const StringCallback
& success
,
131 const FailureCallback
& failure
);
133 // Sends an IP packet contained in |data| to the active VPN configuration
134 // after verifying that it belongs to the extension with id |extension_id|.
135 // Calls |success| or |failure| based on the outcome.
136 void SendPacket(const std::string
& extension_id
,
137 const std::vector
<char>& data
,
138 const SuccessCallback
& success
,
139 const FailureCallback
& failure
);
141 // Notifies connection state |state| to the active VPN configuration after
142 // verifying that it belongs to the extension with id |extension_id|.
143 // Calls |success| or |failure| based on the outcome.
144 void NotifyConnectionStateChanged(
145 const std::string
& extension_id
,
146 extensions::core_api::vpn_provider::VpnConnectionState state
,
147 const SuccessCallback
& success
,
148 const FailureCallback
& failure
);
150 // Verifies if a configuration with name |configuration_name| exists for the
151 // extension with id |extension_id|.
152 bool VerifyConfigExistsForTesting(const std::string
& extension_id
,
153 const std::string
& configuration_name
);
155 // Verifies if the extension has a configuration that is connected.
156 bool VerifyConfigIsConnectedForTesting(const std::string
& extension_id
);
158 // Gets the unique key for the configuration |configuration_name| created by
159 // the extension with id |extension_id|.
160 // This method is made public for testing.
161 static std::string
GetKey(const std::string
& extension_id
,
162 const std::string
& configuration_name
);
165 class VpnConfiguration
;
167 using StringToConfigurationMap
= std::map
<std::string
, VpnConfiguration
*>;
169 // Callback used to indicate that configuration was successfully created.
170 void OnCreateConfigurationSuccess(const SuccessCallback
& callback
,
171 VpnConfiguration
* configuration
,
172 const std::string
& service_path
);
174 // Callback used to indicate that configuration creation failed.
175 void OnCreateConfigurationFailure(
176 const FailureCallback
& callback
,
177 VpnConfiguration
* configuration
,
178 const std::string
& error_name
,
179 scoped_ptr
<base::DictionaryValue
> error_data
);
181 // Callback used to indicate that removing a configuration succeeded.
182 void OnRemoveConfigurationSuccess(const SuccessCallback
& callback
);
184 // Callback used to indicate that removing a configuration failed.
185 void OnRemoveConfigurationFailure(
186 const FailureCallback
& callback
,
187 const std::string
& error_name
,
188 scoped_ptr
<base::DictionaryValue
> error_data
);
190 // Callback used to indicate that GetProperties was successful.
191 void OnGetPropertiesSuccess(const std::string
& service_path
,
192 const base::DictionaryValue
& dictionary
);
194 // Callback used to indicate that GetProperties failed.
195 void OnGetPropertiesFailure(const std::string
& error_name
,
196 scoped_ptr
<base::DictionaryValue
> error_data
);
198 // Creates and adds the configuration to the internal store.
199 VpnConfiguration
* CreateConfigurationInternal(
200 const std::string
& extension_id
,
201 const std::string
& configuration_name
,
202 const std::string
& key
);
204 // Removes configuration from the internal store and destroys it.
205 void DestroyConfigurationInternal(VpnConfiguration
* configuration
);
207 // Verifies if |active_configuration_| exists and if the extension with id
208 // |extension_id| is authorized to access it.
209 bool DoesActiveConfigurationExistAndIsAccessAuthorized(
210 const std::string
& extension_id
);
212 // Send an event with name |event_name| and arguments |event_args| to the
213 // extension with id |extension_id|.
214 void SendSignalToExtension(const std::string
& extension_id
,
215 const std::string
& event_name
,
216 scoped_ptr
<base::ListValue
> event_args
);
218 // Destroy configurations belonging to the extension.
219 void DestroyConfigurationsForExtension(
220 const extensions::Extension
* extension
);
222 // Set the active configuration.
223 void SetActiveConfiguration(VpnConfiguration
* configuration
);
225 content::BrowserContext
* browser_context_
;
226 std::string userid_hash_
;
228 extensions::ExtensionRegistry
* extension_registry_
;
229 extensions::EventRouter
* event_router_
;
230 ShillThirdPartyVpnDriverClient
* shill_client_
;
231 NetworkConfigurationHandler
* network_configuration_handler_
;
232 NetworkProfileHandler
* network_profile_handler_
;
233 NetworkStateHandler
* network_state_handler_
;
235 VpnConfiguration
* active_configuration_
;
237 // Key map owns the VpnConfigurations.
238 StringToConfigurationMap key_to_configuration_map_
;
240 // Service path does not own the VpnConfigurations.
241 StringToConfigurationMap service_path_to_configuration_map_
;
243 base::WeakPtrFactory
<VpnService
> weak_factory_
;
245 DISALLOW_COPY_AND_ASSIGN(VpnService
);
248 } // namespace chromeos
250 #endif // EXTENSIONS_BROWSER_API_VPN_PROVIDER_VPN_SERVICE_H_