[wgpu-headers] Adds CallbackMode::Undefined and some validations for it.

- Note that the plan is to strictly enforce explicit setting of the
  callback mode on the upcoming APIs. Existing APIs, for the most part
  have defaults and should continue working as is until they are
  completely removed and deprecated.

Bug: 42241407
Change-Id: I4a6839894212ff40d016f617b0fe6f993d31df12
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/186581
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Loko Kung <lokokung@google.com>
diff --git a/src/dawn/common/BUILD.gn b/src/dawn/common/BUILD.gn
index e9c4c28..189030f 100644
--- a/src/dawn/common/BUILD.gn
+++ b/src/dawn/common/BUILD.gn
@@ -254,6 +254,7 @@
       "DynamicLib.cpp",
       "DynamicLib.h",
       "Enumerator.h",
+      "FutureUtils.cpp",
       "FutureUtils.h",
       "GPUInfo.cpp",
       "GPUInfo.h",
diff --git a/src/dawn/common/CMakeLists.txt b/src/dawn/common/CMakeLists.txt
index e0f8ca1..ce3bb2b 100644
--- a/src/dawn/common/CMakeLists.txt
+++ b/src/dawn/common/CMakeLists.txt
@@ -61,6 +61,7 @@
     "DynamicLib.cpp"
     "DynamicLib.h"
     "Enumerator.h"
+    "FutureUtils.cpp"
     "FutureUtils.h"
     "GPUInfo.cpp"
     "GPUInfo.h"
diff --git a/src/dawn/common/FutureUtils.cpp b/src/dawn/common/FutureUtils.cpp
new file mode 100644
index 0000000..59b88b4
--- /dev/null
+++ b/src/dawn/common/FutureUtils.cpp
@@ -0,0 +1,43 @@
+// Copyright 2024 The Dawn & Tint Authors
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its
+//    contributors may be used to endorse or promote products derived from
+//    this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "dawn/common/FutureUtils.h"
+
+namespace dawn {
+
+bool ValidateCallbackMode(WGPUCallbackMode mode) {
+    switch (mode) {
+        case WGPUCallbackMode_WaitAnyOnly:
+        case WGPUCallbackMode_AllowProcessEvents:
+        case WGPUCallbackMode_AllowSpontaneous:
+            return true;
+        default:
+            return false;
+    }
+}
+
+}  // namespace dawn
diff --git a/src/dawn/common/FutureUtils.h b/src/dawn/common/FutureUtils.h
index e62eb75..b25224c 100644
--- a/src/dawn/common/FutureUtils.h
+++ b/src/dawn/common/FutureUtils.h
@@ -47,6 +47,8 @@
     Shutdown,
 };
 
+bool ValidateCallbackMode(WGPUCallbackMode mode);
+
 }  // namespace dawn
 
 #endif  // SRC_DAWN_COMMON_FUTUREUTILS_H_
diff --git a/src/dawn/dawn.json b/src/dawn/dawn.json
index 3d3b23a..57198de 100644
--- a/src/dawn/dawn.json
+++ b/src/dawn/dawn.json
@@ -1576,7 +1576,7 @@
         "category": "structure",
         "extensible": "in",
         "members": [
-            {"name": "mode", "type": "callback mode"},
+            {"name": "mode", "type": "callback mode", "default": "wait any only"},
             {"name": "callback", "type": "pop error scope callback"},
             {"name": "old callback", "type": "error callback", "_comment": "TODO(crbug.com/dawn/2021) Deprecate this field once we have moved callers to use the new callback signature."},
             {"name": "userdata", "type": "void *", "default": "nullptr"}
@@ -2415,11 +2415,10 @@
     "callback mode": {
         "category": "enum",
         "emscripten_no_enum_table": true,
-        "_comment": "TODO(crbug.com/dawn/2458): Should this be renumbered to reserve 0?",
         "values": [
-            {"value": 0, "name": "wait any only"},
-            {"value": 1, "name": "allow process events"},
-            {"value": 2, "name": "allow spontaneous"}
+            {"value": 1, "name": "wait any only"},
+            {"value": 2, "name": "allow process events"},
+            {"value": 3, "name": "allow spontaneous"}
         ]
     },
     "future": {
diff --git a/src/dawn/native/EventManager.cpp b/src/dawn/native/EventManager.cpp
index 62cfb50..7f49ee3 100644
--- a/src/dawn/native/EventManager.cpp
+++ b/src/dawn/native/EventManager.cpp
@@ -34,6 +34,7 @@
 
 #include "dawn/common/Assert.h"
 #include "dawn/common/FutureUtils.h"
+#include "dawn/common/Log.h"
 #include "dawn/native/ChainUtils.h"
 #include "dawn/native/Device.h"
 #include "dawn/native/IntegerTypes.h"
@@ -344,6 +345,12 @@
 }
 
 FutureID EventManager::TrackEvent(Ref<TrackedEvent>&& event) {
+    if (!ValidateCallbackMode(ToAPI(event->mCallbackMode))) {
+        // TODO: crbug.com/42241407 - Update to use instance logging callback.
+        dawn::ErrorLog() << "Invalid callback mode: " << ToAPI(event->mCallbackMode);
+        return kNullFutureID;
+    }
+
     FutureID futureID = mNextFutureID++;
     event->mFutureID = futureID;
 
diff --git a/src/dawn/tests/unittests/wire/WireTest.cpp b/src/dawn/tests/unittests/wire/WireTest.cpp
index 327ec0f..730ba13 100644
--- a/src/dawn/tests/unittests/wire/WireTest.cpp
+++ b/src/dawn/tests/unittests/wire/WireTest.cpp
@@ -130,6 +130,7 @@
     // Create the device for testing.
     apiDevice = api.GetNewDevice();
     WGPUDeviceDescriptor deviceDesc = {};
+    deviceDesc.deviceLostCallbackInfo.mode = WGPUCallbackMode_WaitAnyOnly;
     deviceDesc.deviceLostCallbackInfo.callback = deviceLostCallback.Callback();
     deviceDesc.deviceLostCallbackInfo.userdata = deviceLostCallback.MakeUserdata(this);
     EXPECT_CALL(deviceLostCallback, Call).Times(AtMost(1));
diff --git a/src/dawn/wire/client/EventManager.cpp b/src/dawn/wire/client/EventManager.cpp
index f1f0f15..a1e52a3 100644
--- a/src/dawn/wire/client/EventManager.cpp
+++ b/src/dawn/wire/client/EventManager.cpp
@@ -32,6 +32,7 @@
 
 #include "dawn/wire/client/EventManager.h"
 
+#include "dawn/common/Log.h"
 #include "dawn/wire/client/Client.h"
 
 namespace dawn::wire::client {
@@ -70,6 +71,12 @@
 }
 
 std::pair<FutureID, bool> EventManager::TrackEvent(std::unique_ptr<TrackedEvent> event) {
+    if (!ValidateCallbackMode(event->GetCallbackMode())) {
+        // TODO: crbug.com/42241407 - Update to use instance logging callback.
+        dawn::ErrorLog() << "Invalid callback mode: " << event->GetCallbackMode();
+        return {kNullFutureID, false};
+    }
+
     FutureID futureID = mNextFutureID++;
 
     switch (mState) {