2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
|
2018-11-21 23:02:51 +03:00
|
|
|
#include <utility>
|
|
|
|
|
2018-08-25 06:51:49 +03:00
|
|
|
#include "js/CompilationAndEvaluation.h"
|
2018-08-21 01:11:32 +03:00
|
|
|
#include "js/LocaleSensitive.h"
|
2012-01-11 12:23:08 +04:00
|
|
|
#include "js/MemoryMetrics.h"
|
2018-11-09 05:42:48 +03:00
|
|
|
#include "js/SourceText.h"
|
2018-01-30 12:10:25 +03:00
|
|
|
#include "MessageEventRunnable.h"
|
2018-04-17 21:51:04 +03:00
|
|
|
#include "mozilla/ScopeExit.h"
|
2018-06-19 17:14:06 +03:00
|
|
|
#include "mozilla/StaticPrefs.h"
|
2018-06-22 17:22:59 +03:00
|
|
|
#include "mozilla/dom/BlobURLProtocolHandler.h"
|
2017-11-21 23:13:04 +03:00
|
|
|
#include "mozilla/dom/ClientManager.h"
|
|
|
|
#include "mozilla/dom/ClientSource.h"
|
2018-01-12 04:46:08 +03:00
|
|
|
#include "mozilla/dom/ClientState.h"
|
2016-03-24 00:55:07 +03:00
|
|
|
#include "mozilla/dom/Console.h"
|
2018-01-30 12:14:00 +03:00
|
|
|
#include "mozilla/dom/DOMTypes.h"
|
|
|
|
#include "mozilla/dom/Event.h"
|
2013-11-05 18:16:26 +04:00
|
|
|
#include "mozilla/dom/FunctionBinding.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
#include "mozilla/dom/IndexedDatabaseManager.h"
|
2014-02-27 14:51:14 +04:00
|
|
|
#include "mozilla/dom/MessageEvent.h"
|
2013-11-05 18:16:26 +04:00
|
|
|
#include "mozilla/dom/MessageEventBinding.h"
|
2015-06-17 13:44:27 +03:00
|
|
|
#include "mozilla/dom/MessagePort.h"
|
|
|
|
#include "mozilla/dom/MessagePortBinding.h"
|
2017-02-14 18:06:38 +03:00
|
|
|
#include "mozilla/dom/nsCSPUtils.h"
|
2016-06-09 20:04:42 +03:00
|
|
|
#include "mozilla/dom/Performance.h"
|
2018-01-24 19:17:32 +03:00
|
|
|
#include "mozilla/dom/PerformanceStorageWorker.h"
|
2015-04-10 18:27:57 +03:00
|
|
|
#include "mozilla/dom/PromiseDebugging.h"
|
2018-11-20 02:18:33 +03:00
|
|
|
#include "mozilla/dom/RemoteWorkerChild.h"
|
2018-01-31 10:20:25 +03:00
|
|
|
#include "mozilla/dom/WorkerBinding.h"
|
Bug 1382922 - Refactor event queue to allow multiple implementations (r=erahm)
This patch refactors the nsThread event queue to clean it up and to make it easier to restructure. The fundamental concepts are as follows:
Each nsThread will have a pointer to a refcounted SynchronizedEventQueue. A SynchronizedEQ takes care of doing the locking and condition variable work when posting and popping events. For the actual storage of events, it delegates to an AbstractEventQueue data structure. It keeps a UniquePtr to the AbstractEventQueue that it uses for storage.
Both SynchronizedEQ and AbstractEventQueue are abstract classes. There is only one concrete implementation of SynchronizedEQ in this patch, which is called ThreadEventQueue. ThreadEventQueue uses locks and condition variables to post and pop events the same way nsThread does. It also encapsulates the functionality that DOM workers need to implement their special event loops (PushEventQueue and PopEventQueue). In later Quantum DOM work, I plan to have another SynchronizedEQ implementation for the main thread, called SchedulerEventQueue. It will have special code for the cooperatively scheduling threads in Quantum DOM.
There are two concrete implementations of AbstractEventQueue in this patch: EventQueue and PrioritizedEventQueue. EventQueue replaces the old nsEventQueue. The other AbstractEventQueue implementation is PrioritizedEventQueue, which uses multiple queues for different event priorities.
The final major piece here is ThreadEventTarget, which splits some of the code for posting events out of nsThread. Eventually, my plan is for multiple cooperatively scheduled nsThreads to be able to share a ThreadEventTarget. In this patch, though, each nsThread has its own ThreadEventTarget. The class's purpose is just to collect some related code together.
One final note: I tried to avoid virtual dispatch overhead as much as possible. Calls to SynchronizedEQ methods do use virtual dispatch, since I plan to use different implementations for different threads with Quantum DOM. But all the calls to EventQueue methods should be non-virtual. Although the methods are declared virtual, all the classes used are final and the concrete classes involved should all be known through templatization.
MozReview-Commit-ID: 9Evtr9oIJvx
2017-06-21 05:42:13 +03:00
|
|
|
#include "mozilla/ThreadEventQueue.h"
|
2016-11-07 23:30:17 +03:00
|
|
|
#include "mozilla/ThrottledEventQueue.h"
|
2015-10-22 00:10:05 +03:00
|
|
|
#include "mozilla/TimelineConsumers.h"
|
|
|
|
#include "mozilla/WorkerTimelineMarker.h"
|
2015-03-18 21:36:03 +03:00
|
|
|
#include "nsCycleCollector.h"
|
2018-07-09 13:26:59 +03:00
|
|
|
#include "nsGlobalWindowInner.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsNetUtil.h"
|
2018-01-30 12:14:00 +03:00
|
|
|
#include "nsIMemoryReporter.h"
|
|
|
|
#include "nsIPermissionManager.h"
|
2018-03-13 20:36:34 +03:00
|
|
|
#include "nsIRandomGenerator.h"
|
2018-01-30 12:14:00 +03:00
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#include "nsIScriptTimeoutHandler.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIURL.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2015-04-15 19:47:03 +03:00
|
|
|
#include "nsQueryObject.h"
|
2018-03-13 20:36:34 +03:00
|
|
|
#include "nsRFPService.h"
|
2012-08-20 22:34:32 +04:00
|
|
|
#include "nsSandboxFlags.h"
|
2016-08-14 14:39:31 +03:00
|
|
|
#include "nsUTF8Utils.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#include "RuntimeService.h"
|
|
|
|
#include "ScriptLoader.h"
|
2018-01-27 00:08:58 +03:00
|
|
|
#include "mozilla/dom/ServiceWorkerEvents.h"
|
2018-01-27 00:08:59 +03:00
|
|
|
#include "mozilla/dom/ServiceWorkerManager.h"
|
2018-07-10 19:53:03 +03:00
|
|
|
#include "WorkerCSPEventListener.h"
|
2018-01-30 12:12:26 +03:00
|
|
|
#include "WorkerDebugger.h"
|
2014-10-27 20:00:05 +03:00
|
|
|
#include "WorkerDebuggerManager.h"
|
2018-01-30 12:12:50 +03:00
|
|
|
#include "WorkerError.h"
|
2018-01-31 10:20:25 +03:00
|
|
|
#include "WorkerEventTarget.h"
|
2016-04-06 23:27:22 +03:00
|
|
|
#include "WorkerNavigator.h"
|
2018-06-18 23:37:21 +03:00
|
|
|
#include "WorkerRef.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
#include "WorkerRunnable.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "WorkerScope.h"
|
2014-11-17 22:55:37 +03:00
|
|
|
#include "WorkerThread.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-01-30 12:14:00 +03:00
|
|
|
#include "nsThreadManager.h"
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#undef PostMessage
|
|
|
|
#endif
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// JS_MaybeGC will run once every second during normal execution.
|
|
|
|
#define PERIODIC_GC_TIMER_DELAY_SEC 1
|
2012-01-18 00:05:25 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// A shrinking GC will run five seconds after the last event is processed.
|
|
|
|
#define IDLE_GC_TIMER_DELAY_SEC 5
|
2012-01-18 00:05:25 +04:00
|
|
|
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
static mozilla::LazyLogModule sWorkerPrivateLog("WorkerPrivate");
|
|
|
|
static mozilla::LazyLogModule sWorkerTimeoutsLog("WorkerTimeouts");
|
|
|
|
|
|
|
|
mozilla::LogModule* WorkerLog() { return sWorkerPrivateLog; }
|
|
|
|
|
|
|
|
mozilla::LogModule* TimeoutsLog() { return sWorkerTimeoutsLog; }
|
|
|
|
|
2018-03-06 12:19:19 +03:00
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
#define LOG(log, _args) MOZ_LOG(log, LogLevel::Debug, _args);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-01-31 10:25:30 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace ipc;
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
using namespace workerinternals;
|
2014-12-17 09:26:15 +03:00
|
|
|
|
2013-12-08 09:38:32 +04:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(JsWorkerMallocSizeOf)
|
2012-07-13 10:51:01 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
namespace {
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
const nsIID kDEBUGWorkerEventTargetIID = {
|
|
|
|
0xccaba3fa,
|
|
|
|
0x5be2,
|
|
|
|
0x4de2,
|
|
|
|
{0xba, 0x87, 0x3b, 0x3b, 0x5b, 0x1d, 0x5, 0xfb}};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
template <class T>
|
|
|
|
class AutoPtrComparator {
|
|
|
|
typedef nsAutoPtr<T> A;
|
|
|
|
typedef T* B;
|
|
|
|
|
|
|
|
public:
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Equals(const A& a, const B& b) const {
|
2011-07-17 23:09:13 +04:00
|
|
|
return a && b ? *a == *b : !a && !b ? true : false;
|
|
|
|
}
|
2011-09-29 10:19:26 +04:00
|
|
|
bool LessThan(const A& a, const B& b) const {
|
2011-07-17 23:09:13 +04:00
|
|
|
return a && b ? *a < *b : b ? true : false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline AutoPtrComparator<T> GetAutoPtrComparator(
|
|
|
|
const nsTArray<nsAutoPtr<T>>&) {
|
|
|
|
return AutoPtrComparator<T>();
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// This class is used to wrap any runnables that the worker receives via the
|
|
|
|
// nsIEventTarget::Dispatch() method (either from NS_DispatchToCurrentThread or
|
|
|
|
// from the worker's EventTarget).
|
2015-03-21 19:28:04 +03:00
|
|
|
class ExternalRunnableWrapper final : public WorkerRunnable {
|
2016-04-11 21:40:06 +03:00
|
|
|
nsCOMPtr<nsIRunnable> mWrappedRunnable;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
ExternalRunnableWrapper(WorkerPrivate* aWorkerPrivate,
|
2016-04-11 21:40:06 +03:00
|
|
|
nsIRunnable* aWrappedRunnable)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
|
|
|
mWrappedRunnable(aWrappedRunnable) {
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
MOZ_ASSERT(aWrappedRunnable);
|
|
|
|
}
|
|
|
|
|
2018-02-12 23:44:40 +03:00
|
|
|
NS_INLINE_DECL_REFCOUNTING_INHERITED(ExternalRunnableWrapper, WorkerRunnable)
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
~ExternalRunnableWrapper() {}
|
|
|
|
|
2017-07-21 18:16:24 +03:00
|
|
|
virtual bool PreDispatch(WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
// Silence bad assertions.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void PostDispatch(WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult) override {
|
|
|
|
// Silence bad assertions.
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
2013-10-23 17:16:49 +04:00
|
|
|
nsresult rv = mWrappedRunnable->Run();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (!JS_IsExceptionPending(aCx)) {
|
|
|
|
Throw(aCx, rv);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
nsresult Cancel() override {
|
2016-04-11 21:40:06 +03:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsICancelableRunnable> cancelable =
|
|
|
|
do_QueryInterface(mWrappedRunnable);
|
|
|
|
MOZ_ASSERT(cancelable); // We checked this earlier!
|
|
|
|
rv = cancelable->Cancel();
|
2013-10-23 17:16:49 +04:00
|
|
|
nsresult rv2 = WorkerRunnable::Cancel();
|
|
|
|
return NS_FAILED(rv) ? rv : rv2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
struct WindowAction {
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* mWindow;
|
2013-06-05 18:04:23 +04:00
|
|
|
bool mDefaultAction;
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
MOZ_IMPLICIT WindowAction(nsPIDOMWindowInner* aWindow)
|
2014-07-02 14:26:49 +04:00
|
|
|
: mWindow(aWindow), mDefaultAction(true) {}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
bool operator==(const WindowAction& aOther) const {
|
|
|
|
return mWindow == aOther.mWindow;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class WorkerFinishedRunnable final : public WorkerControlRunnable {
|
2012-03-31 08:42:20 +04:00
|
|
|
WorkerPrivate* mFinishedWorker;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
WorkerFinishedRunnable(WorkerPrivate* aWorkerPrivate,
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerPrivate* aFinishedWorker)
|
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
|
|
|
mFinishedWorker(aFinishedWorker) {}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
2016-02-26 00:05:39 +03:00
|
|
|
virtual bool PreDispatch(WorkerPrivate* aWorkerPrivate) override {
|
2011-07-17 23:09:13 +04:00
|
|
|
// Silence bad assertions.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
virtual void PostDispatch(WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult) override {
|
2011-07-17 23:09:13 +04:00
|
|
|
// Silence bad assertions.
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
2017-02-07 18:28:39 +03:00
|
|
|
if (!mFinishedWorker->ProxyReleaseMainThreadObjects()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Failed to dispatch, going to leak!");
|
|
|
|
}
|
|
|
|
|
2018-01-31 10:24:59 +03:00
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_ASSERTION(runtime, "This should never be null!");
|
|
|
|
|
2014-10-27 20:00:05 +03:00
|
|
|
mFinishedWorker->DisableDebugger();
|
|
|
|
|
2016-03-28 20:28:14 +03:00
|
|
|
runtime->UnregisterWorker(mFinishedWorker);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-02-08 10:26:05 +03:00
|
|
|
mFinishedWorker->ClearSelfAndParentEventTargetRef();
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class TopLevelWorkerFinishedRunnable final : public Runnable {
|
2011-07-17 23:09:13 +04:00
|
|
|
WorkerPrivate* mFinishedWorker;
|
|
|
|
|
|
|
|
public:
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit TopLevelWorkerFinishedRunnable(WorkerPrivate* aFinishedWorker)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("TopLevelWorkerFinishedRunnable"),
|
|
|
|
mFinishedWorker(aFinishedWorker) {
|
2011-07-17 23:09:13 +04:00
|
|
|
aFinishedWorker->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
2018-02-12 23:44:40 +03:00
|
|
|
NS_INLINE_DECL_REFCOUNTING_INHERITED(TopLevelWorkerFinishedRunnable, Runnable)
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
private:
|
2014-07-09 01:23:17 +04:00
|
|
|
~TopLevelWorkerFinishedRunnable() {}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_IMETHOD
|
2015-03-21 19:28:04 +03:00
|
|
|
Run() override {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-01-31 10:24:59 +03:00
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
2014-02-02 22:08:50 +04:00
|
|
|
MOZ_ASSERT(runtime);
|
|
|
|
|
2014-10-27 20:00:05 +03:00
|
|
|
mFinishedWorker->DisableDebugger();
|
|
|
|
|
2016-03-28 20:28:14 +03:00
|
|
|
runtime->UnregisterWorker(mFinishedWorker);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2017-02-07 18:28:39 +03:00
|
|
|
if (!mFinishedWorker->ProxyReleaseMainThreadObjects()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
NS_WARNING("Failed to dispatch, going to leak!");
|
|
|
|
}
|
|
|
|
|
2018-02-08 10:26:05 +03:00
|
|
|
mFinishedWorker->ClearSelfAndParentEventTargetRef();
|
2011-07-26 05:49:16 +04:00
|
|
|
return NS_OK;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class ModifyBusyCountRunnable final : public WorkerControlRunnable {
|
2011-07-17 23:09:13 +04:00
|
|
|
bool mIncrease;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ModifyBusyCountRunnable(WorkerPrivate* aWorkerPrivate, bool aIncrease)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, ParentThreadUnchangedBusyCount),
|
2011-07-17 23:09:13 +04:00
|
|
|
mIncrease(aIncrease) {}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
2016-02-26 00:05:39 +03:00
|
|
|
return aWorkerPrivate->ModifyBusyCount(mIncrease);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aRunResult) override {
|
|
|
|
if (mIncrease) {
|
|
|
|
WorkerControlRunnable::PostRun(aCx, aWorkerPrivate, aRunResult);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Don't do anything here as it's possible that aWorkerPrivate has been
|
|
|
|
// deleted.
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class CompileScriptRunnable final : public WorkerRunnable {
|
2015-03-17 13:15:19 +03:00
|
|
|
nsString mScriptURL;
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
public:
|
2015-03-17 13:15:19 +03:00
|
|
|
explicit CompileScriptRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsAString& aScriptURL)
|
2016-06-28 20:28:13 +03:00
|
|
|
: WorkerRunnable(aWorkerPrivate), mScriptURL(aScriptURL) {}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
2016-03-02 00:52:26 +03:00
|
|
|
// We can't implement PreRun effectively, because at the point when that would
|
|
|
|
// run we have not yet done our load so don't know things like our final
|
|
|
|
// principal and whatnot.
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
2016-02-24 18:38:31 +03:00
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2017-11-27 22:48:17 +03:00
|
|
|
if (NS_WARN_IF(!aWorkerPrivate->EnsureClientSource())) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-21 23:13:04 +03:00
|
|
|
|
2018-07-10 19:53:03 +03:00
|
|
|
if (NS_WARN_IF(!aWorkerPrivate->EnsureCSPEventListener())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-04 14:36:25 +03:00
|
|
|
// PerformanceStorage & PerformanceCounter both need to be initialized
|
|
|
|
// on the worker thread before being used on main-thread.
|
|
|
|
// Let's be sure that it is created before any
|
2018-03-14 17:27:13 +03:00
|
|
|
// content loading.
|
|
|
|
aWorkerPrivate->EnsurePerformanceStorage();
|
2018-06-19 17:14:06 +03:00
|
|
|
|
|
|
|
if (mozilla::StaticPrefs::dom_performance_enable_scheduler_timing()) {
|
2018-04-24 23:03:06 +03:00
|
|
|
aWorkerPrivate->EnsurePerformanceCounter();
|
|
|
|
}
|
2018-03-14 17:27:13 +03:00
|
|
|
|
2015-10-15 15:06:55 +03:00
|
|
|
ErrorResult rv;
|
2018-01-31 10:22:03 +03:00
|
|
|
workerinternals::LoadMainScript(aWorkerPrivate, mScriptURL, WorkerScript,
|
|
|
|
rv);
|
2016-02-24 18:38:31 +03:00
|
|
|
rv.WouldReportJSException();
|
2016-02-24 18:38:31 +03:00
|
|
|
// Explicitly ignore NS_BINDING_ABORTED on rv. Or more precisely, still
|
|
|
|
// return false and don't SetWorkerScriptExecutedSuccessfully() in that
|
|
|
|
// case, but don't throw anything on aCx. The idea is to not dispatch error
|
|
|
|
// events if our load is canceled with that error code.
|
|
|
|
if (rv.ErrorCodeIs(NS_BINDING_ABORTED)) {
|
|
|
|
rv.SuppressException();
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-14 03:09:50 +03:00
|
|
|
|
|
|
|
WorkerGlobalScope* globalScope = aWorkerPrivate->GlobalScope();
|
|
|
|
if (NS_WARN_IF(!globalScope)) {
|
|
|
|
// We never got as far as calling GetOrCreateGlobalScope, or it failed.
|
|
|
|
// We have no way to enter a compartment, hence no sane way to report this
|
|
|
|
// error. :(
|
|
|
|
rv.SuppressException();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-02 00:52:26 +03:00
|
|
|
// Make sure to propagate exceptions from rv onto aCx, so that they will get
|
2017-01-28 17:39:24 +03:00
|
|
|
// reported after we return. We want to propagate just JS exceptions,
|
|
|
|
// because all the other errors are handled when the script is loaded.
|
|
|
|
// See: https://dom.spec.whatwg.org/#concept-event-fire
|
|
|
|
if (rv.Failed() && !rv.IsJSException()) {
|
2018-07-18 13:07:14 +03:00
|
|
|
WorkerErrorReport::CreateAndDispatchGenericErrorRunnableToParent(
|
|
|
|
aWorkerPrivate);
|
2017-01-28 17:39:24 +03:00
|
|
|
rv.SuppressException();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-16 11:53:16 +03:00
|
|
|
// This is a little dumb, but aCx is in the null realm here because we
|
2016-02-24 18:38:31 +03:00
|
|
|
// set it up that way in our Run(), since we had not created the global at
|
2018-05-16 11:53:16 +03:00
|
|
|
// that point yet. So we need to enter the realm of our global,
|
2016-02-24 18:38:31 +03:00
|
|
|
// because setting a pending exception on aCx involves wrapping into its
|
2016-05-14 03:09:50 +03:00
|
|
|
// current compartment. Luckily we have a global now.
|
2018-07-31 09:37:17 +03:00
|
|
|
JSAutoRealm ar(aCx, globalScope->GetGlobalJSObject());
|
2016-02-24 18:38:31 +03:00
|
|
|
if (rv.MaybeSetPendingException(aCx)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-11 00:12:55 +03:00
|
|
|
aWorkerPrivate->SetWorkerScriptExecutedSuccessfully();
|
|
|
|
return true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2018-04-02 17:50:23 +03:00
|
|
|
|
|
|
|
void PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aRunResult) override {
|
|
|
|
if (!aRunResult) {
|
|
|
|
aWorkerPrivate->CloseInternal();
|
|
|
|
}
|
|
|
|
WorkerRunnable::PostRun(aCx, aWorkerPrivate, aRunResult);
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class NotifyRunnable final : public WorkerControlRunnable {
|
2018-01-31 10:23:44 +03:00
|
|
|
WorkerStatus mStatus;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
public:
|
2018-01-31 10:23:44 +03:00
|
|
|
NotifyRunnable(WorkerPrivate* aWorkerPrivate, WorkerStatus aStatus)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
2012-08-08 01:38:46 +04:00
|
|
|
mStatus(aStatus) {
|
2018-07-12 20:33:41 +03:00
|
|
|
MOZ_ASSERT(aStatus == Closing || aStatus == Canceling ||
|
|
|
|
aStatus == Killing);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
2016-02-26 00:05:39 +03:00
|
|
|
virtual bool PreDispatch(WorkerPrivate* aWorkerPrivate) override {
|
2016-02-26 00:05:39 +03:00
|
|
|
aWorkerPrivate->AssertIsOnParentThread();
|
2016-02-26 00:05:39 +03:00
|
|
|
return aWorkerPrivate->ModifyBusyCount(true);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
virtual void PostDispatch(WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult) override {
|
2016-02-26 00:05:39 +03:00
|
|
|
aWorkerPrivate->AssertIsOnParentThread();
|
2013-11-05 18:16:24 +04:00
|
|
|
if (!aDispatchResult) {
|
|
|
|
// We couldn't dispatch to the worker, which means it's already dead.
|
|
|
|
// Undo the busy count modification.
|
2016-02-26 00:05:39 +03:00
|
|
|
aWorkerPrivate->ModifyBusyCount(false);
|
2013-11-05 18:16:24 +04:00
|
|
|
}
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2016-09-01 07:33:05 +03:00
|
|
|
virtual void PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aRunResult) override {
|
|
|
|
aWorkerPrivate->ModifyBusyCountFromWorker(false);
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
2018-03-13 17:25:50 +03:00
|
|
|
return aWorkerPrivate->NotifyInternal(mStatus);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
class FreezeRunnable final : public WorkerControlRunnable {
|
2011-07-17 23:09:13 +04:00
|
|
|
public:
|
2015-04-01 12:00:19 +03:00
|
|
|
explicit FreezeRunnable(WorkerPrivate* aWorkerPrivate)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount) {}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
2016-02-29 22:52:43 +03:00
|
|
|
return aWorkerPrivate->FreezeInternal();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
class ThawRunnable final : public WorkerControlRunnable {
|
2011-07-17 23:09:13 +04:00
|
|
|
public:
|
2015-04-01 12:00:19 +03:00
|
|
|
explicit ThawRunnable(WorkerPrivate* aWorkerPrivate)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount) {}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
2016-02-29 22:52:43 +03:00
|
|
|
return aWorkerPrivate->ThawInternal();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-10 11:09:59 +03:00
|
|
|
class PropagateFirstPartyStorageAccessGrantedRunnable final
|
|
|
|
: public WorkerControlRunnable {
|
|
|
|
public:
|
|
|
|
explicit PropagateFirstPartyStorageAccessGrantedRunnable(
|
|
|
|
WorkerPrivate* aWorkerPrivate)
|
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-10 11:09:59 +03:00
|
|
|
private:
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
aWorkerPrivate->PropagateFirstPartyStorageAccessGrantedInternal();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-12 02:41:00 +03:00
|
|
|
class ReportErrorToConsoleRunnable final : public WorkerRunnable {
|
|
|
|
const char* mMessage;
|
2018-01-25 04:59:04 +03:00
|
|
|
const nsTArray<nsString> mParams;
|
2016-04-12 02:41:00 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
// aWorkerPrivate is the worker thread we're on (or the main thread, if null)
|
2018-01-25 04:59:04 +03:00
|
|
|
static void Report(WorkerPrivate* aWorkerPrivate, const char* aMessage,
|
|
|
|
const nsTArray<nsString>& aParams) {
|
2016-04-12 02:41:00 +03:00
|
|
|
if (aWorkerPrivate) {
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
} else {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2016-04-12 02:41:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now fire a runnable to do the same on the parent's thread if we can.
|
|
|
|
if (aWorkerPrivate) {
|
|
|
|
RefPtr<ReportErrorToConsoleRunnable> runnable =
|
2018-01-25 04:59:04 +03:00
|
|
|
new ReportErrorToConsoleRunnable(aWorkerPrivate, aMessage, aParams);
|
2016-04-12 02:41:00 +03:00
|
|
|
runnable->Dispatch();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-25 04:59:04 +03:00
|
|
|
uint16_t paramCount = aParams.Length();
|
|
|
|
const char16_t** params = new const char16_t*[paramCount];
|
|
|
|
for (uint16_t i = 0; i < paramCount; ++i) {
|
|
|
|
params[i] = aParams[i].get();
|
|
|
|
}
|
|
|
|
|
2016-04-12 02:41:00 +03:00
|
|
|
// Log a warning to the console.
|
|
|
|
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
|
2018-01-25 04:59:04 +03:00
|
|
|
NS_LITERAL_CSTRING("DOM"), nullptr,
|
|
|
|
nsContentUtils::eDOM_PROPERTIES, aMessage,
|
|
|
|
paramCount ? params : nullptr, paramCount);
|
|
|
|
delete[] params;
|
2016-04-12 02:41:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-01-25 04:59:04 +03:00
|
|
|
ReportErrorToConsoleRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const char* aMessage,
|
|
|
|
const nsTArray<nsString>& aParams)
|
2016-04-12 02:41:00 +03:00
|
|
|
: WorkerRunnable(aWorkerPrivate, ParentThreadUnchangedBusyCount),
|
2018-01-25 04:59:04 +03:00
|
|
|
mMessage(aMessage),
|
|
|
|
mParams(aParams) {}
|
2016-04-12 02:41:00 +03:00
|
|
|
|
|
|
|
virtual void PostDispatch(WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult) override {
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
// Dispatch may fail if the worker was canceled, no need to report that as
|
|
|
|
// an error, so don't call base class PostDispatch.
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
WorkerPrivate* parent = aWorkerPrivate->GetParent();
|
|
|
|
MOZ_ASSERT_IF(!parent, NS_IsMainThread());
|
2018-01-25 04:59:04 +03:00
|
|
|
Report(parent, mMessage, mParams);
|
2016-04-12 02:41:00 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
class TimerRunnable final : public WorkerRunnable,
|
2017-07-26 21:18:20 +03:00
|
|
|
public nsITimerCallback,
|
|
|
|
public nsINamed {
|
2011-07-17 23:09:13 +04:00
|
|
|
public:
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit TimerRunnable(WorkerPrivate* aWorkerPrivate)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount) {}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
~TimerRunnable() {}
|
|
|
|
|
2016-02-26 00:05:39 +03:00
|
|
|
virtual bool PreDispatch(WorkerPrivate* aWorkerPrivate) override {
|
2011-07-17 23:09:13 +04:00
|
|
|
// Silence bad assertions.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
virtual void PostDispatch(WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult) override {
|
2011-07-17 23:09:13 +04:00
|
|
|
// Silence bad assertions.
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
2011-07-17 23:09:13 +04:00
|
|
|
return aWorkerPrivate->RunExpiredTimeouts(aCx);
|
|
|
|
}
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Notify(nsITimer* aTimer) override { return Run(); }
|
2017-07-26 21:18:20 +03:00
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
GetName(nsACString& aName) override {
|
|
|
|
aName.AssignLiteral("TimerRunnable");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
2017-07-26 21:18:20 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(TimerRunnable, WorkerRunnable, nsITimerCallback,
|
|
|
|
nsINamed)
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
|
2015-03-30 14:54:38 +03:00
|
|
|
class DebuggerImmediateRunnable : public WorkerRunnable {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<dom::Function> mHandler;
|
2015-03-30 14:54:38 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
explicit DebuggerImmediateRunnable(WorkerPrivate* aWorkerPrivate,
|
2015-09-12 00:25:23 +03:00
|
|
|
dom::Function& aHandler)
|
2015-03-30 14:54:38 +03:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
|
|
|
mHandler(&aHandler) {}
|
|
|
|
|
|
|
|
private:
|
2015-03-30 20:18:07 +03:00
|
|
|
virtual bool IsDebuggerRunnable() const override { return true; }
|
2015-03-30 14:54:38 +03:00
|
|
|
|
2016-02-26 00:05:39 +03:00
|
|
|
virtual bool PreDispatch(WorkerPrivate* aWorkerPrivate) override {
|
2015-03-30 14:54:38 +03:00
|
|
|
// Silence bad assertions.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
virtual void PostDispatch(WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult) override {
|
2015-03-30 14:54:38 +03:00
|
|
|
// Silence bad assertions.
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
2016-11-15 08:25:37 +03:00
|
|
|
JS::Rooted<JS::Value> callable(
|
|
|
|
aCx, JS::ObjectOrNullValue(mHandler->CallableOrNull()));
|
2015-03-30 14:54:38 +03:00
|
|
|
JS::HandleValueArray args = JS::HandleValueArray::empty();
|
|
|
|
JS::Rooted<JS::Value> rval(aCx);
|
2016-03-02 00:52:27 +03:00
|
|
|
if (!JS_CallFunctionValue(aCx, global, callable, args, &rval)) {
|
|
|
|
// Just return false; WorkerRunnable::Run will report the exception.
|
2015-03-30 14:54:38 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-26 19:01:33 +03:00
|
|
|
void PeriodicGCTimerCallback(nsITimer* aTimer, void* aClosure) {
|
|
|
|
auto workerPrivate = static_cast<WorkerPrivate*>(aClosure);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(workerPrivate);
|
|
|
|
workerPrivate->AssertIsOnWorkerThread();
|
|
|
|
workerPrivate->GarbageCollectInternal(workerPrivate->GetJSContext(),
|
|
|
|
false /* shrinking */,
|
|
|
|
false /* collect children */);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IdleGCTimerCallback(nsITimer* aTimer, void* aClosure) {
|
|
|
|
auto workerPrivate = static_cast<WorkerPrivate*>(aClosure);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(workerPrivate);
|
|
|
|
workerPrivate->AssertIsOnWorkerThread();
|
|
|
|
workerPrivate->GarbageCollectInternal(workerPrivate->GetJSContext(),
|
|
|
|
true /* shrinking */,
|
|
|
|
false /* collect children */);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-07-07 09:15:15 +03:00
|
|
|
class UpdateContextOptionsRunnable final : public WorkerControlRunnable {
|
|
|
|
JS::ContextOptions mContextOptions;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
public:
|
2016-07-07 09:15:15 +03:00
|
|
|
UpdateContextOptionsRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const JS::ContextOptions& aContextOptions)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
2016-07-07 09:15:15 +03:00
|
|
|
mContextOptions(aContextOptions) {}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
2016-07-07 09:15:15 +03:00
|
|
|
aWorkerPrivate->UpdateContextOptionsInternal(aCx, mContextOptions);
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class UpdateLanguagesRunnable final : public WorkerRunnable {
|
2014-09-05 18:26:34 +04:00
|
|
|
nsTArray<nsString> mLanguages;
|
|
|
|
|
|
|
|
public:
|
|
|
|
UpdateLanguagesRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsTArray<nsString>& aLanguages)
|
2016-06-28 20:28:13 +03:00
|
|
|
: WorkerRunnable(aWorkerPrivate), mLanguages(aLanguages) {}
|
2014-09-05 18:26:34 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
2016-02-26 23:23:12 +03:00
|
|
|
aWorkerPrivate->UpdateLanguagesInternal(mLanguages);
|
2014-09-05 18:26:34 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class UpdateJSWorkerMemoryParameterRunnable final
|
2013-10-23 17:16:49 +04:00
|
|
|
: public WorkerControlRunnable {
|
2013-01-11 02:50:40 +04:00
|
|
|
uint32_t mValue;
|
|
|
|
JSGCParamKey mKey;
|
2012-01-04 23:11:32 +04:00
|
|
|
|
|
|
|
public:
|
2013-01-11 02:50:40 +04:00
|
|
|
UpdateJSWorkerMemoryParameterRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
JSGCParamKey aKey, uint32_t aValue)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
2013-01-11 02:50:40 +04:00
|
|
|
mValue(aValue),
|
|
|
|
mKey(aKey) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
2013-02-16 02:12:19 +04:00
|
|
|
aWorkerPrivate->UpdateJSWorkerMemoryParameterInternal(aCx, mKey, mValue);
|
2012-01-04 23:11:32 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
#ifdef JS_GC_ZEAL
|
2015-03-21 19:28:04 +03:00
|
|
|
class UpdateGCZealRunnable final : public WorkerControlRunnable {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t mGCZeal;
|
2013-05-17 02:49:43 +04:00
|
|
|
uint32_t mFrequency;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
UpdateGCZealRunnable(WorkerPrivate* aWorkerPrivate, uint8_t aGCZeal,
|
2013-05-17 02:49:43 +04:00
|
|
|
uint32_t aFrequency)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
2013-05-17 02:49:43 +04:00
|
|
|
mGCZeal(aGCZeal),
|
|
|
|
mFrequency(aFrequency) {}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
2013-05-17 02:49:43 +04:00
|
|
|
aWorkerPrivate->UpdateGCZealInternal(aCx, mGCZeal, mFrequency);
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class GarbageCollectRunnable final : public WorkerControlRunnable {
|
2012-01-18 00:05:25 +04:00
|
|
|
bool mShrinking;
|
|
|
|
bool mCollectChildren;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GarbageCollectRunnable(WorkerPrivate* aWorkerPrivate, bool aShrinking,
|
|
|
|
bool aCollectChildren)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
2012-01-18 00:05:25 +04:00
|
|
|
mShrinking(aShrinking),
|
|
|
|
mCollectChildren(aCollectChildren) {}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
2016-02-26 00:05:39 +03:00
|
|
|
virtual bool PreDispatch(WorkerPrivate* aWorkerPrivate) override {
|
2012-01-18 00:05:25 +04:00
|
|
|
// Silence bad assertions, this can be dispatched from either the main
|
|
|
|
// thread or the timer thread..
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
virtual void PostDispatch(WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult) override {
|
2012-01-18 00:05:25 +04:00
|
|
|
// Silence bad assertions, this can be dispatched from either the main
|
|
|
|
// thread or the timer thread..
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
2012-01-18 00:05:25 +04:00
|
|
|
aWorkerPrivate->GarbageCollectInternal(aCx, mShrinking, mCollectChildren);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-03 08:07:02 +04:00
|
|
|
class CycleCollectRunnable : public WorkerControlRunnable {
|
|
|
|
bool mCollectChildren;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CycleCollectRunnable(WorkerPrivate* aWorkerPrivate, bool aCollectChildren)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
2013-12-03 08:07:02 +04:00
|
|
|
mCollectChildren(aCollectChildren) {}
|
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
2016-02-26 23:23:12 +03:00
|
|
|
aWorkerPrivate->CycleCollectInternal(mCollectChildren);
|
2013-12-03 08:07:02 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-20 03:08:50 +04:00
|
|
|
class OfflineStatusChangeRunnable : public WorkerRunnable {
|
|
|
|
public:
|
|
|
|
OfflineStatusChangeRunnable(WorkerPrivate* aWorkerPrivate, bool aIsOffline)
|
2016-06-28 20:28:13 +03:00
|
|
|
: WorkerRunnable(aWorkerPrivate), mIsOffline(aIsOffline) {}
|
2013-11-20 03:08:50 +04:00
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
2016-02-26 23:23:12 +03:00
|
|
|
aWorkerPrivate->OfflineStatusChangeEventInternal(mIsOffline);
|
2013-11-20 03:08:50 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool mIsOffline;
|
|
|
|
};
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
class MemoryPressureRunnable : public WorkerControlRunnable {
|
|
|
|
public:
|
|
|
|
explicit MemoryPressureRunnable(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount) {}
|
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
2016-03-24 00:55:07 +03:00
|
|
|
aWorkerPrivate->MemoryPressureInternal();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-23 08:26:21 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
static bool StartsWithExplicit(nsACString& s) {
|
|
|
|
return StringBeginsWith(s, NS_LITERAL_CSTRING("explicit/"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
PRThread* PRThreadFromThread(nsIThread* aThread) {
|
|
|
|
MOZ_ASSERT(aThread);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
PRThread* result;
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(aThread->GetPRThread(&result));
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(result);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
return result;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2018-04-17 21:51:03 +03:00
|
|
|
// A runnable to cancel the worker from the parent thread when self.close() is
|
|
|
|
// called. This runnable is executed on the parent process in order to cancel
|
2018-10-22 18:46:04 +03:00
|
|
|
// the current runnable. It uses a normal WorkerDebuggeeRunnable in order to be
|
|
|
|
// sure that all the pending WorkerDebuggeeRunnables are executed before this.
|
|
|
|
class CancelingOnParentRunnable final : public WorkerDebuggeeRunnable {
|
2018-04-17 21:51:03 +03:00
|
|
|
public:
|
|
|
|
explicit CancelingOnParentRunnable(WorkerPrivate* aWorkerPrivate)
|
2018-10-22 18:46:04 +03:00
|
|
|
: WorkerDebuggeeRunnable(aWorkerPrivate, ParentThreadUnchangedBusyCount) {
|
2018-04-17 21:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
aWorkerPrivate->Cancel();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-17 21:51:04 +03:00
|
|
|
// A runnable to cancel the worker from the parent process.
|
|
|
|
class CancelingWithTimeoutOnParentRunnable final
|
|
|
|
: public WorkerControlRunnable {
|
|
|
|
public:
|
|
|
|
explicit CancelingWithTimeoutOnParentRunnable(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: WorkerControlRunnable(aWorkerPrivate, ParentThreadUnchangedBusyCount) {}
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
aWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
aWorkerPrivate->StartCancelingTimer();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CancelingTimerCallback final : public nsITimerCallback {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
explicit CancelingTimerCallback(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate) {}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Notify(nsITimer* aTimer) override {
|
|
|
|
mWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
mWorkerPrivate->Cancel();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~CancelingTimerCallback() = default;
|
|
|
|
|
|
|
|
// Raw pointer here is OK because the timer is canceled during the shutdown
|
|
|
|
// steps.
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(CancelingTimerCallback, nsITimerCallback)
|
|
|
|
|
2018-04-17 21:51:03 +03:00
|
|
|
// This runnable starts the canceling of a worker after a self.close().
|
|
|
|
class CancelingRunnable final : public Runnable {
|
|
|
|
public:
|
|
|
|
CancelingRunnable() : Runnable("CancelingRunnable") {}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
workerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
// Now we can cancel the this worker from the parent process.
|
|
|
|
RefPtr<CancelingOnParentRunnable> r =
|
|
|
|
new CancelingOnParentRunnable(workerPrivate);
|
|
|
|
r->Dispatch();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
} /* anonymous namespace */
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-02-08 11:33:32 +03:00
|
|
|
class WorkerPrivate::EventTarget final : public nsISerialEventTarget {
|
2015-02-12 12:50:05 +03:00
|
|
|
// This mutex protects mWorkerPrivate and must be acquired *before* the
|
|
|
|
// WorkerPrivate's mutex whenever they must both be held.
|
|
|
|
mozilla::Mutex mMutex;
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsIEventTarget* mWeakNestedEventTarget;
|
|
|
|
nsCOMPtr<nsIEventTarget> mNestedEventTarget;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit EventTarget(WorkerPrivate* aWorkerPrivate)
|
2018-02-08 11:33:32 +03:00
|
|
|
: mMutex("WorkerPrivate::EventTarget::mMutex"),
|
2015-02-12 12:50:05 +03:00
|
|
|
mWorkerPrivate(aWorkerPrivate),
|
|
|
|
mWeakNestedEventTarget(nullptr) {
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
}
|
|
|
|
|
|
|
|
EventTarget(WorkerPrivate* aWorkerPrivate, nsIEventTarget* aNestedEventTarget)
|
2018-02-08 11:33:32 +03:00
|
|
|
: mMutex("WorkerPrivate::EventTarget::mMutex"),
|
2015-02-12 12:50:05 +03:00
|
|
|
mWorkerPrivate(aWorkerPrivate),
|
|
|
|
mWeakNestedEventTarget(aNestedEventTarget),
|
|
|
|
mNestedEventTarget(aNestedEventTarget) {
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
MOZ_ASSERT(aNestedEventTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Disable() {
|
|
|
|
nsCOMPtr<nsIEventTarget> nestedEventTarget;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2017-06-29 21:28:56 +03:00
|
|
|
// Note, Disable() can be called more than once safely.
|
2015-02-12 12:50:05 +03:00
|
|
|
mWorkerPrivate = nullptr;
|
|
|
|
mNestedEventTarget.swap(nestedEventTarget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIEventTarget* GetWeakNestedEventTarget() const {
|
|
|
|
MOZ_ASSERT(mWeakNestedEventTarget);
|
|
|
|
return mWeakNestedEventTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2017-05-20 06:58:07 +03:00
|
|
|
NS_DECL_NSIEVENTTARGET_FULL
|
2015-02-12 12:50:05 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
~EventTarget() {}
|
|
|
|
};
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
struct WorkerPrivate::TimeoutInfo {
|
2016-08-16 09:10:30 +03:00
|
|
|
TimeoutInfo() : mId(0), mIsInterval(false), mCanceled(false) {
|
2018-01-31 10:24:08 +03:00
|
|
|
MOZ_COUNT_CTOR(mozilla::dom::WorkerPrivate::TimeoutInfo);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2018-01-31 10:24:08 +03:00
|
|
|
~TimeoutInfo() { MOZ_COUNT_DTOR(mozilla::dom::WorkerPrivate::TimeoutInfo); }
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
bool operator==(const TimeoutInfo& aOther) {
|
|
|
|
return mTargetTime == aOther.mTargetTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const TimeoutInfo& aOther) {
|
|
|
|
return mTargetTime < aOther.mTargetTime;
|
|
|
|
}
|
|
|
|
|
2016-08-16 09:10:30 +03:00
|
|
|
nsCOMPtr<nsIScriptTimeoutHandler> mHandler;
|
2011-07-17 23:09:13 +04:00
|
|
|
mozilla::TimeStamp mTargetTime;
|
|
|
|
mozilla::TimeDuration mInterval;
|
2013-11-05 18:16:26 +04:00
|
|
|
int32_t mId;
|
2011-07-17 23:09:13 +04:00
|
|
|
bool mIsInterval;
|
|
|
|
bool mCanceled;
|
|
|
|
};
|
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
class WorkerJSContextStats final : public JS::RuntimeStats {
|
|
|
|
const nsCString mRtPath;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit WorkerJSContextStats(const nsACString& aRtPath)
|
|
|
|
: JS::RuntimeStats(JsWorkerMallocSizeOf), mRtPath(aRtPath) {}
|
|
|
|
|
|
|
|
~WorkerJSContextStats() {
|
|
|
|
for (size_t i = 0; i != zoneStatsVector.length(); i++) {
|
|
|
|
delete static_cast<xpc::ZoneStatsExtras*>(zoneStatsVector[i].extra);
|
|
|
|
}
|
|
|
|
|
2018-05-17 17:15:18 +03:00
|
|
|
for (size_t i = 0; i != realmStatsVector.length(); i++) {
|
|
|
|
delete static_cast<xpc::RealmStatsExtras*>(realmStatsVector[i].extra);
|
2016-10-28 12:50:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsCString& Path() const { return mRtPath; }
|
|
|
|
|
|
|
|
virtual void initExtraZoneStats(JS::Zone* aZone,
|
|
|
|
JS::ZoneStats* aZoneStats) override {
|
|
|
|
MOZ_ASSERT(!aZoneStats->extra);
|
|
|
|
|
|
|
|
// ReportJSRuntimeExplicitTreeStats expects that
|
|
|
|
// aZoneStats->extra is a xpc::ZoneStatsExtras pointer.
|
|
|
|
xpc::ZoneStatsExtras* extras = new xpc::ZoneStatsExtras;
|
|
|
|
extras->pathPrefix = mRtPath;
|
|
|
|
extras->pathPrefix += nsPrintfCString("zone(0x%p)/", (void*)aZone);
|
|
|
|
|
|
|
|
MOZ_ASSERT(StartsWithExplicit(extras->pathPrefix));
|
|
|
|
|
|
|
|
aZoneStats->extra = extras;
|
|
|
|
}
|
|
|
|
|
2018-05-30 21:14:19 +03:00
|
|
|
virtual void initExtraRealmStats(JS::Handle<JS::Realm*> aRealm,
|
2018-05-17 17:15:18 +03:00
|
|
|
JS::RealmStats* aRealmStats) override {
|
|
|
|
MOZ_ASSERT(!aRealmStats->extra);
|
2016-10-28 12:50:16 +03:00
|
|
|
|
|
|
|
// ReportJSRuntimeExplicitTreeStats expects that
|
2018-05-17 17:15:18 +03:00
|
|
|
// aRealmStats->extra is a xpc::RealmStatsExtras pointer.
|
|
|
|
xpc::RealmStatsExtras* extras = new xpc::RealmStatsExtras;
|
2016-10-28 12:50:16 +03:00
|
|
|
|
2018-05-31 13:53:26 +03:00
|
|
|
// This is the |jsPathPrefix|. Each worker has exactly one realm.
|
2016-10-28 12:50:16 +03:00
|
|
|
extras->jsPathPrefix.Assign(mRtPath);
|
|
|
|
extras->jsPathPrefix +=
|
2018-06-06 13:55:50 +03:00
|
|
|
nsPrintfCString("zone(0x%p)/", (void*)js::GetRealmZone(aRealm));
|
2018-05-31 13:53:26 +03:00
|
|
|
extras->jsPathPrefix += NS_LITERAL_CSTRING("realm(web-worker)/");
|
2016-10-28 12:50:16 +03:00
|
|
|
|
|
|
|
// This should never be used when reporting with workers (hence the "?!").
|
|
|
|
extras->domPathPrefix.AssignLiteral("explicit/workers/?!/");
|
|
|
|
|
|
|
|
MOZ_ASSERT(StartsWithExplicit(extras->jsPathPrefix));
|
|
|
|
MOZ_ASSERT(StartsWithExplicit(extras->domPathPrefix));
|
|
|
|
|
|
|
|
extras->location = nullptr;
|
|
|
|
|
2018-05-17 17:15:18 +03:00
|
|
|
aRealmStats->extra = extras;
|
2016-10-28 12:50:16 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class WorkerPrivate::MemoryReporter final : public nsIMemoryReporter {
|
2013-12-08 09:39:47 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
friend class WorkerPrivate;
|
|
|
|
|
|
|
|
SharedMutex mMutex;
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
|
|
|
|
public:
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit MemoryReporter(WorkerPrivate* aWorkerPrivate)
|
2018-03-14 06:21:22 +03:00
|
|
|
: mMutex(aWorkerPrivate->mMutex), mWorkerPrivate(aWorkerPrivate) {
|
2013-02-08 15:50:00 +04:00
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2016-08-24 08:23:45 +03:00
|
|
|
CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData,
|
2016-10-28 12:50:16 +03:00
|
|
|
bool aAnonymize) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
class FinishCollectRunnable;
|
|
|
|
|
|
|
|
class CollectReportsRunnable final : public MainThreadWorkerControlRunnable {
|
|
|
|
RefPtr<FinishCollectRunnable> mFinishCollectRunnable;
|
|
|
|
const bool mAnonymize;
|
2013-02-08 15:50:00 +04:00
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
public:
|
|
|
|
CollectReportsRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aHandlerData, bool aAnonymize,
|
|
|
|
const nsACString& aPath);
|
2013-02-08 15:50:00 +04:00
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
private:
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override;
|
2013-02-08 15:50:00 +04:00
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
~CollectReportsRunnable() {
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
mFinishCollectRunnable->Run();
|
|
|
|
return;
|
2015-01-16 07:12:20 +03:00
|
|
|
}
|
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
workerPrivate->DispatchToMainThread(mFinishCollectRunnable.forget()));
|
|
|
|
}
|
|
|
|
};
|
2014-05-21 10:06:54 +04:00
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
class FinishCollectRunnable final : public Runnable {
|
|
|
|
nsCOMPtr<nsIHandleReportCallback> mHandleReport;
|
|
|
|
nsCOMPtr<nsISupports> mHandlerData;
|
2017-07-27 10:05:51 +03:00
|
|
|
size_t mPerformanceUserEntries;
|
|
|
|
size_t mPerformanceResourceEntries;
|
2016-10-28 12:50:16 +03:00
|
|
|
const bool mAnonymize;
|
|
|
|
bool mSuccess;
|
2015-01-23 08:26:21 +03:00
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
public:
|
|
|
|
WorkerJSContextStats mCxStats;
|
|
|
|
|
|
|
|
explicit FinishCollectRunnable(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aHandlerData, bool aAnonymize,
|
|
|
|
const nsACString& aPath);
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override;
|
|
|
|
|
2017-07-27 10:05:51 +03:00
|
|
|
void SetPerformanceSizes(size_t userEntries, size_t resourceEntries) {
|
|
|
|
mPerformanceUserEntries = userEntries;
|
|
|
|
mPerformanceResourceEntries = resourceEntries;
|
|
|
|
}
|
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
void SetSuccess(bool success) { mSuccess = success; }
|
2013-02-08 15:50:00 +04:00
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
private:
|
|
|
|
~FinishCollectRunnable() {
|
|
|
|
// mHandleReport and mHandlerData are released on the main thread.
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2016-10-28 12:50:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FinishCollectRunnable(const FinishCollectRunnable&) = delete;
|
|
|
|
FinishCollectRunnable& operator=(const FinishCollectRunnable&) = delete;
|
|
|
|
FinishCollectRunnable& operator=(const FinishCollectRunnable&&) = delete;
|
|
|
|
};
|
2013-02-08 15:50:00 +04:00
|
|
|
|
|
|
|
~MemoryReporter() {}
|
|
|
|
|
|
|
|
void Disable() {
|
|
|
|
// Called from WorkerPrivate::DisableMemoryReporter.
|
|
|
|
mMutex.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
NS_ASSERTION(mWorkerPrivate, "Disabled more than once!");
|
|
|
|
mWorkerPrivate = nullptr;
|
|
|
|
}
|
2016-10-28 12:50:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(WorkerPrivate::MemoryReporter, nsIMemoryReporter)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerPrivate::MemoryReporter::CollectReports(
|
|
|
|
nsIHandleReportCallback* aHandleReport, nsISupports* aData,
|
|
|
|
bool aAnonymize) {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2016-10-28 12:50:16 +03:00
|
|
|
|
|
|
|
RefPtr<CollectReportsRunnable> runnable;
|
|
|
|
|
2013-06-15 06:48:28 +04:00
|
|
|
{
|
2016-10-28 12:50:16 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2013-06-15 06:48:28 +04:00
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
// This will effectively report 0 memory.
|
|
|
|
nsCOMPtr<nsIMemoryReporterManager> manager =
|
|
|
|
do_GetService("@mozilla.org/memory-reporter-manager;1");
|
|
|
|
if (manager) {
|
|
|
|
manager->EndReport();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2013-06-15 06:48:28 +04:00
|
|
|
}
|
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
nsAutoCString path;
|
|
|
|
path.AppendLiteral("explicit/workers/workers(");
|
|
|
|
if (aAnonymize && !mWorkerPrivate->Domain().IsEmpty()) {
|
|
|
|
path.AppendLiteral("<anonymized-domain>)/worker(<anonymized-url>");
|
|
|
|
} else {
|
|
|
|
nsAutoCString escapedDomain(mWorkerPrivate->Domain());
|
|
|
|
if (escapedDomain.IsEmpty()) {
|
|
|
|
escapedDomain += "chrome";
|
|
|
|
} else {
|
|
|
|
escapedDomain.ReplaceChar('/', '\\');
|
|
|
|
}
|
|
|
|
path.Append(escapedDomain);
|
|
|
|
path.AppendLiteral(")/worker(");
|
|
|
|
NS_ConvertUTF16toUTF8 escapedURL(mWorkerPrivate->ScriptURL());
|
|
|
|
escapedURL.ReplaceChar('/', '\\');
|
|
|
|
path.Append(escapedURL);
|
2013-06-15 06:48:28 +04:00
|
|
|
}
|
2016-10-28 12:50:16 +03:00
|
|
|
path.AppendPrintf(", 0x%p)/", static_cast<void*>(mWorkerPrivate));
|
2013-06-15 06:48:28 +04:00
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
runnable = new CollectReportsRunnable(mWorkerPrivate, aHandleReport, aData,
|
|
|
|
aAnonymize, path);
|
|
|
|
}
|
2013-10-28 08:53:00 +04:00
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
if (!runnable->Dispatch()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2013-06-15 06:48:28 +04:00
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate::MemoryReporter::CollectReportsRunnable::CollectReportsRunnable(
|
|
|
|
WorkerPrivate* aWorkerPrivate, nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aHandlerData, bool aAnonymize, const nsACString& aPath)
|
|
|
|
: MainThreadWorkerControlRunnable(aWorkerPrivate),
|
|
|
|
mFinishCollectRunnable(new FinishCollectRunnable(
|
|
|
|
aHandleReport, aHandlerData, aAnonymize, aPath)),
|
|
|
|
mAnonymize(aAnonymize) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
bool WorkerPrivate::MemoryReporter::CollectReportsRunnable::WorkerRun(
|
|
|
|
JSContext* aCx, WorkerPrivate* aWorkerPrivate) {
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2017-08-11 04:00:05 +03:00
|
|
|
RefPtr<WorkerGlobalScope> scope = aWorkerPrivate->GlobalScope();
|
|
|
|
RefPtr<Performance> performance =
|
|
|
|
scope ? scope->GetPerformanceIfExists() : nullptr;
|
2017-07-27 10:05:51 +03:00
|
|
|
if (performance) {
|
|
|
|
size_t userEntries = performance->SizeOfUserEntries(JsWorkerMallocSizeOf);
|
|
|
|
size_t resourceEntries =
|
|
|
|
performance->SizeOfResourceEntries(JsWorkerMallocSizeOf);
|
|
|
|
mFinishCollectRunnable->SetPerformanceSizes(userEntries, resourceEntries);
|
|
|
|
}
|
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
mFinishCollectRunnable->SetSuccess(aWorkerPrivate->CollectRuntimeStats(
|
|
|
|
&mFinishCollectRunnable->mCxStats, mAnonymize));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate::MemoryReporter::FinishCollectRunnable::FinishCollectRunnable(
|
|
|
|
nsIHandleReportCallback* aHandleReport, nsISupports* aHandlerData,
|
|
|
|
bool aAnonymize, const nsACString& aPath)
|
2018-01-31 10:24:08 +03:00
|
|
|
: mozilla::Runnable(
|
|
|
|
"dom::WorkerPrivate::MemoryReporter::FinishCollectRunnable"),
|
2017-06-12 22:34:10 +03:00
|
|
|
mHandleReport(aHandleReport),
|
|
|
|
mHandlerData(aHandlerData),
|
2017-07-27 10:05:51 +03:00
|
|
|
mPerformanceUserEntries(0),
|
|
|
|
mPerformanceResourceEntries(0),
|
2017-06-12 22:34:10 +03:00
|
|
|
mAnonymize(aAnonymize),
|
|
|
|
mSuccess(false),
|
|
|
|
mCxStats(aPath) {}
|
2016-10-28 12:50:16 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerPrivate::MemoryReporter::FinishCollectRunnable::Run() {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2016-10-28 12:50:16 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIMemoryReporterManager> manager =
|
|
|
|
do_GetService("@mozilla.org/memory-reporter-manager;1");
|
|
|
|
|
|
|
|
if (!manager) return NS_OK;
|
|
|
|
|
|
|
|
if (mSuccess) {
|
|
|
|
xpc::ReportJSRuntimeExplicitTreeStats(
|
|
|
|
mCxStats, mCxStats.Path(), mHandleReport, mHandlerData, mAnonymize);
|
2017-07-27 10:05:51 +03:00
|
|
|
|
|
|
|
if (mPerformanceUserEntries) {
|
|
|
|
nsCString path = mCxStats.Path();
|
|
|
|
path.AppendLiteral("dom/performance/user-entries");
|
|
|
|
mHandleReport->Callback(
|
|
|
|
EmptyCString(), path, nsIMemoryReporter::KIND_HEAP,
|
|
|
|
nsIMemoryReporter::UNITS_BYTES, mPerformanceUserEntries,
|
|
|
|
NS_LITERAL_CSTRING("Memory used for performance user entries."),
|
|
|
|
mHandlerData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPerformanceResourceEntries) {
|
|
|
|
nsCString path = mCxStats.Path();
|
|
|
|
path.AppendLiteral("dom/performance/resource-entries");
|
|
|
|
mHandleReport->Callback(
|
|
|
|
EmptyCString(), path, nsIMemoryReporter::KIND_HEAP,
|
|
|
|
nsIMemoryReporter::UNITS_BYTES, mPerformanceResourceEntries,
|
|
|
|
NS_LITERAL_CSTRING("Memory used for performance resource entries."),
|
|
|
|
mHandlerData);
|
|
|
|
}
|
2016-10-28 12:50:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
manager->EndReport();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-12-08 09:39:47 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerPrivate::SyncLoopInfo::SyncLoopInfo(EventTarget* aEventTarget)
|
|
|
|
: mEventTarget(aEventTarget),
|
|
|
|
mCompleted(false),
|
|
|
|
mResult(false)
|
|
|
|
#ifdef DEBUG
|
|
|
|
,
|
|
|
|
mHasRun(false)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
nsIDocument* WorkerPrivate::GetDocument() const {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2018-02-08 11:33:34 +03:00
|
|
|
if (mLoadInfo.mWindow) {
|
|
|
|
return mLoadInfo.mWindow->GetExtantDoc();
|
2015-04-07 19:19:19 +03:00
|
|
|
}
|
|
|
|
// if we don't have a document, we should query the document
|
|
|
|
// from the parent in case of a nested worker
|
2018-02-08 11:33:34 +03:00
|
|
|
WorkerPrivate* parent = mParent;
|
2015-04-07 19:19:19 +03:00
|
|
|
while (parent) {
|
|
|
|
if (parent->mLoadInfo.mWindow) {
|
|
|
|
return parent->mLoadInfo.mWindow->GetExtantDoc();
|
|
|
|
}
|
|
|
|
parent = parent->GetParent();
|
|
|
|
}
|
|
|
|
// couldn't query a document, give up and return nullptr
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
void WorkerPrivate::SetCSP(nsIContentSecurityPolicy* aCSP) {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2017-03-29 05:20:32 +03:00
|
|
|
if (!aCSP) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-08 11:33:33 +03:00
|
|
|
aCSP->EnsureEventTarget(mMainThreadEventTarget);
|
2018-07-10 19:53:03 +03:00
|
|
|
|
2017-03-29 05:20:32 +03:00
|
|
|
mLoadInfo.mCSP = aCSP;
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
nsresult WorkerPrivate::SetCSPFromHeaderValues(
|
|
|
|
const nsACString& aCSPHeaderValue,
|
|
|
|
const nsACString& aCSPReportOnlyHeaderValue) {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2017-02-14 18:06:38 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mLoadInfo.mCSP);
|
|
|
|
|
|
|
|
NS_ConvertASCIItoUTF16 cspHeaderValue(aCSPHeaderValue);
|
|
|
|
NS_ConvertASCIItoUTF16 cspROHeaderValue(aCSPReportOnlyHeaderValue);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
|
|
|
nsresult rv = mLoadInfo.mPrincipal->EnsureCSP(nullptr, getter_AddRefs(csp));
|
|
|
|
if (!csp) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
csp->EnsureEventTarget(mMainThreadEventTarget);
|
2017-03-29 05:20:32 +03:00
|
|
|
|
2017-02-14 18:06:38 +03:00
|
|
|
// If there's a CSP header, apply it.
|
|
|
|
if (!cspHeaderValue.IsEmpty()) {
|
|
|
|
rv = CSP_AppendCSPFromHeader(csp, cspHeaderValue, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
// If there's a report-only CSP header, apply it.
|
|
|
|
if (!cspROHeaderValue.IsEmpty()) {
|
|
|
|
rv = CSP_AppendCSPFromHeader(csp, cspROHeaderValue, true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set evalAllowed, default value is set in GetAllowsEval
|
|
|
|
bool evalAllowed = false;
|
|
|
|
bool reportEvalViolations = false;
|
|
|
|
rv = csp->GetAllowsEval(&reportEvalViolations, &evalAllowed);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mLoadInfo.mCSP = csp;
|
|
|
|
mLoadInfo.mEvalAllowed = evalAllowed;
|
|
|
|
mLoadInfo.mReportCSPViolations = reportEvalViolations;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
void WorkerPrivate::SetReferrerPolicyFromHeaderValue(
|
|
|
|
const nsACString& aReferrerPolicyHeaderValue) {
|
2017-02-23 18:54:42 +03:00
|
|
|
NS_ConvertUTF8toUTF16 headerValue(aReferrerPolicyHeaderValue);
|
|
|
|
|
|
|
|
if (headerValue.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
net::ReferrerPolicy policy =
|
|
|
|
nsContentUtils::GetReferrerPolicyFromHeader(headerValue);
|
|
|
|
if (policy == net::RP_Unset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetReferrerPolicy(policy);
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:32 +03:00
|
|
|
void WorkerPrivate::Traverse(nsCycleCollectionTraversalCallback& aCb) {
|
2013-10-23 17:16:49 +04:00
|
|
|
AssertIsOnParentThread();
|
2013-11-05 18:16:24 +04:00
|
|
|
|
2018-02-08 10:26:05 +03:00
|
|
|
// The WorkerPrivate::mParentEventTargetRef has a reference to the exposed
|
|
|
|
// Worker object, which is really held by the worker thread. We traverse this
|
|
|
|
// reference if and only if our busy count is zero and we have not released
|
|
|
|
// the main thread reference. We do not unlink it. This allows the CC to
|
|
|
|
// break cycles involving the Worker and begin shutting it down (which does
|
|
|
|
// happen in unlink) but ensures that the WorkerPrivate won't be deleted
|
|
|
|
// before we're done shutting down the thread.
|
2018-02-08 11:33:32 +03:00
|
|
|
if (!mBusyCount && !mMainThreadObjectsForgotten) {
|
2018-02-08 10:26:05 +03:00
|
|
|
nsCycleCollectionTraversalCallback& cb = aCb;
|
2018-02-08 11:33:32 +03:00
|
|
|
WorkerPrivate* tmp = this;
|
2018-02-08 10:26:05 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParentEventTargetRef);
|
2014-03-06 05:58:03 +04:00
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
2013-11-05 18:16:24 +04:00
|
|
|
|
2018-04-17 06:08:26 +03:00
|
|
|
nsresult WorkerPrivate::Dispatch(already_AddRefed<WorkerRunnable> aRunnable,
|
|
|
|
nsIEventTarget* aSyncLoopTarget) {
|
2013-10-23 17:16:49 +04:00
|
|
|
// May be called on any thread!
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerRunnable> runnable(aRunnable);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2018-02-08 11:33:32 +03:00
|
|
|
MOZ_ASSERT_IF(aSyncLoopTarget, mThread);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2018-02-08 11:33:32 +03:00
|
|
|
if (!mThread) {
|
|
|
|
if (ParentStatus() == Pending || mStatus == Pending) {
|
2015-07-10 06:21:46 +03:00
|
|
|
mPreStartRunnables.AppendElement(runnable);
|
2013-10-23 17:16:49 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING(
|
|
|
|
"Using a worker event target after the thread has already"
|
|
|
|
"been released!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mStatus == Dead || (!aSyncLoopTarget && ParentStatus() > Running)) {
|
|
|
|
NS_WARNING(
|
|
|
|
"A runnable was posted to a worker that is already shutting "
|
|
|
|
"down!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
nsresult rv;
|
2013-10-23 17:16:49 +04:00
|
|
|
if (aSyncLoopTarget) {
|
2015-07-10 06:21:46 +03:00
|
|
|
rv = aSyncLoopTarget->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
|
2014-11-17 22:55:37 +03:00
|
|
|
} else {
|
2018-10-23 09:30:30 +03:00
|
|
|
// WorkerDebuggeeRunnables don't need any special treatment here. True,
|
|
|
|
// they should not be delivered to a frozen worker. But frozen workers
|
|
|
|
// aren't drawing from the thread's main event queue anyway, only from
|
|
|
|
// mControlQueue.
|
2018-02-08 11:33:32 +03:00
|
|
|
rv = mThread->DispatchAnyThread(WorkerThreadFriendKey(),
|
|
|
|
runnable.forget());
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCondVar.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-05 18:16:24 +04:00
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
void WorkerPrivate::EnableDebugger() {
|
2014-10-27 20:00:05 +03:00
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
if (NS_FAILED(RegisterWorkerDebugger(this))) {
|
2014-10-27 20:00:05 +03:00
|
|
|
NS_WARNING("Failed to register worker debugger!");
|
2016-01-07 15:35:31 +03:00
|
|
|
return;
|
2014-10-27 20:00:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
void WorkerPrivate::DisableDebugger() {
|
2014-10-27 20:00:05 +03:00
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
if (NS_FAILED(UnregisterWorkerDebugger(this))) {
|
2014-10-27 20:00:05 +03:00
|
|
|
NS_WARNING("Failed to unregister worker debugger!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
nsresult WorkerPrivate::DispatchControlRunnable(
|
|
|
|
already_AddRefed<WorkerControlRunnable> aWorkerControlRunnable) {
|
2013-10-23 17:16:49 +04:00
|
|
|
// May be called on any thread!
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerControlRunnable> runnable(aWorkerControlRunnable);
|
2015-07-10 06:21:46 +03:00
|
|
|
MOZ_ASSERT(runnable);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
if (mStatus == Dead) {
|
2013-10-23 17:16:49 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transfer ownership to the control queue.
|
2018-02-08 11:33:34 +03:00
|
|
|
mControlQueue.Push(runnable.forget().take());
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
if (JSContext* cx = mJSContext) {
|
|
|
|
MOZ_ASSERT(mThread);
|
2016-07-23 20:52:20 +03:00
|
|
|
JS_RequestInterruptCallback(cx);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mCondVar.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-05 18:16:24 +04:00
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
nsresult WorkerPrivate::DispatchDebuggerRunnable(
|
|
|
|
already_AddRefed<WorkerRunnable> aDebuggerRunnable) {
|
2015-03-04 17:11:32 +03:00
|
|
|
// May be called on any thread!
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerRunnable> runnable(aDebuggerRunnable);
|
2015-03-04 17:11:32 +03:00
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
MOZ_ASSERT(runnable);
|
2015-03-04 17:11:32 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
if (mStatus == Dead) {
|
2015-03-04 17:11:32 +03:00
|
|
|
NS_WARNING(
|
|
|
|
"A debugger runnable was posted to a worker that is already "
|
|
|
|
"shutting down!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transfer ownership to the debugger queue.
|
2018-02-08 11:33:34 +03:00
|
|
|
mDebuggerQueue.Push(runnable.forget().take());
|
2015-03-04 17:11:32 +03:00
|
|
|
|
|
|
|
mCondVar.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
already_AddRefed<WorkerRunnable> WorkerPrivate::MaybeWrapAsWorkerRunnable(
|
|
|
|
already_AddRefed<nsIRunnable> aRunnable) {
|
2013-10-23 17:16:49 +04:00
|
|
|
// May be called on any thread!
|
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(aRunnable);
|
|
|
|
MOZ_ASSERT(runnable);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerRunnable> workerRunnable =
|
2015-07-10 06:21:46 +03:00
|
|
|
WorkerRunnable::FromRunnable(runnable);
|
2013-10-23 17:16:49 +04:00
|
|
|
if (workerRunnable) {
|
|
|
|
return workerRunnable.forget();
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
nsCOMPtr<nsICancelableRunnable> cancelable = do_QueryInterface(runnable);
|
2013-10-23 17:16:49 +04:00
|
|
|
if (!cancelable) {
|
|
|
|
MOZ_CRASH("All runnables destined for a worker thread must be cancelable!");
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
workerRunnable = new ExternalRunnableWrapper(this, runnable);
|
2013-10-23 17:16:49 +04:00
|
|
|
return workerRunnable.forget();
|
|
|
|
}
|
2013-11-05 18:16:24 +04:00
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
bool WorkerPrivate::Start() {
|
2011-07-17 23:09:13 +04:00
|
|
|
// May be called on any thread!
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2018-02-08 11:33:34 +03:00
|
|
|
NS_ASSERTION(mParentStatus != Running, "How can this be?!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
if (mParentStatus == Pending) {
|
|
|
|
mParentStatus = Running;
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-19 18:34:58 +04:00
|
|
|
// aCx is null when called from the finalizer
|
2018-04-19 01:02:08 +03:00
|
|
|
bool WorkerPrivate::Notify(WorkerStatus aStatus) {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
bool pending;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
if (mParentStatus >= aStatus) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
pending = mParentStatus == Pending;
|
|
|
|
mParentStatus = aStatus;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pending) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
// Fake a thread here just so that our assertions don't go off for no
|
|
|
|
// reason.
|
2011-07-17 23:09:13 +04:00
|
|
|
nsIThread* currentThread = NS_GetCurrentThread();
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(currentThread);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
MOZ_ASSERT(!mPRThread);
|
|
|
|
mPRThread = PRThreadFromThread(currentThread);
|
|
|
|
MOZ_ASSERT(mPRThread);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
#endif
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
// Worker never got a chance to run, go ahead and delete it.
|
2018-02-08 11:33:34 +03:00
|
|
|
ScheduleDeletion(WorkerPrivate::WorkerNeverRan);
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-17 21:51:04 +03:00
|
|
|
// No Canceling timeout is needed.
|
|
|
|
if (mCancelingTimer) {
|
|
|
|
mCancelingTimer->Cancel();
|
|
|
|
mCancelingTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
RefPtr<NotifyRunnable> runnable = new NotifyRunnable(this, aStatus);
|
2016-02-26 00:05:39 +03:00
|
|
|
return runnable->Dispatch();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
bool WorkerPrivate::Freeze(nsPIDOMWindowInner* aWindow) {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnParentThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
mParentFrozen = true;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-10-23 09:30:30 +03:00
|
|
|
// WorkerDebuggeeRunnables sent from a worker to content must not be delivered
|
|
|
|
// while the worker is frozen.
|
|
|
|
//
|
|
|
|
// Since a top-level worker and all its children share the same
|
|
|
|
// mMainThreadDebuggeeEventTarget, it's sufficient to do this only in the
|
|
|
|
// top-level worker.
|
|
|
|
if (aWindow) {
|
|
|
|
// This is called from WorkerPrivate construction, and We may not have
|
|
|
|
// allocated mMainThreadDebuggeeEventTarget yet.
|
|
|
|
if (mMainThreadDebuggeeEventTarget) {
|
|
|
|
// Pausing a ThrottledEventQueue is infallible.
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(mMainThreadDebuggeeEventTarget->SetIsPaused(true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2018-07-12 20:33:41 +03:00
|
|
|
if (mParentStatus >= Canceling) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
DisableDebugger();
|
2015-10-16 19:48:26 +03:00
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
RefPtr<FreezeRunnable> runnable = new FreezeRunnable(this);
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!runnable->Dispatch()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
bool WorkerPrivate::Thaw(nsPIDOMWindowInner* aWindow) {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnParentThread();
|
2018-02-08 11:33:33 +03:00
|
|
|
MOZ_ASSERT(mParentFrozen);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
mParentFrozen = false;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-10-23 09:30:30 +03:00
|
|
|
// Delivery of WorkerDebuggeeRunnables to the window may resume.
|
|
|
|
//
|
|
|
|
// Since a top-level worker and all its children share the same
|
|
|
|
// mMainThreadDebuggeeEventTarget, it's sufficient to do this only in the
|
|
|
|
// top-level worker.
|
|
|
|
if (aWindow) {
|
|
|
|
// Since the worker is no longer frozen, only a paused parent window should
|
|
|
|
// require the queue to remain paused.
|
|
|
|
//
|
|
|
|
// This can only fail if the ThrottledEventQueue cannot dispatch its
|
|
|
|
// executor to the main thread, in which case the main thread was never
|
|
|
|
// going to draw runnables from it anyway, so the failure doesn't matter.
|
|
|
|
Unused << mMainThreadDebuggeeEventTarget->SetIsPaused(
|
|
|
|
IsParentWindowPaused());
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2018-07-12 20:33:41 +03:00
|
|
|
if (mParentStatus >= Canceling) {
|
2013-06-05 18:04:23 +04:00
|
|
|
return true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
EnableDebugger();
|
2015-10-16 19:48:26 +03:00
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
RefPtr<ThawRunnable> runnable = new ThawRunnable(this);
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!runnable->Dispatch()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-06-06 21:28:47 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
void WorkerPrivate::ParentWindowPaused() {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2018-11-20 02:18:21 +03:00
|
|
|
MOZ_ASSERT(!mParentWindowPaused);
|
|
|
|
mParentWindowPaused = true;
|
2018-10-23 09:30:30 +03:00
|
|
|
|
|
|
|
// This is called from WorkerPrivate construction, and we may not have
|
|
|
|
// allocated mMainThreadDebuggeeEventTarget yet.
|
|
|
|
if (mMainThreadDebuggeeEventTarget) {
|
|
|
|
// Pausing a ThrottledEventQueue is infallible.
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(mMainThreadDebuggeeEventTarget->SetIsPaused(true));
|
|
|
|
}
|
2015-10-07 13:20:59 +03:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
void WorkerPrivate::ParentWindowResumed() {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2015-10-07 13:20:59 +03:00
|
|
|
|
2018-11-20 02:18:21 +03:00
|
|
|
MOZ_ASSERT(mParentWindowPaused);
|
|
|
|
mParentWindowPaused = false;
|
2015-10-07 13:20:59 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2018-07-12 20:33:41 +03:00
|
|
|
if (mParentStatus >= Canceling) {
|
2015-10-07 13:20:59 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 09:30:30 +03:00
|
|
|
// Since the window is no longer paused, the queue should only remain paused
|
|
|
|
// if the worker is frozen.
|
|
|
|
//
|
|
|
|
// This can only fail if the ThrottledEventQueue cannot dispatch its executor
|
|
|
|
// to the main thread, in which case the main thread was never going to draw
|
|
|
|
// runnables from it anyway, so the failure doesn't matter.
|
|
|
|
Unused << mMainThreadDebuggeeEventTarget->SetIsPaused(IsFrozen());
|
2015-10-07 13:20:59 +03:00
|
|
|
}
|
|
|
|
|
2018-07-10 11:09:59 +03:00
|
|
|
void WorkerPrivate::PropagateFirstPartyStorageAccessGranted() {
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2018-07-12 20:33:41 +03:00
|
|
|
if (mParentStatus >= Canceling) {
|
2018-07-10 11:09:59 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<PropagateFirstPartyStorageAccessGrantedRunnable> runnable =
|
|
|
|
new PropagateFirstPartyStorageAccessGrantedRunnable(this);
|
|
|
|
Unused << NS_WARN_IF(!runnable->Dispatch());
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
bool WorkerPrivate::Close() {
|
2017-05-03 17:42:43 +03:00
|
|
|
mMutex.AssertCurrentThreadOwns();
|
2018-02-08 11:33:34 +03:00
|
|
|
if (mParentStatus < Closing) {
|
|
|
|
mParentStatus = Closing;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:32 +03:00
|
|
|
bool WorkerPrivate::ModifyBusyCount(bool aIncrease) {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2018-11-20 02:18:33 +03:00
|
|
|
MOZ_ASSERT(aIncrease || mBusyCount, "Mismatched busy count mods!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
if (aIncrease) {
|
2014-02-02 22:08:50 +04:00
|
|
|
mBusyCount++;
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:24 +04:00
|
|
|
if (--mBusyCount == 0) {
|
2011-07-17 23:09:13 +04:00
|
|
|
bool shouldCancel;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2018-07-12 20:33:41 +03:00
|
|
|
shouldCancel = mParentStatus == Canceling;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-02-26 00:05:39 +03:00
|
|
|
if (shouldCancel && !Cancel()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
bool WorkerPrivate::ProxyReleaseMainThreadObjects() {
|
2015-02-21 18:09:17 +03:00
|
|
|
AssertIsOnParentThread();
|
2018-02-08 11:33:34 +03:00
|
|
|
MOZ_ASSERT(!mMainThreadObjectsForgotten);
|
2015-02-21 18:09:17 +03:00
|
|
|
|
2017-02-07 18:28:39 +03:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroupToCancel;
|
2015-02-21 18:09:17 +03:00
|
|
|
// If we're not overriden, then do nothing here. Let the load group get
|
|
|
|
// handled in ForgetMainThreadObjects().
|
2018-02-08 11:33:34 +03:00
|
|
|
if (mLoadInfo.mInterfaceRequestor) {
|
|
|
|
mLoadInfo.mLoadGroup.swap(loadGroupToCancel);
|
2015-02-21 18:09:17 +03:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
bool result =
|
|
|
|
mLoadInfo.ProxyReleaseMainThreadObjects(this, loadGroupToCancel);
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
mMainThreadObjectsForgotten = true;
|
2017-02-07 18:28:39 +03:00
|
|
|
|
|
|
|
return result;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
void WorkerPrivate::UpdateContextOptions(
|
|
|
|
const JS::ContextOptions& aContextOptions) {
|
2013-06-05 18:04:23 +04:00
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2018-02-08 11:33:34 +03:00
|
|
|
mJSSettings.contextOptions = aContextOptions;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2016-07-07 09:15:15 +03:00
|
|
|
RefPtr<UpdateContextOptionsRunnable> runnable =
|
2018-02-08 11:33:34 +03:00
|
|
|
new UpdateContextOptionsRunnable(this, aContextOptions);
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!runnable->Dispatch()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
NS_WARNING("Failed to update worker context options!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
void WorkerPrivate::UpdateLanguages(const nsTArray<nsString>& aLanguages) {
|
2014-09-05 18:26:34 +04:00
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<UpdateLanguagesRunnable> runnable =
|
2018-02-08 11:33:34 +03:00
|
|
|
new UpdateLanguagesRunnable(this, aLanguages);
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!runnable->Dispatch()) {
|
2014-09-05 18:26:34 +04:00
|
|
|
NS_WARNING("Failed to update worker languages!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
void WorkerPrivate::UpdateJSWorkerMemoryParameter(JSGCParamKey aKey,
|
|
|
|
uint32_t aValue) {
|
2013-06-05 18:04:23 +04:00
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2018-02-08 11:33:34 +03:00
|
|
|
found = mJSSettings.ApplyGCSetting(aKey, aValue);
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<UpdateJSWorkerMemoryParameterRunnable> runnable =
|
2018-02-08 11:33:34 +03:00
|
|
|
new UpdateJSWorkerMemoryParameterRunnable(this, aKey, aValue);
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!runnable->Dispatch()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
NS_WARNING("Failed to update memory parameter!");
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
2012-01-04 23:11:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
#ifdef JS_GC_ZEAL
|
2018-02-08 11:33:34 +03:00
|
|
|
void WorkerPrivate::UpdateGCZeal(uint8_t aGCZeal, uint32_t aFrequency) {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2018-02-08 11:33:34 +03:00
|
|
|
mJSSettings.gcZeal = aGCZeal;
|
|
|
|
mJSSettings.gcZealFrequency = aFrequency;
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<UpdateGCZealRunnable> runnable =
|
2018-02-08 11:33:34 +03:00
|
|
|
new UpdateGCZealRunnable(this, aGCZeal, aFrequency);
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!runnable->Dispatch()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Failed to update worker gczeal!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
void WorkerPrivate::GarbageCollect(bool aShrinking) {
|
2013-06-05 18:04:23 +04:00
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GarbageCollectRunnable> runnable = new GarbageCollectRunnable(
|
2018-02-08 11:33:34 +03:00
|
|
|
this, aShrinking, /* collectChildren = */ true);
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!runnable->Dispatch()) {
|
2013-12-03 08:07:02 +04:00
|
|
|
NS_WARNING("Failed to GC worker!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
void WorkerPrivate::CycleCollect(bool aDummy) {
|
2013-12-03 08:07:02 +04:00
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CycleCollectRunnable> runnable =
|
2018-02-08 11:33:34 +03:00
|
|
|
new CycleCollectRunnable(this, /* collectChildren = */ true);
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!runnable->Dispatch()) {
|
2013-12-03 08:07:02 +04:00
|
|
|
NS_WARNING("Failed to CC worker!");
|
2012-01-18 00:05:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
void WorkerPrivate::OfflineStatusChangeEvent(bool aIsOffline) {
|
2013-11-20 03:08:50 +04:00
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<OfflineStatusChangeRunnable> runnable =
|
2018-02-08 11:33:34 +03:00
|
|
|
new OfflineStatusChangeRunnable(this, aIsOffline);
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!runnable->Dispatch()) {
|
2013-11-20 03:08:50 +04:00
|
|
|
NS_WARNING("Failed to dispatch offline status change event!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
void WorkerPrivate::OfflineStatusChangeEventInternal(bool aIsOffline) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2013-11-20 03:08:50 +04:00
|
|
|
|
2014-08-27 06:42:13 +04:00
|
|
|
// The worker is already in this state. No need to dispatch an event.
|
2018-11-13 23:22:40 +03:00
|
|
|
if (data->mOnLine == !aIsOffline) {
|
2014-08-27 06:42:13 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
for (uint32_t index = 0; index < data->mChildWorkers.Length(); ++index) {
|
|
|
|
data->mChildWorkers[index]->OfflineStatusChangeEvent(aIsOffline);
|
2013-11-20 03:08:50 +04:00
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mOnLine = !aIsOffline;
|
2013-11-20 03:08:50 +04:00
|
|
|
WorkerGlobalScope* globalScope = GlobalScope();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerNavigator> nav = globalScope->GetExistingNavigator();
|
2013-11-20 03:08:50 +04:00
|
|
|
if (nav) {
|
2018-11-13 23:22:40 +03:00
|
|
|
nav->SetOnLine(data->mOnLine);
|
2013-11-20 03:08:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsString eventType;
|
|
|
|
if (aIsOffline) {
|
|
|
|
eventType.AssignLiteral("offline");
|
|
|
|
} else {
|
|
|
|
eventType.AssignLiteral("online");
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Event> event = NS_NewDOMEvent(globalScope, nullptr, nullptr);
|
2013-11-20 03:08:50 +04:00
|
|
|
|
2015-11-13 03:09:42 +03:00
|
|
|
event->InitEvent(eventType, false, false);
|
2013-11-20 03:08:50 +04:00
|
|
|
event->SetTrusted(true);
|
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
globalScope->DispatchEvent(*event);
|
2013-11-20 03:08:50 +04:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
void WorkerPrivate::MemoryPressure(bool aDummy) {
|
2016-03-24 00:55:07 +03:00
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
RefPtr<MemoryPressureRunnable> runnable = new MemoryPressureRunnable(this);
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused << NS_WARN_IF(!runnable->Dispatch());
|
2016-03-24 00:55:07 +03:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
void WorkerPrivate::WorkerScriptLoaded() {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
if (IsSharedWorker() || IsServiceWorker()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
// No longer need to hold references to the window or document we came from.
|
2018-02-08 11:33:34 +03:00
|
|
|
mLoadInfo.mWindow = nullptr;
|
|
|
|
mLoadInfo.mScriptContext = nullptr;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
void WorkerPrivate::SetBaseURI(nsIURI* aBaseURI) {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (!mLoadInfo.mBaseURI) {
|
|
|
|
NS_ASSERTION(GetParent(), "Shouldn't happen without a parent!");
|
|
|
|
mLoadInfo.mResolvedScriptURI = aBaseURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLoadInfo.mBaseURI = aBaseURI;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2011-11-04 20:32:17 +04:00
|
|
|
if (NS_FAILED(aBaseURI->GetSpec(mLocationInfo.mHref))) {
|
|
|
|
mLocationInfo.mHref.Truncate();
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-04-15 23:32:53 +03:00
|
|
|
mLocationInfo.mHostname.Truncate();
|
|
|
|
nsContentUtils::GetHostOrIPv6WithBrackets(aBaseURI, mLocationInfo.mHostname);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-04-15 23:32:53 +03:00
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(aBaseURI));
|
|
|
|
if (!url || NS_FAILED(url->GetFilePath(mLocationInfo.mPathname))) {
|
2011-11-04 20:32:17 +04:00
|
|
|
mLocationInfo.mPathname.Truncate();
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
nsCString temp;
|
|
|
|
|
2011-11-04 20:32:17 +04:00
|
|
|
if (url && NS_SUCCEEDED(url->GetQuery(temp)) && !temp.IsEmpty()) {
|
2014-05-26 22:55:08 +04:00
|
|
|
mLocationInfo.mSearch.Assign('?');
|
2011-07-17 23:09:13 +04:00
|
|
|
mLocationInfo.mSearch.Append(temp);
|
|
|
|
}
|
|
|
|
|
2011-11-04 20:32:17 +04:00
|
|
|
if (NS_SUCCEEDED(aBaseURI->GetRef(temp)) && !temp.IsEmpty()) {
|
2013-10-23 17:16:49 +04:00
|
|
|
if (mLocationInfo.mHash.IsEmpty()) {
|
2014-05-26 22:55:08 +04:00
|
|
|
mLocationInfo.mHash.Assign('#');
|
2013-10-23 17:16:49 +04:00
|
|
|
mLocationInfo.mHash.Append(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(aBaseURI->GetScheme(mLocationInfo.mProtocol))) {
|
2014-05-22 07:48:51 +04:00
|
|
|
mLocationInfo.mProtocol.Append(':');
|
2013-10-23 17:16:49 +04:00
|
|
|
} else {
|
|
|
|
mLocationInfo.mProtocol.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t port;
|
|
|
|
if (NS_SUCCEEDED(aBaseURI->GetPort(&port)) && port != -1) {
|
|
|
|
mLocationInfo.mPort.AppendInt(port);
|
|
|
|
|
|
|
|
nsAutoCString host(mLocationInfo.mHostname);
|
2014-05-22 07:48:51 +04:00
|
|
|
host.Append(':');
|
2013-10-23 17:16:49 +04:00
|
|
|
host.Append(mLocationInfo.mPort);
|
|
|
|
|
|
|
|
mLocationInfo.mHost.Assign(host);
|
|
|
|
} else {
|
|
|
|
mLocationInfo.mHost.Assign(mLocationInfo.mHostname);
|
|
|
|
}
|
2014-01-31 22:22:52 +04:00
|
|
|
|
2014-08-31 14:40:11 +04:00
|
|
|
nsContentUtils::GetUTFOrigin(aBaseURI, mLocationInfo.mOrigin);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
nsresult WorkerPrivate::SetPrincipalOnMainThread(nsIPrincipal* aPrincipal,
|
|
|
|
nsILoadGroup* aLoadGroup) {
|
|
|
|
return mLoadInfo.SetPrincipalOnMainThread(aPrincipal, aLoadGroup);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
nsresult WorkerPrivate::SetPrincipalFromChannel(nsIChannel* aChannel) {
|
|
|
|
return mLoadInfo.SetPrincipalFromChannel(aChannel);
|
2017-02-07 18:28:39 +03:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
bool WorkerPrivate::FinalChannelPrincipalIsValid(nsIChannel* aChannel) {
|
|
|
|
return mLoadInfo.FinalChannelPrincipalIsValid(aChannel);
|
2017-02-07 18:28:39 +03:00
|
|
|
}
|
2017-02-23 18:54:41 +03:00
|
|
|
|
2018-01-05 01:35:41 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
2018-02-08 11:33:34 +03:00
|
|
|
bool WorkerPrivate::PrincipalURIMatchesScriptURL() {
|
|
|
|
return mLoadInfo.PrincipalURIMatchesScriptURL();
|
2017-02-23 18:54:41 +03:00
|
|
|
}
|
2017-02-07 18:28:39 +03:00
|
|
|
#endif
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
void WorkerPrivate::UpdateOverridenLoadGroup(nsILoadGroup* aBaseLoadGroup) {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2015-02-21 18:09:17 +03:00
|
|
|
|
|
|
|
// The load group should have been overriden at init time.
|
2018-02-08 11:33:34 +03:00
|
|
|
mLoadInfo.mInterfaceRequestor->MaybeAddTabChild(aBaseLoadGroup);
|
2015-02-21 18:09:17 +03:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
#ifdef DEBUG
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
void WorkerPrivate::AssertIsOnParentThread() const {
|
|
|
|
if (GetParent()) {
|
|
|
|
GetParent()->AssertIsOnWorkerThread();
|
|
|
|
} else {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
void WorkerPrivate::AssertInnerWindowIsCorrect() const {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Only care about top level workers from windows.
|
2018-02-08 11:33:34 +03:00
|
|
|
if (mParent || !mLoadInfo.mWindow) {
|
2013-10-23 17:16:49 +04:00
|
|
|
return;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-02-08 11:33:34 +03:00
|
|
|
nsPIDOMWindowOuter* outer = mLoadInfo.mWindow->GetOuterWindow();
|
|
|
|
NS_ASSERTION(outer && outer->GetCurrentInnerWindow() == mLoadInfo.mWindow,
|
2013-10-23 17:16:49 +04:00
|
|
|
"Inner window no longer correct!");
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
#endif
|
2014-10-27 20:00:05 +03:00
|
|
|
|
2017-06-06 00:43:34 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
2018-02-08 11:33:34 +03:00
|
|
|
bool WorkerPrivate::PrincipalIsValid() const {
|
|
|
|
return mLoadInfo.PrincipalIsValid();
|
2017-02-14 18:06:39 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
WorkerPrivate::WorkerThreadAccessible::WorkerThreadAccessible(
|
|
|
|
WorkerPrivate* const aParent)
|
|
|
|
: mNumHoldersPreventingShutdownStart(0),
|
|
|
|
mDebuggerEventLoopLevel(0),
|
|
|
|
mErrorHandlerRecursionCount(0),
|
|
|
|
mNextTimeoutId(1),
|
|
|
|
mFrozen(false),
|
|
|
|
mTimerRunning(false),
|
|
|
|
mRunningExpiredTimeouts(false),
|
|
|
|
mPeriodicGCTimerRunning(false),
|
|
|
|
mIdleGCTimerRunning(false),
|
|
|
|
mOnLine(aParent ? aParent->OnLine() : !NS_IsOffline()) {}
|
|
|
|
|
2018-11-21 23:02:51 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool IsNewWorkerSecureContext(const WorkerPrivate* const aParent,
|
|
|
|
const WorkerType aWorkerType,
|
|
|
|
const WorkerLoadInfo& aLoadInfo) {
|
|
|
|
if (aParent) {
|
|
|
|
return aParent->IsSecureContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Our secure context state depends on the kind of worker we have.
|
|
|
|
|
|
|
|
if (aLoadInfo.mPrincipalIsSystem) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aWorkerType == WorkerTypeService) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aLoadInfo.mSecureContext != WorkerLoadInfo::eNotSet) {
|
|
|
|
return aLoadInfo.mSecureContext == WorkerLoadInfo::eSecureContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT_UNREACHABLE(
|
|
|
|
"non-chrome worker that is not a service worker "
|
|
|
|
"that has no parent and no associated window");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-03-12 00:43:30 +03:00
|
|
|
WorkerPrivate::WorkerPrivate(WorkerPrivate* aParent,
|
2013-06-05 18:04:23 +04:00
|
|
|
const nsAString& aScriptURL, bool aIsChromeWorker,
|
2013-10-31 03:40:16 +04:00
|
|
|
WorkerType aWorkerType,
|
2017-05-17 17:49:34 +03:00
|
|
|
const nsAString& aWorkerName,
|
|
|
|
const nsACString& aServiceWorkerScope,
|
2015-02-12 12:50:05 +03:00
|
|
|
WorkerLoadInfo& aLoadInfo)
|
2018-02-08 11:33:34 +03:00
|
|
|
: mMutex("WorkerPrivate Mutex"),
|
|
|
|
mCondVar(mMutex, "WorkerPrivate CondVar"),
|
2018-02-08 11:33:33 +03:00
|
|
|
mParent(aParent),
|
2018-02-08 11:33:33 +03:00
|
|
|
mScriptURL(aScriptURL),
|
|
|
|
mWorkerName(aWorkerName),
|
2018-02-08 11:33:33 +03:00
|
|
|
mWorkerType(aWorkerType),
|
2018-11-21 23:02:51 +03:00
|
|
|
mLoadInfo(std::move(aLoadInfo)),
|
2016-01-07 15:35:31 +03:00
|
|
|
mDebugger(nullptr),
|
2014-11-17 22:55:37 +03:00
|
|
|
mJSContext(nullptr),
|
2013-10-23 17:16:49 +04:00
|
|
|
mPRThread(nullptr),
|
2017-07-21 18:16:23 +03:00
|
|
|
mWorkerControlEventTarget(new WorkerEventTarget(
|
|
|
|
this, WorkerEventTarget::Behavior::ControlOnly)),
|
2017-07-21 18:16:24 +03:00
|
|
|
mWorkerHybridEventTarget(
|
|
|
|
new WorkerEventTarget(this, WorkerEventTarget::Behavior::Hybrid)),
|
2018-02-08 11:33:34 +03:00
|
|
|
mParentStatus(Pending),
|
|
|
|
mStatus(Pending),
|
|
|
|
mBusyCount(0),
|
|
|
|
mLoadingWorkerScript(false),
|
|
|
|
mCreationTimeStamp(TimeStamp::Now()),
|
|
|
|
mCreationTimeHighRes((double)PR_Now() / PR_USEC_PER_MSEC),
|
2018-11-13 23:22:40 +03:00
|
|
|
mWorkerThreadAccessible(aParent),
|
2018-11-20 02:18:21 +03:00
|
|
|
mParentWindowPaused(false),
|
2015-09-29 00:34:28 +03:00
|
|
|
mPendingEventQueueClearing(false),
|
2014-11-17 22:55:37 +03:00
|
|
|
mCancelAllPendingRunnables(false),
|
|
|
|
mWorkerScriptExecutedSuccessfully(false),
|
2016-12-19 05:38:53 +03:00
|
|
|
mFetchHandlerWasAdded(false),
|
2018-02-08 11:33:32 +03:00
|
|
|
mMainThreadObjectsForgotten(false),
|
2018-02-08 11:33:33 +03:00
|
|
|
mIsChromeWorker(aIsChromeWorker),
|
|
|
|
mParentFrozen(false),
|
2018-11-21 23:02:51 +03:00
|
|
|
mIsSecureContext(
|
|
|
|
IsNewWorkerSecureContext(mParent, mWorkerType, mLoadInfo)),
|
2018-02-08 11:33:34 +03:00
|
|
|
mDebuggerRegistered(false),
|
2018-03-01 22:24:20 +03:00
|
|
|
mIsInAutomation(false),
|
2018-03-06 12:19:19 +03:00
|
|
|
mPerformanceCounter(nullptr) {
|
2018-02-08 11:33:33 +03:00
|
|
|
MOZ_ASSERT_IF(!IsDedicatedWorker(), NS_IsMainThread());
|
2018-02-08 11:33:33 +03:00
|
|
|
|
2013-11-24 23:27:15 +04:00
|
|
|
if (aParent) {
|
|
|
|
aParent->AssertIsOnWorkerThread();
|
2018-02-08 11:33:33 +03:00
|
|
|
|
|
|
|
// Note that this copies our parent's secure context state into mJSSettings.
|
|
|
|
aParent->CopyJSSettings(mJSSettings);
|
|
|
|
|
|
|
|
MOZ_ASSERT_IF(mIsChromeWorker, mIsSecureContext);
|
|
|
|
|
2018-03-01 22:24:20 +03:00
|
|
|
mIsInAutomation = aParent->IsInAutomation();
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
MOZ_ASSERT(IsDedicatedWorker());
|
|
|
|
|
|
|
|
if (aParent->mParentFrozen) {
|
|
|
|
Freeze(nullptr);
|
|
|
|
}
|
2013-11-24 23:27:15 +04:00
|
|
|
} else {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2018-02-08 11:33:33 +03:00
|
|
|
|
|
|
|
RuntimeService::GetDefaultJSSettings(mJSSettings);
|
|
|
|
|
|
|
|
if (mIsSecureContext) {
|
|
|
|
mJSSettings.chrome.realmOptions.creationOptions().setSecureContext(true);
|
2018-03-09 18:49:28 +03:00
|
|
|
mJSSettings.chrome.realmOptions.creationOptions().setClampAndJitterTime(
|
|
|
|
false);
|
2018-02-08 11:33:33 +03:00
|
|
|
mJSSettings.content.realmOptions.creationOptions().setSecureContext(true);
|
2018-03-09 18:49:28 +03:00
|
|
|
mJSSettings.content.realmOptions.creationOptions().setClampAndJitterTime(
|
|
|
|
false);
|
2018-02-08 11:33:33 +03:00
|
|
|
}
|
|
|
|
|
2018-03-01 22:24:20 +03:00
|
|
|
mIsInAutomation = xpc::IsInAutomation();
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
// Our parent can get suspended after it initiates the async creation
|
|
|
|
// of a new worker thread. In this case suspend the new worker as well.
|
|
|
|
if (mLoadInfo.mWindow && mLoadInfo.mWindow->IsSuspended()) {
|
|
|
|
ParentWindowPaused();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLoadInfo.mWindow && mLoadInfo.mWindow->IsFrozen()) {
|
|
|
|
Freeze(mLoadInfo.mWindow);
|
|
|
|
}
|
2013-11-24 23:27:15 +04:00
|
|
|
}
|
2016-09-14 06:14:02 +03:00
|
|
|
|
2017-06-13 23:40:00 +03:00
|
|
|
nsCOMPtr<nsISerialEventTarget> target;
|
2016-09-14 06:14:02 +03:00
|
|
|
|
2016-11-07 23:30:17 +03:00
|
|
|
// A child worker just inherits the parent workers ThrottledEventQueue
|
|
|
|
// and main thread target for now. This is mainly due to the restriction
|
|
|
|
// that ThrottledEventQueue can only be created on the main thread at the
|
|
|
|
// moment.
|
2016-09-14 06:14:02 +03:00
|
|
|
if (aParent) {
|
2016-11-07 23:30:17 +03:00
|
|
|
mMainThreadEventTarget = aParent->mMainThreadEventTarget;
|
2018-10-23 09:30:30 +03:00
|
|
|
mMainThreadDebuggeeEventTarget = aParent->mMainThreadDebuggeeEventTarget;
|
2016-11-07 23:30:17 +03:00
|
|
|
return;
|
2016-09-14 06:14:02 +03:00
|
|
|
}
|
|
|
|
|
2016-11-07 23:30:17 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-11-27 22:24:34 +03:00
|
|
|
target =
|
|
|
|
GetWindow() ? GetWindow()->EventTargetFor(TaskCategory::Worker) : nullptr;
|
2016-09-14 06:14:02 +03:00
|
|
|
|
|
|
|
if (!target) {
|
2017-06-13 23:40:00 +03:00
|
|
|
target = GetMainThreadSerialEventTarget();
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(target);
|
2016-09-14 06:14:02 +03:00
|
|
|
}
|
|
|
|
|
2016-11-07 23:30:17 +03:00
|
|
|
// Throttle events to the main thread using a ThrottledEventQueue specific to
|
2018-10-17 11:44:43 +03:00
|
|
|
// this tree of worker threads.
|
|
|
|
mMainThreadEventTarget = ThrottledEventQueue::Create(target);
|
2018-10-23 09:30:30 +03:00
|
|
|
mMainThreadDebuggeeEventTarget = ThrottledEventQueue::Create(target);
|
|
|
|
if (IsParentWindowPaused() || IsFrozen()) {
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(mMainThreadDebuggeeEventTarget->SetIsPaused(true));
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate::~WorkerPrivate() {
|
2018-02-08 11:33:34 +03:00
|
|
|
DropJSObjects(this);
|
|
|
|
|
2017-01-26 19:01:32 +03:00
|
|
|
mWorkerControlEventTarget->ForgetWorkerPrivate(this);
|
2017-07-21 18:16:24 +03:00
|
|
|
|
|
|
|
// We force the hybrid event target to forget the thread when we
|
|
|
|
// enter the Killing state, but we do it again here to be safe.
|
|
|
|
// Its possible that we may be created and destroyed without progressing
|
|
|
|
// to Killing via some obscure code path.
|
|
|
|
mWorkerHybridEventTarget->ForgetWorkerPrivate(this);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2014-05-29 20:19:00 +04:00
|
|
|
// static
|
|
|
|
already_AddRefed<WorkerPrivate> WorkerPrivate::Constructor(
|
|
|
|
JSContext* aCx, const nsAString& aScriptURL, bool aIsChromeWorker,
|
|
|
|
WorkerType aWorkerType, const nsAString& aWorkerName,
|
2017-05-17 17:49:34 +03:00
|
|
|
const nsACString& aServiceWorkerScope, WorkerLoadInfo* aLoadInfo,
|
2015-02-12 12:50:05 +03:00
|
|
|
ErrorResult& aRv) {
|
2013-11-05 18:16:24 +04:00
|
|
|
WorkerPrivate* parent =
|
|
|
|
NS_IsMainThread() ? nullptr : GetCurrentThreadWorkerPrivate();
|
2018-06-18 23:37:21 +03:00
|
|
|
|
|
|
|
// If this is a sub-worker, we need to keep the parent worker alive until this
|
|
|
|
// one is registered.
|
|
|
|
RefPtr<StrongWorkerRef> workerRef;
|
2013-11-05 18:16:24 +04:00
|
|
|
if (parent) {
|
|
|
|
parent->AssertIsOnWorkerThread();
|
2017-05-25 10:04:25 +03:00
|
|
|
|
2018-06-18 23:37:21 +03:00
|
|
|
workerRef = StrongWorkerRef::Create(parent, "WorkerPrivate::Constructor");
|
|
|
|
if (NS_WARN_IF(!workerRef)) {
|
2017-05-25 10:04:25 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
} else {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
Maybe<WorkerLoadInfo> stackLoadInfo;
|
2013-06-05 18:04:23 +04:00
|
|
|
if (!aLoadInfo) {
|
2014-08-14 02:39:41 +04:00
|
|
|
stackLoadInfo.emplace();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2014-05-29 20:19:00 +04:00
|
|
|
nsresult rv =
|
|
|
|
GetLoadInfo(aCx, nullptr, parent, aScriptURL, aIsChromeWorker,
|
2015-06-17 04:21:08 +03:00
|
|
|
InheritLoadGroup, aWorkerType, stackLoadInfo.ptr());
|
2016-02-24 18:38:31 +03:00
|
|
|
aRv.MightThrowJSException();
|
2013-06-05 18:04:23 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2018-01-31 10:22:03 +03:00
|
|
|
workerinternals::ReportLoadError(aRv, rv, aScriptURL);
|
2013-06-05 18:04:23 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-14 02:39:41 +04:00
|
|
|
aLoadInfo = stackLoadInfo.ptr();
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:24 +04:00
|
|
|
// NB: This has to be done before creating the WorkerPrivate, because it will
|
|
|
|
// attempt to use static variables that are initialized in the RuntimeService
|
|
|
|
// constructor.
|
2018-01-31 10:24:59 +03:00
|
|
|
RuntimeService* runtimeService;
|
2013-11-05 18:16:24 +04:00
|
|
|
|
|
|
|
if (!parent) {
|
2018-01-31 10:24:59 +03:00
|
|
|
runtimeService = RuntimeService::GetOrCreateService();
|
2013-11-05 18:16:24 +04:00
|
|
|
if (!runtimeService) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-31 10:24:59 +03:00
|
|
|
runtimeService = RuntimeService::GetService();
|
2013-11-05 18:16:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(runtimeService);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerPrivate> worker =
|
2016-03-12 00:43:30 +03:00
|
|
|
new WorkerPrivate(parent, aScriptURL, aIsChromeWorker, aWorkerType,
|
2017-05-17 17:49:34 +03:00
|
|
|
aWorkerName, aServiceWorkerScope, *aLoadInfo);
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2016-08-08 23:33:39 +03:00
|
|
|
// Gecko contexts always have an explicitly-set default locale (set by
|
|
|
|
// XPJSRuntime::Initialize for the main thread, set by
|
|
|
|
// WorkerThreadPrimaryRunnable::Run for workers just before running worker
|
|
|
|
// code), so this is never SpiderMonkey's builtin default locale.
|
|
|
|
JS::UniqueChars defaultLocale = JS_GetDefaultLocale(aCx);
|
|
|
|
if (NS_WARN_IF(!defaultLocale)) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
worker->mDefaultLocale = std::move(defaultLocale);
|
2016-08-08 23:33:39 +03:00
|
|
|
|
2016-03-28 20:28:14 +03:00
|
|
|
if (!runtimeService->RegisterWorker(worker)) {
|
2013-11-05 18:16:24 +04:00
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-27 20:00:05 +03:00
|
|
|
worker->EnableDebugger();
|
|
|
|
|
2017-02-14 18:06:39 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(worker->PrincipalIsValid());
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CompileScriptRunnable> compiler =
|
2015-03-17 13:15:19 +03:00
|
|
|
new CompileScriptRunnable(worker, aScriptURL);
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!compiler->Dispatch()) {
|
2013-11-05 18:16:24 +04:00
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
2013-06-05 18:04:23 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-02-02 22:08:50 +04:00
|
|
|
worker->mSelfRef = worker;
|
2013-11-05 18:16:24 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
return worker.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2016-01-30 20:05:36 +03:00
|
|
|
nsresult WorkerPrivate::GetLoadInfo(JSContext* aCx, nsPIDOMWindowInner* aWindow,
|
2013-06-05 18:04:23 +04:00
|
|
|
WorkerPrivate* aParent,
|
|
|
|
const nsAString& aScriptURL,
|
2015-02-21 18:09:17 +03:00
|
|
|
bool aIsChromeWorker,
|
|
|
|
LoadGroupBehavior aLoadGroupBehavior,
|
2015-06-17 04:21:08 +03:00
|
|
|
WorkerType aWorkerType,
|
2015-02-12 12:50:05 +03:00
|
|
|
WorkerLoadInfo* aLoadInfo) {
|
2018-01-31 10:22:03 +03:00
|
|
|
using namespace mozilla::dom::workerinternals;
|
2012-09-15 22:51:55 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(aCx);
|
2014-05-07 02:43:02 +04:00
|
|
|
MOZ_ASSERT_IF(NS_IsMainThread(),
|
|
|
|
aCx == nsContentUtils::GetCurrentJSContext());
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (aWindow) {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
WorkerLoadInfo loadInfo;
|
2013-06-05 18:04:23 +04:00
|
|
|
nsresult rv;
|
2012-09-17 04:20:16 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
if (aParent) {
|
|
|
|
aParent->AssertIsOnWorkerThread();
|
|
|
|
|
2013-03-13 00:33:40 +04:00
|
|
|
// If the parent is going away give up now.
|
2018-01-31 10:23:44 +03:00
|
|
|
WorkerStatus parentStatus;
|
2013-03-13 00:33:40 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(aParent->mMutex);
|
2013-06-05 18:04:23 +04:00
|
|
|
parentStatus = aParent->mStatus;
|
2013-03-13 00:33:40 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (parentStatus > Running) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2013-03-13 00:33:40 +04:00
|
|
|
}
|
|
|
|
|
2017-02-07 18:28:38 +03:00
|
|
|
// Passing a pointer to our stack loadInfo is safe here because this
|
|
|
|
// method uses a sync runnable to get the channel from the main thread.
|
2013-06-05 18:04:23 +04:00
|
|
|
rv = ChannelFromScriptURLWorkerThread(aCx, aParent, aScriptURL, loadInfo);
|
2017-02-07 18:28:39 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2017-02-07 18:28:39 +03:00
|
|
|
MOZ_ALWAYS_TRUE(loadInfo.ProxyReleaseMainThreadObjects(aParent));
|
2017-02-07 18:28:39 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
// Now that we've spun the loop there's no guarantee that our parent is
|
|
|
|
// still alive. We may have received control messages initiating shutdown.
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(aParent->mMutex);
|
|
|
|
parentStatus = aParent->mStatus;
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
if (parentStatus > Running) {
|
2017-02-07 18:28:39 +03:00
|
|
|
MOZ_ALWAYS_TRUE(loadInfo.ProxyReleaseMainThreadObjects(aParent));
|
2013-06-05 18:04:23 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadInfo.mDomain = aParent->Domain();
|
2014-12-17 09:26:15 +03:00
|
|
|
loadInfo.mFromWindow = aParent->IsFromWindow();
|
|
|
|
loadInfo.mWindowID = aParent->WindowID();
|
2015-07-16 00:01:02 +03:00
|
|
|
loadInfo.mStorageAllowed = aParent->IsStorageAllowed();
|
2016-09-20 04:13:00 +03:00
|
|
|
loadInfo.mOriginAttributes = aParent->GetOriginAttributes();
|
2015-06-28 06:19:24 +03:00
|
|
|
loadInfo.mServiceWorkersTestingInWindow =
|
|
|
|
aParent->ServiceWorkersTestingInWindow();
|
2018-01-23 18:38:54 +03:00
|
|
|
loadInfo.mParentController = aParent->GetController();
|
2013-06-05 18:04:23 +04:00
|
|
|
} else {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-07-16 00:01:02 +03:00
|
|
|
// Make sure that the IndexedDatabaseManager is set up
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused << NS_WARN_IF(!IndexedDatabaseManager::GetOrCreate());
|
2015-07-16 00:01:02 +03:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(ssm);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2017-02-01 23:43:38 +03:00
|
|
|
bool isChrome = nsContentUtils::IsSystemCaller(aCx);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
// First check to make sure the caller has permission to make a privileged
|
|
|
|
// worker if they called the ChromeWorker/ChromeSharedWorker constructor.
|
2011-07-17 23:09:13 +04:00
|
|
|
if (aIsChromeWorker && !isChrome) {
|
2013-06-05 18:04:23 +04:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2015-06-10 00:34:00 +03:00
|
|
|
// Chrome callers (whether creating a ChromeWorker or Worker) always get the
|
|
|
|
// system principal here as they're allowed to load anything. The script
|
|
|
|
// loader will refuse to run any script that does not also have the system
|
|
|
|
// principal.
|
2013-06-05 18:04:23 +04:00
|
|
|
if (isChrome) {
|
2017-12-07 14:29:05 +03:00
|
|
|
rv = ssm->GetSystemPrincipal(getter_AddRefs(loadInfo.mLoadingPrincipal));
|
2013-06-05 18:04:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-06-10 00:34:00 +03:00
|
|
|
|
|
|
|
loadInfo.mPrincipalIsSystem = true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
// See if we're being called from a window.
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> globalWindow = aWindow;
|
2013-06-05 18:04:23 +04:00
|
|
|
if (!globalWindow) {
|
2018-07-09 13:26:59 +03:00
|
|
|
globalWindow = xpc::CurrentWindowOrNull(aCx);
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
nsCOMPtr<nsIDocument> document;
|
2018-01-23 18:38:54 +03:00
|
|
|
Maybe<ClientInfo> clientInfo;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
if (globalWindow) {
|
2011-07-17 23:09:13 +04:00
|
|
|
// Only use the current inner window, and only use it if the caller can
|
|
|
|
// access it.
|
2016-01-30 20:05:36 +03:00
|
|
|
if (nsPIDOMWindowOuter* outerWindow = globalWindow->GetOuterWindow()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
loadInfo.mWindow = outerWindow->GetCurrentInnerWindow();
|
2015-06-28 06:19:24 +03:00
|
|
|
// TODO: fix this for SharedWorkers with multiple documents (bug
|
|
|
|
// 1177935)
|
|
|
|
loadInfo.mServiceWorkersTestingInWindow =
|
|
|
|
outerWindow->GetServiceWorkersTestingEnabled();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (!loadInfo.mWindow ||
|
|
|
|
(globalWindow != loadInfo.mWindow &&
|
|
|
|
!nsContentUtils::CanCallerAccess(loadInfo.mWindow))) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(loadInfo.mWindow);
|
|
|
|
MOZ_ASSERT(sgo);
|
|
|
|
|
|
|
|
loadInfo.mScriptContext = sgo->GetContext();
|
|
|
|
NS_ENSURE_TRUE(loadInfo.mScriptContext, NS_ERROR_FAILURE);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// If we're called from a window then we can dig out the principal and URI
|
|
|
|
// from the document.
|
2013-06-05 18:04:23 +04:00
|
|
|
document = loadInfo.mWindow->GetExtantDoc();
|
|
|
|
NS_ENSURE_TRUE(document, NS_ERROR_FAILURE);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
loadInfo.mBaseURI = document->GetDocBaseURI();
|
2014-12-12 19:06:00 +03:00
|
|
|
loadInfo.mLoadGroup = document->GetDocumentLoadGroup();
|
2017-05-15 16:52:00 +03:00
|
|
|
NS_ENSURE_TRUE(loadInfo.mLoadGroup, NS_ERROR_FAILURE);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-01-23 18:38:54 +03:00
|
|
|
clientInfo = globalWindow->GetClientInfo();
|
|
|
|
|
2017-12-07 14:29:05 +03:00
|
|
|
// Use the document's NodePrincipal as loading principal if we're not
|
2011-07-17 23:09:13 +04:00
|
|
|
// being called from chrome.
|
2017-12-07 14:29:05 +03:00
|
|
|
if (!loadInfo.mLoadingPrincipal) {
|
|
|
|
loadInfo.mLoadingPrincipal = document->NodePrincipal();
|
|
|
|
NS_ENSURE_TRUE(loadInfo.mLoadingPrincipal, NS_ERROR_FAILURE);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-01-09 01:53:32 +04:00
|
|
|
// We use the document's base domain to limit the number of workers
|
|
|
|
// each domain can create. For sandboxed documents, we use the domain
|
|
|
|
// of their first non-sandboxed document, walking up until we find
|
|
|
|
// one. If we can't find one, we fall back to using the GUID of the
|
|
|
|
// null principal as the base domain.
|
|
|
|
if (document->GetSandboxFlags() & SANDBOXED_ORIGIN) {
|
|
|
|
nsCOMPtr<nsIDocument> tmpDoc = document;
|
|
|
|
do {
|
|
|
|
tmpDoc = tmpDoc->GetParentDocument();
|
|
|
|
} while (tmpDoc && tmpDoc->GetSandboxFlags() & SANDBOXED_ORIGIN);
|
|
|
|
|
|
|
|
if (tmpDoc) {
|
|
|
|
// There was an unsandboxed ancestor, yay!
|
|
|
|
nsCOMPtr<nsIPrincipal> tmpPrincipal = tmpDoc->NodePrincipal();
|
2013-06-05 18:04:23 +04:00
|
|
|
rv = tmpPrincipal->GetBaseDomain(loadInfo.mDomain);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-01-09 01:53:32 +04:00
|
|
|
} else {
|
|
|
|
// No unsandboxed ancestor, use our GUID.
|
2017-12-07 14:29:05 +03:00
|
|
|
rv = loadInfo.mLoadingPrincipal->GetBaseDomain(loadInfo.mDomain);
|
2013-06-05 18:04:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
2012-08-20 22:34:32 +04:00
|
|
|
} else {
|
2013-01-09 01:53:32 +04:00
|
|
|
// Document creating the worker is not sandboxed.
|
2017-12-07 14:29:05 +03:00
|
|
|
rv = loadInfo.mLoadingPrincipal->GetBaseDomain(loadInfo.mDomain);
|
2013-06-05 18:04:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
2012-09-17 04:20:16 +04:00
|
|
|
|
2017-05-15 16:52:00 +03:00
|
|
|
NS_ENSURE_TRUE(NS_LoadGroupMatchesPrincipal(loadInfo.mLoadGroup,
|
2017-12-07 14:29:05 +03:00
|
|
|
loadInfo.mLoadingPrincipal),
|
2017-05-15 16:52:00 +03:00
|
|
|
NS_ERROR_FAILURE);
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
uint32_t perm;
|
2017-12-07 14:29:05 +03:00
|
|
|
rv = permMgr->TestPermissionFromPrincipal(loadInfo.mLoadingPrincipal,
|
|
|
|
"systemXHR", &perm);
|
2013-06-05 18:04:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
loadInfo.mXHRParamsAllowed = perm == nsIPermissionManager::ALLOW_ACTION;
|
2014-08-20 21:34:14 +04:00
|
|
|
|
2014-12-17 09:26:15 +03:00
|
|
|
loadInfo.mFromWindow = true;
|
|
|
|
loadInfo.mWindowID = globalWindow->WindowID();
|
2015-07-16 00:01:02 +03:00
|
|
|
nsContentUtils::StorageAccess access =
|
|
|
|
nsContentUtils::StorageAllowedForWindow(globalWindow);
|
|
|
|
loadInfo.mStorageAllowed = access > nsContentUtils::StorageAccess::eDeny;
|
2016-09-20 04:13:00 +03:00
|
|
|
loadInfo.mOriginAttributes =
|
|
|
|
nsContentUtils::GetOriginAttributes(document);
|
2018-01-23 18:38:54 +03:00
|
|
|
loadInfo.mParentController = globalWindow->GetController();
|
2018-11-20 02:18:21 +03:00
|
|
|
loadInfo.mSecureContext = loadInfo.mWindow->IsSecureContext()
|
|
|
|
? WorkerLoadInfo::eSecureContext
|
|
|
|
: WorkerLoadInfo::eInsecureContext;
|
2013-06-05 18:04:23 +04:00
|
|
|
} else {
|
|
|
|
// Not a window
|
|
|
|
MOZ_ASSERT(isChrome);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// We're being created outside of a window. Need to figure out the script
|
|
|
|
// that is creating us in order for us to use relative URIs later on.
|
2016-03-09 13:20:11 +03:00
|
|
|
JS::AutoFilename fileName;
|
2014-02-25 19:43:14 +04:00
|
|
|
if (JS::DescribeScriptedCaller(aCx, &fileName)) {
|
2013-11-06 18:05:17 +04:00
|
|
|
// In most cases, fileName is URI. In a few other cases
|
|
|
|
// (e.g. xpcshell), fileName is a file path. Ideally, we would
|
|
|
|
// prefer testing whether fileName parses as an URI and fallback
|
|
|
|
// to file path in case of error, but Windows file paths have
|
|
|
|
// the interesting property that they can be parsed as bogus
|
|
|
|
// URIs (e.g. C:/Windows/Tmp is interpreted as scheme "C",
|
|
|
|
// hostname "Windows", path "Tmp"), which defeats this algorithm.
|
|
|
|
// Therefore, we adopt the opposite convention.
|
|
|
|
nsCOMPtr<nsIFile> scriptFile =
|
|
|
|
do_CreateInstance("@mozilla.org/file/local;1", &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2012-09-17 04:20:16 +04:00
|
|
|
|
2014-02-25 19:43:14 +04:00
|
|
|
rv = scriptFile->InitWithPath(NS_ConvertUTF8toUTF16(fileName.get()));
|
2013-11-06 18:05:17 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = NS_NewFileURI(getter_AddRefs(loadInfo.mBaseURI), scriptFile);
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// As expected, fileName is not a path, so proceed with
|
|
|
|
// a uri.
|
|
|
|
rv = NS_NewURI(getter_AddRefs(loadInfo.mBaseURI), fileName.get());
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
loadInfo.mXHRParamsAllowed = true;
|
2014-12-17 09:26:15 +03:00
|
|
|
loadInfo.mFromWindow = false;
|
|
|
|
loadInfo.mWindowID = UINT64_MAX;
|
2015-07-16 00:01:02 +03:00
|
|
|
loadInfo.mStorageAllowed = true;
|
2017-01-12 19:38:48 +03:00
|
|
|
loadInfo.mOriginAttributes = OriginAttributes();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2017-12-07 14:29:05 +03:00
|
|
|
MOZ_ASSERT(loadInfo.mLoadingPrincipal);
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(isChrome || !loadInfo.mDomain.IsEmpty());
|
2012-09-15 22:51:55 +04:00
|
|
|
|
2015-02-21 18:09:17 +03:00
|
|
|
if (!loadInfo.mLoadGroup || aLoadGroupBehavior == OverrideLoadGroup) {
|
2017-12-07 14:29:05 +03:00
|
|
|
OverrideLoadInfoLoadGroup(loadInfo, loadInfo.mLoadingPrincipal);
|
2014-12-12 19:06:00 +03:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(NS_LoadGroupMatchesPrincipal(loadInfo.mLoadGroup,
|
2017-12-07 14:29:05 +03:00
|
|
|
loadInfo.mLoadingPrincipal));
|
2014-12-12 19:06:00 +03:00
|
|
|
|
2016-04-28 10:44:08 +03:00
|
|
|
// Top level workers' main script use the document charset for the script
|
|
|
|
// uri encoding.
|
2018-11-20 02:18:34 +03:00
|
|
|
nsCOMPtr<nsIURI> url;
|
|
|
|
rv = nsContentUtils::NewURIWithDocumentCharset(
|
|
|
|
getter_AddRefs(url), aScriptURL, document, loadInfo.mBaseURI);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2017-12-07 14:29:05 +03:00
|
|
|
rv = ChannelFromScriptURLMainThread(loadInfo.mLoadingPrincipal, document,
|
2014-12-12 19:06:00 +03:00
|
|
|
loadInfo.mLoadGroup, url, clientInfo,
|
2015-06-17 04:21:08 +03:00
|
|
|
ContentPolicyType(aWorkerType),
|
2013-06-05 18:04:23 +04:00
|
|
|
getter_AddRefs(loadInfo.mChannel));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-03-13 00:33:40 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
rv = NS_GetFinalChannelURI(loadInfo.mChannel,
|
|
|
|
getter_AddRefs(loadInfo.mResolvedScriptURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2017-02-07 18:28:39 +03:00
|
|
|
|
|
|
|
rv = loadInfo.SetPrincipalFromChannel(loadInfo.mChannel);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2017-12-07 14:29:05 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(loadInfo.mLoadingPrincipal);
|
2017-02-14 18:06:39 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(loadInfo.PrincipalIsValid());
|
|
|
|
|
2018-11-21 23:02:51 +03:00
|
|
|
*aLoadInfo = std::move(loadInfo);
|
2013-06-05 18:04:23 +04:00
|
|
|
return NS_OK;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2015-02-21 18:09:17 +03:00
|
|
|
// static
|
2017-12-07 14:29:05 +03:00
|
|
|
void WorkerPrivate::OverrideLoadInfoLoadGroup(WorkerLoadInfo& aLoadInfo,
|
|
|
|
nsIPrincipal* aPrincipal) {
|
2015-02-21 18:09:17 +03:00
|
|
|
MOZ_ASSERT(!aLoadInfo.mInterfaceRequestor);
|
2018-01-06 03:50:09 +03:00
|
|
|
MOZ_ASSERT(aLoadInfo.mLoadingPrincipal == aPrincipal);
|
2015-02-21 18:09:17 +03:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
aLoadInfo.mInterfaceRequestor =
|
2017-12-07 14:29:05 +03:00
|
|
|
new WorkerLoadInfo::InterfaceRequestor(aPrincipal, aLoadInfo.mLoadGroup);
|
2015-02-21 18:09:17 +03:00
|
|
|
aLoadInfo.mInterfaceRequestor->MaybeAddTabChild(aLoadInfo.mLoadGroup);
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
// NOTE: this defaults the load context to:
|
|
|
|
// - private browsing = false
|
|
|
|
// - content = true
|
|
|
|
// - use remote tabs = false
|
2015-02-21 18:09:17 +03:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = do_CreateInstance(NS_LOADGROUP_CONTRACTID);
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
loadGroup->SetNotificationCallbacks(aLoadInfo.mInterfaceRequestor);
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(rv);
|
2015-02-21 18:09:17 +03:00
|
|
|
|
|
|
|
aLoadInfo.mLoadGroup = loadGroup.forget();
|
2017-05-15 16:52:00 +03:00
|
|
|
|
2017-12-07 14:29:05 +03:00
|
|
|
MOZ_ASSERT(NS_LoadGroupMatchesPrincipal(aLoadInfo.mLoadGroup, aPrincipal));
|
2015-02-21 18:09:17 +03:00
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
void WorkerPrivate::DoRunLoop(JSContext* aCx) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(mThread);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mJSContext = aCx;
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(mStatus == Pending);
|
2011-07-17 23:09:13 +04:00
|
|
|
mStatus = Running;
|
|
|
|
}
|
|
|
|
|
2016-02-26 23:23:13 +03:00
|
|
|
// Now that we've done that, we can go ahead and set up our AutoJSAPI. We
|
|
|
|
// can't before this point, because it can't find the right JSContext before
|
|
|
|
// then, since it gets it from our mJSContext.
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
MOZ_ASSERT(jsapi.cx() == aCx);
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
EnableMemoryReporter();
|
2012-01-18 00:05:25 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
InitializeGCTimers();
|
2012-01-18 00:05:25 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
for (;;) {
|
2018-07-12 20:33:41 +03:00
|
|
|
WorkerStatus currentStatus;
|
2015-03-04 17:11:32 +03:00
|
|
|
bool debuggerRunnablesPending = false;
|
2013-10-23 17:16:49 +04:00
|
|
|
bool normalRunnablesPending = false;
|
2012-01-18 00:05:25 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
while (mControlQueue.IsEmpty() &&
|
2015-03-04 17:11:32 +03:00
|
|
|
!(debuggerRunnablesPending = !mDebuggerQueue.IsEmpty()) &&
|
2013-10-23 17:16:49 +04:00
|
|
|
!(normalRunnablesPending = NS_HasPendingEvents(mThread))) {
|
2012-12-30 22:21:52 +04:00
|
|
|
WaitForWorkerEvents();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-05-11 21:45:58 +03:00
|
|
|
auto result = ProcessAllControlRunnablesLocked();
|
|
|
|
if (result != ProcessAllControlRunnablesResult::Nothing) {
|
|
|
|
// NB: There's no JS on the stack here, so Abort vs MayContinue is
|
|
|
|
// irrelevant
|
|
|
|
|
|
|
|
// The state of the world may have changed, recheck it.
|
|
|
|
normalRunnablesPending = NS_HasPendingEvents(mThread);
|
|
|
|
// The debugger queue doesn't get cleared, so we can ignore that.
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
currentStatus = mStatus;
|
|
|
|
}
|
|
|
|
|
2016-09-01 07:33:05 +03:00
|
|
|
// if all holders are done then we can kill this thread.
|
2016-06-23 11:53:14 +03:00
|
|
|
if (currentStatus != Running && !HasActiveHolders()) {
|
2018-07-12 20:33:41 +03:00
|
|
|
// Now we are ready to kill the worker thread.
|
|
|
|
if (currentStatus == Canceling) {
|
2018-03-13 17:25:50 +03:00
|
|
|
NotifyInternal(Killing);
|
2016-02-27 05:15:56 +03:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
currentStatus = mStatus;
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(currentStatus == Killing);
|
2011-07-17 23:09:13 +04:00
|
|
|
#else
|
|
|
|
currentStatus = Killing;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're supposed to die then we should exit the loop.
|
|
|
|
if (currentStatus == Killing) {
|
2018-05-02 16:29:26 +03:00
|
|
|
// The ClientSource should be cleared in NotifyInternal() when we reach
|
2018-07-12 20:33:41 +03:00
|
|
|
// or pass Canceling.
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!data->mClientSource);
|
2018-05-02 16:29:26 +03:00
|
|
|
|
2015-04-10 18:27:57 +03:00
|
|
|
// Flush uncaught rejections immediately, without
|
|
|
|
// waiting for a next tick.
|
|
|
|
PromiseDebugging::FlushUncaughtRejections();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
ShutdownGCTimers();
|
2012-01-18 00:05:25 +04:00
|
|
|
|
2011-09-09 04:03:03 +04:00
|
|
|
DisableMemoryReporter();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
mStatus = Dead;
|
|
|
|
mJSContext = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// After mStatus is set to Dead there can be no more
|
|
|
|
// WorkerControlRunnables so no need to lock here.
|
|
|
|
if (!mControlQueue.IsEmpty()) {
|
2017-07-21 18:16:23 +03:00
|
|
|
WorkerControlRunnable* runnable = nullptr;
|
2013-10-23 17:16:49 +04:00
|
|
|
while (mControlQueue.Pop(runnable)) {
|
|
|
|
runnable->Cancel();
|
|
|
|
runnable->Release();
|
|
|
|
}
|
|
|
|
}
|
2013-11-05 18:16:26 +04:00
|
|
|
|
2015-03-04 02:51:53 +03:00
|
|
|
// Unroot the globals
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mScope = nullptr;
|
|
|
|
data->mDebuggerScope = nullptr;
|
2013-11-05 18:16:26 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2015-03-04 17:11:32 +03:00
|
|
|
if (debuggerRunnablesPending || normalRunnablesPending) {
|
|
|
|
// Start the periodic GC timer if it is not already running.
|
|
|
|
SetGCTimerMode(PeriodicTimer);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
2015-03-04 17:11:32 +03:00
|
|
|
if (debuggerRunnablesPending) {
|
2017-07-21 18:16:23 +03:00
|
|
|
WorkerRunnable* runnable = nullptr;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2015-03-04 17:11:32 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2015-03-04 17:11:32 +03:00
|
|
|
mDebuggerQueue.Pop(runnable);
|
|
|
|
debuggerRunnablesPending = !mDebuggerQueue.IsEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(runnable);
|
|
|
|
static_cast<nsIRunnable*>(runnable)->Run();
|
|
|
|
runnable->Release();
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
CycleCollectedJSContext* ccjs = CycleCollectedJSContext::Get();
|
2018-04-16 10:33:55 +03:00
|
|
|
ccjs->PerformDebuggerMicroTaskCheckpoint();
|
2016-03-24 18:12:00 +03:00
|
|
|
|
2015-03-04 17:11:32 +03:00
|
|
|
if (debuggerRunnablesPending) {
|
|
|
|
WorkerDebuggerGlobalScope* globalScope = DebuggerGlobalScope();
|
|
|
|
MOZ_ASSERT(globalScope);
|
2014-10-28 15:08:19 +03:00
|
|
|
|
2015-03-04 17:11:32 +03:00
|
|
|
// Now *might* be a good time to GC. Let the JS engine make the
|
|
|
|
// decision.
|
2018-07-31 09:37:17 +03:00
|
|
|
JSAutoRealm ar(aCx, globalScope->GetGlobalJSObject());
|
2015-03-04 17:11:32 +03:00
|
|
|
JS_MaybeGC(aCx);
|
|
|
|
}
|
|
|
|
} else if (normalRunnablesPending) {
|
|
|
|
// Process a single runnable from the main queue.
|
2016-09-01 07:33:05 +03:00
|
|
|
NS_ProcessNextEvent(mThread, false);
|
2015-03-04 17:11:32 +03:00
|
|
|
|
|
|
|
normalRunnablesPending = NS_HasPendingEvents(mThread);
|
|
|
|
if (normalRunnablesPending && GlobalScope()) {
|
|
|
|
// Now *might* be a good time to GC. Let the JS engine make the
|
|
|
|
// decision.
|
2018-07-31 09:37:17 +03:00
|
|
|
JSAutoRealm ar(aCx, GlobalScope()->GetGlobalJSObject());
|
2013-10-23 17:16:49 +04:00
|
|
|
JS_MaybeGC(aCx);
|
|
|
|
}
|
|
|
|
}
|
2015-03-04 17:11:32 +03:00
|
|
|
|
2015-11-18 06:45:43 +03:00
|
|
|
if (!debuggerRunnablesPending && !normalRunnablesPending) {
|
2015-03-04 17:11:32 +03:00
|
|
|
// Both the debugger event queue and the normal event queue has been
|
|
|
|
// exhausted, cancel the periodic GC timer and schedule the idle GC timer.
|
2013-10-23 17:16:49 +04:00
|
|
|
SetGCTimerMode(IdleTimer);
|
|
|
|
}
|
2016-09-14 06:14:02 +03:00
|
|
|
|
|
|
|
// If the worker thread is spamming the main thread faster than it can
|
2018-10-23 09:30:30 +03:00
|
|
|
// process the work, then pause the worker thread until the main thread
|
|
|
|
// catches up.
|
|
|
|
size_t queuedEvents = mMainThreadEventTarget->Length() +
|
|
|
|
mMainThreadDebuggeeEventTarget->Length();
|
|
|
|
if (queuedEvents > 5000) {
|
2018-10-17 11:44:43 +03:00
|
|
|
mMainThreadEventTarget->AwaitIdle();
|
2016-09-14 06:14:02 +03:00
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
2014-04-20 11:36:40 +04:00
|
|
|
MOZ_CRASH("Shouldn't get here!");
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
void WorkerPrivate::OnProcessNextEvent() {
|
2013-10-23 17:16:49 +04:00
|
|
|
AssertIsOnWorkerThread();
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
|
2016-09-14 16:47:32 +03:00
|
|
|
uint32_t recursionDepth = CycleCollectedJSContext::Get()->RecursionDepth();
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
MOZ_ASSERT(recursionDepth);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
// Normally we process control runnables in DoRunLoop or RunCurrentSyncLoop.
|
|
|
|
// However, it's possible that non-worker C++ could spin its own nested event
|
|
|
|
// loop, and in that case we must ensure that we continue to process control
|
|
|
|
// runnables here.
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
if (recursionDepth > 1 && mSyncLoopStack.Length() < recursionDepth - 1) {
|
2016-05-11 21:45:58 +03:00
|
|
|
Unused << ProcessAllControlRunnables();
|
|
|
|
// There's no running JS, and no state to revalidate, so we can ignore the
|
|
|
|
// return value.
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
void WorkerPrivate::AfterProcessNextEvent() {
|
2013-10-23 17:16:49 +04:00
|
|
|
AssertIsOnWorkerThread();
|
2016-09-14 16:47:32 +03:00
|
|
|
MOZ_ASSERT(CycleCollectedJSContext::Get()->RecursionDepth());
|
2014-12-17 09:26:15 +03:00
|
|
|
}
|
|
|
|
|
2016-09-14 06:14:02 +03:00
|
|
|
nsIEventTarget* WorkerPrivate::MainThreadEventTarget() {
|
|
|
|
return mMainThreadEventTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult WorkerPrivate::DispatchToMainThread(nsIRunnable* aRunnable,
|
|
|
|
uint32_t aFlags) {
|
|
|
|
nsCOMPtr<nsIRunnable> r = aRunnable;
|
|
|
|
return DispatchToMainThread(r.forget(), aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult WorkerPrivate::DispatchToMainThread(
|
|
|
|
already_AddRefed<nsIRunnable> aRunnable, uint32_t aFlags) {
|
2018-05-30 22:15:35 +03:00
|
|
|
return mMainThreadEventTarget->Dispatch(std::move(aRunnable), aFlags);
|
2016-09-14 06:14:02 +03:00
|
|
|
}
|
|
|
|
|
2018-10-23 09:30:30 +03:00
|
|
|
nsresult WorkerPrivate::DispatchDebuggeeToMainThread(
|
|
|
|
already_AddRefed<WorkerDebuggeeRunnable> aRunnable, uint32_t aFlags) {
|
|
|
|
return mMainThreadDebuggeeEventTarget->Dispatch(std::move(aRunnable), aFlags);
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:01:33 +03:00
|
|
|
nsISerialEventTarget* WorkerPrivate::ControlEventTarget() {
|
|
|
|
return mWorkerControlEventTarget;
|
|
|
|
}
|
|
|
|
|
2017-07-21 18:16:24 +03:00
|
|
|
nsISerialEventTarget* WorkerPrivate::HybridEventTarget() {
|
|
|
|
return mWorkerHybridEventTarget;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:13:04 +03:00
|
|
|
bool WorkerPrivate::EnsureClientSource() {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2017-11-21 23:13:04 +03:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if (data->mClientSource) {
|
2017-11-27 22:48:17 +03:00
|
|
|
return true;
|
2017-11-21 23:13:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ClientType type;
|
|
|
|
switch (Type()) {
|
|
|
|
case WorkerTypeDedicated:
|
|
|
|
type = ClientType::Worker;
|
|
|
|
break;
|
|
|
|
case WorkerTypeShared:
|
|
|
|
type = ClientType::Sharedworker;
|
|
|
|
break;
|
|
|
|
case WorkerTypeService:
|
|
|
|
type = ClientType::Serviceworker;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("unknown worker type!");
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mClientSource = ClientManager::CreateSource(
|
|
|
|
type, mWorkerHybridEventTarget, GetPrincipalInfo());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(data->mClientSource);
|
2017-11-27 22:48:17 +03:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if (data->mFrozen) {
|
|
|
|
data->mClientSource->Freeze();
|
2017-11-21 23:13:05 +03:00
|
|
|
}
|
2017-11-27 22:48:17 +03:00
|
|
|
|
2017-12-06 04:45:22 +03:00
|
|
|
// Shortly after the client is reserved we will try loading the main script
|
|
|
|
// for the worker. This may get intercepted by the ServiceWorkerManager
|
|
|
|
// which will then try to create a ClientHandle. Its actually possible for
|
|
|
|
// the main thread to create this ClientHandle before our IPC message creating
|
|
|
|
// the ClientSource completes. To avoid this race we synchronously ping our
|
|
|
|
// parent Client actor here. This ensure the worker ClientSource is created
|
|
|
|
// in the parent before the main thread might try reaching it with a
|
|
|
|
// ClientHandle.
|
|
|
|
//
|
|
|
|
// An alternative solution would have been to handle the out-of-order
|
|
|
|
// operations on the parent side. We could have created a small window where
|
|
|
|
// we allow ClientHandle objects to exist without a ClientSource. We would
|
|
|
|
// then time out these handles if they stayed orphaned for too long. This
|
|
|
|
// approach would be much more complex, but also avoid this extra bit of
|
|
|
|
// latency when starting workers.
|
|
|
|
//
|
|
|
|
// Note, we only have to do this for workers that can be controlled by a
|
|
|
|
// service worker. So avoid the sync overhead here if we are starting a
|
|
|
|
// service worker or a chrome worker.
|
|
|
|
if (Type() != WorkerTypeService && !IsChromeWorker()) {
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mClientSource->WorkerSyncPing(this);
|
2017-12-06 04:45:22 +03:00
|
|
|
}
|
|
|
|
|
2017-11-27 22:48:17 +03:00
|
|
|
return true;
|
2017-11-21 23:13:04 +03:00
|
|
|
}
|
|
|
|
|
2018-07-10 19:53:03 +03:00
|
|
|
bool WorkerPrivate::EnsureCSPEventListener() {
|
2018-10-05 08:41:12 +03:00
|
|
|
if (!mCSPEventListener) {
|
|
|
|
mCSPEventListener = WorkerCSPEventListener::Create(this);
|
|
|
|
if (NS_WARN_IF(!mCSPEventListener)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-10 19:53:03 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-23 09:17:13 +03:00
|
|
|
nsICSPEventListener* WorkerPrivate::CSPEventListener() const {
|
|
|
|
MOZ_ASSERT(mCSPEventListener);
|
|
|
|
return mCSPEventListener;
|
|
|
|
}
|
|
|
|
|
2018-03-14 17:27:13 +03:00
|
|
|
void WorkerPrivate::EnsurePerformanceStorage() {
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (!mPerformanceStorage) {
|
|
|
|
mPerformanceStorage = PerformanceStorageWorker::Create(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:13:04 +03:00
|
|
|
Maybe<ClientInfo> WorkerPrivate::GetClientInfo() const {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2018-05-02 16:29:26 +03:00
|
|
|
Maybe<ClientInfo> clientInfo;
|
2018-11-13 23:22:40 +03:00
|
|
|
if (!data->mClientSource) {
|
2018-07-12 20:33:41 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mStatus >= Canceling);
|
2018-06-01 18:59:07 +03:00
|
|
|
return clientInfo;
|
2018-05-02 16:29:26 +03:00
|
|
|
}
|
2018-11-13 23:22:40 +03:00
|
|
|
clientInfo.emplace(data->mClientSource->Info());
|
2018-06-01 18:59:07 +03:00
|
|
|
return clientInfo;
|
2017-11-21 23:13:04 +03:00
|
|
|
}
|
|
|
|
|
2018-01-12 04:46:08 +03:00
|
|
|
const ClientState WorkerPrivate::GetClientState() const {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(data->mClientSource);
|
2018-01-12 04:46:08 +03:00
|
|
|
ClientState state;
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mClientSource->SnapshotState(&state);
|
2018-06-01 18:59:07 +03:00
|
|
|
return state;
|
2018-01-12 04:46:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const Maybe<ServiceWorkerDescriptor> WorkerPrivate::GetController() {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2018-05-04 16:35:36 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2018-07-12 20:33:41 +03:00
|
|
|
if (mStatus >= Canceling) {
|
2018-05-04 16:35:36 +03:00
|
|
|
return Maybe<ServiceWorkerDescriptor>();
|
|
|
|
}
|
|
|
|
}
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(data->mClientSource);
|
|
|
|
return data->mClientSource->GetController();
|
2018-01-12 04:46:08 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 04:45:22 +03:00
|
|
|
void WorkerPrivate::Control(const ServiceWorkerDescriptor& aServiceWorker) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2017-12-06 04:45:22 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!IsChromeWorker());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(Type() != WorkerTypeService);
|
2018-05-04 16:35:36 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2018-07-12 20:33:41 +03:00
|
|
|
if (mStatus >= Canceling) {
|
2018-05-04 16:35:36 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(data->mClientSource);
|
2018-06-22 17:22:59 +03:00
|
|
|
|
|
|
|
if (IsBlobURI(mLoadInfo.mBaseURI)) {
|
|
|
|
// Blob URL workers can only become controlled by inheriting from
|
|
|
|
// their parent. Make sure to note this properly.
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mClientSource->InheritController(aServiceWorker);
|
2018-06-22 17:22:59 +03:00
|
|
|
} else {
|
|
|
|
// Otherwise this is a normal interception and we simply record the
|
|
|
|
// controller locally.
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mClientSource->SetController(aServiceWorker);
|
2018-06-22 17:22:59 +03:00
|
|
|
}
|
2017-12-06 04:45:22 +03:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:13:04 +03:00
|
|
|
void WorkerPrivate::ExecutionReady() {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2018-05-02 16:29:26 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2018-07-12 20:33:41 +03:00
|
|
|
if (mStatus >= Canceling) {
|
2018-05-02 16:29:26 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(data->mClientSource);
|
|
|
|
data->mClientSource->WorkerExecutionReady(this);
|
2017-11-21 23:13:04 +03:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
void WorkerPrivate::InitializeGCTimers() {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2015-11-18 06:45:43 +03:00
|
|
|
// We need a timer for GC. The basic plan is to run a non-shrinking GC
|
2013-10-23 17:16:49 +04:00
|
|
|
// periodically (PERIODIC_GC_TIMER_DELAY_SEC) while the worker is running.
|
|
|
|
// Once the worker goes idle we set a short (IDLE_GC_TIMER_DELAY_SEC) timer to
|
2015-11-18 06:45:43 +03:00
|
|
|
// run a shrinking GC. If the worker receives more messages then the short
|
|
|
|
// timer is canceled and the periodic timer resumes.
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mGCTimer = NS_NewTimer();
|
|
|
|
MOZ_ASSERT(data->mGCTimer);
|
2015-11-18 06:45:43 +03:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mPeriodicGCTimerRunning = false;
|
|
|
|
data->mIdleGCTimerRunning = false;
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void WorkerPrivate::SetGCTimerMode(GCTimerMode aMode) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
|
|
|
MOZ_ASSERT(data->mGCTimer);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if ((aMode == PeriodicTimer && data->mPeriodicGCTimerRunning) ||
|
|
|
|
(aMode == IdleTimer && data->mIdleGCTimerRunning)) {
|
2013-10-23 17:16:49 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(data->mGCTimer->Cancel());
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mPeriodicGCTimerRunning = false;
|
|
|
|
data->mIdleGCTimerRunning = false;
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
LOG(WorkerLog(),
|
|
|
|
("Worker %p canceled GC timer because %s\n", this,
|
2015-11-18 06:45:43 +03:00
|
|
|
aMode == PeriodicTimer ? "periodic"
|
|
|
|
: aMode == IdleTimer ? "idle" : "none"));
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
if (aMode == NoTimer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aMode == PeriodicTimer || aMode == IdleTimer);
|
|
|
|
|
2017-01-26 19:01:33 +03:00
|
|
|
uint32_t delay = 0;
|
|
|
|
int16_t type = nsITimer::TYPE_ONE_SHOT;
|
|
|
|
nsTimerCallbackFunc callback = nullptr;
|
|
|
|
const char* name = nullptr;
|
2015-11-18 06:45:43 +03:00
|
|
|
|
|
|
|
if (aMode == PeriodicTimer) {
|
|
|
|
delay = PERIODIC_GC_TIMER_DELAY_SEC * 1000;
|
|
|
|
type = nsITimer::TYPE_REPEATING_SLACK;
|
2017-01-26 19:01:33 +03:00
|
|
|
callback = PeriodicGCTimerCallback;
|
2018-01-31 10:24:08 +03:00
|
|
|
name = "dom::PeriodicGCTimerCallback";
|
2015-11-18 06:45:43 +03:00
|
|
|
} else {
|
|
|
|
delay = IDLE_GC_TIMER_DELAY_SEC * 1000;
|
|
|
|
type = nsITimer::TYPE_ONE_SHOT;
|
2017-01-26 19:01:33 +03:00
|
|
|
callback = IdleGCTimerCallback;
|
2018-01-31 10:24:08 +03:00
|
|
|
name = "dom::IdleGCTimerCallback";
|
2015-11-18 06:45:43 +03:00
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(data->mGCTimer->SetTarget(mWorkerControlEventTarget));
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(data->mGCTimer->InitWithNamedFuncCallback(
|
|
|
|
callback, this, delay, type, name));
|
2015-11-18 06:45:43 +03:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
if (aMode == PeriodicTimer) {
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
LOG(WorkerLog(), ("Worker %p scheduled periodic GC timer\n", this));
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mPeriodicGCTimerRunning = true;
|
2015-11-18 06:45:43 +03:00
|
|
|
} else {
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
LOG(WorkerLog(), ("Worker %p scheduled idle GC timer\n", this));
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mIdleGCTimerRunning = true;
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorkerPrivate::ShutdownGCTimers() {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ASSERT(data->mGCTimer);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2015-11-18 06:45:43 +03:00
|
|
|
// Always make sure the timer is canceled.
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(data->mGCTimer->Cancel());
|
2013-10-23 17:16:49 +04:00
|
|
|
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
LOG(WorkerLog(), ("Worker %p killed the GC timer\n", this));
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mGCTimer = nullptr;
|
|
|
|
data->mPeriodicGCTimerRunning = false;
|
|
|
|
data->mIdleGCTimerRunning = false;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 01:28:43 +04:00
|
|
|
bool WorkerPrivate::InterruptCallback(JSContext* aCx) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-12-18 14:48:11 +03:00
|
|
|
// If we are here it's because a WorkerControlRunnable has been dispatched.
|
|
|
|
// The runnable could be processed here or it could have already been
|
|
|
|
// processed by a sync event loop.
|
|
|
|
// The most important thing this method must do, is to decide if the JS
|
|
|
|
// execution should continue or not. If the runnable returns an error or if
|
|
|
|
// the worker status is >= Canceling, we should stop the JS execution.
|
|
|
|
|
2016-03-02 00:52:27 +03:00
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(aCx));
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
bool mayContinue = true;
|
2013-10-23 17:16:49 +04:00
|
|
|
bool scheduledIdleGC = false;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
// Run all control events now.
|
2016-05-11 21:45:58 +03:00
|
|
|
auto result = ProcessAllControlRunnables();
|
|
|
|
if (result == ProcessAllControlRunnablesResult::Abort) {
|
|
|
|
mayContinue = false;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
bool mayFreeze = data->mFrozen;
|
2018-12-18 14:48:11 +03:00
|
|
|
|
|
|
|
{
|
2013-06-05 18:04:23 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
2018-12-18 14:48:11 +03:00
|
|
|
|
|
|
|
if (mayFreeze) {
|
|
|
|
mayFreeze = mStatus <= Running;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mStatus >= Canceling) {
|
|
|
|
mayContinue = false;
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
if (!mayContinue || !mayFreeze) {
|
2011-07-17 23:09:13 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
// Cancel the periodic GC timer here before freezing. The idle GC timer
|
2013-10-23 17:16:49 +04:00
|
|
|
// will clean everything up once it runs.
|
|
|
|
if (!scheduledIdleGC) {
|
|
|
|
SetGCTimerMode(IdleTimer);
|
|
|
|
scheduledIdleGC = true;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
while ((mayContinue = MayContinueRunning())) {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (!mControlQueue.IsEmpty()) {
|
|
|
|
break;
|
|
|
|
}
|
2011-08-02 08:06:17 +04:00
|
|
|
|
2018-02-09 23:17:26 +03:00
|
|
|
WaitForWorkerEvents();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mayContinue) {
|
|
|
|
// We want only uncatchable exceptions here.
|
|
|
|
NS_ASSERTION(!JS_IsExceptionPending(aCx),
|
|
|
|
"Should not have an exception set here!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Make sure the periodic timer gets turned back on here.
|
|
|
|
SetGCTimerMode(PeriodicTimer);
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-17 21:51:03 +03:00
|
|
|
void WorkerPrivate::CloseInternal() {
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
NotifyInternal(Closing);
|
|
|
|
}
|
|
|
|
|
2017-05-22 21:26:39 +03:00
|
|
|
bool WorkerPrivate::IsOnCurrentThread() {
|
2013-10-23 17:16:49 +04:00
|
|
|
// May be called on any thread!
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
MOZ_ASSERT(mPRThread);
|
2017-05-22 21:26:39 +03:00
|
|
|
return PR_GetCurrentThread() == mPRThread;
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
2014-03-05 03:09:23 +04:00
|
|
|
void WorkerPrivate::ScheduleDeletion(WorkerRanOrNot aRanOrNot) {
|
2018-11-13 23:22:40 +03:00
|
|
|
{
|
|
|
|
// mWorkerThreadAccessible's accessor must be destructed before
|
|
|
|
// the scheduled Runnable gets to run.
|
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
|
|
|
MOZ_ASSERT(data->mChildWorkers.IsEmpty());
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(mSyncLoopStack.IsEmpty());
|
2015-09-29 00:34:28 +03:00
|
|
|
MOZ_ASSERT(!mPendingEventQueueClearing);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2014-03-05 03:09:23 +04:00
|
|
|
ClearMainEventQueue(aRanOrNot);
|
2014-07-29 17:25:29 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (WorkerRan == aRanOrNot) {
|
|
|
|
nsIThread* currentThread = NS_GetCurrentThread();
|
|
|
|
MOZ_ASSERT(currentThread);
|
|
|
|
MOZ_ASSERT(!NS_HasPendingEvents(currentThread));
|
|
|
|
}
|
|
|
|
#endif
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
if (WorkerPrivate* parent = GetParent()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerFinishedRunnable> runnable =
|
2013-10-23 17:16:49 +04:00
|
|
|
new WorkerFinishedRunnable(parent, this);
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!runnable->Dispatch()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Failed to dispatch runnable!");
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TopLevelWorkerFinishedRunnable> runnable =
|
2013-10-23 17:16:49 +04:00
|
|
|
new TopLevelWorkerFinishedRunnable(this);
|
2016-09-14 06:14:02 +03:00
|
|
|
if (NS_FAILED(DispatchToMainThread(runnable.forget()))) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Failed to dispatch runnable!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
bool WorkerPrivate::CollectRuntimeStats(JS::RuntimeStats* aRtStats,
|
|
|
|
bool aAnonymize) {
|
|
|
|
AssertIsOnWorkerThread();
|
2013-04-12 07:52:32 +04:00
|
|
|
NS_ASSERTION(aRtStats, "Null RuntimeStats!");
|
2013-02-08 15:50:00 +04:00
|
|
|
NS_ASSERTION(mJSContext, "This must never be null!");
|
2012-12-30 22:21:52 +04:00
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
return JS::CollectRuntimeStats(mJSContext, aRtStats, nullptr, aAnonymize);
|
2011-09-09 04:03:03 +04:00
|
|
|
}
|
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
void WorkerPrivate::EnableMemoryReporter() {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
|
|
|
MOZ_ASSERT(!data->mMemoryReporter);
|
2011-09-09 04:03:03 +04:00
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
// No need to lock here since the main thread can't race until we've
|
|
|
|
// successfully registered the reporter.
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mMemoryReporter = new MemoryReporter(this);
|
2011-09-09 04:03:03 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if (NS_FAILED(RegisterWeakAsyncMemoryReporter(data->mMemoryReporter))) {
|
2012-12-30 22:21:52 +04:00
|
|
|
NS_WARNING("Failed to register memory reporter!");
|
|
|
|
// No need to lock here since a failed registration means our memory
|
|
|
|
// reporter can't start running. Just clean up.
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mMemoryReporter = nullptr;
|
2012-12-30 22:21:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorkerPrivate::DisableMemoryReporter() {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2012-12-30 22:21:52 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MemoryReporter> memoryReporter;
|
2011-09-09 04:03:03 +04:00
|
|
|
{
|
2016-10-28 12:50:16 +03:00
|
|
|
// Mutex protectes MemoryReporter::mWorkerPrivate which is cleared by
|
|
|
|
// MemoryReporter::Disable() below.
|
2011-09-09 04:03:03 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
// There is nothing to do here if the memory reporter was never successfully
|
|
|
|
// registered.
|
2018-11-13 23:22:40 +03:00
|
|
|
if (!data->mMemoryReporter) {
|
2013-02-08 15:50:00 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-05-30 06:39:38 +04:00
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
// We don't need this set any longer. Swap it out so that we can unregister
|
|
|
|
// below.
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mMemoryReporter.swap(memoryReporter);
|
2011-09-09 04:03:03 +04:00
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
// Next disable the memory reporter so that the main thread stops trying to
|
|
|
|
// signal us.
|
|
|
|
memoryReporter->Disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally unregister the memory reporter.
|
2013-11-07 09:35:30 +04:00
|
|
|
if (NS_FAILED(UnregisterWeakMemoryReporter(memoryReporter))) {
|
2013-02-08 15:50:00 +04:00
|
|
|
NS_WARNING("Failed to unregister memory reporter!");
|
2012-12-30 22:21:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 23:17:26 +03:00
|
|
|
void WorkerPrivate::WaitForWorkerEvents() {
|
2018-05-21 09:37:45 +03:00
|
|
|
AUTO_PROFILER_LABEL("WorkerPrivate::WaitForWorkerEvents", IDLE);
|
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
mMutex.AssertCurrentThreadOwns();
|
|
|
|
|
2018-11-02 19:26:50 +03:00
|
|
|
AUTO_PROFILER_THREAD_SLEEP;
|
|
|
|
|
2016-10-28 12:50:16 +03:00
|
|
|
// Wait for a worker event.
|
2018-02-09 23:17:26 +03:00
|
|
|
mCondVar.Wait();
|
2011-09-09 04:03:03 +04:00
|
|
|
}
|
|
|
|
|
2016-05-11 21:45:58 +03:00
|
|
|
WorkerPrivate::ProcessAllControlRunnablesResult
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerPrivate::ProcessAllControlRunnablesLocked() {
|
2011-09-09 04:03:03 +04:00
|
|
|
AssertIsOnWorkerThread();
|
2013-10-23 17:16:49 +04:00
|
|
|
mMutex.AssertCurrentThreadOwns();
|
2011-09-09 04:03:03 +04:00
|
|
|
|
2016-05-11 21:45:58 +03:00
|
|
|
auto result = ProcessAllControlRunnablesResult::Nothing;
|
2011-09-09 04:03:03 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
for (;;) {
|
|
|
|
WorkerControlRunnable* event;
|
|
|
|
if (!mControlQueue.Pop(event)) {
|
|
|
|
break;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MutexAutoUnlock unlock(mMutex);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(event);
|
|
|
|
if (NS_FAILED(static_cast<nsIRunnable*>(event)->Run())) {
|
2016-05-11 21:45:58 +03:00
|
|
|
result = ProcessAllControlRunnablesResult::Abort;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-05-11 21:45:58 +03:00
|
|
|
if (result == ProcessAllControlRunnablesResult::Nothing) {
|
|
|
|
// We ran at least one thing.
|
|
|
|
result = ProcessAllControlRunnablesResult::MayContinue;
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
event->Release();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
return result;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2014-03-05 03:09:23 +04:00
|
|
|
void WorkerPrivate::ClearMainEventQueue(WorkerRanOrNot aRanOrNot) {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2016-07-04 09:18:53 +03:00
|
|
|
MOZ_ASSERT(mSyncLoopStack.IsEmpty());
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(!mCancelAllPendingRunnables);
|
|
|
|
mCancelAllPendingRunnables = true;
|
|
|
|
|
2014-03-05 03:09:23 +04:00
|
|
|
if (WorkerNeverRan == aRanOrNot) {
|
|
|
|
for (uint32_t count = mPreStartRunnables.Length(), index = 0; index < count;
|
|
|
|
index++) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerRunnable> runnable = mPreStartRunnables[index].forget();
|
2014-03-05 03:09:23 +04:00
|
|
|
static_cast<nsIRunnable*>(runnable.get())->Run();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nsIThread* currentThread = NS_GetCurrentThread();
|
|
|
|
MOZ_ASSERT(currentThread);
|
|
|
|
|
|
|
|
NS_ProcessPendingEvents(currentThread);
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(mCancelAllPendingRunnables);
|
|
|
|
mCancelAllPendingRunnables = false;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-02-26 19:32:28 +03:00
|
|
|
void WorkerPrivate::ClearDebuggerEventQueue() {
|
|
|
|
while (!mDebuggerQueue.IsEmpty()) {
|
2017-07-21 18:16:23 +03:00
|
|
|
WorkerRunnable* runnable = nullptr;
|
2016-02-26 19:32:28 +03:00
|
|
|
mDebuggerQueue.Pop(runnable);
|
|
|
|
// It should be ok to simply release the runnable, without running it.
|
|
|
|
runnable->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:52:43 +03:00
|
|
|
bool WorkerPrivate::FreezeInternal() {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
|
|
|
NS_ASSERTION(!data->mFrozen, "Already frozen!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if (data->mClientSource) {
|
|
|
|
data->mClientSource->Freeze();
|
2017-11-21 23:13:05 +03:00
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mFrozen = true;
|
2016-10-04 13:13:33 +03:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
for (uint32_t index = 0; index < data->mChildWorkers.Length(); index++) {
|
|
|
|
data->mChildWorkers[index]->Freeze(nullptr);
|
2016-10-04 13:13:33 +03:00
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:52:43 +03:00
|
|
|
bool WorkerPrivate::ThawInternal() {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
NS_ASSERTION(data->mFrozen, "Not yet frozen!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
for (uint32_t index = 0; index < data->mChildWorkers.Length(); index++) {
|
|
|
|
data->mChildWorkers[index]->Thaw(nullptr);
|
2016-10-04 13:13:33 +03:00
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mFrozen = false;
|
2017-11-21 23:13:05 +03:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if (data->mClientSource) {
|
|
|
|
data->mClientSource->Thaw();
|
2017-11-21 23:13:05 +03:00
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-10 11:09:59 +03:00
|
|
|
void WorkerPrivate::PropagateFirstPartyStorageAccessGrantedInternal() {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2018-07-10 11:09:59 +03:00
|
|
|
|
|
|
|
mLoadInfo.mFirstPartyStorageAccessGranted = true;
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
for (uint32_t index = 0; index < data->mChildWorkers.Length(); index++) {
|
|
|
|
data->mChildWorkers[index]->PropagateFirstPartyStorageAccessGranted();
|
2018-07-10 11:09:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 09:10:30 +03:00
|
|
|
void WorkerPrivate::TraverseTimeouts(nsCycleCollectionTraversalCallback& cb) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
|
|
|
for (uint32_t i = 0; i < data->mTimeouts.Length(); ++i) {
|
|
|
|
TimeoutInfo* tmp = data->mTimeouts[i];
|
2016-08-16 09:10:30 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mHandler)
|
2016-08-16 18:11:22 +03:00
|
|
|
}
|
2016-08-16 09:10:30 +03:00
|
|
|
}
|
|
|
|
|
2016-08-16 09:10:30 +03:00
|
|
|
void WorkerPrivate::UnlinkTimeouts() {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
|
|
|
data->mTimeouts.Clear();
|
2016-08-16 09:10:30 +03:00
|
|
|
}
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
bool WorkerPrivate::ModifyBusyCountFromWorker(bool aIncrease) {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
// If we're in shutdown then the busy count is no longer being considered so
|
|
|
|
// just return now.
|
|
|
|
if (mStatus >= Killing) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ModifyBusyCountRunnable> runnable =
|
2011-07-17 23:09:13 +04:00
|
|
|
new ModifyBusyCountRunnable(this, aIncrease);
|
2016-02-26 23:23:11 +03:00
|
|
|
return runnable->Dispatch();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
bool WorkerPrivate::AddChildWorker(WorkerPrivate* aChildWorker) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-03-13 00:33:40 +04:00
|
|
|
#ifdef DEBUG
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2018-01-31 10:23:44 +03:00
|
|
|
WorkerStatus currentStatus;
|
2013-03-13 00:33:40 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
currentStatus = mStatus;
|
2013-03-13 00:33:40 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-03-13 00:33:40 +04:00
|
|
|
MOZ_ASSERT(currentStatus == Running);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2013-03-13 00:33:40 +04:00
|
|
|
#endif
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
NS_ASSERTION(!data->mChildWorkers.Contains(aChildWorker),
|
2011-07-17 23:09:13 +04:00
|
|
|
"Already know about this one!");
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mChildWorkers.AppendElement(aChildWorker);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
return data->mChildWorkers.Length() == 1 ? ModifyBusyCountFromWorker(true)
|
2011-07-17 23:09:13 +04:00
|
|
|
: true;
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
void WorkerPrivate::RemoveChildWorker(WorkerPrivate* aChildWorker) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
NS_ASSERTION(data->mChildWorkers.Contains(aChildWorker),
|
2011-07-17 23:09:13 +04:00
|
|
|
"Didn't know about this one!");
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mChildWorkers.RemoveElement(aChildWorker);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if (data->mChildWorkers.IsEmpty() && !ModifyBusyCountFromWorker(false)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Failed to modify busy count!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 10:23:44 +03:00
|
|
|
bool WorkerPrivate::AddHolder(WorkerHolder* aHolder, WorkerStatus aFailStatus) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2016-08-18 17:11:04 +03:00
|
|
|
if (mStatus >= aFailStatus) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ASSERT(!data->mHolders.Contains(aHolder), "Already know about this one!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2017-05-10 19:27:10 +03:00
|
|
|
if (aHolder->GetBehavior() == WorkerHolder::PreventIdleShutdownStart) {
|
2018-11-13 23:22:40 +03:00
|
|
|
if (!data->mNumHoldersPreventingShutdownStart &&
|
|
|
|
!ModifyBusyCountFromWorker(true)) {
|
2017-05-10 19:27:10 +03:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mNumHoldersPreventingShutdownStart += 1;
|
2015-06-22 22:13:38 +03:00
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mHolders.AppendElement(aHolder);
|
2015-06-22 22:13:38 +03:00
|
|
|
return true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
void WorkerPrivate::RemoveHolder(WorkerHolder* aHolder) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ASSERT(data->mHolders.Contains(aHolder), "Didn't know about this one!");
|
|
|
|
data->mHolders.RemoveElement(aHolder);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2017-05-10 19:27:10 +03:00
|
|
|
if (aHolder->GetBehavior() == WorkerHolder::PreventIdleShutdownStart) {
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mNumHoldersPreventingShutdownStart -= 1;
|
|
|
|
if (!data->mNumHoldersPreventingShutdownStart &&
|
|
|
|
!ModifyBusyCountFromWorker(false)) {
|
2017-05-10 19:27:10 +03:00
|
|
|
NS_WARNING("Failed to modify busy count!");
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 17:25:50 +03:00
|
|
|
void WorkerPrivate::NotifyHolders(WorkerStatus aStatus) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-05-03 18:03:13 +03:00
|
|
|
NS_ASSERTION(aStatus > Closing, "Bad status!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
nsTObserverArray<WorkerHolder*>::ForwardIterator iter(data->mHolders);
|
2015-04-07 20:31:51 +03:00
|
|
|
while (iter.HasMore()) {
|
2016-08-21 09:41:34 +03:00
|
|
|
WorkerHolder* holder = iter.GetNext();
|
|
|
|
if (!holder->Notify(aStatus)) {
|
|
|
|
NS_WARNING("Failed to notify holder!");
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:33 +03:00
|
|
|
AutoTArray<WorkerPrivate*, 10> children;
|
2018-11-13 23:22:40 +03:00
|
|
|
children.AppendElements(data->mChildWorkers);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < children.Length(); index++) {
|
2016-02-26 00:05:39 +03:00
|
|
|
if (!children[index]->Notify(aStatus)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Failed to notify child worker!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 05:15:56 +03:00
|
|
|
void WorkerPrivate::CancelAllTimeouts() {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2012-03-27 08:05:09 +04:00
|
|
|
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
LOG(TimeoutsLog(), ("Worker %p CancelAllTimeouts.\n", this));
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if (data->mTimerRunning) {
|
|
|
|
NS_ASSERTION(data->mTimer && data->mTimerRunnable, "Huh?!");
|
|
|
|
NS_ASSERTION(!data->mTimeouts.IsEmpty(), "Huh?!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if (NS_FAILED(data->mTimer->Cancel())) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Failed to cancel timer!");
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
for (uint32_t index = 0; index < data->mTimeouts.Length(); index++) {
|
|
|
|
data->mTimeouts[index]->mCanceled = true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-02-27 05:15:56 +03:00
|
|
|
// If mRunningExpiredTimeouts, then the fact that they are all canceled now
|
|
|
|
// means that the currently executing RunExpiredTimeouts will deal with
|
|
|
|
// them. Otherwise, we need to clean them up ourselves.
|
2018-11-13 23:22:40 +03:00
|
|
|
if (!data->mRunningExpiredTimeouts) {
|
|
|
|
data->mTimeouts.Clear();
|
2016-02-27 05:15:56 +03:00
|
|
|
ModifyBusyCountFromWorker(false);
|
2012-03-27 08:05:09 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-02-27 05:15:56 +03:00
|
|
|
// Set mTimerRunning false even if mRunningExpiredTimeouts is true, so that
|
|
|
|
// if we get reentered under this same RunExpiredTimeouts call we don't
|
|
|
|
// assert above that !mTimeouts().IsEmpty(), because that's clearly false
|
|
|
|
// now.
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mTimerRunning = false;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2012-03-27 08:05:09 +04:00
|
|
|
#ifdef DEBUG
|
2018-11-13 23:22:40 +03:00
|
|
|
else if (!data->mRunningExpiredTimeouts) {
|
|
|
|
NS_ASSERTION(data->mTimeouts.IsEmpty(), "Huh?!");
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2012-03-27 08:05:09 +04:00
|
|
|
#endif
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mTimer = nullptr;
|
|
|
|
data->mTimerRunnable = nullptr;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
already_AddRefed<nsIEventTarget> WorkerPrivate::CreateNewSyncLoop(
|
2018-01-31 10:23:44 +03:00
|
|
|
WorkerStatus aFailStatus) {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnWorkerThread();
|
2018-07-12 20:33:41 +03:00
|
|
|
MOZ_ASSERT(
|
|
|
|
aFailStatus >= Canceling,
|
2018-04-18 11:02:29 +03:00
|
|
|
"Sync loops can be created when the worker is in Running/Closing state!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2017-01-05 12:05:32 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (mStatus >= aFailStatus) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Bug 1382922 - Refactor event queue to allow multiple implementations (r=erahm)
This patch refactors the nsThread event queue to clean it up and to make it easier to restructure. The fundamental concepts are as follows:
Each nsThread will have a pointer to a refcounted SynchronizedEventQueue. A SynchronizedEQ takes care of doing the locking and condition variable work when posting and popping events. For the actual storage of events, it delegates to an AbstractEventQueue data structure. It keeps a UniquePtr to the AbstractEventQueue that it uses for storage.
Both SynchronizedEQ and AbstractEventQueue are abstract classes. There is only one concrete implementation of SynchronizedEQ in this patch, which is called ThreadEventQueue. ThreadEventQueue uses locks and condition variables to post and pop events the same way nsThread does. It also encapsulates the functionality that DOM workers need to implement their special event loops (PushEventQueue and PopEventQueue). In later Quantum DOM work, I plan to have another SynchronizedEQ implementation for the main thread, called SchedulerEventQueue. It will have special code for the cooperatively scheduling threads in Quantum DOM.
There are two concrete implementations of AbstractEventQueue in this patch: EventQueue and PrioritizedEventQueue. EventQueue replaces the old nsEventQueue. The other AbstractEventQueue implementation is PrioritizedEventQueue, which uses multiple queues for different event priorities.
The final major piece here is ThreadEventTarget, which splits some of the code for posting events out of nsThread. Eventually, my plan is for multiple cooperatively scheduled nsThreads to be able to share a ThreadEventTarget. In this patch, though, each nsThread has its own ThreadEventTarget. The class's purpose is just to collect some related code together.
One final note: I tried to avoid virtual dispatch overhead as much as possible. Calls to SynchronizedEQ methods do use virtual dispatch, since I plan to use different implementations for different threads with Quantum DOM. But all the calls to EventQueue methods should be non-virtual. Although the methods are declared virtual, all the classes used are final and the concrete classes involved should all be known through templatization.
MozReview-Commit-ID: 9Evtr9oIJvx
2017-06-21 05:42:13 +03:00
|
|
|
auto queue =
|
|
|
|
static_cast<ThreadEventQueue<EventQueue>*>(mThread->EventQueue());
|
|
|
|
nsCOMPtr<nsISerialEventTarget> realEventTarget = queue->PushEventQueue();
|
|
|
|
MOZ_ASSERT(realEventTarget);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<EventTarget> workerEventTarget =
|
2013-10-23 17:16:49 +04:00
|
|
|
new EventTarget(this, realEventTarget);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Modifications must be protected by mMutex in DEBUG builds, see comment
|
|
|
|
// about mSyncLoopStack in WorkerPrivate.h.
|
|
|
|
#ifdef DEBUG
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mSyncLoopStack.AppendElement(new SyncLoopInfo(workerEventTarget));
|
|
|
|
}
|
|
|
|
|
|
|
|
return workerEventTarget.forget();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
bool WorkerPrivate::RunCurrentSyncLoop() {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
JSContext* cx = GetJSContext();
|
|
|
|
MOZ_ASSERT(cx);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// This should not change between now and the time we finish running this sync
|
|
|
|
// loop.
|
|
|
|
uint32_t currentLoopIndex = mSyncLoopStack.Length() - 1;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
SyncLoopInfo* loopInfo = mSyncLoopStack[currentLoopIndex];
|
|
|
|
|
|
|
|
MOZ_ASSERT(loopInfo);
|
|
|
|
MOZ_ASSERT(!loopInfo->mHasRun);
|
|
|
|
MOZ_ASSERT(!loopInfo->mCompleted);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
loopInfo->mHasRun = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (!loopInfo->mCompleted) {
|
|
|
|
bool normalRunnablesPending = false;
|
|
|
|
|
|
|
|
// Don't block with the periodic GC timer running.
|
2014-11-17 22:55:37 +03:00
|
|
|
if (!NS_HasPendingEvents(mThread)) {
|
2013-10-23 17:16:49 +04:00
|
|
|
SetGCTimerMode(IdleTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for something to do.
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
for (;;) {
|
|
|
|
while (mControlQueue.IsEmpty() && !normalRunnablesPending &&
|
2014-11-17 22:55:37 +03:00
|
|
|
!(normalRunnablesPending = NS_HasPendingEvents(mThread))) {
|
2013-10-23 17:16:49 +04:00
|
|
|
WaitForWorkerEvents();
|
|
|
|
}
|
|
|
|
|
2016-05-11 21:45:58 +03:00
|
|
|
auto result = ProcessAllControlRunnablesLocked();
|
|
|
|
if (result != ProcessAllControlRunnablesResult::Nothing) {
|
2018-12-18 14:48:11 +03:00
|
|
|
// The state of the world may have changed. Recheck it if we need to
|
|
|
|
// continue.
|
|
|
|
normalRunnablesPending =
|
|
|
|
result == ProcessAllControlRunnablesResult::MayContinue &&
|
|
|
|
NS_HasPendingEvents(mThread);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2016-05-11 21:45:58 +03:00
|
|
|
// NB: If we processed a NotifyRunnable, we might have run
|
|
|
|
// non-control runnables, one of which may have shut down the
|
|
|
|
// sync loop.
|
|
|
|
if (loopInfo->mCompleted) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we *didn't* run any control runnables, this should be unchanged.
|
|
|
|
MOZ_ASSERT(!loopInfo->mCompleted);
|
|
|
|
|
|
|
|
if (normalRunnablesPending) {
|
2013-10-23 17:16:49 +04:00
|
|
|
break;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 03:22:53 +04:00
|
|
|
if (normalRunnablesPending) {
|
|
|
|
// Make sure the periodic timer is running before we continue.
|
|
|
|
SetGCTimerMode(PeriodicTimer);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
MOZ_ALWAYS_TRUE(NS_ProcessNextEvent(mThread, false));
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2014-01-14 03:22:53 +04:00
|
|
|
// Now *might* be a good time to GC. Let the JS engine make the decision.
|
2015-06-11 00:12:55 +03:00
|
|
|
if (JS::CurrentGlobalOrNull(cx)) {
|
|
|
|
JS_MaybeGC(cx);
|
|
|
|
}
|
2014-01-14 03:22:53 +04:00
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the stack didn't change underneath us.
|
|
|
|
MOZ_ASSERT(mSyncLoopStack[currentLoopIndex] == loopInfo);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2014-01-11 04:37:47 +04:00
|
|
|
return DestroySyncLoop(currentLoopIndex);
|
|
|
|
}
|
|
|
|
|
2018-03-16 17:20:29 +03:00
|
|
|
bool WorkerPrivate::DestroySyncLoop(uint32_t aLoopIndex) {
|
2014-01-11 04:37:47 +04:00
|
|
|
MOZ_ASSERT(!mSyncLoopStack.IsEmpty());
|
|
|
|
MOZ_ASSERT(mSyncLoopStack.Length() - 1 == aLoopIndex);
|
|
|
|
|
|
|
|
// We're about to delete the loop, stash its event target and result.
|
|
|
|
SyncLoopInfo* loopInfo = mSyncLoopStack[aLoopIndex];
|
2013-10-23 17:16:49 +04:00
|
|
|
nsIEventTarget* nestedEventTarget =
|
|
|
|
loopInfo->mEventTarget->GetWeakNestedEventTarget();
|
|
|
|
MOZ_ASSERT(nestedEventTarget);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
bool result = loopInfo->mResult;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
// Modifications must be protected by mMutex in DEBUG builds, see comment
|
|
|
|
// about mSyncLoopStack in WorkerPrivate.h.
|
2011-07-17 23:09:13 +04:00
|
|
|
#ifdef DEBUG
|
2013-10-23 17:16:49 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
2011-07-17 23:09:13 +04:00
|
|
|
#endif
|
|
|
|
|
2014-01-11 04:37:47 +04:00
|
|
|
// This will delete |loopInfo|!
|
|
|
|
mSyncLoopStack.RemoveElementAt(aLoopIndex);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
Bug 1382922 - Refactor event queue to allow multiple implementations (r=erahm)
This patch refactors the nsThread event queue to clean it up and to make it easier to restructure. The fundamental concepts are as follows:
Each nsThread will have a pointer to a refcounted SynchronizedEventQueue. A SynchronizedEQ takes care of doing the locking and condition variable work when posting and popping events. For the actual storage of events, it delegates to an AbstractEventQueue data structure. It keeps a UniquePtr to the AbstractEventQueue that it uses for storage.
Both SynchronizedEQ and AbstractEventQueue are abstract classes. There is only one concrete implementation of SynchronizedEQ in this patch, which is called ThreadEventQueue. ThreadEventQueue uses locks and condition variables to post and pop events the same way nsThread does. It also encapsulates the functionality that DOM workers need to implement their special event loops (PushEventQueue and PopEventQueue). In later Quantum DOM work, I plan to have another SynchronizedEQ implementation for the main thread, called SchedulerEventQueue. It will have special code for the cooperatively scheduling threads in Quantum DOM.
There are two concrete implementations of AbstractEventQueue in this patch: EventQueue and PrioritizedEventQueue. EventQueue replaces the old nsEventQueue. The other AbstractEventQueue implementation is PrioritizedEventQueue, which uses multiple queues for different event priorities.
The final major piece here is ThreadEventTarget, which splits some of the code for posting events out of nsThread. Eventually, my plan is for multiple cooperatively scheduled nsThreads to be able to share a ThreadEventTarget. In this patch, though, each nsThread has its own ThreadEventTarget. The class's purpose is just to collect some related code together.
One final note: I tried to avoid virtual dispatch overhead as much as possible. Calls to SynchronizedEQ methods do use virtual dispatch, since I plan to use different implementations for different threads with Quantum DOM. But all the calls to EventQueue methods should be non-virtual. Although the methods are declared virtual, all the classes used are final and the concrete classes involved should all be known through templatization.
MozReview-Commit-ID: 9Evtr9oIJvx
2017-06-21 05:42:13 +03:00
|
|
|
auto queue =
|
|
|
|
static_cast<ThreadEventQueue<EventQueue>*>(mThread->EventQueue());
|
|
|
|
queue->PopEventQueue(nestedEventTarget);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2016-07-04 09:18:53 +03:00
|
|
|
if (mSyncLoopStack.IsEmpty() && mPendingEventQueueClearing) {
|
2015-09-29 00:34:28 +03:00
|
|
|
mPendingEventQueueClearing = false;
|
2016-07-06 09:36:54 +03:00
|
|
|
ClearMainEventQueue(WorkerRan);
|
2015-09-29 00:34:28 +03:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
return result;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
void WorkerPrivate::StopSyncLoop(nsIEventTarget* aSyncLoopTarget,
|
|
|
|
bool aResult) {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnWorkerThread();
|
2013-10-23 17:16:49 +04:00
|
|
|
AssertValidSyncLoop(aSyncLoopTarget);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(!mSyncLoopStack.IsEmpty());
|
|
|
|
|
|
|
|
for (uint32_t index = mSyncLoopStack.Length(); index > 0; index--) {
|
|
|
|
nsAutoPtr<SyncLoopInfo>& loopInfo = mSyncLoopStack[index - 1];
|
|
|
|
MOZ_ASSERT(loopInfo);
|
|
|
|
MOZ_ASSERT(loopInfo->mEventTarget);
|
|
|
|
|
|
|
|
if (loopInfo->mEventTarget == aSyncLoopTarget) {
|
|
|
|
// Can't assert |loop->mHasRun| here because dispatch failures can cause
|
|
|
|
// us to bail out early.
|
|
|
|
MOZ_ASSERT(!loopInfo->mCompleted);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
loopInfo->mResult = aResult;
|
|
|
|
loopInfo->mCompleted = true;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
loopInfo->mEventTarget->Disable();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!SameCOMIdentity(loopInfo->mEventTarget, aSyncLoopTarget));
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CRASH("Unknown sync loop!");
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
void WorkerPrivate::AssertValidSyncLoop(nsIEventTarget* aSyncLoopTarget) {
|
|
|
|
MOZ_ASSERT(aSyncLoopTarget);
|
|
|
|
|
|
|
|
EventTarget* workerTarget;
|
|
|
|
nsresult rv = aSyncLoopTarget->QueryInterface(
|
|
|
|
kDEBUGWorkerEventTargetIID, reinterpret_cast<void**>(&workerTarget));
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
MOZ_ASSERT(workerTarget);
|
|
|
|
|
|
|
|
bool valid = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < mSyncLoopStack.Length(); index++) {
|
|
|
|
nsAutoPtr<SyncLoopInfo>& loopInfo = mSyncLoopStack[index];
|
|
|
|
MOZ_ASSERT(loopInfo);
|
|
|
|
MOZ_ASSERT(loopInfo->mEventTarget);
|
|
|
|
|
|
|
|
if (loopInfo->mEventTarget == aSyncLoopTarget) {
|
|
|
|
valid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!SameCOMIdentity(loopInfo->mEventTarget, aSyncLoopTarget));
|
|
|
|
}
|
|
|
|
}
|
2012-12-22 00:14:47 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(valid);
|
2012-12-22 00:14:47 +04:00
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
#endif
|
2012-12-22 00:14:47 +04:00
|
|
|
|
2018-04-19 01:02:08 +03:00
|
|
|
void WorkerPrivate::PostMessageToParent(
|
2013-11-05 18:16:26 +04:00
|
|
|
JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
2017-02-03 13:00:38 +03:00
|
|
|
const Sequence<JSObject*>& aTransferable, ErrorResult& aRv) {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
JS::Rooted<JS::Value> transferable(aCx, JS::UndefinedValue());
|
2017-02-03 13:00:38 +03:00
|
|
|
|
|
|
|
aRv = nsContentUtils::CreateJSValueFromSequenceOfObject(aCx, aTransferable,
|
|
|
|
&transferable);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MessageEventRunnable> runnable = new MessageEventRunnable(
|
2015-09-16 06:27:56 +03:00
|
|
|
this, WorkerRunnable::ParentThreadUnchangedBusyCount);
|
2011-08-16 07:40:38 +04:00
|
|
|
|
2015-10-22 00:10:05 +03:00
|
|
|
UniquePtr<AbstractTimelineMarker> start;
|
|
|
|
UniquePtr<AbstractTimelineMarker> end;
|
|
|
|
RefPtr<TimelineConsumers> timelines = TimelineConsumers::Get();
|
|
|
|
bool isTimelineRecording = timelines && !timelines->IsEmpty();
|
|
|
|
|
|
|
|
if (isTimelineRecording) {
|
|
|
|
start = MakeUnique<WorkerTimelineMarker>(
|
|
|
|
NS_IsMainThread()
|
|
|
|
? ProfileTimelineWorkerOperationType::SerializeDataOnMainThread
|
|
|
|
: ProfileTimelineWorkerOperationType::SerializeDataOffMainThread,
|
|
|
|
MarkerTracingType::START);
|
|
|
|
}
|
|
|
|
|
2016-10-24 16:14:45 +03:00
|
|
|
runnable->Write(aCx, aMessage, transferable, JS::CloneDataPolicy(), aRv);
|
2015-10-22 00:10:05 +03:00
|
|
|
|
|
|
|
if (isTimelineRecording) {
|
|
|
|
end = MakeUnique<WorkerTimelineMarker>(
|
|
|
|
NS_IsMainThread()
|
|
|
|
? ProfileTimelineWorkerOperationType::SerializeDataOnMainThread
|
|
|
|
: ProfileTimelineWorkerOperationType::SerializeDataOffMainThread,
|
|
|
|
MarkerTracingType::END);
|
|
|
|
timelines->AddMarkerForAllObservedDocShells(start);
|
|
|
|
timelines->AddMarkerForAllObservedDocShells(end);
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2013-11-05 18:16:26 +04:00
|
|
|
return;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!runnable->Dispatch()) {
|
2013-11-05 18:16:26 +04:00
|
|
|
aRv = NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2015-03-27 09:17:16 +03:00
|
|
|
void WorkerPrivate::EnterDebuggerEventLoop() {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2015-03-27 09:17:16 +03:00
|
|
|
|
|
|
|
JSContext* cx = GetJSContext();
|
|
|
|
MOZ_ASSERT(cx);
|
2018-04-16 10:43:56 +03:00
|
|
|
CycleCollectedJSContext* ccjscx = CycleCollectedJSContext::Get();
|
2015-03-27 09:17:16 +03:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
uint32_t currentEventLoopLevel = ++data->mDebuggerEventLoopLevel;
|
2015-03-27 09:17:16 +03:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
while (currentEventLoopLevel <= data->mDebuggerEventLoopLevel) {
|
2015-03-27 09:17:16 +03:00
|
|
|
bool debuggerRunnablesPending = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
debuggerRunnablesPending = !mDebuggerQueue.IsEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't block with the periodic GC timer running.
|
|
|
|
if (!debuggerRunnablesPending) {
|
|
|
|
SetGCTimerMode(IdleTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for something to do
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
std::queue<RefPtr<MicroTaskRunnable>>& debuggerMtQueue =
|
2018-04-16 10:43:56 +03:00
|
|
|
ccjscx->GetDebuggerMicroTaskQueue();
|
2015-03-27 09:17:16 +03:00
|
|
|
while (mControlQueue.IsEmpty() &&
|
2017-08-10 14:27:07 +03:00
|
|
|
!(debuggerRunnablesPending = !mDebuggerQueue.IsEmpty()) &&
|
2017-11-17 06:01:27 +03:00
|
|
|
debuggerMtQueue.empty()) {
|
2015-03-27 09:17:16 +03:00
|
|
|
WaitForWorkerEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessAllControlRunnablesLocked();
|
2016-05-11 21:45:58 +03:00
|
|
|
|
|
|
|
// XXXkhuey should we abort JS on the stack here if we got Abort above?
|
2015-03-27 09:17:16 +03:00
|
|
|
}
|
2018-04-16 10:43:56 +03:00
|
|
|
ccjscx->PerformDebuggerMicroTaskCheckpoint();
|
2015-03-27 09:17:16 +03:00
|
|
|
if (debuggerRunnablesPending) {
|
|
|
|
// Start the periodic GC timer if it is not already running.
|
|
|
|
SetGCTimerMode(PeriodicTimer);
|
|
|
|
|
2017-07-21 18:16:23 +03:00
|
|
|
WorkerRunnable* runnable = nullptr;
|
2015-03-27 09:17:16 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
mDebuggerQueue.Pop(runnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(runnable);
|
|
|
|
static_cast<nsIRunnable*>(runnable)->Run();
|
|
|
|
runnable->Release();
|
|
|
|
|
2018-04-16 10:43:56 +03:00
|
|
|
ccjscx->PerformDebuggerMicroTaskCheckpoint();
|
2016-03-24 18:12:00 +03:00
|
|
|
|
2015-03-27 09:17:16 +03:00
|
|
|
// Now *might* be a good time to GC. Let the JS engine make the decision.
|
2015-06-11 00:12:55 +03:00
|
|
|
if (JS::CurrentGlobalOrNull(cx)) {
|
|
|
|
JS_MaybeGC(cx);
|
|
|
|
}
|
2015-03-27 09:17:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorkerPrivate::LeaveDebuggerEventLoop() {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2015-03-27 09:17:16 +03:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
// TODO: Why lock the mutex if we're accessing data accessible to one thread
|
|
|
|
// only?
|
2015-03-27 09:17:16 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if (data->mDebuggerEventLoopLevel > 0) {
|
|
|
|
--data->mDebuggerEventLoopLevel;
|
2015-03-27 09:17:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 14:15:59 +03:00
|
|
|
void WorkerPrivate::PostMessageToDebugger(const nsAString& aMessage) {
|
|
|
|
mDebugger->PostMessageToDebugger(aMessage);
|
|
|
|
}
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
void WorkerPrivate::SetDebuggerImmediate(dom::Function& aHandler,
|
|
|
|
ErrorResult& aRv) {
|
2015-03-30 14:54:38 +03:00
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DebuggerImmediateRunnable> runnable =
|
2015-03-30 14:54:38 +03:00
|
|
|
new DebuggerImmediateRunnable(this, aHandler);
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!runnable->Dispatch()) {
|
2015-03-30 14:54:38 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 22:09:45 +03:00
|
|
|
void WorkerPrivate::ReportErrorToDebugger(const nsAString& aFilename,
|
|
|
|
uint32_t aLineno,
|
|
|
|
const nsAString& aMessage) {
|
|
|
|
mDebugger->ReportErrorToDebugger(aFilename, aLineno, aMessage);
|
|
|
|
}
|
|
|
|
|
2018-03-13 17:25:50 +03:00
|
|
|
bool WorkerPrivate::NotifyInternal(WorkerStatus aStatus) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(aStatus > Running && aStatus < Dead, "Bad status!");
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<EventTarget> eventTarget;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
// Save the old status and set the new status.
|
2018-01-31 10:23:44 +03:00
|
|
|
WorkerStatus previousStatus;
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (mStatus >= aStatus) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-12 20:33:41 +03:00
|
|
|
if (aStatus >= Canceling) {
|
2018-05-02 16:29:26 +03:00
|
|
|
MutexAutoUnlock unlock(mMutex);
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mClientSource.reset();
|
|
|
|
if (data->mScope) {
|
|
|
|
data->mScope->NoteTerminating();
|
2018-04-26 19:18:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 19:24:22 +03:00
|
|
|
// Make sure the hybrid event target stops dispatching runnables
|
|
|
|
// once we reaching the killing state.
|
|
|
|
if (aStatus == Killing) {
|
|
|
|
// To avoid deadlock we always acquire the event target mutex before the
|
|
|
|
// worker private mutex. (We do it in this order because this is what
|
|
|
|
// workers best for event dispatching.) To enforce that order here we
|
|
|
|
// need to unlock the worker private mutex before we lock the event target
|
|
|
|
// mutex in ForgetWorkerPrivate.
|
|
|
|
{
|
|
|
|
MutexAutoUnlock unlock(mMutex);
|
|
|
|
mWorkerHybridEventTarget->ForgetWorkerPrivate(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the status code again in case another NotifyInternal came in
|
|
|
|
// while we were unlocked above.
|
|
|
|
if (mStatus >= aStatus) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
previousStatus = mStatus;
|
|
|
|
mStatus = aStatus;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2017-05-03 17:42:43 +03:00
|
|
|
// Mark parent status as closing immediately to avoid new events being
|
|
|
|
// dispatched after we clear the queue below.
|
|
|
|
if (aStatus == Closing) {
|
|
|
|
Close();
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(previousStatus != Pending);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-05-03 18:03:13 +03:00
|
|
|
if (aStatus >= Closing) {
|
|
|
|
CancelAllTimeouts();
|
|
|
|
}
|
|
|
|
|
2016-08-21 09:41:34 +03:00
|
|
|
// Let all our holders know the new status.
|
2018-05-03 18:03:13 +03:00
|
|
|
if (aStatus > Closing) {
|
|
|
|
NotifyHolders(aStatus);
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-10-03 06:03:28 +04:00
|
|
|
// If this is the first time our status has changed then we need to clear the
|
|
|
|
// main event queue.
|
|
|
|
if (previousStatus == Running) {
|
2015-09-29 00:34:28 +03:00
|
|
|
// NB: If we're in a sync loop, we can't clear the queue immediately,
|
|
|
|
// because this is the wrong queue. So we have to defer it until later.
|
2016-07-04 09:18:53 +03:00
|
|
|
if (!mSyncLoopStack.IsEmpty()) {
|
2015-09-29 00:34:28 +03:00
|
|
|
mPendingEventQueueClearing = true;
|
|
|
|
} else {
|
|
|
|
ClearMainEventQueue(WorkerRan);
|
|
|
|
}
|
2012-10-03 06:03:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the worker script never ran, or failed to compile, we don't need to do
|
2016-09-01 07:33:05 +03:00
|
|
|
// anything else.
|
2016-04-06 06:12:56 +03:00
|
|
|
if (!GlobalScope()) {
|
2012-10-03 06:03:28 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-17 21:51:03 +03:00
|
|
|
// Don't abort the script now, but we dispatch a runnable to do it when the
|
|
|
|
// current JS frame is executed.
|
2011-07-17 23:09:13 +04:00
|
|
|
if (aStatus == Closing) {
|
2018-04-17 21:51:03 +03:00
|
|
|
if (mSyncLoopStack.IsEmpty()) {
|
|
|
|
// Here we use a normal runnable to know when the current JS chunk of code
|
|
|
|
// is finished. We cannot use a WorkerRunnable because they are not
|
|
|
|
// accepted any more by the worker, and we do not want to use a
|
|
|
|
// WorkerControlRunnable because they are immediately executed.
|
|
|
|
RefPtr<CancelingRunnable> r = new CancelingRunnable();
|
|
|
|
mThread->nsThread::Dispatch(r.forget(), NS_DISPATCH_NORMAL);
|
2018-04-17 21:51:04 +03:00
|
|
|
|
|
|
|
// At the same time, we want to be sure that we interrupt infinite loops.
|
|
|
|
// The following runnable starts a timer that cancel the worker, from the
|
|
|
|
// parent thread, after CANCELING_TIMEOUT millseconds.
|
|
|
|
RefPtr<CancelingWithTimeoutOnParentRunnable> rr =
|
|
|
|
new CancelingWithTimeoutOnParentRunnable(this);
|
|
|
|
rr->Dispatch();
|
2018-04-17 21:51:03 +03:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-12 20:33:41 +03:00
|
|
|
MOZ_ASSERT(aStatus == Canceling || aStatus == Killing);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Always abort the script.
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-14 14:39:31 +03:00
|
|
|
void WorkerPrivate::ReportError(JSContext* aCx,
|
|
|
|
JS::ConstUTF8CharsZ aToStringResult,
|
2011-07-17 23:09:13 +04:00
|
|
|
JSErrorReport* aReport) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if (!MayContinueRunning() || data->mErrorHandlerRecursionCount == 2) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
NS_ASSERTION(data->mErrorHandlerRecursionCount == 0 ||
|
|
|
|
data->mErrorHandlerRecursionCount == 1,
|
2011-07-17 23:09:13 +04:00
|
|
|
"Bad recursion logic!");
|
|
|
|
|
2016-08-29 19:30:51 +03:00
|
|
|
JS::Rooted<JS::Value> exn(aCx);
|
|
|
|
if (!JS_GetPendingException(aCx, &exn)) {
|
|
|
|
// Probably shouldn't actually happen? But let's go ahead and just use null
|
|
|
|
// for lack of anything better.
|
|
|
|
exn.setNull();
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
|
2017-02-15 17:53:07 +03:00
|
|
|
WorkerErrorReport report;
|
2011-07-17 23:09:13 +04:00
|
|
|
if (aReport) {
|
2017-02-15 17:53:07 +03:00
|
|
|
report.AssignErrorReport(aReport);
|
2011-07-17 23:09:13 +04:00
|
|
|
} else {
|
2017-02-15 17:53:07 +03:00
|
|
|
report.mFlags = nsIScriptError::errorFlag | nsIScriptError::exceptionFlag;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2017-02-15 17:53:07 +03:00
|
|
|
if (report.mMessage.IsEmpty() && aToStringResult) {
|
2016-08-14 14:39:31 +03:00
|
|
|
nsDependentCString toStringResult(aToStringResult.c_str());
|
2017-02-15 17:53:07 +03:00
|
|
|
if (!AppendUTF8toUTF16(toStringResult, report.mMessage,
|
|
|
|
mozilla::fallible)) {
|
2016-03-10 12:50:56 +03:00
|
|
|
// Try again, with only a 1 KB string. Do this infallibly this time.
|
|
|
|
// If the user doesn't have 1 KB to spare we're done anyways.
|
2016-08-14 14:39:31 +03:00
|
|
|
uint32_t index = std::min(uint32_t(1024), toStringResult.Length());
|
|
|
|
|
|
|
|
// Drop the last code point that may be cropped.
|
|
|
|
index = RewindToPriorUTF8Codepoint(toStringResult.BeginReading(), index);
|
|
|
|
|
|
|
|
nsDependentCString truncatedToStringResult(aToStringResult.c_str(),
|
|
|
|
index);
|
2017-02-15 17:53:07 +03:00
|
|
|
AppendUTF8toUTF16(truncatedToStringResult, report.mMessage);
|
2016-03-10 12:50:56 +03:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mErrorHandlerRecursionCount++;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// Don't want to run the scope's error handler if this is a recursive error or
|
2016-09-01 07:33:05 +03:00
|
|
|
// if we ran out of memory.
|
2018-11-13 23:22:40 +03:00
|
|
|
bool fireAtScope = data->mErrorHandlerRecursionCount == 1 &&
|
2017-02-15 17:53:07 +03:00
|
|
|
report.mErrorNumber != JSMSG_OUT_OF_MEMORY &&
|
2015-06-11 00:12:55 +03:00
|
|
|
JS::CurrentGlobalOrNull(aCx);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-01-31 10:19:51 +03:00
|
|
|
WorkerErrorReport::ReportError(aCx, this, fireAtScope, nullptr, report, 0,
|
|
|
|
exn);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mErrorHandlerRecursionCount--;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-04-12 02:41:00 +03:00
|
|
|
// static
|
|
|
|
void WorkerPrivate::ReportErrorToConsole(const char* aMessage) {
|
2018-01-25 04:59:04 +03:00
|
|
|
nsTArray<nsString> emptyParams;
|
|
|
|
WorkerPrivate::ReportErrorToConsole(aMessage, emptyParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void WorkerPrivate::ReportErrorToConsole(const char* aMessage,
|
|
|
|
const nsTArray<nsString>& aParams) {
|
2016-04-12 02:41:00 +03:00
|
|
|
WorkerPrivate* wp = nullptr;
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
wp = GetCurrentThreadWorkerPrivate();
|
|
|
|
}
|
|
|
|
|
2018-01-25 04:59:04 +03:00
|
|
|
ReportErrorToConsoleRunnable::Report(wp, aMessage, aParams);
|
2016-04-12 02:41:00 +03:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
int32_t WorkerPrivate::SetTimeout(JSContext* aCx,
|
2016-08-16 09:10:30 +03:00
|
|
|
nsIScriptTimeoutHandler* aHandler,
|
|
|
|
int32_t aTimeout, bool aIsInterval,
|
2013-11-05 18:16:26 +04:00
|
|
|
ErrorResult& aRv) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2016-08-16 09:10:30 +03:00
|
|
|
MOZ_ASSERT(aHandler);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
const int32_t timerId = data->mNextTimeoutId++;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-01-31 10:23:44 +03:00
|
|
|
WorkerStatus currentStatus;
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
currentStatus = mStatus;
|
|
|
|
}
|
|
|
|
|
2012-03-27 08:05:09 +04:00
|
|
|
// If the worker is trying to call setTimeout/setInterval and the parent
|
|
|
|
// thread has initiated the close process then just silently fail.
|
|
|
|
if (currentStatus >= Closing) {
|
2018-03-31 10:47:51 +03:00
|
|
|
return timerId;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoPtr<TimeoutInfo> newInfo(new TimeoutInfo());
|
|
|
|
newInfo->mIsInterval = aIsInterval;
|
|
|
|
newInfo->mId = timerId;
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
if (MOZ_UNLIKELY(timerId == INT32_MAX)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Timeout ids overflowed!");
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mNextTimeoutId = 1;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-08-16 09:10:30 +03:00
|
|
|
newInfo->mHandler = aHandler;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// See if any of the optional arguments were passed.
|
2013-11-05 18:16:26 +04:00
|
|
|
aTimeout = std::max(0, aTimeout);
|
|
|
|
newInfo->mInterval = TimeDuration::FromMilliseconds(aTimeout);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
newInfo->mTargetTime = TimeStamp::Now() + newInfo->mInterval;
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
nsAutoPtr<TimeoutInfo>* insertedInfo = data->mTimeouts.InsertElementSorted(
|
2018-11-13 23:22:40 +03:00
|
|
|
newInfo.forget(), GetAutoPtrComparator(data->mTimeouts));
|
2011-07-17 23:09:13 +04:00
|
|
|
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
LOG(TimeoutsLog(), ("Worker %p has new timeout: delay=%d interval=%s\n", this,
|
|
|
|
aTimeout, aIsInterval ? "yes" : "no"));
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
// If the timeout we just made is set to fire next then we need to update the
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
// timer, unless we're currently running timeouts.
|
2018-11-13 23:22:40 +03:00
|
|
|
if (insertedInfo == data->mTimeouts.Elements() &&
|
|
|
|
!data->mRunningExpiredTimeouts) {
|
|
|
|
if (!data->mTimer) {
|
|
|
|
data->mTimer = NS_NewTimer();
|
|
|
|
if (!data->mTimer) {
|
2017-10-16 09:15:40 +03:00
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
2013-11-05 18:16:26 +04:00
|
|
|
return 0;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mTimerRunnable = new TimerRunnable(this);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if (!data->mTimerRunning) {
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!ModifyBusyCountFromWorker(true)) {
|
2013-11-05 18:16:26 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return 0;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mTimerRunning = true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!RescheduleTimeoutTimer(aCx)) {
|
2013-11-05 18:16:26 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return 0;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
return timerId;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
void WorkerPrivate::ClearTimeout(int32_t aId) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if (!data->mTimeouts.IsEmpty()) {
|
|
|
|
NS_ASSERTION(data->mTimerRunning, "Huh?!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
for (uint32_t index = 0; index < data->mTimeouts.Length(); index++) {
|
|
|
|
nsAutoPtr<TimeoutInfo>& info = data->mTimeouts[index];
|
2011-07-17 23:09:13 +04:00
|
|
|
if (info->mId == aId) {
|
|
|
|
info->mCanceled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WorkerPrivate::RunExpiredTimeouts(JSContext* aCx) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// We may be called recursively (e.g. close() inside a timeout) or we could
|
|
|
|
// have been canceled while this event was pending, bail out if there is
|
|
|
|
// nothing to do.
|
2018-11-13 23:22:40 +03:00
|
|
|
if (data->mRunningExpiredTimeouts || !data->mTimerRunning) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
NS_ASSERTION(data->mTimer && data->mTimerRunnable, "Must have a timer!");
|
|
|
|
NS_ASSERTION(!data->mTimeouts.IsEmpty(), "Should have some work to do!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
bool retval = true;
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
AutoPtrComparator<TimeoutInfo> comparator =
|
|
|
|
GetAutoPtrComparator(data->mTimeouts);
|
2013-11-11 12:04:41 +04:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// We want to make sure to run *something*, even if the timer fired a little
|
|
|
|
// early. Fudge the value of now to at least include the first timeout.
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
const TimeStamp actual_now = TimeStamp::Now();
|
2018-11-13 23:22:40 +03:00
|
|
|
const TimeStamp now = std::max(actual_now, data->mTimeouts[0]->mTargetTime);
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
|
|
|
|
if (now != actual_now) {
|
|
|
|
LOG(TimeoutsLog(), ("Worker %p fudged timeout by %f ms.\n", this,
|
|
|
|
(now - actual_now).ToMilliseconds()));
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<TimeoutInfo*, 10> expiredTimeouts;
|
2018-11-13 23:22:40 +03:00
|
|
|
for (uint32_t index = 0; index < data->mTimeouts.Length(); index++) {
|
|
|
|
nsAutoPtr<TimeoutInfo>& info = data->mTimeouts[index];
|
2011-08-26 11:34:10 +04:00
|
|
|
if (info->mTargetTime > now) {
|
|
|
|
break;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2011-08-26 11:34:10 +04:00
|
|
|
expiredTimeouts.AppendElement(info);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Guard against recursion.
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mRunningExpiredTimeouts = true;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// Run expired timeouts.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < expiredTimeouts.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
TimeoutInfo*& info = expiredTimeouts[index];
|
|
|
|
|
|
|
|
if (info->mCanceled) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
LOG(TimeoutsLog(),
|
|
|
|
("Worker %p executing timeout with original delay %f ms.\n", this,
|
|
|
|
info->mInterval.ToMilliseconds()));
|
|
|
|
|
2016-02-26 23:23:13 +03:00
|
|
|
// Always check JS_IsExceptionPending if something fails, and if
|
|
|
|
// JS_IsExceptionPending returns false (i.e. uncatchable exception) then
|
2011-08-26 11:34:10 +04:00
|
|
|
// break out of the loop.
|
2016-02-26 23:23:13 +03:00
|
|
|
const char* reason;
|
|
|
|
if (info->mIsInterval) {
|
|
|
|
reason = "setInterval handler";
|
|
|
|
} else {
|
|
|
|
reason = "setTimeout handler";
|
|
|
|
}
|
|
|
|
|
2016-08-16 09:10:30 +03:00
|
|
|
RefPtr<Function> callback = info->mHandler->GetCallback();
|
|
|
|
if (!callback) {
|
2017-11-17 06:01:27 +03:00
|
|
|
nsAutoMicroTask mt;
|
|
|
|
|
2016-07-08 03:08:25 +03:00
|
|
|
AutoEntryScript aes(global, reason, false);
|
2016-08-16 09:10:30 +03:00
|
|
|
|
|
|
|
// Evaluate the timeout expression.
|
2016-09-05 22:25:13 +03:00
|
|
|
const nsAString& script = info->mHandler->GetHandlerText();
|
2016-08-16 09:10:30 +03:00
|
|
|
|
|
|
|
const char* filename = nullptr;
|
|
|
|
uint32_t lineNo = 0, dummyColumn = 0;
|
|
|
|
info->mHandler->GetLocation(&filename, &lineNo, &dummyColumn);
|
|
|
|
|
|
|
|
JS::CompileOptions options(aes.cx());
|
|
|
|
options.setFileAndLine(filename, lineNo).setNoScriptRval(true);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> unused(aes.cx());
|
|
|
|
|
2018-11-09 05:42:48 +03:00
|
|
|
JS::SourceText<char16_t> srcBuf;
|
2018-10-23 22:27:16 +03:00
|
|
|
if (!srcBuf.init(aes.cx(), script.BeginReading(), script.Length(),
|
2018-11-09 05:42:48 +03:00
|
|
|
JS::SourceOwnership::Borrowed) ||
|
2018-10-23 22:27:16 +03:00
|
|
|
!JS::Evaluate(aes.cx(), options, srcBuf, &unused)) {
|
|
|
|
if (!JS_IsExceptionPending(aCx)) {
|
|
|
|
retval = false;
|
|
|
|
break;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2016-08-16 09:10:30 +03:00
|
|
|
} else {
|
|
|
|
ErrorResult rv;
|
|
|
|
JS::Rooted<JS::Value> ignoredVal(aCx);
|
|
|
|
callback->Call(GlobalScope(), info->mHandler->GetArgs(), &ignoredVal, rv,
|
|
|
|
reason);
|
|
|
|
if (rv.IsUncatchableException()) {
|
|
|
|
rv.SuppressException();
|
|
|
|
retval = false;
|
|
|
|
break;
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
2016-08-16 09:10:30 +03:00
|
|
|
|
|
|
|
rv.SuppressException();
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
2011-08-26 11:34:10 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
NS_ASSERTION(data->mRunningExpiredTimeouts, "Someone changed this!");
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// No longer possible to be called recursively.
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mRunningExpiredTimeouts = false;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2011-08-26 11:34:10 +04:00
|
|
|
// Now remove canceled and expired timeouts from the main list.
|
2012-08-10 20:10:22 +04:00
|
|
|
// NB: The timeouts present in expiredTimeouts must have the same order
|
|
|
|
// with respect to each other in mTimeouts. That is, mTimeouts is just
|
|
|
|
// expiredTimeouts with extra elements inserted. There may be unexpired
|
|
|
|
// timeouts that have been inserted between the expired timeouts if the
|
|
|
|
// timeout event handler called setTimeout/setInterval.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0, expiredTimeoutIndex = 0,
|
2012-08-10 20:10:22 +04:00
|
|
|
expiredTimeoutLength = expiredTimeouts.Length();
|
2018-11-13 23:22:40 +03:00
|
|
|
index < data->mTimeouts.Length();) {
|
|
|
|
nsAutoPtr<TimeoutInfo>& info = data->mTimeouts[index];
|
2012-08-10 20:10:22 +04:00
|
|
|
if ((expiredTimeoutIndex < expiredTimeoutLength &&
|
|
|
|
info == expiredTimeouts[expiredTimeoutIndex] &&
|
|
|
|
++expiredTimeoutIndex) ||
|
|
|
|
info->mCanceled) {
|
|
|
|
if (info->mIsInterval && !info->mCanceled) {
|
|
|
|
// Reschedule intervals.
|
|
|
|
info->mTargetTime = info->mTargetTime + info->mInterval;
|
|
|
|
// Don't resort the list here, we'll do that at the end.
|
|
|
|
++index;
|
|
|
|
} else {
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mTimeouts.RemoveElement(info);
|
2012-08-10 20:10:22 +04:00
|
|
|
}
|
2011-08-26 11:34:10 +04:00
|
|
|
} else {
|
2012-08-10 20:10:22 +04:00
|
|
|
// If info did not match the current entry in expiredTimeouts, it
|
|
|
|
// shouldn't be there at all.
|
|
|
|
NS_ASSERTION(!expiredTimeouts.Contains(info),
|
|
|
|
"Our timeouts are out of order!");
|
|
|
|
++index;
|
2011-08-26 11:34:10 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mTimeouts.Sort(comparator);
|
2012-08-10 20:10:22 +04:00
|
|
|
|
2012-03-27 08:05:09 +04:00
|
|
|
// Either signal the parent that we're no longer using timeouts or reschedule
|
|
|
|
// the timer.
|
2018-11-13 23:22:40 +03:00
|
|
|
if (data->mTimeouts.IsEmpty()) {
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!ModifyBusyCountFromWorker(false)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
retval = false;
|
|
|
|
}
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mTimerRunning = false;
|
2011-07-17 23:09:13 +04:00
|
|
|
} else if (retval && !RescheduleTimeoutTimer(aCx)) {
|
|
|
|
retval = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WorkerPrivate::RescheduleTimeoutTimer(JSContext* aCx) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
|
|
|
MOZ_ASSERT(!data->mRunningExpiredTimeouts);
|
|
|
|
NS_ASSERTION(!data->mTimeouts.IsEmpty(), "Should have some timeouts!");
|
|
|
|
NS_ASSERTION(data->mTimer && data->mTimerRunnable, "Should have a timer!");
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
|
|
|
|
// NB: This is important! The timer may have already fired, e.g. if a timeout
|
|
|
|
// callback itself calls setTimeout for a short duration and then takes longer
|
|
|
|
// than that to finish executing. If that has happened, it's very important
|
|
|
|
// that we don't execute the event that is now pending in our event queue, or
|
|
|
|
// our code in RunExpiredTimeouts to "fudge" the timeout value will unleash an
|
|
|
|
// early timeout when we execute the event we're about to queue.
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mTimer->Cancel();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
double delta =
|
2018-11-13 23:22:40 +03:00
|
|
|
(data->mTimeouts[0]->mTargetTime - TimeStamp::Now()).ToMilliseconds();
|
2013-01-15 16:22:03 +04:00
|
|
|
uint32_t delay = delta > 0 ? std::min(delta, double(UINT32_MAX)) : 0;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2017-07-26 23:03:57 +03:00
|
|
|
LOG(TimeoutsLog(),
|
|
|
|
("Worker %p scheduled timer for %d ms, %zu pending timeouts\n", this,
|
2018-11-13 23:22:40 +03:00
|
|
|
delay, data->mTimeouts.Length()));
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
nsresult rv = data->mTimer->InitWithCallback(data->mTimerRunnable, delay,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
2011-07-17 23:09:13 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2016-08-14 14:39:28 +03:00
|
|
|
JS_ReportErrorASCII(aCx, "Failed to start timer!");
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-17 21:51:04 +03:00
|
|
|
void WorkerPrivate::StartCancelingTimer() {
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2018-04-17 21:51:04 +03:00
|
|
|
auto errorCleanup = MakeScopeExit([&] { mCancelingTimer = nullptr; });
|
2018-04-17 21:51:04 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(!mCancelingTimer);
|
|
|
|
|
|
|
|
if (WorkerPrivate* parent = GetParent()) {
|
|
|
|
mCancelingTimer = NS_NewTimer(parent->ControlEventTarget());
|
|
|
|
} else {
|
|
|
|
mCancelingTimer = NS_NewTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mCancelingTimer)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is not needed if we are already in an advanced shutdown state.
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2018-07-12 20:33:41 +03:00
|
|
|
if (ParentStatus() >= Canceling) {
|
2018-04-17 21:51:04 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 21:36:18 +03:00
|
|
|
uint32_t cancelingTimeoutMillis =
|
|
|
|
StaticPrefs::dom_worker_canceling_timeoutMilliseconds();
|
2018-04-17 21:51:04 +03:00
|
|
|
|
2018-04-17 21:51:04 +03:00
|
|
|
RefPtr<CancelingTimerCallback> callback = new CancelingTimerCallback(this);
|
2018-04-17 21:51:04 +03:00
|
|
|
nsresult rv = mCancelingTimer->InitWithCallback(
|
2018-04-17 21:51:04 +03:00
|
|
|
callback, cancelingTimeoutMillis, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-17 21:51:04 +03:00
|
|
|
errorCleanup.release();
|
2018-04-17 21:51:04 +03:00
|
|
|
}
|
|
|
|
|
2016-07-07 09:15:15 +03:00
|
|
|
void WorkerPrivate::UpdateContextOptionsInternal(
|
|
|
|
JSContext* aCx, const JS::ContextOptions& aContextOptions) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-07-07 09:15:15 +03:00
|
|
|
JS::ContextOptionsRef(aCx) = aContextOptions;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
for (uint32_t index = 0; index < data->mChildWorkers.Length(); index++) {
|
|
|
|
data->mChildWorkers[index]->UpdateContextOptions(aContextOptions);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
void WorkerPrivate::UpdateLanguagesInternal(
|
|
|
|
const nsTArray<nsString>& aLanguages) {
|
2014-09-05 18:26:34 +04:00
|
|
|
WorkerGlobalScope* globalScope = GlobalScope();
|
|
|
|
if (globalScope) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerNavigator> nav = globalScope->GetExistingNavigator();
|
2014-09-05 18:26:34 +04:00
|
|
|
if (nav) {
|
|
|
|
nav->SetLanguages(aLanguages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
|
|
|
for (uint32_t index = 0; index < data->mChildWorkers.Length(); index++) {
|
|
|
|
data->mChildWorkers[index]->UpdateLanguages(aLanguages);
|
2014-09-05 18:26:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-11 02:50:40 +04:00
|
|
|
void WorkerPrivate::UpdateJSWorkerMemoryParameterInternal(JSContext* aCx,
|
|
|
|
JSGCParamKey aKey,
|
|
|
|
uint32_t aValue) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2013-05-17 02:49:43 +04:00
|
|
|
|
|
|
|
// XXX aValue might be 0 here (telling us to unset a previous value for child
|
|
|
|
// workers). Calling JS_SetGCParameter with a value of 0 isn't actually
|
|
|
|
// supported though. We really need some way to revert to a default value
|
|
|
|
// here.
|
|
|
|
if (aValue) {
|
2016-07-05 15:35:21 +03:00
|
|
|
JS_SetGCParameter(aCx, aKey, aValue);
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
2012-01-04 23:11:32 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
for (uint32_t index = 0; index < data->mChildWorkers.Length(); index++) {
|
|
|
|
data->mChildWorkers[index]->UpdateJSWorkerMemoryParameter(aKey, aValue);
|
2012-01-04 23:11:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
#ifdef JS_GC_ZEAL
|
2013-05-17 02:49:43 +04:00
|
|
|
void WorkerPrivate::UpdateGCZealInternal(JSContext* aCx, uint8_t aGCZeal,
|
|
|
|
uint32_t aFrequency) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-07-05 12:06:05 +03:00
|
|
|
JS_SetGCZeal(aCx, aGCZeal, aFrequency);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
for (uint32_t index = 0; index < data->mChildWorkers.Length(); index++) {
|
|
|
|
data->mChildWorkers[index]->UpdateGCZeal(aGCZeal, aFrequency);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
void WorkerPrivate::GarbageCollectInternal(JSContext* aCx, bool aShrinking,
|
|
|
|
bool aCollectChildren) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2012-01-18 00:05:25 +04:00
|
|
|
|
2015-06-11 00:12:55 +03:00
|
|
|
if (!GlobalScope()) {
|
2013-10-23 17:16:49 +04:00
|
|
|
// We haven't compiled anything yet. Just bail out.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-25 23:03:27 +04:00
|
|
|
if (aShrinking || aCollectChildren) {
|
2016-07-07 10:55:41 +03:00
|
|
|
JS::PrepareForFullGC(aCx);
|
2013-07-25 23:03:27 +04:00
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
if (aShrinking) {
|
2018-05-25 02:19:18 +03:00
|
|
|
JS::NonIncrementalGC(aCx, GC_SHRINK, JS::gcreason::DOM_WORKER);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
if (!aCollectChildren) {
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
LOG(WorkerLog(), ("Worker %p collected idle garbage\n", this));
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
2013-05-17 02:49:43 +04:00
|
|
|
} else {
|
2018-05-25 02:19:18 +03:00
|
|
|
JS::NonIncrementalGC(aCx, GC_NORMAL, JS::gcreason::DOM_WORKER);
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
LOG(WorkerLog(), ("Worker %p collected garbage\n", this));
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
2012-01-18 00:05:25 +04:00
|
|
|
} else {
|
2013-05-17 02:49:43 +04:00
|
|
|
JS_MaybeGC(aCx);
|
Bug 1059469: Part 2 - When rescheduling the interval timer, cancel it first, and refactor things so that actually does something. r=bent
RunExpiredTimeouts has "fudging" code to always ensure that we execute at least one timeout. This is intended to cover cases where an nsITimer fires slightly early, but it means we must be careful not to fire a timer more times than we intend to or we'll execute a timeout prematurely.
Consider a sequences of setTimeout calls alternating in delay between 0ms and 1000ms. When the 1000ms timeout fires, it schedules a 0ms timeout. The setTimeout call itself calls RescheduleTimeoutTimer, which schedules the timer for a 0 ms delay. And once we unwind the 1000ms timeout RunExpiredTimeouts will also schedule the timer for a 0 ms delay. If the timer has fired (remember, it's processed on a completely different thread) in the meantime, we ultimately will get two callbacks from nsITimer for our 0 ms timeout. The first will run the 0 ms timeout and schedule a 1000 ms timeout, and the second will run the 1000 ms timeout (remember, RunExpiredTimeouts always runs at least one timeout!) ~999 ms ahead of schedule.
The solution is to cancel the timer in RescheduleTimeoutTimer, so that when we call it the second time it will cause any pending events from the first scheduling to be canceled. But this actually doesn't work at all, because of how we use nsITimer. Before worker threads were capable of accepting arbitrary runnables we created TimerThreadEventTarget, which translates the timer firing to the special worker event queue when the timer thread attempts to *dispatch* a runnable to the worker. We still need this for some of the other types of timers (which use control runnables that interrupt JS, and not the regular event queue). But setTimeout can simply run like a normal nsITimer callback now. We need that here, or calling nsITimer::Cancel won't actually do anything, because the timer's event was ignored and TimerThreadEventTarget created its own event.
2016-01-07 00:18:29 +03:00
|
|
|
LOG(WorkerLog(), ("Worker %p collected periodic garbage\n", this));
|
2012-01-18 00:05:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aCollectChildren) {
|
2018-11-13 23:22:40 +03:00
|
|
|
for (uint32_t index = 0; index < data->mChildWorkers.Length(); index++) {
|
|
|
|
data->mChildWorkers[index]->GarbageCollect(aShrinking);
|
2012-01-18 00:05:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
void WorkerPrivate::CycleCollectInternal(bool aCollectChildren) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2013-12-03 08:07:02 +04:00
|
|
|
|
|
|
|
nsCycleCollector_collect(nullptr);
|
|
|
|
|
|
|
|
if (aCollectChildren) {
|
2018-11-13 23:22:40 +03:00
|
|
|
for (uint32_t index = 0; index < data->mChildWorkers.Length(); index++) {
|
|
|
|
data->mChildWorkers[index]->CycleCollect(/* aDummy = */ false);
|
2013-12-03 08:07:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
void WorkerPrivate::MemoryPressureInternal() {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2016-03-24 00:55:07 +03:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if (data->mScope) {
|
|
|
|
RefPtr<Console> console = data->mScope->GetConsoleIfExists();
|
2017-07-20 13:57:08 +03:00
|
|
|
if (console) {
|
|
|
|
console->ClearStorage();
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
RefPtr<Performance> performance = data->mScope->GetPerformanceIfExists();
|
2017-07-20 13:57:08 +03:00
|
|
|
if (performance) {
|
|
|
|
performance->MemoryPressure();
|
|
|
|
}
|
2016-03-24 00:55:07 +03:00
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if (data->mDebuggerScope) {
|
|
|
|
RefPtr<Console> console = data->mDebuggerScope->GetConsoleIfExists();
|
2017-07-20 13:57:08 +03:00
|
|
|
if (console) {
|
|
|
|
console->ClearStorage();
|
|
|
|
}
|
2016-03-24 00:55:07 +03:00
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
for (uint32_t index = 0; index < data->mChildWorkers.Length(); index++) {
|
|
|
|
data->mChildWorkers[index]->MemoryPressure(false);
|
2016-03-24 00:55:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
void WorkerPrivate::SetThread(WorkerThread* aThread) {
|
2013-10-23 17:16:49 +04:00
|
|
|
if (aThread) {
|
2014-11-17 22:55:37 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
bool isOnCurrentThread;
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(aThread->IsOnCurrentThread(&isOnCurrentThread)));
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ASSERT(!isOnCurrentThread);
|
2014-11-17 22:55:37 +03:00
|
|
|
}
|
|
|
|
#endif
|
2013-06-23 01:48:00 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(!mPRThread);
|
|
|
|
mPRThread = PRThreadFromThread(aThread);
|
|
|
|
MOZ_ASSERT(mPRThread);
|
2018-11-13 23:22:40 +03:00
|
|
|
|
|
|
|
mWorkerThreadAccessible.Transfer(mPRThread);
|
2013-10-23 17:16:49 +04:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mPRThread);
|
|
|
|
}
|
2018-11-13 23:22:40 +03:00
|
|
|
}
|
2013-06-23 01:48:00 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
void WorkerPrivate::SetWorkerPrivateInWorkerThread(
|
|
|
|
WorkerThread* const aThread) {
|
|
|
|
MutexAutoLock lock(mMutex);
|
2014-11-17 22:55:37 +03:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ASSERT(!mThread);
|
|
|
|
MOZ_ASSERT(mStatus == Pending);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
mThread = aThread;
|
|
|
|
mThread->SetWorker(WorkerThreadFriendKey{}, this);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if (!mPreStartRunnables.IsEmpty()) {
|
|
|
|
for (uint32_t index = 0; index < mPreStartRunnables.Length(); index++) {
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(mThread->DispatchAnyThread(
|
|
|
|
WorkerThreadFriendKey{}, mPreStartRunnables[index].forget()));
|
|
|
|
}
|
|
|
|
mPreStartRunnables.Clear();
|
|
|
|
}
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
void WorkerPrivate::ResetWorkerPrivateInWorkerThread() {
|
|
|
|
RefPtr<WorkerThread> doomedThread;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
// Release the mutex before doomedThread.
|
|
|
|
MutexAutoLock lock(mMutex);
|
2014-11-17 22:55:37 +03:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ASSERT(mThread);
|
2014-11-17 22:55:37 +03:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
mThread->SetWorker(WorkerThreadFriendKey{}, nullptr);
|
|
|
|
mThread.swap(doomedThread);
|
2013-06-23 01:48:00 +04:00
|
|
|
}
|
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
void WorkerPrivate::BeginCTypesCall() {
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Don't try to GC while we're blocked in a ctypes call.
|
|
|
|
SetGCTimerMode(NoTimer);
|
2012-12-30 22:21:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void WorkerPrivate::EndCTypesCall() {
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Make sure the periodic timer is running before we start running JS again.
|
|
|
|
SetGCTimerMode(PeriodicTimer);
|
2012-12-30 22:21:52 +04:00
|
|
|
}
|
|
|
|
|
2015-09-16 06:27:56 +03:00
|
|
|
bool WorkerPrivate::ConnectMessagePort(
|
2018-11-20 02:18:33 +03:00
|
|
|
JSContext* aCx, const MessagePortIdentifier& aIdentifier) {
|
2013-06-05 18:04:23 +04:00
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
WorkerGlobalScope* globalScope = GlobalScope();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
JS::Rooted<JSObject*> jsGlobal(aCx, globalScope->GetWrapper());
|
|
|
|
MOZ_ASSERT(jsGlobal);
|
|
|
|
|
2015-09-16 06:27:56 +03:00
|
|
|
// This MessagePortIdentifier is used to create a new port, still connected
|
|
|
|
// with the other one, but in the worker thread.
|
|
|
|
ErrorResult rv;
|
2016-03-01 17:21:11 +03:00
|
|
|
RefPtr<MessagePort> port = MessagePort::Create(globalScope, aIdentifier, rv);
|
2015-09-16 06:27:56 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
2016-07-22 17:50:10 +03:00
|
|
|
rv.SuppressException();
|
2015-09-16 06:27:56 +03:00
|
|
|
return false;
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
GlobalObject globalObject(aCx, jsGlobal);
|
|
|
|
if (globalObject.Failed()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-22 00:42:50 +04:00
|
|
|
RootedDictionary<MessageEventInit> init(aCx);
|
2018-11-27 10:44:47 +03:00
|
|
|
init.mData = JS_GetEmptyStringValue(aCx);
|
2013-11-22 00:39:43 +04:00
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
2013-12-20 12:51:03 +04:00
|
|
|
init.mSource.SetValue().SetAsMessagePort() = port;
|
2016-10-26 23:00:17 +03:00
|
|
|
if (!init.mPorts.AppendElement(port.forget(), fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-22 00:39:43 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MessageEvent> event = MessageEvent::Constructor(
|
2014-02-27 14:51:14 +04:00
|
|
|
globalObject, NS_LITERAL_STRING("connect"), init, rv);
|
2013-11-05 18:16:26 +04:00
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
globalScope->DispatchEvent(*event);
|
2015-09-15 21:08:09 +03:00
|
|
|
|
2015-09-16 06:27:56 +03:00
|
|
|
return true;
|
2015-09-15 21:08:09 +03:00
|
|
|
}
|
|
|
|
|
2015-03-04 02:51:53 +03:00
|
|
|
WorkerGlobalScope* WorkerPrivate::GetOrCreateGlobalScope(JSContext* aCx) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2013-11-05 18:16:26 +04:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
if (!data->mScope) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerGlobalScope> globalScope;
|
2015-03-04 02:51:53 +03:00
|
|
|
if (IsSharedWorker()) {
|
2015-10-01 02:11:03 +03:00
|
|
|
globalScope = new SharedWorkerGlobalScope(this, WorkerName());
|
2015-03-04 02:51:53 +03:00
|
|
|
} else if (IsServiceWorker()) {
|
2018-02-14 22:23:15 +03:00
|
|
|
globalScope = new ServiceWorkerGlobalScope(
|
|
|
|
this, GetServiceWorkerRegistrationDescriptor());
|
2015-03-04 02:51:53 +03:00
|
|
|
} else {
|
2017-05-17 17:48:54 +03:00
|
|
|
globalScope = new DedicatedWorkerGlobalScope(this, WorkerName());
|
2015-03-04 02:51:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> global(aCx);
|
|
|
|
NS_ENSURE_TRUE(globalScope->WrapGlobalObject(aCx, &global), nullptr);
|
|
|
|
|
2018-07-31 09:37:17 +03:00
|
|
|
JSAutoRealm ar(aCx, global);
|
2015-03-04 02:51:53 +03:00
|
|
|
|
2015-06-11 00:12:55 +03:00
|
|
|
// RegisterBindings() can spin a nested event loop so we have to set mScope
|
|
|
|
// before calling it, and we have to make sure to unset mScope if it fails.
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mScope = std::move(globalScope);
|
2015-06-11 00:12:55 +03:00
|
|
|
|
2015-03-04 02:51:53 +03:00
|
|
|
if (!RegisterBindings(aCx, global)) {
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mScope = nullptr;
|
2015-03-04 02:51:53 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_FireOnNewGlobalObject(aCx, global);
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
return data->mScope;
|
2015-03-04 02:51:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
WorkerDebuggerGlobalScope* WorkerPrivate::CreateDebuggerGlobalScope(
|
|
|
|
JSContext* aCx) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2015-03-04 02:51:53 +03:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ASSERT(!data->mDebuggerScope);
|
2015-03-04 02:51:53 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerDebuggerGlobalScope> globalScope =
|
2015-03-04 02:51:53 +03:00
|
|
|
new WorkerDebuggerGlobalScope(this);
|
|
|
|
|
2015-01-09 00:56:42 +03:00
|
|
|
JS::Rooted<JSObject*> global(aCx);
|
2015-03-04 02:51:53 +03:00
|
|
|
NS_ENSURE_TRUE(globalScope->WrapGlobalObject(aCx, &global), nullptr);
|
2013-11-05 18:16:26 +04:00
|
|
|
|
2018-07-31 09:37:17 +03:00
|
|
|
JSAutoRealm ar(aCx, global);
|
2013-11-05 18:16:26 +04:00
|
|
|
|
2016-02-22 12:41:09 +03:00
|
|
|
// RegisterDebuggerBindings() can spin a nested event loop so we have to set
|
|
|
|
// mDebuggerScope before calling it, and we have to make sure to unset
|
|
|
|
// mDebuggerScope if it fails.
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mDebuggerScope = std::move(globalScope);
|
2016-02-22 12:41:09 +03:00
|
|
|
|
|
|
|
if (!RegisterDebuggerBindings(aCx, global)) {
|
2018-11-13 23:22:40 +03:00
|
|
|
data->mDebuggerScope = nullptr;
|
2013-11-05 18:16:26 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_FireOnNewGlobalObject(aCx, global);
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
return data->mDebuggerScope;
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
|
|
|
|
2018-03-13 23:16:48 +03:00
|
|
|
bool WorkerPrivate::IsOnWorkerThread() const {
|
2018-07-28 01:26:08 +03:00
|
|
|
// We can't use mThread because it must be protected by mMutex and sometimes
|
|
|
|
// this method is called when mMutex is already locked. This method should
|
|
|
|
// always work.
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(mPRThread,
|
|
|
|
"AssertIsOnWorkerThread() called before a thread was assigned!");
|
|
|
|
|
2018-07-28 01:26:08 +03:00
|
|
|
return mPRThread == PR_GetCurrentThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2018-03-13 23:16:48 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
void WorkerPrivate::AssertIsOnWorkerThread() const {
|
|
|
|
MOZ_ASSERT(IsOnWorkerThread());
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
#endif // DEBUG
|
|
|
|
|
2017-11-15 09:58:38 +03:00
|
|
|
void WorkerPrivate::DumpCrashInformation(nsACString& aString) {
|
2018-11-13 23:22:40 +03:00
|
|
|
MOZ_ACCESS_THREAD_BOUND(mWorkerThreadAccessible, data);
|
2017-11-15 09:58:38 +03:00
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
nsTObserverArray<WorkerHolder*>::ForwardIterator iter(data->mHolders);
|
2017-11-15 09:58:38 +03:00
|
|
|
while (iter.HasMore()) {
|
|
|
|
WorkerHolder* holder = iter.GetNext();
|
|
|
|
aString.Append("|");
|
|
|
|
aString.Append(holder->Name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 14:36:25 +03:00
|
|
|
void WorkerPrivate::EnsurePerformanceCounter() {
|
2018-03-06 12:19:19 +03:00
|
|
|
AssertIsOnWorkerThread();
|
2018-06-19 17:14:06 +03:00
|
|
|
MOZ_ASSERT(mozilla::StaticPrefs::dom_performance_enable_scheduler_timing());
|
2018-03-06 12:19:19 +03:00
|
|
|
if (!mPerformanceCounter) {
|
2018-06-19 17:14:06 +03:00
|
|
|
nsPrintfCString workerName("Worker:%s",
|
|
|
|
NS_ConvertUTF16toUTF8(mWorkerName).get());
|
|
|
|
mPerformanceCounter = new PerformanceCounter(workerName);
|
2018-03-06 12:19:19 +03:00
|
|
|
}
|
2018-04-04 14:36:25 +03:00
|
|
|
}
|
2018-03-06 12:19:19 +03:00
|
|
|
|
2018-04-04 14:36:25 +03:00
|
|
|
PerformanceCounter* WorkerPrivate::GetPerformanceCounter() {
|
2018-03-06 12:19:19 +03:00
|
|
|
return mPerformanceCounter;
|
|
|
|
}
|
|
|
|
|
2018-01-24 19:17:31 +03:00
|
|
|
PerformanceStorage* WorkerPrivate::GetPerformanceStorage() {
|
2018-01-31 10:24:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2018-03-14 17:27:13 +03:00
|
|
|
MOZ_ASSERT(mPerformanceStorage);
|
2018-01-24 19:17:32 +03:00
|
|
|
return mPerformanceStorage;
|
2018-01-24 19:17:31 +03:00
|
|
|
}
|
|
|
|
|
2018-11-20 02:18:33 +03:00
|
|
|
void WorkerPrivate::SetRemoteWorkerController(RemoteWorkerChild* aController) {
|
2018-11-20 02:18:21 +03:00
|
|
|
AssertIsOnMainThread();
|
2018-11-20 02:18:33 +03:00
|
|
|
MOZ_ASSERT(aController);
|
|
|
|
MOZ_ASSERT(!mRemoteWorkerController);
|
2018-11-20 02:18:21 +03:00
|
|
|
|
2018-11-20 02:18:33 +03:00
|
|
|
mRemoteWorkerController = aController;
|
2018-11-20 02:18:21 +03:00
|
|
|
}
|
|
|
|
|
2018-11-20 02:18:33 +03:00
|
|
|
RemoteWorkerChild* WorkerPrivate::GetRemoteWorkerController() {
|
2018-11-20 02:18:21 +03:00
|
|
|
AssertIsOnMainThread();
|
2018-11-20 02:18:33 +03:00
|
|
|
MOZ_ASSERT(mRemoteWorkerController);
|
|
|
|
return mRemoteWorkerController;
|
2018-11-20 02:18:21 +03:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:33:32 +03:00
|
|
|
NS_IMPL_ADDREF(WorkerPrivate::EventTarget)
|
|
|
|
NS_IMPL_RELEASE(WorkerPrivate::EventTarget)
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2018-02-08 11:33:32 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN(WorkerPrivate::EventTarget)
|
2017-05-23 00:25:43 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISerialEventTarget)
|
2013-10-23 17:16:49 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIEventTarget)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
#ifdef DEBUG
|
|
|
|
// kDEBUGWorkerEventTargetIID is special in that it does not AddRef its
|
|
|
|
// result.
|
|
|
|
if (aIID.Equals(kDEBUGWorkerEventTargetIID)) {
|
|
|
|
*aInstancePtr = this;
|
|
|
|
return NS_OK;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-02-08 11:33:32 +03:00
|
|
|
WorkerPrivate::EventTarget::DispatchFromScript(nsIRunnable* aRunnable,
|
|
|
|
uint32_t aFlags) {
|
2015-07-10 06:21:46 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event(aRunnable);
|
|
|
|
return Dispatch(event.forget(), aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-02-08 11:33:32 +03:00
|
|
|
WorkerPrivate::EventTarget::Dispatch(already_AddRefed<nsIRunnable> aRunnable,
|
|
|
|
uint32_t aFlags) {
|
2013-10-23 17:16:49 +04:00
|
|
|
// May be called on any thread!
|
2015-07-10 06:21:46 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event(aRunnable);
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Workers only support asynchronous dispatch for now.
|
|
|
|
if (NS_WARN_IF(aFlags != NS_DISPATCH_NORMAL)) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerRunnable> workerRunnable;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
NS_WARNING(
|
|
|
|
"A runnable was posted to a worker that is already shutting "
|
|
|
|
"down!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
if (event) {
|
|
|
|
workerRunnable = mWorkerPrivate->MaybeWrapAsWorkerRunnable(event.forget());
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv =
|
2018-04-17 06:08:26 +03:00
|
|
|
mWorkerPrivate->Dispatch(workerRunnable.forget(), mNestedEventTarget);
|
2013-10-23 17:16:49 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2016-05-13 01:15:43 +03:00
|
|
|
NS_IMETHODIMP
|
2018-02-08 11:33:32 +03:00
|
|
|
WorkerPrivate::EventTarget::DelayedDispatch(already_AddRefed<nsIRunnable>,
|
|
|
|
uint32_t)
|
|
|
|
|
2016-05-13 01:15:43 +03:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
NS_IMETHODIMP
|
2018-02-08 11:33:32 +03:00
|
|
|
WorkerPrivate::EventTarget::IsOnCurrentThread(bool* aIsOnCurrentThread) {
|
2013-10-23 17:16:49 +04:00
|
|
|
// May be called on any thread!
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aIsOnCurrentThread);
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
NS_WARNING("A worker's event target was used after the worker has !");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2017-05-22 21:26:39 +03:00
|
|
|
*aIsOnCurrentThread = mWorkerPrivate->IsOnCurrentThread();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(bool)
|
2018-02-08 11:33:32 +03:00
|
|
|
WorkerPrivate::EventTarget::IsOnCurrentThreadInfallible() {
|
2017-05-22 21:26:39 +03:00
|
|
|
// May be called on any thread!
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
NS_WARNING("A worker's event target was used after the worker has !");
|
|
|
|
return false;
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
2017-05-22 21:26:39 +03:00
|
|
|
return mWorkerPrivate->IsOnCurrentThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2018-01-31 10:25:30 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|