Bug 458160 - Enable downloadable .otf fonts under Windows. r=roc, sr=vlad.
[wine-gecko.git] / dom / src / threads / nsDOMWorkerScriptLoader.h
blob2e71fc45086d9f45957cd3279a2296d98ef4c0c5
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 "nsIRunnable.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 "nsAutoJSValHolder.h"
54 #include "nsCOMPtr.h"
55 #include "nsStringGlue.h"
56 #include "nsTArray.h"
57 #include "prlock.h"
59 #include "nsDOMWorker.h"
61 class nsIThread;
63 /**
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
68 * this:
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
88 * bail out entirely.
90 class nsDOMWorkerScriptLoader : public nsDOMWorkerFeature,
91 public nsIRunnable,
92 public nsIStreamLoaderObserver
94 friend class AutoSuspendWorkerEvents;
95 friend class ScriptLoaderRunnable;
97 public:
98 NS_DECL_ISUPPORTS_INHERITED
99 NS_DECL_NSIRUNNABLE
100 NS_DECL_NSISTREAMLOADEROBSERVER
102 nsDOMWorkerScriptLoader(nsDOMWorker* aWorker);
104 nsresult LoadScripts(JSContext* aCx,
105 const nsTArray<nsString>& aURLs,
106 PRBool aForWorker);
108 nsresult LoadScript(JSContext* aCx,
109 const nsString& aURL,
110 PRBool aForWorker);
112 virtual void Cancel();
114 private:
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,
126 nsresult aStatus,
127 PRUint32 aStringLen,
128 const PRUint8* aString);
130 void NotifyDone();
132 void SuspendWorkerEvents();
133 void ResumeWorkerEvents();
135 PRLock* Lock() {
136 return mWorker->Lock();
139 class ScriptLoaderRunnable : public nsIRunnable
141 public:
142 NS_DECL_ISUPPORTS
144 protected:
145 // Meant to be inherited.
146 ScriptLoaderRunnable(nsDOMWorkerScriptLoader* aLoader);
147 virtual ~ScriptLoaderRunnable();
149 public:
150 void Revoke();
152 protected:
153 PRBool mRevoked;
155 private:
156 nsDOMWorkerScriptLoader* mLoader;
159 class ScriptCompiler : public ScriptLoaderRunnable
161 public:
162 NS_DECL_NSIRUNNABLE
164 ScriptCompiler(nsDOMWorkerScriptLoader* aLoader,
165 const nsString& aScriptText,
166 const nsCString& aFilename,
167 nsAutoJSValHolder& aScriptObj);
169 private:
170 nsString mScriptText;
171 nsCString mFilename;
172 nsAutoJSValHolder& mScriptObj;
175 class ScriptLoaderDone : public ScriptLoaderRunnable
177 public:
178 NS_DECL_NSIRUNNABLE
180 ScriptLoaderDone(nsDOMWorkerScriptLoader* aLoader,
181 volatile PRBool* aDoneFlag);
183 private:
184 volatile PRBool* mDoneFlag;
187 class AutoSuspendWorkerEvents
189 public:
190 AutoSuspendWorkerEvents(nsDOMWorkerScriptLoader* aLoader);
191 ~AutoSuspendWorkerEvents();
193 private:
194 nsDOMWorkerScriptLoader* mLoader;
197 struct ScriptLoadInfo
199 ScriptLoadInfo() : done(PR_FALSE), result(NS_ERROR_NOT_INITIALIZED) { }
201 nsString url;
202 nsString scriptText;
203 PRBool done;
204 nsresult result;
205 nsCOMPtr<nsIURI> finalURI;
206 nsCOMPtr<nsIChannel> channel;
207 nsAutoJSValHolder scriptObj;
210 nsIThread* mTarget;
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__ */