2626import android .graphics .Rect ;
2727import android .graphics .drawable .Drawable ;
2828import android .net .http .SslCertificate ;
29- import android .os .Build ;
3029import android .os .Bundle ;
3130import android .os .Looper ;
3231import android .os .Message ;
@@ -1479,18 +1478,30 @@ public void setPictureListener(PictureListener listener) {
14791478 /**
14801479 * Injects the supplied Java object into this WebView. The object is
14811480 * injected into the JavaScript context of the main frame, using the
1482- * supplied name. This allows the Java object's public methods to be
1483- * accessed from JavaScript. Note that that injected objects will not
1481+ * supplied name. This allows the Java object's methods to be
1482+ * accessed from JavaScript. For API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
1483+ * and above, only public methods that are annotated with
1484+ * {@link android.webkit.JavascriptInterface} can be accessed from JavaScript.
1485+ * For API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN} or below,
1486+ * all public methods (including the inherited ones) can be accessed, see the
1487+ * important security note below for implications. Note that injected objects will not
14841488 * appear in JavaScript until the page is next (re)loaded. For example:
1485- * <pre> webView.addJavascriptInterface(new Object(), "injectedObject");
1489+ * <pre>
1490+ * class JsObject {
1491+ * {@literal @}JavascriptInterface
1492+ * public String toString() { return "injectedObject"; }
1493+ * }
1494+ * webView.addJavascriptInterface(new JsObject(), "injectedObject");
14861495 * webView.loadData("<!DOCTYPE html><title></title>", "text/html", null);
14871496 * webView.loadUrl("javascript:alert(injectedObject.toString())");</pre>
14881497 * <p>
14891498 * <strong>IMPORTANT:</strong>
14901499 * <ul>
14911500 * <li> This method can be used to allow JavaScript to control the host
14921501 * application. This is a powerful feature, but also presents a security
1493- * risk, particularly as JavaScript could use reflection to access an
1502+ * risk for applications targeting API level
1503+ * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} or below, because
1504+ * JavaScript could use reflection to access an
14941505 * injected object's public fields. Use of this method in a WebView
14951506 * containing untrusted content could allow an attacker to manipulate the
14961507 * host application in unintended ways, executing Java code with the
@@ -1499,6 +1510,7 @@ public void setPictureListener(PictureListener listener) {
14991510 * <li> JavaScript interacts with Java object on a private, background
15001511 * thread of this WebView. Care is therefore required to maintain thread
15011512 * safety.</li>
1513+ * <li> The Java object's fields are not accessible.</li>
15021514 * </ul>
15031515 *
15041516 * @param object the Java object to inject into this WebView's JavaScript
@@ -1508,9 +1520,6 @@ public void setPictureListener(PictureListener listener) {
15081520 public void addJavascriptInterface (Object object , String name ) {
15091521 checkThread ();
15101522 mProvider .addJavascriptInterface (object , name );
1511- // TODO in a separate CL provide logic to enable annotations for API level JB_MR1 and above. Don't forget to
1512- // update the doc, set a link to annotation and unhide the annotation.
1513- // also describe that fields of java objects are not accessible from JS.
15141523 }
15151524
15161525 /**
0 commit comments