Skip to content

Commit 934b847

Browse files
author
Tianjie
committed
Update UE to remove MessageLoop::current()->WatchFileDescriptor.
MessageLoop::current()->WatchFileDescriptor is deprecated. And UE should remove usages of it. Test: mma && unittest Change-Id: Ib1ef2e6b6a38ad2a8d07b78bcd72fdb3b7f82226
1 parent ebd5e25 commit 934b847

8 files changed

+45
-182
lines changed

common/subprocess.cc

-19
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,7 @@ void Subprocess::OnStdoutReady(SubprocessRecord* record) {
129129
if (!ok || eof) {
130130
// There was either an error or an EOF condition, so we are done watching
131131
// the file descriptor.
132-
#ifdef __ANDROID__
133-
MessageLoop::current()->CancelTask(record->stdout_task_id);
134-
record->stdout_task_id = MessageLoop::kTaskIdNull;
135-
#else
136132
record->stdout_controller.reset();
137-
#endif // __ANDROID__
138133
return;
139134
}
140135
} while (bytes_read);
@@ -149,12 +144,7 @@ void Subprocess::ChildExitedCallback(const siginfo_t& info) {
149144
// Make sure we read any remaining process output and then close the pipe.
150145
OnStdoutReady(record);
151146

152-
#ifdef __ANDROID__
153-
MessageLoop::current()->CancelTask(record->stdout_task_id);
154-
record->stdout_task_id = MessageLoop::kTaskIdNull;
155-
#else
156147
record->stdout_controller.reset();
157-
#endif // __ANDROID__
158148

159149
// Don't print any log if the subprocess exited with exit code 0.
160150
if (info.si_code != CLD_EXITED) {
@@ -209,18 +199,9 @@ pid_t Subprocess::ExecFlags(const vector<string>& cmd,
209199
<< record->stdout_fd << ".";
210200
}
211201

212-
#ifdef __ANDROID__
213-
record->stdout_task_id = MessageLoop::current()->WatchFileDescriptor(
214-
FROM_HERE,
215-
record->stdout_fd,
216-
MessageLoop::WatchMode::kWatchRead,
217-
true,
218-
base::Bind(&Subprocess::OnStdoutReady, record.get()));
219-
#else
220202
record->stdout_controller = base::FileDescriptorWatcher::WatchReadable(
221203
record->stdout_fd,
222204
base::BindRepeating(&Subprocess::OnStdoutReady, record.get()));
223-
#endif // __ANDROID__
224205

225206
subprocess_records_[pid] = std::move(record);
226207
return pid;

common/subprocess.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,8 @@ class Subprocess {
123123

124124
// These are used to monitor the stdout of the running process, including
125125
// the stderr if it was redirected.
126-
#ifdef __ANDROID__
127-
brillo::MessageLoop::TaskId stdout_task_id{
128-
brillo::MessageLoop::kTaskIdNull};
129-
#else
130126
std::unique_ptr<base::FileDescriptorWatcher::Controller> stdout_controller;
131-
#endif // __ANDROID__
127+
132128
int stdout_fd{-1};
133129
std::string stdout;
134130
};

common/subprocess_unittest.cc

-20
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ class SubprocessTest : public ::testing::Test {
7474
brillo::BaseMessageLoop loop_{&base_loop_};
7575
brillo::AsynchronousSignalHandler async_signal_handler_;
7676
Subprocess subprocess_;
77-
#ifndef __ANDROID__
7877
unique_ptr<base::FileDescriptorWatcher::Controller> watcher_;
79-
#endif // __ANDROID__
8078

8179
};
8280

@@ -261,23 +259,6 @@ TEST_F(SubprocessTest, CancelTest) {
261259
int fifo_fd = HANDLE_EINTR(open(fifo_path.c_str(), O_RDONLY));
262260
EXPECT_GE(fifo_fd, 0);
263261

264-
#ifdef __ANDROID__
265-
loop_.WatchFileDescriptor(FROM_HERE,
266-
fifo_fd,
267-
MessageLoop::WatchMode::kWatchRead,
268-
false,
269-
base::Bind(
270-
[](int fifo_fd, uint32_t tag) {
271-
char c;
272-
EXPECT_EQ(1,
273-
HANDLE_EINTR(read(fifo_fd, &c, 1)));
274-
EXPECT_EQ('X', c);
275-
LOG(INFO) << "Killing tag " << tag;
276-
Subprocess::Get().KillExec(tag);
277-
},
278-
fifo_fd,
279-
tag));
280-
#else
281262
watcher_ = base::FileDescriptorWatcher::WatchReadable(
282263
fifo_fd,
283264
base::Bind(
@@ -295,7 +276,6 @@ TEST_F(SubprocessTest, CancelTest) {
295276
base::Unretained(&watcher_),
296277
fifo_fd,
297278
tag));
298-
#endif // __ANDROID__
299279

300280
// This test would leak a callback that runs when the child process exits
301281
// unless we wait for it to run.

libcurl_http_fetcher.cc

+15-85
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include <base/strings/string_split.h>
3232
#include <base/strings/string_util.h>
3333
#include <base/strings/stringprintf.h>
34+
#include <base/threading/thread_task_runner_handle.h>
35+
3436

3537
#ifdef __ANDROID__
3638
#include <cutils/qtaguid.h>
@@ -81,23 +83,9 @@ int LibcurlHttpFetcher::LibcurlCloseSocketCallback(void* clientp,
8183

8284
LibcurlHttpFetcher* fetcher = static_cast<LibcurlHttpFetcher*>(clientp);
8385
// Stop watching the socket before closing it.
84-
#ifdef __ANDROID__
85-
for (size_t t = 0; t < arraysize(fetcher->fd_task_maps_); ++t) {
86-
const auto fd_task_pair = fetcher->fd_task_maps_[t].find(item);
87-
if (fd_task_pair != fetcher->fd_task_maps_[t].end()) {
88-
if (!MessageLoop::current()->CancelTask(fd_task_pair->second)) {
89-
LOG(WARNING) << "Error canceling the watch task "
90-
<< fd_task_pair->second << " for "
91-
<< (t ? "writing" : "reading") << " the fd " << item;
92-
}
93-
fetcher->fd_task_maps_[t].erase(item);
94-
}
95-
}
96-
#else
9786
for (size_t t = 0; t < base::size(fetcher->fd_controller_maps_); ++t) {
9887
fetcher->fd_controller_maps_[t].erase(item);
9988
}
100-
#endif // __ANDROID__
10189

10290
// Documentation for this callback says to return 0 on success or 1 on error.
10391
if (!IGNORE_EINTR(close(item)))
@@ -471,6 +459,19 @@ void LibcurlHttpFetcher::CurlPerformOnce() {
471459
// There's either more work to do or we are paused, so we just keep the
472460
// file descriptors to watch up to date and exit, until we are done with the
473461
// work and we are not paused.
462+
#ifdef __ANDROID__
463+
// When there's no base::SingleThreadTaskRunner on current thread, it's not
464+
// possible to watch file descriptors. Just poll it later. This usually
465+
// happens if brillo::FakeMessageLoop is used.
466+
if (!base::ThreadTaskRunnerHandle::IsSet()) {
467+
MessageLoop::current()->PostDelayedTask(
468+
FROM_HERE,
469+
base::Bind(&LibcurlHttpFetcher::CurlPerformOnce,
470+
base::Unretained(this)),
471+
TimeDelta::FromSeconds(1));
472+
return;
473+
}
474+
#endif
474475
SetupMessageLoopSources();
475476
return;
476477
}
@@ -691,63 +692,6 @@ void LibcurlHttpFetcher::SetupMessageLoopSources() {
691692

692693
// We should iterate through all file descriptors up to libcurl's fd_max or
693694
// the highest one we're tracking, whichever is larger.
694-
#ifdef __ANDROID__
695-
for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) {
696-
if (!fd_task_maps_[t].empty())
697-
fd_max = max(fd_max, fd_task_maps_[t].rbegin()->first);
698-
}
699-
700-
// For each fd, if we're not tracking it, track it. If we are tracking it, but
701-
// libcurl doesn't care about it anymore, stop tracking it. After this loop,
702-
// there should be exactly as many tasks scheduled in fd_task_maps_[0|1] as
703-
// there are read/write fds that we're tracking.
704-
for (int fd = 0; fd <= fd_max; ++fd) {
705-
// Note that fd_exc is unused in the current version of libcurl so is_exc
706-
// should always be false.
707-
bool is_exc = FD_ISSET(fd, &fd_exc) != 0;
708-
bool must_track[2] = {
709-
is_exc || (FD_ISSET(fd, &fd_read) != 0), // track 0 -- read
710-
is_exc || (FD_ISSET(fd, &fd_write) != 0) // track 1 -- write
711-
};
712-
MessageLoop::WatchMode watch_modes[2] = {
713-
MessageLoop::WatchMode::kWatchRead,
714-
MessageLoop::WatchMode::kWatchWrite,
715-
};
716-
717-
for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) {
718-
auto fd_task_it = fd_task_maps_[t].find(fd);
719-
bool tracked = fd_task_it != fd_task_maps_[t].end();
720-
721-
if (!must_track[t]) {
722-
// If we have an outstanding io_channel, remove it.
723-
if (tracked) {
724-
MessageLoop::current()->CancelTask(fd_task_it->second);
725-
fd_task_maps_[t].erase(fd_task_it);
726-
}
727-
continue;
728-
}
729-
730-
// If we are already tracking this fd, continue -- nothing to do.
731-
if (tracked)
732-
continue;
733-
734-
// Track a new fd.
735-
fd_task_maps_[t][fd] = MessageLoop::current()->WatchFileDescriptor(
736-
FROM_HERE,
737-
fd,
738-
watch_modes[t],
739-
true, // persistent
740-
base::Bind(&LibcurlHttpFetcher::CurlPerformOnce,
741-
base::Unretained(this)));
742-
743-
static int io_counter = 0;
744-
io_counter++;
745-
if (io_counter % 50 == 0) {
746-
LOG(INFO) << "io_counter = " << io_counter;
747-
}
748-
}
749-
}
750-
#else
751695
for (size_t t = 0; t < base::size(fd_controller_maps_); ++t) {
752696
if (!fd_controller_maps_[t].empty())
753697
fd_max = max(fd_max, fd_controller_maps_[t].rbegin()->first);
@@ -803,7 +747,6 @@ void LibcurlHttpFetcher::SetupMessageLoopSources() {
803747
}
804748
}
805749
}
806-
#endif // __ANDROID__
807750

808751
// Set up a timeout callback for libcurl.
809752
if (timeout_id_ == MessageLoop::kTaskIdNull) {
@@ -848,22 +791,9 @@ void LibcurlHttpFetcher::CleanUp() {
848791
MessageLoop::current()->CancelTask(timeout_id_);
849792
timeout_id_ = MessageLoop::kTaskIdNull;
850793

851-
#ifdef __ANDROID__
852-
for (size_t t = 0; t < arraysize(fd_task_maps_); ++t) {
853-
for (const auto& fd_taks_pair : fd_task_maps_[t]) {
854-
if (!MessageLoop::current()->CancelTask(fd_taks_pair.second)) {
855-
LOG(WARNING) << "Error canceling the watch task " << fd_taks_pair.second
856-
<< " for " << (t ? "writing" : "reading") << " the fd "
857-
<< fd_taks_pair.first;
858-
}
859-
}
860-
fd_task_maps_[t].clear();
861-
}
862-
#else
863794
for (size_t t = 0; t < base::size(fd_controller_maps_); ++t) {
864795
fd_controller_maps_[t].clear();
865796
}
866-
#endif // __ANDROID__
867797

868798
if (curl_http_headers_) {
869799
curl_slist_free_all(curl_http_headers_);

libcurl_http_fetcher.h

-4
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,8 @@ class LibcurlHttpFetcher : public HttpFetcher {
255255
// the message loop. libcurl may open/close descriptors and switch their
256256
// directions so maintain two separate lists so that watch conditions can be
257257
// set appropriately.
258-
#ifdef __ANDROID__
259-
std::map<int, brillo::MessageLoop::TaskId> fd_task_maps_[2];
260-
#else
261258
std::map<int, std::unique_ptr<base::FileDescriptorWatcher::Controller>>
262259
fd_controller_maps_[2];
263-
#endif // __ANDROID__
264260

265261
// The TaskId of the timer we're waiting on. kTaskIdNull if we are not waiting
266262
// on it.

libcurl_http_fetcher_unittest.cc

+29-23
Original file line numberDiff line numberDiff line change
@@ -94,37 +94,24 @@ TEST_F(LibcurlHttpFetcherTest, InvalidURLTest) {
9494
no_network_max_retries);
9595
}
9696

97-
#ifdef __ANDROID__
98-
TEST_F(LibcurlHttpFetcherTest, CouldntResolveHostTest) {
97+
TEST_F(LibcurlHttpFetcherTest, CouldNotResolveHostTest) {
9998
int no_network_max_retries = 1;
10099
libcurl_fetcher_.set_no_network_max_retries(no_network_max_retries);
101100

102-
// This test actually sends request to internet but according to
103-
// https://tools.ietf.org/html/rfc2606#section-2, .invalid domain names are
104-
// reserved and sure to be invalid. Ideally we should mock libcurl or
105-
// reorganize LibcurlHttpFetcher so the part that sends request can be mocked
106-
// easily.
107-
// TODO(xiaochu) Refactor LibcurlHttpFetcher (and its relates) so it's
108-
// easier to mock the part that depends on internet connectivity.
109101
libcurl_fetcher_.BeginTransfer("https://An-uNres0lvable-uRl.invalid");
110-
while (loop_.PendingTasks()) {
102+
103+
#ifdef __ANDROID__
104+
// It's slower on Android that libcurl handle may not finish within 1 cycle.
105+
// Will need to wait for more cycles until it finishes. Original test didn't
106+
// correctly handle when we need to re-watch libcurl fds.
107+
while (loop_.PendingTasks() &&
108+
libcurl_fetcher_.GetAuxiliaryErrorCode() == ErrorCode::kSuccess) {
111109
loop_.RunOnce(true);
112110
}
113-
114-
// If libcurl fails to resolve the name, we call res_init() to reload
115-
// resolv.conf and retry exactly once more. See crbug.com/982813 for details.
116-
EXPECT_EQ(libcurl_fetcher_.get_no_network_max_retries(),
117-
no_network_max_retries + 1);
118-
}
119111
#else
120-
TEST_F(LibcurlHttpFetcherTest, CouldNotResolveHostTest) {
121-
int no_network_max_retries = 1;
122-
libcurl_fetcher_.set_no_network_max_retries(no_network_max_retries);
123-
124-
libcurl_fetcher_.BeginTransfer("https://An-uNres0lvable-uRl.invalid");
125-
126112
// The first time it can't resolve.
127113
loop_.RunOnce(true);
114+
#endif
128115
EXPECT_EQ(libcurl_fetcher_.GetAuxiliaryErrorCode(),
129116
ErrorCode::kUnresolvedHostError);
130117

@@ -154,8 +141,18 @@ TEST_F(LibcurlHttpFetcherTest, HostResolvedTest) {
154141
// easier to mock the part that depends on internet connectivity.
155142
libcurl_fetcher_.BeginTransfer("https://An-uNres0lvable-uRl.invalid");
156143

144+
#ifdef __ANDROID__
145+
// It's slower on Android that libcurl handle may not finish within 1 cycle.
146+
// Will need to wait for more cycles until it finishes. Original test didn't
147+
// correctly handle when we need to re-watch libcurl fds.
148+
while (loop_.PendingTasks() &&
149+
libcurl_fetcher_.GetAuxiliaryErrorCode() == ErrorCode::kSuccess) {
150+
loop_.RunOnce(true);
151+
}
152+
#else
157153
// The first time it can't resolve.
158154
loop_.RunOnce(true);
155+
#endif
159156
EXPECT_EQ(libcurl_fetcher_.GetAuxiliaryErrorCode(),
160157
ErrorCode::kUnresolvedHostError);
161158

@@ -168,9 +165,19 @@ TEST_F(LibcurlHttpFetcherTest, HostResolvedTest) {
168165
[this]() { libcurl_fetcher_.http_response_code_ = 0; }));
169166
libcurl_fetcher_.transfer_size_ = 10;
170167

168+
#ifdef __ANDROID__
169+
// It's slower on Android that libcurl handle may not finish within 1 cycle.
170+
// Will need to wait for more cycles until it finishes. Original test didn't
171+
// correctly handle when we need to re-watch libcurl fds.
172+
while (loop_.PendingTasks() && libcurl_fetcher_.GetAuxiliaryErrorCode() ==
173+
ErrorCode::kUnresolvedHostError) {
174+
loop_.RunOnce(true);
175+
}
176+
#else
171177
// This time the host is resolved. But after that again we can't resolve
172178
// anymore (See above).
173179
loop_.RunOnce(true);
180+
#endif
174181
EXPECT_EQ(libcurl_fetcher_.GetAuxiliaryErrorCode(),
175182
ErrorCode::kUnresolvedHostRecovered);
176183

@@ -186,7 +193,6 @@ TEST_F(LibcurlHttpFetcherTest, HostResolvedTest) {
186193
EXPECT_EQ(libcurl_fetcher_.get_no_network_max_retries(),
187194
no_network_max_retries + 1);
188195
}
189-
#endif // __ANDROID__
190196

191197
TEST_F(LibcurlHttpFetcherTest, HttpFetcherStateMachineRetryFailedTest) {
192198
state_machine_.UpdateState(true);

0 commit comments

Comments
 (0)