2012-04-27 23:19:06 +02:00
|
|
|
// 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"
|
2013-06-22 04:06:32 +02:00
|
|
|
#include "third_party/WebKit/public/platform/WebString.h"
|
|
|
|
#include "third_party/WebKit/public/platform/WebURL.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebDocument.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebElement.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebFrame.h"
|
2016-07-21 23:21:32 +02:00
|
|
|
#include "third_party/WebKit/public/web/WebLocalFrame.h"
|
2013-06-22 04:06:32 +02:00
|
|
|
#include "third_party/WebKit/public/web/WebNode.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebRange.h"
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
using blink::WebDocument;
|
|
|
|
using blink::WebElement;
|
|
|
|
using blink::WebFrame;
|
|
|
|
using blink::WebNode;
|
|
|
|
using blink::WebRange;
|
|
|
|
using blink::WebString;
|
|
|
|
using blink::WebURL;
|
2012-04-27 23:19:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
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();
|
2013-12-17 23:04:35 +01:00
|
|
|
return GetOrCreateNode(document.getElementById(base::string16(id)));
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetFocusedNode() {
|
|
|
|
const WebDocument& document = frame_->document();
|
2014-04-04 18:50:38 +02:00
|
|
|
return GetOrCreateNode(document.focusedElement());
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDOMDocumentImpl::HasSelection() {
|
2016-07-21 23:21:32 +02:00
|
|
|
if (!VerifyContext() || !frame_->isWebLocalFrame())
|
2012-04-27 23:19:06 +02:00
|
|
|
return false;
|
|
|
|
|
2016-07-21 23:21:32 +02:00
|
|
|
return frame_->toWebLocalFrame()->hasSelection();
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CefDOMDocumentImpl::GetSelectionStartOffset() {
|
2016-07-21 23:21:32 +02:00
|
|
|
if (!VerifyContext() || !frame_->isWebLocalFrame())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
blink::WebLocalFrame* local_frame = frame_->toWebLocalFrame();
|
|
|
|
if (!!local_frame->hasSelection())
|
2012-04-27 23:19:06 +02:00
|
|
|
return 0;
|
|
|
|
|
2016-07-21 23:21:32 +02:00
|
|
|
const WebRange& range = local_frame->selectionRange();
|
2012-04-27 23:19:06 +02:00
|
|
|
if (range.isNull())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return range.startOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefDOMDocumentImpl::GetSelectionEndOffset() {
|
2016-07-21 23:21:32 +02:00
|
|
|
if (!VerifyContext() || !frame_->isWebLocalFrame())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
blink::WebLocalFrame* local_frame = frame_->toWebLocalFrame();
|
|
|
|
if (!!local_frame->hasSelection())
|
2012-04-27 23:19:06 +02:00
|
|
|
return 0;
|
|
|
|
|
2016-07-21 23:21:32 +02:00
|
|
|
const WebRange& range = local_frame->selectionRange();
|
2012-04-27 23:19:06 +02:00
|
|
|
if (range.isNull())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return range.endOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDOMDocumentImpl::GetSelectionAsMarkup() {
|
|
|
|
CefString str;
|
2016-07-21 23:21:32 +02:00
|
|
|
if (!VerifyContext() || !frame_->isWebLocalFrame())
|
2012-04-27 23:19:06 +02:00
|
|
|
return str;
|
|
|
|
|
2016-07-21 23:21:32 +02:00
|
|
|
blink::WebLocalFrame* local_frame = frame_->toWebLocalFrame();
|
|
|
|
if (!!local_frame->hasSelection())
|
|
|
|
return str;
|
|
|
|
|
|
|
|
const WebString& markup = local_frame->selectionAsMarkup();
|
2012-04-27 23:19:06 +02:00
|
|
|
if (!markup.isNull())
|
|
|
|
str = markup;
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDOMDocumentImpl::GetSelectionAsText() {
|
|
|
|
CefString str;
|
2016-07-21 23:21:32 +02:00
|
|
|
if (!VerifyContext() || !frame_->isWebLocalFrame())
|
|
|
|
return str;
|
|
|
|
|
|
|
|
blink::WebLocalFrame* local_frame = frame_->toWebLocalFrame();
|
|
|
|
if (!!local_frame->hasSelection())
|
2012-04-27 23:19:06 +02:00
|
|
|
return str;
|
|
|
|
|
2016-07-21 23:21:32 +02:00
|
|
|
const WebString& text = local_frame->selectionAsText();
|
2012-04-27 23:19:06 +02:00
|
|
|
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();
|
2013-12-17 23:04:35 +01:00
|
|
|
const WebURL& url = document.completeURL(base::string16(partialURL));
|
2012-04-27 23:19:06 +02:00
|
|
|
if (!url.isNull()) {
|
|
|
|
GURL gurl = url;
|
|
|
|
str = gurl.spec();
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetOrCreateNode(
|
2013-11-08 22:28:56 +01:00
|
|
|
const blink::WebNode& node) {
|
2012-04-27 23:19:06 +02:00
|
|
|
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));
|
2014-09-27 01:48:19 +02:00
|
|
|
node_map_.insert(std::make_pair(node, nodeImpl.get()));
|
2012-04-27 23:19:06 +02:00
|
|
|
return nodeImpl;
|
|
|
|
}
|
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
void CefDOMDocumentImpl::RemoveNode(const blink::WebNode& node) {
|
2012-04-27 23:19:06 +02:00
|
|
|
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.
|
2014-07-15 00:18:51 +02:00
|
|
|
DCHECK(HasOneRef());
|
2012-04-27 23:19:06 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|