1 /*
2  * Copyright 2020 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.ScriptHandler;
20 
21 import org.chromium.support_lib_boundary.ScriptHandlerBoundaryInterface;
22 import org.chromium.support_lib_boundary.util.BoundaryInterfaceReflectionUtil;
23 import org.jspecify.annotations.NonNull;
24 
25 import java.lang.reflect.InvocationHandler;
26 
27 /**
28  * Internal implementation of {@link androidx.webkit.ScriptHandler}.
29  */
30 public class ScriptHandlerImpl implements ScriptHandler {
31     private final ScriptHandlerBoundaryInterface mBoundaryInterface;
32 
ScriptHandlerImpl(@onNull ScriptHandlerBoundaryInterface boundaryInterface)33     private ScriptHandlerImpl(@NonNull ScriptHandlerBoundaryInterface boundaryInterface) {
34         mBoundaryInterface = boundaryInterface;
35     }
36 
37     /**
38      * Removes the corresponding script from WebView.
39      */
40     @Override
remove()41     public void remove() {
42         // If this method is called, the feature must exist, so no need to check feature
43         // DOCUMENT_START_JAVASCRIPT.
44         mBoundaryInterface.remove();
45     }
46 
47     /**
48      * Create an AndroidX ScriptHandler from the given InvocationHandler.
49      */
toScriptHandler( @onNull InvocationHandler invocationHandler)50     public static @NonNull ScriptHandlerImpl toScriptHandler(
51             /* ScriptHandler */ @NonNull InvocationHandler invocationHandler) {
52         final ScriptHandlerBoundaryInterface boundaryInterface =
53                 BoundaryInterfaceReflectionUtil.castToSuppLibClass(
54                         ScriptHandlerBoundaryInterface.class, invocationHandler);
55         return new ScriptHandlerImpl(boundaryInterface);
56     }
57 }
58