Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef ScriptForbiddenScope_h | 5 #ifndef ScriptForbiddenScope_h |
| 6 #define ScriptForbiddenScope_h | 6 #define ScriptForbiddenScope_h |
| 7 | 7 |
| 8 #include "platform/PlatformExport.h" | 8 #include "platform/PlatformExport.h" |
| 9 #include "wtf/Allocator.h" | 9 #include "wtf/Allocator.h" |
| 10 #include "wtf/AutoReset.h" | 10 #include "wtf/AutoReset.h" |
| 11 #include "wtf/DynamicAnnotations.h" | |
| 11 #include "wtf/Optional.h" | 12 #include "wtf/Optional.h" |
| 12 | 13 |
| 13 namespace blink { | 14 namespace blink { |
| 14 | 15 |
| 15 // Scoped disabling of script execution on the main thread, | 16 // Scoped disabling of script execution on the main thread, |
| 16 // and only to be used by the main thread. | 17 // and only to be used by the main thread. |
| 17 class PLATFORM_EXPORT ScriptForbiddenScope final { | 18 class PLATFORM_EXPORT ScriptForbiddenScope final { |
| 18 STACK_ALLOCATED(); | 19 STACK_ALLOCATED(); |
| 19 WTF_MAKE_NONCOPYABLE(ScriptForbiddenScope); | 20 WTF_MAKE_NONCOPYABLE(ScriptForbiddenScope); |
| 20 | 21 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 45 } | 46 } |
| 46 static void exit() { | 47 static void exit() { |
| 47 DCHECK(isMainThread()); | 48 DCHECK(isMainThread()); |
| 48 DCHECK(s_scriptForbiddenCount); | 49 DCHECK(s_scriptForbiddenCount); |
| 49 --s_scriptForbiddenCount; | 50 --s_scriptForbiddenCount; |
| 50 } | 51 } |
| 51 static bool isScriptForbidden() { | 52 static bool isScriptForbidden() { |
| 52 // Check s_scriptForbiddenCount first to avoid any calls to isMainThread() | 53 // Check s_scriptForbiddenCount first to avoid any calls to isMainThread() |
| 53 // since under normal situations where we check this (ex. inside | 54 // since under normal situations where we check this (ex. inside |
| 54 // V8ScriptRunner) the value should always be zero. | 55 // V8ScriptRunner) the value should always be zero. |
| 56 WTF_ANNOTATE_BENIGN_RACE(&s_scriptForbiddenCount, | |
|
Alexander Potapenko
2017/03/17 10:14:50
Please no.
WTF_ANNOTATE_BENIGN_RACE is a beast we
| |
| 57 "Safe to racy read s_scriptForbiddenCount since " | |
| 58 "the value only matters on the main thread."); | |
| 55 return s_scriptForbiddenCount && isMainThread(); | 59 return s_scriptForbiddenCount && isMainThread(); |
|
Alexander Potapenko
2017/03/17 10:14:50
If the value only matters on the main thread, how
| |
| 56 } | 60 } |
| 57 | 61 |
| 58 private: | 62 private: |
| 59 static unsigned s_scriptForbiddenCount; | 63 static unsigned s_scriptForbiddenCount; |
| 60 }; | 64 }; |
| 61 | 65 |
| 62 // Scoped disabling of script execution on the main thread, | 66 // Scoped disabling of script execution on the main thread, |
| 63 // if called on the main thread. | 67 // if called on the main thread. |
| 64 // | 68 // |
| 65 // No effect when used by from other threads -- simplifies | 69 // No effect when used by from other threads -- simplifies |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 79 ~ScriptForbiddenIfMainThreadScope() { | 83 ~ScriptForbiddenIfMainThreadScope() { |
| 80 if (m_IsMainThread) | 84 if (m_IsMainThread) |
| 81 ScriptForbiddenScope::exit(); | 85 ScriptForbiddenScope::exit(); |
| 82 } | 86 } |
| 83 bool m_IsMainThread; | 87 bool m_IsMainThread; |
| 84 }; | 88 }; |
| 85 | 89 |
| 86 } // namespace blink | 90 } // namespace blink |
| 87 | 91 |
| 88 #endif // ScriptForbiddenScope_h | 92 #endif // ScriptForbiddenScope_h |
| OLD | NEW |