Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / api / vpn_provider / vpn_service.h
blob7aa1dc212ad67944b334dd796ba602a3e67c6b64
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_
8 #include <map>
9 #include <string>
10 #include <vector>
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_event_histogram_value.h"
20 #include "extensions/browser/extension_registry_observer.h"
21 #include "extensions/common/api/vpn_provider.h"
23 namespace base {
25 class DictionaryValue;
26 class ListValue;
28 } // namespace base
30 namespace content {
32 class BrowserContext;
34 } // namespace content
36 namespace extensions {
38 class EventRouter;
39 class ExtensionRegistry;
41 } // namespace extensions
43 namespace chromeos {
45 class NetworkConfigurationHandler;
46 class NetworkProfileHandler;
47 class NetworkStateHandler;
48 class ShillThirdPartyVpnDriverClient;
50 // The class manages the VPN configurations.
51 class VpnService : public KeyedService,
52 public NetworkConfigurationObserver,
53 public NetworkStateHandlerObserver,
54 public extensions::ExtensionRegistryObserver {
55 public:
56 using SuccessCallback = base::Closure;
57 using StringCallback = base::Callback<void(const std::string& result)>;
58 using FailureCallback =
59 base::Callback<void(const std::string& error_name,
60 const std::string& error_message)>;
62 VpnService(content::BrowserContext* browser_context,
63 const std::string& userid_hash,
64 extensions::ExtensionRegistry* extension_registry,
65 extensions::EventRouter* event_router,
66 ShillThirdPartyVpnDriverClient* shill_client,
67 NetworkConfigurationHandler* network_configuration_handler,
68 NetworkProfileHandler* network_profile_handler,
69 NetworkStateHandler* network_state_handler);
70 ~VpnService() override;
72 void SendShowAddDialogToExtension(const std::string& extension_id);
74 void SendShowConfigureDialogToExtension(const std::string& extension_id,
75 const std::string& configuration_id);
77 void SendPlatformError(const std::string& extension_id,
78 const std::string& configuration_id,
79 const std::string& error_message);
81 // NetworkConfigurationObserver:
82 void OnConfigurationCreated(const std::string& service_path,
83 const std::string& profile_path,
84 const base::DictionaryValue& properties,
85 Source source) override;
86 void OnConfigurationRemoved(const std::string& service_path,
87 const std::string& guid,
88 Source source) override;
89 void OnPropertiesSet(const std::string& service_path,
90 const std::string& guid,
91 const base::DictionaryValue& set_properties,
92 Source source) override;
93 void OnConfigurationProfileChanged(const std::string& service_path,
94 const std::string& profile_path,
95 Source source) override;
97 // NetworkStateHandlerObserver:
98 void NetworkListChanged() override;
100 // ExtensionRegistryObserver:
101 void OnExtensionUninstalled(content::BrowserContext* browser_context,
102 const extensions::Extension* extension,
103 extensions::UninstallReason reason) override;
104 void OnExtensionUnloaded(
105 content::BrowserContext* browser_context,
106 const extensions::Extension* extension,
107 extensions::UnloadedExtensionInfo::Reason reason) override;
109 // Creates a new VPN configuration with |configuration_name| as the name and
110 // attaches it to the extension with id |extension_id|.
111 // Calls |success| or |failure| based on the outcome.
112 void CreateConfiguration(const std::string& extension_id,
113 const std::string& extension_name,
114 const std::string& configuration_name,
115 const SuccessCallback& success,
116 const FailureCallback& failure);
118 // Destroys the VPN configuration with |configuration_id| after verifying that
119 // it belongs to the extension with id |extension_id|.
120 // Calls |success| or |failure| based on the outcome.
121 void DestroyConfiguration(const std::string& extension_id,
122 const std::string& configuration_id,
123 const SuccessCallback& success,
124 const FailureCallback& failure);
126 // Set |parameters| for the active VPN configuration after verifying that it
127 // belongs to the extension with id |extension_id|.
128 // Calls |success| or |failure| based on the outcome.
129 void SetParameters(const std::string& extension_id,
130 const base::DictionaryValue& parameters,
131 const StringCallback& success,
132 const FailureCallback& failure);
134 // Sends an IP packet contained in |data| to the active VPN configuration
135 // after verifying that it belongs to the extension with id |extension_id|.
136 // Calls |success| or |failure| based on the outcome.
137 void SendPacket(const std::string& extension_id,
138 const std::vector<char>& data,
139 const SuccessCallback& success,
140 const FailureCallback& failure);
142 // Notifies connection state |state| to the active VPN configuration after
143 // verifying that it belongs to the extension with id |extension_id|.
144 // Calls |success| or |failure| based on the outcome.
145 void NotifyConnectionStateChanged(
146 const std::string& extension_id,
147 extensions::api::vpn_provider::VpnConnectionState state,
148 const SuccessCallback& success,
149 const FailureCallback& failure);
151 // Verifies if a configuration with name |configuration_name| exists for the
152 // extension with id |extension_id|.
153 bool VerifyConfigExistsForTesting(const std::string& extension_id,
154 const std::string& configuration_name);
156 // Verifies if the extension has a configuration that is connected.
157 bool VerifyConfigIsConnectedForTesting(const std::string& extension_id);
159 // Gets the unique key for the configuration |configuration_name| created by
160 // the extension with id |extension_id|.
161 // This method is made public for testing.
162 static std::string GetKey(const std::string& extension_id,
163 const std::string& configuration_name);
165 private:
166 class VpnConfiguration;
168 using StringToConfigurationMap = std::map<std::string, VpnConfiguration*>;
170 // Callback used to indicate that configuration was successfully created.
171 void OnCreateConfigurationSuccess(const SuccessCallback& callback,
172 VpnConfiguration* configuration,
173 const std::string& service_path);
175 // Callback used to indicate that configuration creation failed.
176 void OnCreateConfigurationFailure(
177 const FailureCallback& callback,
178 VpnConfiguration* configuration,
179 const std::string& error_name,
180 scoped_ptr<base::DictionaryValue> error_data);
182 // Callback used to indicate that removing a configuration succeeded.
183 void OnRemoveConfigurationSuccess(const SuccessCallback& callback);
185 // Callback used to indicate that removing a configuration failed.
186 void OnRemoveConfigurationFailure(
187 const FailureCallback& callback,
188 const std::string& error_name,
189 scoped_ptr<base::DictionaryValue> error_data);
191 // Callback used to indicate that GetProperties was successful.
192 void OnGetPropertiesSuccess(const std::string& service_path,
193 const base::DictionaryValue& dictionary);
195 // Callback used to indicate that GetProperties failed.
196 void OnGetPropertiesFailure(const std::string& error_name,
197 scoped_ptr<base::DictionaryValue> error_data);
199 // Creates and adds the configuration to the internal store.
200 VpnConfiguration* CreateConfigurationInternal(
201 const std::string& extension_id,
202 const std::string& configuration_name,
203 const std::string& key);
205 // Removes configuration from the internal store and destroys it.
206 void DestroyConfigurationInternal(VpnConfiguration* configuration);
208 // Verifies if |active_configuration_| exists and if the extension with id
209 // |extension_id| is authorized to access it.
210 bool DoesActiveConfigurationExistAndIsAccessAuthorized(
211 const std::string& extension_id);
213 // Send an event with name |event_name| and arguments |event_args| to the
214 // extension with id |extension_id|.
215 void SendSignalToExtension(const std::string& extension_id,
216 extensions::events::HistogramValue histogram_value,
217 const std::string& event_name,
218 scoped_ptr<base::ListValue> event_args);
220 // Destroy configurations belonging to the extension.
221 void DestroyConfigurationsForExtension(
222 const extensions::Extension* extension);
224 // Set the active configuration.
225 void SetActiveConfiguration(VpnConfiguration* configuration);
227 content::BrowserContext* browser_context_;
228 std::string userid_hash_;
230 extensions::ExtensionRegistry* extension_registry_;
231 extensions::EventRouter* event_router_;
232 ShillThirdPartyVpnDriverClient* shill_client_;
233 NetworkConfigurationHandler* network_configuration_handler_;
234 NetworkProfileHandler* network_profile_handler_;
235 NetworkStateHandler* network_state_handler_;
237 VpnConfiguration* active_configuration_;
239 // Key map owns the VpnConfigurations.
240 StringToConfigurationMap key_to_configuration_map_;
242 // Service path does not own the VpnConfigurations.
243 StringToConfigurationMap service_path_to_configuration_map_;
245 base::WeakPtrFactory<VpnService> weak_factory_;
247 DISALLOW_COPY_AND_ASSIGN(VpnService);
250 } // namespace chromeos
252 #endif // EXTENSIONS_BROWSER_API_VPN_PROVIDER_VPN_SERVICE_H_