WorkerRunnable.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "WorkerRunnable.h"
  6. #include "nsGlobalWindow.h"
  7. #include "nsIEventTarget.h"
  8. #include "nsIGlobalObject.h"
  9. #include "nsIRunnable.h"
  10. #include "nsThreadUtils.h"
  11. #include "mozilla/DebugOnly.h"
  12. #include "mozilla/ErrorResult.h"
  13. #include "mozilla/Unused.h"
  14. #include "mozilla/dom/ScriptSettings.h"
  15. #include "js/RootingAPI.h"
  16. #include "js/Value.h"
  17. #include "WorkerPrivate.h"
  18. #include "WorkerScope.h"
  19. using namespace mozilla;
  20. USING_WORKERS_NAMESPACE
  21. namespace {
  22. const nsIID kWorkerRunnableIID = {
  23. 0x320cc0b5, 0xef12, 0x4084, { 0x88, 0x6e, 0xca, 0x6a, 0x81, 0xe4, 0x1d, 0x68 }
  24. };
  25. } // namespace
  26. #ifdef DEBUG
  27. WorkerRunnable::WorkerRunnable(WorkerPrivate* aWorkerPrivate,
  28. TargetAndBusyBehavior aBehavior)
  29. : mWorkerPrivate(aWorkerPrivate), mBehavior(aBehavior), mCanceled(0),
  30. mCallingCancelWithinRun(false)
  31. {
  32. MOZ_ASSERT(aWorkerPrivate);
  33. }
  34. #endif
  35. bool
  36. WorkerRunnable::IsDebuggerRunnable() const
  37. {
  38. return false;
  39. }
  40. nsIGlobalObject*
  41. WorkerRunnable::DefaultGlobalObject() const
  42. {
  43. if (IsDebuggerRunnable()) {
  44. return mWorkerPrivate->DebuggerGlobalScope();
  45. } else {
  46. return mWorkerPrivate->GlobalScope();
  47. }
  48. }
  49. bool
  50. WorkerRunnable::PreDispatch(WorkerPrivate* aWorkerPrivate)
  51. {
  52. #ifdef DEBUG
  53. MOZ_ASSERT(aWorkerPrivate);
  54. switch (mBehavior) {
  55. case ParentThreadUnchangedBusyCount:
  56. aWorkerPrivate->AssertIsOnWorkerThread();
  57. break;
  58. case WorkerThreadModifyBusyCount:
  59. case WorkerThreadUnchangedBusyCount:
  60. aWorkerPrivate->AssertIsOnParentThread();
  61. break;
  62. default:
  63. MOZ_ASSERT_UNREACHABLE("Unknown behavior!");
  64. }
  65. #endif
  66. if (mBehavior == WorkerThreadModifyBusyCount) {
  67. return aWorkerPrivate->ModifyBusyCount(true);
  68. }
  69. return true;
  70. }
  71. bool
  72. WorkerRunnable::Dispatch()
  73. {
  74. bool ok = PreDispatch(mWorkerPrivate);
  75. if (ok) {
  76. ok = DispatchInternal();
  77. }
  78. PostDispatch(mWorkerPrivate, ok);
  79. return ok;
  80. }
  81. bool
  82. WorkerRunnable::DispatchInternal()
  83. {
  84. RefPtr<WorkerRunnable> runnable(this);
  85. if (mBehavior == WorkerThreadModifyBusyCount ||
  86. mBehavior == WorkerThreadUnchangedBusyCount) {
  87. if (IsDebuggerRunnable()) {
  88. return NS_SUCCEEDED(mWorkerPrivate->DispatchDebuggerRunnable(runnable.forget()));
  89. } else {
  90. return NS_SUCCEEDED(mWorkerPrivate->Dispatch(runnable.forget()));
  91. }
  92. }
  93. MOZ_ASSERT(mBehavior == ParentThreadUnchangedBusyCount);
  94. if (WorkerPrivate* parent = mWorkerPrivate->GetParent()) {
  95. return NS_SUCCEEDED(parent->Dispatch(runnable.forget()));
  96. }
  97. nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
  98. MOZ_ASSERT(mainThread);
  99. return NS_SUCCEEDED(mainThread->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL));
  100. }
  101. void
  102. WorkerRunnable::PostDispatch(WorkerPrivate* aWorkerPrivate,
  103. bool aDispatchResult)
  104. {
  105. MOZ_ASSERT(aWorkerPrivate);
  106. #ifdef DEBUG
  107. switch (mBehavior) {
  108. case ParentThreadUnchangedBusyCount:
  109. aWorkerPrivate->AssertIsOnWorkerThread();
  110. break;
  111. case WorkerThreadModifyBusyCount:
  112. aWorkerPrivate->AssertIsOnParentThread();
  113. break;
  114. case WorkerThreadUnchangedBusyCount:
  115. aWorkerPrivate->AssertIsOnParentThread();
  116. break;
  117. default:
  118. MOZ_ASSERT_UNREACHABLE("Unknown behavior!");
  119. }
  120. #endif
  121. if (!aDispatchResult) {
  122. if (mBehavior == WorkerThreadModifyBusyCount) {
  123. aWorkerPrivate->ModifyBusyCount(false);
  124. }
  125. }
  126. }
  127. bool
  128. WorkerRunnable::PreRun(WorkerPrivate* aWorkerPrivate)
  129. {
  130. return true;
  131. }
  132. void
  133. WorkerRunnable::PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
  134. bool aRunResult)
  135. {
  136. MOZ_ASSERT(aCx);
  137. MOZ_ASSERT(aWorkerPrivate);
  138. #ifdef DEBUG
  139. switch (mBehavior) {
  140. case ParentThreadUnchangedBusyCount:
  141. aWorkerPrivate->AssertIsOnParentThread();
  142. break;
  143. case WorkerThreadModifyBusyCount:
  144. aWorkerPrivate->AssertIsOnWorkerThread();
  145. break;
  146. case WorkerThreadUnchangedBusyCount:
  147. aWorkerPrivate->AssertIsOnWorkerThread();
  148. break;
  149. default:
  150. MOZ_ASSERT_UNREACHABLE("Unknown behavior!");
  151. }
  152. #endif
  153. if (mBehavior == WorkerThreadModifyBusyCount) {
  154. aWorkerPrivate->ModifyBusyCountFromWorker(false);
  155. }
  156. }
  157. // static
  158. WorkerRunnable*
  159. WorkerRunnable::FromRunnable(nsIRunnable* aRunnable)
  160. {
  161. MOZ_ASSERT(aRunnable);
  162. WorkerRunnable* runnable;
  163. nsresult rv = aRunnable->QueryInterface(kWorkerRunnableIID,
  164. reinterpret_cast<void**>(&runnable));
  165. if (NS_FAILED(rv)) {
  166. return nullptr;
  167. }
  168. MOZ_ASSERT(runnable);
  169. return runnable;
  170. }
  171. NS_IMPL_ADDREF(WorkerRunnable)
  172. NS_IMPL_RELEASE(WorkerRunnable)
  173. NS_INTERFACE_MAP_BEGIN(WorkerRunnable)
  174. NS_INTERFACE_MAP_ENTRY(nsIRunnable)
  175. NS_INTERFACE_MAP_ENTRY(nsICancelableRunnable)
  176. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIRunnable)
  177. // kWorkerRunnableIID is special in that it does not AddRef its result.
  178. if (aIID.Equals(kWorkerRunnableIID)) {
  179. *aInstancePtr = this;
  180. return NS_OK;
  181. }
  182. else
  183. NS_INTERFACE_MAP_END
  184. NS_IMETHODIMP
  185. WorkerRunnable::Run()
  186. {
  187. bool targetIsWorkerThread = mBehavior == WorkerThreadModifyBusyCount ||
  188. mBehavior == WorkerThreadUnchangedBusyCount;
  189. #ifdef DEBUG
  190. MOZ_ASSERT_IF(mCallingCancelWithinRun, targetIsWorkerThread);
  191. if (targetIsWorkerThread) {
  192. mWorkerPrivate->AssertIsOnWorkerThread();
  193. }
  194. else {
  195. MOZ_ASSERT(mBehavior == ParentThreadUnchangedBusyCount);
  196. mWorkerPrivate->AssertIsOnParentThread();
  197. }
  198. #endif
  199. if (IsCanceled() && !mCallingCancelWithinRun) {
  200. return NS_OK;
  201. }
  202. if (targetIsWorkerThread &&
  203. mWorkerPrivate->AllPendingRunnablesShouldBeCanceled() &&
  204. !IsCanceled() && !mCallingCancelWithinRun) {
  205. // Prevent recursion.
  206. mCallingCancelWithinRun = true;
  207. Cancel();
  208. MOZ_ASSERT(mCallingCancelWithinRun);
  209. mCallingCancelWithinRun = false;
  210. MOZ_ASSERT(IsCanceled(), "Subclass Cancel() didn't set IsCanceled()!");
  211. if (mBehavior == WorkerThreadModifyBusyCount) {
  212. mWorkerPrivate->ModifyBusyCountFromWorker(false);
  213. }
  214. return NS_OK;
  215. }
  216. bool result = PreRun(mWorkerPrivate);
  217. if (!result) {
  218. MOZ_ASSERT(targetIsWorkerThread,
  219. "The only PreRun implementation that can fail is "
  220. "ScriptExecutorRunnable");
  221. mWorkerPrivate->AssertIsOnWorkerThread();
  222. MOZ_ASSERT(!JS_IsExceptionPending(mWorkerPrivate->GetJSContext()));
  223. // We can't enter a useful compartment on the JSContext here; just pass it
  224. // in as-is.
  225. PostRun(mWorkerPrivate->GetJSContext(), mWorkerPrivate, false);
  226. return NS_ERROR_FAILURE;
  227. }
  228. // Track down the appropriate global, if any, to use for the AutoEntryScript.
  229. nsCOMPtr<nsIGlobalObject> globalObject;
  230. bool isMainThread = !targetIsWorkerThread && !mWorkerPrivate->GetParent();
  231. MOZ_ASSERT(isMainThread == NS_IsMainThread());
  232. RefPtr<WorkerPrivate> kungFuDeathGrip;
  233. if (targetIsWorkerThread) {
  234. JSContext* cx = GetCurrentThreadJSContext();
  235. if (NS_WARN_IF(!cx)) {
  236. return NS_ERROR_FAILURE;
  237. }
  238. JSObject* global = JS::CurrentGlobalOrNull(cx);
  239. if (global) {
  240. globalObject = xpc::NativeGlobal(global);
  241. } else {
  242. globalObject = DefaultGlobalObject();
  243. }
  244. // We may still not have a globalObject here: in the case of
  245. // CompileScriptRunnable, we don't actually create the global object until
  246. // we have the script data, which happens in a syncloop under
  247. // CompileScriptRunnable::WorkerRun, so we can't assert that it got created
  248. // in the PreRun call above.
  249. } else {
  250. kungFuDeathGrip = mWorkerPrivate;
  251. if (isMainThread) {
  252. globalObject = nsGlobalWindow::Cast(mWorkerPrivate->GetWindow());
  253. } else {
  254. globalObject = mWorkerPrivate->GetParent()->GlobalScope();
  255. }
  256. }
  257. // We might run script as part of WorkerRun, so we need an AutoEntryScript.
  258. // This is part of the HTML spec for workers at:
  259. // http://www.whatwg.org/specs/web-apps/current-work/#run-a-worker
  260. // If we don't have a globalObject we have to use an AutoJSAPI instead, but
  261. // this is OK as we won't be running script in these circumstances.
  262. Maybe<mozilla::dom::AutoJSAPI> maybeJSAPI;
  263. Maybe<mozilla::dom::AutoEntryScript> aes;
  264. JSContext* cx;
  265. AutoJSAPI* jsapi;
  266. if (globalObject) {
  267. aes.emplace(globalObject, "Worker runnable", isMainThread);
  268. jsapi = aes.ptr();
  269. cx = aes->cx();
  270. } else {
  271. maybeJSAPI.emplace();
  272. maybeJSAPI->Init();
  273. jsapi = maybeJSAPI.ptr();
  274. cx = jsapi->cx();
  275. }
  276. // Note that we can't assert anything about mWorkerPrivate->GetWrapper()
  277. // existing, since it may in fact have been GCed (and we may be one of the
  278. // runnables cleaning up the worker as a result).
  279. // If we are on the parent thread and that thread is not the main thread,
  280. // then we must be a dedicated worker (because there are no
  281. // Shared/ServiceWorkers whose parent is itself a worker) and then we
  282. // definitely have a globalObject. If it _is_ the main thread, globalObject
  283. // can be null for workers started from JSMs or other non-window contexts,
  284. // sadly.
  285. MOZ_ASSERT_IF(!targetIsWorkerThread && !isMainThread,
  286. mWorkerPrivate->IsDedicatedWorker() && globalObject);
  287. // If we're on the parent thread we might be in a null compartment in the
  288. // situation described above when globalObject is null. Make sure to enter
  289. // the compartment of the worker's reflector if there is one. There might
  290. // not be one if we're just starting to compile the script for this worker.
  291. Maybe<JSAutoCompartment> ac;
  292. if (!targetIsWorkerThread && mWorkerPrivate->GetWrapper()) {
  293. // If we're on the parent thread and have a reflector and a globalObject,
  294. // then the compartments of cx, globalObject, and the worker's reflector
  295. // should all match.
  296. MOZ_ASSERT_IF(globalObject,
  297. js::GetObjectCompartment(mWorkerPrivate->GetWrapper()) ==
  298. js::GetContextCompartment(cx));
  299. MOZ_ASSERT_IF(globalObject,
  300. js::GetObjectCompartment(mWorkerPrivate->GetWrapper()) ==
  301. js::GetObjectCompartment(globalObject->GetGlobalJSObject()));
  302. // If we're on the parent thread and have a reflector, then our
  303. // JSContext had better be either in the null compartment (and hence
  304. // have no globalObject) or in the compartment of our reflector.
  305. MOZ_ASSERT(!js::GetContextCompartment(cx) ||
  306. js::GetObjectCompartment(mWorkerPrivate->GetWrapper()) ==
  307. js::GetContextCompartment(cx),
  308. "Must either be in the null compartment or in our reflector "
  309. "compartment");
  310. ac.emplace(cx, mWorkerPrivate->GetWrapper());
  311. }
  312. MOZ_ASSERT(!jsapi->HasException());
  313. result = WorkerRun(cx, mWorkerPrivate);
  314. MOZ_ASSERT_IF(result, !jsapi->HasException());
  315. jsapi->ReportException();
  316. // We can't even assert that this didn't create our global, since in the case
  317. // of CompileScriptRunnable it _does_.
  318. // It would be nice to avoid passing a JSContext to PostRun, but in the case
  319. // of ScriptExecutorRunnable we need to know the current compartment on the
  320. // JSContext (the one we set up based on the global returned from PreRun) so
  321. // that we can sanely do exception reporting. In particular, we want to make
  322. // sure that we do our JS_SetPendingException while still in that compartment,
  323. // because otherwise we might end up trying to create a cross-compartment
  324. // wrapper when we try to move the JS exception from our runnable's
  325. // ErrorResult to the JSContext, and that's not desirable in this case.
  326. //
  327. // We _could_ skip passing a JSContext here and then in
  328. // ScriptExecutorRunnable::PostRun end up grabbing it from the WorkerPrivate
  329. // and looking at its current compartment. But that seems like slightly weird
  330. // action-at-a-distance...
  331. //
  332. // In any case, we do NOT try to change the compartment on the JSContext at
  333. // this point; in the one case in which we could do that
  334. // (CompileScriptRunnable) it actually doesn't matter which compartment we're
  335. // in for PostRun.
  336. PostRun(cx, mWorkerPrivate, result);
  337. MOZ_ASSERT(!jsapi->HasException());
  338. return result ? NS_OK : NS_ERROR_FAILURE;
  339. }
  340. nsresult
  341. WorkerRunnable::Cancel()
  342. {
  343. uint32_t canceledCount = ++mCanceled;
  344. MOZ_ASSERT(canceledCount, "Cancel() overflow!");
  345. // The docs say that Cancel() should not be called more than once and that we
  346. // should throw NS_ERROR_UNEXPECTED if it is.
  347. return (canceledCount == 1) ? NS_OK : NS_ERROR_UNEXPECTED;
  348. }
  349. void
  350. WorkerDebuggerRunnable::PostDispatch(WorkerPrivate* aWorkerPrivate,
  351. bool aDispatchResult)
  352. {
  353. }
  354. WorkerSyncRunnable::WorkerSyncRunnable(WorkerPrivate* aWorkerPrivate,
  355. nsIEventTarget* aSyncLoopTarget)
  356. : WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
  357. mSyncLoopTarget(aSyncLoopTarget)
  358. {
  359. #ifdef DEBUG
  360. if (mSyncLoopTarget) {
  361. mWorkerPrivate->AssertValidSyncLoop(mSyncLoopTarget);
  362. }
  363. #endif
  364. }
  365. WorkerSyncRunnable::WorkerSyncRunnable(
  366. WorkerPrivate* aWorkerPrivate,
  367. already_AddRefed<nsIEventTarget>&& aSyncLoopTarget)
  368. : WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
  369. mSyncLoopTarget(aSyncLoopTarget)
  370. {
  371. #ifdef DEBUG
  372. if (mSyncLoopTarget) {
  373. mWorkerPrivate->AssertValidSyncLoop(mSyncLoopTarget);
  374. }
  375. #endif
  376. }
  377. WorkerSyncRunnable::~WorkerSyncRunnable()
  378. {
  379. }
  380. bool
  381. WorkerSyncRunnable::DispatchInternal()
  382. {
  383. if (mSyncLoopTarget) {
  384. RefPtr<WorkerSyncRunnable> runnable(this);
  385. return NS_SUCCEEDED(mSyncLoopTarget->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL));
  386. }
  387. return WorkerRunnable::DispatchInternal();
  388. }
  389. void
  390. MainThreadWorkerSyncRunnable::PostDispatch(WorkerPrivate* aWorkerPrivate,
  391. bool aDispatchResult)
  392. {
  393. }
  394. MainThreadStopSyncLoopRunnable::MainThreadStopSyncLoopRunnable(
  395. WorkerPrivate* aWorkerPrivate,
  396. already_AddRefed<nsIEventTarget>&& aSyncLoopTarget,
  397. bool aResult)
  398. : WorkerSyncRunnable(aWorkerPrivate, Move(aSyncLoopTarget)), mResult(aResult)
  399. {
  400. AssertIsOnMainThread();
  401. #ifdef DEBUG
  402. mWorkerPrivate->AssertValidSyncLoop(mSyncLoopTarget);
  403. #endif
  404. }
  405. nsresult
  406. MainThreadStopSyncLoopRunnable::Cancel()
  407. {
  408. nsresult rv = Run();
  409. NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Run() failed");
  410. nsresult rv2 = WorkerSyncRunnable::Cancel();
  411. NS_WARNING_ASSERTION(NS_SUCCEEDED(rv2), "Cancel() failed");
  412. return NS_FAILED(rv) ? rv : rv2;
  413. }
  414. bool
  415. MainThreadStopSyncLoopRunnable::WorkerRun(JSContext* aCx,
  416. WorkerPrivate* aWorkerPrivate)
  417. {
  418. aWorkerPrivate->AssertIsOnWorkerThread();
  419. MOZ_ASSERT(mSyncLoopTarget);
  420. nsCOMPtr<nsIEventTarget> syncLoopTarget;
  421. mSyncLoopTarget.swap(syncLoopTarget);
  422. aWorkerPrivate->StopSyncLoop(syncLoopTarget, mResult);
  423. return true;
  424. }
  425. bool
  426. MainThreadStopSyncLoopRunnable::DispatchInternal()
  427. {
  428. MOZ_ASSERT(mSyncLoopTarget);
  429. RefPtr<MainThreadStopSyncLoopRunnable> runnable(this);
  430. return NS_SUCCEEDED(mSyncLoopTarget->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL));
  431. }
  432. void
  433. MainThreadStopSyncLoopRunnable::PostDispatch(WorkerPrivate* aWorkerPrivate,
  434. bool aDispatchResult)
  435. {
  436. }
  437. #ifdef DEBUG
  438. WorkerControlRunnable::WorkerControlRunnable(WorkerPrivate* aWorkerPrivate,
  439. TargetAndBusyBehavior aBehavior)
  440. : WorkerRunnable(aWorkerPrivate, aBehavior)
  441. {
  442. MOZ_ASSERT(aWorkerPrivate);
  443. MOZ_ASSERT(aBehavior == ParentThreadUnchangedBusyCount ||
  444. aBehavior == WorkerThreadUnchangedBusyCount,
  445. "WorkerControlRunnables should not modify the busy count");
  446. }
  447. #endif
  448. nsresult
  449. WorkerControlRunnable::Cancel()
  450. {
  451. if (NS_FAILED(Run())) {
  452. NS_WARNING("WorkerControlRunnable::Run() failed.");
  453. }
  454. return WorkerRunnable::Cancel();
  455. }
  456. bool
  457. WorkerControlRunnable::DispatchInternal()
  458. {
  459. RefPtr<WorkerControlRunnable> runnable(this);
  460. if (mBehavior == WorkerThreadUnchangedBusyCount) {
  461. return NS_SUCCEEDED(mWorkerPrivate->DispatchControlRunnable(runnable.forget()));
  462. }
  463. if (WorkerPrivate* parent = mWorkerPrivate->GetParent()) {
  464. return NS_SUCCEEDED(parent->DispatchControlRunnable(runnable.forget()));
  465. }
  466. nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
  467. MOZ_ASSERT(mainThread);
  468. return NS_SUCCEEDED(mainThread->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL));
  469. }
  470. NS_IMPL_ISUPPORTS_INHERITED0(WorkerControlRunnable, WorkerRunnable)
  471. WorkerMainThreadRunnable::WorkerMainThreadRunnable(WorkerPrivate* aWorkerPrivate,
  472. const nsACString& aTelemetryKey)
  473. : mWorkerPrivate(aWorkerPrivate)
  474. , mTelemetryKey(aTelemetryKey)
  475. {
  476. mWorkerPrivate->AssertIsOnWorkerThread();
  477. }
  478. void
  479. WorkerMainThreadRunnable::Dispatch(Status aFailStatus, ErrorResult& aRv)
  480. {
  481. mWorkerPrivate->AssertIsOnWorkerThread();
  482. AutoSyncLoopHolder syncLoop(mWorkerPrivate, aFailStatus);
  483. mSyncLoopTarget = syncLoop.GetEventTarget();
  484. if (!mSyncLoopTarget) {
  485. // SyncLoop creation can fail if the worker is shutting down.
  486. aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
  487. return;
  488. }
  489. RefPtr<WorkerMainThreadRunnable> runnable(this);
  490. DebugOnly<nsresult> rv =
  491. NS_DispatchToMainThread(runnable.forget(), NS_DISPATCH_NORMAL);
  492. MOZ_ASSERT(NS_SUCCEEDED(rv),
  493. "Should only fail after xpcom-shutdown-threads and we're gone by then");
  494. if (!syncLoop.Run()) {
  495. aRv.ThrowUncatchableException();
  496. }
  497. }
  498. NS_IMETHODIMP
  499. WorkerMainThreadRunnable::Run()
  500. {
  501. AssertIsOnMainThread();
  502. bool runResult = MainThreadRun();
  503. RefPtr<MainThreadStopSyncLoopRunnable> response =
  504. new MainThreadStopSyncLoopRunnable(mWorkerPrivate,
  505. mSyncLoopTarget.forget(),
  506. runResult);
  507. MOZ_ALWAYS_TRUE(response->Dispatch());
  508. return NS_OK;
  509. }
  510. WorkerCheckAPIExposureOnMainThreadRunnable::WorkerCheckAPIExposureOnMainThreadRunnable(WorkerPrivate* aWorkerPrivate):
  511. WorkerMainThreadRunnable(aWorkerPrivate,
  512. NS_LITERAL_CSTRING("WorkerCheckAPIExposureOnMainThread"))
  513. {}
  514. WorkerCheckAPIExposureOnMainThreadRunnable::~WorkerCheckAPIExposureOnMainThreadRunnable()
  515. {}
  516. bool
  517. WorkerCheckAPIExposureOnMainThreadRunnable::Dispatch()
  518. {
  519. ErrorResult rv;
  520. WorkerMainThreadRunnable::Dispatch(Terminating, rv);
  521. bool ok = !rv.Failed();
  522. rv.SuppressException();
  523. return ok;
  524. }
  525. bool
  526. WorkerSameThreadRunnable::PreDispatch(WorkerPrivate* aWorkerPrivate)
  527. {
  528. // We don't call WorkerRunnable::PreDispatch, because we're using
  529. // WorkerThreadModifyBusyCount for mBehavior, and WorkerRunnable will assert
  530. // that PreDispatch is on the parent thread in that case.
  531. aWorkerPrivate->AssertIsOnWorkerThread();
  532. return true;
  533. }
  534. void
  535. WorkerSameThreadRunnable::PostDispatch(WorkerPrivate* aWorkerPrivate,
  536. bool aDispatchResult)
  537. {
  538. // We don't call WorkerRunnable::PostDispatch, because we're using
  539. // WorkerThreadModifyBusyCount for mBehavior, and WorkerRunnable will assert
  540. // that PostDispatch is on the parent thread in that case.
  541. aWorkerPrivate->AssertIsOnWorkerThread();
  542. if (aDispatchResult) {
  543. DebugOnly<bool> willIncrement = aWorkerPrivate->ModifyBusyCountFromWorker(true);
  544. // Should never fail since if this thread is still running, so should the
  545. // parent and it should be able to process a control runnable.
  546. MOZ_ASSERT(willIncrement);
  547. }
  548. }
  549. WorkerProxyToMainThreadRunnable::WorkerProxyToMainThreadRunnable(WorkerPrivate* aWorkerPrivate)
  550. : mWorkerPrivate(aWorkerPrivate)
  551. {
  552. MOZ_ASSERT(mWorkerPrivate);
  553. mWorkerPrivate->AssertIsOnWorkerThread();
  554. }
  555. WorkerProxyToMainThreadRunnable::~WorkerProxyToMainThreadRunnable()
  556. {}
  557. bool
  558. WorkerProxyToMainThreadRunnable::Dispatch()
  559. {
  560. mWorkerPrivate->AssertIsOnWorkerThread();
  561. if (NS_WARN_IF(!HoldWorker())) {
  562. RunBackOnWorkerThread();
  563. return false;
  564. }
  565. if (NS_WARN_IF(NS_FAILED(NS_DispatchToMainThread(this)))) {
  566. ReleaseWorker();
  567. RunBackOnWorkerThread();
  568. return false;
  569. }
  570. return true;
  571. }
  572. NS_IMETHODIMP
  573. WorkerProxyToMainThreadRunnable::Run()
  574. {
  575. AssertIsOnMainThread();
  576. RunOnMainThread();
  577. PostDispatchOnMainThread();
  578. return NS_OK;
  579. }
  580. void
  581. WorkerProxyToMainThreadRunnable::PostDispatchOnMainThread()
  582. {
  583. class ReleaseRunnable final : public MainThreadWorkerControlRunnable
  584. {
  585. RefPtr<WorkerProxyToMainThreadRunnable> mRunnable;
  586. public:
  587. ReleaseRunnable(WorkerPrivate* aWorkerPrivate,
  588. WorkerProxyToMainThreadRunnable* aRunnable)
  589. : MainThreadWorkerControlRunnable(aWorkerPrivate)
  590. , mRunnable(aRunnable)
  591. {
  592. MOZ_ASSERT(aRunnable);
  593. }
  594. // We must call RunBackOnWorkerThread() also if the runnable is canceled.
  595. nsresult
  596. Cancel() override
  597. {
  598. WorkerRun(nullptr, mWorkerPrivate);
  599. return MainThreadWorkerControlRunnable::Cancel();
  600. }
  601. virtual bool
  602. WorkerRun(JSContext* aCx, workers::WorkerPrivate* aWorkerPrivate) override
  603. {
  604. MOZ_ASSERT(aWorkerPrivate);
  605. aWorkerPrivate->AssertIsOnWorkerThread();
  606. if (mRunnable) {
  607. mRunnable->RunBackOnWorkerThread();
  608. // Let's release the worker thread.
  609. mRunnable->ReleaseWorker();
  610. mRunnable = nullptr;
  611. }
  612. return true;
  613. }
  614. private:
  615. ~ReleaseRunnable()
  616. {}
  617. };
  618. RefPtr<WorkerControlRunnable> runnable =
  619. new ReleaseRunnable(mWorkerPrivate, this);
  620. Unused << NS_WARN_IF(!runnable->Dispatch());
  621. }
  622. bool
  623. WorkerProxyToMainThreadRunnable::HoldWorker()
  624. {
  625. mWorkerPrivate->AssertIsOnWorkerThread();
  626. MOZ_ASSERT(!mWorkerHolder);
  627. class SimpleWorkerHolder final : public WorkerHolder
  628. {
  629. public:
  630. bool Notify(Status aStatus) override
  631. {
  632. // We don't care about the notification. We just want to keep the
  633. // mWorkerPrivate alive.
  634. return true;
  635. }
  636. };
  637. UniquePtr<WorkerHolder> workerHolder(new SimpleWorkerHolder());
  638. if (NS_WARN_IF(!workerHolder->HoldWorker(mWorkerPrivate, Canceling))) {
  639. return false;
  640. }
  641. mWorkerHolder = Move(workerHolder);
  642. return true;
  643. }
  644. void
  645. WorkerProxyToMainThreadRunnable::ReleaseWorker()
  646. {
  647. mWorkerPrivate->AssertIsOnWorkerThread();
  648. MOZ_ASSERT(mWorkerHolder);
  649. mWorkerHolder = nullptr;
  650. }