OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 "content/browser/renderer_host/pepper/pepper_network_monitor_host.h" |
| 6 |
| 7 #include "base/task_runner_util.h" |
| 8 #include "base/threading/sequenced_worker_pool.h" |
| 9 #include "content/browser/renderer_host/pepper/browser_ppapi_host_impl.h" |
| 10 #include "content/browser/renderer_host/pepper/pepper_socket_utils.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/common/socket_permission_request.h" |
| 13 #include "ppapi/proxy/ppapi_messages.h" |
| 14 #include "ppapi/shared_impl/private/net_address_private_impl.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 namespace { |
| 19 |
| 20 bool CanUseNetworkMonitor(bool external_plugin, |
| 21 int render_process_id, |
| 22 int render_view_id) { |
| 23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 24 |
| 25 SocketPermissionRequest request = SocketPermissionRequest( |
| 26 SocketPermissionRequest::NETWORK_STATE, std::string(), 0); |
| 27 return pepper_socket_utils::CanUseSocketAPIs( |
| 28 external_plugin, true /* private_api */, request, render_process_id, |
| 29 render_view_id); |
| 30 } |
| 31 |
| 32 scoped_ptr<net::NetworkInterfaceList> GetNetworkList() { |
| 33 scoped_ptr<net::NetworkInterfaceList> list(new net::NetworkInterfaceList()); |
| 34 net::GetNetworkList(list.get()); |
| 35 return list.Pass(); |
| 36 } |
| 37 |
| 38 } // namespace |
| 39 |
| 40 PepperNetworkMonitorHost::PepperNetworkMonitorHost( |
| 41 BrowserPpapiHostImpl* host, |
| 42 PP_Instance instance, |
| 43 PP_Resource resource) |
| 44 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 45 weak_factory_(this) { |
| 46 int render_process_id; |
| 47 int render_view_id; |
| 48 host->GetRenderViewIDsForInstance(instance, |
| 49 &render_process_id, |
| 50 &render_view_id); |
| 51 |
| 52 BrowserThread::PostTaskAndReplyWithResult( |
| 53 BrowserThread::UI, FROM_HERE, |
| 54 base::Bind(&CanUseNetworkMonitor, host->external_plugin(), |
| 55 render_process_id, render_view_id), |
| 56 base::Bind(&PepperNetworkMonitorHost::OnPermissionCheckResult, |
| 57 weak_factory_.GetWeakPtr())); |
| 58 } |
| 59 |
| 60 PepperNetworkMonitorHost::~PepperNetworkMonitorHost() { |
| 61 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 62 } |
| 63 |
| 64 void PepperNetworkMonitorHost::OnIPAddressChanged() { |
| 65 GetAndSendNetworkList(); |
| 66 } |
| 67 |
| 68 void PepperNetworkMonitorHost::OnPermissionCheckResult( |
| 69 bool can_use_network_monitor) { |
| 70 if (!can_use_network_monitor) { |
| 71 host()->SendUnsolicitedReply(pp_resource(), |
| 72 PpapiPluginMsg_NetworkMonitor_Forbidden()); |
| 73 return; |
| 74 } |
| 75 |
| 76 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
| 77 GetAndSendNetworkList(); |
| 78 } |
| 79 |
| 80 void PepperNetworkMonitorHost::GetAndSendNetworkList() { |
| 81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 82 |
| 83 // Call GetNetworkList() on a thread that allows blocking IO. |
| 84 base::PostTaskAndReplyWithResult( |
| 85 BrowserThread::GetBlockingPool(), FROM_HERE, |
| 86 base::Bind(&GetNetworkList), |
| 87 base::Bind(&PepperNetworkMonitorHost::SendNetworkList, |
| 88 weak_factory_.GetWeakPtr())); |
| 89 } |
| 90 |
| 91 void PepperNetworkMonitorHost::SendNetworkList( |
| 92 scoped_ptr<net::NetworkInterfaceList> list) { |
| 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 94 |
| 95 scoped_ptr<ppapi::proxy::SerializedNetworkList> list_copy( |
| 96 new ppapi::proxy::SerializedNetworkList(list->size())); |
| 97 for (size_t i = 0; i < list->size(); ++i) { |
| 98 const net::NetworkInterface& network = list->at(i); |
| 99 ppapi::proxy::SerializedNetworkInfo& network_copy = list_copy->at(i); |
| 100 network_copy.name = network.name; |
| 101 |
| 102 network_copy.addresses.resize( |
| 103 1, ppapi::NetAddressPrivateImpl::kInvalidNetAddress); |
| 104 bool result = ppapi::NetAddressPrivateImpl::IPEndPointToNetAddress( |
| 105 network.address, 0, &(network_copy.addresses[0])); |
| 106 DCHECK(result); |
| 107 |
| 108 // TODO(sergeyu): Currently net::NetworkInterfaceList provides |
| 109 // only name and one IP address. Add all other fields and copy |
| 110 // them here. |
| 111 network_copy.type = PP_NETWORKLIST_UNKNOWN; |
| 112 network_copy.state = PP_NETWORKLIST_UP; |
| 113 network_copy.display_name = network.name; |
| 114 network_copy.mtu = 0; |
| 115 } |
| 116 host()->SendUnsolicitedReply( |
| 117 pp_resource(), PpapiPluginMsg_NetworkMonitor_NetworkList(*list_copy)); |
| 118 } |
| 119 |
| 120 } // namespace content |
OLD | NEW |