Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ios / web / public / web_thread.h
blobf820fb756b5bcfe06d8fb5f0e59c6841cbec18d9
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_PUBLIC_WEB_THREAD_H_
6 #define IOS_WEB_PUBLIC_WEB_THREAD_H_
8 #include <string>
10 #include "base/callback_forward.h"
11 #include "base/compiler_specific.h"
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "base/message_loop/message_loop_proxy.h"
15 #include "base/task_runner_util.h"
17 namespace base {
18 class MessageLoop;
19 class SequencedWorkerPool;
22 namespace tracked_objects {
23 class Location;
26 namespace web {
28 // Use DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::ID) to assert that a function
29 // can only be called on the named WebThread.
30 // TODO(ios): rename to DCHECK_CURRENTLY_ON once iOS is independent from
31 // content/ so it won't collide with the macro DCHECK_CURRENTLY_ON in content/.
32 // http://crbug.com/438202
33 #define DCHECK_CURRENTLY_ON_WEB_THREAD(thread_identifier) \
34 (DCHECK(::web::WebThread::CurrentlyOn(thread_identifier)) \
35 << ::web::WebThread::GetDCheckCurrentlyOnErrorMessage(thread_identifier))
37 ///////////////////////////////////////////////////////////////////////////////
38 // WebThread
40 // Utility functions for threads that are known by name. For example, there is
41 // one IO thread for the entire process, and various pieces of code find it
42 // useful to retrieve a pointer to the IO thread's message loop.
44 // Invoke a task by thread ID:
46 // WebThread::PostTask(WebThread::IO, FROM_HERE, task);
48 // The return value is false if the task couldn't be posted because the target
49 // thread doesn't exist. If this could lead to data loss, you need to check the
50 // result and restructure the code to ensure it doesn't occur.
52 // This class automatically handles the lifetime of different threads.
53 // It's always safe to call PostTask on any thread. If it's not yet created,
54 // the task is deleted. There are no race conditions. If the thread that the
55 // task is posted to is guaranteed to outlive the current thread, then no locks
56 // are used. You should never need to cache pointers to MessageLoops, since
57 // they're not thread safe.
58 class WebThread {
59 public:
60 // An enumeration of the well-known threads.
61 // NOTE: threads must be listed in the order of their life-time, with each
62 // thread outliving every other thread below it.
63 enum ID {
64 // The main thread in the browser.
65 UI,
67 // This is the thread that interacts with the database.
68 DB,
70 // This is the thread that interacts with the file system.
71 FILE,
73 // Used for file system operations that block user interactions.
74 // Responsiveness of this thread affect users.
75 FILE_USER_BLOCKING,
77 // This is the thread to handle slow HTTP cache operations.
78 CACHE,
80 // This is the thread that processes non-blocking IO, i.e. IPC and network.
81 // Blocking IO should happen on other threads like DB, FILE,
82 // FILE_USER_BLOCKING and CACHE depending on the usage.
83 IO,
85 // NOTE: do not add new threads here that are only used by a small number of
86 // files. Instead you should just use a Thread class and pass its
87 // MessageLoopProxy around. Named threads there are only for threads that
88 // are used in many places.
90 // This identifier does not represent a thread. Instead it counts the
91 // number of well-known threads. Insert new well-known threads before this
92 // identifier.
93 ID_COUNT
96 // These are the same methods as in message_loop.h, but are guaranteed to
97 // either get posted to the MessageLoop if it's still alive, or be deleted
98 // otherwise.
99 // They return true iff the thread existed and the task was posted.
100 static bool PostTask(ID identifier,
101 const tracked_objects::Location& from_here,
102 const base::Closure& task);
103 static bool PostDelayedTask(ID identifier,
104 const tracked_objects::Location& from_here,
105 const base::Closure& task,
106 base::TimeDelta delay);
108 static bool PostTaskAndReply(ID identifier,
109 const tracked_objects::Location& from_here,
110 const base::Closure& task,
111 const base::Closure& reply);
113 template <typename ReturnType, typename ReplyArgType>
114 static bool PostTaskAndReplyWithResult(
115 ID identifier,
116 const tracked_objects::Location& from_here,
117 const base::Callback<ReturnType(void)>& task,
118 const base::Callback<void(ReplyArgType)>& reply) {
119 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
120 GetMessageLoopProxyForThread(identifier);
121 return base::PostTaskAndReplyWithResult(message_loop_proxy.get(), from_here,
122 task, reply);
125 template <class T>
126 static bool DeleteSoon(ID identifier,
127 const tracked_objects::Location& from_here,
128 const T* object) {
129 return GetMessageLoopProxyForThread(identifier)
130 ->DeleteSoon(from_here, object);
133 // Simplified wrappers for posting to the blocking thread pool. Use this
134 // for doing things like blocking I/O.
136 // The first variant will run the task in the pool with no sequencing
137 // semantics, so may get run in parallel with other posted tasks. The second
138 // variant will all post a task with no sequencing semantics, and will post a
139 // reply task to the origin TaskRunner upon completion. The third variant
140 // provides sequencing between tasks with the same sequence token name.
142 // These tasks are guaranteed to run before shutdown.
144 // If you need to provide different shutdown semantics (like you have
145 // something slow and noncritical that doesn't need to block shutdown),
146 // or you want to manually provide a sequence token (which saves a map
147 // lookup and is guaranteed unique without you having to come up with a
148 // unique string), you can access the sequenced worker pool directly via
149 // GetBlockingPool().
151 // If you need to PostTaskAndReplyWithResult, use
152 // base::PostTaskAndReplyWithResult() with GetBlockingPool() as the task
153 // runner.
154 static bool PostBlockingPoolTask(const tracked_objects::Location& from_here,
155 const base::Closure& task);
156 static bool PostBlockingPoolSequencedTask(
157 const std::string& sequence_token_name,
158 const tracked_objects::Location& from_here,
159 const base::Closure& task);
161 // Returns the thread pool used for blocking file I/O. Use this object to
162 // perform random blocking operations such as file writes.
163 static base::SequencedWorkerPool* GetBlockingPool() WARN_UNUSED_RESULT;
165 // Returns a pointer to the thread's message loop, which will become
166 // invalid during shutdown, so you probably shouldn't hold onto it.
168 // This must not be called before the thread is started, or after
169 // the thread is stopped, or it will DCHECK.
171 // Ownership remains with the WebThread implementation, so you must not
172 // delete the pointer.
173 static base::MessageLoop* UnsafeGetMessageLoopForThread(ID identifier);
175 // Callable on any thread. Returns whether execution is currently on the
176 // given thread. To DCHECK this, use the DCHECK_CURRENTLY_ON_WEB_THREAD()
177 // macro above.
178 static bool CurrentlyOn(ID identifier) WARN_UNUSED_RESULT;
180 // Callers can hold on to a refcounted MessageLoopProxy beyond the lifetime
181 // of the thread.
182 static scoped_refptr<base::MessageLoopProxy> GetMessageLoopProxyForThread(
183 ID identifier);
185 // Returns an appropriate error message for when
186 // DCHECK_CURRENTLY_ON_WEB_THREAD() fails.
187 static std::string GetDCheckCurrentlyOnErrorMessage(ID expected);
189 private:
190 WebThread() {}
191 DISALLOW_COPY_AND_ASSIGN(WebThread);
194 } // namespace web
196 #endif // IOS_WEB_PUBLIC_WEB_THREAD_H_