1 /* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is worker threads.
17 * The Initial Developer of the Original Code is
18 * Mozilla Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2008
20 * the Initial Developer. All Rights Reserved.
23 * Ben Turner <bent.mozilla@gmail.com> (Original Author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef __NSDOMWORKERSCRIPTLOADER_H__
40 #define __NSDOMWORKERSCRIPTLOADER_H__
43 #include "nsThreadUtils.h"
44 #include "nsIStreamLoader.h"
47 #include "nsIChannel.h"
52 #include "nsAutoPtr.h"
53 #include "nsAutoJSObjectHolder.h"
55 #include "nsStringGlue.h"
60 #include "nsDOMWorkerThread.h"
63 * This class takes a list of script URLs, downloads the scripts, compiles the
64 * scripts, and then finally executes them. Due to platform limitations all
65 * network operations must happen on the main thread so this object sends events
66 * back and forth from the worker thread to the main thread. The flow goes like
69 * 1. (Worker thread) nsDOMWorkerScriptLoader created.
70 * 2. (Worker thread) LoadScript(s) called. Some simple argument validation is
71 * performed (currently limited to ensuring that all
72 * arguments are strings). nsDOMWorkerScriptLoader is then
73 * dispatched to the main thread.
74 * 3. (Main thread) Arguments validated as URIs, security checks performed,
75 * content policy consulted. Network loads begin.
76 * 4. (Necko thread) Necko stuff!
77 * 5. (Main thread) Completed downloads are packaged in a ScriptCompiler
78 * runnable and sent to the worker thread.
79 * 6. (Worker thread) ScriptCompiler runnables are processed (i.e. their
80 * scripts are compiled) in the order in which the necko
81 * downloads completed.
82 * 7. (Worker thread) After all loads complete and all compilation succeeds
83 * the scripts are executed in the order that the URLs were
84 * given to LoadScript(s).
86 * Currently if *anything* after 2 fails then we cancel any pending loads and
89 class nsDOMWorkerScriptLoader
: public nsRunnable
,
90 public nsIStreamLoaderObserver
92 friend class AutoSuspendWorkerEvents
;
93 friend class nsDOMWorkerFunctions
;
94 friend class nsDOMWorkerThread
;
95 friend class ScriptLoaderRunnable
;
98 NS_DECL_ISUPPORTS_INHERITED
100 NS_DECL_NSISTREAMLOADEROBSERVER
102 nsDOMWorkerScriptLoader();
104 nsresult
LoadScripts(nsDOMWorkerThread
* aWorker
,
106 const nsTArray
<nsString
>& aURLs
);
108 nsresult
LoadScript(nsDOMWorkerThread
* aWorker
,
110 const nsString
& aURL
);
115 ~nsDOMWorkerScriptLoader();
117 nsresult
DoRunLoop();
118 nsresult
VerifyScripts();
119 nsresult
ExecuteScripts();
121 nsresult
RunInternal();
123 nsresult
OnStreamCompleteInternal(nsIStreamLoader
* aLoader
,
124 nsISupports
* aContext
,
127 const PRUint8
* aString
);
131 void SuspendWorkerEvents();
132 void ResumeWorkerEvents();
135 return mWorker
->Lock();
138 class ScriptLoaderRunnable
: public nsRunnable
141 // Meant to be inherited.
142 ScriptLoaderRunnable(nsDOMWorkerScriptLoader
* aLoader
);
143 virtual ~ScriptLoaderRunnable();
152 nsDOMWorkerScriptLoader
* mLoader
;
155 class ScriptCompiler
: public ScriptLoaderRunnable
160 ScriptCompiler(nsDOMWorkerScriptLoader
* aLoader
,
162 const nsString
& aScriptText
,
163 const nsCString
& aFilename
,
164 nsAutoJSObjectHolder
& aScriptObj
);
168 nsString mScriptText
;
170 nsAutoJSObjectHolder
& mScriptObj
;
173 class ScriptLoaderDone
: public ScriptLoaderRunnable
178 ScriptLoaderDone(nsDOMWorkerScriptLoader
* aLoader
,
179 volatile PRBool
* aDoneFlag
);
182 volatile PRBool
* mDoneFlag
;
185 class AutoSuspendWorkerEvents
188 AutoSuspendWorkerEvents(nsDOMWorkerScriptLoader
* aLoader
);
189 ~AutoSuspendWorkerEvents();
192 nsDOMWorkerScriptLoader
* mLoader
;
195 struct ScriptLoadInfo
197 ScriptLoadInfo() : done(PR_FALSE
), result(NS_ERROR_NOT_INITIALIZED
) { }
203 nsCOMPtr
<nsIURI
> finalURI
;
204 nsCOMPtr
<nsIChannel
> channel
;
205 nsAutoJSObjectHolder scriptObj
;
208 nsDOMWorkerThread
* mWorker
;
212 nsRefPtr
<ScriptLoaderDone
> mDoneRunnable
;
214 PRUint32 mScriptCount
;
215 nsTArray
<ScriptLoadInfo
> mLoadInfos
;
217 PRPackedBool mCanceled
;
218 PRPackedBool mTrackedByWorker
;
220 // Protected by mWorker's lock!
221 nsTArray
<ScriptLoaderRunnable
*> mPendingRunnables
;
224 #endif /* __NSDOMWORKERSCRIPTLOADER_H__ */