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 "nsIRunnable.h"
44 #include "nsIStreamLoader.h"
47 #include "nsIChannel.h"
52 #include "nsAutoPtr.h"
53 #include "nsAutoJSValHolder.h"
55 #include "nsStringGlue.h"
59 #include "nsDOMWorker.h"
64 * This class takes a list of script URLs, downloads the scripts, compiles the
65 * scripts, and then finally executes them. Due to platform limitations all
66 * network operations must happen on the main thread so this object sends events
67 * back and forth from the worker thread to the main thread. The flow goes like
70 * 1. (Worker thread) nsDOMWorkerScriptLoader created.
71 * 2. (Worker thread) LoadScript(s) called. Some simple argument validation is
72 * performed (currently limited to ensuring that all
73 * arguments are strings). nsDOMWorkerScriptLoader is then
74 * dispatched to the main thread.
75 * 3. (Main thread) Arguments validated as URIs, security checks performed,
76 * content policy consulted. Network loads begin.
77 * 4. (Necko thread) Necko stuff!
78 * 5. (Main thread) Completed downloads are packaged in a ScriptCompiler
79 * runnable and sent to the worker thread.
80 * 6. (Worker thread) ScriptCompiler runnables are processed (i.e. their
81 * scripts are compiled) in the order in which the necko
82 * downloads completed.
83 * 7. (Worker thread) After all loads complete and all compilation succeeds
84 * the scripts are executed in the order that the URLs were
85 * given to LoadScript(s).
87 * Currently if *anything* after 2 fails then we cancel any pending loads and
90 class nsDOMWorkerScriptLoader
: public nsDOMWorkerFeature
,
92 public nsIStreamLoaderObserver
94 friend class AutoSuspendWorkerEvents
;
95 friend class ScriptLoaderRunnable
;
98 NS_DECL_ISUPPORTS_INHERITED
100 NS_DECL_NSISTREAMLOADEROBSERVER
102 nsDOMWorkerScriptLoader(nsDOMWorker
* aWorker
);
104 nsresult
LoadScripts(JSContext
* aCx
,
105 const nsTArray
<nsString
>& aURLs
,
108 nsresult
LoadScript(JSContext
* aCx
,
109 const nsString
& aURL
,
112 virtual void Cancel();
115 ~nsDOMWorkerScriptLoader() { }
118 nsresult
DoRunLoop(JSContext
* aCx
);
119 nsresult
VerifyScripts(JSContext
* aCx
);
120 nsresult
ExecuteScripts(JSContext
* aCx
);
122 nsresult
RunInternal();
124 nsresult
OnStreamCompleteInternal(nsIStreamLoader
* aLoader
,
125 nsISupports
* aContext
,
128 const PRUint8
* aString
);
132 void SuspendWorkerEvents();
133 void ResumeWorkerEvents();
136 return mWorker
->Lock();
139 class ScriptLoaderRunnable
: public nsIRunnable
145 // Meant to be inherited.
146 ScriptLoaderRunnable(nsDOMWorkerScriptLoader
* aLoader
);
147 virtual ~ScriptLoaderRunnable();
156 nsDOMWorkerScriptLoader
* mLoader
;
159 class ScriptCompiler
: public ScriptLoaderRunnable
164 ScriptCompiler(nsDOMWorkerScriptLoader
* aLoader
,
165 const nsString
& aScriptText
,
166 const nsCString
& aFilename
,
167 nsAutoJSValHolder
& aScriptObj
);
170 nsString mScriptText
;
172 nsAutoJSValHolder
& mScriptObj
;
175 class ScriptLoaderDone
: public ScriptLoaderRunnable
180 ScriptLoaderDone(nsDOMWorkerScriptLoader
* aLoader
,
181 volatile PRBool
* aDoneFlag
);
184 volatile PRBool
* mDoneFlag
;
187 class AutoSuspendWorkerEvents
190 AutoSuspendWorkerEvents(nsDOMWorkerScriptLoader
* aLoader
);
191 ~AutoSuspendWorkerEvents();
194 nsDOMWorkerScriptLoader
* mLoader
;
197 struct ScriptLoadInfo
199 ScriptLoadInfo() : done(PR_FALSE
), result(NS_ERROR_NOT_INITIALIZED
) { }
205 nsCOMPtr
<nsIURI
> finalURI
;
206 nsCOMPtr
<nsIChannel
> channel
;
207 nsAutoJSValHolder scriptObj
;
212 nsRefPtr
<ScriptLoaderDone
> mDoneRunnable
;
214 PRUint32 mScriptCount
;
215 nsTArray
<ScriptLoadInfo
> mLoadInfos
;
217 // Protected by mWorker's lock!
218 nsTArray
<ScriptLoaderRunnable
*> mPendingRunnables
;
220 PRPackedBool mCanceled
;
221 PRPackedBool mForWorker
;
224 #endif /* __NSDOMWORKERSCRIPTLOADER_H__ */