/// <summary> /// Retrieves command call parameters and creates wrapper for them /// </summary> /// <param name="commandStr">Command string in the form 'service/action/callback/args'</param> /// <returns>New class instance or null of string does not represent Cordova command</returns> /// public static CordovaCommandCall Parse(string commandStr) { if (string.IsNullOrEmpty(commandStr)) { return(null); //throw new ArgumentNullException("commandStr"); } string[] split = commandStr.Split('/'); if (split.Length < 3) { return(null); } CordovaCommandCall commandCallParameters = new CordovaCommandCall(); commandCallParameters.Service = split[0]; commandCallParameters.Action = split[1]; commandCallParameters.CallbackId = split[2]; //commandCallParameters.Args = split.Length <= 3 ? String.Empty : String.Join("/", split.Skip(3)); try { string arg = split.Length <= 3 ? "[]" : String.Join("/", split.Skip(3)); if (!arg.StartsWith("[")) // save the exception { arg = string.Format("[{0}]", arg); } List <string> args = JSON.JsonHelper.Deserialize <List <string> >(arg); args.Add(commandCallParameters.CallbackId); commandCallParameters.Args = JSON.JsonHelper.Serialize(args.ToArray()); } catch (Exception) { return(null); } // sanity check for illegal names // was failing with :: // CordovaCommandResult :: 1, Device1, {"status":1,"message":"{\"name\":\"XD..... if (commandCallParameters.Service.IndexOfAny(new char[] { '@', ':', ',', '!', ' ' }) > -1) { return(null); } return(commandCallParameters); }
/* * This method does the work of routing commands * NotifyEventArgs.Value contains a string passed from JS * If the command already exists in our map, we will just attempt to call the method(action) specified, and pass the args along * Otherwise, we create a new instance of the command, add it to the map, and call it ... * This method may also receive JS error messages caught by window.onerror, in any case where the commandStr does not appear to be a valid command * it is simply output to the debugger output, and the method returns. * **/ private void GapBrowser_ScriptNotify(object sender, NotifyEventArgs e) { string commandStr = e.Value; if (commandStr.IndexOf("DOMStorage") == 0) { this.domStorageHelper.HandleStorageCommand(commandStr); return; } CordovaCommandCall commandCallParams = CordovaCommandCall.Parse(commandStr); if (commandCallParams == null) { // ERROR Debug.WriteLine("ScriptNotify :: " + commandStr); } else { this.nativeExecution.ProcessCommand(commandCallParams); string AlertVisible = "false"; var UserSetting = Windows.Storage.ApplicationData.Current.LocalSettings; AlertVisible = UserSetting.Values["IsAlertVisible"].ToString(); if (AlertVisible == "false") { ViewWebView(); } if (AlertVisible == "true") { HideWebView(); } } }
/// <summary> /// Executes command and returns result back. /// </summary> /// <param name="commandCallParams">Command to execute</param> public void ProcessCommand(CordovaCommandCall commandCallParams) { if (commandCallParams == null) { throw new ArgumentNullException("commandCallParams"); } try { BaseCommand bc = CommandFactory.CreateByServiceName(commandCallParams.Service); if (bc == null) { this.OnCommandResult(commandCallParams.CallbackId, new PluginResult(PluginResult.Status.CLASS_NOT_FOUND_EXCEPTION)); return; } EventHandler <PluginResult> OnCommandResultHandler = delegate(object o, PluginResult res) { Debug.WriteLine("I am in onCommandResultHandler for calling onCommandResult Method"); //this.OnCommandResult(commandCallParams.CallbackId, res); if (res.CallbackId == null || res.CallbackId == commandCallParams.CallbackId) { this.OnCommandResult(commandCallParams.CallbackId, res); if (!res.KeepCallback) { bc.RemoveResultHandler(commandCallParams.CallbackId); } } }; //bc.OnCommandResult += OnCommandResultHandler; bc.AddResultHandler(commandCallParams.CallbackId, OnCommandResultHandler); EventHandler <ScriptCallback> OnCustomScriptHandler = delegate(object o, ScriptCallback script) { this.InvokeScriptCallback(script); }; bc.OnCustomScript += OnCustomScriptHandler; try { bc.InvokeMethodNamed(commandCallParams.CallbackId, commandCallParams.Action, commandCallParams.Args); } catch (Exception ex) { Debug.WriteLine("ERROR: Exception in ProcessCommand :: " + ex.Message); bc.OnCommandResult -= OnCommandResultHandler; bc.OnCustomScript -= OnCustomScriptHandler; Debug.WriteLine("ERROR: failed to InvokeMethodNamed :: " + commandCallParams.Action + " on Object :: " + commandCallParams.Service); this.OnCommandResult(commandCallParams.CallbackId, new PluginResult(PluginResult.Status.INVALID_ACTION)); return; } } catch (Exception ex) { // ERROR Debug.WriteLine(String.Format("ERROR: Unable to execute command :: {0}:{1}:{3} ", commandCallParams.Service, commandCallParams.Action, ex.Message)); this.OnCommandResult(commandCallParams.CallbackId, new PluginResult(PluginResult.Status.ERROR)); return; } }