PostMessage for Android Webview

2,846 views
Skip to first unread message

Selim Gurun

unread,
May 23, 2014, 4:19:12 PM5/23/14
to Nasko Oskov, Daniel Cheng, Site Isolation Development
Hello,

I am working on adding a postmessage based API for Android Webview. We want the API to be similar to HTML5 postMessage:

public interface JsMessage {
    // an ancestor for container classes that can be send as messages between Java and JS
}

public void postMessageToFrame(String frameId, JsMessage message, Uri targetOrigin);

Here, the first parameter frameId indicates which frame to send the message to, i.e.

<iframe id="foo" name="bar" src="iframe.html" onload="sendMsg()" ></iframe>


My question is, what would be the proper attribute to use here? The id or the name?



If id is the better choice, in chromium, is there any example code that I can follow to find a frame given the Id? (I think already cracked the name case).

Thanks,

-Selim

Eric Seidel

unread,
May 23, 2014, 4:31:43 PM5/23/14
to Selim Gurun, Nasko Oskov, Daniel Cheng, Site Isolation Development
ids are document-local. Frame names are page-global. I suspect you
want the name.

Daniel Cheng

unread,
May 23, 2014, 4:32:36 PM5/23/14
to Selim Gurun, Nasko Oskov, Daniel Cheng, Site Isolation Development
Usually, postMessage doesn't target a frame by name. You have a DOMWindow reference and postMessage to that. Would it be reasonable to have the WebView API do something similar?

If we just use the name, there will be frames that are potentially untargetable (internally, we'll have to pick between duplicate names arbitrarily).

Selim Gurun

unread,
May 23, 2014, 4:56:37 PM5/23/14
to Daniel Cheng, Nasko Oskov, Daniel Cheng, Site Isolation Development
himm, actually we were thinking of two APIs: postMessageToFrame() and postMessageToMainWindow(). So you are correct, if a frame does not have a name or multiple frames have the same name, then the developer won't be able to post a message. I am not sure how important it is to support these cases in a Webview scenario. I would assume that the developer of the native App would control the Web content. 

Mikhail Naganov

unread,
May 27, 2014, 8:09:58 AM5/27/14
to Selim Gurun, Daniel Cheng, Nasko Oskov, Daniel Cheng, Site Isolation Development
> I would assume that the developer of the native App would control the Web content.

Even if the app shows arbitrary content, it is possible to inject a piece of JavaScript that will handle necessary arrangements on the page's side.
Reply all
Reply to author
Forward
0 new messages