@@ -89,8 +89,19 @@ class BrowserFrame extends Handler {
8989 // Is this frame the main frame?
9090 private boolean mIsMainFrame ;
9191
92+ // Javascript interface object
93+ private class JSObject {
94+ Object object ;
95+ boolean requireAnnotation ;
96+
97+ public JSObject (Object object , boolean requireAnnotation ) {
98+ this .object = object ;
99+ this .requireAnnotation = requireAnnotation ;
100+ }
101+ }
102+
92103 // Attached Javascript interfaces
93- private Map <String , Object > mJavaScriptObjects ;
104+ private Map <String , JSObject > mJavaScriptObjects ;
94105 private Set <Object > mRemovedJavaScriptObjects ;
95106
96107 // Key store handler when Chromium HTTP stack is used.
@@ -234,10 +245,8 @@ public BrowserFrame(Context context, WebViewCore w, CallbackProxy proxy,
234245 }
235246 sConfigCallback .addHandler (this );
236247
237- mJavaScriptObjects = javascriptInterfaces ;
238- if (mJavaScriptObjects == null ) {
239- mJavaScriptObjects = new HashMap <String , Object >();
240- }
248+ mJavaScriptObjects = new HashMap <String , JSObject >();
249+ addJavaScriptObjects (javascriptInterfaces );
241250 mRemovedJavaScriptObjects = new HashSet <Object >();
242251
243252 mSettings = settings ;
@@ -590,15 +599,36 @@ private void windowObjectCleared(int nativeFramePointer) {
590599 Iterator <String > iter = mJavaScriptObjects .keySet ().iterator ();
591600 while (iter .hasNext ()) {
592601 String interfaceName = iter .next ();
593- Object object = mJavaScriptObjects .get (interfaceName );
594- if (object != null ) {
602+ JSObject jsobject = mJavaScriptObjects .get (interfaceName );
603+ if (jsobject != null && jsobject . object != null ) {
595604 nativeAddJavascriptInterface (nativeFramePointer ,
596- mJavaScriptObjects . get ( interfaceName ) , interfaceName );
605+ jsobject . object , interfaceName , jsobject . requireAnnotation );
597606 }
598607 }
599608 mRemovedJavaScriptObjects .clear ();
600609 }
601610
611+ /*
612+ * Add javascript objects to the internal list of objects. The default behavior
613+ * is to allow access to inherited methods (no annotation needed). This is only
614+ * used when js objects are passed through a constructor (via a hidden constructor).
615+ *
616+ * @TODO change the default behavior to be compatible with the public addjavascriptinterface
617+ */
618+ private void addJavaScriptObjects (Map <String , Object > javascriptInterfaces ) {
619+
620+ // TODO in a separate CL provide logic to enable annotations for API level JB_MR1 and above.
621+ if (javascriptInterfaces == null ) return ;
622+ Iterator <String > iter = javascriptInterfaces .keySet ().iterator ();
623+ while (iter .hasNext ()) {
624+ String interfaceName = iter .next ();
625+ Object object = javascriptInterfaces .get (interfaceName );
626+ if (object != null ) {
627+ mJavaScriptObjects .put (interfaceName , new JSObject (object , false ));
628+ }
629+ }
630+ }
631+
602632 /**
603633 * This method is called by WebCore to check whether application
604634 * wants to hijack url loading
@@ -616,11 +646,11 @@ public boolean handleUrl(String url) {
616646 }
617647 }
618648
619- public void addJavascriptInterface (Object obj , String interfaceName ) {
649+ public void addJavascriptInterface (Object obj , String interfaceName ,
650+ boolean requireAnnotation ) {
620651 assert obj != null ;
621652 removeJavascriptInterface (interfaceName );
622-
623- mJavaScriptObjects .put (interfaceName , obj );
653+ mJavaScriptObjects .put (interfaceName , new JSObject (obj , requireAnnotation ));
624654 }
625655
626656 public void removeJavascriptInterface (String interfaceName ) {
@@ -1245,7 +1275,7 @@ private native void nativeCallPolicyFunction(int policyFunction,
12451275 * Add a javascript interface to the main frame.
12461276 */
12471277 private native void nativeAddJavascriptInterface (int nativeFramePointer ,
1248- Object obj , String interfaceName );
1278+ Object obj , String interfaceName , boolean requireAnnotation );
12491279
12501280 public native void clearCache ();
12511281
0 commit comments