Add direct DOM access (issue #511).

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@610 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt
2012-04-27 21:19:06 +00:00
parent 4fe0ddf640
commit db3a9817ed
58 changed files with 5089 additions and 7 deletions

View File

@@ -392,6 +392,24 @@ CefRefPtr<CefFrameImpl> CefBrowserImpl::GetWebFrameImpl(int64 frame_id) {
return NULL;
}
void CefBrowserImpl::AddFrameObject(int64 frame_id,
CefTrackNode* tracked_object) {
CefRefPtr<CefTrackManager> manager;
if (!frame_objects_.empty()) {
FrameObjectMap::const_iterator it = frame_objects_.find(frame_id);
if (it != frame_objects_.end())
manager = it->second;
}
if (!manager.get()) {
manager = new CefTrackManager();
frame_objects_.insert(std::make_pair(frame_id, manager));
}
manager->Add(tracked_object);
}
// RenderViewObserver methods.
// -----------------------------------------------------------------------------
@@ -417,11 +435,20 @@ void CefBrowserImpl::DidStartProvisionalLoad(WebKit::WebFrame* frame) {
void CefBrowserImpl::FrameDetached(WebFrame* frame) {
int64 frame_id = frame->identifier();
// Remove the frame from the map.
FrameMap::iterator it = frames_.find(frame_id);
DCHECK(it != frames_.end());
it->second->Detach();
frames_.erase(it);
{
// Remove the frame from the map.
FrameMap::iterator it = frames_.find(frame_id);
DCHECK(it != frames_.end());
it->second->Detach();
frames_.erase(it);
}
if (!frame_objects_.empty()) {
// Remove any tracked objects associated with the frame.
FrameObjectMap::iterator it = frame_objects_.find(frame_id);
if (it != frame_objects_.end())
frame_objects_.erase(it);
}
// Notify the browser that the frame has detached.
Send(new CefHostMsg_FrameDetached(routing_id(), frame_id));

View File

@@ -14,6 +14,7 @@
#include "include/cef_browser.h"
#include "include/cef_client.h"
#include "libcef/common/response_manager.h"
#include "libcef/common/tracker.h"
#include "libcef/renderer/frame_impl.h"
#include "content/public/renderer/render_view_observer.h"
@@ -83,6 +84,9 @@ class CefBrowserImpl : public CefBrowser,
CefRefPtr<CefFrameImpl> GetWebFrameImpl(WebKit::WebFrame* frame);
CefRefPtr<CefFrameImpl> GetWebFrameImpl(int64 frame_id);
// Frame objects will be deleted immediately before the frame is closed.
void AddFrameObject(int64 frame_id, CefTrackNode* tracked_object);
int browser_window_id() const { return browser_window_id_; }
content::RenderView* render_view() {
return content::RenderViewObserver::render_view();
@@ -115,6 +119,11 @@ class CefBrowserImpl : public CefBrowser,
typedef std::map<int64, CefRefPtr<CefFrameImpl> > FrameMap;
FrameMap frames_;
// Map of unique frame ids to CefTrackManager objects that need to be cleaned
// up when the frame is deleted.
typedef std::map<int64, CefRefPtr<CefTrackManager> > FrameObjectMap;
FrameObjectMap frame_objects_;
// Manages response registrations.
CefResponseManager response_manager_;

View File

@@ -0,0 +1,262 @@
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "libcef/renderer/dom_document_impl.h"
#include "libcef/renderer/dom_node_impl.h"
#include "libcef/renderer/thread_util.h"
#include "base/logging.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebRange.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h"
using WebKit::WebDocument;
using WebKit::WebElement;
using WebKit::WebFrame;
using WebKit::WebNode;
using WebKit::WebRange;
using WebKit::WebString;
using WebKit::WebURL;
CefDOMDocumentImpl::CefDOMDocumentImpl(CefBrowserImpl* browser,
WebFrame* frame)
: browser_(browser),
frame_(frame) {
const WebDocument& document = frame_->document();
DCHECK(!document.isNull());
}
CefDOMDocumentImpl::~CefDOMDocumentImpl() {
CEF_REQUIRE_RT();
// Verify that the Detach() method has been called.
DCHECK(frame_ == NULL);
}
CefDOMDocumentImpl::Type CefDOMDocumentImpl::GetType() {
if (!VerifyContext())
return DOM_DOCUMENT_TYPE_UNKNOWN;
const WebDocument& document = frame_->document();
if (document.isHTMLDocument())
return DOM_DOCUMENT_TYPE_HTML;
if (document.isXHTMLDocument())
return DOM_DOCUMENT_TYPE_XHTML;
if (document.isPluginDocument())
return DOM_DOCUMENT_TYPE_PLUGIN;
return DOM_DOCUMENT_TYPE_UNKNOWN;
}
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetDocument() {
const WebDocument& document = frame_->document();
return GetOrCreateNode(document.document());
}
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetBody() {
const WebDocument& document = frame_->document();
return GetOrCreateNode(document.body());
}
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetHead() {
WebDocument document = frame_->document();
return GetOrCreateNode(document.head());
}
CefString CefDOMDocumentImpl::GetTitle() {
CefString str;
if (!VerifyContext())
return str;
const WebDocument& document = frame_->document();
const WebString& title = document.title();
if (!title.isNull())
str = title;
return str;
}
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetElementById(const CefString& id) {
const WebDocument& document = frame_->document();
return GetOrCreateNode(document.getElementById(string16(id)));
}
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetFocusedNode() {
const WebDocument& document = frame_->document();
return GetOrCreateNode(document.focusedNode());
}
bool CefDOMDocumentImpl::HasSelection() {
if (!VerifyContext())
return false;
return frame_->hasSelection();
}
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetSelectionStartNode() {
if (!VerifyContext() || !frame_->hasSelection())
return NULL;
const WebRange& range = frame_->selectionRange();
if (range.isNull())
return NULL;
int exceptionCode;
return GetOrCreateNode(range.startContainer(exceptionCode));
}
int CefDOMDocumentImpl::GetSelectionStartOffset() {
if (!VerifyContext() || !frame_->hasSelection())
return 0;
const WebRange& range = frame_->selectionRange();
if (range.isNull())
return 0;
return range.startOffset();
}
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetSelectionEndNode() {
if (!VerifyContext() || !frame_->hasSelection())
return NULL;
const WebRange& range = frame_->selectionRange();
if (range.isNull())
return NULL;
int exceptionCode;
return GetOrCreateNode(range.endContainer(exceptionCode));
}
int CefDOMDocumentImpl::GetSelectionEndOffset() {
if (!VerifyContext() || !frame_->hasSelection())
return 0;
const WebRange& range = frame_->selectionRange();
if (range.isNull())
return 0;
return range.endOffset();
}
CefString CefDOMDocumentImpl::GetSelectionAsMarkup() {
CefString str;
if (!VerifyContext() || !frame_->hasSelection())
return str;
const WebString& markup = frame_->selectionAsMarkup();
if (!markup.isNull())
str = markup;
return str;
}
CefString CefDOMDocumentImpl::GetSelectionAsText() {
CefString str;
if (!VerifyContext() || !frame_->hasSelection())
return str;
const WebString& text = frame_->selectionAsText();
if (!text.isNull())
str = text;
return str;
}
CefString CefDOMDocumentImpl::GetBaseURL() {
CefString str;
if (!VerifyContext())
return str;
const WebDocument& document = frame_->document();
const WebURL& url = document.baseURL();
if (!url.isNull()) {
GURL gurl = url;
str = gurl.spec();
}
return str;
}
CefString CefDOMDocumentImpl::GetCompleteURL(const CefString& partialURL) {
CefString str;
if (!VerifyContext())
return str;
const WebDocument& document = frame_->document();
const WebURL& url = document.completeURL(string16(partialURL));
if (!url.isNull()) {
GURL gurl = url;
str = gurl.spec();
}
return str;
}
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetOrCreateNode(
const WebKit::WebNode& node) {
if (!VerifyContext())
return NULL;
// Nodes may potentially be null.
if (node.isNull())
return NULL;
if (!node_map_.empty()) {
// Locate the existing node, if any.
NodeMap::const_iterator it = node_map_.find(node);
if (it != node_map_.end())
return it->second;
}
// Create the new node object.
CefRefPtr<CefDOMNode> nodeImpl(new CefDOMNodeImpl(this, node));
node_map_.insert(std::make_pair(node, nodeImpl));
return nodeImpl;
}
void CefDOMDocumentImpl::RemoveNode(const WebKit::WebNode& node) {
if (!VerifyContext())
return;
if (!node_map_.empty()) {
NodeMap::iterator it = node_map_.find(node);
if (it != node_map_.end())
node_map_.erase(it);
}
}
void CefDOMDocumentImpl::Detach() {
if (!VerifyContext())
return;
// If you hit this assert it means that you are keeping references to node
// objects beyond the valid scope.
DCHECK(node_map_.empty());
// If you hit this assert it means that you are keeping references to this
// document object beyond the valid scope.
DCHECK_EQ(GetRefCt(), 1);
if (!node_map_.empty()) {
NodeMap::const_iterator it = node_map_.begin();
for (; it != node_map_.end(); ++it)
static_cast<CefDOMNodeImpl*>(it->second)->Detach();
node_map_.clear();
}
frame_ = NULL;
}
bool CefDOMDocumentImpl::VerifyContext() {
if (!CEF_CURRENTLY_ON_RT() || frame_ == NULL) {
NOTREACHED();
return false;
}
return true;
}

View File

@@ -0,0 +1,66 @@
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#ifndef CEF_LIBCEF_DOM_DOCUMENT_IMPL_H_
#define CEF_LIBCEF_DOM_DOCUMENT_IMPL_H_
#pragma once
#include <map>
#include "include/cef_dom.h"
namespace WebKit {
class WebFrame;
class WebNode;
};
class CefBrowserImpl;
class CefDOMDocumentImpl : public CefDOMDocument {
public:
CefDOMDocumentImpl(CefBrowserImpl* browser,
WebKit::WebFrame* frame);
virtual ~CefDOMDocumentImpl();
// CefDOMDocument methods.
virtual Type GetType() OVERRIDE;
virtual CefRefPtr<CefDOMNode> GetDocument() OVERRIDE;
virtual CefRefPtr<CefDOMNode> GetBody() OVERRIDE;
virtual CefRefPtr<CefDOMNode> GetHead() OVERRIDE;
virtual CefString GetTitle() OVERRIDE;
virtual CefRefPtr<CefDOMNode> GetElementById(const CefString& id) OVERRIDE;
virtual CefRefPtr<CefDOMNode> GetFocusedNode() OVERRIDE;
virtual bool HasSelection() OVERRIDE;
virtual CefRefPtr<CefDOMNode> GetSelectionStartNode() OVERRIDE;
virtual int GetSelectionStartOffset() OVERRIDE;
virtual CefRefPtr<CefDOMNode> GetSelectionEndNode() OVERRIDE;
virtual int GetSelectionEndOffset() OVERRIDE;
virtual CefString GetSelectionAsMarkup() OVERRIDE;
virtual CefString GetSelectionAsText() OVERRIDE;
virtual CefString GetBaseURL() OVERRIDE;
virtual CefString GetCompleteURL(const CefString& partialURL) OVERRIDE;
CefBrowserImpl* GetBrowser() { return browser_; }
WebKit::WebFrame* GetFrame() { return frame_; }
// The document maintains a map of all existing node objects.
CefRefPtr<CefDOMNode> GetOrCreateNode(const WebKit::WebNode& node);
void RemoveNode(const WebKit::WebNode& node);
// Must be called before the object is destroyed.
void Detach();
// Verify that the object exists and is being accessed on the UI thread.
bool VerifyContext();
protected:
CefBrowserImpl* browser_;
WebKit::WebFrame* frame_;
typedef std::map<WebKit::WebNode, CefDOMNode*> NodeMap;
NodeMap node_map_;
IMPLEMENT_REFCOUNTING(CefDOMDocumentImpl);
};
#endif // CEF_LIBCEF_DOM_DOCUMENT_IMPL_H_

View File

@@ -0,0 +1,160 @@
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "libcef/renderer/dom_event_impl.h"
#include "libcef/renderer/dom_document_impl.h"
#include "libcef/renderer/thread_util.h"
#include "base/logging.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMEvent.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
using WebKit::WebDOMEvent;
using WebKit::WebString;
CefDOMEventImpl::CefDOMEventImpl(CefRefPtr<CefDOMDocumentImpl> document,
const WebKit::WebDOMEvent& event)
: document_(document),
event_(event) {
DCHECK(!event_.isNull());
}
CefDOMEventImpl::~CefDOMEventImpl() {
CEF_REQUIRE_RT();
DCHECK(event_.isNull());
}
CefString CefDOMEventImpl::GetType() {
CefString str;
if (!VerifyContext())
return str;
const WebString& type = event_.type();
if (!type.isNull())
str = type;
return str;
}
CefDOMEventImpl::Category CefDOMEventImpl::GetCategory() {
if (!VerifyContext())
return DOM_EVENT_CATEGORY_UNKNOWN;
int flags = 0;
if (event_.isUIEvent())
flags |= DOM_EVENT_CATEGORY_UI;
if (event_.isMouseEvent())
flags |= DOM_EVENT_CATEGORY_MOUSE;
if (event_.isMutationEvent())
flags |= DOM_EVENT_CATEGORY_MUTATION;
if (event_.isKeyboardEvent())
flags |= DOM_EVENT_CATEGORY_KEYBOARD;
if (event_.isTextEvent())
flags |= DOM_EVENT_CATEGORY_TEXT;
if (event_.isCompositionEvent())
flags |= DOM_EVENT_CATEGORY_COMPOSITION;
if (event_.isDragEvent())
flags |= DOM_EVENT_CATEGORY_DRAG;
if (event_.isClipboardEvent())
flags |= DOM_EVENT_CATEGORY_CLIPBOARD;
if (event_.isMessageEvent())
flags |= DOM_EVENT_CATEGORY_MESSAGE;
if (event_.isWheelEvent())
flags |= DOM_EVENT_CATEGORY_WHEEL;
if (event_.isBeforeTextInsertedEvent())
flags |= DOM_EVENT_CATEGORY_BEFORE_TEXT_INSERTED;
if (event_.isOverflowEvent())
flags |= DOM_EVENT_CATEGORY_OVERFLOW;
if (event_.isPageTransitionEvent())
flags |= DOM_EVENT_CATEGORY_PAGE_TRANSITION;
if (event_.isPopStateEvent())
flags |= DOM_EVENT_CATEGORY_POPSTATE;
if (event_.isProgressEvent())
flags |= DOM_EVENT_CATEGORY_PROGRESS;
if (event_.isXMLHttpRequestProgressEvent())
flags |= DOM_EVENT_CATEGORY_XMLHTTPREQUEST_PROGRESS;
if (event_.isWebKitAnimationEvent())
flags |= DOM_EVENT_CATEGORY_WEBKIT_ANIMATION;
if (event_.isWebKitTransitionEvent())
flags |= DOM_EVENT_CATEGORY_WEBKIT_TRANSITION;
if (event_.isBeforeLoadEvent())
flags |= DOM_EVENT_CATEGORY_BEFORE_LOAD;
return static_cast<Category>(flags);
}
CefDOMEventImpl::Phase CefDOMEventImpl::GetPhase() {
if (!VerifyContext())
return DOM_EVENT_PHASE_UNKNOWN;
switch (event_.eventPhase()) {
case WebDOMEvent::CapturingPhase:
return DOM_EVENT_PHASE_CAPTURING;
case WebDOMEvent::AtTarget:
return DOM_EVENT_PHASE_AT_TARGET;
case WebDOMEvent::BubblingPhase:
return DOM_EVENT_PHASE_BUBBLING;
}
return DOM_EVENT_PHASE_UNKNOWN;
}
bool CefDOMEventImpl::CanBubble() {
if (!VerifyContext())
return false;
return event_.bubbles();
}
bool CefDOMEventImpl::CanCancel() {
if (!VerifyContext())
return false;
return event_.cancelable();
}
CefRefPtr<CefDOMDocument> CefDOMEventImpl::GetDocument() {
if (!VerifyContext())
return NULL;
return document_.get();
}
CefRefPtr<CefDOMNode> CefDOMEventImpl::GetTarget() {
if (!VerifyContext())
return NULL;
return document_->GetOrCreateNode(event_.target());
}
CefRefPtr<CefDOMNode> CefDOMEventImpl::GetCurrentTarget() {
if (!VerifyContext())
return NULL;
return document_->GetOrCreateNode(event_.currentTarget());
}
void CefDOMEventImpl::Detach() {
// If you hit this assert it means that you are keeping references to this
// event object beyond the valid scope.
DCHECK_EQ(GetRefCt(), 1);
document_ = NULL;
event_.assign(WebDOMEvent());
}
bool CefDOMEventImpl::VerifyContext() {
if (!document_.get()) {
NOTREACHED();
return false;
}
if (!document_->VerifyContext())
return false;
if (event_.isNull()) {
NOTREACHED();
return false;
}
return true;
}

View File

@@ -0,0 +1,43 @@
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#ifndef CEF_LIBCEF_DOM_EVENT_IMPL_H_
#define CEF_LIBCEF_DOM_EVENT_IMPL_H_
#pragma once
#include "include/cef_dom.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMEvent.h"
class CefDOMDocumentImpl;
class CefDOMEventImpl : public CefDOMEvent {
public:
CefDOMEventImpl(CefRefPtr<CefDOMDocumentImpl> document,
const WebKit::WebDOMEvent& event);
virtual ~CefDOMEventImpl();
// CefDOMEvent methods.
virtual CefString GetType() OVERRIDE;
virtual Category GetCategory() OVERRIDE;
virtual Phase GetPhase() OVERRIDE;
virtual bool CanBubble() OVERRIDE;
virtual bool CanCancel() OVERRIDE;
virtual CefRefPtr<CefDOMDocument> GetDocument() OVERRIDE;
virtual CefRefPtr<CefDOMNode> GetTarget() OVERRIDE;
virtual CefRefPtr<CefDOMNode> GetCurrentTarget() OVERRIDE;
// Will be called from CefDOMEventListenerWrapper::handleEvent().
void Detach();
// Verify that the object exists and is being accessed on the UI thread.
bool VerifyContext();
protected:
CefRefPtr<CefDOMDocumentImpl> document_;
WebKit::WebDOMEvent event_;
IMPLEMENT_REFCOUNTING(CefDOMEventImpl);
};
#endif // CEF_LIBCEF_DOM_EVENT_IMPL_H_

View File

@@ -0,0 +1,459 @@
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "libcef/renderer/dom_node_impl.h"
#include "libcef/common/tracker.h"
#include "libcef/renderer/browser_impl.h"
#include "libcef/renderer/dom_document_impl.h"
#include "libcef/renderer/dom_event_impl.h"
#include "libcef/renderer/thread_util.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMEvent.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDOMEventListener.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebSelectElement.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
using WebKit::WebDocument;
using WebKit::WebDOMEvent;
using WebKit::WebDOMEventListener;
using WebKit::WebElement;
using WebKit::WebFrame;
using WebKit::WebFormControlElement;
using WebKit::WebInputElement;
using WebKit::WebNode;
using WebKit::WebSelectElement;
using WebKit::WebString;
namespace {
// Wrapper implementation for WebDOMEventListener.
class CefDOMEventListenerWrapper : public WebDOMEventListener,
public CefTrackNode {
public:
CefDOMEventListenerWrapper(CefBrowserImpl* browser, WebFrame* frame,
CefRefPtr<CefDOMEventListener> listener)
: browser_(browser),
frame_(frame),
listener_(listener) {
// Cause this object to be deleted immediately before the frame is closed.
browser->AddFrameObject(frame->identifier(), this);
}
virtual ~CefDOMEventListenerWrapper() {
CEF_REQUIRE_RT();
}
virtual void handleEvent(const WebDOMEvent& event) {
CefRefPtr<CefDOMDocumentImpl> documentImpl;
CefRefPtr<CefDOMEventImpl> eventImpl;
if (!event.isNull()) {
// Create CefDOMDocumentImpl and CefDOMEventImpl objects that are valid
// only for the scope of this method.
const WebDocument& document = frame_->document();
if (!document.isNull()) {
documentImpl = new CefDOMDocumentImpl(browser_, frame_);
eventImpl = new CefDOMEventImpl(documentImpl, event);
}
}
listener_->HandleEvent(eventImpl.get());
if (eventImpl.get())
eventImpl->Detach();
if (documentImpl.get())
documentImpl->Detach();
}
protected:
CefBrowserImpl* browser_;
WebFrame* frame_;
CefRefPtr<CefDOMEventListener> listener_;
};
} // namespace
CefDOMNodeImpl::CefDOMNodeImpl(CefRefPtr<CefDOMDocumentImpl> document,
const WebKit::WebNode& node)
: document_(document),
node_(node) {
}
CefDOMNodeImpl::~CefDOMNodeImpl() {
CEF_REQUIRE_RT();
if (document_.get() && !node_.isNull()) {
// Remove the node from the document.
document_->RemoveNode(node_);
}
}
CefDOMNodeImpl::Type CefDOMNodeImpl::GetType() {
if (!VerifyContext())
return DOM_NODE_TYPE_UNSUPPORTED;
switch (node_.nodeType()) {
case WebNode::ElementNode:
return DOM_NODE_TYPE_ELEMENT;
case WebNode::AttributeNode:
return DOM_NODE_TYPE_ATTRIBUTE;
case WebNode::TextNode:
return DOM_NODE_TYPE_TEXT;
case WebNode::CDataSectionNode:
return DOM_NODE_TYPE_CDATA_SECTION;
case WebNode::EntityReferenceNode:
return DOM_NODE_TYPE_ENTITY_REFERENCE;
case WebNode::EntityNode:
return DOM_NODE_TYPE_ENTITY;
case WebNode::ProcessingInstructionsNode:
return DOM_NODE_TYPE_PROCESSING_INSTRUCTIONS;
case WebNode::CommentNode:
return DOM_NODE_TYPE_COMMENT;
case WebNode::DocumentNode:
return DOM_NODE_TYPE_DOCUMENT;
case WebNode::DocumentTypeNode:
return DOM_NODE_TYPE_DOCUMENT_TYPE;
case WebNode::DocumentFragmentNode:
return DOM_NODE_TYPE_DOCUMENT_FRAGMENT;
case WebNode::NotationNode:
return DOM_NODE_TYPE_NOTATION;
case WebNode::XPathNamespaceNode:
return DOM_NODE_TYPE_XPATH_NAMESPACE;
default:
return DOM_NODE_TYPE_UNSUPPORTED;
}
}
bool CefDOMNodeImpl::IsText() {
if (!VerifyContext())
return false;
return node_.isTextNode();
}
bool CefDOMNodeImpl::IsElement() {
if (!VerifyContext())
return false;
return node_.isElementNode();
}
bool CefDOMNodeImpl::IsFormControlElement() {
if (!VerifyContext())
return false;
if (node_.isElementNode()) {
const WebElement& element = node_.toConst<WebElement>();
return element.isFormControlElement();
}
return false;
}
CefString CefDOMNodeImpl::GetFormControlElementType() {
CefString str;
if (!VerifyContext())
return str;
if (node_.isElementNode()) {
const WebElement& element = node_.toConst<WebElement>();
if (element.isFormControlElement()) {
// Retrieve the type from the form control element.
const WebFormControlElement& formElement =
node_.toConst<WebFormControlElement>();
const string16& form_control_type = formElement.formControlType();
str = form_control_type;
}
}
return str;
}
bool CefDOMNodeImpl::IsSame(CefRefPtr<CefDOMNode> that) {
if (!VerifyContext())
return false;
CefDOMNodeImpl* impl = static_cast<CefDOMNodeImpl*>(that.get());
if (!impl || !impl->VerifyContext())
return false;
return node_.equals(impl->node_);
}
CefString CefDOMNodeImpl::GetName() {
CefString str;
if (!VerifyContext())
return str;
const WebString& name = node_.nodeName();
if (!name.isNull())
str = name;
return str;
}
CefString CefDOMNodeImpl::GetValue() {
CefString str;
if (!VerifyContext())
return str;
if (node_.isElementNode()) {
const WebElement& element = node_.toConst<WebElement>();
if (element.isFormControlElement()) {
// Retrieve the value from the form control element.
const WebFormControlElement& formElement =
node_.toConst<WebFormControlElement>();
string16 value;
const string16& form_control_type = formElement.formControlType();
if (form_control_type == ASCIIToUTF16("text")) {
const WebInputElement& input_element =
formElement.toConst<WebInputElement>();
value = input_element.value();
} else if (form_control_type == ASCIIToUTF16("select-one")) {
const WebSelectElement& select_element =
formElement.toConst<WebSelectElement>();
value = select_element.value();
}
TrimWhitespace(value, TRIM_LEADING, &value);
str = value;
}
}
if (str.empty()) {
const WebString& value = node_.nodeValue();
if (!value.isNull())
str = value;
}
return str;
}
bool CefDOMNodeImpl::SetValue(const CefString& value) {
if (!VerifyContext())
return false;
if (node_.isElementNode())
return false;
return node_.setNodeValue(string16(value));
}
CefString CefDOMNodeImpl::GetAsMarkup() {
CefString str;
if (!VerifyContext())
return str;
const WebString& markup = node_.createMarkup();
if (!markup.isNull())
str = markup;
return str;
}
CefRefPtr<CefDOMDocument> CefDOMNodeImpl::GetDocument() {
if (!VerifyContext())
return NULL;
return document_.get();
}
CefRefPtr<CefDOMNode> CefDOMNodeImpl::GetParent() {
if (!VerifyContext())
return NULL;
return document_->GetOrCreateNode(node_.parentNode());
}
CefRefPtr<CefDOMNode> CefDOMNodeImpl::GetPreviousSibling() {
if (!VerifyContext())
return NULL;
return document_->GetOrCreateNode(node_.previousSibling());
}
CefRefPtr<CefDOMNode> CefDOMNodeImpl::GetNextSibling() {
if (!VerifyContext())
return NULL;
return document_->GetOrCreateNode(node_.nextSibling());
}
bool CefDOMNodeImpl::HasChildren() {
if (!VerifyContext())
return false;
return node_.hasChildNodes();
}
CefRefPtr<CefDOMNode> CefDOMNodeImpl::GetFirstChild() {
if (!VerifyContext())
return NULL;
return document_->GetOrCreateNode(node_.firstChild());
}
CefRefPtr<CefDOMNode> CefDOMNodeImpl::GetLastChild() {
if (!VerifyContext())
return NULL;
return document_->GetOrCreateNode(node_.lastChild());
}
void CefDOMNodeImpl::AddEventListener(const CefString& eventType,
CefRefPtr<CefDOMEventListener> listener,
bool useCapture) {
if (!VerifyContext())
return;
node_.addEventListener(string16(eventType),
new CefDOMEventListenerWrapper(document_->GetBrowser(),
document_->GetFrame(), listener),
useCapture);
}
CefString CefDOMNodeImpl::GetElementTagName() {
CefString str;
if (!VerifyContext())
return str;
if (!node_.isElementNode()) {
NOTREACHED();
return str;
}
const WebElement& element = node_.toConst<WebKit::WebElement>();
const WebString& tagname = element.tagName();
if (!tagname.isNull())
str = tagname;
return str;
}
bool CefDOMNodeImpl::HasElementAttributes() {
if (!VerifyContext())
return false;
if (!node_.isElementNode()) {
NOTREACHED();
return false;
}
const WebElement& element = node_.toConst<WebKit::WebElement>();
return (element.attributeCount() > 0);
}
bool CefDOMNodeImpl::HasElementAttribute(const CefString& attrName) {
if (!VerifyContext())
return false;
if (!node_.isElementNode()) {
NOTREACHED();
return false;
}
const WebElement& element = node_.toConst<WebKit::WebElement>();
return element.hasAttribute(string16(attrName));
}
CefString CefDOMNodeImpl::GetElementAttribute(const CefString& attrName) {
CefString str;
if (!VerifyContext())
return str;
if (!node_.isElementNode()) {
NOTREACHED();
return str;
}
const WebElement& element = node_.toConst<WebKit::WebElement>();
const WebString& attr = element.getAttribute(string16(attrName));
if (!attr.isNull())
str = attr;
return str;
}
void CefDOMNodeImpl::GetElementAttributes(AttributeMap& attrMap) {
if (!VerifyContext())
return;
if (!node_.isElementNode()) {
NOTREACHED();
return;
}
const WebElement& element = node_.toConst<WebKit::WebElement>();
unsigned int len = element.attributeCount();
if (len == 0)
return;
for (unsigned int i = 0; i < len; ++i) {
string16 name = element.attributeLocalName(i);
string16 value = element.attributeValue(i);
attrMap.insert(std::make_pair(name, value));
}
}
bool CefDOMNodeImpl::SetElementAttribute(const CefString& attrName,
const CefString& value) {
if (!VerifyContext())
return false;
if (!node_.isElementNode()) {
NOTREACHED();
return false;
}
WebElement element = node_.to<WebKit::WebElement>();
return element.setAttribute(string16(attrName), string16(value));
}
CefString CefDOMNodeImpl::GetElementInnerText() {
CefString str;
if (!VerifyContext())
return str;
if (!node_.isElementNode()) {
NOTREACHED();
return str;
}
WebElement element = node_.to<WebKit::WebElement>();
const WebString& text = element.innerText();
if (!text.isNull())
str = text;
return str;
}
void CefDOMNodeImpl::Detach() {
document_ = NULL;
node_.assign(WebNode());
}
bool CefDOMNodeImpl::VerifyContext() {
if (!document_.get()) {
NOTREACHED();
return false;
}
if (!document_->VerifyContext())
return false;
if (node_.isNull()) {
NOTREACHED();
return false;
}
return true;
}

View File

@@ -0,0 +1,63 @@
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#ifndef CEF_LIBCEF_DOM_NODE_IMPL_H_
#define CEF_LIBCEF_DOM_NODE_IMPL_H_
#pragma once
#include "include/cef_dom.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h"
class CefDOMDocumentImpl;
class CefDOMNodeImpl : public CefDOMNode {
public:
CefDOMNodeImpl(CefRefPtr<CefDOMDocumentImpl> document,
const WebKit::WebNode& node);
virtual ~CefDOMNodeImpl();
// CefDOMNode methods.
virtual Type GetType() OVERRIDE;
virtual bool IsText() OVERRIDE;
virtual bool IsElement() OVERRIDE;
virtual bool IsFormControlElement() OVERRIDE;
virtual CefString GetFormControlElementType() OVERRIDE;
virtual bool IsSame(CefRefPtr<CefDOMNode> that) OVERRIDE;
virtual CefString GetName() OVERRIDE;
virtual CefString GetValue() OVERRIDE;
virtual bool SetValue(const CefString& value) OVERRIDE;
virtual CefString GetAsMarkup() OVERRIDE;
virtual CefRefPtr<CefDOMDocument> GetDocument() OVERRIDE;
virtual CefRefPtr<CefDOMNode> GetParent() OVERRIDE;
virtual CefRefPtr<CefDOMNode> GetPreviousSibling() OVERRIDE;
virtual CefRefPtr<CefDOMNode> GetNextSibling() OVERRIDE;
virtual bool HasChildren() OVERRIDE;
virtual CefRefPtr<CefDOMNode> GetFirstChild() OVERRIDE;
virtual CefRefPtr<CefDOMNode> GetLastChild() OVERRIDE;
virtual void AddEventListener(const CefString& eventType,
CefRefPtr<CefDOMEventListener> listener,
bool useCapture) OVERRIDE;
virtual CefString GetElementTagName() OVERRIDE;
virtual bool HasElementAttributes() OVERRIDE;
virtual bool HasElementAttribute(const CefString& attrName) OVERRIDE;
virtual CefString GetElementAttribute(const CefString& attrName) OVERRIDE;
virtual void GetElementAttributes(AttributeMap& attrMap) OVERRIDE;
virtual bool SetElementAttribute(const CefString& attrName,
const CefString& value) OVERRIDE;
virtual CefString GetElementInnerText() OVERRIDE;
// Will be called from CefDOMDocumentImpl::Detach().
void Detach();
// Verify that the object exists and is being accessed on the UI thread.
bool VerifyContext();
protected:
CefRefPtr<CefDOMDocumentImpl> document_;
WebKit::WebNode node_;
IMPLEMENT_REFCOUNTING(CefDOMNodeImpl);
};
#endif // CEF_LIBCEF_DOM_NODE_IMPL_H_

View File

@@ -8,6 +8,7 @@
#include "libcef/common/http_header_utils.h"
#include "libcef/common/request_impl.h"
#include "libcef/renderer/browser_impl.h"
#include "libcef/renderer/dom_document_impl.h"
#include "libcef/renderer/thread_util.h"
#include "libcef/renderer/v8_impl.h"
#include "libcef/renderer/webkit_glue.h"
@@ -240,6 +241,25 @@ CefRefPtr<CefV8Context> CefFrameImpl::GetV8Context() {
}
}
void CefFrameImpl::VisitDOM(CefRefPtr<CefDOMVisitor> visitor) {
CEF_REQUIRE_RT_RETURN_VOID();
if (!frame_)
return;
// Create a CefDOMDocumentImpl object that is valid only for the scope of this
// method.
CefRefPtr<CefDOMDocumentImpl> documentImpl;
const WebKit::WebDocument& document = frame_->document();
if (!document.isNull())
documentImpl = new CefDOMDocumentImpl(browser_, frame_);
visitor->Visit(documentImpl.get());
if (documentImpl.get())
documentImpl->Detach();
}
void CefFrameImpl::Detach() {
browser_ = NULL;
frame_ = NULL;

View File

@@ -52,6 +52,7 @@ class CefFrameImpl : public CefFrame {
virtual CefString GetURL() OVERRIDE;
virtual CefRefPtr<CefBrowser> GetBrowser() OVERRIDE;
virtual CefRefPtr<CefV8Context> GetV8Context() OVERRIDE;
virtual void VisitDOM(CefRefPtr<CefDOMVisitor> visitor) OVERRIDE;
void Detach();