OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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. |
| 4 |
| 5 #include "chrome/browser/chromeos/vpn_provider/pepper_vpn_provider_service_helpe
r.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/memory/singleton.h" |
| 10 #include "base/strings/string_util.h" |
| 11 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 12 #include "chrome/browser/profiles/profile_manager.h" |
| 13 #include "content/browser/renderer_host/pepper/pepper_vpn_provider_message_filte
r_chromeos.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/render_process_host.h" |
| 16 #include "extensions/browser/api/vpn_provider/vpn_service.h" |
| 17 #include "extensions/browser/api/vpn_provider/vpn_service_factory.h" |
| 18 #include "extensions/common/api/vpn_provider.h" |
| 19 #include "ppapi/proxy/serialized_structs.h" |
| 20 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 21 |
| 22 namespace chromeos { |
| 23 namespace vpn_provider { |
| 24 |
| 25 VpnProviderServiceHelper::VpnProviderServiceHelper() : weak_factory_(this) { |
| 26 } |
| 27 |
| 28 VpnProviderServiceHelper::~VpnProviderServiceHelper() { |
| 29 } |
| 30 |
| 31 // static |
| 32 VpnProviderServiceHelper* VpnProviderServiceHelper::GetInstance() { |
| 33 return base::Singleton<VpnProviderServiceHelper>::get(); |
| 34 } |
| 35 |
| 36 // ---------------- content::BrowserThread::IO Functions ---------------- |
| 37 void VpnProviderServiceHelper::AddServiceID_IO( |
| 38 const std::string& service_id, |
| 39 base::WeakPtr<content::PepperVpnProviderMessageFilterChromeOS> vpn_provider, |
| 40 int render_process_id) { |
| 41 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 42 |
| 43 service_to_host_map_[service_id] = vpn_provider; |
| 44 |
| 45 content::BrowserThread::PostTask( |
| 46 content::BrowserThread::UI, FROM_HERE, |
| 47 base::Bind(&VpnProviderServiceHelper::AddServiceID_UI, |
| 48 weak_factory_.GetWeakPtr(), service_id, render_process_id)); |
| 49 } |
| 50 |
| 51 void VpnProviderServiceHelper::RemoveServiceID_IO(const std::string& service_id, |
| 52 int render_process_id) { |
| 53 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 54 |
| 55 service_to_host_map_.erase(service_id); |
| 56 |
| 57 content::BrowserThread::PostTask( |
| 58 content::BrowserThread::UI, FROM_HERE, |
| 59 base::Bind(&VpnProviderServiceHelper::RemoveServiceID_UI, |
| 60 weak_factory_.GetWeakPtr(), service_id, render_process_id)); |
| 61 } |
| 62 |
| 63 void VpnProviderServiceHelper::OnPlatformMessage_IO( |
| 64 const std::string& service_id, |
| 65 std::string id, |
| 66 PP_VpnProvider_PlatformMessage status, |
| 67 std::string message) { |
| 68 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 69 |
| 70 if (service_to_host_map_.find(service_id) == service_to_host_map_.end()) { |
| 71 return; |
| 72 } |
| 73 |
| 74 service_to_host_map_[service_id]->Send_OnPlatformMessage(id, status, message); |
| 75 } |
| 76 |
| 77 void VpnProviderServiceHelper::OnPacketReceived_IO( |
| 78 const std::string& service_id, |
| 79 const std::vector<char>& data) { |
| 80 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 81 |
| 82 if (service_to_host_map_.find(service_id) == service_to_host_map_.end()) { |
| 83 return; |
| 84 } |
| 85 |
| 86 service_to_host_map_[service_id]->Send_OnPacketReceived(data); |
| 87 } |
| 88 |
| 89 void VpnProviderServiceHelper::OnConfigurationEvent_IO( |
| 90 const std::string& service_id, |
| 91 const std::string& id, |
| 92 PP_VpnProvider_ConfigMessage message, |
| 93 const std::string& name, |
| 94 const std::string& data) { |
| 95 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 96 |
| 97 if (service_to_host_map_.find(service_id) == service_to_host_map_.end()) { |
| 98 return; |
| 99 } |
| 100 |
| 101 service_to_host_map_[service_id]->Send_OnConfigurationEvent(id, message, name, |
| 102 data); |
| 103 } |
| 104 |
| 105 void VpnProviderServiceHelper::OnUIEvent_IO(const std::string& service_id, |
| 106 PP_VpnProvider_UIEvent event, |
| 107 const std::string& id) { |
| 108 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 109 |
| 110 if (service_to_host_map_.find(service_id) == service_to_host_map_.end()) { |
| 111 return; |
| 112 } |
| 113 |
| 114 service_to_host_map_[service_id]->Send_OnUIEvent(event, id); |
| 115 } |
| 116 |
| 117 // ---------------- content::BrowserThread::UI Functions ---------------- |
| 118 chromeos::VpnService* VpnProviderServiceHelper::GetVpnService_UI( |
| 119 int render_process_id) { |
| 120 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 121 |
| 122 content::RenderProcessHost* render_process_host = |
| 123 content::RenderProcessHost::FromID(render_process_id); |
| 124 chromeos::VpnService* service = |
| 125 chromeos::VpnServiceFactory::GetForBrowserContext( |
| 126 render_process_host->GetBrowserContext()); |
| 127 |
| 128 return service; |
| 129 } |
| 130 |
| 131 void VpnProviderServiceHelper::AddServiceID_UI(const std::string& service_id, |
| 132 int render_process_id) { |
| 133 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 134 |
| 135 chromeos::VpnService* service = GetVpnService_UI(render_process_id); |
| 136 if (service) { |
| 137 service->AddPlugin(service_id); |
| 138 } else { |
| 139 NOTREACHED(); |
| 140 } |
| 141 } |
| 142 |
| 143 void VpnProviderServiceHelper::RemoveServiceID_UI(const std::string& service_id, |
| 144 int render_process_id) { |
| 145 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 146 |
| 147 chromeos::VpnService* service = GetVpnService_UI(render_process_id); |
| 148 if (service) { |
| 149 service->RemovePlugin(service_id); |
| 150 } else { |
| 151 NOTREACHED(); |
| 152 } |
| 153 } |
| 154 |
| 155 void VpnProviderServiceHelper::CreateConfig_UI(const std::string& service_id, |
| 156 const std::string& name, |
| 157 int render_process_id, |
| 158 SuccessCallback success, |
| 159 FailureCallback failure) { |
| 160 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 161 |
| 162 chromeos::VpnService* service = GetVpnService_UI(render_process_id); |
| 163 if (service) { |
| 164 service->CreateConfiguration(service_id, service_id, name, success, |
| 165 failure); |
| 166 } else { |
| 167 NOTREACHED(); |
| 168 } |
| 169 } |
| 170 |
| 171 void VpnProviderServiceHelper::DestroyConfig_UI(const std::string& service_id, |
| 172 const std::string& id, |
| 173 int render_process_id, |
| 174 SuccessCallback success, |
| 175 FailureCallback failure) { |
| 176 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 177 |
| 178 chromeos::VpnService* service = GetVpnService_UI(render_process_id); |
| 179 if (service) { |
| 180 service->DestroyConfiguration(service_id, id, success, failure); |
| 181 } else { |
| 182 NOTREACHED(); |
| 183 } |
| 184 } |
| 185 |
| 186 void VpnProviderServiceHelper::SetParameters_UI( |
| 187 const std::string& service_id, |
| 188 const ppapi::proxy::SerializedVpnProviderParameters& serialized_params, |
| 189 int render_process_id, |
| 190 StringCallback success, |
| 191 FailureCallback failure) { |
| 192 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 193 |
| 194 chromeos::VpnService* service = GetVpnService_UI(render_process_id); |
| 195 if (service) { |
| 196 base::DictionaryValue dictionary_params; |
| 197 ConvertParams(serialized_params, &dictionary_params); |
| 198 service->SetParameters(service_id, dictionary_params, success, failure); |
| 199 } else { |
| 200 NOTREACHED(); |
| 201 } |
| 202 } |
| 203 |
| 204 void VpnProviderServiceHelper::SendPacket_UI(const std::string& service_id, |
| 205 const std::vector<char>& data, |
| 206 int render_process_id, |
| 207 SuccessCallback success, |
| 208 FailureCallback failure) { |
| 209 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 210 |
| 211 chromeos::VpnService* service = GetVpnService_UI(render_process_id); |
| 212 if (service) { |
| 213 service->SendPacket(service_id, data, success, failure); |
| 214 } else { |
| 215 NOTREACHED(); |
| 216 } |
| 217 } |
| 218 |
| 219 void VpnProviderServiceHelper::NotifyConnectionStateChanged_UI( |
| 220 const std::string& service_id, |
| 221 PP_VpnProvider_VpnConnectionState status, |
| 222 int render_process_id, |
| 223 SuccessCallback success, |
| 224 FailureCallback failure) { |
| 225 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 226 |
| 227 chromeos::VpnService* service = GetVpnService_UI(render_process_id); |
| 228 if (service) { |
| 229 service->NotifyConnectionStateChanged( |
| 230 service_id, |
| 231 (extensions::api::vpn_provider::VpnConnectionState)status, success, |
| 232 failure); |
| 233 } else { |
| 234 NOTREACHED(); |
| 235 } |
| 236 } |
| 237 |
| 238 void VpnProviderServiceHelper::OnPlatformMessage_UI( |
| 239 const std::string& service_id, |
| 240 std::string id, |
| 241 PP_VpnProvider_PlatformMessage status, |
| 242 std::string message) { |
| 243 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 244 |
| 245 content::BrowserThread::PostTask( |
| 246 content::BrowserThread::IO, FROM_HERE, |
| 247 base::Bind(&VpnProviderServiceHelper::OnPlatformMessage_IO, |
| 248 weak_factory_.GetWeakPtr(), service_id, id, status, message)); |
| 249 } |
| 250 |
| 251 void VpnProviderServiceHelper::OnPacketReceived_UI( |
| 252 const std::string& service_id, |
| 253 const std::vector<char>& data) { |
| 254 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 255 |
| 256 content::BrowserThread::PostTask( |
| 257 content::BrowserThread::IO, FROM_HERE, |
| 258 base::Bind(&VpnProviderServiceHelper::OnPacketReceived_IO, |
| 259 weak_factory_.GetWeakPtr(), service_id, data)); |
| 260 } |
| 261 void VpnProviderServiceHelper::OnConfigurationRemoved_UI( |
| 262 const std::string& service_id, |
| 263 const std::string& id) { |
| 264 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 265 |
| 266 content::BrowserThread::PostTask( |
| 267 content::BrowserThread::IO, FROM_HERE, |
| 268 base::Bind(&VpnProviderServiceHelper::OnConfigurationEvent_IO, |
| 269 weak_factory_.GetWeakPtr(), service_id, id, |
| 270 PP_VPN_PROVIDER_CONFIG_REMOVED, "", "")); |
| 271 } |
| 272 |
| 273 void VpnProviderServiceHelper::OnConfigurationCreated_UI( |
| 274 const std::string& service_id, |
| 275 const std::string& id, |
| 276 const std::string& name, |
| 277 const std::string& data) { |
| 278 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 279 |
| 280 content::BrowserThread::PostTask( |
| 281 content::BrowserThread::IO, FROM_HERE, |
| 282 base::Bind(&VpnProviderServiceHelper::OnConfigurationEvent_IO, |
| 283 weak_factory_.GetWeakPtr(), service_id, id, |
| 284 PP_VPN_PROVIDER_CONFIG_CREATED, id, data)); |
| 285 } |
| 286 |
| 287 void VpnProviderServiceHelper::OnUIEvent_UI(const std::string& service_id, |
| 288 PP_VpnProvider_UIEvent event, |
| 289 const std::string& id) { |
| 290 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 291 |
| 292 content::BrowserThread::PostTask( |
| 293 content::BrowserThread::IO, FROM_HERE, |
| 294 base::Bind(&VpnProviderServiceHelper::OnUIEvent_IO, |
| 295 weak_factory_.GetWeakPtr(), service_id, event, id)); |
| 296 } |
| 297 |
| 298 void VpnProviderServiceHelper::ConvertParams( |
| 299 const ppapi::proxy::SerializedVpnProviderParameters& serialized_params, |
| 300 base::DictionaryValue* dictionary_params) { |
| 301 dictionary_params->SetStringWithoutPathExpansion( |
| 302 shill::kAddressParameterThirdPartyVpn, serialized_params.address); |
| 303 dictionary_params->SetStringWithoutPathExpansion( |
| 304 shill::kSubnetPrefixParameterThirdPartyVpn, serialized_params.subnet); |
| 305 |
| 306 // exclusion_list is mandatory |
| 307 dictionary_params->SetStringWithoutPathExpansion( |
| 308 shill::kExclusionListParameterThirdPartyVpn, |
| 309 base::JoinString(serialized_params.exclusion_list, |
| 310 std::string(1, shill::kIPDelimiter))); |
| 311 |
| 312 // inclusion_list is mandatory |
| 313 dictionary_params->SetStringWithoutPathExpansion( |
| 314 shill::kInclusionListParameterThirdPartyVpn, |
| 315 base::JoinString(serialized_params.inclusion_list, |
| 316 std::string(1, shill::kIPDelimiter))); |
| 317 |
| 318 // mtu is optional |
| 319 if (serialized_params.mtu) { |
| 320 dictionary_params->SetStringWithoutPathExpansion( |
| 321 shill::kMtuParameterThirdPartyVpn, |
| 322 std::to_string(serialized_params.mtu)); |
| 323 } |
| 324 |
| 325 // broadcast_address is optional |
| 326 if (!serialized_params.broadcast_address.empty()) { |
| 327 dictionary_params->SetStringWithoutPathExpansion( |
| 328 shill::kBroadcastAddressParameterThirdPartyVpn, |
| 329 serialized_params.broadcast_address); |
| 330 } |
| 331 |
| 332 // domain_search is optional |
| 333 if (serialized_params.domain_search.size()) { |
| 334 dictionary_params->SetStringWithoutPathExpansion( |
| 335 shill::kDomainSearchParameterThirdPartyVpn, |
| 336 base::JoinString(serialized_params.domain_search, |
| 337 std::string(1, shill::kNonIPDelimiter))); |
| 338 } |
| 339 |
| 340 // dns_servers is mandatory |
| 341 dictionary_params->SetStringWithoutPathExpansion( |
| 342 shill::kDnsServersParameterThirdPartyVpn, |
| 343 base::JoinString(serialized_params.dns_servers, |
| 344 std::string(1, shill::kIPDelimiter))); |
| 345 } |
| 346 |
| 347 } // namespace vpn_provider |
| 348 } // namespace chromeos |
OLD | NEW |