Skip to content

Commit c54d100

Browse files
dpa99cjanpio
authored andcommitted
(iOS & Android) Add postMessage API support (#362)
<!-- Please make sure the checklist boxes are all checked before submitting the PR. The checklist is intended as a quick reference, for complete details please see our Contributor Guidelines: http://cordova.apache.org/contribute/contribute_guidelines.html Thanks! --> ### Platforms affected Android iOS (both UIWebView & WKWebView implementations) ### What does this PR do? Adds support for [postMessage API](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) enabling pages loaded into the InappBrowser to post messages back to the parent Webview of the Cordova app. For example, sending event messages associated with UI interactions such as button clicks from the wrapped page back to the parent app Webview. ### What testing has been done on this change? Automated tests have been extended to cover the `message` event. ### Checklist - [x ] Commit message follows the format: "GH-3232: (android) Fix bug with resolving file paths", where CB-xxxx is the JIRA ID & "android" is the platform affected. - [ x] Added automated test coverage as appropriate for this change.
1 parent 0fd43ae commit c54d100

File tree

7 files changed

+130
-21
lines changed

7 files changed

+130
-21
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ The object returned from a call to `cordova.InAppBrowser.open` when the target i
213213
- __loaderror__: event fires when the `InAppBrowser` encounters an error when loading a URL.
214214
- __exit__: event fires when the `InAppBrowser` window is closed.
215215
- __beforeload__: event fires when the `InAppBrowser` decides whether to load an URL or not (only with option `beforeload=yes`).
216+
- __message__: event fires when the `InAppBrowser` receives a message posted from the page loaded inside the `InAppBrowser` Webview.
216217

217218
- __callback__: the function that executes when the event fires. The function is passed an `InAppBrowserEvent` object as a parameter.
218219

@@ -238,6 +239,7 @@ function showHelp(url) {
238239

239240
inAppBrowserRef.addEventListener('beforeload', beforeloadCallBack);
240241

242+
inAppBrowserRef.addEventListener('message', messageCallBack);
241243
}
242244

243245
function loadStartCallBack() {
@@ -252,6 +254,13 @@ function loadStopCallBack() {
252254

253255
inAppBrowserRef.insertCSS({ code: "body{font-size: 25px;" });
254256

257+
inAppBrowserRef.executeScript({ code: "\
258+
var message = 'this is the message';\
259+
var messageObj = {my_message: message};\
260+
var stringifiedMessageObj = JSON.stringify(messageObj);\
261+
webkit.messageHandlers.cordova_iab.postMessage(stringifiedMessageObj);"
262+
});
263+
255264
$('#status-message').text("");
256265

257266
inAppBrowserRef.show();
@@ -300,18 +309,24 @@ function beforeloadCallback(params, callback) {
300309

301310
}
302311

312+
function messageCallback(params){
313+
$('#status-message').text("message received: "+params.data.my_message);
314+
}
315+
303316
```
304317

305318
### InAppBrowserEvent Properties
306319

307-
- __type__: the eventname, either `loadstart`, `loadstop`, `loaderror`, or `exit`. _(String)_
320+
- __type__: the eventname, either `loadstart`, `loadstop`, `loaderror`, `message` or `exit`. _(String)_
308321

309322
- __url__: the URL that was loaded. _(String)_
310323

311324
- __code__: the error code, only in the case of `loaderror`. _(Number)_
312325

313326
- __message__: the error message, only in the case of `loaderror`. _(String)_
314327

328+
- __data__: the message contents , only in the case of `message`. A stringified JSON object. _(String)_
329+
315330

316331
### Supported Platforms
317332

@@ -323,7 +338,11 @@ function beforeloadCallback(params, callback) {
323338

324339
### Browser Quirks
325340

326-
`loadstart` and `loaderror` events are not being fired.
341+
`loadstart`, `loaderror`, `message` events are not fired.
342+
343+
### Windows Quirks
344+
345+
`message` event is not fired.
327346

328347
### Quick Example
329348

@@ -344,6 +363,7 @@ function beforeloadCallback(params, callback) {
344363
- __loadstop__: event fires when the `InAppBrowser` finishes loading a URL.
345364
- __loaderror__: event fires when the `InAppBrowser` encounters an error loading a URL.
346365
- __exit__: event fires when the `InAppBrowser` window is closed.
366+
- __message__: event fires when the `InAppBrowser` receives a message posted from the page loaded inside the `InAppBrowser` Webview.
347367

348368
- __callback__: the function to execute when the event fires.
349369
The function is passed an `InAppBrowserEvent` object.

src/android/InAppBrowser.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Licensed to the Apache Software Foundation (ASF) under one
4848
import android.webkit.CookieManager;
4949
import android.webkit.CookieSyncManager;
5050
import android.webkit.HttpAuthHandler;
51+
import android.webkit.JavascriptInterface;
5152
import android.webkit.ValueCallback;
5253
import android.webkit.WebChromeClient;
5354
import android.webkit.WebSettings;
@@ -95,6 +96,7 @@ public class InAppBrowser extends CordovaPlugin {
9596
private static final String LOAD_START_EVENT = "loadstart";
9697
private static final String LOAD_STOP_EVENT = "loadstop";
9798
private static final String LOAD_ERROR_EVENT = "loaderror";
99+
private static final String MESSAGE_EVENT = "message";
98100
private static final String CLEAR_ALL_CACHE = "clearcache";
99101
private static final String CLEAR_SESSION_CACHE = "clearsessioncache";
100102
private static final String HARDWARE_BACK_BUTTON = "hardwareback";
@@ -952,8 +954,24 @@ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType)
952954
settings.setBuiltInZoomControls(showZoomControls);
953955
settings.setPluginState(android.webkit.WebSettings.PluginState.ON);
954956

957+
// Add postMessage interface
958+
class JsObject {
959+
@JavascriptInterface
960+
public void postMessage(String data) {
961+
try {
962+
JSONObject obj = new JSONObject();
963+
obj.put("type", MESSAGE_EVENT);
964+
obj.put("data", new JSONObject(data));
965+
sendUpdate(obj, true);
966+
} catch (JSONException ex) {
967+
LOG.e(LOG_TAG, "data object passed to postMessage has caused a JSON error.");
968+
}
969+
}
970+
}
971+
955972
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
956973
settings.setMediaPlaybackRequiresUserGesture(mediaPlaybackRequiresUserGesture);
974+
inAppWebView.addJavascriptInterface(new JsObject(), "cordova_iab");
957975
}
958976

959977
String overrideUserAgent = preferences.getString("OverrideUserAgent", null);
@@ -1270,6 +1288,11 @@ public void onPageStarted(WebView view, String url, Bitmap favicon) {
12701288
public void onPageFinished(WebView view, String url) {
12711289
super.onPageFinished(view, url);
12721290

1291+
// Set the namespace for postMessage()
1292+
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1){
1293+
injectDeferredObject("window.webkit={messageHandlers:{cordova_iab:cordova_iab}}", null);
1294+
}
1295+
12731296
// CB-10395 InAppBrowser's WebView not storing cookies reliable to local device storage
12741297
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
12751298
CookieManager.getInstance().flush();

src/ios/CDVUIInAppBrowser.m

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,16 @@ - (void)loadAfterBeforeload:(CDVInvokedUrlCommand*)command
337337
[self.inAppBrowserViewController navigateTo:url];
338338
}
339339

340+
-(void)createIframeBridge
341+
{
342+
// Create an iframe bridge in the new document to communicate with the CDVThemeableBrowserViewController
343+
NSString* jsIframeBridge = @"var e = _cdvIframeBridge=d.getElementById('_cdvIframeBridge'); if(!_cdvIframeBridge) {e = _cdvIframeBridge = d.createElement('iframe'); e.id='_cdvIframeBridge'; e.style.display='none'; d.body.appendChild(e);}";
344+
// Add the postMessage API
345+
NSString* jspostMessageApi = @"window.webkit={messageHandlers:{cordova_iab:{postMessage:function(message){_cdvIframeBridge.src='gap-iab://message/'+encodeURIComponent(message);}}}}";
346+
// Inject the JS to the webview
347+
[self.inAppBrowserViewController.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"(function(d){%@%@})(document)", jsIframeBridge, jspostMessageApi]];
348+
}
349+
340350
// This is a helper method for the inject{Script|Style}{Code|File} API calls, which
341351
// provides a consistent method for injecting JavaScript code into the document.
342352
//
@@ -348,9 +358,6 @@ - (void)loadAfterBeforeload:(CDVInvokedUrlCommand*)command
348358

349359
- (void)injectDeferredObject:(NSString*)source withWrapper:(NSString*)jsWrapper
350360
{
351-
// Ensure an iframe bridge is created to communicate with the CDVUIInAppBrowserViewController
352-
[self.inAppBrowserViewController.webView stringByEvaluatingJavaScriptFromString:@"(function(d){_cdvIframeBridge=d.getElementById('_cdvIframeBridge');if(!_cdvIframeBridge) {var e = _cdvIframeBridge = d.createElement('iframe');e.id='_cdvIframeBridge'; e.style.display='none';d.body.appendChild(e);}})(document)"];
353-
354361
if (jsWrapper != nil) {
355362
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:@[source] options:0 error:nil];
356363
NSString* sourceArrayString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
@@ -472,6 +479,22 @@ - (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*
472479
}
473480
[self.commandDelegate sendPluginResult:pluginResult callbackId:scriptCallbackId];
474481
return NO;
482+
}else if ([scriptCallbackId isEqualToString:@"message"] && (self.callbackId != nil)) {
483+
// Send a message event
484+
NSString* scriptResult = [url path];
485+
if ((scriptResult != nil) && ([scriptResult length] > 1)) {
486+
scriptResult = [scriptResult substringFromIndex:1];
487+
NSError* __autoreleasing error = nil;
488+
NSData* decodedResult = [NSJSONSerialization JSONObjectWithData:[scriptResult dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
489+
if (error == nil) {
490+
NSMutableDictionary* dResult = [NSMutableDictionary new];
491+
[dResult setValue:@"message" forKey:@"type"];
492+
[dResult setObject:decodedResult forKey:@"data"];
493+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dResult];
494+
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
495+
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
496+
}
497+
}
475498
}
476499
}
477500

@@ -513,6 +536,7 @@ - (void)webViewDidStartLoad:(UIWebView*)theWebView
513536

514537
- (void)webViewDidFinishLoad:(UIWebView*)theWebView
515538
{
539+
[self createIframeBridge];
516540
if (self.callbackId != nil) {
517541
// TODO: It would be more useful to return the URL the page is actually on (e.g. if it's been redirected).
518542
NSString* url = [self.inAppBrowserViewController.currentURL absoluteString];

src/ios/CDVWKInAppBrowser.m

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -555,23 +555,37 @@ - (void)userContentController:(nonnull WKUserContentController *)userContentCont
555555

556556
CDVPluginResult* pluginResult = nil;
557557

558-
NSDictionary* messageContent = (NSDictionary*) message.body;
559-
NSString* scriptCallbackId = messageContent[@"id"];
560-
561-
if([messageContent objectForKey:@"d"]){
562-
NSString* scriptResult = messageContent[@"d"];
563-
NSError* __autoreleasing error = nil;
564-
NSData* decodedResult = [NSJSONSerialization JSONObjectWithData:[scriptResult dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
565-
if ((error == nil) && [decodedResult isKindOfClass:[NSArray class]]) {
566-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:(NSArray*)decodedResult];
558+
if([message.body isKindOfClass:[NSDictionary class]]){
559+
NSDictionary* messageContent = (NSDictionary*) message.body;
560+
NSString* scriptCallbackId = messageContent[@"id"];
561+
562+
if([messageContent objectForKey:@"d"]){
563+
NSString* scriptResult = messageContent[@"d"];
564+
NSError* __autoreleasing error = nil;
565+
NSData* decodedResult = [NSJSONSerialization JSONObjectWithData:[scriptResult dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
566+
if ((error == nil) && [decodedResult isKindOfClass:[NSArray class]]) {
567+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:(NSArray*)decodedResult];
568+
} else {
569+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION];
570+
}
567571
} else {
568-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION];
572+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]];
573+
}
574+
[self.commandDelegate sendPluginResult:pluginResult callbackId:scriptCallbackId];
575+
}else if(self.callbackId != nil){
576+
// Send a message event
577+
NSString* messageContent = (NSString*) message.body;
578+
NSError* __autoreleasing error = nil;
579+
NSData* decodedResult = [NSJSONSerialization JSONObjectWithData:[messageContent dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
580+
if (error == nil) {
581+
NSMutableDictionary* dResult = [NSMutableDictionary new];
582+
[dResult setValue:@"message" forKey:@"type"];
583+
[dResult setObject:decodedResult forKey:@"data"];
584+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dResult];
585+
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
586+
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
569587
}
570-
} else {
571-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:@[]];
572588
}
573-
574-
[self.commandDelegate sendPluginResult:pluginResult callbackId:scriptCallbackId];
575589
}
576590

577591
- (void)didStartProvisionalNavigation:(WKWebView*)theWebView

tests/tests.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
var cordova = require('cordova');
2525
var isWindows = cordova.platformId === 'windows';
2626
var isIos = cordova.platformId === 'ios';
27+
var isAndroid = cordova.platformId === 'android';
2728
var isBrowser = cordova.platformId === 'browser';
2829

2930
window.alert = window.alert || navigator.notification.alert;
@@ -151,6 +152,32 @@ exports.defineAutoTests = function () {
151152
done();
152153
});
153154
});
155+
156+
it('inappbrowser.spec.7 should support message event', function (done) {
157+
if (!isAndroid && !isIos) {
158+
return pending(cordova.platformId + ' platform doesn\'t support message event');
159+
}
160+
var messageKey = 'my_message';
161+
var messageValue = 'is_this';
162+
iabInstance = cordova.InAppBrowser.open(url, '_blank', platformOpts);
163+
iabInstance.addEventListener('message', function (evt) {
164+
// Verify message event
165+
expect(evt).toBeDefined();
166+
expect(evt.type).toEqual('message');
167+
expect(evt.data).toBeDefined();
168+
expect(evt.data[messageKey]).toBeDefined();
169+
expect(evt.data[messageKey]).toEqual(messageValue);
170+
done();
171+
});
172+
iabInstance.addEventListener('loadstop', function (evt) {
173+
var code = '(function(){\n' +
174+
' var message = {' + messageKey + ': "' + messageValue + '"};\n' +
175+
' webkit.messageHandlers.cordova_iab.postMessage(JSON.stringify(message));\n' +
176+
'})()';
177+
iabInstance.executeScript({ code: code });
178+
});
179+
180+
});
154181
});
155182
};
156183
if (isIos) {

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Copyright (c) Microsoft Open Technologies Inc
77
// Licensed under the MIT license.
88
// TypeScript Version: 2.3
9-
type channel = "loadstart" | "loadstop" | "loaderror" | "exit";
9+
type channel = "loadstart" | "loadstop" | "loaderror" | "exit" | "message";
1010

1111
interface Window {
1212
/**

www/inappbrowser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
'loadstop': channel.create('loadstop'),
3939
'loaderror': channel.create('loaderror'),
4040
'exit': channel.create('exit'),
41-
'customscheme': channel.create('customscheme')
41+
'customscheme': channel.create('customscheme'),
42+
'message': channel.create('message')
4243
};
4344
}
4445

0 commit comments

Comments
 (0)