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 "extensions/browser/api/system_network/system_network_api.h"
7 #include "net/base/ip_address_number.h"
10 const char kNetworkListError
[] = "Network lookup failed or unsupported";
13 namespace extensions
{
16 SystemNetworkGetNetworkInterfacesFunction::
17 SystemNetworkGetNetworkInterfacesFunction() {
20 SystemNetworkGetNetworkInterfacesFunction::
21 ~SystemNetworkGetNetworkInterfacesFunction() {
24 bool SystemNetworkGetNetworkInterfacesFunction::RunAsync() {
25 content::BrowserThread::PostTask(
26 content::BrowserThread::FILE,
29 &SystemNetworkGetNetworkInterfacesFunction::GetListOnFileThread
,
34 void SystemNetworkGetNetworkInterfacesFunction::GetListOnFileThread() {
35 net::NetworkInterfaceList interface_list
;
36 if (net::GetNetworkList(&interface_list
,
37 net::INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES
)) {
38 content::BrowserThread::PostTask(
39 content::BrowserThread::UI
,
42 &SystemNetworkGetNetworkInterfacesFunction::SendResponseOnUIThread
,
48 content::BrowserThread::PostTask(
49 content::BrowserThread::UI
,
51 base::Bind(&SystemNetworkGetNetworkInterfacesFunction::HandleGetListError
,
55 void SystemNetworkGetNetworkInterfacesFunction::HandleGetListError() {
56 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
57 error_
= kNetworkListError
;
61 void SystemNetworkGetNetworkInterfacesFunction::SendResponseOnUIThread(
62 const net::NetworkInterfaceList
& interface_list
) {
63 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
65 std::vector
<linked_ptr
<api::system_network::NetworkInterface
>> create_arg
;
66 create_arg
.reserve(interface_list
.size());
67 for (net::NetworkInterfaceList::const_iterator i
= interface_list
.begin();
68 i
!= interface_list
.end();
70 linked_ptr
<api::system_network::NetworkInterface
> info
=
71 make_linked_ptr(new api::system_network::NetworkInterface
);
73 info
->address
= net::IPAddressToString(i
->address
);
74 info
->prefix_length
= i
->prefix_length
;
75 create_arg
.push_back(info
);
79 api::system_network::GetNetworkInterfaces::Results::Create(create_arg
);
84 } // namespace extensions