IndexedDB: ASSERT hit calling open from callback in Worker
[WebKit-https.git] / Source / WebCore / workers / WorkerThread.cpp
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 #include "config.h"
28
29 #if ENABLE(WORKERS)
30
31 #include "WorkerThread.h"
32
33 #include "DatabaseContext.h"
34 #include "DedicatedWorkerContext.h"
35 #include "InspectorInstrumentation.h"
36 #include "KURL.h"
37 #include "PlatformString.h"
38 #include "ScriptSourceCode.h"
39 #include "ScriptValue.h"
40 #include "ThreadGlobalData.h"
41
42 #include <utility>
43 #include <wtf/Noncopyable.h>
44
45 #if ENABLE(SQL_DATABASE)
46 #include "DatabaseTask.h"
47 #include "DatabaseTracker.h"
48 #endif
49
50 #if PLATFORM(CHROMIUM)
51 #include <public/Platform.h>
52 #include <public/WebWorkerRunLoop.h>
53 #endif
54
55 namespace WebCore {
56
57 static Mutex& threadCountMutex()
58 {
59     AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
60     return mutex;
61 }
62
63 unsigned WorkerThread::m_threadCount = 0;
64
65 unsigned WorkerThread::workerThreadCount()
66 {
67     MutexLocker lock(threadCountMutex());
68     return m_threadCount;
69 }
70
71 struct WorkerThreadStartupData {
72     WTF_MAKE_NONCOPYABLE(WorkerThreadStartupData); WTF_MAKE_FAST_ALLOCATED;
73 public:
74     static PassOwnPtr<WorkerThreadStartupData> create(const KURL& scriptURL, const String& userAgent, const GroupSettings* settings, const String& sourceCode, WorkerThreadStartMode startMode, const String& contentSecurityPolicy, ContentSecurityPolicy::HeaderType contentSecurityPolicyType)
75     {
76         return adoptPtr(new WorkerThreadStartupData(scriptURL, userAgent, settings, sourceCode, startMode, contentSecurityPolicy, contentSecurityPolicyType));
77     }
78
79     KURL m_scriptURL;
80     String m_userAgent;
81     OwnPtr<GroupSettings> m_groupSettings;
82     String m_sourceCode;
83     WorkerThreadStartMode m_startMode;
84     String m_contentSecurityPolicy;
85     ContentSecurityPolicy::HeaderType m_contentSecurityPolicyType;
86 private:
87     WorkerThreadStartupData(const KURL& scriptURL, const String& userAgent, const GroupSettings*, const String& sourceCode, WorkerThreadStartMode, const String& contentSecurityPolicy, ContentSecurityPolicy::HeaderType contentSecurityPolicyType);
88 };
89
90 WorkerThreadStartupData::WorkerThreadStartupData(const KURL& scriptURL, const String& userAgent, const GroupSettings* settings, const String& sourceCode, WorkerThreadStartMode startMode, const String& contentSecurityPolicy, ContentSecurityPolicy::HeaderType contentSecurityPolicyType)
91     : m_scriptURL(scriptURL.copy())
92     , m_userAgent(userAgent.isolatedCopy())
93     , m_sourceCode(sourceCode.isolatedCopy())
94     , m_startMode(startMode)
95     , m_contentSecurityPolicy(contentSecurityPolicy.isolatedCopy())
96     , m_contentSecurityPolicyType(contentSecurityPolicyType)
97 {
98     if (!settings)
99         return;
100
101     m_groupSettings = GroupSettings::create();
102     m_groupSettings->setLocalStorageQuotaBytes(settings->localStorageQuotaBytes());
103     m_groupSettings->setIndexedDBQuotaBytes(settings->indexedDBQuotaBytes());
104     m_groupSettings->setIndexedDBDatabasePath(settings->indexedDBDatabasePath().isolatedCopy());
105 }
106
107 WorkerThread::WorkerThread(const KURL& scriptURL, const String& userAgent, const GroupSettings* settings, const String& sourceCode, WorkerLoaderProxy& workerLoaderProxy, WorkerReportingProxy& workerReportingProxy, WorkerThreadStartMode startMode, const String& contentSecurityPolicy, ContentSecurityPolicy::HeaderType contentSecurityPolicyType)
108     : m_threadID(0)
109     , m_workerLoaderProxy(workerLoaderProxy)
110     , m_workerReportingProxy(workerReportingProxy)
111     , m_startupData(WorkerThreadStartupData::create(scriptURL, userAgent, settings, sourceCode, startMode, contentSecurityPolicy, contentSecurityPolicyType))
112 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
113     , m_notificationClient(0)
114 #endif
115 {
116     MutexLocker lock(threadCountMutex());
117     m_threadCount++;
118 }
119
120 WorkerThread::~WorkerThread()
121 {
122     MutexLocker lock(threadCountMutex());
123     ASSERT(m_threadCount > 0);
124     m_threadCount--;
125 }
126
127 bool WorkerThread::start()
128 {
129     // Mutex protection is necessary to ensure that m_threadID is initialized when the thread starts.
130     MutexLocker lock(m_threadCreationMutex);
131
132     if (m_threadID)
133         return true;
134
135     m_threadID = createThread(WorkerThread::workerThreadStart, this, "WebCore: Worker");
136
137     return m_threadID;
138 }
139
140 void WorkerThread::workerThreadStart(void* thread)
141 {
142     static_cast<WorkerThread*>(thread)->workerThread();
143 }
144
145 void WorkerThread::workerThread()
146 {
147     {
148         MutexLocker lock(m_threadCreationMutex);
149         m_workerContext = createWorkerContext(m_startupData->m_scriptURL, m_startupData->m_userAgent, m_startupData->m_groupSettings.release(), m_startupData->m_contentSecurityPolicy, m_startupData->m_contentSecurityPolicyType);
150
151         if (m_runLoop.terminated()) {
152             // The worker was terminated before the thread had a chance to run. Since the context didn't exist yet,
153             // forbidExecution() couldn't be called from stop().
154            m_workerContext->script()->forbidExecution();
155         }
156     }
157 #if PLATFORM(CHROMIUM)
158     // The corresponding call to didStopWorkerRunLoop is in
159     // ~WorkerScriptController.
160     WebKit::Platform::current()->didStartWorkerRunLoop(WebKit::WebWorkerRunLoop(&m_runLoop));
161 #endif
162
163     WorkerScriptController* script = m_workerContext->script();
164 #if ENABLE(INSPECTOR)
165     InspectorInstrumentation::willEvaluateWorkerScript(workerContext(), m_startupData->m_startMode);
166 #endif
167     script->evaluate(ScriptSourceCode(m_startupData->m_sourceCode, m_startupData->m_scriptURL));
168     // Free the startup data to cause its member variable deref's happen on the worker's thread (since
169     // all ref/derefs of these objects are happening on the thread at this point). Note that
170     // WorkerThread::~WorkerThread happens on a different thread where it was created.
171     m_startupData.clear();
172
173     runEventLoop();
174
175     ThreadIdentifier threadID = m_threadID;
176
177     ASSERT(m_workerContext->hasOneRef());
178
179     // The below assignment will destroy the context, which will in turn notify messaging proxy.
180     // We cannot let any objects survive past thread exit, because no other thread will run GC or otherwise destroy them.
181     m_workerContext = 0;
182
183     // Clean up WebCore::ThreadGlobalData before WTF::WTFThreadData goes away!
184     threadGlobalData().destroy();
185
186     // The thread object may be already destroyed from notification now, don't try to access "this".
187     detachThread(threadID);
188 }
189
190 void WorkerThread::runEventLoop()
191 {
192     // Does not return until terminated.
193     m_runLoop.run(m_workerContext.get());
194 }
195
196 class WorkerThreadShutdownFinishTask : public ScriptExecutionContext::Task {
197 public:
198     static PassOwnPtr<WorkerThreadShutdownFinishTask> create()
199     {
200         return adoptPtr(new WorkerThreadShutdownFinishTask());
201     }
202
203     virtual void performTask(ScriptExecutionContext *context)
204     {
205         ASSERT(context->isWorkerContext());
206         WorkerContext* workerContext = static_cast<WorkerContext*>(context);
207 #if ENABLE(INSPECTOR)
208         workerContext->clearInspector();
209 #endif
210         // It's not safe to call clearScript until all the cleanup tasks posted by functions initiated by WorkerThreadShutdownStartTask have completed.
211         workerContext->clearScript();
212     }
213
214     virtual bool isCleanupTask() const { return true; }
215 };
216
217 class WorkerThreadShutdownStartTask : public ScriptExecutionContext::Task {
218 public:
219     static PassOwnPtr<WorkerThreadShutdownStartTask> create()
220     {
221         return adoptPtr(new WorkerThreadShutdownStartTask());
222     }
223
224     virtual void performTask(ScriptExecutionContext *context)
225     {
226         ASSERT(context->isWorkerContext());
227         WorkerContext* workerContext = static_cast<WorkerContext*>(context);
228
229 #if ENABLE(SQL_DATABASE)
230         // FIXME: Should we stop the databases as part of stopActiveDOMObjects() below?
231         DatabaseTaskSynchronizer cleanupSync;
232         DatabaseContext::stopDatabases(workerContext, &cleanupSync);
233 #endif
234
235         workerContext->stopActiveDOMObjects();
236
237         workerContext->notifyObserversOfStop();
238
239         // Event listeners would keep DOMWrapperWorld objects alive for too long. Also, they have references to JS objects,
240         // which become dangling once Heap is destroyed.
241         workerContext->removeAllEventListeners();
242
243 #if ENABLE(SQL_DATABASE)
244         // We wait for the database thread to clean up all its stuff so that we
245         // can do more stringent leak checks as we exit.
246         cleanupSync.waitForTaskCompletion();
247 #endif
248
249         // Stick a shutdown command at the end of the queue, so that we deal
250         // with all the cleanup tasks the databases post first.
251         workerContext->postTask(WorkerThreadShutdownFinishTask::create());
252     }
253
254     virtual bool isCleanupTask() const { return true; }
255 };
256
257 void WorkerThread::stop()
258 {
259     // Mutex protection is necessary because stop() can be called before the context is fully created.
260     MutexLocker lock(m_threadCreationMutex);
261
262     // Ensure that tasks are being handled by thread event loop. If script execution weren't forbidden, a while(1) loop in JS could keep the thread alive forever.
263     if (m_workerContext) {
264         m_workerContext->script()->scheduleExecutionTermination();
265
266 #if ENABLE(SQL_DATABASE)
267         DatabaseTracker::tracker().interruptAllDatabasesForContext(m_workerContext.get());
268 #endif
269         m_runLoop.postTask(WorkerThreadShutdownStartTask::create());
270     }
271     m_runLoop.terminate();
272 }
273
274 } // namespace WebCore
275
276 #endif // ENABLE(WORKERS)