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