Index: chrome/browser/chromeos/vpn_provider/pepper_vpn_provider_service_helper.cc |
diff --git a/chrome/browser/chromeos/vpn_provider/pepper_vpn_provider_service_helper.cc b/chrome/browser/chromeos/vpn_provider/pepper_vpn_provider_service_helper.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..671d3a0dfca60824abc509589148884ac16f9370 |
--- /dev/null |
+++ b/chrome/browser/chromeos/vpn_provider/pepper_vpn_provider_service_helper.cc |
@@ -0,0 +1,348 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/chromeos/vpn_provider/pepper_vpn_provider_service_helper.h" |
+ |
+#include <string> |
+ |
+#include "base/memory/singleton.h" |
+#include "base/strings/string_util.h" |
+#include "chrome/browser/chromeos/profiles/profile_helper.h" |
+#include "chrome/browser/profiles/profile_manager.h" |
+#include "content/browser/renderer_host/pepper/pepper_vpn_provider_message_filter_chromeos.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/render_process_host.h" |
+#include "extensions/browser/api/vpn_provider/vpn_service.h" |
+#include "extensions/browser/api/vpn_provider/vpn_service_factory.h" |
+#include "extensions/common/api/vpn_provider.h" |
+#include "ppapi/proxy/serialized_structs.h" |
+#include "third_party/cros_system_api/dbus/service_constants.h" |
+ |
+namespace chromeos { |
+namespace vpn_provider { |
+ |
+VpnProviderServiceHelper::VpnProviderServiceHelper() : weak_factory_(this) { |
+} |
+ |
+VpnProviderServiceHelper::~VpnProviderServiceHelper() { |
+} |
+ |
+// static |
+VpnProviderServiceHelper* VpnProviderServiceHelper::GetInstance() { |
+ return base::Singleton<VpnProviderServiceHelper>::get(); |
+} |
+ |
+// ---------------- content::BrowserThread::IO Functions ---------------- |
+void VpnProviderServiceHelper::AddServiceID_IO( |
+ const std::string& service_id, |
+ base::WeakPtr<content::PepperVpnProviderMessageFilterChromeOS> vpn_provider, |
+ int render_process_id) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ |
+ service_to_host_map_[service_id] = vpn_provider; |
+ |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, FROM_HERE, |
+ base::Bind(&VpnProviderServiceHelper::AddServiceID_UI, |
+ weak_factory_.GetWeakPtr(), service_id, render_process_id)); |
+} |
+ |
+void VpnProviderServiceHelper::RemoveServiceID_IO(const std::string& service_id, |
+ int render_process_id) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ |
+ service_to_host_map_.erase(service_id); |
+ |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, FROM_HERE, |
+ base::Bind(&VpnProviderServiceHelper::RemoveServiceID_UI, |
+ weak_factory_.GetWeakPtr(), service_id, render_process_id)); |
+} |
+ |
+void VpnProviderServiceHelper::OnPlatformMessage_IO( |
+ const std::string& service_id, |
+ std::string id, |
+ PP_VpnProvider_PlatformMessage status, |
+ std::string message) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ |
+ if (service_to_host_map_.find(service_id) == service_to_host_map_.end()) { |
+ return; |
+ } |
+ |
+ service_to_host_map_[service_id]->Send_OnPlatformMessage(id, status, message); |
+} |
+ |
+void VpnProviderServiceHelper::OnPacketReceived_IO( |
+ const std::string& service_id, |
+ const std::vector<char>& data) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ |
+ if (service_to_host_map_.find(service_id) == service_to_host_map_.end()) { |
+ return; |
+ } |
+ |
+ service_to_host_map_[service_id]->Send_OnPacketReceived(data); |
+} |
+ |
+void VpnProviderServiceHelper::OnConfigurationEvent_IO( |
+ const std::string& service_id, |
+ const std::string& id, |
+ PP_VpnProvider_ConfigMessage message, |
+ const std::string& name, |
+ const std::string& data) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ |
+ if (service_to_host_map_.find(service_id) == service_to_host_map_.end()) { |
+ return; |
+ } |
+ |
+ service_to_host_map_[service_id]->Send_OnConfigurationEvent(id, message, name, |
+ data); |
+} |
+ |
+void VpnProviderServiceHelper::OnUIEvent_IO(const std::string& service_id, |
+ PP_VpnProvider_UIEvent event, |
+ const std::string& id) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ |
+ if (service_to_host_map_.find(service_id) == service_to_host_map_.end()) { |
+ return; |
+ } |
+ |
+ service_to_host_map_[service_id]->Send_OnUIEvent(event, id); |
+} |
+ |
+// ---------------- content::BrowserThread::UI Functions ---------------- |
+chromeos::VpnService* VpnProviderServiceHelper::GetVpnService_UI( |
+ int render_process_id) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ content::RenderProcessHost* render_process_host = |
+ content::RenderProcessHost::FromID(render_process_id); |
+ chromeos::VpnService* service = |
+ chromeos::VpnServiceFactory::GetForBrowserContext( |
+ render_process_host->GetBrowserContext()); |
+ |
+ return service; |
+} |
+ |
+void VpnProviderServiceHelper::AddServiceID_UI(const std::string& service_id, |
+ int render_process_id) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ chromeos::VpnService* service = GetVpnService_UI(render_process_id); |
+ if (service) { |
+ service->AddPlugin(service_id); |
+ } else { |
+ NOTREACHED(); |
+ } |
+} |
+ |
+void VpnProviderServiceHelper::RemoveServiceID_UI(const std::string& service_id, |
+ int render_process_id) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ chromeos::VpnService* service = GetVpnService_UI(render_process_id); |
+ if (service) { |
+ service->RemovePlugin(service_id); |
+ } else { |
+ NOTREACHED(); |
+ } |
+} |
+ |
+void VpnProviderServiceHelper::CreateConfig_UI(const std::string& service_id, |
+ const std::string& name, |
+ int render_process_id, |
+ SuccessCallback success, |
+ FailureCallback failure) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ chromeos::VpnService* service = GetVpnService_UI(render_process_id); |
+ if (service) { |
+ service->CreateConfiguration(service_id, service_id, name, success, |
+ failure); |
+ } else { |
+ NOTREACHED(); |
+ } |
+} |
+ |
+void VpnProviderServiceHelper::DestroyConfig_UI(const std::string& service_id, |
+ const std::string& id, |
+ int render_process_id, |
+ SuccessCallback success, |
+ FailureCallback failure) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ chromeos::VpnService* service = GetVpnService_UI(render_process_id); |
+ if (service) { |
+ service->DestroyConfiguration(service_id, id, success, failure); |
+ } else { |
+ NOTREACHED(); |
+ } |
+} |
+ |
+void VpnProviderServiceHelper::SetParameters_UI( |
+ const std::string& service_id, |
+ const ppapi::proxy::SerializedVpnProviderParameters& serialized_params, |
+ int render_process_id, |
+ StringCallback success, |
+ FailureCallback failure) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ chromeos::VpnService* service = GetVpnService_UI(render_process_id); |
+ if (service) { |
+ base::DictionaryValue dictionary_params; |
+ ConvertParams(serialized_params, &dictionary_params); |
+ service->SetParameters(service_id, dictionary_params, success, failure); |
+ } else { |
+ NOTREACHED(); |
+ } |
+} |
+ |
+void VpnProviderServiceHelper::SendPacket_UI(const std::string& service_id, |
+ const std::vector<char>& data, |
+ int render_process_id, |
+ SuccessCallback success, |
+ FailureCallback failure) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ chromeos::VpnService* service = GetVpnService_UI(render_process_id); |
+ if (service) { |
+ service->SendPacket(service_id, data, success, failure); |
+ } else { |
+ NOTREACHED(); |
+ } |
+} |
+ |
+void VpnProviderServiceHelper::NotifyConnectionStateChanged_UI( |
+ const std::string& service_id, |
+ PP_VpnProvider_VpnConnectionState status, |
+ int render_process_id, |
+ SuccessCallback success, |
+ FailureCallback failure) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ chromeos::VpnService* service = GetVpnService_UI(render_process_id); |
+ if (service) { |
+ service->NotifyConnectionStateChanged( |
+ service_id, |
+ (extensions::api::vpn_provider::VpnConnectionState)status, success, |
+ failure); |
+ } else { |
+ NOTREACHED(); |
+ } |
+} |
+ |
+void VpnProviderServiceHelper::OnPlatformMessage_UI( |
+ const std::string& service_id, |
+ std::string id, |
+ PP_VpnProvider_PlatformMessage status, |
+ std::string message) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::IO, FROM_HERE, |
+ base::Bind(&VpnProviderServiceHelper::OnPlatformMessage_IO, |
+ weak_factory_.GetWeakPtr(), service_id, id, status, message)); |
+} |
+ |
+void VpnProviderServiceHelper::OnPacketReceived_UI( |
+ const std::string& service_id, |
+ const std::vector<char>& data) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::IO, FROM_HERE, |
+ base::Bind(&VpnProviderServiceHelper::OnPacketReceived_IO, |
+ weak_factory_.GetWeakPtr(), service_id, data)); |
+} |
+void VpnProviderServiceHelper::OnConfigurationRemoved_UI( |
+ const std::string& service_id, |
+ const std::string& id) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::IO, FROM_HERE, |
+ base::Bind(&VpnProviderServiceHelper::OnConfigurationEvent_IO, |
+ weak_factory_.GetWeakPtr(), service_id, id, |
+ PP_VPN_PROVIDER_CONFIG_REMOVED, "", "")); |
+} |
+ |
+void VpnProviderServiceHelper::OnConfigurationCreated_UI( |
+ const std::string& service_id, |
+ const std::string& id, |
+ const std::string& name, |
+ const std::string& data) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::IO, FROM_HERE, |
+ base::Bind(&VpnProviderServiceHelper::OnConfigurationEvent_IO, |
+ weak_factory_.GetWeakPtr(), service_id, id, |
+ PP_VPN_PROVIDER_CONFIG_CREATED, id, data)); |
+} |
+ |
+void VpnProviderServiceHelper::OnUIEvent_UI(const std::string& service_id, |
+ PP_VpnProvider_UIEvent event, |
+ const std::string& id) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::IO, FROM_HERE, |
+ base::Bind(&VpnProviderServiceHelper::OnUIEvent_IO, |
+ weak_factory_.GetWeakPtr(), service_id, event, id)); |
+} |
+ |
+void VpnProviderServiceHelper::ConvertParams( |
+ const ppapi::proxy::SerializedVpnProviderParameters& serialized_params, |
+ base::DictionaryValue* dictionary_params) { |
+ dictionary_params->SetStringWithoutPathExpansion( |
+ shill::kAddressParameterThirdPartyVpn, serialized_params.address); |
+ dictionary_params->SetStringWithoutPathExpansion( |
+ shill::kSubnetPrefixParameterThirdPartyVpn, serialized_params.subnet); |
+ |
+ // exclusion_list is mandatory |
+ dictionary_params->SetStringWithoutPathExpansion( |
+ shill::kExclusionListParameterThirdPartyVpn, |
+ base::JoinString(serialized_params.exclusion_list, |
+ std::string(1, shill::kIPDelimiter))); |
+ |
+ // inclusion_list is mandatory |
+ dictionary_params->SetStringWithoutPathExpansion( |
+ shill::kInclusionListParameterThirdPartyVpn, |
+ base::JoinString(serialized_params.inclusion_list, |
+ std::string(1, shill::kIPDelimiter))); |
+ |
+ // mtu is optional |
+ if (serialized_params.mtu) { |
+ dictionary_params->SetStringWithoutPathExpansion( |
+ shill::kMtuParameterThirdPartyVpn, |
+ std::to_string(serialized_params.mtu)); |
+ } |
+ |
+ // broadcast_address is optional |
+ if (!serialized_params.broadcast_address.empty()) { |
+ dictionary_params->SetStringWithoutPathExpansion( |
+ shill::kBroadcastAddressParameterThirdPartyVpn, |
+ serialized_params.broadcast_address); |
+ } |
+ |
+ // domain_search is optional |
+ if (serialized_params.domain_search.size()) { |
+ dictionary_params->SetStringWithoutPathExpansion( |
+ shill::kDomainSearchParameterThirdPartyVpn, |
+ base::JoinString(serialized_params.domain_search, |
+ std::string(1, shill::kNonIPDelimiter))); |
+ } |
+ |
+ // dns_servers is mandatory |
+ dictionary_params->SetStringWithoutPathExpansion( |
+ shill::kDnsServersParameterThirdPartyVpn, |
+ base::JoinString(serialized_params.dns_servers, |
+ std::string(1, shill::kIPDelimiter))); |
+} |
+ |
+} // namespace vpn_provider |
+} // namespace chromeos |