Check for *.googlezip.net proxies when migrating proxy pref for DRP.
[chromium-blink-merge.git] / base / threading / thread_restrictions.h
blob54f50ebca8796acf51a1f4435a8ff9071cdc2a67
1 // Copyright (c) 2012 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 BASE_THREADING_THREAD_RESTRICTIONS_H_
6 #define BASE_THREADING_THREAD_RESTRICTIONS_H_
8 #include "base/base_export.h"
9 #include "base/basictypes.h"
11 // See comment at top of thread_checker.h
12 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
13 #define ENABLE_THREAD_RESTRICTIONS 1
14 #else
15 #define ENABLE_THREAD_RESTRICTIONS 0
16 #endif
18 class BrowserProcessImpl;
19 class HistogramSynchronizer;
20 class NativeBackendKWallet;
21 class ScopedAllowWaitForLegacyWebViewApi;
23 namespace cc {
24 class CompletionEvent;
25 class TaskGraphRunner;
27 namespace chromeos {
28 class BlockingMethodCaller;
29 namespace system {
30 class StatisticsProviderImpl;
33 namespace chrome_browser_net {
34 class Predictor;
36 namespace content {
37 class BrowserGpuChannelHostFactory;
38 class BrowserGpuMemoryBufferManager;
39 class BrowserShutdownProfileDumper;
40 class BrowserTestBase;
41 class GpuChannelHost;
42 class NestedMessagePumpAndroid;
43 class RenderWidgetResizeHelper;
44 class ScopedAllowWaitForAndroidLayoutTests;
45 class ScopedAllowWaitForDebugURL;
46 class TextInputClientMac;
47 } // namespace content
48 namespace dbus {
49 class Bus;
51 namespace disk_cache {
52 class BackendImpl;
53 class InFlightIO;
55 namespace mojo {
56 namespace common {
57 class WatcherThreadManager;
60 namespace net {
61 namespace internal {
62 class AddressTrackerLinux;
66 namespace remoting {
67 class AutoThread;
70 namespace base {
72 namespace android {
73 class JavaHandlerThread;
76 class SequencedWorkerPool;
77 class SimpleThread;
78 class Thread;
79 class ThreadTestHelper;
81 // Certain behavior is disallowed on certain threads. ThreadRestrictions helps
82 // enforce these rules. Examples of such rules:
84 // * Do not do blocking IO (makes the thread janky)
85 // * Do not access Singleton/LazyInstance (may lead to shutdown crashes)
87 // Here's more about how the protection works:
89 // 1) If a thread should not be allowed to make IO calls, mark it:
90 // base::ThreadRestrictions::SetIOAllowed(false);
91 // By default, threads *are* allowed to make IO calls.
92 // In Chrome browser code, IO calls should be proxied to the File thread.
94 // 2) If a function makes a call that will go out to disk, check whether the
95 // current thread is allowed:
96 // base::ThreadRestrictions::AssertIOAllowed();
99 // Style tip: where should you put AssertIOAllowed checks? It's best
100 // if you put them as close to the disk access as possible, at the
101 // lowest level. This rule is simple to follow and helps catch all
102 // callers. For example, if your function GoDoSomeBlockingDiskCall()
103 // only calls other functions in Chrome and not fopen(), you should go
104 // add the AssertIOAllowed checks in the helper functions.
106 class BASE_EXPORT ThreadRestrictions {
107 public:
108 // Constructing a ScopedAllowIO temporarily allows IO for the current
109 // thread. Doing this is almost certainly always incorrect.
110 class BASE_EXPORT ScopedAllowIO {
111 public:
112 ScopedAllowIO() { previous_value_ = SetIOAllowed(true); }
113 ~ScopedAllowIO() { SetIOAllowed(previous_value_); }
114 private:
115 // Whether IO is allowed when the ScopedAllowIO was constructed.
116 bool previous_value_;
118 DISALLOW_COPY_AND_ASSIGN(ScopedAllowIO);
121 // Constructing a ScopedAllowSingleton temporarily allows accessing for the
122 // current thread. Doing this is almost always incorrect.
123 class BASE_EXPORT ScopedAllowSingleton {
124 public:
125 ScopedAllowSingleton() { previous_value_ = SetSingletonAllowed(true); }
126 ~ScopedAllowSingleton() { SetSingletonAllowed(previous_value_); }
127 private:
128 // Whether singleton use is allowed when the ScopedAllowSingleton was
129 // constructed.
130 bool previous_value_;
132 DISALLOW_COPY_AND_ASSIGN(ScopedAllowSingleton);
135 #if ENABLE_THREAD_RESTRICTIONS
136 // Set whether the current thread to make IO calls.
137 // Threads start out in the *allowed* state.
138 // Returns the previous value.
139 static bool SetIOAllowed(bool allowed);
141 // Check whether the current thread is allowed to make IO calls,
142 // and DCHECK if not. See the block comment above the class for
143 // a discussion of where to add these checks.
144 static void AssertIOAllowed();
146 // Set whether the current thread can use singletons. Returns the previous
147 // value.
148 static bool SetSingletonAllowed(bool allowed);
150 // Check whether the current thread is allowed to use singletons (Singleton /
151 // LazyInstance). DCHECKs if not.
152 static void AssertSingletonAllowed();
154 // Disable waiting on the current thread. Threads start out in the *allowed*
155 // state. Returns the previous value.
156 static void DisallowWaiting();
158 // Check whether the current thread is allowed to wait, and DCHECK if not.
159 static void AssertWaitAllowed();
160 #else
161 // Inline the empty definitions of these functions so that they can be
162 // compiled out.
163 static bool SetIOAllowed(bool allowed) { return true; }
164 static void AssertIOAllowed() {}
165 static bool SetSingletonAllowed(bool allowed) { return true; }
166 static void AssertSingletonAllowed() {}
167 static void DisallowWaiting() {}
168 static void AssertWaitAllowed() {}
169 #endif
171 private:
172 // DO NOT ADD ANY OTHER FRIEND STATEMENTS, talk to jam or brettw first.
173 // BEGIN ALLOWED USAGE.
174 friend class content::BrowserShutdownProfileDumper;
175 friend class content::BrowserTestBase;
176 friend class content::NestedMessagePumpAndroid;
177 friend class content::RenderWidgetResizeHelper;
178 friend class content::ScopedAllowWaitForAndroidLayoutTests;
179 friend class content::ScopedAllowWaitForDebugURL;
180 friend class ::HistogramSynchronizer;
181 friend class ::ScopedAllowWaitForLegacyWebViewApi;
182 friend class cc::CompletionEvent;
183 friend class cc::TaskGraphRunner;
184 friend class mojo::common::WatcherThreadManager;
185 friend class remoting::AutoThread;
186 friend class MessagePumpDefault;
187 friend class SequencedWorkerPool;
188 friend class SimpleThread;
189 friend class Thread;
190 friend class ThreadTestHelper;
191 friend class PlatformThread;
192 friend class android::JavaHandlerThread;
194 // END ALLOWED USAGE.
195 // BEGIN USAGE THAT NEEDS TO BE FIXED.
196 friend class ::chromeos::BlockingMethodCaller; // http://crbug.com/125360
197 friend class ::chromeos::system::StatisticsProviderImpl; // http://crbug.com/125385
198 friend class chrome_browser_net::Predictor; // http://crbug.com/78451
199 friend class
200 content::BrowserGpuChannelHostFactory; // http://crbug.com/125248
201 friend class
202 content::BrowserGpuMemoryBufferManager; // http://crbug.com/420368
203 friend class content::GpuChannelHost; // http://crbug.com/125264
204 friend class content::TextInputClientMac; // http://crbug.com/121917
205 friend class dbus::Bus; // http://crbug.com/125222
206 friend class disk_cache::BackendImpl; // http://crbug.com/74623
207 friend class disk_cache::InFlightIO; // http://crbug.com/74623
208 friend class net::internal::AddressTrackerLinux; // http://crbug.com/125097
209 friend class ::BrowserProcessImpl; // http://crbug.com/125207
210 friend class ::NativeBackendKWallet; // http://crbug.com/125331
211 // END USAGE THAT NEEDS TO BE FIXED.
213 #if ENABLE_THREAD_RESTRICTIONS
214 static bool SetWaitAllowed(bool allowed);
215 #else
216 static bool SetWaitAllowed(bool allowed) { return true; }
217 #endif
219 // Constructing a ScopedAllowWait temporarily allows waiting on the current
220 // thread. Doing this is almost always incorrect, which is why we limit who
221 // can use this through friend. If you find yourself needing to use this, find
222 // another way. Talk to jam or brettw.
223 class BASE_EXPORT ScopedAllowWait {
224 public:
225 ScopedAllowWait() { previous_value_ = SetWaitAllowed(true); }
226 ~ScopedAllowWait() { SetWaitAllowed(previous_value_); }
227 private:
228 // Whether singleton use is allowed when the ScopedAllowWait was
229 // constructed.
230 bool previous_value_;
232 DISALLOW_COPY_AND_ASSIGN(ScopedAllowWait);
235 DISALLOW_IMPLICIT_CONSTRUCTORS(ThreadRestrictions);
238 } // namespace base
240 #endif // BASE_THREADING_THREAD_RESTRICTIONS_H_