From 1ecf2d57b0ac29dd8e25fc09d19ac401fff2ef20 Mon Sep 17 00:00:00 2001 From: Rich McNeary Date: Fri, 4 Dec 2015 16:01:26 -0500 Subject: [PATCH] Initial implementation complete. Add support for when an element node in the DOM gets focus. Also invoked when focus passes from an element node to an non-element node or nothing. --- .../CefAppUnmanagedWrapper.cpp | 28 +++++- CefSharp.Core/Internals/ClientAdapter.cpp | 33 +++++-- CefSharp.Example/RequestHandler.cs | 7 +- CefSharp/CefSharp.csproj | 2 + CefSharp/DomNode.cs | 88 +++++++++++++++++++ CefSharp/IDomNode.cs | 47 ++++++++++ CefSharp/IRequestHandler.cs | 4 +- 7 files changed, 200 insertions(+), 9 deletions(-) create mode 100644 CefSharp/DomNode.cs create mode 100644 CefSharp/IDomNode.cs diff --git a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp index dfa6611ea..cf0a45032 100644 --- a/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp +++ b/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp @@ -102,7 +102,33 @@ namespace CefSharp auto focusedNodeChangedMessage = CefProcessMessage::Create(kOnFocusedNodeChanged); auto list = focusedNodeChangedMessage->GetArgumentList(); - SetInt64(123, focusedNodeChangedMessage->GetArgumentList(), 0); + // Neded in the browser process to get the frame. + list->SetString(0, frame->GetName()); + + // The node will be empty if an element loses focus but another one + // doesn't gain focus. Only transfer information if the node is an + // element (it might be text). + if (node != nullptr && node->IsElement()) + { + // True when a node exists, false if it doesn't. + list->SetBool(1, true); + + // Store the tag name. + list->SetString(2, node->GetElementTagName()); + + // Transfer the attributes in a Dictionary. + auto attributes = CefDictionaryValue::Create(); + CefDOMNode::AttributeMap attributeMap; + node->GetElementAttributes(attributeMap); + for (auto iter : attributeMap) + attributes->SetString(iter.first, iter.second); + + list->SetDictionary(3, attributes); + } + else + { + list->SetBool(1, false); + } browser->SendProcessMessage(CefProcessId::PID_BROWSER, focusedNodeChangedMessage); } diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 82fd6f9d4..76d027627 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -996,13 +996,36 @@ namespace CefSharp if (name == kOnFocusedNodeChanged) { - auto v = GetInt64(argList, 0); - auto message = System::String::Format("{0} - {1}", gcnew String(kOnFocusedNodeChanged.c_str()), v); - System::Console::WriteLine(message); - auto handler = _browserControl->RequestHandler; if (handler != nullptr) - handler->OnFocusedNodeChanged(_browserControl, GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup())); + { + // 0: frame name (string) + // 1: is a node (bool) + // 2: tag name (string) + // 3: attributes (dictionary) + CefFrameWrapper frameWrapper(browser->GetFrame(argList->GetString(0))); + auto notEmpty = argList->GetBool(1); + if (notEmpty) + { + // Node information was passed from the render process. + auto argAttributes = argList->GetDictionary(3); + auto attributes = gcnew System::Collections::Generic::Dictionary(); + CefDictionaryValue::KeyList keys; + argAttributes->GetKeys(keys); + for (auto key : keys) + attributes->Add(gcnew String(key.c_str()), gcnew String(argAttributes->GetString(key).c_str())); + + auto node = gcnew DomNode(attributes); + node->TagName = gcnew String(argList->GetString(2).c_str()); + + handler->OnFocusedNodeChanged(_browserControl, GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup()), %frameWrapper, node); + } + else + { + // Node information was not provided. + handler->OnFocusedNodeChanged(_browserControl, GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup()), %frameWrapper, nullptr); + } + } } else if (name == kEvaluateJavascriptResponse || name == kJavascriptCallbackResponse) { diff --git a/CefSharp.Example/RequestHandler.cs b/CefSharp.Example/RequestHandler.cs index 9dea4f685..f42e4fe1e 100644 --- a/CefSharp.Example/RequestHandler.cs +++ b/CefSharp.Example/RequestHandler.cs @@ -178,9 +178,12 @@ namespace CefSharp.Example return false; } - void IRequestHandler.OnFocusedNodeChanged (IWebBrowser browserControl, IBrowser browser) + void IRequestHandler.OnFocusedNodeChanged (IWebBrowser browserControl, IBrowser browser, IFrame frame, IDomNode node) { - Console.WriteLine ("OnFocusedNodeChanged"); + if (node != null) + Console.WriteLine ("OnFocusedNodeChanged() - " + node.ToString ()); + else + Console.WriteLine ("OnFocusedNodeChanged() - lost focus"); } } } diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index e0aac335a..f4f02d31b 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -77,8 +77,10 @@ + + diff --git a/CefSharp/DomNode.cs b/CefSharp/DomNode.cs new file mode 100644 index 000000000..c2d327ece --- /dev/null +++ b/CefSharp/DomNode.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; + +namespace CefSharp +{ + /// + /// Represents a node in the browser's DOM. + /// + public class DomNode : IDomNode + { + private IDictionary _attributes = null; + + public DomNode (IDictionary attributes) + { + _attributes = attributes; + } + + public override string ToString () + { + var sb = new StringBuilder (); + if (_attributes != null) + { + foreach (var pair in _attributes) + sb.AppendFormat ("{0}{1}:'{2}'", 0 < sb.Length ? ", " : String.Empty, pair.Key, pair.Value); + } + + if (!String.IsNullOrWhiteSpace (TagName)) + sb.Insert (0, String.Format ("{0} ", TagName)); + + if (sb.Length < 1) + return base.ToString (); + + return sb.ToString (); + } + + public string this[string name] + { + get + { + if (_attributes == null || _attributes.Count < 1 || !_attributes.ContainsKey (name)) + return null; + + return _attributes[name]; + } + } + + public string TagName + { + get; + set; + } + + public ReadOnlyCollection AttributeNames + { + get + { + if (_attributes == null) + return new ReadOnlyCollection (new List ()); + + return Array.AsReadOnly (_attributes.Keys.ToArray ()); + } + } + + public bool HasAttribute (string attributeName) + { + if (_attributes == null) + return false; + + return _attributes.ContainsKey (attributeName); + } + + public IEnumerator> GetEnumerator () + { + if (_attributes == null) + return new Dictionary ().GetEnumerator (); + + return _attributes.GetEnumerator (); + } + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () + { + return GetEnumerator (); + } + } +} diff --git a/CefSharp/IDomNode.cs b/CefSharp/IDomNode.cs new file mode 100644 index 000000000..37877bc8a --- /dev/null +++ b/CefSharp/IDomNode.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; + +namespace CefSharp +{ + /// + /// Represents the data belonging to a node in the browser's DOM. + /// + public interface IDomNode : IEnumerable> + { + /// + /// Get the value of an attribute. + /// + /// + /// The name of the attribute value to get. + /// + /// + /// The attribute value if the name exists in the DomNode's attributes. + /// Null if the name does not exist. + /// + string this[string attributeName] { get; } + + /// + /// The name of the HTML element. + /// + string TagName { get; } + + /// + /// Get a read only list of the attribute names. + /// + ReadOnlyCollection AttributeNames { get; } + + /// + /// Determine if the DomNode has the requested attribute. + /// + /// + /// The name of the attribute value. + /// + /// + /// True if the attribute exists in the DomNode, false if it does not. + /// + bool HasAttribute (string attributeName); + } +} diff --git a/CefSharp/IRequestHandler.cs b/CefSharp/IRequestHandler.cs index 5990dfd51..b72d7616d 100644 --- a/CefSharp/IRequestHandler.cs +++ b/CefSharp/IRequestHandler.cs @@ -175,6 +175,8 @@ namespace CefSharp /// /// The ChromiumWebBrowser control /// the browser object - void OnFocusedNodeChanged (IWebBrowser browserControl, IBrowser browser); + /// The frame object + /// An object with information about the node that has focus. + void OnFocusedNodeChanged (IWebBrowser browserControl, IBrowser browser, IFrame frame, IDomNode node); } }