Source: CEPEngine_extensions.js

/**************************************************************************************************
 *
 * ADOBE SYSTEMS INCORPORATED
 * Copyright 2013 Adobe Systems Incorporated
 * All Rights Reserved.
 *
 * NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the
 * terms of the Adobe license agreement accompanying it.  If you have received this file from a
 * source other than Adobe, then your use, modification, or distribution of it requires the prior
 * written permission of Adobe.
 *
 **************************************************************************************************/

/** CEPEngine - v8.0.0 */
// This is the JavaScript code for bridging to native functionality
// Note: So far all native file i/o functions are synchronous, and aynchronous file i/o is TBD

/**
 * The value of the "err" property returned from CEP functions
 * 
 * Possible error codes and their corresponding meanings:
 *  <ul>
 *      <li>0 - NO_ERROR</li>
 *      <li>1 - ERR_UNKNOWN</li>
 *      <li>2 - ERR_INVALID_PARAMS</li>
 *      <li>3 - ERR_NOT_FOUND</li>
 *      <li>4 - ERR_CANT_READ</li>
 *      <li>5 - ERR_UNSUPPORTED_ENCODING</li>
 *      <li>6 - ERR_CANT_WRITE</li>
 *      <li>7 - ERR_OUT_OF_SPACE</li>
 *      <li>8 - ERR_NOT_FILE</li>
 *      <li>9 - ERR_NOT_DIRECTORY</li>
 *      <li>10 - ERR_FILE_EXISTS</li>
 *      <li>101 - ERR_EXCEED_MAX_NUM_PROCESS</li>
 *      <li>201 - ERR_INVALID_URL</li>
 *      <li>202 - DEPRECATED_API</li>
 *  </ul>
 * @typedef {number} [ErrorCode]
 */

/**
 * Initializes the CEPEngine to utilize CEF capabilities.
 * @namespace cep
 */

/**
 * Communicates with the computer's native file system
 * @namespace cep.fs
 * @memberof cep
 */

/**
 * Reads and/or initializes processes
 * @namespace cep.process
 * @memberof cep
 */

/**
 * Encodes/decodes text and data to/from Base64
 * @namespace cep.encoding
 * @memberof cep
 */

/**
 * Communicates with OS browser and internet
 * @namespace cep.util
 * @memberof cep
 */

var cep;
if (!cep) {
    cep = {};
}
if (!cep.fs) {
    cep.fs = {};
}
if (!cep.process) {
    cep.process = {};
}
if (!cep.encoding) {
    cep.encoding = {};
}
if (!cep.util) {
    cep.util = {};
}

(function () {
    'use strict';
    // Internal function to get the last error code.
    //        native function GetLastError();
    function getLastError() {
        return GetLastError();
    }

    function getErrorResult() {
        var result = {
            err: getLastError()
        };
        return result;
    }

    // Error values. These MUST be in sync with the error values
    // at the top of CEPEngine_extensions.cpp

    /**
     * @constant {ErrorCode}
     * @readonly
     * @memberof cep.fs
     */
    cep.fs.NO_ERROR = 0;

    /**
     * @constant {ErrorCode}
     * @readonly
     * @memberof cep.fs
     */
    cep.fs.ERR_UNKNOWN = 1;

    /**
     * @constant {ErrorCode}
     * @readonly
     * @memberof cep.fs
     */
    cep.fs.ERR_INVALID_PARAMS = 2;

    /**
     * @constant {ErrorCode}
     * @readonly
     * @memberof cep.fs
     */
    cep.fs.ERR_NOT_FOUND = 3;

    /**
     * @constant {ErrorCode}
     * @readonly
     * @memberof cep.fs
     */
    cep.fs.ERR_CANT_READ = 4;

    /**
     * @constant {ErrorCode}
     * @readonly
     * @memberof cep.fs
     */
    cep.fs.ERR_UNSUPPORTED_ENCODING = 5;

    /**
     * @constant {ErrorCode}
     * @readonly
     * @memberof cep.fs
     */
    cep.fs.ERR_CANT_WRITE = 6;

    /**
     * @constant {ErrorCode}
     * @readonly
     * @memberof cep.fs
     */
    cep.fs.ERR_OUT_OF_SPACE = 7;

    /**
     * @constant {ErrorCode}
     * @readonly
     * @memberof cep.fs
     */
    cep.fs.ERR_NOT_FILE = 8;

    /**
     * @constant {ErrorCode}
     * @readonly
     * @memberof cep.fs
     */
    cep.fs.ERR_NOT_DIRECTORY = 9;

    /**
     * @constant {ErrorCode}
     * @readonly
     * @memberof cep.fs                       
     */
    cep.fs.ERR_FILE_EXISTS = 10;

    /**
     * @constant {ErrorCode}
     * @readonly
     * @memberof cep.process
     */
    cep.process.ERR_EXCEED_MAX_NUM_PROCESS = 101;

    /**
     * @constant {ErrorCode}
     * @readonly
     * @memberof cep.process                      
     */
    cep.util.ERR_INVALID_URL = 201;

    /**
     * @constant {ErrorCode}
     * @readonly
     * @memberof cep.util           
     */
    cep.util.DEPRECATED_API = 202;

    /**
     * @constant {string}
     * @default
     * @memberof cep.encoding
     */
    cep.encoding.UTF8 = "UTF-8";

    /**
     * @constant {string}
     * @default
     * @memberof cep.encoding
     */
    cep.encoding.Base64 = "Base64";

    /**
     * Displays the OS File Open dialog, allowing the user to select files or directories.
     * @function
     * @param {boolean} allowMultipleSelection When true, multiple files/folders can be selected.
     * @param {boolean} chooseDirectory When true, only folders can be selected. When false, only
     *        files can be selected.
     * @param {string} title Title of the open dialog.
     * @param {string} initialPath Initial path to display in the dialog. Pass NULL or "" to
     *        display the last path chosen.
     * @param {string[]} fileTypes The file extensions (without the dot) for the types
     *      of files that can be selected. Ignored when chooseDirectory=true.
     *
     * @return {(object|ErrorCode)} An object with these properties:
     *      <ul>
     *          <li>"data": An array of the full names of the selected files.</li>
     *          <li>"err": The status of the operation, one of
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *              </ul>
     *          </li>
     *      </ul>
     * @memberof cep.fs
     *               
     * @example
     * // Display images chosen from showOpenDialog() in <img> tags
     * 
     *    var cepEngine = window.cep.fs,
     *        $imgBttn = $('button'),
     *        $imgDiv = $('.results'), 
     *        fileTypes = ["gif", "jpg", "jpeg", "png", "bmp", "webp", "svg"],
     *        choices = cepEngine.showOpenDialog(true, false, 'Open References', '', fileTypes),
     *        images = choices.data; // The array of files selected by the user
     *    function openImages() {
     *      if (images.length) {
     *          for (var i = 0; i < images.length; i++) {
     *               var imgurl = images[i];
     *               $imgDiv.append("<img src='" + imgurl + "'/>");
     *           }
     *       } else {
     *           return false;
     *       }
     *   }
     *   $imgBttn.click(openImages);
     **/
    //    native function ShowOpenDialog();
    cep.fs.showOpenDialog = function (allowMultipleSelection, chooseDirectory, title, initialPath, fileTypes) {
        var resultString = ShowOpenDialog(allowMultipleSelection, chooseDirectory,
            title || 'Open', initialPath || '',
            fileTypes ? fileTypes.join(' ') : '');

        var result = {
            data: JSON.parse(resultString || '[]'),
            err: getLastError()
        };
        return result;
    };

    /**
     * Displays the OS File Open dialog, allowing the user to select files or directories.
     * @function
     * @param {boolean} allowMultipleSelection When true, multiple files/folders can be selected.
     * @param {boolean} chooseDirectory When true, only folders can be selected. When false, only
     *        files can be selected.
     * @param {string} title Title of the open dialog.
     * @param {string} initialPath Initial path to display in the dialog. Pass NULL or "" to
     *        display the last path chosen.
     * @param {string[]} fileTypes The file extensions (without the dot) for the types
     *      of files that can be selected. Ignored when chooseDirectory=true.
     * @param {string} friendlyFilePrefix String to put in front of the extensions
     *      of files that can be selected. Ignored when chooseDirectory=true. (win only)
     * @param {string} prompt String for OK button (mac only, default is "Open" on mac, "Open" or "Select Folder" on win).
     *                        
     * @return {(object|ErrorCode)} An object with these properties:
     *      <ul><li>"data": An array of the full names of the selected files.</li>
     *          <li>"err": The status of the operation, one of
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *              </ul>
     *          </li>
     *      </ul>
     * @memberof cep.fs
     *
     * @example 
     * // Example filetypes and prefixes
     *    var cepEngine = window.cep.fs,
     *        $imgBttn = $('button'),
     *        $imgDiv = $('.results'), 
     *      fileTypes = ["gif", "jpg", "jpeg", "png", "bmp", "webp", "svg"],
     *      friendlyFilePrefix = "Images (*.gif;*.jpg;*.jpeg;*.png;*.bmp;*.webp;*.svg)",
     *      choices = cepEngine.showOpenDialogEx(true, false, 'Open References', '', fileTypes, friendlyFilePrefix),
     *      images = choices.data; // The array of files selected by the user
     *    function openImages() {
     *      if (images.length) {
     *          for (var i = 0; i < images.length; i++) {
     *               var imgurl = images[i];
     *               $imgDiv.append("<img src='" + imgurl + "'/>");
     *           }
     *       } else {
     *           return false;
     *       }
     *   }
     *   $imgBttn.click(openImages);
     **/
    //    native function ShowOpenDialogEx();
    cep.fs.showOpenDialogEx = function (allowMultipleSelection, chooseDirectory, title, initialPath, fileTypes,
        friendlyFilePrefix, prompt) {
        var resultString = ShowOpenDialogEx(allowMultipleSelection, chooseDirectory,
            title || 'Open', initialPath || '',
            fileTypes ? fileTypes.join(' ') : '', friendlyFilePrefix || '',
            prompt || '');

        var result = {
            data: JSON.parse(resultString || '[]'),
            err: getLastError()
        };
        return result;
    };

    /**
     * Displays the OS File Save dialog, allowing the user to type in a file name.
     * @function
     * @param {string} title Title of the save dialog.
     * @param {string} initialPath Initial path to display in the dialog. Pass NULL or "" to
     *        display the last path chosen.
     * @param {string[]} fileTypes The file extensions (without the dot) for the types
     *      of files that can be selected.
     * @param {string} defaultName String to start with for the file name.
     * @param {string} friendlyFilePrefix String to put in front of the extensions of files that can be selected. (win only)
     * @param {string} prompt String for Save button (mac only, default is "Save" on mac and win).
     * @param {string} nameFieldLabel String displayed in front of the file name text field (mac only, "File name:" on win).
     *
     * @return {(object|ErrorCode)} An object with these properties:
     *      <ul><li>"data": The file path selected to save at or "" if canceled</li>
     *          <li>"err": The status of the operation, one of
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *              </ul>
     *          </li>
     *      </ul>
     *      @memberof cep.fs
     * @example 
     * // Example filetypes and prefixes
     *          var fileTypes = ["gif", "jpg", "jpeg", "png", "bmp", "webp", "svg"];
     *          var friendlyFilePrefix = "Images (*.gif;*.jpg;*.jpeg;*.png;*.bmp;*.webp;*.svg)";
     **/
    //    native function ShowSaveDialogEx();
    cep.fs.showSaveDialogEx = function (title, initialPath, fileTypes, defaultName, friendlyFilePrefix, prompt, nameFieldLabel) {
        var resultString = ShowSaveDialogEx(title || '', initialPath || '',
            fileTypes ? fileTypes.join(' ') : '', defaultName || '',
            friendlyFilePrefix || '', prompt || '', nameFieldLabel || '');

        var result = {
            data: resultString || '',
            err: getLastError()
        };
        return result;
    };

    /**
     * Reads the contents of a folder.
     * @function
     * @param {string} path The path of the folder to read.
     *
     * @return {(object|ErrorCode)} An object with these properties:
     *      <ul>
     *          <li>"data": An array of the names of the contained files (excluding '.' and '..'.</li>
     *          <li>"err": The status of the operation, one of:
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_UNKNOWN</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *                  <li>ERR_NOT_FOUND</li>
     *                  <li>ERR_CANT_READ</li>
     *              </ul>
     *          </li>
     *          </ul>
     *          @memberof cep.fs
     **/
    //    native function ReadDir();
    cep.fs.readdir = function (path) {
        var resultString = ReadDir(path);
        var result = {
            data: JSON.parse(resultString || '[]'),
            err: getLastError()
        };
        return result;
    };

    /**
     * Creates a new folder.
     * @function
     * @param {string} path The path of the folder to create.
     *
     * @return {(object|ErrorCode)} An object with this property:
     *      <ul>
     *          <li>"err": The status of the operation, one of:
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_UNKNOWN</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *              </ul>
     *          </li>
     *      </ul>
     *         @memberof cep.fs
     **/
    //     native function MakeDir();
    cep.fs.makedir = function (path) {
        MakeDir(path);
        return getErrorResult();
    };

    /**
     * Renames a file or folder.
     * @function
     * @param {string} oldPath The old name of the file or folder.
     * @param {string} newPath The new name of the file or folder.
     * @return {(object|ErrorCode)} An object with this property:
     *      <ul>
     *          <li>"err": The status of the operation, one of:
     *          <ul>
     *              <li>NO_ERROR</li>
     *              <li>ERR_UNKNOWN</li>
     *              <li>ERR_INVALID_PARAMS</li>
     *              <li>ERR_NOT_FOUND</li>
     *              <li>ERR_FILE_EXISTS</li>
     *          </ul>
     *          </li>
     *      </ul>
     * @memberof cep.fs
     **/
    //     native function Rename();
    cep.fs.rename = function (oldPath, newPath) {
        Rename(oldPath, newPath);
        return getErrorResult();
    };

    /**
     * Reports whether an item is a file or folder.
     * @function
     * @param {string} path The path of the file or folder.
     * @return {(object|ErrorCode)} An object with these properties:
     *      <ul>
     *          <li>"data": An object with properties
     *              <ul>
     *                  <li>isFile (boolean)</li>
     *                  <li>isDirectory (boolean)</li>
     *                  <li>mtime (modification DateTime)</li>
     *              </ul>
     *          </li>
     *          <li>"err": The status of the operation, one of
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_UNKNOWN</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *                  <li>ERR_NOT_FOUND</li>
     *              </ul>
     *          </li>
     *      </ul>
     * @memberof cep.fs
     **/
    //    native function IsDirectory();
    //    native function GetFileModificationTime();
    cep.fs.stat = function (path) {
        var isDir = IsDirectory(path);
        var modtime = GetFileModificationTime(path);
        var result = {
            data: {
                isFile: function () {
                    return !isDir;
                },
                isDirectory: function () {
                    return isDir;
                },
                mtime: modtime
            },
            err: getLastError()
        };

        return result;
    };

    /**
     * Reads the entire contents of a file.
     * @function
     * @param {string} path The path of the file to read.
     * @param {string} encoding The encoding of the contents of file, one of
     *      UTF8 (the default) or Base64.
     *
     * @return {(object|ErrorCode)} An object with these properties:
     *      <ul>
     *          <li>"data": The file contents. </li>
     *          <li>"err": The status of the operation, one of
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_UNKNOWN</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *                  <li>ERR_NOT_FOUND</li>
     *                  <li>ERR_CANT_READ</li>
     *                  <li>ERR_UNSUPPORTED_ENCODING</li>
     *              </ul>
     *          </li>
     *      </ul>
     *      @memberof cep.fs
     **/
    //    native function ReadFile();
    cep.fs.readFile = function (path, encoding) {
        encoding = encoding ? encoding : cep.encoding.UTF8;
        var contents = ReadFile(path, encoding);
        var result = {
            data: contents,
            err: getLastError()
        };
        return result;
    };

    /**
     * Writes data to a file, replacing the file if it already exists.
     * @function
     * @param {string} path The path of the file to write.
     * @param {string} data The data to write to the file.
     * @param {string} encoding  The encoding of the contents of file, one of
     *      UTF8 (the default) or Base64.
     *
     * @return {(object|ErrorCode)} An object with this property:
     *      <ul>
     *          <li>"err": The status of the operation, one of:
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_UNKNOWN</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *                  <li>ERR_UNSUPPORTED_ENCODING</li>
     *                  <li>ERR_CANT_WRITE</li>
     *                  <li>ERR_OUT_OF_SPACE</li>
     *              </ul>
     *          </li>
     *      </ul>
     **/
    //    native function WriteFile();
    cep.fs.writeFile = function (path, data, encoding) {
        encoding = encoding ? encoding : cep.encoding.UTF8;
        WriteFile(path, data, encoding);
        return getErrorResult();
    };

    /**
     * Sets permissions for a file or folder.
     *
     * @param {string} path The path of the file or folder.
     * @param {number} mode The permissions in numeric format (for example, 0777).
     *
     * @return {(object|ErrorCode)} An object with this property:
     *      <ul>
     *          <li>"err": The status of the operation, one of:
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_UNKNOWN</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *                  <li>ERR_CANT_WRITE</li>
     *              </ul>
     *          </li>
     *      </ul>
     *          @memberof cep
     **/
    //    native function SetPosixPermissions();
    cep.fs.chmod = function (path, mode) {
        SetPosixPermissions(path, mode);
        return getErrorResult();
    };

    /**
     * Deletes a file.
     * @function
     * @param {string} path The path of the file to delete.
     *
     * @return {(object|ErrorCode)} An object with this property:
     *      <ul>
     *          <li>"err": The status of the operation, one of:
     *              <li>NO_ERROR</li>
     *              <li>ERR_UNKNOWN</li>
     *              <li>ERR_INVALID_PARAMS</li>
     *              <li>ERR_NOT_FOUND</li>
     *              <li>ERR_NOT_FILE</li>
     *          </li>
     *      </ul>
     * @memberof cep.process
     **/
    //    native function DeleteFileOrDirectory();
    //    native function IsDirectory();
    cep.fs.deleteFile = function (path) {
        if (IsDirectory(path)) {
            var result = {
                err: cep.fs.ERR_NOT_FILE
            };
            return result;
        }
        DeleteFileOrDirectory(path);
        return getErrorResult();
    };

    /**
     * Creates a process.
     * @function
     * @param {list} arguments The arguments to create process. The first one is the full path of the executable, followed by the arguments of the executable.
     *
     * @return {(object|ErrorCode)} An object with these properties:
     *      <ul>
     *          <li>"data": The pid of the process, or -1 on error. </li>
     *          <li>"err": The status of the operation, one of
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_UNKNOWN</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *                  <li>ERR_EXCEED_MAX_NUM_PROCESS</li>
     *                  <li>ERR_NOT_FOUND</li>
     *                  <li>ERR_NOT_FILE</li>
     *              </ul>
     *          </li>
     *      </ul>
     *      @memberof cep.process
     **/
    //    native function CreateProcess();
    cep.process.createProcess = function () {
        var args = Array.prototype.slice.call(arguments);
        var pid = CreateProcess(args);
        var result = {
            data: pid,
            err: getLastError()
        };
        return result;
    };

    /**
     * Registers a standard-output handler for a process.
     * @function
     * @param {number} pid The pid of the process.
     * @param {function} callback The handler function for the standard output callback.
     *
     * @return {(object|ErrorCode)} An object with this property:
     *      <ul>
     *          <li>"err": The status of the operation, one of:
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_UNKNOWN</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *                  <li>ERR_INVALID_PROCESS_ID</li>
     *              </ul>    
     *          </li>
     *      </ul>
     * @memberof cep.process
     **/
    //    native function SetupStdOutHandler();
    cep.process.stdout = function (pid, callback) {
        SetupStdOutHandler(pid, callback);
        return getErrorResult();
    };

    /**
     * Registers up a standard-error handler for a process.
     * @function
     * @param {number} pid The pid of the process.
     * @param {function} callback The handler function for the standard error callback.
     *
     * @return {(object|ErrorCode)} An object with this property:
     *      <ul>
     *          <li>"err": The status of the operation, one of:
     *               <ul>   
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_UNKNOWN</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *                  <li>ERR_INVALID_PROCESS_ID</li>
     *              </ul>
     *          </li>
     *      </ul>
     * @memberof cep.process
     **/
    //    native function SetupStdErrHandler();
    cep.process.stderr = function (pid, callback) {
        SetupStdErrHandler(pid, callback);
        return getErrorResult();
    };

    /**
     * Writes data to the standard input of a process.
     * @function
     * @param {number} pid  The pid of the process
     * @param {string} data The data to write.
     *
     * @return {(object|ErrorCode)} An object with this property:
     *      <ul>
     *          <li>"err": The status of the operation, one of:
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_UNKNOWN</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *                  <li>ERR_INVALID_PROCESS_ID</li>
     *              </ul>
     *          </li>
     *      </ul>
     * @memberof cep.process
     **/
    //    native function WriteStdIn();
    cep.process.stdin = function (pid, data) {
        WriteStdIn(pid, data);
        return getErrorResult();
    };

    /**
     * Retrieves the working directory of a process.
     * @function
     * @param {number} pid The pid of the process.
     *
     * @return {(object|ErrorCode)} An object with these properties:
     *      <ul>
     *          <li>"data": The path of the working directory. </li>
     *          <li>"err": The status of the operation, one of
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_UNKNOWN</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *                  <li>ERR_INVALID_PROCESS_ID</li>
     *              </ul>
     *          </li>
     *      </ul>
     * @memberof cep.process
     **/
    //    native function GetWorkingDirectory();
    cep.process.getWorkingDirectory = function (pid) {
        var wd = GetWorkingDirectory(pid);
        var result = {
            data: wd,
            err: getLastError()
        };
        return result;
    };

    /**
     * Waits for a process to quit.
     * @function
     * @param {number} pid The pid of the process.
     *
     * @return {(object|ErrorCode)} An object with this property:
     *      <ul>
     *          <li>"err": The status of the operation, one of:
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_UNKNOWN</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *                  <li>ERR_INVALID_PROCESS_ID</li>
     *              </ul>
     *          </li>
     *      </ul>
     * @memberof cep.process
     **/
    //    native function WaitFor();
    cep.process.waitfor = function (pid) {
        WaitFor(pid);
        return getErrorResult();
    };

    /**
     * Registers a handler for the onquit callback of a process.
     * @function
     * @param {number} pid  The pid of the process.
     * @param {function} callback The handler function.
     *
     * @return {(object|ErrorCode)} An object with this property:
     *      <ul>
     *          <li>"err": The status of the operation, one of:
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_UNKNOWN</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *                  <li>ERR_INVALID_PROCESS_ID</li>
     *              </ul>
     *          </li>
     *      </ul>
     * @memberof cep.process
     **/
    //    native function OnQuit();
    cep.process.onquit = function (pid, callback) {
        OnQuit(pid, callback);
        return getErrorResult();
    };

    /**
     * Reports whether a process is currently running.
     * @function
     * @param {number} pid The pid of the process.
     *
     * @return {(object|ErrorCode)} An object with these properties:
     *      <ul>
     *          <li>"data": True if the process is running, false otherwise. </li>
     *          <li>"err": The status of the operation, one of
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_UNKNOWN</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *                  <li>ERR_INVALID_PROCESS_ID</li>
     *              </ul>
     *          </li>
     *          </ul>
     * @memberof cep.process
     **/
    //    native function IsRunning();
    cep.process.isRunning = function (pid) {
        var isRunning = IsRunning(pid);
        var result = {
            data: isRunning,
            err: getLastError()
        };
        return result;
    };

    /**
     * Terminates a process.
     * @function
     * @param {number} pid The process ID
     *
     * @return {(object|ErrorCode)} An object with this property:
     *      <ul>
     *          <li>"err": The status of the operation, one of:
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_UNKNOWN</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *                  <li>ERR_INVALID_PROCESS_ID</li>
     *              </ul>
     *          </li>
     *      </ul>
     * @memberof cep.process
     **/
    //    native function Terminate();
    cep.process.terminate = function (pid) {
        Terminate(pid);
        return getErrorResult();
    };

    /**
     * Encoding/decoding conversions.
     * @namespace
     * @memberof cep.encoding
     */
    cep.encoding.convertion = {
        /**
         * UTF-8 to Base64 conversion
         * @param   {string} str The string to convert
         * @return {string} A string representation bytes of encoded ASCII data (one character represents a single byte.)
         */

        utf8_to_b64: function (str) {
            return window.btoa(unescape(encodeURIComponent(str)));
        },

        /**
         * Base64 to UTF-8 conversion
         * If a base64 string contains any whitespace character, DOM Exception 5 occurs during window.atob, please see
         * {@link http://stackoverflow.com/questions/14695988/dom-exception-5-invalid-character-error-on-valid-base64-image-string-in-javascri|this stackoverflow answer} for more information.
         * @param   {string}   base64str The string to convert
         * @return {string} A string representation the decoded UTF=8 data
         */

        b64_to_utf8: function (base64str) {
            base64str = base64str.replace(/\s/g, '');
            return decodeURIComponent(escape(window.atob(base64str)));
        },

        /**
         * Binary to Base64 encoded ASCII conversion
         * @param   {string} binary A string in which each character represents a byte of binary data to encoded
         * @returns {string} A string containing the Base64 representation
         */

        binary_to_b64: function (binary) {
            return window.btoa(binary);
        },

        /**
         * Base64 to Binary conversion
         * @param   {string} base64str Base64 data to be converted
         * @returns {string} Data in binary format
         */

        b64_to_binary: function (base64str) {
            return window.atob(base64str);
        },

        /**
         * ASCII to Base64 conversion
         * @param   {string} ascii A string representing ASCII data
         * @returns {string} The ASCII data as a string
         */

        ascii_to_b64: function (ascii) {
            return window.btoa(binary);
        },

        /**
         * Base64 to ASCII conversion
         * @param   {string} base64str Base64 data as a string
         * @returns {string} A string representation of the ASCII data
         */

        b64_to_ascii: function (base64str) {
            return window.atob(base64str);
        }
    };

    /**
     * Opens a page in the default system browser.
     * @function
     * @param {string} url The URL of the page/file to open, or the email address.
     * Must use HTTP/HTTPS/file/mailto.
     *
     * @return {(object|ErrorCode)} An object with this property:
     *      <ul>
     *          <li>"err": The status of the operation, one of:
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_UNKNOWN</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *              </ul>
     *          </li>
     *      </ul>
     * @memberof cep.util
     * @example
     *  "http://www.adobe.com"
     *  "https://github.com"
     *  "file:///C:/log.txt"
     *  "mailto:test@adobe.com"
     **/
    //    native function OpenURLInDefaultBrowser();
    cep.util.openURLInDefaultBrowser = function (url) {
        if (url && (url.indexOf("http://") === 0 ||
                url.indexOf("https://") === 0 ||
                url.indexOf("file://") === 0 ||
                url.indexOf("mailto:") === 0)) {
            OpenURLInDefaultBrowser(url);
            return getErrorResult();
        } else {
            return {
                err: cep.util.ERR_INVALID_URL
            };
        }
    };

    /**
     * Registers a callback function for extension unload. If called more than once,
     * the last callback that is successfully registered is used.
     * @function
     * @deprecated since version 6.0.0
     *
     * @param {function} callback The handler function.
     *
     * @return {(object|ErrorCode)} An object with this property:
     *      <ul>
     *          <li>"err": The status of the operation, one of:
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *              </ul>
     *          </li>
     *      </ul>
     * @memberof cep.util
     **/
    //    native function RegisterExtensionUnloadCallback();
    cep.util.registerExtensionUnloadCallback = function (callback) {
        return {
            err: cep.util.DEPRECATED_API
        };
    };

    /**
     * Stores the user's proxy credentials
     * @function
     * @param {string} username proxy username
     * @param {string} password proxy password
     *
     * @return {(object|ErrorCode)} An object with this property:
     *      <ul>
     *          <li>"err": The status of the operation, one of
     *              <ul>
     *                  <li>NO_ERROR</li>
     *                  <li>ERR_INVALID_PARAMS</li>
     *              </ul>
     *          </li>
     *      </ul>
     * @memberof cep.util
     **/
    //    native function StoreProxyCredentials();
    cep.util.storeProxyCredentials = function (username, password) {
        StoreProxyCredentials(username, password);
        return getErrorResult();
    };

})();