Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / web / web_thread_impl.h
blob4ed12fe89152b7d61d2703bca35cbc58d92c7051
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef IOS_WEB_WEB_THREAD_IMPL_H_
6 #define IOS_WEB_WEB_THREAD_IMPL_H_
8 #include "base/threading/thread.h"
9 #include "ios/web/public/web_thread.h"
11 namespace web {
13 class WebThreadDelegate;
15 class WebThreadImpl : public WebThread, public base::Thread {
16 public:
17 // Construct a WebThreadImpl with the supplied identifier. It is an error
18 // to construct a WebThreadImpl that already exists.
19 explicit WebThreadImpl(WebThread::ID identifier);
21 // Special constructor for the main (UI) thread and unittests. If a
22 // |message_loop| is provied, we use a dummy thread here since the main
23 // thread already exists.
24 WebThreadImpl(WebThread::ID identifier, base::MessageLoop* message_loop);
25 ~WebThreadImpl() override;
27 static void ShutdownThreadPool();
29 // TODO(stuartmorgan): Move this to WebThread (where it belongs) once
30 // the alternate BrowserThread-backed-WebThread implementation goes away. See
31 // the note in web_thread_delegate.h.
33 // Sets the delegate for the specified WebThread.
35 // Only one delegate may be registered at a time. Delegates may be
36 // unregistered by providing a nullptr pointer.
38 // If the caller unregisters a delegate before CleanUp has been
39 // called, it must perform its own locking to ensure the delegate is
40 // not deleted while unregistering.
41 static void SetDelegate(ID identifier, WebThreadDelegate* delegate);
43 protected:
44 void Init() override;
45 void Run(base::MessageLoop* message_loop) override;
46 void CleanUp() override;
48 private:
49 // This class implements all the functionality of the public WebThread
50 // functions, but state is stored in the WebThreadImpl to keep
51 // the API cleaner. Therefore make WebThread a friend class.
52 friend class WebThread;
54 // The following are unique function names that makes it possible to tell
55 // the thread id from the callstack alone in crash dumps.
56 void UIThreadRun(base::MessageLoop* message_loop);
57 void DBThreadRun(base::MessageLoop* message_loop);
58 void FileThreadRun(base::MessageLoop* message_loop);
59 void FileUserBlockingThreadRun(base::MessageLoop* message_loop);
60 void CacheThreadRun(base::MessageLoop* message_loop);
61 void IOThreadRun(base::MessageLoop* message_loop);
63 static bool PostTaskHelper(WebThread::ID identifier,
64 const tracked_objects::Location& from_here,
65 const base::Closure& task,
66 base::TimeDelta delay,
67 bool nestable);
69 // Common initialization code for the constructors.
70 void Initialize();
72 // Performs cleanup that needs to happen on the IO thread before calling the
73 // embedder's CleanUp function.
74 void IOThreadPreCleanUp();
76 // For testing.
77 friend class TestWebThreadBundle;
78 friend class TestWebThreadBundleImpl;
79 static void FlushThreadPoolHelperForTesting();
81 // The identifier of this thread. Only one thread can exist with a given
82 // identifier at a given time.
83 ID identifier_;
86 } // namespace web
88 #endif // IOS_WEB_WEB_THREAD_IMPL_H_