Avi Drissman | ea1be23 | 2022-09-14 23:29:06 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Rohit Rao | ecad200 | 2018-11-16 19:42:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ios/web/web_sub_thread.h" |
| 6 | |
Rohit Rao | 752eeba7 | 2018-12-22 19:28:08 | [diff] [blame] | 7 | #include "base/debug/alias.h" |
Avi Drissman | cac43f2 | 2023-01-12 00:58:41 | [diff] [blame] | 8 | #include "base/functional/bind.h" |
Rohit Rao | ecad200 | 2018-11-16 19:42:05 | [diff] [blame] | 9 | #include "base/threading/thread_restrictions.h" |
Yi Su | a98c08fb | 2019-06-13 09:03:56 | [diff] [blame] | 10 | #include "ios/web/public/thread/web_thread_delegate.h" |
Rohit Rao | ecad200 | 2018-11-16 19:42:05 | [diff] [blame] | 11 | #include "ios/web/web_thread_impl.h" |
Rohit Rao | ecad200 | 2018-11-16 19:42:05 | [diff] [blame] | 12 | |
| 13 | namespace web { |
| 14 | |
Rohit Rao | 752eeba7 | 2018-12-22 19:28:08 | [diff] [blame] | 15 | namespace { |
| 16 | WebThreadDelegate* g_io_thread_delegate = nullptr; |
| 17 | } // namespace |
Rohit Rao | ecad200 | 2018-11-16 19:42:05 | [diff] [blame] | 18 | |
Rohit Rao | 752eeba7 | 2018-12-22 19:28:08 | [diff] [blame] | 19 | // static |
| 20 | void WebThread::SetIOThreadDelegate(WebThreadDelegate* delegate) { |
Gauthier Ambard | 41b08bdd | 2022-09-08 21:18:22 | [diff] [blame] | 21 | // `delegate` can only be set/unset while WebThread::IO isn't up. |
Rohit Rao | 752eeba7 | 2018-12-22 19:28:08 | [diff] [blame] | 22 | DCHECK(!WebThread::IsThreadInitialized(WebThread::IO)); |
| 23 | // and it cannot be set twice. |
| 24 | DCHECK(!g_io_thread_delegate || !delegate); |
| 25 | |
| 26 | g_io_thread_delegate = delegate; |
| 27 | } |
| 28 | |
| 29 | WebSubThread::WebSubThread(WebThread::ID identifier) |
| 30 | : base::Thread(WebThreadImpl::GetThreadName(identifier)), |
| 31 | identifier_(identifier) { |
| 32 | // Not bound to creation thread. |
| 33 | DETACH_FROM_THREAD(web_thread_checker_); |
| 34 | } |
Rohit Rao | ecad200 | 2018-11-16 19:42:05 | [diff] [blame] | 35 | |
| 36 | WebSubThread::~WebSubThread() { |
| 37 | Stop(); |
| 38 | } |
| 39 | |
Rohit Rao | 752eeba7 | 2018-12-22 19:28:08 | [diff] [blame] | 40 | void WebSubThread::RegisterAsWebThread() { |
| 41 | DCHECK(IsRunning()); |
Rohit Rao | ecad200 | 2018-11-16 19:42:05 | [diff] [blame] | 42 | |
Rohit Rao | 752eeba7 | 2018-12-22 19:28:08 | [diff] [blame] | 43 | DCHECK(!web_thread_); |
| 44 | web_thread_.reset(new WebThreadImpl(identifier_, task_runner())); |
Rohit Rao | 752eeba7 | 2018-12-22 19:28:08 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void WebSubThread::AllowBlockingForTesting() { |
| 48 | DCHECK(!IsRunning()); |
| 49 | is_blocking_allowed_for_testing_ = true; |
| 50 | } |
| 51 | |
| 52 | void WebSubThread::Init() { |
| 53 | DCHECK_CALLED_ON_VALID_THREAD(web_thread_checker_); |
| 54 | |
| 55 | if (!is_blocking_allowed_for_testing_) { |
Rohit Rao | ecad200 | 2018-11-16 19:42:05 | [diff] [blame] | 56 | base::DisallowUnresponsiveTasks(); |
| 57 | } |
| 58 | } |
| 59 | |
Rohit Rao | 752eeba7 | 2018-12-22 19:28:08 | [diff] [blame] | 60 | void WebSubThread::Run(base::RunLoop* run_loop) { |
| 61 | DCHECK_CALLED_ON_VALID_THREAD(web_thread_checker_); |
Rohit Rao | ecad200 | 2018-11-16 19:42:05 | [diff] [blame] | 62 | |
Rohit Rao | 752eeba7 | 2018-12-22 19:28:08 | [diff] [blame] | 63 | switch (identifier_) { |
| 64 | case WebThread::UI: |
| 65 | // The main thread is usually promoted as the UI thread and doesn't go |
| 66 | // through Run() but some tests do run a separate UI thread. |
| 67 | UIThreadRun(run_loop); |
| 68 | break; |
| 69 | case WebThread::IO: |
| 70 | IOThreadRun(run_loop); |
| 71 | return; |
| 72 | case WebThread::ID_COUNT: |
| 73 | NOTREACHED(); |
| 74 | break; |
| 75 | } |
Rohit Rao | ecad200 | 2018-11-16 19:42:05 | [diff] [blame] | 76 | } |
| 77 | |
Rohit Rao | 752eeba7 | 2018-12-22 19:28:08 | [diff] [blame] | 78 | void WebSubThread::CleanUp() { |
| 79 | DCHECK_CALLED_ON_VALID_THREAD(web_thread_checker_); |
| 80 | |
Rohit Rao | 752eeba7 | 2018-12-22 19:28:08 | [diff] [blame] | 81 | if (identifier_ == WebThread::IO && g_io_thread_delegate) |
| 82 | g_io_thread_delegate->CleanUp(); |
| 83 | |
| 84 | web_thread_.reset(); |
| 85 | } |
| 86 | |
Rohit Rao | 752eeba7 | 2018-12-22 19:28:08 | [diff] [blame] | 87 | void WebSubThread::UIThreadRun(base::RunLoop* run_loop) { |
Rohit Rao | 752eeba7 | 2018-12-22 19:28:08 | [diff] [blame] | 88 | Thread::Run(run_loop); |
Olivier Li | 19d8925 | 2020-05-13 17:57:55 | [diff] [blame] | 89 | // Inhibit tail calls of Run and inhibit code folding. |
| 90 | const int line_number = __LINE__; |
Rohit Rao | 752eeba7 | 2018-12-22 19:28:08 | [diff] [blame] | 91 | base::debug::Alias(&line_number); |
| 92 | } |
| 93 | |
| 94 | void WebSubThread::IOThreadRun(base::RunLoop* run_loop) { |
Rohit Rao | 752eeba7 | 2018-12-22 19:28:08 | [diff] [blame] | 95 | Thread::Run(run_loop); |
Olivier Li | 19d8925 | 2020-05-13 17:57:55 | [diff] [blame] | 96 | // Inhibit tail calls of Run and inhibit code folding. |
| 97 | const int line_number = __LINE__; |
Rohit Rao | 752eeba7 | 2018-12-22 19:28:08 | [diff] [blame] | 98 | base::debug::Alias(&line_number); |
| 99 | } |
| 100 | |
Rohit Rao | ecad200 | 2018-11-16 19:42:05 | [diff] [blame] | 101 | } // namespace web |