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