|
31 | 31 | #include <base/strings/string_split.h>
|
32 | 32 | #include <base/strings/string_util.h>
|
33 | 33 | #include <base/strings/stringprintf.h>
|
| 34 | +#include <base/threading/thread_task_runner_handle.h> |
| 35 | + |
34 | 36 |
|
35 | 37 | #ifdef __ANDROID__
|
36 | 38 | #include <cutils/qtaguid.h>
|
@@ -81,23 +83,9 @@ int LibcurlHttpFetcher::LibcurlCloseSocketCallback(void* clientp,
|
81 | 83 |
|
82 | 84 | LibcurlHttpFetcher* fetcher = static_cast<LibcurlHttpFetcher*>(clientp);
|
83 | 85 | // 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 |
97 | 86 | for (size_t t = 0; t < base::size(fetcher->fd_controller_maps_); ++t) {
|
98 | 87 | fetcher->fd_controller_maps_[t].erase(item);
|
99 | 88 | }
|
100 |
| -#endif // __ANDROID__ |
101 | 89 |
|
102 | 90 | // Documentation for this callback says to return 0 on success or 1 on error.
|
103 | 91 | if (!IGNORE_EINTR(close(item)))
|
@@ -471,6 +459,19 @@ void LibcurlHttpFetcher::CurlPerformOnce() {
|
471 | 459 | // There's either more work to do or we are paused, so we just keep the
|
472 | 460 | // file descriptors to watch up to date and exit, until we are done with the
|
473 | 461 | // 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 |
474 | 475 | SetupMessageLoopSources();
|
475 | 476 | return;
|
476 | 477 | }
|
@@ -691,63 +692,6 @@ void LibcurlHttpFetcher::SetupMessageLoopSources() {
|
691 | 692 |
|
692 | 693 | // We should iterate through all file descriptors up to libcurl's fd_max or
|
693 | 694 | // 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 |
751 | 695 | for (size_t t = 0; t < base::size(fd_controller_maps_); ++t) {
|
752 | 696 | if (!fd_controller_maps_[t].empty())
|
753 | 697 | fd_max = max(fd_max, fd_controller_maps_[t].rbegin()->first);
|
@@ -803,7 +747,6 @@ void LibcurlHttpFetcher::SetupMessageLoopSources() {
|
803 | 747 | }
|
804 | 748 | }
|
805 | 749 | }
|
806 |
| -#endif // __ANDROID__ |
807 | 750 |
|
808 | 751 | // Set up a timeout callback for libcurl.
|
809 | 752 | if (timeout_id_ == MessageLoop::kTaskIdNull) {
|
@@ -848,22 +791,9 @@ void LibcurlHttpFetcher::CleanUp() {
|
848 | 791 | MessageLoop::current()->CancelTask(timeout_id_);
|
849 | 792 | timeout_id_ = MessageLoop::kTaskIdNull;
|
850 | 793 |
|
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 |
863 | 794 | for (size_t t = 0; t < base::size(fd_controller_maps_); ++t) {
|
864 | 795 | fd_controller_maps_[t].clear();
|
865 | 796 | }
|
866 |
| -#endif // __ANDROID__ |
867 | 797 |
|
868 | 798 | if (curl_http_headers_) {
|
869 | 799 | curl_slist_free_all(curl_http_headers_);
|
|
0 commit comments