public class

WebMessageAdapter

extends java.lang.Object

implements org.chromium.support_lib_boundary.WebMessageBoundaryInterface

 java.lang.Object

↳androidx.webkit.internal.WebMessageAdapter

Gradle dependencies

compile group: 'androidx.webkit', name: 'webkit', version: '1.12.0-rc01'

  • groupId: androidx.webkit
  • artifactId: webkit
  • version: 1.12.0-rc01

Artifact androidx.webkit:webkit:1.12.0-rc01 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.webkit:webkit com.android.support:webkit

Overview

Adapter between WebMessageCompat and org.chromium.support_lib_boundary.WebMessageBoundaryInterface. This class is used to pass a PostMessage from the app to Chromium.

Summary

Constructors
publicWebMessageAdapter(WebMessageCompat webMessage)

Methods
public java.lang.StringgetData()

public java.lang.reflect.InvocationHandlergetMessagePayload()

public java.lang.reflect.InvocationHandlergetPorts()

public java.lang.StringgetSupportedFeatures()

public static booleanisMessagePayloadTypeSupportedByWebView(int type)

Utility method to check if the WebMessageCompat payload type is supported by WebView.

public static WebMessageCompatwebMessageCompatFromBoundaryInterface(org.chromium.support_lib_boundary.WebMessageBoundaryInterface boundaryInterface)

Utility method used to convert PostMessages from the Chromium side to WebMessageCompat objects - a class apps recognize.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public WebMessageAdapter(WebMessageCompat webMessage)

Methods

public java.lang.String getData()

Deprecated: Keep backwards compatibility with old version of WebView. This method is equivalent to getAsString.

public java.lang.reflect.InvocationHandler getMessagePayload()

public java.lang.reflect.InvocationHandler getPorts()

public java.lang.String getSupportedFeatures()

public static boolean isMessagePayloadTypeSupportedByWebView(int type)

Utility method to check if the WebMessageCompat payload type is supported by WebView.

public static WebMessageCompat webMessageCompatFromBoundaryInterface(org.chromium.support_lib_boundary.WebMessageBoundaryInterface boundaryInterface)

Utility method used to convert PostMessages from the Chromium side to WebMessageCompat objects - a class apps recognize. Return null when the WebMessageCompat payload type is not supported by AndroidX now.

Source

/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.webkit.internal;

import static org.chromium.support_lib_boundary.WebMessagePayloadBoundaryInterface.WebMessagePayloadType;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.webkit.WebMessageCompat;
import androidx.webkit.WebMessagePortCompat;

import org.chromium.support_lib_boundary.WebMessageBoundaryInterface;
import org.chromium.support_lib_boundary.WebMessagePayloadBoundaryInterface;
import org.chromium.support_lib_boundary.util.BoundaryInterfaceReflectionUtil;
import org.chromium.support_lib_boundary.util.Features;

import java.lang.reflect.InvocationHandler;
import java.util.Objects;

/**
 * Adapter between {@link WebMessageCompat} and
 * {@link org.chromium.support_lib_boundary.WebMessageBoundaryInterface}.
 * This class is used to pass a PostMessage from the app to Chromium.
 */
public class WebMessageAdapter implements WebMessageBoundaryInterface {
    private WebMessageCompat mWebMessageCompat;

    private static final String[] sFeatures = {Features.WEB_MESSAGE_ARRAY_BUFFER};

    public WebMessageAdapter(@NonNull WebMessageCompat webMessage) {
        this.mWebMessageCompat = webMessage;
    }

    /**
     * @deprecated  Keep backwards compatibility with old version of WebView. This method is
     * equivalent to {@link WebMessagePayloadBoundaryInterface#getAsString()}.
     */
    @Deprecated
    @Override
    @Nullable
    public String getData() {
        return mWebMessageCompat.getData();
    }

    @Override
    @Nullable
    public InvocationHandler getMessagePayload() {
        final WebMessagePayloadAdapter adapter;
        switch (mWebMessageCompat.getType()) {
            case WebMessageCompat.TYPE_STRING:
                adapter = new WebMessagePayloadAdapter(mWebMessageCompat.getData());
                break;
            case WebMessageCompat.TYPE_ARRAY_BUFFER:
                adapter = new WebMessagePayloadAdapter(
                        Objects.requireNonNull(mWebMessageCompat.getArrayBuffer()));
                break;
            default:
                throw new IllegalStateException(
                        "Unknown web message payload type: " + mWebMessageCompat.getType());
        }
        return BoundaryInterfaceReflectionUtil.createInvocationHandlerFor(adapter);
    }

    @Override
    @Nullable
    public InvocationHandler[] getPorts() {
        WebMessagePortCompat[] ports = mWebMessageCompat.getPorts();
        if (ports == null) return null;

        InvocationHandler[] invocationHandlers = new InvocationHandler[ports.length];
        for (int n = 0; n < ports.length; n++) {
            invocationHandlers[n] = ports[n].getInvocationHandler();
        }
        return invocationHandlers;
    }

    @Override
    @NonNull
    public String[] getSupportedFeatures() {
        // getData() and getPorts() are not covered by feature flags.
        return sFeatures;
    }

    /**
     * Utility method to check if the WebMessageCompat payload type is supported by WebView.
     */
    public static boolean isMessagePayloadTypeSupportedByWebView(
            @WebMessageCompat.Type final int type) {
        return type == WebMessageCompat.TYPE_STRING
                || (type == WebMessageCompat.TYPE_ARRAY_BUFFER
                && WebViewFeatureInternal.WEB_MESSAGE_ARRAY_BUFFER.isSupportedByWebView());
    }

    // ====================================================================================
    // Methods related to converting a WebMessageBoundaryInterface into a WebMessageCompat.
    // ====================================================================================

    /**
     * Utility method used to convert PostMessages from the Chromium side to
     * {@link WebMessageCompat} objects - a class apps recognize.
     * Return null when the WebMessageCompat payload type is not supported by AndroidX now.
     */
    @SuppressWarnings("deprecation")
    @Nullable
    public static WebMessageCompat webMessageCompatFromBoundaryInterface(
            @NonNull WebMessageBoundaryInterface boundaryInterface) {
        final WebMessagePortCompat[] ports = toWebMessagePortCompats(
                boundaryInterface.getPorts());
        if (WebViewFeatureInternal.WEB_MESSAGE_ARRAY_BUFFER.isSupportedByWebView()) {
            WebMessagePayloadBoundaryInterface payloadInterface =
                    BoundaryInterfaceReflectionUtil.castToSuppLibClass(
                            WebMessagePayloadBoundaryInterface.class,
                            boundaryInterface.getMessagePayload());
            final @WebMessagePayloadType int type = payloadInterface.getType();
            switch (type) {
                case WebMessagePayloadType.TYPE_STRING:
                    return new WebMessageCompat(payloadInterface.getAsString(), ports);
                case WebMessagePayloadType.TYPE_ARRAY_BUFFER:
                    return new WebMessageCompat(payloadInterface.getAsArrayBuffer(), ports);
            }
            // Unsupported message type.
            return null;
        }
        // MessagePayload not supported by WebView, fallback.
        return new WebMessageCompat(boundaryInterface.getData(), ports);
    }

    @NonNull
    private static WebMessagePortCompat[] toWebMessagePortCompats(InvocationHandler[] ports) {
        WebMessagePortCompat[] compatPorts = new WebMessagePortCompat[ports.length];
        for (int n = 0; n < ports.length; n++) {
            compatPorts[n] = new WebMessagePortImpl(ports[n]);
        }
        return compatPorts;
    }
}