// Copyright © 2019 The CefSharp Authors. All rights reserved. // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. using System; using CefSharp.ModelBinding; namespace CefSharp { /// /// Calling CefSharp.PostMessage in Javascript triggers the JavascriptMessageReceived /// This event args contains the frame, browser and message corrisponding to that call /// public class JavascriptMessageReceivedEventArgs : EventArgs { private static IBinder Binder = new DefaultBinder(); /// /// The frame that called CefSharp.PostMessage in Javascript /// public IFrame Frame { get; private set; } /// /// The browser that hosts the /// public IBrowser Browser { get; private set; } /// /// Message can be a primative type or a simple object that represents a copy /// of the data sent from the browser /// public object Message { get; private set; } /// /// Constructor. /// /// The browser that hosts the /// The frame that called CefSharp.PostMessage in Javascript. /// Message can be a primative type or a simple object that represents a copy of the data sent from the /// browser. public JavascriptMessageReceivedEventArgs(IBrowser browser, IFrame frame, object message) { Browser = browser; Frame = frame; Message = message; } /// /// Converts the to a specific type using the /// that CefSharp provides /// /// Type /// Type public T ConvertMessageTo() { if (Message == null) { return default(T); } return (T)Binder.Bind(Message, typeof(T)); } /// /// Provide a custom instance of /// that will be used when /// is called. You may wish to provide a custom instance in cases where you /// wish to override the name conversion. /// e.g. You wish to convert names from camelCase /// /// binder instance /// /// JavascriptMessageReceivedEventArgs.SetBinder(new DefaultBinder(new CamelCaseJavascriptNameConverter())); /// public static void SetBinder(IBinder binder) { if (binder == null) { throw new ArgumentNullException(nameof(binder)); } Binder = binder; } } }