OLD | NEW |
(Empty) | |
| 1 |
| 2 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 // Use of this source code is governed by a BSD-style license that can be |
| 4 // found in the LICENSE file. |
| 5 |
| 6 #include "content/public/browser/pepper_vpn_provider_service_helper.h" |
| 7 |
| 8 #include "base/memory/singleton.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/render_process_host.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 VpnMessageFilterDelegate::~VpnMessageFilterDelegate() {} |
| 15 |
| 16 VpnServiceDelegate::~VpnServiceDelegate() {} |
| 17 |
| 18 VpnProviderServiceHelper::VpnProviderServiceHelper() : weak_factory_(this) {} |
| 19 |
| 20 VpnProviderServiceHelper::~VpnProviderServiceHelper() {} |
| 21 |
| 22 // static |
| 23 VpnProviderServiceHelper* VpnProviderServiceHelper::GetInstance() { |
| 24 return base::Singleton<VpnProviderServiceHelper>::get(); |
| 25 } |
| 26 |
| 27 void VpnProviderServiceHelper::OnPacketReceivedIO( |
| 28 const std::string& extension_id, |
| 29 const std::vector<char>& data) { |
| 30 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 31 |
| 32 if (extension_to_host_map_.find(extension_id) == |
| 33 extension_to_host_map_.end()) { |
| 34 NOTREACHED(); |
| 35 return; |
| 36 } |
| 37 |
| 38 extension_to_host_map_[extension_id]->SendOnPacketReceived(data); |
| 39 } |
| 40 |
| 41 void VpnProviderServiceHelper::OnUnbindIO(const std::string& extension_id) { |
| 42 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 43 |
| 44 if (extension_to_host_map_.find(extension_id) == |
| 45 extension_to_host_map_.end()) { |
| 46 NOTREACHED(); |
| 47 return; |
| 48 } |
| 49 |
| 50 extension_to_host_map_[extension_id]->SendOnUnbind(); |
| 51 } |
| 52 |
| 53 void VpnProviderServiceHelper::RegisterDelegate( |
| 54 const std::string& extension_id, |
| 55 std::unique_ptr<VpnMessageFilterDelegate> delegate) { |
| 56 extension_to_host_map_[extension_id] = std::move(delegate); |
| 57 } |
| 58 |
| 59 void VpnProviderServiceHelper::RegisterDelegate( |
| 60 content::BrowserContext* context, |
| 61 std::unique_ptr<VpnServiceDelegate> delegate) { |
| 62 context_to_service_map_[context] = std::move(delegate); |
| 63 } |
| 64 |
| 65 void VpnProviderServiceHelper::Bind(const std::string& extension_id, |
| 66 const std::string& configuration_id, |
| 67 const std::string& configuration_name, |
| 68 int render_process_id, |
| 69 const SuccessCallback& success, |
| 70 const FailureCallback& failure) { |
| 71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 72 |
| 73 content::RenderProcessHost* render_process_host = |
| 74 content::RenderProcessHost::FromID(render_process_id); |
| 75 if (render_process_host == NULL) { |
| 76 NOTREACHED(); |
| 77 return; |
| 78 } |
| 79 content::BrowserContext* context = render_process_host->GetBrowserContext(); |
| 80 if (context == NULL) { |
| 81 NOTREACHED(); |
| 82 return; |
| 83 } |
| 84 if (context_to_service_map_.find(context) == context_to_service_map_.end()) { |
| 85 NOTREACHED(); |
| 86 return; |
| 87 } |
| 88 if (extension_to_host_map_.find(extension_id) == |
| 89 extension_to_host_map_.end()) { |
| 90 NOTREACHED(); |
| 91 return; |
| 92 } |
| 93 |
| 94 context_to_service_map_[context]->Bind(extension_id, configuration_id, |
| 95 configuration_name, success, failure); |
| 96 } |
| 97 |
| 98 void VpnProviderServiceHelper::OnUnbind(const std::string& extension_id) { |
| 99 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 100 |
| 101 content::BrowserThread::PostTask( |
| 102 content::BrowserThread::IO, FROM_HERE, |
| 103 base::Bind(&VpnProviderServiceHelper::OnUnbindIO, |
| 104 weak_factory_.GetWeakPtr(), extension_id)); |
| 105 } |
| 106 |
| 107 void VpnProviderServiceHelper::SendPacket(const std::string& extension_id, |
| 108 const std::vector<char>& data, |
| 109 int render_process_id, |
| 110 SuccessCallback success, |
| 111 FailureCallback failure) { |
| 112 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 113 |
| 114 content::RenderProcessHost* render_process_host = |
| 115 content::RenderProcessHost::FromID(render_process_id); |
| 116 if (render_process_host == NULL) { |
| 117 NOTREACHED(); |
| 118 return; |
| 119 } |
| 120 |
| 121 content::BrowserContext* context = render_process_host->GetBrowserContext(); |
| 122 if (context == NULL) { |
| 123 NOTREACHED(); |
| 124 return; |
| 125 } |
| 126 |
| 127 if (context_to_service_map_.find(context) == context_to_service_map_.end()) { |
| 128 NOTREACHED(); |
| 129 return; |
| 130 } |
| 131 |
| 132 context_to_service_map_[context]->SendPacket(extension_id, data, success, |
| 133 failure); |
| 134 } |
| 135 |
| 136 void VpnProviderServiceHelper::OnPacketReceived(const std::string& extension_id, |
| 137 const std::vector<char>& data) { |
| 138 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 139 // TODO(adrian.belgun): |
| 140 // Possile optimization: Handle packets only on UI thread (3) |
| 141 content::BrowserThread::PostTask( |
| 142 content::BrowserThread::IO, FROM_HERE, |
| 143 base::Bind(&VpnProviderServiceHelper::OnPacketReceivedIO, |
| 144 weak_factory_.GetWeakPtr(), extension_id, data)); |
| 145 } |
| 146 |
| 147 } // namespace content |
OLD | NEW |