Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / workers / WorkerThread.h
1 /*
2  * Copyright (C) 2008 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 COMPUTER, 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 COMPUTER, 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 WorkerThread_h
28 #define WorkerThread_h
29
30 #include "core/dom/ExecutionContextTask.h"
31 #include "core/frame/csp/ContentSecurityPolicy.h"
32 #include "core/workers/WorkerGlobalScope.h"
33 #include "platform/SharedTimer.h"
34 #include "platform/heap/glue/MessageLoopInterruptor.h"
35 #include "platform/heap/glue/PendingGCRunner.h"
36 #include "platform/weborigin/SecurityOrigin.h"
37 #include "public/platform/WebThread.h"
38 #include "wtf/Forward.h"
39 #include "wtf/MessageQueue.h"
40 #include "wtf/OwnPtr.h"
41 #include "wtf/PassRefPtr.h"
42 #include "wtf/RefCounted.h"
43
44 namespace blink {
45 class WebWaitableEvent;
46 }
47
48 namespace blink {
49
50     class KURL;
51     class WorkerGlobalScope;
52     class WorkerInspectorController;
53     class WorkerLoaderProxy;
54     class WorkerReportingProxy;
55     class WorkerSharedTimer;
56     class WorkerThreadShutdownFinishTask;
57     class WorkerThreadStartupData;
58     class WorkerThreadTask;
59
60     enum WorkerThreadStartMode { DontPauseWorkerGlobalScopeOnStart, PauseWorkerGlobalScopeOnStart };
61
62
63     class WorkerThread : public RefCounted<WorkerThread> {
64     public:
65         virtual ~WorkerThread();
66
67         virtual void start();
68         virtual void stop();
69
70         // Can be used to wait for this worker thread to shut down.
71         // (This is signalled on the main thread, so it's assumed to be waited on the worker context thread)
72         blink::WebWaitableEvent* shutdownEvent() { return m_shutdownEvent.get(); }
73
74         bool isCurrentThread() const;
75         WorkerLoaderProxy& workerLoaderProxy() const { return m_workerLoaderProxy; }
76         WorkerReportingProxy& workerReportingProxy() const { return m_workerReportingProxy; }
77
78         void postTask(PassOwnPtr<ExecutionContextTask>);
79         void postDebuggerTask(PassOwnPtr<ExecutionContextTask>);
80
81         enum WaitMode { WaitForMessage, DontWaitForMessage };
82         MessageQueueWaitResult runDebuggerTask(WaitMode = WaitForMessage);
83
84         // These methods should be called if the holder of the thread is
85         // going to call runDebuggerTask in a loop.
86         void willEnterNestedLoop();
87         void didLeaveNestedLoop();
88
89         WorkerGlobalScope* workerGlobalScope() const { return m_workerGlobalScope.get(); }
90         bool terminated() const { return m_terminated; }
91
92         // Number of active worker threads.
93         static unsigned workerThreadCount();
94
95         void interruptAndDispatchInspectorCommands();
96         void setWorkerInspectorController(WorkerInspectorController*);
97
98     protected:
99         WorkerThread(WorkerLoaderProxy&, WorkerReportingProxy&, PassOwnPtrWillBeRawPtr<WorkerThreadStartupData>);
100
101         // Factory method for creating a new worker context for the thread.
102         virtual PassRefPtrWillBeRawPtr<WorkerGlobalScope> createWorkerGlobalScope(PassOwnPtrWillBeRawPtr<WorkerThreadStartupData>) = 0;
103
104         virtual void postInitialize() { }
105
106     private:
107         friend class WorkerSharedTimer;
108         friend class WorkerThreadShutdownFinishTask;
109
110         void initialize();
111         void cleanup();
112         void idleHandler();
113         void postDelayedTask(PassOwnPtr<ExecutionContextTask>, long long delayMs);
114
115         bool m_terminated;
116         OwnPtr<WorkerSharedTimer> m_sharedTimer;
117         MessageQueue<WorkerThreadTask> m_debuggerMessageQueue;
118         OwnPtr<PendingGCRunner> m_pendingGCRunner;
119         OwnPtr<WebThread::TaskObserver> m_microtaskRunner;
120         OwnPtr<MessageLoopInterruptor> m_messageLoopInterruptor;
121
122         WorkerLoaderProxy& m_workerLoaderProxy;
123         WorkerReportingProxy& m_workerReportingProxy;
124
125         RefPtrWillBePersistent<WorkerInspectorController> m_workerInspectorController;
126         Mutex m_workerInspectorControllerMutex;
127
128         Mutex m_threadCreationMutex;
129         RefPtrWillBePersistent<WorkerGlobalScope> m_workerGlobalScope;
130         OwnPtrWillBePersistent<WorkerThreadStartupData> m_startupData;
131
132         // Used to signal thread shutdown.
133         OwnPtr<blink::WebWaitableEvent> m_shutdownEvent;
134
135         // FIXME: This has to be last because of crbug.com/401397 - the
136         // WorkerThread might get deleted before it had a chance to properly
137         // shut down. By deleting the WebThread first, we can guarantee that
138         // no pending tasks on the thread might want to access any of the other
139         // members during the WorkerThread's destruction.
140         OwnPtr<blink::WebThread> m_thread;
141     };
142
143 } // namespace blink
144
145 #endif // WorkerThread_h