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