1*6777b538SAndroid Build Coastguard Worker // Copyright 2018 The Chromium Authors 2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_TASK_CURRENT_THREAD_H_ 6*6777b538SAndroid Build Coastguard Worker #define BASE_TASK_CURRENT_THREAD_H_ 7*6777b538SAndroid Build Coastguard Worker 8*6777b538SAndroid Build Coastguard Worker #include <ostream> 9*6777b538SAndroid Build Coastguard Worker #include <type_traits> 10*6777b538SAndroid Build Coastguard Worker 11*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h" 12*6777b538SAndroid Build Coastguard Worker #include "base/callback_list.h" 13*6777b538SAndroid Build Coastguard Worker #include "base/check.h" 14*6777b538SAndroid Build Coastguard Worker #include "base/functional/callback_forward.h" 15*6777b538SAndroid Build Coastguard Worker #include "base/memory/raw_ptr.h" 16*6777b538SAndroid Build Coastguard Worker #include "base/memory/scoped_refptr.h" 17*6777b538SAndroid Build Coastguard Worker #include "base/message_loop/ios_cronet_buildflags.h" 18*6777b538SAndroid Build Coastguard Worker #include "base/message_loop/message_pump_for_io.h" 19*6777b538SAndroid Build Coastguard Worker #include "base/message_loop/message_pump_for_ui.h" 20*6777b538SAndroid Build Coastguard Worker #include "base/pending_task.h" 21*6777b538SAndroid Build Coastguard Worker #include "base/task/sequence_manager/task_time_observer.h" 22*6777b538SAndroid Build Coastguard Worker #include "base/task/single_thread_task_runner.h" 23*6777b538SAndroid Build Coastguard Worker #include "base/task/task_observer.h" 24*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h" 25*6777b538SAndroid Build Coastguard Worker 26*6777b538SAndroid Build Coastguard Worker namespace autofill { 27*6777b538SAndroid Build Coastguard Worker class NextIdleTimeTicks; 28*6777b538SAndroid Build Coastguard Worker } 29*6777b538SAndroid Build Coastguard Worker 30*6777b538SAndroid Build Coastguard Worker namespace content { 31*6777b538SAndroid Build Coastguard Worker class BrowserMainLoop; 32*6777b538SAndroid Build Coastguard Worker } 33*6777b538SAndroid Build Coastguard Worker 34*6777b538SAndroid Build Coastguard Worker namespace web { 35*6777b538SAndroid Build Coastguard Worker class WebTaskEnvironment; 36*6777b538SAndroid Build Coastguard Worker } 37*6777b538SAndroid Build Coastguard Worker 38*6777b538SAndroid Build Coastguard Worker namespace base { 39*6777b538SAndroid Build Coastguard Worker 40*6777b538SAndroid Build Coastguard Worker namespace test { 41*6777b538SAndroid Build Coastguard Worker bool RunUntil(FunctionRef<bool(void)>); 42*6777b538SAndroid Build Coastguard Worker void TestPredicateOrRegisterOnNextIdleCallback(base::FunctionRef<bool(void)>, 43*6777b538SAndroid Build Coastguard Worker CallbackListSubscription*, 44*6777b538SAndroid Build Coastguard Worker OnceClosure); 45*6777b538SAndroid Build Coastguard Worker } // namespace test 46*6777b538SAndroid Build Coastguard Worker 47*6777b538SAndroid Build Coastguard Worker namespace sequence_manager { 48*6777b538SAndroid Build Coastguard Worker namespace internal { 49*6777b538SAndroid Build Coastguard Worker class SequenceManagerImpl; 50*6777b538SAndroid Build Coastguard Worker } 51*6777b538SAndroid Build Coastguard Worker } // namespace sequence_manager 52*6777b538SAndroid Build Coastguard Worker 53*6777b538SAndroid Build Coastguard Worker // CurrentThread is a proxy to a subset of Task related APIs bound to the 54*6777b538SAndroid Build Coastguard Worker // current thread 55*6777b538SAndroid Build Coastguard Worker // 56*6777b538SAndroid Build Coastguard Worker // Current(UI|IO)Thread is available statically through 57*6777b538SAndroid Build Coastguard Worker // Current(UI|IO)Thread::Get() on threads that have registered as CurrentThread 58*6777b538SAndroid Build Coastguard Worker // on this physical thread (e.g. by using SingleThreadTaskExecutor). APIs 59*6777b538SAndroid Build Coastguard Worker // intended for all consumers on the thread should be on Current(UI|IO)Thread, 60*6777b538SAndroid Build Coastguard Worker // while internal APIs might be on multiple internal classes (e.g. 61*6777b538SAndroid Build Coastguard Worker // SequenceManager). 62*6777b538SAndroid Build Coastguard Worker // 63*6777b538SAndroid Build Coastguard Worker // Why: Historically MessageLoop would take care of everything related to event 64*6777b538SAndroid Build Coastguard Worker // processing on a given thread. Nowadays that functionality is split among 65*6777b538SAndroid Build Coastguard Worker // different classes. At that time MessageLoop::current() gave access to the 66*6777b538SAndroid Build Coastguard Worker // full MessageLoop API, preventing both addition of powerful owner-only APIs as 67*6777b538SAndroid Build Coastguard Worker // well as making it harder to remove callers of deprecated APIs (that need to 68*6777b538SAndroid Build Coastguard Worker // stick around for a few owner-only use cases and re-accrue callers after 69*6777b538SAndroid Build Coastguard Worker // cleanup per remaining publicly available). 70*6777b538SAndroid Build Coastguard Worker // 71*6777b538SAndroid Build Coastguard Worker // As such, many methods below are flagged as deprecated and should be removed 72*6777b538SAndroid Build Coastguard Worker // once all static callers have been migrated. 73*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT CurrentThread { 74*6777b538SAndroid Build Coastguard Worker public: 75*6777b538SAndroid Build Coastguard Worker // CurrentThread is effectively just a disguised pointer and is fine to 76*6777b538SAndroid Build Coastguard Worker // copy/move around. 77*6777b538SAndroid Build Coastguard Worker CurrentThread(const CurrentThread& other) = default; 78*6777b538SAndroid Build Coastguard Worker CurrentThread(CurrentThread&& other) = default; 79*6777b538SAndroid Build Coastguard Worker CurrentThread& operator=(const CurrentThread& other) = default; 80*6777b538SAndroid Build Coastguard Worker 81*6777b538SAndroid Build Coastguard Worker friend bool operator==(const CurrentThread&, const CurrentThread&) = default; 82*6777b538SAndroid Build Coastguard Worker 83*6777b538SAndroid Build Coastguard Worker // Returns a proxy object to interact with the Task related APIs for the 84*6777b538SAndroid Build Coastguard Worker // current thread. It must only be used on the thread it was obtained. 85*6777b538SAndroid Build Coastguard Worker static CurrentThread Get(); 86*6777b538SAndroid Build Coastguard Worker 87*6777b538SAndroid Build Coastguard Worker // Return an empty CurrentThread. No methods should be called on this 88*6777b538SAndroid Build Coastguard Worker // object. 89*6777b538SAndroid Build Coastguard Worker static CurrentThread GetNull(); 90*6777b538SAndroid Build Coastguard Worker 91*6777b538SAndroid Build Coastguard Worker // Returns true if the current thread is registered to expose CurrentThread 92*6777b538SAndroid Build Coastguard Worker // API. Prefer this to verifying the boolean value of Get() (so that Get() can 93*6777b538SAndroid Build Coastguard Worker // ultimately DCHECK it's only invoked when IsSet()). 94*6777b538SAndroid Build Coastguard Worker static bool IsSet(); 95*6777b538SAndroid Build Coastguard Worker 96*6777b538SAndroid Build Coastguard Worker // Allow CurrentThread to be used like a pointer to support the many 97*6777b538SAndroid Build Coastguard Worker // callsites that used MessageLoop::current() that way when it was a 98*6777b538SAndroid Build Coastguard Worker // MessageLoop*. 99*6777b538SAndroid Build Coastguard Worker CurrentThread* operator->() { return this; } 100*6777b538SAndroid Build Coastguard Worker explicit operator bool() const { return !!current_; } 101*6777b538SAndroid Build Coastguard Worker 102*6777b538SAndroid Build Coastguard Worker // A DestructionObserver is notified when the current task execution 103*6777b538SAndroid Build Coastguard Worker // environment is being destroyed. These observers are notified prior to 104*6777b538SAndroid Build Coastguard Worker // CurrentThread::IsSet() being changed to return false. This gives interested 105*6777b538SAndroid Build Coastguard Worker // parties the chance to do final cleanup. 106*6777b538SAndroid Build Coastguard Worker // 107*6777b538SAndroid Build Coastguard Worker // NOTE: Any tasks posted to the current thread during this notification will 108*6777b538SAndroid Build Coastguard Worker // not be run. Instead, they will be deleted. 109*6777b538SAndroid Build Coastguard Worker // 110*6777b538SAndroid Build Coastguard Worker // Deprecation note: Prefer SequenceLocalStorageSlot<std::unique_ptr<Foo>> to 111*6777b538SAndroid Build Coastguard Worker // DestructionObserver to bind an object's lifetime to the current 112*6777b538SAndroid Build Coastguard Worker // thread/sequence. 113*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT DestructionObserver { 114*6777b538SAndroid Build Coastguard Worker public: 115*6777b538SAndroid Build Coastguard Worker // TODO(https://crbug.com/891670): Rename to 116*6777b538SAndroid Build Coastguard Worker // WillDestroyCurrentTaskExecutionEnvironment 117*6777b538SAndroid Build Coastguard Worker virtual void WillDestroyCurrentMessageLoop() = 0; 118*6777b538SAndroid Build Coastguard Worker 119*6777b538SAndroid Build Coastguard Worker protected: 120*6777b538SAndroid Build Coastguard Worker virtual ~DestructionObserver() = default; 121*6777b538SAndroid Build Coastguard Worker }; 122*6777b538SAndroid Build Coastguard Worker 123*6777b538SAndroid Build Coastguard Worker // Add a DestructionObserver, which will start receiving notifications 124*6777b538SAndroid Build Coastguard Worker // immediately. 125*6777b538SAndroid Build Coastguard Worker void AddDestructionObserver(DestructionObserver* destruction_observer); 126*6777b538SAndroid Build Coastguard Worker 127*6777b538SAndroid Build Coastguard Worker // Remove a DestructionObserver. It is safe to call this method while a 128*6777b538SAndroid Build Coastguard Worker // DestructionObserver is receiving a notification callback. 129*6777b538SAndroid Build Coastguard Worker void RemoveDestructionObserver(DestructionObserver* destruction_observer); 130*6777b538SAndroid Build Coastguard Worker 131*6777b538SAndroid Build Coastguard Worker // Forwards to SequenceManager::SetTaskRunner(). 132*6777b538SAndroid Build Coastguard Worker // DEPRECATED(https://crbug.com/825327): only owners of the SequenceManager 133*6777b538SAndroid Build Coastguard Worker // instance should replace its TaskRunner. 134*6777b538SAndroid Build Coastguard Worker void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner); 135*6777b538SAndroid Build Coastguard Worker 136*6777b538SAndroid Build Coastguard Worker // Forwards to SequenceManager::(Add|Remove)TaskObserver. 137*6777b538SAndroid Build Coastguard Worker // DEPRECATED(https://crbug.com/825327): only owners of the SequenceManager 138*6777b538SAndroid Build Coastguard Worker // instance should add task observers on it. 139*6777b538SAndroid Build Coastguard Worker void AddTaskObserver(TaskObserver* task_observer); 140*6777b538SAndroid Build Coastguard Worker void RemoveTaskObserver(TaskObserver* task_observer); 141*6777b538SAndroid Build Coastguard Worker 142*6777b538SAndroid Build Coastguard Worker // When this functionality is enabled, the queue time will be recorded for 143*6777b538SAndroid Build Coastguard Worker // posted tasks. 144*6777b538SAndroid Build Coastguard Worker void SetAddQueueTimeToTasks(bool enable); 145*6777b538SAndroid Build Coastguard Worker 146*6777b538SAndroid Build Coastguard Worker // Registers a `OnceClosure` to be called on this thread the next time it goes 147*6777b538SAndroid Build Coastguard Worker // idle. This is meant for internal usage; callers should use BEST_EFFORT 148*6777b538SAndroid Build Coastguard Worker // tasks instead of this for generic work that needs to wait until quiescence 149*6777b538SAndroid Build Coastguard Worker // to run. 150*6777b538SAndroid Build Coastguard Worker class RegisterOnNextIdleCallbackPasskey { 151*6777b538SAndroid Build Coastguard Worker private: RegisterOnNextIdleCallbackPasskey()152*6777b538SAndroid Build Coastguard Worker RegisterOnNextIdleCallbackPasskey() {} 153*6777b538SAndroid Build Coastguard Worker 154*6777b538SAndroid Build Coastguard Worker friend autofill::NextIdleTimeTicks; 155*6777b538SAndroid Build Coastguard Worker friend content::BrowserMainLoop; 156*6777b538SAndroid Build Coastguard Worker friend bool test::RunUntil(FunctionRef<bool(void)>); 157*6777b538SAndroid Build Coastguard Worker friend void test::TestPredicateOrRegisterOnNextIdleCallback( 158*6777b538SAndroid Build Coastguard Worker base::FunctionRef<bool(void)>, 159*6777b538SAndroid Build Coastguard Worker CallbackListSubscription*, 160*6777b538SAndroid Build Coastguard Worker OnceClosure); 161*6777b538SAndroid Build Coastguard Worker }; 162*6777b538SAndroid Build Coastguard Worker [[nodiscard]] CallbackListSubscription RegisterOnNextIdleCallback( 163*6777b538SAndroid Build Coastguard Worker RegisterOnNextIdleCallbackPasskey, 164*6777b538SAndroid Build Coastguard Worker OnceClosure on_next_idle_callback); 165*6777b538SAndroid Build Coastguard Worker 166*6777b538SAndroid Build Coastguard Worker // Enables nested task processing in scope of an upcoming native message loop. 167*6777b538SAndroid Build Coastguard Worker // Some unwanted message loops may occur when using common controls or printer 168*6777b538SAndroid Build Coastguard Worker // functions. Hence, nested task processing is disabled by default to avoid 169*6777b538SAndroid Build Coastguard Worker // unplanned reentrancy. This re-enables it in cases where the stack is 170*6777b538SAndroid Build Coastguard Worker // reentrancy safe and processing nestable tasks is explicitly safe. 171*6777b538SAndroid Build Coastguard Worker // 172*6777b538SAndroid Build Coastguard Worker // For instance, 173*6777b538SAndroid Build Coastguard Worker // - The current thread is running a message loop. 174*6777b538SAndroid Build Coastguard Worker // - It receives a task #1 and executes it. 175*6777b538SAndroid Build Coastguard Worker // - The task #1 implicitly starts a nested message loop, like a MessageBox in 176*6777b538SAndroid Build Coastguard Worker // the unit test. This can also be StartDoc or GetSaveFileName. 177*6777b538SAndroid Build Coastguard Worker // - The thread receives a task #2 before or while in this second message 178*6777b538SAndroid Build Coastguard Worker // loop. 179*6777b538SAndroid Build Coastguard Worker // - With NestableTasksAllowed set to true, the task #2 will run right away. 180*6777b538SAndroid Build Coastguard Worker // Otherwise, it will get executed right after task #1 completes at "thread 181*6777b538SAndroid Build Coastguard Worker // message loop level". 182*6777b538SAndroid Build Coastguard Worker // 183*6777b538SAndroid Build Coastguard Worker // Use RunLoop::Type::kNestableTasksAllowed when nesting is triggered by the 184*6777b538SAndroid Build Coastguard Worker // application RunLoop rather than by native code. 185*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT ScopedAllowApplicationTasksInNativeNestedLoop { 186*6777b538SAndroid Build Coastguard Worker public: 187*6777b538SAndroid Build Coastguard Worker ScopedAllowApplicationTasksInNativeNestedLoop(); 188*6777b538SAndroid Build Coastguard Worker ~ScopedAllowApplicationTasksInNativeNestedLoop(); 189*6777b538SAndroid Build Coastguard Worker 190*6777b538SAndroid Build Coastguard Worker private: 191*6777b538SAndroid Build Coastguard Worker const raw_ptr<sequence_manager::internal::SequenceManagerImpl> 192*6777b538SAndroid Build Coastguard Worker sequence_manager_; 193*6777b538SAndroid Build Coastguard Worker const bool previous_state_; 194*6777b538SAndroid Build Coastguard Worker }; 195*6777b538SAndroid Build Coastguard Worker 196*6777b538SAndroid Build Coastguard Worker // Returns true if nestable tasks are allowed on the current thread at this 197*6777b538SAndroid Build Coastguard Worker // time (i.e. if a native nested loop would start from the callee's point in 198*6777b538SAndroid Build Coastguard Worker // the stack, would it be allowed to run application tasks). 199*6777b538SAndroid Build Coastguard Worker bool ApplicationTasksAllowedInNativeNestedLoop() const; 200*6777b538SAndroid Build Coastguard Worker 201*6777b538SAndroid Build Coastguard Worker // Returns true if this instance is bound to the current thread. 202*6777b538SAndroid Build Coastguard Worker bool IsBoundToCurrentThread() const; 203*6777b538SAndroid Build Coastguard Worker 204*6777b538SAndroid Build Coastguard Worker // Returns true if the current thread is idle (ignoring delayed tasks). This 205*6777b538SAndroid Build Coastguard Worker // is the same condition which triggers DoWork() to return false: i.e. out of 206*6777b538SAndroid Build Coastguard Worker // tasks which can be processed at the current run-level -- there might be 207*6777b538SAndroid Build Coastguard Worker // deferred non-nestable tasks remaining if currently in a nested run level. 208*6777b538SAndroid Build Coastguard Worker bool IsIdleForTesting(); 209*6777b538SAndroid Build Coastguard Worker 210*6777b538SAndroid Build Coastguard Worker // Enables ThreadControllerWithMessagePumpImpl's TimeKeeper metrics. 211*6777b538SAndroid Build Coastguard Worker // `thread_name` will be used as a suffix. 212*6777b538SAndroid Build Coastguard Worker void EnableMessagePumpTimeKeeperMetrics(const char* thread_name); 213*6777b538SAndroid Build Coastguard Worker 214*6777b538SAndroid Build Coastguard Worker protected: CurrentThread(sequence_manager::internal::SequenceManagerImpl * sequence_manager)215*6777b538SAndroid Build Coastguard Worker explicit CurrentThread( 216*6777b538SAndroid Build Coastguard Worker sequence_manager::internal::SequenceManagerImpl* sequence_manager) 217*6777b538SAndroid Build Coastguard Worker : current_(sequence_manager) {} 218*6777b538SAndroid Build Coastguard Worker 219*6777b538SAndroid Build Coastguard Worker static sequence_manager::internal::SequenceManagerImpl* 220*6777b538SAndroid Build Coastguard Worker GetCurrentSequenceManagerImpl(); 221*6777b538SAndroid Build Coastguard Worker 222*6777b538SAndroid Build Coastguard Worker friend class MessagePumpLibeventTest; 223*6777b538SAndroid Build Coastguard Worker friend class ScheduleWorkTest; 224*6777b538SAndroid Build Coastguard Worker friend class Thread; 225*6777b538SAndroid Build Coastguard Worker friend class sequence_manager::internal::SequenceManagerImpl; 226*6777b538SAndroid Build Coastguard Worker friend class MessageLoopTaskRunnerTest; 227*6777b538SAndroid Build Coastguard Worker friend class web::WebTaskEnvironment; 228*6777b538SAndroid Build Coastguard Worker 229*6777b538SAndroid Build Coastguard Worker raw_ptr<sequence_manager::internal::SequenceManagerImpl> current_; 230*6777b538SAndroid Build Coastguard Worker }; 231*6777b538SAndroid Build Coastguard Worker 232*6777b538SAndroid Build Coastguard Worker #if !BUILDFLAG(IS_NACL) 233*6777b538SAndroid Build Coastguard Worker 234*6777b538SAndroid Build Coastguard Worker // UI extension of CurrentThread. 235*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT CurrentUIThread : public CurrentThread { 236*6777b538SAndroid Build Coastguard Worker public: 237*6777b538SAndroid Build Coastguard Worker // Returns an interface for the CurrentUIThread of the current thread. 238*6777b538SAndroid Build Coastguard Worker // Asserts that IsSet(). 239*6777b538SAndroid Build Coastguard Worker static CurrentUIThread Get(); 240*6777b538SAndroid Build Coastguard Worker 241*6777b538SAndroid Build Coastguard Worker // Returns true if the current thread is running a CurrentUIThread. 242*6777b538SAndroid Build Coastguard Worker static bool IsSet(); 243*6777b538SAndroid Build Coastguard Worker 244*6777b538SAndroid Build Coastguard Worker CurrentUIThread* operator->() { return this; } 245*6777b538SAndroid Build Coastguard Worker 246*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_OZONE) && !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_WIN) 247*6777b538SAndroid Build Coastguard Worker static_assert( 248*6777b538SAndroid Build Coastguard Worker std::is_base_of_v<WatchableIOMessagePumpPosix, MessagePumpForUI>, 249*6777b538SAndroid Build Coastguard Worker "CurrentThreadForUI::WatchFileDescriptor is supported only" 250*6777b538SAndroid Build Coastguard Worker "by MessagePumpLibevent and MessagePumpGlib implementations."); 251*6777b538SAndroid Build Coastguard Worker bool WatchFileDescriptor(int fd, 252*6777b538SAndroid Build Coastguard Worker bool persistent, 253*6777b538SAndroid Build Coastguard Worker MessagePumpForUI::Mode mode, 254*6777b538SAndroid Build Coastguard Worker MessagePumpForUI::FdWatchController* controller, 255*6777b538SAndroid Build Coastguard Worker MessagePumpForUI::FdWatcher* delegate); 256*6777b538SAndroid Build Coastguard Worker #endif 257*6777b538SAndroid Build Coastguard Worker 258*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_IOS) 259*6777b538SAndroid Build Coastguard Worker // Forwards to SequenceManager::Attach(). 260*6777b538SAndroid Build Coastguard Worker // TODO(https://crbug.com/825327): Plumb the actual SequenceManager* to 261*6777b538SAndroid Build Coastguard Worker // callers and remove ability to access this method from 262*6777b538SAndroid Build Coastguard Worker // CurrentUIThread. 263*6777b538SAndroid Build Coastguard Worker void Attach(); 264*6777b538SAndroid Build Coastguard Worker #endif 265*6777b538SAndroid Build Coastguard Worker 266*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_ANDROID) 267*6777b538SAndroid Build Coastguard Worker // Forwards to MessagePumpAndroid::Abort(). 268*6777b538SAndroid Build Coastguard Worker // TODO(https://crbug.com/825327): Plumb the actual MessagePumpForUI* to 269*6777b538SAndroid Build Coastguard Worker // callers and remove ability to access this method from 270*6777b538SAndroid Build Coastguard Worker // CurrentUIThread. 271*6777b538SAndroid Build Coastguard Worker void Abort(); 272*6777b538SAndroid Build Coastguard Worker #endif 273*6777b538SAndroid Build Coastguard Worker 274*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN) 275*6777b538SAndroid Build Coastguard Worker void AddMessagePumpObserver(MessagePumpForUI::Observer* observer); 276*6777b538SAndroid Build Coastguard Worker void RemoveMessagePumpObserver(MessagePumpForUI::Observer* observer); 277*6777b538SAndroid Build Coastguard Worker #endif 278*6777b538SAndroid Build Coastguard Worker 279*6777b538SAndroid Build Coastguard Worker private: CurrentUIThread(sequence_manager::internal::SequenceManagerImpl * current)280*6777b538SAndroid Build Coastguard Worker explicit CurrentUIThread( 281*6777b538SAndroid Build Coastguard Worker sequence_manager::internal::SequenceManagerImpl* current) 282*6777b538SAndroid Build Coastguard Worker : CurrentThread(current) {} 283*6777b538SAndroid Build Coastguard Worker 284*6777b538SAndroid Build Coastguard Worker MessagePumpForUI* GetMessagePumpForUI() const; 285*6777b538SAndroid Build Coastguard Worker }; 286*6777b538SAndroid Build Coastguard Worker 287*6777b538SAndroid Build Coastguard Worker #endif // !BUILDFLAG(IS_NACL) 288*6777b538SAndroid Build Coastguard Worker 289*6777b538SAndroid Build Coastguard Worker // ForIO extension of CurrentThread. 290*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT CurrentIOThread : public CurrentThread { 291*6777b538SAndroid Build Coastguard Worker public: 292*6777b538SAndroid Build Coastguard Worker // Returns an interface for the CurrentIOThread of the current thread. 293*6777b538SAndroid Build Coastguard Worker // Asserts that IsSet(). 294*6777b538SAndroid Build Coastguard Worker static CurrentIOThread Get(); 295*6777b538SAndroid Build Coastguard Worker 296*6777b538SAndroid Build Coastguard Worker // Returns true if the current thread is running a CurrentIOThread. 297*6777b538SAndroid Build Coastguard Worker static bool IsSet(); 298*6777b538SAndroid Build Coastguard Worker 299*6777b538SAndroid Build Coastguard Worker CurrentIOThread* operator->() { return this; } 300*6777b538SAndroid Build Coastguard Worker 301*6777b538SAndroid Build Coastguard Worker #if !BUILDFLAG(IS_NACL) 302*6777b538SAndroid Build Coastguard Worker 303*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN) 304*6777b538SAndroid Build Coastguard Worker // Please see MessagePumpWin for definitions of these methods. 305*6777b538SAndroid Build Coastguard Worker HRESULT RegisterIOHandler(HANDLE file, MessagePumpForIO::IOHandler* handler); 306*6777b538SAndroid Build Coastguard Worker bool RegisterJobObject(HANDLE job, MessagePumpForIO::IOHandler* handler); 307*6777b538SAndroid Build Coastguard Worker #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) 308*6777b538SAndroid Build Coastguard Worker // Please see WatchableIOMessagePumpPosix for definition. 309*6777b538SAndroid Build Coastguard Worker // Prefer base::FileDescriptorWatcher for non-critical IO. 310*6777b538SAndroid Build Coastguard Worker bool WatchFileDescriptor(int fd, 311*6777b538SAndroid Build Coastguard Worker bool persistent, 312*6777b538SAndroid Build Coastguard Worker MessagePumpForIO::Mode mode, 313*6777b538SAndroid Build Coastguard Worker MessagePumpForIO::FdWatchController* controller, 314*6777b538SAndroid Build Coastguard Worker MessagePumpForIO::FdWatcher* delegate); 315*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_WIN) 316*6777b538SAndroid Build Coastguard Worker 317*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && !BUILDFLAG(CRONET_BUILD)) 318*6777b538SAndroid Build Coastguard Worker bool WatchMachReceivePort( 319*6777b538SAndroid Build Coastguard Worker mach_port_t port, 320*6777b538SAndroid Build Coastguard Worker MessagePumpForIO::MachPortWatchController* controller, 321*6777b538SAndroid Build Coastguard Worker MessagePumpForIO::MachPortWatcher* delegate); 322*6777b538SAndroid Build Coastguard Worker #endif 323*6777b538SAndroid Build Coastguard Worker 324*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_FUCHSIA) 325*6777b538SAndroid Build Coastguard Worker // Additional watch API for native platform resources. 326*6777b538SAndroid Build Coastguard Worker bool WatchZxHandle(zx_handle_t handle, 327*6777b538SAndroid Build Coastguard Worker bool persistent, 328*6777b538SAndroid Build Coastguard Worker zx_signals_t signals, 329*6777b538SAndroid Build Coastguard Worker MessagePumpForIO::ZxHandleWatchController* controller, 330*6777b538SAndroid Build Coastguard Worker MessagePumpForIO::ZxHandleWatcher* delegate); 331*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_FUCHSIA) 332*6777b538SAndroid Build Coastguard Worker 333*6777b538SAndroid Build Coastguard Worker #endif // !BUILDFLAG(IS_NACL) 334*6777b538SAndroid Build Coastguard Worker 335*6777b538SAndroid Build Coastguard Worker private: CurrentIOThread(sequence_manager::internal::SequenceManagerImpl * current)336*6777b538SAndroid Build Coastguard Worker explicit CurrentIOThread( 337*6777b538SAndroid Build Coastguard Worker sequence_manager::internal::SequenceManagerImpl* current) 338*6777b538SAndroid Build Coastguard Worker : CurrentThread(current) {} 339*6777b538SAndroid Build Coastguard Worker 340*6777b538SAndroid Build Coastguard Worker MessagePumpForIO* GetMessagePumpForIO() const; 341*6777b538SAndroid Build Coastguard Worker }; 342*6777b538SAndroid Build Coastguard Worker 343*6777b538SAndroid Build Coastguard Worker } // namespace base 344*6777b538SAndroid Build Coastguard Worker 345*6777b538SAndroid Build Coastguard Worker #endif // BASE_TASK_CURRENT_THREAD_H_ 346