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