| 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/Optional.h" | 11 #include "wtf/Optional.h" |
| 12 | 12 |
| 13 namespace blink { | 13 namespace blink { |
| 14 | 14 |
| 15 // Scoped disabling of script execution on the main thread, | 15 // Scoped disabling of script execution on the main thread, |
| 16 // and only to be used by the main thread. | 16 // and only to be used by the main thread. |
| 17 class PLATFORM_EXPORT ScriptForbiddenScope final { | 17 class PLATFORM_EXPORT ScriptForbiddenScope final { |
| 18 STACK_ALLOCATED(); | 18 STACK_ALLOCATED(); |
| 19 WTF_MAKE_NONCOPYABLE(ScriptForbiddenScope); | 19 WTF_MAKE_NONCOPYABLE(ScriptForbiddenScope); |
| 20 public: | 20 public: |
| 21 ScriptForbiddenScope(); | 21 ScriptForbiddenScope() { enter(); } |
| 22 ~ScriptForbiddenScope(); | 22 ~ScriptForbiddenScope() { exit(); } |
| 23 | 23 |
| 24 class PLATFORM_EXPORT AllowUserAgentScript final { | 24 class PLATFORM_EXPORT AllowUserAgentScript final { |
| 25 STACK_ALLOCATED(); | 25 STACK_ALLOCATED(); |
| 26 WTF_MAKE_NONCOPYABLE(AllowUserAgentScript); | 26 WTF_MAKE_NONCOPYABLE(AllowUserAgentScript); |
| 27 public: | 27 public: |
| 28 AllowUserAgentScript(); | 28 AllowUserAgentScript() |
| 29 ~AllowUserAgentScript(); | 29 { |
| 30 if (isMainThread()) |
| 31 m_change.emplace(&s_scriptForbiddenCount, 0); |
| 32 } |
| 33 ~AllowUserAgentScript() |
| 34 { |
| 35 DCHECK(!isMainThread() || !s_scriptForbiddenCount); |
| 36 } |
| 37 |
| 30 private: | 38 private: |
| 31 Optional<AutoReset<unsigned>> m_change; | 39 Optional<AutoReset<unsigned>> m_change; |
| 32 }; | 40 }; |
| 33 | 41 |
| 34 static void enter(); | 42 static void enter() |
| 35 static void exit(); | 43 { |
| 36 static bool isScriptForbidden(); | 44 DCHECK(isMainThread()); |
| 45 ++s_scriptForbiddenCount; |
| 46 } |
| 47 static void exit() |
| 48 { |
| 49 DCHECK(s_scriptForbiddenCount); |
| 50 --s_scriptForbiddenCount; |
| 51 } |
| 52 static bool isScriptForbidden() |
| 53 { |
| 54 return isMainThread() && s_scriptForbiddenCount; |
| 55 } |
| 56 |
| 57 private: |
| 58 static unsigned s_scriptForbiddenCount; |
| 37 }; | 59 }; |
| 38 | 60 |
| 39 // Scoped disabling of script execution on the main thread, | 61 // Scoped disabling of script execution on the main thread, |
| 40 // if called on the main thread. | 62 // if called on the main thread. |
| 41 // | 63 // |
| 42 // No effect when used by from other threads -- simplifies | 64 // No effect when used by from other threads -- simplifies |
| 43 // call sites that might be used by multiple threads to have | 65 // call sites that might be used by multiple threads to have |
| 44 // this scope object perform the is-main-thread check on | 66 // this scope object perform the is-main-thread check on |
| 45 // its behalf. | 67 // its behalf. |
| 46 class PLATFORM_EXPORT ScriptForbiddenIfMainThreadScope final { | 68 class PLATFORM_EXPORT ScriptForbiddenIfMainThreadScope final { |
| 47 STACK_ALLOCATED(); | 69 STACK_ALLOCATED(); |
| 48 WTF_MAKE_NONCOPYABLE(ScriptForbiddenIfMainThreadScope); | 70 WTF_MAKE_NONCOPYABLE(ScriptForbiddenIfMainThreadScope); |
| 49 public: | 71 public: |
| 50 ScriptForbiddenIfMainThreadScope(); | 72 ScriptForbiddenIfMainThreadScope() |
| 51 ~ScriptForbiddenIfMainThreadScope(); | 73 { |
| 74 m_IsMainThread = isMainThread(); |
| 75 if (m_IsMainThread) |
| 76 ScriptForbiddenScope::enter(); |
| 77 } |
| 78 ~ScriptForbiddenIfMainThreadScope() |
| 79 { |
| 80 if (m_IsMainThread) |
| 81 ScriptForbiddenScope::exit(); |
| 82 } |
| 83 bool m_IsMainThread; |
| 52 }; | 84 }; |
| 53 | 85 |
| 54 } // namespace blink | 86 } // namespace blink |
| 55 | 87 |
| 56 #endif // ScriptForbiddenScope_h | 88 #endif // ScriptForbiddenScope_h |
| OLD | NEW |