1 /*
2  * Copyright 2022 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.internal;
18 
19 import androidx.webkit.WebMessageCompat;
20 
21 import org.chromium.support_lib_boundary.WebMessagePayloadBoundaryInterface;
22 import org.jspecify.annotations.NonNull;
23 import org.jspecify.annotations.Nullable;
24 
25 import java.util.Objects;
26 
27 /**
28  * Adapter between {@link WebMessageCompat} and {@link WebMessagePayloadBoundaryInterface}.
29  * This class is used to pass **payload** of a WebMessageCompat to Chromium.
30  */
31 public class WebMessagePayloadAdapter implements WebMessagePayloadBoundaryInterface {
32     private final @WebMessagePayloadType int mType;
33     private final @Nullable String mString;
34     private final byte @Nullable [] mArrayBuffer;
35 
WebMessagePayloadAdapter(final @Nullable String data)36     public WebMessagePayloadAdapter(final @Nullable String data) {
37         mType = WebMessageCompat.TYPE_STRING;
38         mString = data;
39         mArrayBuffer = null;
40     }
41 
WebMessagePayloadAdapter(final byte @NonNull [] arrayBuffer)42     public WebMessagePayloadAdapter(final byte @NonNull [] arrayBuffer) {
43         mType = WebMessageCompat.TYPE_ARRAY_BUFFER;
44         mString = null;
45         mArrayBuffer = arrayBuffer;
46     }
47 
48     @Override
getSupportedFeatures()49     public String @NonNull [] getSupportedFeatures() {
50         // getType, getAsString and getAsArrayBuffer are covered by
51         // WEB_MESSAGE_ARRAY_BUFFER.
52         return new String[0];
53     }
54 
55     @Override
getType()56     public int getType() {
57         return mType;
58     }
59 
60     @Override
getAsString()61     public @Nullable String getAsString() {
62         checkType(WebMessagePayloadType.TYPE_STRING);
63         return mString;
64     }
65 
66     @Override
getAsArrayBuffer()67     public byte @NonNull [] getAsArrayBuffer() {
68         checkType(WebMessagePayloadType.TYPE_ARRAY_BUFFER);
69         return Objects.requireNonNull(mArrayBuffer);
70     }
71 
72     /**
73      * Check if current message payload type is the {@code expectedType}.
74      */
checkType(@ebMessagePayloadType int expectedType)75     private void checkType(@WebMessagePayloadType int expectedType) {
76         if (mType != expectedType) {
77             throw new IllegalStateException("Expected " + expectedType + ", but type is " + mType);
78         }
79     }
80 
81 }
82