Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(982)

Side by Side Diff: extensions/browser/api/system_network/system_network_api.cc

Issue 2975153002: Convert FILE thread usage in system_network_api.cc to TaskScheduler. (Closed)
Patch Set: GetListOnBlockingTaskRunner Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/browser/api/system_network/system_network_api.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/browser/api/system_network/system_network_api.h" 5 #include "extensions/browser/api/system_network/system_network_api.h"
6 6
7 #include "base/task_scheduler/post_task.h"
8
7 namespace { 9 namespace {
8 const char kNetworkListError[] = "Network lookup failed or unsupported"; 10 const char kNetworkListError[] = "Network lookup failed or unsupported";
11
12 std::unique_ptr<net::NetworkInterfaceList> GetListOnBlockingTaskRunner() {
13 auto interface_list = base::MakeUnique<net::NetworkInterfaceList>();
14 if (net::GetNetworkList(interface_list.get(),
15 net::INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)) {
16 return interface_list;
17 }
18
19 return nullptr;
20 }
21
9 } // namespace 22 } // namespace
10 23
11 namespace extensions { 24 namespace extensions {
12 namespace api { 25 namespace api {
13 26
14 SystemNetworkGetNetworkInterfacesFunction:: 27 SystemNetworkGetNetworkInterfacesFunction::
15 SystemNetworkGetNetworkInterfacesFunction() { 28 SystemNetworkGetNetworkInterfacesFunction() {
16 } 29 }
17 30
18 SystemNetworkGetNetworkInterfacesFunction:: 31 SystemNetworkGetNetworkInterfacesFunction::
19 ~SystemNetworkGetNetworkInterfacesFunction() { 32 ~SystemNetworkGetNetworkInterfacesFunction() {
20 } 33 }
21 34
22 ExtensionFunction::ResponseAction 35 ExtensionFunction::ResponseAction
23 SystemNetworkGetNetworkInterfacesFunction::Run() { 36 SystemNetworkGetNetworkInterfacesFunction::Run() {
24 content::BrowserThread::PostTask( 37 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
25 content::BrowserThread::FILE, 38 constexpr base::TaskTraits kTraits = {
26 FROM_HERE, 39 base::MayBlock(), base::TaskPriority::USER_VISIBLE,
27 base::Bind( 40 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN};
28 &SystemNetworkGetNetworkInterfacesFunction::GetListOnFileThread, 41 using Self = SystemNetworkGetNetworkInterfacesFunction;
29 this)); 42 base::PostTaskWithTraitsAndReplyWithResult(
43 FROM_HERE, kTraits, base::BindOnce(&GetListOnBlockingTaskRunner),
44 base::BindOnce(&Self::SendResponseOnUIThread, this));
30 return RespondLater(); 45 return RespondLater();
31 } 46 }
32 47
33 void SystemNetworkGetNetworkInterfacesFunction::GetListOnFileThread() { 48 void SystemNetworkGetNetworkInterfacesFunction::SendResponseOnUIThread(
34 net::NetworkInterfaceList interface_list; 49 std::unique_ptr<net::NetworkInterfaceList> interface_list) {
35 if (net::GetNetworkList(&interface_list, 50 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
36 net::INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)) { 51 if (!interface_list) {
37 content::BrowserThread::PostTask( 52 Respond(Error(kNetworkListError));
38 content::BrowserThread::UI,
39 FROM_HERE,
40 base::Bind(
41 &SystemNetworkGetNetworkInterfacesFunction::SendResponseOnUIThread,
42 this,
43 interface_list));
44 return; 53 return;
45 } 54 }
46 55
47 content::BrowserThread::PostTask(
48 content::BrowserThread::UI,
49 FROM_HERE,
50 base::Bind(&SystemNetworkGetNetworkInterfacesFunction::HandleGetListError,
51 this));
52 }
53
54 void SystemNetworkGetNetworkInterfacesFunction::HandleGetListError() {
55 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
56 Respond(Error(kNetworkListError));
57 }
58
59 void SystemNetworkGetNetworkInterfacesFunction::SendResponseOnUIThread(
60 const net::NetworkInterfaceList& interface_list) {
61 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
62
63 std::vector<api::system_network::NetworkInterface> create_arg; 56 std::vector<api::system_network::NetworkInterface> create_arg;
64 create_arg.reserve(interface_list.size()); 57 create_arg.reserve(interface_list->size());
65 for (const net::NetworkInterface& interface : interface_list) { 58 for (const net::NetworkInterface& interface : *interface_list) {
66 api::system_network::NetworkInterface info; 59 api::system_network::NetworkInterface info;
67 info.name = interface.name; 60 info.name = interface.name;
68 info.address = interface.address.ToString(); 61 info.address = interface.address.ToString();
69 info.prefix_length = interface.prefix_length; 62 info.prefix_length = interface.prefix_length;
70 create_arg.push_back(std::move(info)); 63 create_arg.push_back(std::move(info));
71 } 64 }
72 65
73 Respond(ArgumentList( 66 Respond(ArgumentList(
74 api::system_network::GetNetworkInterfaces::Results::Create(create_arg))); 67 api::system_network::GetNetworkInterfaces::Results::Create(create_arg)));
75 } 68 }
76 69
77 } // namespace api 70 } // namespace api
78 } // namespace extensions 71 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/system_network/system_network_api.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698