blob: 1138942e24a5be1b7901f8ab437737769d6e4a09 [file] [log] [blame]
Avi Drissmanea1be232022-09-14 23:29:061// Copyright 2018 The Chromium Authors
Rohit Raoecad2002018-11-16 19:42:052// 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 Rao752eeba72018-12-22 19:28:087#include "base/debug/alias.h"
Avi Drissmancac43f22023-01-12 00:58:418#include "base/functional/bind.h"
Rohit Raoecad2002018-11-16 19:42:059#include "base/threading/thread_restrictions.h"
Yi Sua98c08fb2019-06-13 09:03:5610#include "ios/web/public/thread/web_thread_delegate.h"
Rohit Raoecad2002018-11-16 19:42:0511#include "ios/web/web_thread_impl.h"
Rohit Raoecad2002018-11-16 19:42:0512
13namespace web {
14
Rohit Rao752eeba72018-12-22 19:28:0815namespace {
16WebThreadDelegate* g_io_thread_delegate = nullptr;
17} // namespace
Rohit Raoecad2002018-11-16 19:42:0518
Rohit Rao752eeba72018-12-22 19:28:0819// static
20void WebThread::SetIOThreadDelegate(WebThreadDelegate* delegate) {
Gauthier Ambard41b08bdd2022-09-08 21:18:2221 // `delegate` can only be set/unset while WebThread::IO isn't up.
Rohit Rao752eeba72018-12-22 19:28:0822 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
29WebSubThread::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 Raoecad2002018-11-16 19:42:0535
36WebSubThread::~WebSubThread() {
37 Stop();
38}
39
Rohit Rao752eeba72018-12-22 19:28:0840void WebSubThread::RegisterAsWebThread() {
41 DCHECK(IsRunning());
Rohit Raoecad2002018-11-16 19:42:0542
Rohit Rao752eeba72018-12-22 19:28:0843 DCHECK(!web_thread_);
44 web_thread_.reset(new WebThreadImpl(identifier_, task_runner()));
Rohit Rao752eeba72018-12-22 19:28:0845}
46
47void WebSubThread::AllowBlockingForTesting() {
48 DCHECK(!IsRunning());
49 is_blocking_allowed_for_testing_ = true;
50}
51
52void WebSubThread::Init() {
53 DCHECK_CALLED_ON_VALID_THREAD(web_thread_checker_);
54
55 if (!is_blocking_allowed_for_testing_) {
Rohit Raoecad2002018-11-16 19:42:0556 base::DisallowUnresponsiveTasks();
57 }
58}
59
Rohit Rao752eeba72018-12-22 19:28:0860void WebSubThread::Run(base::RunLoop* run_loop) {
61 DCHECK_CALLED_ON_VALID_THREAD(web_thread_checker_);
Rohit Raoecad2002018-11-16 19:42:0562
Rohit Rao752eeba72018-12-22 19:28:0863 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 Raoecad2002018-11-16 19:42:0576}
77
Rohit Rao752eeba72018-12-22 19:28:0878void WebSubThread::CleanUp() {
79 DCHECK_CALLED_ON_VALID_THREAD(web_thread_checker_);
80
Rohit Rao752eeba72018-12-22 19:28:0881 if (identifier_ == WebThread::IO && g_io_thread_delegate)
82 g_io_thread_delegate->CleanUp();
83
84 web_thread_.reset();
85}
86
Rohit Rao752eeba72018-12-22 19:28:0887void WebSubThread::UIThreadRun(base::RunLoop* run_loop) {
Rohit Rao752eeba72018-12-22 19:28:0888 Thread::Run(run_loop);
Olivier Li19d89252020-05-13 17:57:5589 // Inhibit tail calls of Run and inhibit code folding.
90 const int line_number = __LINE__;
Rohit Rao752eeba72018-12-22 19:28:0891 base::debug::Alias(&line_number);
92}
93
94void WebSubThread::IOThreadRun(base::RunLoop* run_loop) {
Rohit Rao752eeba72018-12-22 19:28:0895 Thread::Run(run_loop);
Olivier Li19d89252020-05-13 17:57:5596 // Inhibit tail calls of Run and inhibit code folding.
97 const int line_number = __LINE__;
Rohit Rao752eeba72018-12-22 19:28:0898 base::debug::Alias(&line_number);
99}
100
Rohit Raoecad2002018-11-16 19:42:05101} // namespace web