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/HashMap.h>
31#include <wtf/HashSet.h>
32#include <wtf/Noncopyable.h>
33#include <wtf/OwnPtr.h>
34#include <wtf/text/StringHash.h>
35
36#include <wtf/ThreadSpecific.h>
37#include <wtf/Threading.h>
38using WTF::ThreadSpecific;
39
40namespace WebCore {
41
42    class EventNames;
43    class ReplayInputTypes;
44    class ThreadTimers;
45
46    struct CachedResourceRequestInitiators;
47    struct ICUConverterWrapper;
48    struct TECConverterWrapper;
49
50    class ThreadGlobalData {
51        WTF_MAKE_NONCOPYABLE(ThreadGlobalData);
52    public:
53        ThreadGlobalData();
54        ~ThreadGlobalData();
55        void destroy(); // called on workers to clean up the ThreadGlobalData before the thread exits.
56
57        const CachedResourceRequestInitiators& cachedResourceRequestInitiators() { return *m_cachedResourceRequestInitiators; }
58        EventNames& eventNames() { return *m_eventNames; }
59        ThreadTimers& threadTimers() { return *m_threadTimers; }
60#if ENABLE(WEB_REPLAY)
61        ReplayInputTypes& inputTypes() { return *m_inputTypes; }
62#endif
63
64        ICUConverterWrapper& cachedConverterICU() { return *m_cachedConverterICU; }
65
66#if PLATFORM(MAC)
67        TECConverterWrapper& cachedConverterTEC() { return *m_cachedConverterTEC; }
68#endif
69
70#if USE(WEB_THREAD)
71        void setWebCoreThreadData();
72#endif
73
74    private:
75        OwnPtr<CachedResourceRequestInitiators> m_cachedResourceRequestInitiators;
76        OwnPtr<EventNames> m_eventNames;
77        OwnPtr<ThreadTimers> m_threadTimers;
78
79#if ENABLE(WEB_REPLAY)
80        std::unique_ptr<ReplayInputTypes> m_inputTypes;
81#endif
82
83#ifndef NDEBUG
84        bool m_isMainThread;
85#endif
86
87        OwnPtr<ICUConverterWrapper> m_cachedConverterICU;
88
89#if PLATFORM(MAC)
90        OwnPtr<TECConverterWrapper> m_cachedConverterTEC;
91#endif
92
93        static ThreadSpecific<ThreadGlobalData>* staticData;
94#if USE(WEB_THREAD)
95        static ThreadGlobalData* sharedMainThreadStaticData;
96#endif
97        friend ThreadGlobalData& threadGlobalData();
98    };
99
100#if USE(WEB_THREAD)
101ThreadGlobalData& threadGlobalData();
102#else
103ThreadGlobalData& threadGlobalData() PURE_FUNCTION;
104#endif
105
106} // namespace WebCore
107
108#endif // ThreadGlobalData_h
109