diff --git a/README.md b/README.md
index 00e2b7b..668e2b6 100644
--- a/README.md
+++ b/README.md
@@ -18,7 +18,7 @@ cordova plugin add nodejs-mobile-cordova
## Reporting Issues
-We have a [central repo](https://github.com/janeasystems/nodejs-mobile/issues) where we manage all the issues related to Node.js for Mobile Apps, including specific issues of the Node.js for Mobile Cordova plugin.
+We have a [central repo](https://github.com/janeasystems/nodejs-mobile/issues) where we manage all the issues related to Node.js for Mobile Apps, including specific issues of the Node.js for Mobile Apps Cordova plugin.
So please, open the issue [there](https://github.com/janeasystems/nodejs-mobile/issues).
## Cordova Methods
@@ -32,26 +32,52 @@ So please, open the issue [there](https://github.com/janeasystems/nodejs-mobile/
### nodejs.start
```js
- nodejs.start(scriptFileName, callback);
+ nodejs.start(scriptFileName, callback [, options]);
```
+| Param | Type |
+| --- | --- |
+| scriptFileName | string
|
+| callback | function
|
+| options | [StartupOptions](#cordova.StartupOptions)
|
+
### nodejs.startWithScript
```js
- nodejs.startWithScript(scriptBody, callback);
+ nodejs.startWithScript(scriptBody, callback [, options]);
```
+| Param | Type |
+| --- | --- |
+| scriptBody | string
|
+| callback | function
|
+| options | [StartupOptions](#cordova.StartupOptions)
|
### nodejs.channel.setListener
```js
nodejs.channel.setListener(listenerCallback);
```
+| Param | Type |
+| --- | --- |
+| listenerCallback | function
|
### nodejs.channel.send
```js
nodejs.channel.send(message);
```
+| Param | Type |
+| --- | --- |
+| message | string
|
+
+
+### StartupOptions: object
+| Name | Type | Default | Description |
+| --- | --- | --- | --- |
+| redirectOutputToLogcat | boolean
| true
| Allows to disable the redirection of the Node stdout/stderr to the Android logcat |
+
+Note: the stdout/stderr redirection is applied to the whole application, the side effect is that some undesired/duplicated output may appear in the logcat.
+For example, the Chromium console output `I/chromium: [INFO:CONSOLE(xx)]` is also sent to stderr and will show up in logcat has well, with the `NODEJS-MOBILE` log tag.
## Node.js Methods
@@ -67,13 +93,19 @@ So please, open the issue [there](https://github.com/janeasystems/nodejs-mobile/
```
cordova.channel.send(message);
```
+| Param | Type |
+| --- | --- |
+| message | string
|
### cordova.channel.on
```
cordova.channel.on('message', listnerCallback);
```
-
+| Param | Type |
+| --- | --- |
+| 'message' | const string
|
+| listnerCallback | function
|
## Usage
@@ -111,8 +143,7 @@ Switch to Xcode:
* enlarge the `Console` area, at the end of the console log it should show:
```
-2017-10-02 18:49:18.606100+0200 HelloCordova[2182:1463518] NodeJs Engine Started
+2017-10-02 18:49:18.606100+0200 HelloCordova[2182:1463518] Node.js Mobile Engine Started
[node] received: Hello from Cordova!
2017-10-02 18:49:18.690132+0200 HelloCordova[2182:1463518] [cordova] received: Replying to this message: Hello from Cordova!
```
-
diff --git a/install/hooks/ios/after-plugin-install.js b/install/hooks/ios/after-plugin-install.js
index b529e20..0ccaf02 100644
--- a/install/hooks/ios/after-plugin-install.js
+++ b/install/hooks/ios/after-plugin-install.js
@@ -15,8 +15,8 @@ module.exports = function(context) {
xcodeProject.parseSync();
var firstTargetUUID = xcodeProject.getFirstTarget().uuid;
- //Adds a build phase to rebuild native modules
- var rebuildNativeModulesBuildPhaseName = 'Build NodeJS Mobile Native Modules';
+ // Adds a build phase to rebuild native modules.
+ var rebuildNativeModulesBuildPhaseName = 'Build Node.js Mobile Native Modules';
var rebuildNativeModulesBuildPhaseScript = `
set -e
if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then
@@ -55,8 +55,8 @@ popd
);
}
- //Adds a build phase to sign native modules
- var signNativeModulesBuildPhaseName = 'Sign NodeJS Mobile Native Modules';
+ // Adds a build phase to sign native modules.
+ var signNativeModulesBuildPhaseName = 'Sign Node.js Mobile Native Modules';
var signNativeModulesBuildPhaseScript = `
set -e
if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then
diff --git a/install/hooks/ios/before-plugin-uninstall.js b/install/hooks/ios/before-plugin-uninstall.js
index db297cb..a2a1666 100644
--- a/install/hooks/ios/before-plugin-uninstall.js
+++ b/install/hooks/ios/before-plugin-uninstall.js
@@ -4,7 +4,7 @@ var fs = require('fs');
module.exports = function(context) {
var xcode = context.requireCordovaModule('xcode');
- //Adds a custom function to remove script build phases, which is not supported on cordova's Xcode module yet.
+ // Adds a custom function to remove script build phases, which is not supported on cordova's Xcode module yet.
xcode.project.prototype.myRemovePbxScriptBuildPhase = function (buildPhaseName, target) {
var buildPhaseTargetUuid = target || this.getFirstTarget().uuid;
@@ -14,7 +14,7 @@ module.exports = function(context) {
throw new Error("Couldn't find the build script phase to remove: " + buildPhaseName );
}
- // remove the '_comment' suffix to get the actual uuid
+ // Remove the '_comment' suffix to get the actual uuid.
var buildPhaseUuid=buildPhaseUuid_comment.split('_')[0];
// Remove from the pbxBuildPhaseObjects
@@ -49,15 +49,15 @@ module.exports = function(context) {
xcodeProject.parseSync();
var firstTargetUUID = xcodeProject.getFirstTarget().uuid;
- //Removes the build phase to rebuild native modules
- var rebuildNativeModulesBuildPhaseName = 'Build NodeJS Mobile Native Modules';
+ // Removes the build phase to rebuild native modules.
+ var rebuildNativeModulesBuildPhaseName = 'Build Node.js Mobile Native Modules';
var rebuildNativeModulesBuildPhase = xcodeProject.buildPhaseObject('PBXShellScriptBuildPhase', rebuildNativeModulesBuildPhaseName, firstTargetUUID);
if (rebuildNativeModulesBuildPhase) {
xcodeProject.myRemovePbxScriptBuildPhase(rebuildNativeModulesBuildPhaseName, firstTargetUUID);
}
- //Removes the build phase to sign native modules
- var signNativeModulesBuildPhaseName = 'Sign NodeJS Mobile Native Modules';
+ // Removes the build phase to sign native modules.
+ var signNativeModulesBuildPhaseName = 'Sign Node.js Mobile Native Modules';
var signNativeModulesBuildPhase = xcodeProject.buildPhaseObject('PBXShellScriptBuildPhase', signNativeModulesBuildPhaseName, firstTargetUUID);
if (signNativeModulesBuildPhase) {
xcodeProject.myRemovePbxScriptBuildPhase(signNativeModulesBuildPhaseName, firstTargetUUID);
diff --git a/install/nodejs-mobile-cordova-assets/builtin_modules/cordova-bridge/package.json b/install/nodejs-mobile-cordova-assets/builtin_modules/cordova-bridge/package.json
index 7fb42bb..96c08d9 100644
--- a/install/nodejs-mobile-cordova-assets/builtin_modules/cordova-bridge/package.json
+++ b/install/nodejs-mobile-cordova-assets/builtin_modules/cordova-bridge/package.json
@@ -1,7 +1,7 @@
{
"name": "cordova-bridge",
"version": "0.1.0",
- "description": "NodeJS for Mobile Native Bridge",
+ "description": "Node.js for Mobile Apps Native Bridge",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
diff --git a/install/sample-project/www/js/index.js b/install/sample-project/www/js/index.js
index c22189e..ddeecf4 100644
--- a/install/sample-project/www/js/index.js
+++ b/install/sample-project/www/js/index.js
@@ -29,18 +29,21 @@ var app = {
app.initialize();
function channelListener(msg) {
- console.log("[cordova] received:", msg);
-}
-
+ console.log('[cordova] received: ' + msg);
+};
+
+function startupCallback(err) {
+ if (err) {
+ console.log(err);
+ } else {
+ console.log ('Node.js Mobile Engine Started');
+ nodejs.channel.send('Hello from Cordova!');
+ }
+};
+
function startNodeProject() {
-nodejs.channel.setListener(channelListener);
-nodejs.start("main.js",
- function(err) {
- if (err) {
- console.log(err);
- } else {
- console.log ("NodeJs Engine Started");
- nodejs.channel.send("Hello from Cordova!");
- }
- });
-}
+ nodejs.channel.setListener(channelListener);
+ nodejs.start('main.js', startupCallback);
+ // To disable the stdout/stderr redirection to the Android logcat:
+ // nodejs.start('main.js', startupCallback, { redirectOutputToLogcat: false });
+};
diff --git a/install/sample-project/www/nodejs-project/package.json b/install/sample-project/www/nodejs-project/package.json
index be300bc..f4dd2de 100644
--- a/install/sample-project/www/nodejs-project/package.json
+++ b/install/sample-project/www/nodejs-project/package.json
@@ -1,7 +1,7 @@
{
"name": "nodejs-mobile-sample-project",
"version": "0.1.0",
- "description": "NodeJS for Mobile sample project",
+ "description": "Node.js for Mobile Apps sample project",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
diff --git a/plugin.xml b/plugin.xml
index dcb3de1..78ed9fb 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -4,8 +4,8 @@
id="nodejs-mobile-cordova"
version="0.1.2">
- NodeJS Mobile
- NodeJS for Mobile Cordova Plugin
+ Node.js Mobile
+ Node.js for Mobile Apps Cordova Plugin
MIT
cordova,mobile,nodejs,node.js
@@ -35,8 +35,8 @@
-
-
+
+
@@ -69,9 +69,9 @@
-
-
-
+
+
+
diff --git a/src/android/CMakeLists.txt b/src/android/CMakeLists.txt
index 5d69248..cdb0e18 100644
--- a/src/android/CMakeLists.txt
+++ b/src/android/CMakeLists.txt
@@ -11,13 +11,13 @@ cmake_minimum_required(VERSION 3.4.1)
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
- native-module
+ nodejs-mobile-cordova-native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
- native-module.cpp
+ native-lib.cpp
cordova-bridge.cpp
)
@@ -54,7 +54,7 @@ find_library( # Sets the name of the path variable.
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
- native-module
+ nodejs-mobile-cordova-native-lib
libnode
diff --git a/src/android/java/com/janeasystems/cdvnodejsmobile/NodeJS.java b/src/android/java/com/janeasystems/cdvnodejsmobile/NodeJS.java
index 2ba8cb3..45f43c6 100644
--- a/src/android/java/com/janeasystems/cdvnodejsmobile/NodeJS.java
+++ b/src/android/java/com/janeasystems/cdvnodejsmobile/NodeJS.java
@@ -1,7 +1,15 @@
+/*
+ Node.js for Mobile Apps Cordova plugin.
+
+ Implements the plugin APIs exposed to the Cordova layer and routes messages
+ between the Cordova layer and the Node.js engine.
+ */
+
package com.janeasystems.cdvnodejsmobile;
import org.apache.cordova.*;
import org.json.JSONArray;
+import org.json.JSONObject;
import org.json.JSONException;
import android.util.Log;
@@ -37,18 +45,18 @@ public class NodeJS extends CordovaPlugin {
private long previousLastUpdateTime = 0;
private static boolean appPaused = false;
- private static String LOGTAG = "NodeJS-Cordova";
+ private static String LOGTAG = "NODEJS-CORDOVA";
private static boolean engineAlreadyStarted = false;
private static CallbackContext channelListenerContext = null;
static {
- System.loadLibrary("native-module");
+ System.loadLibrary("nodejs-mobile-cordova-native-lib");
System.loadLibrary("node");
}
- public native Integer startNodeWithArguments(String[] arguments, String nodePath);
+ public native Integer startNodeWithArguments(String[] arguments, String nodePath, boolean redirectOutputToLogcat);
public native void sendToNode(String msg);
public native String getCurrentABIName();
@@ -81,10 +89,12 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo
result = this.setChannelListener(callbackContext);
} else if (action.equals("startEngine")) {
String target = data.getString(0);
- result = this.startEngine(target, callbackContext);
+ JSONObject startOptions = data.getJSONObject(1);
+ result = this.startEngine(target, startOptions, callbackContext);
} else if (action.equals("startEngineWithScript")) {
String scriptBody = data.getString(0);
- result = this.startEngineWithScript(scriptBody, callbackContext);
+ JSONObject startOptions = data.getJSONObject(1);
+ result = this.startEngineWithScript(scriptBody, startOptions, callbackContext);
} else {
Log.e(LOGTAG, "Invalid action: " + action);
result = false;
@@ -137,7 +147,8 @@ private boolean setChannelListener(final CallbackContext callbackContext) {
return true;
}
- private boolean startEngine(String scriptFileName, final CallbackContext callbackContext) {
+ private boolean startEngine(final String scriptFileName, final JSONObject startOptions,
+ final CallbackContext callbackContext) {
Log.v(LOGTAG, "StartEngine: " + scriptFileName);
if (NodeJS.engineAlreadyStarted == true) {
@@ -158,6 +169,8 @@ private boolean startEngine(String scriptFileName, final CallbackContext callbac
return false;
}
+ final boolean redirectOutputToLogcat = getOptionRedirectOutputToLogcat(startOptions);
+
NodeJS.engineAlreadyStarted = true;
Log.v(LOGTAG, "Script absolute path: " + scriptFileAbsolutePath);
new Thread(new Runnable() {
@@ -165,7 +178,8 @@ private boolean startEngine(String scriptFileName, final CallbackContext callbac
public void run() {
startNodeWithArguments(
new String[]{"node", scriptFileAbsolutePath},
- NodeJS.nodePath);
+ NodeJS.nodePath,
+ redirectOutputToLogcat);
}
}).start();
@@ -173,7 +187,8 @@ public void run() {
return true;
}
- private boolean startEngineWithScript(String scriptBody, final CallbackContext callbackContext) {
+ private boolean startEngineWithScript(final String scriptBody, final JSONObject startOptions,
+ final CallbackContext callbackContext) {
Log.v(LOGTAG, "StartEngineWithScript: " + scriptBody);
boolean result = true;
String errorMsg = "";
@@ -183,6 +198,8 @@ private boolean startEngineWithScript(String scriptBody, final CallbackContext c
return false;
}
+ final boolean redirectOutputToLogcat = getOptionRedirectOutputToLogcat(startOptions);
+
final String scriptBodyToRun = new String(scriptBody);
Log.v(LOGTAG, "Script absolute path: " + scriptBody);
new Thread(new Runnable() {
@@ -190,7 +207,8 @@ private boolean startEngineWithScript(String scriptBody, final CallbackContext c
public void run() {
startNodeWithArguments(
new String[]{"node", "-e", scriptBodyToRun},
- NodeJS.nodePath);
+ NodeJS.nodePath,
+ redirectOutputToLogcat);
}
}).start();
@@ -442,4 +460,28 @@ private static boolean deleteFolderRecursively(File file) {
return false;
}
}
+
+ private static boolean getOptionRedirectOutputToLogcat(final JSONObject startOptions) {
+ if (BuildConfig.DEBUG) {
+ if (startOptions.names() != null) {
+ for (int i = 0; i < startOptions.names().length(); i++) {
+ try {
+ Log.v(LOGTAG, "Start engine option: " + startOptions.names().getString(i));
+ } catch (JSONException e) {
+ }
+ }
+ }
+ }
+
+ final String OPTION_NAME = "redirectOutputToLogcat";
+ boolean result = true;
+ if (startOptions.has(OPTION_NAME) == true) {
+ try {
+ result = startOptions.getBoolean(OPTION_NAME);
+ } catch(JSONException e) {
+ Log.e(LOGTAG, e.getMessage());
+ }
+ }
+ return result;
+ }
}
\ No newline at end of file
diff --git a/src/android/jni/cordova-bridge.cpp b/src/android/jni/cordova-bridge.cpp
deleted file mode 100644
index 01fce66..0000000
--- a/src/android/jni/cordova-bridge.cpp
+++ /dev/null
@@ -1,261 +0,0 @@
-#include "include/node/node_api.h"
-#include "include/node/uv.h"
-#include "cordova-bridge.h"
-#define NM_F_BUILTIN 0x1
-#include