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 ;
@@ -1494,18 +1493,30 @@ public void setPictureListener(PictureListener listener) {
14941493 /**
14951494 * Injects the supplied Java object into this WebView. The object is
14961495 * injected into the JavaScript context of the main frame, using the
1497- * supplied name. This allows the Java object's public methods to be
1498- * accessed from JavaScript. Note that that injected objects will not
1496+ * supplied name. This allows the Java object's methods to be
1497+ * accessed from JavaScript. For API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
1498+ * and above, only public methods that are annotated with
1499+ * {@link android.webkit.JavascriptInterface} can be accessed from JavaScript.
1500+ * For API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN} or below,
1501+ * all public methods (including the inherited ones) can be accessed, see the
1502+ * important security note below for implications. Note that injected objects will not
14991503 * appear in JavaScript until the page is next (re)loaded. For example:
1500- * <pre> webView.addJavascriptInterface(new Object(), "injectedObject");
1504+ * <pre>
1505+ * class JsObject {
1506+ * {@literal @}JavascriptInterface
1507+ * public String toString() { return "injectedObject"; }
1508+ * }
1509+ * webView.addJavascriptInterface(new JsObject(), "injectedObject");
15011510 * webView.loadData("<!DOCTYPE html><title></title>", "text/html", null);
15021511 * webView.loadUrl("javascript:alert(injectedObject.toString())");</pre>
15031512 * <p>
15041513 * <strong>IMPORTANT:</strong>
15051514 * <ul>
15061515 * <li> This method can be used to allow JavaScript to control the host
15071516 * application. This is a powerful feature, but also presents a security
1508- * risk, particularly as JavaScript could use reflection to access an
1517+ * risk for applications targeting API level
1518+ * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} or below, because
1519+ * JavaScript could use reflection to access an
15091520 * injected object's public fields. Use of this method in a WebView
15101521 * containing untrusted content could allow an attacker to manipulate the
15111522 * host application in unintended ways, executing Java code with the
@@ -1514,6 +1525,7 @@ public void setPictureListener(PictureListener listener) {
15141525 * <li> JavaScript interacts with Java object on a private, background
15151526 * thread of this WebView. Care is therefore required to maintain thread
15161527 * safety.</li>
1528+ * <li> The Java object's fields are not accessible.</li>
15171529 * </ul>
15181530 *
15191531 * @param object the Java object to inject into this WebView's JavaScript
@@ -1523,9 +1535,6 @@ public void setPictureListener(PictureListener listener) {
15231535 public void addJavascriptInterface (Object object , String name ) {
15241536 checkThread ();
15251537 mProvider .addJavascriptInterface (object , name );
1526- // TODO in a separate CL provide logic to enable annotations for API level JB_MR1 and above. Don't forget to
1527- // update the doc, set a link to annotation and unhide the annotation.
1528- // also describe that fields of java objects are not accessible from JS.
15291538 }
15301539
15311540 /**
0 commit comments