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"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "third_party/blink/public/platform/web_string.h"
|
|
|
|
#include "third_party/blink/public/platform/web_url.h"
|
|
|
|
#include "third_party/blink/public/web/web_document.h"
|
|
|
|
#include "third_party/blink/public/web/web_element.h"
|
|
|
|
#include "third_party/blink/public/web/web_local_frame.h"
|
|
|
|
#include "third_party/blink/public/web/web_node.h"
|
|
|
|
#include "third_party/blink/public/web/web_range.h"
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
using blink::WebDocument;
|
|
|
|
using blink::WebElement;
|
2017-07-27 01:19:27 +02:00
|
|
|
using blink::WebLocalFrame;
|
2013-11-08 22:28:56 +01:00
|
|
|
using blink::WebNode;
|
|
|
|
using blink::WebRange;
|
|
|
|
using blink::WebString;
|
|
|
|
using blink::WebURL;
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2017-07-27 01:19:27 +02:00
|
|
|
CefDOMDocumentImpl::CefDOMDocumentImpl(CefBrowserImpl* browser,
|
|
|
|
WebLocalFrame* frame)
|
2017-05-17 11:29:28 +02:00
|
|
|
: browser_(browser), frame_(frame) {
|
2017-04-20 21:28:17 +02:00
|
|
|
const WebDocument& document = frame_->GetDocument();
|
|
|
|
DCHECK(!document.IsNull());
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefDOMDocumentImpl::~CefDOMDocumentImpl() {
|
|
|
|
CEF_REQUIRE_RT();
|
|
|
|
|
|
|
|
// Verify that the Detach() method has been called.
|
2020-01-15 14:36:24 +01:00
|
|
|
DCHECK(frame_ == nullptr);
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefDOMDocumentImpl::Type CefDOMDocumentImpl::GetType() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!VerifyContext()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return DOM_DOCUMENT_TYPE_UNKNOWN;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
const WebDocument& document = frame_->GetDocument();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (document.IsHTMLDocument()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return DOM_DOCUMENT_TYPE_HTML;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (document.IsXHTMLDocument()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return DOM_DOCUMENT_TYPE_XHTML;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (document.IsPluginDocument()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return DOM_DOCUMENT_TYPE_PLUGIN;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
return DOM_DOCUMENT_TYPE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetDocument() {
|
2017-04-20 21:28:17 +02:00
|
|
|
const WebDocument& document = frame_->GetDocument();
|
|
|
|
return GetOrCreateNode(document.GetDocument());
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetBody() {
|
2017-04-20 21:28:17 +02:00
|
|
|
const WebDocument& document = frame_->GetDocument();
|
|
|
|
return GetOrCreateNode(document.Body());
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetHead() {
|
2017-04-20 21:28:17 +02:00
|
|
|
WebDocument document = frame_->GetDocument();
|
|
|
|
return GetOrCreateNode(document.Head());
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDOMDocumentImpl::GetTitle() {
|
|
|
|
CefString str;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!VerifyContext()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return str;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
const WebDocument& document = frame_->GetDocument();
|
|
|
|
const WebString& title = document.Title();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!title.IsNull()) {
|
2017-04-20 21:28:17 +02:00
|
|
|
str = title.Utf16();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetElementById(const CefString& id) {
|
2017-04-20 21:28:17 +02:00
|
|
|
const WebDocument& document = frame_->GetDocument();
|
2018-03-20 21:15:08 +01:00
|
|
|
return GetOrCreateNode(
|
|
|
|
document.GetElementById(WebString::FromUTF16(id.ToString16())));
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetFocusedNode() {
|
2017-04-20 21:28:17 +02:00
|
|
|
const WebDocument& document = frame_->GetDocument();
|
|
|
|
return GetOrCreateNode(document.FocusedElement());
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDOMDocumentImpl::HasSelection() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!VerifyContext()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2017-07-27 01:19:27 +02:00
|
|
|
return frame_->HasSelection();
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CefDOMDocumentImpl::GetSelectionStartOffset() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!VerifyContext()) {
|
2016-07-21 23:21:32 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-07-21 23:21:32 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!frame_->HasSelection()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2017-07-27 01:19:27 +02:00
|
|
|
const WebRange& range = frame_->SelectionRange();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (range.IsNull()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
return range.StartOffset();
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CefDOMDocumentImpl::GetSelectionEndOffset() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!VerifyContext()) {
|
2016-07-21 23:21:32 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-07-21 23:21:32 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!frame_->HasSelection()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2017-07-27 01:19:27 +02:00
|
|
|
const WebRange& range = frame_->SelectionRange();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (range.IsNull()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
return range.EndOffset();
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDOMDocumentImpl::GetSelectionAsMarkup() {
|
|
|
|
CefString str;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!VerifyContext()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return str;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!frame_->HasSelection()) {
|
2016-07-21 23:21:32 +02:00
|
|
|
return str;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-07-21 23:21:32 +02:00
|
|
|
|
2017-07-27 01:19:27 +02:00
|
|
|
const WebString& markup = frame_->SelectionAsMarkup();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!markup.IsNull()) {
|
2017-04-20 21:28:17 +02:00
|
|
|
str = markup.Utf16();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDOMDocumentImpl::GetSelectionAsText() {
|
|
|
|
CefString str;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!VerifyContext()) {
|
2016-07-21 23:21:32 +02:00
|
|
|
return str;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-07-21 23:21:32 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!frame_->HasSelection()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return str;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2017-07-27 01:19:27 +02:00
|
|
|
const WebString& text = frame_->SelectionAsText();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!text.IsNull()) {
|
2017-04-20 21:28:17 +02:00
|
|
|
str = text.Utf16();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDOMDocumentImpl::GetBaseURL() {
|
|
|
|
CefString str;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!VerifyContext()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return str;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
const WebDocument& document = frame_->GetDocument();
|
|
|
|
const WebURL& url = document.BaseURL();
|
|
|
|
if (!url.IsNull()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
GURL gurl = url;
|
|
|
|
str = gurl.spec();
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDOMDocumentImpl::GetCompleteURL(const CefString& partialURL) {
|
|
|
|
CefString str;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!VerifyContext()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return str;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
const WebDocument& document = frame_->GetDocument();
|
2018-03-20 21:15:08 +01:00
|
|
|
const WebURL& url =
|
|
|
|
document.CompleteURL(WebString::FromUTF16(partialURL.ToString16()));
|
2017-04-20 21:28:17 +02:00
|
|
|
if (!url.IsNull()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
GURL gurl = url;
|
|
|
|
str = gurl.spec();
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetOrCreateNode(
|
2013-11-08 22:28:56 +01:00
|
|
|
const blink::WebNode& node) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!VerifyContext()) {
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
|
|
|
// Nodes may potentially be null.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (node.IsNull()) {
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
|
|
|
if (!node_map_.empty()) {
|
|
|
|
// Locate the existing node, if any.
|
|
|
|
NodeMap::const_iterator it = node_map_.find(node);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (it != node_map_.end()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return it->second;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!VerifyContext()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
|
|
|
if (!node_map_.empty()) {
|
|
|
|
NodeMap::iterator it = node_map_.find(node);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (it != node_map_.end()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
node_map_.erase(it);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDOMDocumentImpl::Detach() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!VerifyContext()) {
|
2012-04-27 23:19:06 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
|
|
|
|
// 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();
|
2023-01-02 23:59:03 +01:00
|
|
|
for (; it != node_map_.end(); ++it) {
|
2012-04-27 23:19:06 +02:00
|
|
|
static_cast<CefDOMNodeImpl*>(it->second)->Detach();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-27 23:19:06 +02:00
|
|
|
node_map_.clear();
|
|
|
|
}
|
|
|
|
|
2020-01-15 14:36:24 +01:00
|
|
|
frame_ = nullptr;
|
2012-04-27 23:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDOMDocumentImpl::VerifyContext() {
|
2020-01-15 14:36:24 +01:00
|
|
|
if (!CEF_CURRENTLY_ON_RT() || frame_ == nullptr) {
|
2012-04-27 23:19:06 +02:00
|
|
|
NOTREACHED();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|