Chromium Code Reviews| 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 "content/browser/renderer_host/pepper/browser_ppapi_host_impl.h" | |
| 8 #include "content/browser/renderer_host/pepper/pepper_socket_utils.h" | |
| 9 #include "content/public/browser/browser_thread.h" | |
| 10 #include "content/public/common/socket_permission_request.h" | |
| 11 #include "ppapi/proxy/ppapi_messages.h" | |
| 12 #include "ppapi/shared_impl/private/net_address_private_impl.h" | |
| 13 | |
| 14 namespace content { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 bool CanUseNetworkMonitor(bool external_plugin, | |
| 19 int render_process_id, | |
| 20 int render_view_id) { | |
| 21 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 22 | |
| 23 SocketPermissionRequest request = | |
| 24 SocketPermissionRequest(SocketPermissionRequest::NETWORK_STATE, | |
| 25 std::string(), 0); | |
| 26 return pepper_socket_utils::CanUseSocketAPIs( | |
| 27 external_plugin, true /* private_api */, | |
|
yzshen1
2013/09/11 22:51:26
wrong indent.
Sergey Ulanov
2013/09/12 07:01:51
Done.
| |
| 28 request, render_process_id, render_view_id); | |
| 29 } | |
| 30 | |
| 31 scoped_ptr<net::NetworkInterfaceList> GetNetworkList() { | |
| 32 scoped_ptr<net::NetworkInterfaceList> list(new net::NetworkInterfaceList()); | |
| 33 net::GetNetworkList(list.get()); | |
| 34 return list.Pass(); | |
| 35 } | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 PepperNetworkMonitorHost::PepperNetworkMonitorHost( | |
| 40 BrowserPpapiHostImpl* host, | |
| 41 PP_Instance instance, | |
| 42 PP_Resource resource, | |
| 43 const ppapi::host::HostMessageContext& context) | |
| 44 : ResourceHost(host->GetPpapiHost(), instance, resource), | |
| 45 reply_context_(context.MakeReplyMessageContext()), | |
|
yzshen1
2013/09/11 22:51:26
This makes a "reply" for the Create message, which
Sergey Ulanov
2013/09/12 07:01:51
Done.
| |
| 46 weak_factory_(this) { | |
| 47 int render_process_id; | |
| 48 int render_view_id; | |
| 49 host->GetRenderViewIDsForInstance(instance, | |
| 50 &render_process_id, | |
| 51 &render_view_id); | |
| 52 | |
| 53 BrowserThread::PostTaskAndReplyWithResult( | |
| 54 BrowserThread::UI, FROM_HERE, | |
| 55 base::Bind(&CanUseNetworkMonitor, host->external_plugin(), | |
| 56 render_process_id, render_view_id), | |
| 57 base::Bind(&PepperNetworkMonitorHost::OnPermissionCheckResult, | |
| 58 weak_factory_.GetWeakPtr())); | |
| 59 } | |
| 60 | |
| 61 PepperNetworkMonitorHost::~PepperNetworkMonitorHost() { | |
| 62 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | |
| 63 } | |
| 64 | |
| 65 void PepperNetworkMonitorHost::OnIPAddressChanged() { | |
| 66 GetAndSendNetworkList(); | |
| 67 } | |
| 68 | |
| 69 void PepperNetworkMonitorHost::OnPermissionCheckResult( | |
| 70 bool can_use_network_monitor) { | |
| 71 if (!can_use_network_monitor) { | |
| 72 host()->SendReply(reply_context_, PpapiMsg_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::NetworkList> list_copy( | |
| 96 new ::ppapi::NetworkList(list->size())); | |
| 97 for (size_t i = 0; i < list->size(); ++i) { | |
| 98 const net::NetworkInterface& network = list->at(i); | |
| 99 ::ppapi::NetworkInfo& 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()->SendReply(reply_context_, | |
| 117 PpapiMsg_NetworkMonitor_NetworkList(*list_copy)); | |
| 118 } | |
| 119 | |
| 120 } // namespace content | |
| OLD | NEW |