1 /* 2 * Copyright (C) 2012 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.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SuppressLint; 22 import android.annotation.SystemApi; 23 import android.content.Intent; 24 import android.content.res.Configuration; 25 import android.graphics.Bitmap; 26 import android.graphics.Canvas; 27 import android.graphics.Paint; 28 import android.graphics.Picture; 29 import android.graphics.Rect; 30 import android.graphics.drawable.Drawable; 31 import android.net.Uri; 32 import android.net.http.SslCertificate; 33 import android.os.Bundle; 34 import android.os.Handler; 35 import android.os.Message; 36 import android.print.PrintDocumentAdapter; 37 import android.util.LongSparseArray; 38 import android.util.SparseArray; 39 import android.view.DragEvent; 40 import android.view.KeyEvent; 41 import android.view.MotionEvent; 42 import android.view.View; 43 import android.view.ViewGroup.LayoutParams; 44 import android.view.WindowInsets; 45 import android.view.accessibility.AccessibilityEvent; 46 import android.view.accessibility.AccessibilityNodeInfo; 47 import android.view.accessibility.AccessibilityNodeProvider; 48 import android.view.autofill.AutofillId; 49 import android.view.autofill.AutofillValue; 50 import android.view.inputmethod.EditorInfo; 51 import android.view.inputmethod.InputConnection; 52 import android.view.textclassifier.TextClassifier; 53 import android.view.translation.TranslationCapability; 54 import android.view.translation.TranslationSpec.DataFormat; 55 import android.view.translation.ViewTranslationRequest; 56 import android.view.translation.ViewTranslationResponse; 57 import android.webkit.WebView.HitTestResult; 58 import android.webkit.WebView.PictureListener; 59 import android.webkit.WebView.VisualStateCallback; 60 61 62 import java.io.BufferedWriter; 63 import java.io.File; 64 import java.util.List; 65 import java.util.Map; 66 import java.util.concurrent.Executor; 67 import java.util.function.Consumer; 68 69 /** 70 * WebView backend provider interface: this interface is the abstract backend to a WebView 71 * instance; each WebView object is bound to exactly one WebViewProvider object which implements 72 * the runtime behavior of that WebView. 73 * 74 * All methods must behave as per their namesake in {@link WebView}, unless otherwise noted. 75 * 76 * @hide Not part of the public API; only required by system implementors. 77 */ 78 @SystemApi 79 public interface WebViewProvider { 80 //------------------------------------------------------------------------- 81 // Main interface for backend provider of the WebView class. 82 //------------------------------------------------------------------------- 83 /** 84 * Initialize this WebViewProvider instance. Called after the WebView has fully constructed. 85 * @param javaScriptInterfaces is a Map of interface names, as keys, and 86 * object implementing those interfaces, as values. 87 * @param privateBrowsing If {@code true} the web view will be initialized in private / 88 * incognito mode. 89 */ init(Map<String, Object> javaScriptInterfaces, boolean privateBrowsing)90 public void init(Map<String, Object> javaScriptInterfaces, 91 boolean privateBrowsing); 92 93 // Deprecated - should never be called setHorizontalScrollbarOverlay(boolean overlay)94 public void setHorizontalScrollbarOverlay(boolean overlay); 95 96 // Deprecated - should never be called setVerticalScrollbarOverlay(boolean overlay)97 public void setVerticalScrollbarOverlay(boolean overlay); 98 99 // Deprecated - should never be called overlayHorizontalScrollbar()100 public boolean overlayHorizontalScrollbar(); 101 102 // Deprecated - should never be called overlayVerticalScrollbar()103 public boolean overlayVerticalScrollbar(); 104 getVisibleTitleHeight()105 public int getVisibleTitleHeight(); 106 getCertificate()107 public SslCertificate getCertificate(); 108 setCertificate(SslCertificate certificate)109 public void setCertificate(SslCertificate certificate); 110 savePassword(String host, String username, String password)111 public void savePassword(String host, String username, String password); 112 setHttpAuthUsernamePassword(String host, String realm, String username, String password)113 public void setHttpAuthUsernamePassword(String host, String realm, 114 String username, String password); 115 getHttpAuthUsernamePassword(String host, String realm)116 public String[] getHttpAuthUsernamePassword(String host, String realm); 117 118 /** 119 * See {@link WebView#destroy()}. 120 * As well as releasing the internal state and resources held by the implementation, 121 * the provider should null all references it holds on the WebView proxy class, and ensure 122 * no further method calls are made to it. 123 */ destroy()124 public void destroy(); 125 setNetworkAvailable(boolean networkUp)126 public void setNetworkAvailable(boolean networkUp); 127 saveState(Bundle outState)128 public WebBackForwardList saveState(Bundle outState); 129 savePicture(Bundle b, final File dest)130 public boolean savePicture(Bundle b, final File dest); 131 restorePicture(Bundle b, File src)132 public boolean restorePicture(Bundle b, File src); 133 restoreState(Bundle inState)134 public WebBackForwardList restoreState(Bundle inState); 135 loadUrl(String url, Map<String, String> additionalHttpHeaders)136 public void loadUrl(String url, Map<String, String> additionalHttpHeaders); 137 loadUrl(String url)138 public void loadUrl(String url); 139 postUrl(String url, byte[] postData)140 public void postUrl(String url, byte[] postData); 141 loadData(String data, String mimeType, String encoding)142 public void loadData(String data, String mimeType, String encoding); 143 loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl)144 public void loadDataWithBaseURL(String baseUrl, String data, 145 String mimeType, String encoding, String historyUrl); 146 evaluateJavaScript(String script, ValueCallback<String> resultCallback)147 public void evaluateJavaScript(String script, ValueCallback<String> resultCallback); 148 saveWebArchive(String filename)149 public void saveWebArchive(String filename); 150 saveWebArchive(String basename, boolean autoname, ValueCallback<String> callback)151 public void saveWebArchive(String basename, boolean autoname, ValueCallback<String> callback); 152 stopLoading()153 public void stopLoading(); 154 reload()155 public void reload(); 156 canGoBack()157 public boolean canGoBack(); 158 goBack()159 public void goBack(); 160 canGoForward()161 public boolean canGoForward(); 162 goForward()163 public void goForward(); 164 canGoBackOrForward(int steps)165 public boolean canGoBackOrForward(int steps); 166 goBackOrForward(int steps)167 public void goBackOrForward(int steps); 168 isPrivateBrowsingEnabled()169 public boolean isPrivateBrowsingEnabled(); 170 pageUp(boolean top)171 public boolean pageUp(boolean top); 172 pageDown(boolean bottom)173 public boolean pageDown(boolean bottom); 174 insertVisualStateCallback(long requestId, VisualStateCallback callback)175 public void insertVisualStateCallback(long requestId, VisualStateCallback callback); 176 clearView()177 public void clearView(); 178 capturePicture()179 public Picture capturePicture(); 180 createPrintDocumentAdapter(String documentName)181 public PrintDocumentAdapter createPrintDocumentAdapter(String documentName); 182 getScale()183 public float getScale(); 184 setInitialScale(int scaleInPercent)185 public void setInitialScale(int scaleInPercent); 186 invokeZoomPicker()187 public void invokeZoomPicker(); 188 getHitTestResult()189 public HitTestResult getHitTestResult(); 190 requestFocusNodeHref(Message hrefMsg)191 public void requestFocusNodeHref(Message hrefMsg); 192 requestImageRef(Message msg)193 public void requestImageRef(Message msg); 194 getUrl()195 public String getUrl(); 196 getOriginalUrl()197 public String getOriginalUrl(); 198 getTitle()199 public String getTitle(); 200 getFavicon()201 public Bitmap getFavicon(); 202 getTouchIconUrl()203 public String getTouchIconUrl(); 204 getProgress()205 public int getProgress(); 206 getContentHeight()207 public int getContentHeight(); 208 getContentWidth()209 public int getContentWidth(); 210 pauseTimers()211 public void pauseTimers(); 212 resumeTimers()213 public void resumeTimers(); 214 onPause()215 public void onPause(); 216 onResume()217 public void onResume(); 218 isPaused()219 public boolean isPaused(); 220 freeMemory()221 public void freeMemory(); 222 clearCache(boolean includeDiskFiles)223 public void clearCache(boolean includeDiskFiles); 224 clearFormData()225 public void clearFormData(); 226 clearHistory()227 public void clearHistory(); 228 clearSslPreferences()229 public void clearSslPreferences(); 230 copyBackForwardList()231 public WebBackForwardList copyBackForwardList(); 232 setFindListener(WebView.FindListener listener)233 public void setFindListener(WebView.FindListener listener); 234 findNext(boolean forward)235 public void findNext(boolean forward); 236 findAll(String find)237 public int findAll(String find); 238 findAllAsync(String find)239 public void findAllAsync(String find); 240 showFindDialog(String text, boolean showIme)241 public boolean showFindDialog(String text, boolean showIme); 242 clearMatches()243 public void clearMatches(); 244 documentHasImages(Message response)245 public void documentHasImages(Message response); 246 setWebViewClient(WebViewClient client)247 public void setWebViewClient(WebViewClient client); 248 getWebViewClient()249 public WebViewClient getWebViewClient(); 250 251 @Nullable getWebViewRenderProcess()252 public WebViewRenderProcess getWebViewRenderProcess(); 253 setWebViewRenderProcessClient( @ullable Executor executor, @Nullable WebViewRenderProcessClient client)254 public void setWebViewRenderProcessClient( 255 @Nullable Executor executor, 256 @Nullable WebViewRenderProcessClient client); 257 258 @Nullable getWebViewRenderProcessClient()259 public WebViewRenderProcessClient getWebViewRenderProcessClient(); 260 setDownloadListener(DownloadListener listener)261 public void setDownloadListener(DownloadListener listener); 262 setWebChromeClient(WebChromeClient client)263 public void setWebChromeClient(WebChromeClient client); 264 getWebChromeClient()265 public WebChromeClient getWebChromeClient(); 266 setPictureListener(PictureListener listener)267 public void setPictureListener(PictureListener listener); 268 addJavascriptInterface(Object obj, String interfaceName)269 public void addJavascriptInterface(Object obj, String interfaceName); 270 removeJavascriptInterface(String interfaceName)271 public void removeJavascriptInterface(String interfaceName); 272 createWebMessageChannel()273 public WebMessagePort[] createWebMessageChannel(); 274 postMessageToMainFrame(WebMessage message, Uri targetOrigin)275 public void postMessageToMainFrame(WebMessage message, Uri targetOrigin); 276 getSettings()277 public WebSettings getSettings(); 278 setMapTrackballToArrowKeys(boolean setMap)279 public void setMapTrackballToArrowKeys(boolean setMap); 280 flingScroll(int vx, int vy)281 public void flingScroll(int vx, int vy); 282 getZoomControls()283 public View getZoomControls(); 284 canZoomIn()285 public boolean canZoomIn(); 286 canZoomOut()287 public boolean canZoomOut(); 288 zoomBy(float zoomFactor)289 public boolean zoomBy(float zoomFactor); 290 zoomIn()291 public boolean zoomIn(); 292 zoomOut()293 public boolean zoomOut(); 294 dumpViewHierarchyWithProperties(BufferedWriter out, int level)295 public void dumpViewHierarchyWithProperties(BufferedWriter out, int level); 296 findHierarchyView(String className, int hashCode)297 public View findHierarchyView(String className, int hashCode); 298 setRendererPriorityPolicy(int rendererRequestedPriority, boolean waivedWhenNotVisible)299 public void setRendererPriorityPolicy(int rendererRequestedPriority, boolean waivedWhenNotVisible); 300 getRendererRequestedPriority()301 public int getRendererRequestedPriority(); 302 getRendererPriorityWaivedWhenNotVisible()303 public boolean getRendererPriorityWaivedWhenNotVisible(); 304 305 @SuppressWarnings("unused") setTextClassifier(@ullable TextClassifier textClassifier)306 public default void setTextClassifier(@Nullable TextClassifier textClassifier) {} 307 308 @NonNull getTextClassifier()309 public default TextClassifier getTextClassifier() { return TextClassifier.NO_OP; } 310 311 //------------------------------------------------------------------------- 312 // Provider internal methods 313 //------------------------------------------------------------------------- 314 315 /** 316 * @return the ViewDelegate implementation. This provides the functionality to back all of 317 * the name-sake functions from the View and ViewGroup base classes of WebView. 318 */ getViewDelegate()319 /* package */ ViewDelegate getViewDelegate(); 320 321 /** 322 * @return a ScrollDelegate implementation. Normally this would be same object as is 323 * returned by getViewDelegate(). 324 */ getScrollDelegate()325 /* package */ ScrollDelegate getScrollDelegate(); 326 327 /** 328 * Only used by FindActionModeCallback to inform providers that the find dialog has 329 * been dismissed. 330 */ notifyFindDialogDismissed()331 public void notifyFindDialogDismissed(); 332 333 //------------------------------------------------------------------------- 334 // View / ViewGroup delegation methods 335 //------------------------------------------------------------------------- 336 337 /** 338 * Provides mechanism for the name-sake methods declared in View and ViewGroup to be delegated 339 * into the WebViewProvider instance. 340 * NOTE: For many of these methods, the WebView will provide a super.Foo() call before or after 341 * making the call into the provider instance. This is done for convenience in the common case 342 * of maintaining backward compatibility. For remaining super class calls (e.g. where the 343 * provider may need to only conditionally make the call based on some internal state) see the 344 * {@link WebView.PrivateAccess} callback class. 345 */ 346 // TODO: See if the pattern of the super-class calls can be rationalized at all, and document 347 // the remainder on the methods below. 348 interface ViewDelegate { shouldDelayChildPressedState()349 public boolean shouldDelayChildPressedState(); 350 onProvideVirtualStructure(android.view.ViewStructure structure)351 public void onProvideVirtualStructure(android.view.ViewStructure structure); 352 onProvideAutofillVirtualStructure( @uppressWarnings"unused") android.view.ViewStructure structure, @SuppressWarnings("unused") int flags)353 default void onProvideAutofillVirtualStructure( 354 @SuppressWarnings("unused") android.view.ViewStructure structure, 355 @SuppressWarnings("unused") int flags) { 356 } 357 autofill(@uppressWarnings"unused") SparseArray<AutofillValue> values)358 default void autofill(@SuppressWarnings("unused") SparseArray<AutofillValue> values) { 359 } 360 isVisibleToUserForAutofill(@uppressWarnings"unused") int virtualId)361 default boolean isVisibleToUserForAutofill(@SuppressWarnings("unused") int virtualId) { 362 return true; // true is the default value returned by View.isVisibleToUserForAutofill() 363 } 364 onProvideContentCaptureStructure( @onNull @uppressWarnings"unused") android.view.ViewStructure structure, @SuppressWarnings("unused") int flags)365 default void onProvideContentCaptureStructure( 366 @NonNull @SuppressWarnings("unused") android.view.ViewStructure structure, 367 @SuppressWarnings("unused") int flags) { 368 } 369 370 @SuppressLint("NullableCollection") onCreateVirtualViewTranslationRequests( @onNull @uppressWarnings"unused") long[] virtualIds, @NonNull @SuppressWarnings("unused") @DataFormat int[] supportedFormats, @NonNull @SuppressWarnings("unused") Consumer<ViewTranslationRequest> requestsCollector)371 default void onCreateVirtualViewTranslationRequests( 372 @NonNull @SuppressWarnings("unused") long[] virtualIds, 373 @NonNull @SuppressWarnings("unused") @DataFormat int[] supportedFormats, 374 @NonNull @SuppressWarnings("unused") 375 Consumer<ViewTranslationRequest> requestsCollector) { 376 } 377 onVirtualViewTranslationResponses( @onNull @uppressWarnings"unused") LongSparseArray<ViewTranslationResponse> response)378 default void onVirtualViewTranslationResponses( 379 @NonNull @SuppressWarnings("unused") 380 LongSparseArray<ViewTranslationResponse> response) { 381 } 382 dispatchCreateViewTranslationRequest( @onNull @uppressWarnings"unused") Map<AutofillId, long[]> viewIds, @NonNull @SuppressWarnings("unused") @DataFormat int[] supportedFormats, @Nullable @SuppressWarnings("unused") TranslationCapability capability, @NonNull @SuppressWarnings("unused") List<ViewTranslationRequest> requests)383 default void dispatchCreateViewTranslationRequest( 384 @NonNull @SuppressWarnings("unused") Map<AutofillId, long[]> viewIds, 385 @NonNull @SuppressWarnings("unused") @DataFormat int[] supportedFormats, 386 @Nullable @SuppressWarnings("unused") TranslationCapability capability, 387 @NonNull @SuppressWarnings("unused") List<ViewTranslationRequest> requests) { 388 389 } 390 getAccessibilityNodeProvider()391 public AccessibilityNodeProvider getAccessibilityNodeProvider(); 392 onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)393 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info); 394 onInitializeAccessibilityEvent(AccessibilityEvent event)395 public void onInitializeAccessibilityEvent(AccessibilityEvent event); 396 performAccessibilityAction(int action, Bundle arguments)397 public boolean performAccessibilityAction(int action, Bundle arguments); 398 setOverScrollMode(int mode)399 public void setOverScrollMode(int mode); 400 setScrollBarStyle(int style)401 public void setScrollBarStyle(int style); 402 onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar, int l, int t, int r, int b)403 public void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar, int l, int t, 404 int r, int b); 405 onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY)406 public void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY); 407 onWindowVisibilityChanged(int visibility)408 public void onWindowVisibilityChanged(int visibility); 409 onDraw(Canvas canvas)410 public void onDraw(Canvas canvas); 411 setLayoutParams(LayoutParams layoutParams)412 public void setLayoutParams(LayoutParams layoutParams); 413 performLongClick()414 public boolean performLongClick(); 415 onConfigurationChanged(Configuration newConfig)416 public void onConfigurationChanged(Configuration newConfig); 417 onCreateInputConnection(EditorInfo outAttrs)418 public InputConnection onCreateInputConnection(EditorInfo outAttrs); 419 onDragEvent(DragEvent event)420 public boolean onDragEvent(DragEvent event); 421 onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)422 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event); 423 onKeyDown(int keyCode, KeyEvent event)424 public boolean onKeyDown(int keyCode, KeyEvent event); 425 onKeyUp(int keyCode, KeyEvent event)426 public boolean onKeyUp(int keyCode, KeyEvent event); 427 onAttachedToWindow()428 public void onAttachedToWindow(); 429 onDetachedFromWindow()430 public void onDetachedFromWindow(); 431 onMovedToDisplay(int displayId, Configuration config)432 public default void onMovedToDisplay(int displayId, Configuration config) {} 433 onVisibilityChanged(View changedView, int visibility)434 public void onVisibilityChanged(View changedView, int visibility); 435 onWindowFocusChanged(boolean hasWindowFocus)436 public void onWindowFocusChanged(boolean hasWindowFocus); 437 onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect)438 public void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect); 439 setFrame(int left, int top, int right, int bottom)440 public boolean setFrame(int left, int top, int right, int bottom); 441 onSizeChanged(int w, int h, int ow, int oh)442 public void onSizeChanged(int w, int h, int ow, int oh); 443 onScrollChanged(int l, int t, int oldl, int oldt)444 public void onScrollChanged(int l, int t, int oldl, int oldt); 445 dispatchKeyEvent(KeyEvent event)446 public boolean dispatchKeyEvent(KeyEvent event); 447 onTouchEvent(MotionEvent ev)448 public boolean onTouchEvent(MotionEvent ev); 449 onHoverEvent(MotionEvent event)450 public boolean onHoverEvent(MotionEvent event); 451 onGenericMotionEvent(MotionEvent event)452 public boolean onGenericMotionEvent(MotionEvent event); 453 onTrackballEvent(MotionEvent ev)454 public boolean onTrackballEvent(MotionEvent ev); 455 requestFocus(int direction, Rect previouslyFocusedRect)456 public boolean requestFocus(int direction, Rect previouslyFocusedRect); 457 onMeasure(int widthMeasureSpec, int heightMeasureSpec)458 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec); 459 requestChildRectangleOnScreen(View child, Rect rect, boolean immediate)460 public boolean requestChildRectangleOnScreen(View child, Rect rect, boolean immediate); 461 setBackgroundColor(int color)462 public void setBackgroundColor(int color); 463 setLayerType(int layerType, Paint paint)464 public void setLayerType(int layerType, Paint paint); 465 preDispatchDraw(Canvas canvas)466 public void preDispatchDraw(Canvas canvas); 467 onStartTemporaryDetach()468 public void onStartTemporaryDetach(); 469 onFinishTemporaryDetach()470 public void onFinishTemporaryDetach(); 471 onActivityResult(int requestCode, int resultCode, Intent data)472 public void onActivityResult(int requestCode, int resultCode, Intent data); 473 getHandler(Handler originalHandler)474 public Handler getHandler(Handler originalHandler); 475 findFocus(View originalFocusedView)476 public View findFocus(View originalFocusedView); 477 478 @SuppressWarnings("unused") onCheckIsTextEditor()479 default boolean onCheckIsTextEditor() { 480 return false; 481 } 482 483 @SuppressWarnings("unused") 484 @Nullable onApplyWindowInsets(@ullable WindowInsets insets)485 default WindowInsets onApplyWindowInsets(@Nullable WindowInsets insets) { 486 return null; 487 } 488 } 489 490 interface ScrollDelegate { 491 // These methods are declared protected in the ViewGroup base class. This interface 492 // exists to promote them to public so they may be called by the WebView proxy class. 493 // TODO: Combine into ViewDelegate? 494 /** 495 * See {@link android.webkit.WebView#computeHorizontalScrollRange} 496 */ computeHorizontalScrollRange()497 public int computeHorizontalScrollRange(); 498 499 /** 500 * See {@link android.webkit.WebView#computeHorizontalScrollOffset} 501 */ computeHorizontalScrollOffset()502 public int computeHorizontalScrollOffset(); 503 504 /** 505 * See {@link android.webkit.WebView#computeVerticalScrollRange} 506 */ computeVerticalScrollRange()507 public int computeVerticalScrollRange(); 508 509 /** 510 * See {@link android.webkit.WebView#computeVerticalScrollOffset} 511 */ computeVerticalScrollOffset()512 public int computeVerticalScrollOffset(); 513 514 /** 515 * See {@link android.webkit.WebView#computeVerticalScrollExtent} 516 */ computeVerticalScrollExtent()517 public int computeVerticalScrollExtent(); 518 519 /** 520 * See {@link android.webkit.WebView#computeScroll} 521 */ computeScroll()522 public void computeScroll(); 523 } 524 } 525