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