private JavaScriptPlugin(PluginProcess pluginProcess, object plugin, JavaScriptPluginAttribute pluginAttribute) { NativeObject = plugin; _pluginId = pluginAttribute.Name; _callbackThread = pluginAttribute.CallbackThread; if (string.IsNullOrEmpty(_pluginId)) { _isDynamic = true; _pluginId = Guid.NewGuid().ToString(); } var pluginType = NativeObject.GetType(); _methods = new ConcurrentDictionary <string, MethodInfo>( GetParagonMembers(pluginType.GetMethods)); _events = new ConcurrentDictionary <string, JavaScriptEvent>(GetParagonMembers(pluginType.GetEvents) .Where(m => m.Value.EventHandlerType == typeof(JavaScriptPluginCallback)) .ToDictionary(m => m.Key, m => new JavaScriptEvent(NativeObject, m.Value))); _properties = new ConcurrentDictionary <string, PropertyInfo>(GetParagonMembers(pluginType.GetProperties)); if (_isDynamic) { var disposedEvent = pluginType .GetEvents(BindingFlags.Instance | BindingFlags.Public) .FirstOrDefault(e => { var a = e.GetCustomAttributes(typeof(JavaScriptDisposeAttribute), true); return(a.Length > 0); }); if (disposedEvent != null && disposedEvent.EventHandlerType == typeof(JavaScriptPluginCallback)) { _disposedEvent = disposedEvent; _disposedHandler = OnNativeObjectDisposed; _disposedEvent.AddEventHandler(NativeObject, _disposedHandler); } } foreach (var nativeEvent in _events.Values) { nativeEvent.AttachToEvent(); } Descriptor = new PluginDescriptor { PluginId = _pluginId, Methods = _methods.Select(method => new MethodDescriptor { MethodName = method.Key, HasCallbackParameter = HasCallbackParameter(method.Value), IsVoid = method.Value.ReturnType == typeof(void) }).ToList(), Events = _events.Keys.ToList() }; }
public static void Raise(this JavaScriptPluginCallback handler, Func <object[]> getArgs) { JavaScriptPluginCallback local = Interlocked.CompareExchange(ref handler, null, null); if (local != null) { local(getArgs()); } }
public static void Raise(this JavaScriptPluginCallback handler) { JavaScriptPluginCallback local = Interlocked.CompareExchange(ref handler, null, null); if (local != null) { local(); } }
public void Create(string startUrl, CreateWindowOptions options, JavaScriptPluginCallback callback) { Logger.Info(string.Format("Create window : {0}", startUrl)); var windowManager = Application.WindowManager as IApplicationWindowManagerEx; if (windowManager != null) { windowManager.CreateWindow(new CreateWindowRequest(startUrl, options, callback)); } }
public JavaScriptEvent(object nativeObject, EventInfo eventInfo) { ThrowIfDisposed(); if (eventInfo == null) { throw new ArgumentNullException("eventInfo"); } _nativeObject = nativeObject; _eventInfo = eventInfo; _handler = OnEventFired; }
public void ShowEditHotKeysDialog(EditHotKeyDialogArgs dialogArgs) { var mainWindow = (Window)this.application.WindowManager.AllWindows[0]; var showDialog = new Action(() => { var viewModel = new HotKeyConfigurationViewModel(); var window = new HotKeyConfigurationWindow(); EventHandler <RequestSaveEventArgs> requestSaveHandler = (sender, args) => { JavaScriptPluginCallback handler = HotKeysEdited; if (handler != null) { handler(new object[] { args.isHotKeyEnabled, args.selectedModifier, args.keys }); } window.Close(); }; EventHandler requestCloseHandler = (sender, args) => window.Close(); viewModel.RequestSave += requestSaveHandler; viewModel.RequestClose += requestCloseHandler; viewModel.IsHotKeyEnabled = dialogArgs.IsEnabled; viewModel.SelectedModifier = (ModifierKeys)Enum.Parse(typeof(ModifierKeys), dialogArgs.Modifier); viewModel.Keys = (Keys)Enum.Parse(typeof(Keys), dialogArgs.Key); window.DataContext = viewModel; window.Closing += (sender, args) => { viewModel.RequestSave -= requestSaveHandler; viewModel.RequestClose -= requestCloseHandler; }; window.Owner = mainWindow; window.ShowDialog(); }); if (!mainWindow.Dispatcher.CheckAccess()) { mainWindow.Dispatcher.Invoke(showDialog); } else { showDialog.Invoke(); } }
public void Capture(JavaScriptPluginCallback onComplete) { this.onComplete = onComplete; var activeWindow = Application.FindWindow(PluginExecutionContext.BrowserIdentifier); var window = activeWindow.Unwrap(); Action toInvoke = () => { var snippingWindow = new SnippingWindow(); snippingWindow.Closing += OnClosing; snippingWindow.Owner = window; snippingWindow.Show(); }; window.Dispatcher.Invoke(toInvoke); }
public CreateWindowRequest( string startUrl, CreateWindowOptions options, JavaScriptPluginCallback windowCreatedCallback, string id = null) { _stopwatch = AutoStopwatch.TimeIt("Creating app window"); RequestId = id; if (string.IsNullOrEmpty(id)) { RequestId = Guid.NewGuid().ToString(); } StartUrl = startUrl; Options = options; if (windowCreatedCallback != null) { _callbackReference = new WeakReference(windowCreatedCallback, true); _windowCreatedCallback = windowCreatedCallback; } }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public void Dispose() { if (_disposed) { return; } _disposed = true; if (_disposedEvent != null && _disposedHandler != null) { _disposedEvent.RemoveEventHandler(NativeObject, _disposedHandler); } NativeObject = null; _disposedHandler = null; _methods.Clear(); _properties.Clear(); var events = _events.Values.ToList(); _events.Clear(); events.ForEach(e => e.Dispose()); }
public FileDialogCallback(JavaScriptPluginCallback callback) { _callback = callback; }
private Action CreateInvoker(IPluginManager pluginManager, int browserId, long frameId, int contextId, string methodName, IJavaScriptParameters parameters, IJavaScriptPluginCallback callback) { ThrowIfDisposed(); if (parameters == null) { throw new ArgumentNullException("parameters"); } MethodInfo method; if (!_methods.TryGetValue(methodName, out method)) { if (callback != null) { var msg = string.Format("Error executing {0} on plugin {1} - method not found", methodName, _pluginId); InvokeReturnCallback(callback, null, 0, msg); } } var nativeObject = NativeObject; if (nativeObject == null) { if (callback != null) { var msg = string.Format("Error executing {0} on plugin {1} - plugin object has been disposed", methodName, _pluginId); InvokeReturnCallback(callback, null, 0, msg); } } object[] arguments; var hasCallbackParameter = HasCallbackParameter(method); var methodParams = method.GetParameters(); var parameterDefinitions = hasCallbackParameter ? methodParams.Take(methodParams.Length - 1).ToArray() : methodParams; try { arguments = parameters.GetConvertedParameters(parameterDefinitions, pluginManager); if (hasCallbackParameter) { // Create a new args array with length + 1 so we can add the callback param to it. var args = new object[arguments.Length + 1]; Array.Copy(arguments, args, arguments.Length); // Extract the callback and wrap it. JavaScriptPluginCallback callbackParam = null; if (callback != null) { var parameterCallback = callback.GetParameterCallback(); if (parameterCallback != null) { callbackParam = parameterCallback.Invoke; } } // Add the wrapped callback to the args list. args[args.Length - 1] = callbackParam; arguments = args; } } catch (Exception ex) { Logger.Error("Error converting plugin invocation parameters: " + ex); if (callback != null) { InvokeReturnCallback(callback, null, -1, ex.Message); } return(null); } var invoke = new Action( () => { using (PluginExecutionContext.Create(browserId, contextId, frameId)) { object result = null; var errorCode = 0; var error = string.Empty; try { result = method.Invoke(nativeObject, arguments); } catch (Exception e) { while (e is TargetInvocationException) { e = e.InnerException; } errorCode = -1; error = e.Message; Logger.Error("Error executing plugin method: " + e); } if (callback != null) { InvokeReturnCallback(callback, result, errorCode, error); } } }); return(invoke); }