1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.webkit;
18 
19 import androidx.annotation.RequiresFeature;
20 import androidx.annotation.RestrictTo;
21 import androidx.annotation.UiThread;
22 
23 import org.jspecify.annotations.NonNull;
24 
25 /**
26  * This class represents the JavaScript object injected by {@link
27  * WebViewCompat#addWebMessageListener(android.webkit.WebView, String, java.util.Set,
28  * WebViewCompat.WebMessageListener) WebViewCompat#addWebMessageListener}. An instance will be given
29  * by {@link WebViewCompat.WebMessageListener#onPostMessage(android.webkit.WebView,
30  * WebMessageCompat, android.net.Uri, boolean, JavaScriptReplyProxy)
31  * WebMessageListener#onPostMessage}. The app can use {@link #postMessage(String)} to talk to the
32  * JavaScript context.
33  *
34  * <p>
35  * There is a 1:1 relationship between this object and the JavaScript object in a frame.
36  *
37  * @see WebViewCompat#addWebMessageListener(android.webkit.WebView, String, java.util.Set,
38  * WebViewCompat.WebMessageListener).
39  */
40 // UI thread not currently enforced, but required
41 @UiThread
42 public abstract class JavaScriptReplyProxy {
43     /**
44      * Post a String message to the injected JavaScript object which sent this {@link
45      * JavaScriptReplyProxy}.
46      *
47      * @param message The String data to send to the JavaScript context.
48      */
49     @RequiresFeature(name = WebViewFeature.WEB_MESSAGE_LISTENER,
50             enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
postMessage(@onNull String message)51     public abstract void postMessage(@NonNull String message);
52 
53     /**
54      * Post a ArrayBuffer message to the injected JavaScript object which sent this
55      * {@link JavaScriptReplyProxy}. Be aware that large byte buffers can lead to out-of-memory
56      * crashes on low-end devices.
57      *
58      * @param arrayBuffer The ArrayBuffer to send to the JavaScript context. An empty ArrayBuffer
59      *                    is supported.
60      */
61     @RequiresFeature(name = WebViewFeature.WEB_MESSAGE_ARRAY_BUFFER,
62             enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
postMessage(byte @NonNull [] arrayBuffer)63     public abstract void postMessage(byte @NonNull [] arrayBuffer);
64 
65     /**
66      * This class cannot be created by applications.
67      */
68     @RestrictTo(RestrictTo.Scope.LIBRARY)
JavaScriptReplyProxy()69     public JavaScriptReplyProxy() {}
70 }
71