• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 android.webkit;
18 
19 import android.annotation.FlaggedApi;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.RequiresFeature;
23 import android.annotation.RequiresPermission;
24 import android.annotation.SuppressLint;
25 import android.annotation.SystemApi;
26 import android.app.SystemServiceRegistry;
27 import android.content.Context;
28 import android.content.pm.PackageInfo;
29 import android.content.pm.PackageManager;
30 import android.os.RemoteException;
31 
32 /** @hide */
33 @FlaggedApi(Flags.FLAG_UPDATE_SERVICE_IPC_WRAPPER)
34 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
35 public final class WebViewUpdateManager {
36     private final IWebViewUpdateService mService;
37 
38     /** @hide */
WebViewUpdateManager(@onNull IWebViewUpdateService service)39     public WebViewUpdateManager(@NonNull IWebViewUpdateService service) {
40         mService = service;
41     }
42 
43     /**
44      * Get the singleton instance of the manager.
45      *
46      * <p>This exists for the benefit of callsites without a {@link Context}; prefer
47      * {@link Context#getSystemService(Class)} otherwise.
48      *
49      * <p>This must only be called on devices with {@link PackageManager#FEATURE_WEBVIEW},
50      * and will WTF or throw {@link UnsupportedOperationException} otherwise.
51      */
52     @SuppressLint("ManagerLookup") // service opts in to getSystemServiceWithNoContext()
53     @RequiresFeature(PackageManager.FEATURE_WEBVIEW)
getInstance()54     public static @NonNull WebViewUpdateManager getInstance() {
55         WebViewUpdateManager manager =
56                 (WebViewUpdateManager) SystemServiceRegistry.getSystemServiceWithNoContext(
57                         Context.WEBVIEW_UPDATE_SERVICE);
58         if (manager == null) {
59             throw new UnsupportedOperationException("WebView not supported by device");
60         } else {
61             return manager;
62         }
63     }
64 
65     /**
66      * Block until system-level WebView preparations are complete.
67      *
68      * <p>This also makes the current WebView provider package visible to the caller.
69      *
70      * @return the status of WebView preparation and the current provider package.
71      */
waitForAndGetProvider()72     public @NonNull WebViewProviderResponse waitForAndGetProvider() {
73         try {
74             return mService.waitForAndGetProvider();
75         } catch (RemoteException e) {
76             throw e.rethrowFromSystemServer();
77         }
78     }
79 
80     /**
81      * Get the package that is the system's current WebView implementation.
82      *
83      * @return the package, or null if no valid implementation is present.
84      */
getCurrentWebViewPackage()85     public @Nullable PackageInfo getCurrentWebViewPackage() {
86         try {
87             return mService.getCurrentWebViewPackage();
88         } catch (RemoteException e) {
89             throw e.rethrowFromSystemServer();
90         }
91     }
92 
93     /**
94      * Get the complete list of supported WebView providers for this device.
95      *
96      * <p>This includes all configured providers, regardless of whether they are currently available
97      * or valid.
98      */
99     @SuppressLint({"ParcelableList", "ArrayReturn"})
getAllWebViewPackages()100     public @NonNull WebViewProviderInfo[] getAllWebViewPackages() {
101         try {
102             return mService.getAllWebViewPackages();
103         } catch (RemoteException e) {
104             throw e.rethrowFromSystemServer();
105         }
106     }
107 
108     /**
109      * Get the list of currently-valid WebView providers for this device.
110      *
111      * <p>This only includes providers that are currently present on the device and meet the
112      * validity criteria (signature, version, etc), but does not check if the provider is installed
113      * and enabled for all users.
114      *
115      * <p>Note that this will be filtered by the caller's package visibility; callers should
116      * have QUERY_ALL_PACKAGES permission to ensure that the list is complete.
117      */
118     @SuppressLint({"ParcelableList", "ArrayReturn"})
119     @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS)
getValidWebViewPackages()120     public @NonNull WebViewProviderInfo[] getValidWebViewPackages() {
121         try {
122             return mService.getValidWebViewPackages();
123         } catch (RemoteException e) {
124             throw e.rethrowFromSystemServer();
125         }
126     }
127 
128     /**
129      * Get the package name of the current WebView implementation.
130      *
131      * @return the package name, or null if no valid implementation is present.
132      */
getCurrentWebViewPackageName()133     public @Nullable String getCurrentWebViewPackageName() {
134         try {
135             return mService.getCurrentWebViewPackageName();
136         } catch (RemoteException e) {
137             throw e.rethrowFromSystemServer();
138         }
139     }
140 
141     /**
142      * Ask the system to switch to a specific WebView implementation if possible.
143      *
144      * <p>This choice will be stored persistently.
145      *
146      * @param newProvider the package name to use.
147      * @return the package name which is now in use, which may not be the
148      *         requested one if it was not usable.
149      */
150     @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
changeProviderAndSetting(@onNull String newProvider)151     public @Nullable String changeProviderAndSetting(@NonNull String newProvider) {
152         try {
153             return mService.changeProviderAndSetting(newProvider);
154         } catch (RemoteException e) {
155             throw e.rethrowFromSystemServer();
156         }
157     }
158 
159     /**
160      * Used by the relro file creator to notify the service that it's done.
161      * @hide
162      */
notifyRelroCreationCompleted()163     void notifyRelroCreationCompleted() {
164         try {
165             mService.notifyRelroCreationCompleted();
166         } catch (RemoteException e) {
167             throw e.rethrowFromSystemServer();
168         }
169     }
170 
171     /**
172      * Get the WebView provider which will be used if no explicit choice has been made.
173      *
174      * <p>The default provider is not guaranteed to be a valid/usable WebView implementation.
175      *
176      * @return the default WebView provider.
177      */
178     @FlaggedApi(Flags.FLAG_UPDATE_SERVICE_V2)
getDefaultWebViewPackage()179     public @NonNull WebViewProviderInfo getDefaultWebViewPackage() {
180         try {
181             return mService.getDefaultWebViewPackage();
182         } catch (RemoteException e) {
183             throw e.rethrowFromSystemServer();
184         }
185     }
186 }
187