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.
5 #include "content/browser/renderer_host/pepper/pepper_network_monitor_host.h"
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"
21 bool CanUseNetworkMonitor(bool external_plugin
,
22 int render_process_id
,
23 int render_frame_id
) {
24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
26 SocketPermissionRequest request
= SocketPermissionRequest(
27 SocketPermissionRequest::NETWORK_STATE
, std::string(), 0);
28 return pepper_socket_utils::CanUseSocketAPIs(
29 external_plugin
, false /* private_api */, &request
, render_process_id
,
33 scoped_ptr
<net::NetworkInterfaceList
> GetNetworkList() {
34 scoped_ptr
<net::NetworkInterfaceList
> list(new net::NetworkInterfaceList());
35 net::GetNetworkList(list
.get(), net::INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES
);
41 PepperNetworkMonitorHost::PepperNetworkMonitorHost(
42 BrowserPpapiHostImpl
* host
,
45 : ResourceHost(host
->GetPpapiHost(), instance
, resource
),
47 int render_process_id
;
49 host
->GetRenderFrameIDsForInstance(instance
,
53 BrowserThread::PostTaskAndReplyWithResult(
54 BrowserThread::UI
, FROM_HERE
,
55 base::Bind(&CanUseNetworkMonitor
, host
->external_plugin(),
56 render_process_id
, render_frame_id
),
57 base::Bind(&PepperNetworkMonitorHost::OnPermissionCheckResult
,
58 weak_factory_
.GetWeakPtr()));
61 PepperNetworkMonitorHost::~PepperNetworkMonitorHost() {
62 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
65 void PepperNetworkMonitorHost::OnIPAddressChanged() {
66 GetAndSendNetworkList();
69 void PepperNetworkMonitorHost::OnPermissionCheckResult(
70 bool can_use_network_monitor
) {
71 if (!can_use_network_monitor
) {
72 host()->SendUnsolicitedReply(pp_resource(),
73 PpapiPluginMsg_NetworkMonitor_Forbidden());
77 net::NetworkChangeNotifier::AddIPAddressObserver(this);
78 GetAndSendNetworkList();
81 void PepperNetworkMonitorHost::GetAndSendNetworkList() {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
84 // Call GetNetworkList() on a thread that allows blocking IO.
85 base::PostTaskAndReplyWithResult(
86 BrowserThread::GetBlockingPool(), FROM_HERE
,
87 base::Bind(&GetNetworkList
),
88 base::Bind(&PepperNetworkMonitorHost::SendNetworkList
,
89 weak_factory_
.GetWeakPtr()));
92 void PepperNetworkMonitorHost::SendNetworkList(
93 scoped_ptr
<net::NetworkInterfaceList
> list
) {
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
96 scoped_ptr
<ppapi::proxy::SerializedNetworkList
> list_copy(
97 new ppapi::proxy::SerializedNetworkList(list
->size()));
98 for (size_t i
= 0; i
< list
->size(); ++i
) {
99 const net::NetworkInterface
& network
= list
->at(i
);
100 ppapi::proxy::SerializedNetworkInfo
& network_copy
= list_copy
->at(i
);
101 network_copy
.name
= network
.name
;
103 network_copy
.addresses
.resize(
104 1, ppapi::NetAddressPrivateImpl::kInvalidNetAddress
);
105 bool result
= ppapi::NetAddressPrivateImpl::IPEndPointToNetAddress(
106 network
.address
, 0, &(network_copy
.addresses
[0]));
109 // TODO(sergeyu): Currently net::NetworkInterfaceList provides
110 // only name and one IP address. Add all other fields and copy
112 network_copy
.type
= PP_NETWORKLIST_TYPE_UNKNOWN
;
113 network_copy
.state
= PP_NETWORKLIST_STATE_UP
;
114 network_copy
.display_name
= network
.name
;
115 network_copy
.mtu
= 0;
117 host()->SendUnsolicitedReply(
118 pp_resource(), PpapiPluginMsg_NetworkMonitor_NetworkList(*list_copy
));
121 } // namespace content