Bug 1202706 - Part 3: Hook up worker use counters to WebIDL bindings; r=bzbarsky
authorEdgar Chen <echen@mozilla.com>
Wed, 04 Dec 2019 23:21:58 +0000
changeset 505566 95b8b6b94213f4549642f0137d6e3ff0129ef68a
parent 505565 9eeb03a503fb86df387049e878c0c5dd76e0387c
child 505567 2dfc53add3de16916c27818a904ab3e8adc250df
push id102347
push userechen@mozilla.com
push dateWed, 04 Dec 2019 23:23:46 +0000
treeherderautoland@2dfc53add3de [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbzbarsky
bugs1202706
milestone73.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1202706 - Part 3: Hook up worker use counters to WebIDL bindings; r=bzbarsky Differential Revision: https://phabricator.services.mozilla.com/D53749
dom/bindings/BindingUtils.cpp
dom/bindings/BindingUtils.h
dom/bindings/Codegen.py
--- a/dom/bindings/BindingUtils.cpp
+++ b/dom/bindings/BindingUtils.cpp
@@ -3875,16 +3875,24 @@ void AssertReflectorHasGivenProto(JSCont
 void SetUseCounter(JSObject* aObject, UseCounter aUseCounter) {
   nsGlobalWindowInner* win =
       xpc::WindowGlobalOrNull(js::UncheckedUnwrap(aObject));
   if (win && win->GetDocument()) {
     win->GetDocument()->SetUseCounter(aUseCounter);
   }
 }
 
+void SetUseCounter(UseCounterWorker aUseCounter) {
+  // If this is called from Worklet thread, workerPrivate will be null.
+  WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
+  if (workerPrivate) {
+    workerPrivate->SetUseCounter(aUseCounter);
+  }
+}
+
 namespace {
 
 #define DEPRECATED_OPERATION(_op) #_op,
 static const char* kDeprecatedOperations[] = {
 #include "nsDeprecatedOperationList.h"
     nullptr};
 #undef DEPRECATED_OPERATION
 
--- a/dom/bindings/BindingUtils.h
+++ b/dom/bindings/BindingUtils.h
@@ -45,16 +45,17 @@
 
 #include "nsWrapperCacheInlines.h"
 
 class nsGenericHTMLElement;
 
 namespace mozilla {
 
 enum UseCounter : int16_t;
+enum class UseCounterWorker : int16_t;
 
 namespace dom {
 class CustomElementReactionsStack;
 class MessageManagerGlobal;
 class DedicatedWorkerGlobalScope;
 template <typename KeyType, typename ValueType>
 class Record;
 class WindowProxyHolder;
@@ -3092,16 +3093,17 @@ bool GetDesiredProto(JSContext* aCx, con
 // This function is expected to be called from the constructor function for an
 // HTML or XUL element interface; the global/callargs need to be whatever was
 // passed to that constructor function.
 already_AddRefed<Element> CreateXULOrHTMLElement(
     const GlobalObject& aGlobal, const JS::CallArgs& aCallArgs,
     JS::Handle<JSObject*> aGivenProto, ErrorResult& aRv);
 
 void SetUseCounter(JSObject* aObject, UseCounter aUseCounter);
+void SetUseCounter(UseCounterWorker aUseCounter);
 
 // Warnings
 void DeprecationWarning(JSContext* aCx, JSObject* aObject,
                         Document::DeprecatedOperations aOperation);
 
 void DeprecationWarning(const GlobalObject& aGlobal,
                         Document::DeprecatedOperations aOperation);
 
--- a/dom/bindings/Codegen.py
+++ b/dom/bindings/Codegen.py
@@ -8090,17 +8090,43 @@ class CGPerSignatureCall(CGThing):
                 # a serious problem.  In common cases it's just an argument and
                 # we're MOZ_CAN_RUN_SCRIPT, but in some cases it's on the stack
                 # and being kept alive via references from JS.
                 object="MOZ_KnownLive(self)",
                 argsPost=argsPost, resultVar=resultVar))
 
         if useCounterName:
             # Generate a telemetry call for when [UseCounter] is used.
-            code = "SetUseCounter(obj, eUseCounter_%s);\n" % useCounterName
+            windowCode = fill(
+                """
+                SetUseCounter(obj, eUseCounter_${useCounterName});
+                """,
+                useCounterName = useCounterName)
+            workerCode = fill(
+                """
+                SetUseCounter(UseCounterWorker::${useCounterName});
+                """,
+                useCounterName = useCounterName)
+            code = ""
+            if idlNode.isExposedInWindow() and idlNode.isExposedInAnyWorker():
+                code += fill(
+                    """
+                    if (NS_IsMainThread()) {
+                      ${windowCode}
+                    } else {
+                      ${workerCode}
+                    }
+                    """,
+                    windowCode=windowCode,
+                    workerCode=workerCode)
+            elif idlNode.isExposedInWindow():
+                code += windowCode
+            elif idlNode.isExposedInAnyWorker():
+                code += workerCode
+
             cgThings.append(CGGeneric(code))
 
         self.cgRoot = CGList(cgThings)
 
     def getArguments(self):
         return [(a, "arg" + str(i)) for i, a in enumerate(self.arguments)]
 
     def isFallible(self):