forked from sdesalas/trifleJS
/
Callback.cs
183 lines (174 loc) · 6.49 KB
/
Callback.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using System.Web.Script.Serialization;
using Noesis.Javascript;
namespace TrifleJS
{
public class Callback
{
/// <summary>
/// Allows callback from C# middleware to the V8 JavaScript Runtime.
/// Deletes the original callback function.
/// </summary>
/// <param name="id">Callback id</param>
/// <param name="arguments">any arguments to pass to the callback</param>
/// <returns></returns>
public static bool ExecuteOnce(string id, params object[] arguments)
{
return Execute(id, true, arguments);
}
/// <summary>
/// Allows callback from C# middleware to the V8 JavaScript Runtime.
/// Keeps the original callback function to allow multiple execution.
/// </summary>
/// <param name="id">Callback id</param>
/// <param name="arguments">any arguments to pass to the callback</param>
/// <returns></returns>
public static bool Execute(string id, params object[] arguments)
{
return Execute(id, false, arguments);
}
/// <summary>
/// Executes a callback
/// </summary>
public static bool Execute(string id, bool once, params object[] arguments)
{
try
{
if (arguments == null) { arguments = new object[0]; }
String cmd = String.Format(@"trifle.Callback.{0}('{1}', [{2}]);",
once ? "executeOnce" : "execute",
id,
String.Join(",", Parse(arguments))
);
Program.context.Run(cmd, "Callback#" + id);
}
catch (Exception ex) {
API.Context.Handle(ex);
return false;
}
return true;
}
/// <summary>
/// Parses input/output to make it JavaScript friendly
/// </summary>
/// <param name="arguments">an array of argument objects (of any type)</param>
/// <returns>list of parsed arguments</returns>
public static string[] Parse(params object[] arguments) {
List<string> input = new List<string>();
foreach (object argument in arguments)
{
input.Add(ParseOne(argument));
}
return input.ToArray();
}
/// <summary>
/// Parses input/output tomake it JavaScript friendly
/// </summary>
/// <param name="argument">an argument object (of any type)</param>
/// <returns>the parsed argument</returns>
public static string ParseOne(object argument) {
if (argument == null)
{
return "null";
}
else
{
switch (argument.GetType().Name)
{
case "Int32":
case "Double":
return argument.ToString();
case "Boolean":
return argument.ToString().ToLowerInvariant();
case "String":
// Fix for undefined (coming up as null)
if ("{{undefined}}".Equals(argument))
{
return "undefined";
}
else
{
return String.Format("\"{0}\"", argument.ToString());
}
default:
return Utils.Serialize(argument);
}
}
}
/// <summary>
/// Allows callbacks from IE to C# and the V8 Javascript Runtime,
/// PLEASE NOTE: Debugging inside this class is likely to
/// make IE loose focus, causing unexpected behaviour.
/// </summary>
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public class External
{
/// <summary>
/// Initialises the ObjectForScripting class
/// </summary>
/// <param name="page">A page where scripts are being executed</param>
public External(API.Modules.WebPage page) {
this.page = page;
}
public API.Modules.WebPage page;
/// <summary>
/// Outputs a debug message
/// </summary>
/// <param name="message"></param>
public void xdebug(string message) {
API.Console.xdebug(message);
}
/// <summary>
/// Performs a callback in V8 environment
/// </summary>
/// <param name="id"></param>
public void doCallback(string id)
{
Callback.Execute(id);
}
/// <summary>
/// Handles javascript prompt() functionality.
/// </summary>
/// <param name="message"></param>
public object dialog(string dialog, string message, string defaultValue) {
try
{
// Execute in V8 engine and return result
object result = Program.context.Run(
String.Format("WebPage.onDialog({0}, {1}, {2})", ParseOne(dialog), ParseOne(message), ParseOne(defaultValue)),
"WebPage.onDialog()"
);
return result;
} catch (Exception ex) {
API.Context.Handle(ex);
}
return null;
}
/// <summary>
/// Passes control over to page.onCallback() function (if initialized).
/// </summary>
/// <param name="jsonArray"></param>
/// <returns></returns>
public object callPhantom(string jsonArray) {
try
{
// Execute in V8 engine and return result
object result = Program.context.Run(
String.Format("WebPage.onCallback({0});", jsonArray),
"WebPage.onCallback()"
);
return result;
}
catch (Exception ex) {
API.Context.Handle(ex);
}
return null;
}
}
}
}