1 : /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 : /* vim: set ts=2 et sw=2 tw=80: */
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is Web Workers.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * The Mozilla Foundation.
20 : * Portions created by the Initial Developer are Copyright (C) 2011
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Ben Turner <bent.mozilla@gmail.com> (Original Author)
25 : *
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.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef mozilla_dom_workers_runtimeservice_h__
41 : #define mozilla_dom_workers_runtimeservice_h__
42 :
43 : #include "Workers.h"
44 :
45 : #include "nsIObserver.h"
46 :
47 : #include "jsapi.h"
48 : #include "mozilla/Mutex.h"
49 : #include "mozilla/TimeStamp.h"
50 : #include "nsAutoPtr.h"
51 : #include "nsClassHashtable.h"
52 : #include "nsCOMPtr.h"
53 : #include "nsHashKeys.h"
54 : #include "nsStringGlue.h"
55 : #include "nsTArray.h"
56 :
57 : class nsIThread;
58 : class nsITimer;
59 : class nsPIDOMWindow;
60 :
61 : BEGIN_WORKERS_NAMESPACE
62 :
63 : class WorkerPrivate;
64 :
65 : class RuntimeService : public nsIObserver
66 : {
67 : struct WorkerDomainInfo
68 0 : {
69 : nsCString mDomain;
70 : nsTArray<WorkerPrivate*> mActiveWorkers;
71 : nsTArray<WorkerPrivate*> mQueuedWorkers;
72 : PRUint32 mChildWorkerCount;
73 :
74 0 : WorkerDomainInfo() : mActiveWorkers(1), mChildWorkerCount(0) { }
75 :
76 : PRUint32
77 0 : ActiveWorkerCount() const
78 : {
79 0 : return mActiveWorkers.Length() + mChildWorkerCount;
80 : }
81 : };
82 :
83 : struct IdleThreadInfo
84 0 : {
85 : nsCOMPtr<nsIThread> mThread;
86 : mozilla::TimeStamp mExpirationTime;
87 : };
88 :
89 : mozilla::Mutex mMutex;
90 :
91 : // Protected by mMutex.
92 : nsClassHashtable<nsCStringHashKey, WorkerDomainInfo> mDomainMap;
93 :
94 : // Protected by mMutex.
95 : nsTArray<IdleThreadInfo> mIdleThreadArray;
96 :
97 : // *Not* protected by mMutex.
98 : nsClassHashtable<nsVoidPtrHashKey, nsTArray<WorkerPrivate*> > mWindowMap;
99 :
100 : // Only used on the main thread.
101 : nsCOMPtr<nsITimer> mIdleThreadTimer;
102 :
103 : nsCString mDetectorName;
104 : nsCString mSystemCharset;
105 :
106 : static PRUint32 sDefaultJSContextOptions;
107 : static PRUint32 sDefaultJSRuntimeHeapSize;
108 : static PRInt32 sCloseHandlerTimeoutSeconds;
109 :
110 : #ifdef JS_GC_ZEAL
111 : static PRUint8 sDefaultGCZeal;
112 : #endif
113 :
114 : public:
115 : struct NavigatorStrings
116 0 : {
117 : nsString mAppName;
118 : nsString mAppVersion;
119 : nsString mPlatform;
120 : nsString mUserAgent;
121 : };
122 :
123 : private:
124 : NavigatorStrings mNavigatorStrings;
125 :
126 : // True when the observer service holds a reference to this object.
127 : bool mObserved;
128 : bool mShuttingDown;
129 : bool mNavigatorStringsLoaded;
130 :
131 : public:
132 : NS_DECL_ISUPPORTS
133 : NS_DECL_NSIOBSERVER
134 :
135 : static RuntimeService*
136 : GetOrCreateService();
137 :
138 : static RuntimeService*
139 : GetService();
140 :
141 : bool
142 : RegisterWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate);
143 :
144 : void
145 : UnregisterWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate);
146 :
147 : void
148 : CancelWorkersForWindow(JSContext* aCx, nsPIDOMWindow* aWindow);
149 :
150 : void
151 : SuspendWorkersForWindow(JSContext* aCx, nsPIDOMWindow* aWindow);
152 :
153 : void
154 : ResumeWorkersForWindow(JSContext* aCx, nsPIDOMWindow* aWindow);
155 :
156 : const nsACString&
157 0 : GetDetectorName() const
158 : {
159 0 : return mDetectorName;
160 : }
161 :
162 : const nsACString&
163 0 : GetSystemCharset() const
164 : {
165 0 : return mSystemCharset;
166 : }
167 :
168 : const NavigatorStrings&
169 0 : GetNavigatorStrings() const
170 : {
171 0 : return mNavigatorStrings;
172 : }
173 :
174 : void
175 : NoteIdleThread(nsIThread* aThread);
176 :
177 : static PRUint32
178 0 : GetDefaultJSContextOptions()
179 : {
180 0 : AssertIsOnMainThread();
181 0 : return sDefaultJSContextOptions;
182 : }
183 :
184 : static void
185 0 : SetDefaultJSContextOptions(PRUint32 aOptions)
186 : {
187 0 : AssertIsOnMainThread();
188 0 : sDefaultJSContextOptions = aOptions;
189 0 : }
190 :
191 : void
192 : UpdateAllWorkerJSContextOptions();
193 :
194 : static PRUint32
195 0 : GetDefaultJSRuntimeHeapSize()
196 : {
197 0 : AssertIsOnMainThread();
198 0 : return sDefaultJSRuntimeHeapSize;
199 : }
200 :
201 : static void
202 0 : SetDefaultJSRuntimeHeapSize(PRUint32 aMaxBytes)
203 : {
204 0 : AssertIsOnMainThread();
205 0 : sDefaultJSRuntimeHeapSize = aMaxBytes;
206 0 : }
207 :
208 : void
209 : UpdateAllWorkerJSRuntimeHeapSize();
210 :
211 : static PRUint32
212 0 : GetCloseHandlerTimeoutSeconds()
213 : {
214 0 : return sCloseHandlerTimeoutSeconds > 0 ? sCloseHandlerTimeoutSeconds : 0;
215 : }
216 :
217 : #ifdef JS_GC_ZEAL
218 : static PRUint8
219 0 : GetDefaultGCZeal()
220 : {
221 0 : AssertIsOnMainThread();
222 0 : return sDefaultGCZeal;
223 : }
224 :
225 : static void
226 0 : SetDefaultGCZeal(PRUint8 aGCZeal)
227 : {
228 0 : AssertIsOnMainThread();
229 0 : sDefaultGCZeal = aGCZeal;
230 0 : }
231 :
232 : void
233 : UpdateAllWorkerGCZeal();
234 : #endif
235 :
236 : void
237 : GarbageCollectAllWorkers(bool aShrinking);
238 :
239 : class AutoSafeJSContext
240 : {
241 : JSContext* mContext;
242 :
243 : public:
244 : AutoSafeJSContext(JSContext* aCx = nsnull);
245 : ~AutoSafeJSContext();
246 :
247 0 : operator JSContext*() const
248 : {
249 0 : return mContext;
250 : }
251 :
252 : static JSContext*
253 : GetSafeContext();
254 : };
255 :
256 : private:
257 : RuntimeService();
258 : ~RuntimeService();
259 :
260 : nsresult
261 : Init();
262 :
263 : void
264 : Cleanup();
265 :
266 : static PLDHashOperator
267 : AddAllTopLevelWorkersToArray(const nsACString& aKey,
268 : WorkerDomainInfo* aData,
269 : void* aUserArg);
270 :
271 : void
272 : GetWorkersForWindow(nsPIDOMWindow* aWindow,
273 : nsTArray<WorkerPrivate*>& aWorkers);
274 :
275 : bool
276 : ScheduleWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate);
277 :
278 : static void
279 : ShutdownIdleThreads(nsITimer* aTimer, void* aClosure);
280 : };
281 :
282 : END_WORKERS_NAMESPACE
283 :
284 : #endif /* mozilla_dom_workers_runtimeservice_h__ */
|