Index: content/public/browser/pepper_vpn_provider_service_helper.cc |
diff --git a/content/public/browser/pepper_vpn_provider_service_helper.cc b/content/public/browser/pepper_vpn_provider_service_helper.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a6099e07f711f9e2f8a0e71fe978112855168bf7 |
--- /dev/null |
+++ b/content/public/browser/pepper_vpn_provider_service_helper.cc |
@@ -0,0 +1,147 @@ |
+ |
+// Copyright 2016 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 "content/public/browser/pepper_vpn_provider_service_helper.h" |
+ |
+#include "base/memory/singleton.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/render_process_host.h" |
+ |
+namespace content { |
+ |
+VpnMessageFilterDelegate::~VpnMessageFilterDelegate() {} |
+ |
+VpnServiceDelegate::~VpnServiceDelegate() {} |
+ |
+VpnProviderServiceHelper::VpnProviderServiceHelper() : weak_factory_(this) {} |
+ |
+VpnProviderServiceHelper::~VpnProviderServiceHelper() {} |
+ |
+// static |
+VpnProviderServiceHelper* VpnProviderServiceHelper::GetInstance() { |
+ return base::Singleton<VpnProviderServiceHelper>::get(); |
+} |
+ |
+void VpnProviderServiceHelper::OnPacketReceivedIO( |
+ const std::string& extension_id, |
+ const std::vector<char>& data) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ |
+ if (extension_to_host_map_.find(extension_id) == |
+ extension_to_host_map_.end()) { |
+ NOTREACHED(); |
+ return; |
+ } |
+ |
+ extension_to_host_map_[extension_id]->SendOnPacketReceived(data); |
+} |
+ |
+void VpnProviderServiceHelper::OnUnbindIO(const std::string& extension_id) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ |
+ if (extension_to_host_map_.find(extension_id) == |
+ extension_to_host_map_.end()) { |
+ NOTREACHED(); |
+ return; |
+ } |
+ |
+ extension_to_host_map_[extension_id]->SendOnUnbind(); |
+} |
+ |
+void VpnProviderServiceHelper::RegisterDelegate( |
+ const std::string& extension_id, |
+ std::unique_ptr<VpnMessageFilterDelegate> delegate) { |
+ extension_to_host_map_[extension_id] = std::move(delegate); |
+} |
+ |
+void VpnProviderServiceHelper::RegisterDelegate( |
+ content::BrowserContext* context, |
+ std::unique_ptr<VpnServiceDelegate> delegate) { |
+ context_to_service_map_[context] = std::move(delegate); |
+} |
+ |
+void VpnProviderServiceHelper::Bind(const std::string& extension_id, |
+ const std::string& configuration_id, |
+ const std::string& configuration_name, |
+ int render_process_id, |
+ const SuccessCallback& success, |
+ const FailureCallback& failure) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ content::RenderProcessHost* render_process_host = |
+ content::RenderProcessHost::FromID(render_process_id); |
+ if (render_process_host == NULL) { |
+ NOTREACHED(); |
+ return; |
+ } |
+ content::BrowserContext* context = render_process_host->GetBrowserContext(); |
+ if (context == NULL) { |
+ NOTREACHED(); |
+ return; |
+ } |
+ if (context_to_service_map_.find(context) == context_to_service_map_.end()) { |
+ NOTREACHED(); |
+ return; |
+ } |
+ if (extension_to_host_map_.find(extension_id) == |
+ extension_to_host_map_.end()) { |
+ NOTREACHED(); |
+ return; |
+ } |
+ |
+ context_to_service_map_[context]->Bind(extension_id, configuration_id, |
+ configuration_name, success, failure); |
+} |
+ |
+void VpnProviderServiceHelper::OnUnbind(const std::string& extension_id) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::IO, FROM_HERE, |
+ base::Bind(&VpnProviderServiceHelper::OnUnbindIO, |
+ weak_factory_.GetWeakPtr(), extension_id)); |
+} |
+ |
+void VpnProviderServiceHelper::SendPacket(const std::string& extension_id, |
+ const std::vector<char>& data, |
+ int render_process_id, |
+ SuccessCallback success, |
+ FailureCallback failure) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ content::RenderProcessHost* render_process_host = |
+ content::RenderProcessHost::FromID(render_process_id); |
+ if (render_process_host == NULL) { |
+ NOTREACHED(); |
+ return; |
+ } |
+ |
+ content::BrowserContext* context = render_process_host->GetBrowserContext(); |
+ if (context == NULL) { |
+ NOTREACHED(); |
+ return; |
+ } |
+ |
+ if (context_to_service_map_.find(context) == context_to_service_map_.end()) { |
+ NOTREACHED(); |
+ return; |
+ } |
+ |
+ context_to_service_map_[context]->SendPacket(extension_id, data, success, |
+ failure); |
+} |
+ |
+void VpnProviderServiceHelper::OnPacketReceived(const std::string& extension_id, |
+ const std::vector<char>& data) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ // TODO(adrian.belgun): |
+ // Possile optimization: Handle packets only on UI thread (3) |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::IO, FROM_HERE, |
+ base::Bind(&VpnProviderServiceHelper::OnPacketReceivedIO, |
+ weak_factory_.GetWeakPtr(), extension_id, data)); |
+} |
+ |
+} // namespace content |