Reland "Replace String with WebMessagePayload for AndroidX WebMessage API"

This is a reland of commit bc7514a7cf7b67ffc3fb0698310a23246df5a7c5

- Keep WebContents#postMessageToMainFrame(String, ...), it's used
by downstream.

Original change's description:
> Replace String with WebMessagePayload for AndroidX WebMessage API
>
> Design doc: https://docs.google.com/document/d/1nsQnwwY43OuA4PsSaMioiKA0c_M1eV1XX2q15YFA7cI/edit?usp=sharing
>
> This CL did not add new features.
> - Add a new boundary type WebMessagePayload to replace String
> WebMessage.getData().
> - Add new data class MessagePayload in content/, and replace existing
> PostMessage API with the new type.
> - A followup change will add ArrayBuffer support in
> WebViewCompat.postMessageToMainFrame and
> WebChannelCompat.postMessage.
>
> How to test: run tests in existing AndroidX Webkit.
>
> Bug: 1023334
> Change-Id: Ib5b35b974bee597a6efada75503c5288f7058c50
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3583520
> Reviewed-by: Bo Liu <boliu@chromium.org>
> Reviewed-by: Richard Coles <torne@chromium.org>
> Reviewed-by: Peter Conn <peconn@chromium.org>
> Commit-Queue: Linyue He <linyhe@microsoft.com>
> Cr-Commit-Position: refs/heads/main@{#995852}

Bug: 1023334
Change-Id: I9e2fa991d04e575add848d908a5d097a35a990b2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3607795
Reviewed-by: Bo Liu <boliu@chromium.org>
Commit-Queue: Linyue He <linyhe@microsoft.com>
Reviewed-by: Richard Coles <torne@chromium.org>
Reviewed-by: Peter Conn <peconn@chromium.org>
Cr-Commit-Position: refs/heads/main@{#997137}
NOKEYCHECK=True
GitOrigin-RevId: 173c29edc14315e542c0c497d0d5bee5c718a289
diff --git a/java/src/com/android/webview/chromium/SharedWebViewChromium.java b/java/src/com/android/webview/chromium/SharedWebViewChromium.java
index 9f0a2a0..64fc057 100644
--- a/java/src/com/android/webview/chromium/SharedWebViewChromium.java
+++ b/java/src/com/android/webview/chromium/SharedWebViewChromium.java
@@ -13,6 +13,7 @@
 import org.chromium.android_webview.WebMessageListener;
 import org.chromium.android_webview.WebViewChromiumRunQueue;
 import org.chromium.base.ThreadUtils;
+import org.chromium.content_public.browser.MessagePayload;
 import org.chromium.content_public.browser.MessagePort;
 
 import java.util.concurrent.Callable;
@@ -105,18 +106,18 @@
         return mAwContents.createMessageChannel();
     }
 
-    public void postMessageToMainFrame(
-            final String message, final String targetOrigin, final MessagePort[] sentPorts) {
+    public void postMessageToMainFrame(final MessagePayload messagePayload,
+            final String targetOrigin, final MessagePort[] sentPorts) {
         if (checkNeedsPost()) {
             mRunQueue.addTask(new Runnable() {
                 @Override
                 public void run() {
-                    postMessageToMainFrame(message, targetOrigin, sentPorts);
+                    postMessageToMainFrame(messagePayload, targetOrigin, sentPorts);
                 }
             });
             return;
         }
-        mAwContents.postMessageToMainFrame(message, targetOrigin, sentPorts);
+        mAwContents.postMessageToMainFrame(messagePayload, targetOrigin, sentPorts);
     }
 
     public void addWebMessageListener(final String jsObjectName, final String[] allowedOriginRules,
diff --git a/java/src/com/android/webview/chromium/WebMessagePortAdapter.java b/java/src/com/android/webview/chromium/WebMessagePortAdapter.java
index 84dd3ea..065ebed 100644
--- a/java/src/com/android/webview/chromium/WebMessagePortAdapter.java
+++ b/java/src/com/android/webview/chromium/WebMessagePortAdapter.java
@@ -8,6 +8,7 @@
 import android.webkit.WebMessage;
 import android.webkit.WebMessagePort;
 
+import org.chromium.content_public.browser.MessagePayload;
 import org.chromium.content_public.browser.MessagePort;
 
 /**
@@ -23,7 +24,8 @@
 
     @Override
     public void postMessage(WebMessage message) {
-        mPort.postMessage(message.getData(), toMessagePorts(message.getPorts()));
+        mPort.postMessage(
+                new MessagePayload(message.getData()), toMessagePorts(message.getPorts()));
     }
 
     @Override
@@ -40,9 +42,9 @@
     public void setWebMessageCallback(final WebMessageCallback callback, final Handler handler) {
         mPort.setMessageCallback(new MessagePort.MessageCallback() {
             @Override
-            public void onMessage(String message, MessagePort[] ports) {
+            public void onMessage(MessagePayload messagePayload, MessagePort[] ports) {
                 callback.onMessage(WebMessagePortAdapter.this,
-                        new WebMessage(message, fromMessagePorts(ports)));
+                        new WebMessage(messagePayload.getAsString(), fromMessagePorts(ports)));
             }
         }, handler);
     }
diff --git a/java/src/com/android/webview/chromium/WebViewChromium.java b/java/src/com/android/webview/chromium/WebViewChromium.java
index 88b4629..42f4654 100644
--- a/java/src/com/android/webview/chromium/WebViewChromium.java
+++ b/java/src/com/android/webview/chromium/WebViewChromium.java
@@ -69,6 +69,7 @@
 import org.chromium.components.content_capture.ContentCaptureFeatures;
 import org.chromium.components.content_capture.OnscreenContentProvider;
 import org.chromium.components.embedder_support.application.ClassLoaderContextWrapperFactory;
+import org.chromium.content_public.browser.MessagePayload;
 import org.chromium.content_public.browser.NavigationHistory;
 import org.chromium.content_public.browser.SmartClipProvider;
 import org.chromium.url.GURL;
@@ -1624,8 +1625,10 @@
     @Override
     public void postMessageToMainFrame(final WebMessage message, final Uri targetOrigin) {
         recordWebViewApiCall(ApiCall.POST_MESSAGE_TO_MAIN_FRAME);
-        mSharedWebViewChromium.postMessageToMainFrame(message.getData(), targetOrigin.toString(),
-                WebMessagePortAdapter.toMessagePorts(message.getPorts()));
+        // Create MessagePayload from AOSP WebMessage, MessagePayload is not directly supported by
+        // AOSP.
+        mSharedWebViewChromium.postMessageToMainFrame(new MessagePayload(message.getData()),
+                targetOrigin.toString(), WebMessagePortAdapter.toMessagePorts(message.getPorts()));
     }
 
     @Override