1/*
2 * Copyright (C) 2008, 2014 Apple Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
27#ifndef ThreadGlobalData_h
28#define ThreadGlobalData_h
29
30#include <wtf/ThreadSafeRefCounted.h>
31#include <wtf/text/StringHash.h>
32
33namespace JSC {
34class ExecState;
35}
36
37namespace WebCore {
38
39 class QualifiedNameCache;
40 class ThreadTimers;
41
42 struct CachedResourceRequestInitiators;
43 struct EventNames;
44 struct ICUConverterWrapper;
45
46#if USE(WEB_THREAD)
47 class ThreadGlobalData : public ThreadSafeRefCounted<ThreadGlobalData> {
48#else
49 class ThreadGlobalData {
50#endif
51 WTF_MAKE_NONCOPYABLE(ThreadGlobalData);
52 WTF_MAKE_FAST_ALLOCATED;
53 public:
54 WEBCORE_EXPORT ThreadGlobalData();
55 WEBCORE_EXPORT ~ThreadGlobalData();
56 void destroy(); // called on workers to clean up the ThreadGlobalData before the thread exits.
57
58 const CachedResourceRequestInitiators& cachedResourceRequestInitiators() { return *m_cachedResourceRequestInitiators; }
59 EventNames& eventNames() { return *m_eventNames; }
60 ThreadTimers& threadTimers() { return *m_threadTimers; }
61 QualifiedNameCache& qualifiedNameCache() { return *m_qualifiedNameCache; }
62
63 ICUConverterWrapper& cachedConverterICU() { return *m_cachedConverterICU; }
64
65 JSC::ExecState* currentState() const { return m_currentState; }
66 void setCurrentState(JSC::ExecState* state) { m_currentState = state; }
67
68#if USE(WEB_THREAD)
69 void setWebCoreThreadData();
70#endif
71
72 bool isInRemoveAllEventListeners() const { return m_isInRemoveAllEventListeners; }
73 void setIsInRemoveAllEventListeners(bool value) { m_isInRemoveAllEventListeners = value; }
74
75 private:
76 std::unique_ptr<CachedResourceRequestInitiators> m_cachedResourceRequestInitiators;
77 std::unique_ptr<EventNames> m_eventNames;
78 std::unique_ptr<ThreadTimers> m_threadTimers;
79 std::unique_ptr<QualifiedNameCache> m_qualifiedNameCache;
80 JSC::ExecState* m_currentState { nullptr };
81
82#ifndef NDEBUG
83 bool m_isMainThread;
84#endif
85
86 bool m_isInRemoveAllEventListeners { false };
87
88 std::unique_ptr<ICUConverterWrapper> m_cachedConverterICU;
89
90 WEBCORE_EXPORT friend ThreadGlobalData& threadGlobalData();
91 };
92
93#if USE(WEB_THREAD)
94WEBCORE_EXPORT ThreadGlobalData& threadGlobalData();
95#else
96WEBCORE_EXPORT ThreadGlobalData& threadGlobalData() PURE_FUNCTION;
97#endif
98
99} // namespace WebCore
100
101#endif // ThreadGlobalData_h
102