Module: cordovaHost

Cordova host implementation.

Version:
  • $$Id: cordovaHost.js 1365 2017-04-17 00:44:19Z chadlia.jerad $$
Author:
  • Chadlia Jerad, Edward A. Lee, Marten Lohstroh, Elizabeth Osyk, Victor Nouvellet, Matt Weber
Source:

Members

(inner) _accessorPath

Relative path (from www) to the accessors directory.

Source:

(inner) _includePath

Relative path (from www) to the include directory.

Source:

(inner) _loadedAccessors

Cached accessors.

Source:

(inner) _loadedModules

Cached modules.

Source:

(inner) _modulesCommonPath

Relative path (from www) to the common modules directory.

Source:

(inner) _modulesLocalPath

Relative path (from www) to the local modules directory.

Source:

(inner) _rootStr

Full path to the root (www).

Source:

Methods

(inner) getAccessorCode(path) → {string}

Return the source of an accessor definition from the accessor library on the host. The ".js" extension of the accessor class is optional. This is a blocking call.

Parameters:
Name Type Description
path string

The path on the server for the JavaScript code, e.g. 'net/REST'.

Source:
Returns:

The source code of the accessor.

Type
string

(inner) getHostName()

Return the name of this host.

Return the string "Cordova".

Source:
Returns:

In cordovaHost.js, return "Cordova".

(inner) getJavaScript(path, callback)

Return the source code of a JavaScript file. Append the string '.js' to the specified path (if it is not already there) and issue an HTTP GET request with the specified relative path. If no callback function is given, then this is a blocking request. If a callback is given, then the response will be passed as the second argument of the callback. The first argument is used to pass an error message in case the operation was unsuccessful (and will be null otherwise).

Parameters:
Name Type Description
path string

The relative path on the server.

callback function

The callback function.

Source:

(inner) getResource(uri, options, callback)

Get a resource using XMLHttpRequest for remote files and window.resolveLocalFileSystemURL for local files.

The reason for using a different function for local files is subtle, and has to do with how mobile apps are sandboxed differently than web apps. XMLHttpRequest will load local files, but is restricted by the domain name and top level domain name of the current URI, which can change from the app's www directory if the user navigates to another page of the app and interfere with file loading. Not only does window.resolveLocalFileSystemURL not have this issue it provides access to other files in the app sandbox which may not be in the app's www directory. For example both Android and iOS provide persistant and private data storage (see https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/) outside of the app's www directory.

Unlike other hosts, this implementation is exclusively asynchronous. Although getJavaScript above shows synchronous loading, this is to be avoided because a synchronous call to getResource during execution would make the swarmlet freeze. Therefore a null callback argument will generate an error.

  • $KEYSTORE is replaced with _root + "keystore/" This means each cordova swarmlet on a phone is assumed to have its own app-specific read-only keystore.

    FIXME: complete this implementation for more resource types such as binary files (see https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html#store-an-existing-binary-file-) WARNING: the encoding parameter in options is currently ignored FIXME: print file errors with more informative messages than "[object]".

Below are the types of resources that are handled all other resources will cause an error.

  • Text (UTF-8)
Parameters:
Name Type Description
uri string

A specification for the resource. If the uri starts with http or https, this function will attempt to load the resource from the web. If the uri is an absolute path on mobile (starts with file:///) it will attempt to load it locally. If the uri is a relative path, this function will attempt to load it relative to this app's www directory (located at _root).

options string

FXIME: Until more than UTF-8 resources are supported, encoding inputs are ignored is ignored. The below documentation is copied from the GetResource accessor. The options parameter may have the following values:

  • If the type of the options parameter is a Number, then it is assumed to be the timeout in milliseconds. Timeout defaults to 5000ms.
  • If the type of the options parameter is a String, then it is assumed to be the encoding, for example "UTF-8". If the value is "Raw" or "raw" then the data is returned as an unsigned array of bytes. The default encoding is the default encoding of the system.
  • If the type of the options parameter is an Object, then it may have the following fields: encoding The encoding of the file, see above for values. timeout {number} The timeout in milliseconds.
callback

A callback function which will be called with two arguments: 1) The first argument is null if the resource was successfully acquired and a non-null error code if wasn't. The error message will be an http status message https://www.w3schools.com/tags/ref_httpmessages.asp if a web resource was sought, and a cordova-plugin-file error code (see https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/#list-of-error-codes-and-meanings) if a local resource was sought. 2) The second argument is the desired resource, if successfully acquired and null otherwise.

Source:

(inner) instantiate(accessorName, accessorClass) → {object}

Instantiate and return an accessor. This will throw an exception if there is no such accessor class on the accessor search path.

Parameters:
Name Type Description
accessorName string

The name to give to the instance.

accessorClass string

Fully qualified accessor class name, e.g. 'net/REST'.

Source:
Returns:

An accessor instance.

Type
object

(inner) loadModule(id) → {boolean}

Return true if the module was loaded successfully, false otherwise. If the code was loaded before, do nothing, otherwise, retrieve it.

Parameters:
Name Type Description
id string

Identifier for the module.

Source:
Returns:

Whether the module was loaded successfully.

Type
boolean

(inner) require(id) → {object}

This require function is similar to Node's implementation (module.js). If the given identifier specifies a full path then try to use that path. Otherwise, attempt to load a local module. If that fails, attempt to load a global module. If that also fails, use the native require function implemented by Cordova.

Parameters:
Name Type Description
id string

Identifier for the module.

Source:
See:
Returns:

The modules exports.

Type
object