public class

WebMessagePortImpl

extends WebMessagePortCompat

 java.lang.Object

androidx.webkit.WebMessagePortCompat

↳androidx.webkit.internal.WebMessagePortImpl

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

Implementation of WebMessagePortCompat. This class uses either the framework, the WebView APK, or both, to implement WebMessagePortCompat functionality.

Summary

Constructors
publicWebMessagePortImpl(WebMessagePort frameworksImpl)

Methods
public abstract voidclose()

Close the message port and free any resources associated with it.

public static WebMessagecompatToFrameworkMessage(WebMessageCompat message)

Convert a WebMessageCompat into the corresponding framework class .

public static WebMessagePortcompatToPorts(WebMessagePortCompat compatPorts[])

Convert an array of WebMessagePortCompat objects into an array containing objects of the corresponding framework class .

public static WebMessageCompatframeworkMessageToCompat(WebMessage message)

Convert a into the corresponding support library class WebMessageCompat.

public abstract WebMessagePortgetFrameworkPort()

Internal getter returning the private implementing this class.

public abstract java.lang.reflect.InvocationHandlergetInvocationHandler()

Internal getter returning the private java.lang.reflect.InvocationHandler implementing this class.

public static WebMessagePortCompatportsToCompat(WebMessagePort ports[])

Convert an array of objects into an array containing objects of the corresponding support library class WebMessagePortCompat.

public abstract voidpostMessage(WebMessageCompat message)

Post a WebMessage to the entangled port.

public abstract voidsetWebMessageCallback(Handler handler, WebMessagePortCompat.WebMessageCallbackCompat callback)

Sets a callback to receive message events on the handler that is provided by the application.

public abstract voidsetWebMessageCallback(WebMessagePortCompat.WebMessageCallbackCompat callback)

Sets a callback to receive message events on the main thread.

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

Constructors

public WebMessagePortImpl(WebMessagePort frameworksImpl)

Methods

public abstract void postMessage(WebMessageCompat message)

Post a WebMessage to the entangled port.

This method should only be called if WebViewFeature.isFeatureSupported(String) returns true for WebViewFeature.WEB_MESSAGE_PORT_POST_MESSAGE.

When posting a WebMessageCompat with type WebMessageCompat.TYPE_ARRAY_BUFFER, this method should check if WebViewFeature.isFeatureSupported(String) returns true for WebViewFeature.WEB_MESSAGE_ARRAY_BUFFER. Example:

 if (message.getType() == WebMessageCompat.TYPE_ARRAY_BUFFER) {
     if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_ARRAY_BUFFER) {
         // ArrayBuffer message is supported, send message here.
         port.postMessage(message);
     }
 }
 

Parameters:

message: the message from Java to JS.

public abstract void close()

Close the message port and free any resources associated with it.

This method should only be called if WebViewFeature.isFeatureSupported(String) returns true for WebViewFeature.WEB_MESSAGE_PORT_CLOSE.

public abstract void setWebMessageCallback(WebMessagePortCompat.WebMessageCallbackCompat callback)

Sets a callback to receive message events on the main thread.

This method should only be called if WebViewFeature.isFeatureSupported(String) returns true for WebViewFeature.WEB_MESSAGE_PORT_SET_MESSAGE_CALLBACK.

Parameters:

callback: the message callback.

public abstract void setWebMessageCallback(Handler handler, WebMessagePortCompat.WebMessageCallbackCompat callback)

Sets a callback to receive message events on the handler that is provided by the application. If the handler is null the message events are received on the main thread.

This method should only be called if WebViewFeature.isFeatureSupported(String) returns true for WebViewFeature.WEB_MESSAGE_PORT_SET_MESSAGE_CALLBACK.

Parameters:

handler: the handler to receive the message events.
callback: the message callback.

public abstract WebMessagePort getFrameworkPort()

Internal getter returning the private implementing this class. This is only available on devices with an Android versions supporting WebMessagePorts.

public abstract java.lang.reflect.InvocationHandler getInvocationHandler()

Internal getter returning the private java.lang.reflect.InvocationHandler implementing this class. This is only available on devices where the support library glue in the WebView APK supports WebMessagePortCompat.

public static WebMessagePortCompat portsToCompat(WebMessagePort ports[])

Convert an array of objects into an array containing objects of the corresponding support library class WebMessagePortCompat.

public static WebMessagePort compatToPorts(WebMessagePortCompat compatPorts[])

Convert an array of WebMessagePortCompat objects into an array containing objects of the corresponding framework class .

public static WebMessage compatToFrameworkMessage(WebMessageCompat message)

Convert a WebMessageCompat into the corresponding framework class .

public static WebMessageCompat frameworkMessageToCompat(WebMessage message)

Convert a into the corresponding support library class WebMessageCompat.

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 android.os.Handler;
import android.webkit.WebMessage;
import android.webkit.WebMessagePort;

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

import org.chromium.support_lib_boundary.WebMessagePortBoundaryInterface;
import org.chromium.support_lib_boundary.util.BoundaryInterfaceReflectionUtil;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * Implementation of {@link WebMessagePortCompat}.
 * This class uses either the framework, the WebView APK, or both, to implement
 * {@link WebMessagePortCompat} functionality.
 */
public class WebMessagePortImpl extends WebMessagePortCompat {
    private WebMessagePort mFrameworksImpl;
    private WebMessagePortBoundaryInterface mBoundaryInterface;

    public WebMessagePortImpl(@NonNull WebMessagePort frameworksImpl) {
        mFrameworksImpl = frameworksImpl;
    }

    public WebMessagePortImpl(@NonNull InvocationHandler invocationHandler) {
        mBoundaryInterface = BoundaryInterfaceReflectionUtil.castToSuppLibClass(
                WebMessagePortBoundaryInterface.class, invocationHandler);
    }

    @RequiresApi(23)
    private WebMessagePort getFrameworksImpl() {
        if (mFrameworksImpl == null) {
            mFrameworksImpl = WebViewGlueCommunicator.getCompatConverter().convertWebMessagePort(
                    Proxy.getInvocationHandler(mBoundaryInterface));
        }
        return mFrameworksImpl;
    }

    private WebMessagePortBoundaryInterface getBoundaryInterface() {
        if (mBoundaryInterface == null) {
            mBoundaryInterface = BoundaryInterfaceReflectionUtil.castToSuppLibClass(
                    WebMessagePortBoundaryInterface.class,
                    WebViewGlueCommunicator.getCompatConverter().convertWebMessagePort(
                            mFrameworksImpl));
        }
        return mBoundaryInterface;
    }

    @Override
    public void postMessage(@NonNull WebMessageCompat message) {
        final ApiFeature.M feature = WebViewFeatureInternal.WEB_MESSAGE_PORT_POST_MESSAGE;
        // Only String type is supported by framework.
        if (feature.isSupportedByFramework() && message.getType() == WebMessageCompat.TYPE_STRING) {
            ApiHelperForM.postMessage(getFrameworksImpl(), compatToFrameworkMessage(message));
        } else if (feature.isSupportedByWebView()
                && WebMessageAdapter.isMessagePayloadTypeSupportedByWebView(message.getType())) {
            getBoundaryInterface().postMessage(
                    BoundaryInterfaceReflectionUtil.createInvocationHandlerFor(
                            new WebMessageAdapter(message)));
        } else {
            throw WebViewFeatureInternal.getUnsupportedOperationException();
        }
    }

    @Override
    public void close() {
        final ApiFeature.M feature = WebViewFeatureInternal.WEB_MESSAGE_PORT_CLOSE;
        if (feature.isSupportedByFramework()) {
            ApiHelperForM.close(getFrameworksImpl());
        } else if (feature.isSupportedByWebView()) {
            getBoundaryInterface().close();
        } else {
            throw WebViewFeatureInternal.getUnsupportedOperationException();
        }
    }

    @Override
    public void setWebMessageCallback(@NonNull final WebMessageCallbackCompat callback) {
        final ApiFeature.M feature = WebViewFeatureInternal.WEB_MESSAGE_PORT_SET_MESSAGE_CALLBACK;
        if (feature.isSupportedByWebView()) {
            // We prefer use WebView impl, since the impl in framework does not support
            // WebMessageCompat types other than String.
            getBoundaryInterface().setWebMessageCallback(
                    BoundaryInterfaceReflectionUtil.createInvocationHandlerFor(
                            new WebMessageCallbackAdapter(callback)));
        } else if (feature.isSupportedByFramework()) {
            ApiHelperForM.setWebMessageCallback(getFrameworksImpl(), callback);
        } else {
            throw WebViewFeatureInternal.getUnsupportedOperationException();
        }
    }

    @Override
    public void setWebMessageCallback(@Nullable Handler handler,
            @NonNull final WebMessageCallbackCompat callback) {
        final ApiFeature.M feature = WebViewFeatureInternal.CREATE_WEB_MESSAGE_CHANNEL;
        if (feature.isSupportedByWebView()) {
            // We prefer use WebView impl, since the impl in framework does not support
            // WebMessageCompat types other than String.
            getBoundaryInterface().setWebMessageCallback(
                    BoundaryInterfaceReflectionUtil.createInvocationHandlerFor(
                            new WebMessageCallbackAdapter(callback)), handler);
        } else if (feature.isSupportedByFramework()) {
            ApiHelperForM.setWebMessageCallback(getFrameworksImpl(), callback, handler);
        } else {
            throw WebViewFeatureInternal.getUnsupportedOperationException();
        }
    }

    @NonNull
    @RequiresApi(23)
    @Override
    public WebMessagePort getFrameworkPort() {
        return getFrameworksImpl();
    }

    @NonNull
    @Override
    public InvocationHandler getInvocationHandler() {
        return Proxy.getInvocationHandler(getBoundaryInterface());
    }

    /**
     * Convert an array of {@link WebMessagePort} objects into an array containing objects of the
     * corresponding support library class {@link WebMessagePortCompat}.
     */
    @Nullable
    public static WebMessagePortCompat[] portsToCompat(@Nullable WebMessagePort[] ports) {
        if (ports == null) return null;
        WebMessagePortCompat[] compatPorts = new WebMessagePortCompat[ports.length];
        for (int n = 0; n < ports.length; n++) {
            compatPorts[n] = new WebMessagePortImpl(ports[n]);
        }
        return compatPorts;
    }

    /**
     * Convert an array of {@link WebMessagePortCompat} objects into an array containing objects of
     * the corresponding framework class {@link WebMessagePort}.
     */
    @RequiresApi(23)
    @Nullable
    public static WebMessagePort[] compatToPorts(@Nullable WebMessagePortCompat[] compatPorts) {
        if (compatPorts == null) return null;
        WebMessagePort[] ports = new WebMessagePort[compatPorts.length];
        for (int n = 0; n < ports.length; n++) {
            ports[n] = compatPorts[n].getFrameworkPort();
        }
        return ports;
    }

    /**
     * Convert a {@link WebMessageCompat} into the corresponding framework class {@link WebMessage}.
     */
    @RequiresApi(23)
    @NonNull
    public static WebMessage compatToFrameworkMessage(@NonNull WebMessageCompat message) {
        return ApiHelperForM.createWebMessage(message);
    }

    /**
     * Convert a {@link WebMessage} into the corresponding support library class
     * {@link WebMessageCompat}.
     */
    @RequiresApi(23)
    @NonNull
    public static WebMessageCompat frameworkMessageToCompat(@NonNull WebMessage message) {
        return ApiHelperForM.createWebMessageCompat(message);
    }
}