[dawn][emscripten] Updates callbacks to use StringView.

- This change needs
  https://dawn-review.googlesource.com/c/dawn/+/209098 to
  land before it should be landed.

Bug: 373466748
Change-Id: I6d7f453106bb55d1485f7f34928d3f73ef57f4ef
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/210777
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Loko Kung <lokokung@google.com>
diff --git a/third_party/emdawnwebgpu/webgpu.cpp b/third_party/emdawnwebgpu/webgpu.cpp
index 106ec1f..e473877 100644
--- a/third_party/emdawnwebgpu/webgpu.cpp
+++ b/third_party/emdawnwebgpu/webgpu.cpp
@@ -17,7 +17,6 @@
 #include <map>
 #include <memory>
 #include <mutex>
-#include <optional>
 #include <set>
 #include <tuple>
 #include <unordered_map>
@@ -259,6 +258,11 @@
   return object.Detach();
 }
 
+// StringView utilities.
+WGPUStringView ToOutputStringView(const std::string& s) {
+  return {s.data(), s.size()};
+}
+
 // clang-format off
 // X Macro to help generate boilerplate code for all refcounted object types.
 #define WGPU_OBJECTS(X) \
@@ -814,7 +818,7 @@
                 mStatus == WGPUCreatePipelineAsyncStatus_Success
                     ? ReturnToAPI(std::move(mPipeline))
                     : nullptr,
-                mMessage ? mMessage->c_str() : nullptr, mUserdata1, mUserdata2);
+                ToOutputStringView(mMessage), mUserdata1, mUserdata2);
     }
   }
 
@@ -826,7 +830,7 @@
 
   WGPUCreatePipelineAsyncStatus mStatus = WGPUCreatePipelineAsyncStatus_Success;
   Ref<Pipeline> mPipeline;
-  std::optional<std::string> mMessage = std::nullopt;
+  std::string mMessage;
 };
 using CreateComputePipelineEvent =
     CreatePipelineEventBase<WGPUComputePipeline,
@@ -870,8 +874,8 @@
       WGPUDevice device = mReason != WGPUDeviceLostReason_FailedCreation
                               ? mDevice.Get()
                               : nullptr;
-      mCallback(&device, mReason, mMessage ? mMessage->c_str() : nullptr,
-                mUserdata1, mUserdata2);
+      mCallback(&device, mReason, ToOutputStringView(mMessage), mUserdata1,
+                mUserdata2);
     }
   }
 
@@ -883,7 +887,7 @@
   Ref<WGPUDevice> mDevice;
 
   WGPUDeviceLostReason mReason;
-  std::optional<std::string> mMessage;
+  std::string mMessage;
 };
 
 class PopErrorScopeEvent final : public TrackedEvent {
@@ -916,8 +920,8 @@
       mMessage = "A valid external Instance reference no longer exists.";
     }
     if (mCallback) {
-      mCallback(mStatus, mErrorType, mMessage ? mMessage->c_str() : nullptr,
-                mUserdata1, mUserdata2);
+      mCallback(mStatus, mErrorType, ToOutputStringView(mMessage), mUserdata1,
+                mUserdata2);
     }
   }
 
@@ -928,7 +932,7 @@
 
   WGPUPopErrorScopeStatus mStatus = WGPUPopErrorScopeStatus_Success;
   WGPUErrorType mErrorType = WGPUErrorType_Unknown;
-  std::optional<std::string> mMessage;
+  std::string mMessage;
 };
 
 class MapAsyncEvent final : public TrackedEvent {
@@ -975,8 +979,7 @@
     }
 
     if (mCallback) {
-      mCallback(mStatus, mMessage ? mMessage->c_str() : nullptr, mUserdata1,
-                mUserdata2);
+      mCallback(mStatus, ToOutputStringView(mMessage), mUserdata1, mUserdata2);
     }
   }
 
@@ -987,7 +990,7 @@
 
   Ref<WGPUBuffer> mBuffer;
   WGPUMapAsyncStatus mStatus = WGPUMapAsyncStatus_Success;
-  std::optional<std::string> mMessage = std::nullopt;
+  std::string mMessage;
 };
 
 class RequestAdapterEvent final : public TrackedEvent {
@@ -1023,7 +1026,7 @@
                 mStatus == WGPURequestAdapterStatus_Success
                     ? ReturnToAPI(std::move(mAdapter))
                     : nullptr,
-                mMessage ? mMessage->c_str() : nullptr, mUserdata1, mUserdata2);
+                ToOutputStringView(mMessage), mUserdata1, mUserdata2);
     }
   }
 
@@ -1034,7 +1037,7 @@
 
   WGPURequestAdapterStatus mStatus;
   Ref<WGPUAdapter> mAdapter;
-  std::optional<std::string> mMessage = std::nullopt;
+  std::string mMessage;
 };
 
 class RequestDeviceEvent final : public TrackedEvent {
@@ -1070,7 +1073,7 @@
                 mStatus == WGPURequestDeviceStatus_Success
                     ? ReturnToAPI(std::move(mDevice))
                     : nullptr,
-                mMessage ? mMessage->c_str() : nullptr, mUserdata1, mUserdata2);
+                ToOutputStringView(mMessage), mUserdata1, mUserdata2);
     }
   }
 
@@ -1081,7 +1084,7 @@
 
   WGPURequestDeviceStatus mStatus;
   Ref<WGPUDevice> mDevice;
-  std::optional<std::string> mMessage = std::nullopt;
+  std::string mMessage;
 };
 
 class WorkDoneEvent final : public TrackedEvent {
@@ -1379,7 +1382,9 @@
   if (mUncapturedErrorCallbackInfo.callback) {
     WGPUDeviceImpl* device = this;
     mUncapturedErrorCallbackInfo.callback(
-        &device, type, message, mUncapturedErrorCallbackInfo.userdata1,
+        &device, type,
+        WGPUStringView{.data = message, .length = std::strlen(message)},
+        mUncapturedErrorCallbackInfo.userdata1,
         mUncapturedErrorCallbackInfo.userdata2);
   }
 }
@@ -1513,7 +1518,7 @@
   WGPURequestDeviceCallbackInfo2 callbackInfo = {};
   callbackInfo.mode = WGPUCallbackMode_AllowSpontaneous;
   callbackInfo.callback = [](WGPURequestDeviceStatus status, WGPUDevice device,
-                             char const* message, void* callback,
+                             WGPUStringView message, void* callback,
                              void* userdata) {
     auto cb = reinterpret_cast<WGPURequestDeviceCallback>(callback);
     cb(status, device, message, userdata);
@@ -1631,8 +1636,9 @@
   WGPUCreateComputePipelineAsyncCallbackInfo2 callbackInfo = {};
   callbackInfo.mode = WGPUCallbackMode_AllowSpontaneous;
   callbackInfo.callback = [](WGPUCreatePipelineAsyncStatus status,
-                             WGPUComputePipeline pipeline, char const* message,
-                             void* callback, void* userdata) {
+                             WGPUComputePipeline pipeline,
+                             WGPUStringView message, void* callback,
+                             void* userdata) {
     auto cb =
         reinterpret_cast<WGPUCreateComputePipelineAsyncCallback>(callback);
     cb(status, pipeline, message, userdata);
@@ -1665,8 +1671,9 @@
   WGPUCreateRenderPipelineAsyncCallbackInfo2 callbackInfo = {};
   callbackInfo.mode = WGPUCallbackMode_AllowSpontaneous;
   callbackInfo.callback = [](WGPUCreatePipelineAsyncStatus status,
-                             WGPURenderPipeline pipeline, char const* message,
-                             void* callback, void* userdata) {
+                             WGPURenderPipeline pipeline,
+                             WGPUStringView message, void* callback,
+                             void* userdata) {
     auto cb = reinterpret_cast<WGPUCreateRenderPipelineAsyncCallback>(callback);
     cb(status, pipeline, message, userdata);
   };
@@ -1731,7 +1738,7 @@
   WGPURequestAdapterCallbackInfo2 callbackInfo = {};
   callbackInfo.mode = WGPUCallbackMode_AllowSpontaneous;
   callbackInfo.callback = [](WGPURequestAdapterStatus status,
-                             WGPUAdapter adapter, char const* message,
+                             WGPUAdapter adapter, WGPUStringView message,
                              void* callback, void* userdata) {
     auto cb = reinterpret_cast<WGPURequestAdapterCallback>(callback);
     cb(status, adapter, message, userdata);