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