/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* 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/. */ #include "WorkerPrivate.h" #include "amIAddonManager.h" #include "nsIClassInfo.h" #include "nsIContentSecurityPolicy.h" #include "nsIConsoleService.h" #include "nsIDOMDOMException.h" #include "nsIDOMEvent.h" #include "nsIDocument.h" #include "nsIDocShell.h" #include "nsIInterfaceRequestor.h" #include "nsIMemoryReporter.h" #include "nsINetworkInterceptController.h" #include "nsIPermissionManager.h" #include "nsIScriptError.h" #include "nsIScriptGlobalObject.h" #include "nsIScriptSecurityManager.h" #include "nsIScriptTimeoutHandler.h" #include "nsITabChild.h" #include "nsITextToSubURI.h" #include "nsIThreadInternal.h" #include "nsITimer.h" #include "nsIURI.h" #include "nsIURL.h" #include "nsIWeakReferenceUtils.h" #include "nsIWorkerDebugger.h" #include "nsIXPConnect.h" #include "nsPIDOMWindow.h" #include "nsGlobalWindow.h" #include #include "ImageContainer.h" #include "jsfriendapi.h" #include "js/MemoryMetrics.h" #include "mozilla/Assertions.h" #include "mozilla/Attributes.h" #include "mozilla/ContentEvents.h" #include "mozilla/EventDispatcher.h" #include "mozilla/Likely.h" #include "mozilla/LoadContext.h" #include "mozilla/Move.h" #include "mozilla/dom/BindingUtils.h" #include "mozilla/dom/Console.h" #include "mozilla/dom/DocGroup.h" #include "mozilla/dom/ErrorEvent.h" #include "mozilla/dom/ErrorEventBinding.h" #include "mozilla/dom/Exceptions.h" #include "mozilla/dom/ExtendableMessageEventBinding.h" #include "mozilla/dom/FunctionBinding.h" #include "mozilla/dom/IndexedDatabaseManager.h" #include "mozilla/dom/MessageEvent.h" #include "mozilla/dom/MessageEventBinding.h" #include "mozilla/dom/MessagePort.h" #include "mozilla/dom/MessagePortBinding.h" #include "mozilla/dom/Performance.h" #include "mozilla/dom/PMessagePort.h" #include "mozilla/dom/Promise.h" #include "mozilla/dom/PromiseDebugging.h" #include "mozilla/dom/PromiseNativeHandler.h" #include "mozilla/dom/SimpleGlobalObject.h" #include "mozilla/dom/ScriptSettings.h" #include "mozilla/dom/StructuredCloneHolder.h" #include "mozilla/dom/TabChild.h" #include "mozilla/dom/WorkerBinding.h" #include "mozilla/dom/WorkerDebuggerGlobalScopeBinding.h" #include "mozilla/dom/WorkerGlobalScopeBinding.h" #include "mozilla/Preferences.h" #include "mozilla/ThrottledEventQueue.h" #include "mozilla/TimelineConsumers.h" #include "mozilla/WorkerTimelineMarker.h" #include "nsAlgorithm.h" #include "nsContentUtils.h" #include "nsCycleCollector.h" #include "nsError.h" #include "nsDOMJSUtils.h" #include "nsHostObjectProtocolHandler.h" #include "nsJSEnvironment.h" #include "nsJSUtils.h" #include "nsNetUtil.h" #include "nsPrintfCString.h" #include "nsProxyRelease.h" #include "nsQueryObject.h" #include "nsSandboxFlags.h" #include "nsScriptError.h" #include "nsUTF8Utils.h" #include "prthread.h" #include "xpcpublic.h" #ifdef DEBUG #include "nsThreadManager.h" #endif #include "Navigator.h" #include "Principal.h" #include "RuntimeService.h" #include "ScriptLoader.h" #include "ServiceWorkerEvents.h" #include "ServiceWorkerManager.h" #include "ServiceWorkerWindowClient.h" #include "SharedWorker.h" #include "WorkerDebuggerManager.h" #include "WorkerHolder.h" #include "WorkerNavigator.h" #include "WorkerRunnable.h" #include "WorkerScope.h" #include "WorkerThread.h" // JS_MaybeGC will run once every second during normal execution. #define PERIODIC_GC_TIMER_DELAY_SEC 1 // A shrinking GC will run five seconds after the last event is processed. #define IDLE_GC_TIMER_DELAY_SEC 5 #define PREF_WORKERS_ENABLED "dom.workers.enabled" static mozilla::LazyLogModule sWorkerPrivateLog("WorkerPrivate"); static mozilla::LazyLogModule sWorkerTimeoutsLog("WorkerTimeouts"); mozilla::LogModule* WorkerLog() { return sWorkerPrivateLog; } mozilla::LogModule* TimeoutsLog() { return sWorkerTimeoutsLog; } #define LOG(log, _args) MOZ_LOG(log, LogLevel::Debug, _args); using namespace mozilla; using namespace mozilla::dom; using namespace mozilla::ipc; USING_WORKERS_NAMESPACE MOZ_DEFINE_MALLOC_SIZE_OF(JsWorkerMallocSizeOf) #ifdef DEBUG BEGIN_WORKERS_NAMESPACE void AssertIsOnMainThread() { MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!"); } END_WORKERS_NAMESPACE #endif namespace { #ifdef DEBUG const nsIID kDEBUGWorkerEventTargetIID = { 0xccaba3fa, 0x5be2, 0x4de2, { 0xba, 0x87, 0x3b, 0x3b, 0x5b, 0x1d, 0x5, 0xfb } }; #endif // The number of nested timeouts before we start clamping. HTML says 5. const uint32_t kClampTimeoutNestingLevel = 5u; template class AutoPtrComparator { typedef nsAutoPtr A; typedef T* B; public: bool Equals(const A& a, const B& b) const { return a && b ? *a == *b : !a && !b ? true : false; } bool LessThan(const A& a, const B& b) const { return a && b ? *a < *b : b ? true : false; } }; template inline AutoPtrComparator GetAutoPtrComparator(const nsTArray >&) { return AutoPtrComparator(); } // Specialize this if there's some class that has multiple nsISupports bases. template struct ISupportsBaseInfo { typedef T ISupportsBase; }; template