Bug 450449 - " Implement 'importScripts' for worker threads". r=jst+mrbkap, sr=jst.
[wine-gecko.git] / dom / src / threads / nsDOMWorkerThread.h
blobe2e21d9f385e4380d45e2c087965d8970a4e37a6
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 * Vladimir Vukicevic <vladimir@pobox.com> (Original Author)
24 * Ben Turner <bent.mozilla@gmail.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef __NSDOMWORKERTHREAD_H__
41 #define __NSDOMWORKERTHREAD_H__
43 // Bases
44 #include "nsDOMWorkerBase.h"
45 #include "nsIClassInfo.h"
46 #include "nsIDOMThreads.h"
48 // Other includes
49 #include "jsapi.h"
50 #include "nsAutoJSObjectHolder.h"
51 #include "nsCOMPtr.h"
52 #include "nsStringGlue.h"
53 #include "nsTArray.h"
54 #include "nsThreadUtils.h"
55 #include "prclist.h"
56 #include "prlock.h"
58 // DOMWorker includes
59 #include "nsDOMThreadService.h"
61 // Macro to generate nsIClassInfo methods for these threadsafe DOM classes
62 #define NS_IMPL_THREADSAFE_DOM_CI(_class) \
63 NS_IMETHODIMP \
64 _class::GetInterfaces(PRUint32* _count, nsIID*** _array) \
65 { \
66 return NS_CI_INTERFACE_GETTER_NAME(_class)(_count, _array); \
67 } \
69 NS_IMETHODIMP \
70 _class::GetHelperForLanguage(PRUint32 _language, nsISupports** _retval) \
71 { \
72 *_retval = nsnull; \
73 return NS_OK; \
74 } \
76 NS_IMETHODIMP \
77 _class::GetContractID(char** _contractID) \
78 { \
79 *_contractID = nsnull; \
80 return NS_OK; \
81 } \
83 NS_IMETHODIMP \
84 _class::GetClassDescription(char** _classDescription) \
85 { \
86 *_classDescription = nsnull; \
87 return NS_OK; \
88 } \
90 NS_IMETHODIMP \
91 _class::GetClassID(nsCID** _classID) \
92 { \
93 *_classID = nsnull; \
94 return NS_OK; \
95 } \
97 NS_IMETHODIMP \
98 _class::GetImplementationLanguage(PRUint32* _language) \
99 { \
100 *_language = nsIProgrammingLanguage::CPLUSPLUS; \
101 return NS_OK; \
104 NS_IMETHODIMP \
105 _class::GetFlags(PRUint32* _flags) \
107 *_flags = nsIClassInfo::THREADSAFE | nsIClassInfo::DOM_OBJECT; \
108 return NS_OK; \
111 NS_IMETHODIMP \
112 _class::GetClassIDNoAlloc(nsCID* _classIDNoAlloc) \
114 return NS_ERROR_NOT_AVAILABLE; \
117 class nsDOMWorkerPool;
118 class nsDOMWorkerTimeout;
120 class nsDOMWorkerThread : public nsDOMWorkerBase,
121 public nsIDOMWorkerThread,
122 public nsIClassInfo
124 friend class nsDOMCreateJSContextRunnable;
125 friend class nsDOMWorkerFunctions;
126 friend class nsDOMWorkerPool;
127 friend class nsDOMWorkerRunnable;
128 friend class nsDOMWorkerScriptLoader;
129 friend class nsDOMWorkerTimeout;
131 friend JSBool DOMWorkerOperationCallback(JSContext* aCx);
133 public:
134 NS_DECL_ISUPPORTS
135 NS_DECL_NSIDOMWORKERTHREAD
136 NS_DECL_NSICLASSINFO
138 nsDOMWorkerThread(nsDOMWorkerPool* aPool,
139 const nsAString& aSource,
140 PRBool aSourceIsURL);
142 virtual nsDOMWorkerPool* Pool() {
143 NS_ASSERTION(!IsCanceled(), "Don't touch Pool after we've been canceled!");
144 return mPool;
147 private:
148 virtual ~nsDOMWorkerThread();
150 nsresult Init();
152 // For nsDOMWorkerBase
153 virtual nsresult HandleMessage(const nsAString& aMessage,
154 nsDOMWorkerBase* aSourceThread);
156 // For nsDOMWorkerBase
157 virtual nsresult DispatchMessage(nsIRunnable* aRunnable);
159 virtual void Cancel();
160 virtual void Suspend();
161 virtual void Resume();
163 PRBool SetGlobalForContext(JSContext* aCx);
164 PRBool CompileGlobalObject(JSContext* aCx);
166 inline nsDOMWorkerTimeout* FirstTimeout();
167 inline nsDOMWorkerTimeout* NextTimeout(nsDOMWorkerTimeout* aTimeout);
169 void AddTimeout(nsDOMWorkerTimeout* aTimeout);
170 void RemoveTimeout(nsDOMWorkerTimeout* aTimeout);
171 void ClearTimeouts();
172 void CancelTimeout(PRUint32 aId);
173 void SuspendTimeouts();
174 void ResumeTimeouts();
176 void CancelScriptLoaders();
178 PRLock* Lock() {
179 return mLock;
182 nsDOMWorkerPool* mPool;
183 nsString mSource;
184 nsString mSourceURL;
186 nsAutoJSObjectHolder mGlobal;
187 PRBool mCompiled;
189 PRUint32 mCallbackCount;
191 PRUint32 mNextTimeoutId;
193 PRLock* mLock;
194 PRCList mTimeouts;
196 nsTArray<nsDOMWorkerScriptLoader*> mScriptLoaders;
199 #endif /* __NSDOMWORKERTHREAD_H__ */