Bug 462525 - username truncation code is unnecessarily duplicated in nsLoginManagerP...
[wine-gecko.git] / dom / src / threads / nsDOMWorkerScriptLoader.h
blobeb19fc039f64c686ac9334784200e563c52845cf
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
13 * License.
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.
22 * Contributor(s):
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__
42 // Bases
43 #include "nsThreadUtils.h"
44 #include "nsIStreamLoader.h"
46 // Interfaces
47 #include "nsIChannel.h"
48 #include "nsIURI.h"
50 // Other includes
51 #include "jsapi.h"
52 #include "nsAutoPtr.h"
53 #include "nsAutoJSObjectHolder.h"
54 #include "nsCOMPtr.h"
55 #include "nsStringGlue.h"
56 #include "nsTArray.h"
57 #include "prlock.h"
59 // DOMWorker includes
60 #include "nsDOMWorkerThread.h"
62 /**
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
67 * this:
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
87 * bail out entirely.
89 class nsDOMWorkerScriptLoader : public nsRunnable,
90 public nsIStreamLoaderObserver
92 friend class AutoSuspendWorkerEvents;
93 friend class nsDOMWorkerFunctions;
94 friend class nsDOMWorkerThread;
95 friend class ScriptLoaderRunnable;
97 public:
98 NS_DECL_ISUPPORTS_INHERITED
99 NS_DECL_NSIRUNNABLE
100 NS_DECL_NSISTREAMLOADEROBSERVER
102 nsDOMWorkerScriptLoader();
104 nsresult LoadScripts(nsDOMWorkerThread* aWorker,
105 JSContext* aCx,
106 const nsTArray<nsString>& aURLs);
108 nsresult LoadScript(nsDOMWorkerThread* aWorker,
109 JSContext* aCx,
110 const nsString& aURL);
112 void Cancel();
114 private:
115 ~nsDOMWorkerScriptLoader();
117 nsresult DoRunLoop();
118 nsresult VerifyScripts();
119 nsresult ExecuteScripts();
121 nsresult RunInternal();
123 nsresult OnStreamCompleteInternal(nsIStreamLoader* aLoader,
124 nsISupports* aContext,
125 nsresult aStatus,
126 PRUint32 aStringLen,
127 const PRUint8* aString);
129 void NotifyDone();
131 void SuspendWorkerEvents();
132 void ResumeWorkerEvents();
134 PRLock* Lock() {
135 return mWorker->Lock();
138 class ScriptLoaderRunnable : public nsRunnable
140 protected:
141 // Meant to be inherited.
142 ScriptLoaderRunnable(nsDOMWorkerScriptLoader* aLoader);
143 virtual ~ScriptLoaderRunnable();
145 public:
146 void Revoke();
148 protected:
149 PRBool mRevoked;
151 private:
152 nsDOMWorkerScriptLoader* mLoader;
155 class ScriptCompiler : public ScriptLoaderRunnable
157 public:
158 NS_DECL_NSIRUNNABLE
160 ScriptCompiler(nsDOMWorkerScriptLoader* aLoader,
161 JSContext* aCx,
162 const nsString& aScriptText,
163 const nsCString& aFilename,
164 nsAutoJSObjectHolder& aScriptObj);
166 private:
167 JSContext* mCx;
168 nsString mScriptText;
169 nsCString mFilename;
170 nsAutoJSObjectHolder& mScriptObj;
173 class ScriptLoaderDone : public ScriptLoaderRunnable
175 public:
176 NS_DECL_NSIRUNNABLE
178 ScriptLoaderDone(nsDOMWorkerScriptLoader* aLoader,
179 volatile PRBool* aDoneFlag);
181 private:
182 volatile PRBool* mDoneFlag;
185 class AutoSuspendWorkerEvents
187 public:
188 AutoSuspendWorkerEvents(nsDOMWorkerScriptLoader* aLoader);
189 ~AutoSuspendWorkerEvents();
191 private:
192 nsDOMWorkerScriptLoader* mLoader;
195 struct ScriptLoadInfo
197 ScriptLoadInfo() : done(PR_FALSE), result(NS_ERROR_NOT_INITIALIZED) { }
199 nsString url;
200 nsString scriptText;
201 PRBool done;
202 nsresult result;
203 nsCOMPtr<nsIURI> finalURI;
204 nsCOMPtr<nsIChannel> channel;
205 nsAutoJSObjectHolder scriptObj;
208 nsDOMWorkerThread* mWorker;
209 nsIThread* mTarget;
210 JSContext* mCx;
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__ */