1 // Copyright 2018 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package com.android.webview.chromium; 6 7 import android.app.ActivityManager; 8 import android.content.Context; 9 import android.content.Intent; 10 import android.net.Uri; 11 import android.os.Looper; 12 13 import org.chromium.android_webview.AwContentsClient; 14 import org.chromium.android_webview.AwContentsStatics; 15 import org.chromium.android_webview.AwDevToolsServer; 16 import org.chromium.android_webview.AwSettings; 17 import org.chromium.base.BuildInfo; 18 import org.chromium.base.Callback; 19 import org.chromium.base.MemoryPressureLevel; 20 import org.chromium.base.ThreadUtils; 21 import org.chromium.base.memory.MemoryPressureMonitor; 22 import org.chromium.base.task.PostTask; 23 import org.chromium.content_public.browser.UiThreadTaskTraits; 24 25 import java.util.List; 26 27 /** 28 * This class provides functionality that is accessed in a static way from apps using WebView. 29 * This class is meant to be shared between the webkit-glue layer and the support library glue 30 * layer. 31 * Ideally this class would live in a lower layer than the webkit-glue layer, to allow sharing the 32 * implementation between different glue layers without needing to depend on the webkit-glue layer 33 * (right now there are dependencies from this class on the webkit-glue layer though). 34 */ 35 public class SharedStatics { 36 private AwDevToolsServer mDevToolsServer; 37 SharedStatics()38 public SharedStatics() {} 39 findAddress(String addr)40 public String findAddress(String addr) { 41 return AwContentsStatics.findAddress(addr); 42 } 43 getDefaultUserAgent(Context context)44 public String getDefaultUserAgent(Context context) { 45 return AwSettings.getDefaultUserAgent(); 46 } 47 setWebContentsDebuggingEnabled(boolean enable)48 public void setWebContentsDebuggingEnabled(boolean enable) { 49 // On debug builds, Web Contents debugging is enabled elsewhere, and cannot be disabled. 50 if (BuildInfo.isDebugAndroid()) return; 51 setWebContentsDebuggingEnabledUnconditionally(enable); 52 } 53 setWebContentsDebuggingEnabledUnconditionally(boolean enable)54 public void setWebContentsDebuggingEnabledUnconditionally(boolean enable) { 55 if (Looper.myLooper() != ThreadUtils.getUiThreadLooper()) { 56 throw new RuntimeException( 57 "Toggling of Web Contents Debugging must be done on the UI thread"); 58 } 59 if (mDevToolsServer == null) { 60 if (!enable) return; 61 mDevToolsServer = new AwDevToolsServer(); 62 } 63 mDevToolsServer.setRemoteDebuggingEnabled(enable); 64 } 65 clearClientCertPreferences(Runnable onCleared)66 public void clearClientCertPreferences(Runnable onCleared) { 67 // clang-format off 68 PostTask.runOrPostTask(UiThreadTaskTraits.DEFAULT, () -> 69 AwContentsStatics.clearClientCertPreferences(onCleared)); 70 // clang-format on 71 } 72 freeMemoryForTests()73 public void freeMemoryForTests() { 74 if (ActivityManager.isRunningInTestHarness()) { 75 PostTask.postTask(UiThreadTaskTraits.DEFAULT, () -> { 76 // This variable is needed to prevent weird formatting by "git cl format". 77 MemoryPressureMonitor pressureMonitor = MemoryPressureMonitor.INSTANCE; 78 pressureMonitor.notifyPressure(MemoryPressureLevel.CRITICAL); 79 }); 80 } 81 } 82 enableSlowWholeDocumentDraw()83 public void enableSlowWholeDocumentDraw() { 84 WebViewChromium.enableSlowWholeDocumentDraw(); 85 } 86 parseFileChooserResult(int resultCode, Intent intent)87 public Uri[] parseFileChooserResult(int resultCode, Intent intent) { 88 return AwContentsClient.parseFileChooserResult(resultCode, intent); 89 } 90 91 /** 92 * Starts Safe Browsing initialization. This should only be called once. 93 * @param context is the application context the WebView will be used in. 94 * @param callback will be called with the value true if initialization is 95 * successful. The callback will be run on the UI thread. 96 */ initSafeBrowsing(Context context, Callback<Boolean> callback)97 public void initSafeBrowsing(Context context, Callback<Boolean> callback) { 98 // clang-format off 99 PostTask.runOrPostTask(UiThreadTaskTraits.DEFAULT, 100 () -> AwContentsStatics.initSafeBrowsing(context, callback)); 101 // clang-format on 102 } 103 setSafeBrowsingAllowlist(List<String> urls, Callback<Boolean> callback)104 public void setSafeBrowsingAllowlist(List<String> urls, Callback<Boolean> callback) { 105 // clang-format off 106 PostTask.runOrPostTask(UiThreadTaskTraits.DEFAULT, 107 () -> AwContentsStatics.setSafeBrowsingAllowlist(urls, callback)); 108 // clang-format on 109 } 110 111 /** 112 * Returns a URL pointing to the privacy policy for Safe Browsing reporting. 113 * 114 * @return the url pointing to a privacy policy document which can be displayed 115 * to users. 116 */ getSafeBrowsingPrivacyPolicyUrl()117 public Uri getSafeBrowsingPrivacyPolicyUrl() { 118 return PostTask.runSynchronously(UiThreadTaskTraits.DEFAULT, 119 () -> AwContentsStatics.getSafeBrowsingPrivacyPolicyUrl()); 120 } 121 isMultiProcessEnabled()122 public boolean isMultiProcessEnabled() { 123 return AwContentsStatics.isMultiProcessEnabled(); 124 } 125 } 126