| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 * | 24 * |
| 25 */ | 25 */ |
| 26 | 26 |
| 27 #ifndef WorkerThread_h | 27 #ifndef WorkerThread_h |
| 28 #define WorkerThread_h | 28 #define WorkerThread_h |
| 29 | 29 |
| 30 #include "core/dom/ExecutionContextTask.h" |
| 30 #include "core/frame/csp/ContentSecurityPolicy.h" | 31 #include "core/frame/csp/ContentSecurityPolicy.h" |
| 31 #include "core/workers/WorkerRunLoop.h" | 32 #include "core/workers/WorkerGlobalScope.h" |
| 33 #include "platform/SharedTimer.h" |
| 32 #include "platform/weborigin/SecurityOrigin.h" | 34 #include "platform/weborigin/SecurityOrigin.h" |
| 35 #include "public/platform/WebThread.h" |
| 33 #include "wtf/Forward.h" | 36 #include "wtf/Forward.h" |
| 37 #include "wtf/MessageQueue.h" |
| 34 #include "wtf/OwnPtr.h" | 38 #include "wtf/OwnPtr.h" |
| 35 #include "wtf/PassRefPtr.h" | 39 #include "wtf/PassRefPtr.h" |
| 36 #include "wtf/RefCounted.h" | 40 #include "wtf/RefCounted.h" |
| 37 | 41 |
| 38 namespace blink { | 42 namespace blink { |
| 39 class WebWaitableEvent; | 43 class WebWaitableEvent; |
| 40 } | 44 } |
| 41 | 45 |
| 42 namespace blink { | 46 namespace blink { |
| 43 | 47 |
| 44 class KURL; | 48 class KURL; |
| 45 class WorkerGlobalScope; | 49 class WorkerGlobalScope; |
| 50 class WorkerInspectorController; |
| 46 class WorkerLoaderProxy; | 51 class WorkerLoaderProxy; |
| 47 class WorkerReportingProxy; | 52 class WorkerReportingProxy; |
| 53 class WorkerSharedTimer; |
| 54 class WorkerThreadShutdownFinishTask; |
| 48 class WorkerThreadStartupData; | 55 class WorkerThreadStartupData; |
| 49 class WorkerInspectorController; | 56 class WorkerThreadTask; |
| 50 | 57 |
| 51 enum WorkerThreadStartMode { DontPauseWorkerGlobalScopeOnStart, PauseWorkerG
lobalScopeOnStart }; | 58 enum WorkerThreadStartMode { DontPauseWorkerGlobalScopeOnStart, PauseWorkerG
lobalScopeOnStart }; |
| 52 | 59 |
| 60 |
| 53 class WorkerThread : public RefCounted<WorkerThread> { | 61 class WorkerThread : public RefCounted<WorkerThread> { |
| 54 public: | 62 public: |
| 55 virtual ~WorkerThread(); | 63 virtual ~WorkerThread(); |
| 56 | 64 |
| 57 bool start(); | 65 virtual void start(); |
| 58 void stop(); | 66 virtual void stop(); |
| 59 | 67 |
| 60 // Can be used to wait for this worker thread to shut down. | 68 // Can be used to wait for this worker thread to shut down. |
| 61 // (This is signalled on the main thread, so it's assumed to be waited o
n the worker context thread) | 69 // (This is signalled on the main thread, so it's assumed to be waited o
n the worker context thread) |
| 62 blink::WebWaitableEvent* shutdownEvent() { return m_shutdownEvent.get();
} | 70 blink::WebWaitableEvent* shutdownEvent() { return m_shutdownEvent.get();
} |
| 63 | 71 |
| 64 bool isCurrentThread() const; | 72 bool isCurrentThread() const; |
| 65 WorkerRunLoop& runLoop() { return m_runLoop; } | |
| 66 WorkerLoaderProxy& workerLoaderProxy() const { return m_workerLoaderProx
y; } | 73 WorkerLoaderProxy& workerLoaderProxy() const { return m_workerLoaderProx
y; } |
| 67 WorkerReportingProxy& workerReportingProxy() const { return m_workerRepo
rtingProxy; } | 74 WorkerReportingProxy& workerReportingProxy() const { return m_workerRepo
rtingProxy; } |
| 68 | 75 |
| 69 void postTask(PassOwnPtr<ExecutionContextTask>); | 76 void postTask(PassOwnPtr<ExecutionContextTask>); |
| 70 void postDebuggerTask(PassOwnPtr<ExecutionContextTask>); | 77 void postDebuggerTask(PassOwnPtr<ExecutionContextTask>); |
| 71 | 78 |
| 72 MessageQueueWaitResult runDebuggerTask(WorkerRunLoop::WaitMode = WorkerR
unLoop::WaitForMessage); | 79 enum WaitMode { WaitForMessage, DontWaitForMessage }; |
| 73 bool terminated() { return m_runLoop.terminated(); } | 80 MessageQueueWaitResult runDebuggerTask(WaitMode = WaitForMessage); |
| 81 |
| 82 // These methods should be called if the holder of the thread is |
| 83 // going to call runDebuggerTask in a loop. |
| 84 void willEnterNestedLoop(); |
| 85 void didLeaveNestedLoop(); |
| 86 |
| 87 blink::WebThread* webThread() { return m_thread.get(); } |
| 88 WorkerGlobalScope* workerGlobalScope() const { return m_workerGlobalScop
e.get(); } |
| 89 bool terminated() const { return m_terminated; } |
| 74 | 90 |
| 75 // Number of active worker threads. | 91 // Number of active worker threads. |
| 76 static unsigned workerThreadCount(); | 92 static unsigned workerThreadCount(); |
| 77 | 93 |
| 78 WorkerGlobalScope* workerGlobalScope() { return m_workerGlobalScope.get(
); } | |
| 79 | |
| 80 void interruptAndDispatchInspectorCommands(); | 94 void interruptAndDispatchInspectorCommands(); |
| 81 void setWorkerInspectorController(WorkerInspectorController*); | 95 void setWorkerInspectorController(WorkerInspectorController*); |
| 82 | 96 |
| 83 protected: | 97 protected: |
| 84 WorkerThread(WorkerLoaderProxy&, WorkerReportingProxy&, PassOwnPtrWillBe
RawPtr<WorkerThreadStartupData>); | 98 WorkerThread(WorkerLoaderProxy&, WorkerReportingProxy&, PassOwnPtrWillBe
RawPtr<WorkerThreadStartupData>); |
| 85 | 99 |
| 86 // Factory method for creating a new worker context for the thread. | 100 // Factory method for creating a new worker context for the thread. |
| 87 virtual PassRefPtrWillBeRawPtr<WorkerGlobalScope> createWorkerGlobalScop
e(PassOwnPtrWillBeRawPtr<WorkerThreadStartupData>) = 0; | 101 virtual PassRefPtrWillBeRawPtr<WorkerGlobalScope> createWorkerGlobalScop
e(PassOwnPtrWillBeRawPtr<WorkerThreadStartupData>) = 0; |
| 88 | 102 |
| 89 // Executes the event loop for the worker thread. Derived classes can ov
erride to perform actions before/after entering the event loop. | 103 virtual void postInitialize() { } |
| 90 virtual void runEventLoop(); | |
| 91 | 104 |
| 92 private: | 105 private: |
| 93 // Static function executed as the core routine on the new thread. Passe
d a pointer to a WorkerThread object. | 106 friend class WorkerThreadShutdownFinishTask; |
| 94 static void workerThreadStart(void*); | |
| 95 | 107 |
| 96 void workerThread(); | 108 void initialize(); |
| 109 void cleanup(); |
| 110 void idleHandler(); |
| 97 | 111 |
| 98 ThreadIdentifier m_threadID; | 112 bool m_terminated; |
| 99 WorkerRunLoop m_runLoop; | 113 OwnPtr<blink::WebThread> m_thread; |
| 114 OwnPtr<WorkerSharedTimer> m_sharedTimer; |
| 115 MessageQueue<WorkerThreadTask> m_debuggerMessageQueue; |
| 116 |
| 100 WorkerLoaderProxy& m_workerLoaderProxy; | 117 WorkerLoaderProxy& m_workerLoaderProxy; |
| 101 WorkerReportingProxy& m_workerReportingProxy; | 118 WorkerReportingProxy& m_workerReportingProxy; |
| 102 | 119 |
| 103 RefPtrWillBePersistent<WorkerGlobalScope> m_workerGlobalScope; | |
| 104 Mutex m_threadCreationMutex; | |
| 105 | |
| 106 RefPtrWillBePersistent<WorkerInspectorController> m_workerInspectorContr
oller; | 120 RefPtrWillBePersistent<WorkerInspectorController> m_workerInspectorContr
oller; |
| 107 Mutex m_workerInspectorControllerMutex; | 121 Mutex m_workerInspectorControllerMutex; |
| 108 | 122 |
| 123 Mutex m_threadCreationMutex; |
| 124 RefPtrWillBePersistent<WorkerGlobalScope> m_workerGlobalScope; |
| 109 OwnPtrWillBePersistent<WorkerThreadStartupData> m_startupData; | 125 OwnPtrWillBePersistent<WorkerThreadStartupData> m_startupData; |
| 110 | 126 |
| 127 OwnPtr<WeakPtrFactory<WorkerThread> > m_weakFactory; |
| 128 |
| 111 // Used to signal thread shutdown. | 129 // Used to signal thread shutdown. |
| 112 OwnPtr<blink::WebWaitableEvent> m_shutdownEvent; | 130 OwnPtr<blink::WebWaitableEvent> m_shutdownEvent; |
| 113 }; | 131 }; |
| 114 | 132 |
| 115 } // namespace blink | 133 } // namespace blink |
| 116 | 134 |
| 117 #endif // WorkerThread_h | 135 #endif // WorkerThread_h |
| OLD | NEW |