2012-04-03 03:34:16 +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/frame_impl.h"
|
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
#include "base/compiler_specific.h"
|
|
|
|
|
|
|
|
// Enable deprecation warnings for MSVC. See http://crbug.com/585142.
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(default:4996)
|
|
|
|
#endif
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "libcef/common/cef_messages.h"
|
2015-11-26 03:53:12 +01:00
|
|
|
#include "libcef/common/net/http_header_utils.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "libcef/common/request_impl.h"
|
|
|
|
#include "libcef/renderer/browser_impl.h"
|
2012-04-27 23:19:06 +02:00
|
|
|
#include "libcef/renderer/dom_document_impl.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "libcef/renderer/thread_util.h"
|
|
|
|
#include "libcef/renderer/v8_impl.h"
|
|
|
|
#include "libcef/renderer/webkit_glue.h"
|
|
|
|
|
2013-06-22 04:06:32 +02:00
|
|
|
#include "third_party/WebKit/public/platform/WebData.h"
|
|
|
|
#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/WebFrame.h"
|
2016-03-16 03:55:59 +01:00
|
|
|
#include "third_party/WebKit/public/web/WebFrameContentDumper.h"
|
2014-02-19 17:27:54 +01:00
|
|
|
#include "third_party/WebKit/public/web/WebKit.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/WebView.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebScriptSource.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
using blink::WebString;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
CefFrameImpl::CefFrameImpl(CefBrowserImpl* browser,
|
2013-11-08 22:28:56 +01:00
|
|
|
blink::WebFrame* frame)
|
2012-04-03 03:34:16 +02:00
|
|
|
: browser_(browser),
|
|
|
|
frame_(frame),
|
2014-04-04 18:50:38 +02:00
|
|
|
frame_id_(webkit_glue::GetIdentifier(frame)) {
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefFrameImpl::~CefFrameImpl() {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefFrameImpl::IsValid() {
|
|
|
|
CEF_REQUIRE_RT_RETURN(false);
|
|
|
|
|
|
|
|
return (frame_ != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameImpl::Undo() {
|
2016-07-21 23:21:32 +02:00
|
|
|
ExecuteCommand("Undo");
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameImpl::Redo() {
|
2016-07-21 23:21:32 +02:00
|
|
|
ExecuteCommand("Redo");
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameImpl::Cut() {
|
2016-07-21 23:21:32 +02:00
|
|
|
ExecuteCommand("Cut");
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameImpl::Copy() {
|
2016-07-21 23:21:32 +02:00
|
|
|
ExecuteCommand("Copy");
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameImpl::Paste() {
|
2016-07-21 23:21:32 +02:00
|
|
|
ExecuteCommand("Paste");
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameImpl::Delete() {
|
2016-07-21 23:21:32 +02:00
|
|
|
ExecuteCommand("Delete");
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameImpl::SelectAll() {
|
2016-07-21 23:21:32 +02:00
|
|
|
ExecuteCommand("SelectAll");
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameImpl::ViewSource() {
|
|
|
|
NOTREACHED() << "ViewSource cannot be called from the renderer process";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameImpl::GetSource(CefRefPtr<CefStringVisitor> visitor) {
|
|
|
|
CEF_REQUIRE_RT_RETURN_VOID();
|
2017-04-20 21:28:17 +02:00
|
|
|
if (frame_ && frame_->IsWebLocalFrame()) {
|
2016-03-16 03:55:59 +01:00
|
|
|
const CefString& content =
|
2017-04-20 21:28:17 +02:00
|
|
|
std::string(blink::WebFrameContentDumper::DumpAsMarkup(
|
|
|
|
frame_->ToWebLocalFrame()).Utf8());
|
2012-04-03 03:34:16 +02:00
|
|
|
visitor->Visit(content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameImpl::GetText(CefRefPtr<CefStringVisitor> visitor) {
|
|
|
|
CEF_REQUIRE_RT_RETURN_VOID();
|
|
|
|
|
|
|
|
if (frame_) {
|
2016-03-16 03:55:59 +01:00
|
|
|
const CefString& content = webkit_glue::DumpDocumentText(frame_);
|
2012-04-03 03:34:16 +02:00
|
|
|
visitor->Visit(content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameImpl::LoadRequest(CefRefPtr<CefRequest> request) {
|
|
|
|
CEF_REQUIRE_RT_RETURN_VOID();
|
|
|
|
|
|
|
|
if (!browser_)
|
|
|
|
return;
|
|
|
|
|
2014-07-16 23:27:25 +02:00
|
|
|
CefMsg_LoadRequest_Params params;
|
2012-04-03 03:34:16 +02:00
|
|
|
params.url = GURL(std::string(request->GetURL()));
|
|
|
|
params.method = request->GetMethod();
|
|
|
|
params.frame_id = frame_id_;
|
2014-07-16 23:27:25 +02:00
|
|
|
params.first_party_for_cookies =
|
|
|
|
GURL(std::string(request->GetFirstPartyForCookies()));
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
CefRequest::HeaderMap headerMap;
|
|
|
|
request->GetHeaderMap(headerMap);
|
|
|
|
if (!headerMap.empty())
|
|
|
|
params.headers = HttpHeaderUtils::GenerateHeaders(headerMap);
|
|
|
|
|
|
|
|
CefRefPtr<CefPostData> postData = request->GetPostData();
|
|
|
|
if (postData.get()) {
|
|
|
|
CefPostDataImpl* impl = static_cast<CefPostDataImpl*>(postData.get());
|
|
|
|
params.upload_data = new net::UploadData();
|
|
|
|
impl->Get(*params.upload_data.get());
|
|
|
|
}
|
|
|
|
|
2014-07-16 23:27:25 +02:00
|
|
|
params.load_flags = request->GetFlags();
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
browser_->LoadRequest(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameImpl::LoadURL(const CefString& url) {
|
|
|
|
CEF_REQUIRE_RT_RETURN_VOID();
|
|
|
|
|
|
|
|
if (!browser_)
|
|
|
|
return;
|
|
|
|
|
2014-07-16 23:27:25 +02:00
|
|
|
CefMsg_LoadRequest_Params params;
|
2012-04-03 03:34:16 +02:00
|
|
|
params.url = GURL(url.ToString());
|
|
|
|
params.method = "GET";
|
|
|
|
params.frame_id = frame_id_;
|
|
|
|
|
|
|
|
browser_->LoadRequest(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameImpl::LoadString(const CefString& string,
|
|
|
|
const CefString& url) {
|
|
|
|
CEF_REQUIRE_RT_RETURN_VOID();
|
|
|
|
|
|
|
|
if (frame_) {
|
|
|
|
GURL gurl = GURL(url.ToString());
|
2017-04-20 21:28:17 +02:00
|
|
|
frame_->LoadHTMLString(string.ToString(), gurl);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameImpl::ExecuteJavaScript(const CefString& jsCode,
|
|
|
|
const CefString& scriptUrl,
|
|
|
|
int startLine) {
|
|
|
|
CEF_REQUIRE_RT_RETURN_VOID();
|
|
|
|
|
2012-06-11 17:52:49 +02:00
|
|
|
if (jsCode.empty())
|
|
|
|
return;
|
2016-10-27 18:34:19 +02:00
|
|
|
if (startLine < 1)
|
|
|
|
startLine = 1;
|
2012-06-11 17:52:49 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
if (frame_) {
|
|
|
|
GURL gurl = GURL(scriptUrl.ToString());
|
2017-04-20 21:28:17 +02:00
|
|
|
frame_->ExecuteScript(
|
|
|
|
blink::WebScriptSource(WebString::FromUTF16(jsCode.ToString16()), gurl,
|
2017-03-03 23:37:23 +01:00
|
|
|
startLine));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefFrameImpl::IsMain() {
|
|
|
|
CEF_REQUIRE_RT_RETURN(false);
|
|
|
|
|
|
|
|
if (frame_)
|
2017-04-20 21:28:17 +02:00
|
|
|
return (frame_->Parent() == NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefFrameImpl::IsFocused() {
|
|
|
|
CEF_REQUIRE_RT_RETURN(false);
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (frame_ && frame_->View())
|
|
|
|
return (frame_->View()->FocusedFrame() == frame_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefFrameImpl::GetName() {
|
|
|
|
CefString name;
|
|
|
|
CEF_REQUIRE_RT_RETURN(name);
|
|
|
|
|
|
|
|
if (frame_)
|
2017-04-20 21:28:17 +02:00
|
|
|
name = webkit_glue::GetUniqueName(frame_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 CefFrameImpl::GetIdentifier() {
|
|
|
|
CEF_REQUIRE_RT_RETURN(0);
|
|
|
|
|
|
|
|
return frame_id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefFrame> CefFrameImpl::GetParent() {
|
|
|
|
CEF_REQUIRE_RT_RETURN(NULL);
|
|
|
|
|
|
|
|
if (frame_) {
|
2017-04-20 21:28:17 +02:00
|
|
|
blink::WebFrame* parent = frame_->Parent();
|
2012-04-03 03:34:16 +02:00
|
|
|
if (parent)
|
|
|
|
return browser_->GetWebFrameImpl(parent).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefFrameImpl::GetURL() {
|
|
|
|
CefString url;
|
|
|
|
CEF_REQUIRE_RT_RETURN(url);
|
|
|
|
|
|
|
|
if (frame_) {
|
2017-04-20 21:28:17 +02:00
|
|
|
GURL gurl = frame_->GetDocument().Url();
|
2012-04-03 03:34:16 +02:00
|
|
|
url = gurl.spec();
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefBrowser> CefFrameImpl::GetBrowser() {
|
|
|
|
CEF_REQUIRE_RT_RETURN(NULL);
|
|
|
|
|
|
|
|
return browser_;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Context> CefFrameImpl::GetV8Context() {
|
|
|
|
CEF_REQUIRE_RT_RETURN(NULL);
|
|
|
|
|
|
|
|
if (frame_) {
|
2017-04-20 21:28:17 +02:00
|
|
|
v8::Isolate* isolate = blink::MainThreadIsolate();
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
2017-04-20 21:28:17 +02:00
|
|
|
return new CefV8ContextImpl(isolate, frame_->MainWorldScriptContext());
|
2012-04-03 03:34:16 +02:00
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-27 23:19:06 +02:00
|
|
|
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;
|
2017-04-20 21:28:17 +02:00
|
|
|
const blink::WebDocument& document = frame_->GetDocument();
|
|
|
|
if (!document.IsNull())
|
2012-04-27 23:19:06 +02:00
|
|
|
documentImpl = new CefDOMDocumentImpl(browser_, frame_);
|
|
|
|
|
|
|
|
visitor->Visit(documentImpl.get());
|
|
|
|
|
|
|
|
if (documentImpl.get())
|
|
|
|
documentImpl->Detach();
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
void CefFrameImpl::Detach() {
|
|
|
|
browser_ = NULL;
|
|
|
|
frame_ = NULL;
|
|
|
|
}
|
2016-03-16 03:55:59 +01:00
|
|
|
|
2016-07-21 23:21:32 +02:00
|
|
|
void CefFrameImpl::ExecuteCommand(const std::string& command) {
|
|
|
|
CEF_REQUIRE_RT_RETURN_VOID();
|
2017-04-20 21:28:17 +02:00
|
|
|
if (frame_ && frame_->IsWebLocalFrame())
|
|
|
|
frame_->ToWebLocalFrame()->ExecuteCommand(WebString::FromUTF8(command));
|
2016-07-21 23:21:32 +02:00
|
|
|
}
|
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
|
|
|
|
// Enable deprecation warnings for MSVC. See http://crbug.com/585142.
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|