cef/libcef/renderer/dom_document_impl.cc

279 lines
6.3 KiB
C++
Raw Normal View History

// 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/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"
using blink::WebDocument;
using blink::WebElement;
using blink::WebLocalFrame;
using blink::WebNode;
using blink::WebRange;
using blink::WebString;
using blink::WebURL;
CefDOMDocumentImpl::CefDOMDocumentImpl(CefBrowserImpl* browser,
WebLocalFrame* frame)
: browser_(browser), frame_(frame) {
const WebDocument& document = frame_->GetDocument();
DCHECK(!document.IsNull());
}
CefDOMDocumentImpl::~CefDOMDocumentImpl() {
CEF_REQUIRE_RT();
// Verify that the Detach() method has been called.
DCHECK(frame_ == nullptr);
}
CefDOMDocumentImpl::Type CefDOMDocumentImpl::GetType() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return DOM_DOCUMENT_TYPE_UNKNOWN;
2023-01-02 23:59:03 +01:00
}
const WebDocument& document = frame_->GetDocument();
2023-01-02 23:59:03 +01:00
if (document.IsHTMLDocument()) {
return DOM_DOCUMENT_TYPE_HTML;
2023-01-02 23:59:03 +01:00
}
if (document.IsXHTMLDocument()) {
return DOM_DOCUMENT_TYPE_XHTML;
2023-01-02 23:59:03 +01:00
}
if (document.IsPluginDocument()) {
return DOM_DOCUMENT_TYPE_PLUGIN;
2023-01-02 23:59:03 +01:00
}
return DOM_DOCUMENT_TYPE_UNKNOWN;
}
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetDocument() {
const WebDocument& document = frame_->GetDocument();
return GetOrCreateNode(document.GetDocument());
}
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetBody() {
const WebDocument& document = frame_->GetDocument();
return GetOrCreateNode(document.Body());
}
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetHead() {
WebDocument document = frame_->GetDocument();
return GetOrCreateNode(document.Head());
}
CefString CefDOMDocumentImpl::GetTitle() {
CefString str;
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return str;
2023-01-02 23:59:03 +01:00
}
const WebDocument& document = frame_->GetDocument();
const WebString& title = document.Title();
2023-01-02 23:59:03 +01:00
if (!title.IsNull()) {
str = title.Utf16();
2023-01-02 23:59:03 +01:00
}
return str;
}
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetElementById(const CefString& id) {
const WebDocument& document = frame_->GetDocument();
return GetOrCreateNode(
document.GetElementById(WebString::FromUTF16(id.ToString16())));
}
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetFocusedNode() {
const WebDocument& document = frame_->GetDocument();
return GetOrCreateNode(document.FocusedElement());
}
bool CefDOMDocumentImpl::HasSelection() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return false;
2023-01-02 23:59:03 +01:00
}
return frame_->HasSelection();
}
int CefDOMDocumentImpl::GetSelectionStartOffset() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return 0;
2023-01-02 23:59:03 +01:00
}
2023-01-02 23:59:03 +01:00
if (!frame_->HasSelection()) {
return 0;
2023-01-02 23:59:03 +01:00
}
const WebRange& range = frame_->SelectionRange();
2023-01-02 23:59:03 +01:00
if (range.IsNull()) {
return 0;
2023-01-02 23:59:03 +01:00
}
return range.StartOffset();
}
int CefDOMDocumentImpl::GetSelectionEndOffset() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return 0;
2023-01-02 23:59:03 +01:00
}
2023-01-02 23:59:03 +01:00
if (!frame_->HasSelection()) {
return 0;
2023-01-02 23:59:03 +01:00
}
const WebRange& range = frame_->SelectionRange();
2023-01-02 23:59:03 +01:00
if (range.IsNull()) {
return 0;
2023-01-02 23:59:03 +01:00
}
return range.EndOffset();
}
CefString CefDOMDocumentImpl::GetSelectionAsMarkup() {
CefString str;
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return str;
2023-01-02 23:59:03 +01:00
}
2023-01-02 23:59:03 +01:00
if (!frame_->HasSelection()) {
return str;
2023-01-02 23:59:03 +01:00
}
const WebString& markup = frame_->SelectionAsMarkup();
2023-01-02 23:59:03 +01:00
if (!markup.IsNull()) {
str = markup.Utf16();
2023-01-02 23:59:03 +01:00
}
return str;
}
CefString CefDOMDocumentImpl::GetSelectionAsText() {
CefString str;
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return str;
2023-01-02 23:59:03 +01:00
}
2023-01-02 23:59:03 +01:00
if (!frame_->HasSelection()) {
return str;
2023-01-02 23:59:03 +01:00
}
const WebString& text = frame_->SelectionAsText();
2023-01-02 23:59:03 +01:00
if (!text.IsNull()) {
str = text.Utf16();
2023-01-02 23:59:03 +01:00
}
return str;
}
CefString CefDOMDocumentImpl::GetBaseURL() {
CefString str;
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return str;
2023-01-02 23:59:03 +01:00
}
const WebDocument& document = frame_->GetDocument();
const WebURL& url = document.BaseURL();
if (!url.IsNull()) {
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()) {
return str;
2023-01-02 23:59:03 +01:00
}
const WebDocument& document = frame_->GetDocument();
const WebURL& url =
document.CompleteURL(WebString::FromUTF16(partialURL.ToString16()));
if (!url.IsNull()) {
GURL gurl = url;
str = gurl.spec();
}
return str;
}
CefRefPtr<CefDOMNode> CefDOMDocumentImpl::GetOrCreateNode(
const blink::WebNode& node) {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return nullptr;
2023-01-02 23:59:03 +01:00
}
// Nodes may potentially be null.
2023-01-02 23:59:03 +01:00
if (node.IsNull()) {
return nullptr;
2023-01-02 23:59:03 +01: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()) {
return it->second;
2023-01-02 23:59:03 +01:00
}
}
// Create the new node object.
CefRefPtr<CefDOMNode> nodeImpl(new CefDOMNodeImpl(this, node));
node_map_.insert(std::make_pair(node, nodeImpl.get()));
return nodeImpl;
}
void CefDOMDocumentImpl::RemoveNode(const blink::WebNode& node) {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return;
2023-01-02 23:59:03 +01: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()) {
node_map_.erase(it);
2023-01-02 23:59:03 +01:00
}
}
}
void CefDOMDocumentImpl::Detach() {
2023-01-02 23:59:03 +01:00
if (!VerifyContext()) {
return;
2023-01-02 23:59:03 +01: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.
Introduce the use of Chromium types (issue #1336). Changes to the CEF public API: - Add base::Bind, base::Callback, base::Lock, base::WeakPtr, scoped_refptr, scoped_ptr and supporting types. - Add include/wrapper/cef_closure_task.h helpers for converting a base::Closure to a CefTask. - Change CefRefPtr to extend scoped_refptr. -- Change CefBase method signatures to match RefCountedThreadSafeBase. - Change IMPLEMENT_REFCOUNTING to use base::AtomicRefCount*. -- Remove the CefAtomic* functions. -- IMPLEMENT_REFCOUNTING now enforces via a compile-time error that the correct class name was passed to the macro. - Change IMPLEMENT_LOCKING to use base::Lock. -- Remove the CefCriticalSection class. -- Deprecate the IMPLEMENT_LOCKING macro. -- base::Lock will DCHECK() in Debug builds if lock usage is reentrant. - Move include/internal/cef_tuple.h to include/base/cef_tuple.h. - Allow an empty |callback| parameter passed to CefBeginTracing. Changes to the CEF implementation: - Fix incorrect names passed to the IMPLEMENT_REFCOUNTING macro. - Fix instances of reentrant locking in the CefXmlObject and CefRequest implementations. - Remove use of the IMPLEMENT_LOCKING macro. Changes to cef_unittests: - Add tests/unittests/chromium_includes.h and always include it first from unit test .cc files to avoid name conflicts with Chromium types. - Fix wrong header include ordering. - Remove use of the IMPLEMENT_LOCKING macro. Changes to cefclient and cefsimple: - Use base::Bind and cef_closure_task.h instead of NewCefRunnable*. - Remove use of the IMPEMENT_LOCKING macro. - Fix incorrect/unnecessary locking. - Add additional runtime thread checks. - Windows: Perform actions on the UI thread instead of the main thread when running in multi-threaded-message-loop mode to avoid excessive locking. git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@1769 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
2014-07-15 00:18:51 +02:00
DCHECK(HasOneRef());
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) {
static_cast<CefDOMNodeImpl*>(it->second)->Detach();
2023-01-02 23:59:03 +01:00
}
node_map_.clear();
}
frame_ = nullptr;
}
bool CefDOMDocumentImpl::VerifyContext() {
if (!CEF_CURRENTLY_ON_RT() || frame_ == nullptr) {
DCHECK(false);
return false;
}
return true;
}