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.
|
|
|
|
|
2017-05-19 11:06:00 +02:00
|
|
|
#include "libcef/browser/frame_host_impl.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "include/cef_request.h"
|
|
|
|
#include "include/cef_stream.h"
|
|
|
|
#include "include/cef_v8.h"
|
|
|
|
#include "libcef/browser/browser_host_impl.h"
|
2017-05-17 11:29:28 +02:00
|
|
|
#include "libcef/common/cef_messages.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Implementation of CommandResponseHandler for calling a CefStringVisitor.
|
|
|
|
class StringVisitHandler : public CefResponseManager::Handler {
|
|
|
|
public:
|
|
|
|
explicit StringVisitHandler(CefRefPtr<CefStringVisitor> visitor)
|
2017-05-17 11:29:28 +02:00
|
|
|
: visitor_(visitor) {}
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnResponse(const Cef_Response_Params& params) override {
|
2012-04-03 03:34:16 +02:00
|
|
|
visitor_->Visit(params.response);
|
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
private:
|
|
|
|
CefRefPtr<CefStringVisitor> visitor_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(StringVisitHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Implementation of CommandResponseHandler for calling ViewText().
|
|
|
|
class ViewTextHandler : public CefResponseManager::Handler {
|
|
|
|
public:
|
2017-05-17 11:29:28 +02:00
|
|
|
explicit ViewTextHandler(CefRefPtr<CefFrameHostImpl> frame) : frame_(frame) {}
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnResponse(const Cef_Response_Params& params) override {
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRefPtr<CefBrowser> browser = frame_->GetBrowser();
|
|
|
|
if (browser.get()) {
|
2017-05-17 11:29:28 +02:00
|
|
|
static_cast<CefBrowserHostImpl*>(browser.get())
|
|
|
|
->ViewText(params.response);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
private:
|
|
|
|
CefRefPtr<CefFrameHostImpl> frame_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(ViewTextHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
CefFrameHostImpl::CefFrameHostImpl(CefBrowserHostImpl* browser,
|
|
|
|
int64 frame_id,
|
2013-05-31 17:34:53 +02:00
|
|
|
bool is_main_frame,
|
|
|
|
const CefString& url,
|
|
|
|
const CefString& name,
|
|
|
|
int64 parent_frame_id)
|
2012-04-03 03:34:16 +02:00
|
|
|
: frame_id_(frame_id),
|
|
|
|
is_main_frame_(is_main_frame),
|
|
|
|
browser_(browser),
|
2014-09-24 17:38:11 +02:00
|
|
|
is_focused_(is_main_frame_), // The main frame always starts focused.
|
2013-05-31 17:34:53 +02:00
|
|
|
url_(url),
|
|
|
|
name_(name),
|
2017-05-17 11:29:28 +02:00
|
|
|
parent_frame_id_(parent_frame_id == kUnspecifiedFrameId
|
|
|
|
? kInvalidFrameId
|
|
|
|
: parent_frame_id) {}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefFrameHostImpl::~CefFrameHostImpl() {}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
bool CefFrameHostImpl::IsValid() {
|
|
|
|
base::AutoLock lock_scope(state_lock_);
|
|
|
|
return (browser_ != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameHostImpl::Undo() {
|
2013-05-31 17:34:53 +02:00
|
|
|
SendCommand("Undo", NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameHostImpl::Redo() {
|
2013-05-31 17:34:53 +02:00
|
|
|
SendCommand("Redo", NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameHostImpl::Cut() {
|
2013-05-31 17:34:53 +02:00
|
|
|
SendCommand("Cut", NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameHostImpl::Copy() {
|
2013-05-31 17:34:53 +02:00
|
|
|
SendCommand("Copy", NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameHostImpl::Paste() {
|
2013-05-31 17:34:53 +02:00
|
|
|
SendCommand("Paste", NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameHostImpl::Delete() {
|
2013-05-31 17:34:53 +02:00
|
|
|
SendCommand("Delete", NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameHostImpl::SelectAll() {
|
2013-05-31 17:34:53 +02:00
|
|
|
SendCommand("SelectAll", NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameHostImpl::ViewSource() {
|
2013-05-31 17:34:53 +02:00
|
|
|
SendCommand("GetSource", new ViewTextHandler(this));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameHostImpl::GetSource(CefRefPtr<CefStringVisitor> visitor) {
|
2013-05-31 17:34:53 +02:00
|
|
|
SendCommand("GetSource", new StringVisitHandler(visitor));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameHostImpl::GetText(CefRefPtr<CefStringVisitor> visitor) {
|
2013-05-31 17:34:53 +02:00
|
|
|
SendCommand("GetText", new StringVisitHandler(visitor));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameHostImpl::LoadRequest(CefRefPtr<CefRequest> request) {
|
2013-05-31 17:34:53 +02:00
|
|
|
CefRefPtr<CefBrowserHostImpl> browser;
|
|
|
|
int64 frame_id;
|
|
|
|
|
|
|
|
{
|
|
|
|
base::AutoLock lock_scope(state_lock_);
|
|
|
|
browser = browser_;
|
|
|
|
frame_id = (is_main_frame_ ? kMainFrameId : frame_id_);
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:23:12 +02:00
|
|
|
if (browser.get() && frame_id != kInvalidFrameId)
|
2013-05-31 17:34:53 +02:00
|
|
|
browser->LoadRequest(frame_id, request);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameHostImpl::LoadURL(const CefString& url) {
|
2013-05-31 17:34:53 +02:00
|
|
|
CefRefPtr<CefBrowserHostImpl> browser;
|
|
|
|
int64 frame_id;
|
|
|
|
|
|
|
|
{
|
|
|
|
base::AutoLock lock_scope(state_lock_);
|
|
|
|
browser = browser_;
|
|
|
|
frame_id = (is_main_frame_ ? kMainFrameId : frame_id_);
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:23:12 +02:00
|
|
|
if (browser.get() && frame_id != kInvalidFrameId) {
|
2014-07-16 23:27:25 +02:00
|
|
|
browser->LoadURL(frame_id, url, content::Referrer(),
|
2014-09-27 01:48:19 +02:00
|
|
|
ui::PAGE_TRANSITION_TYPED, std::string());
|
2013-08-27 20:19:21 +02:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameHostImpl::LoadString(const CefString& string,
|
|
|
|
const CefString& url) {
|
2013-05-31 17:34:53 +02:00
|
|
|
CefRefPtr<CefBrowserHostImpl> browser;
|
|
|
|
int64 frame_id;
|
|
|
|
|
|
|
|
{
|
|
|
|
base::AutoLock lock_scope(state_lock_);
|
|
|
|
browser = browser_;
|
|
|
|
frame_id = (is_main_frame_ ? kMainFrameId : frame_id_);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2013-05-31 17:34:53 +02:00
|
|
|
|
2015-10-09 17:23:12 +02:00
|
|
|
if (browser.get() && frame_id != kInvalidFrameId)
|
2013-05-31 17:34:53 +02:00
|
|
|
browser->LoadString(frame_id, string, url);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameHostImpl::ExecuteJavaScript(const CefString& jsCode,
|
|
|
|
const CefString& scriptUrl,
|
|
|
|
int startLine) {
|
2012-10-18 00:45:49 +02:00
|
|
|
SendJavaScript(jsCode, scriptUrl, startLine);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefFrameHostImpl::IsMain() {
|
|
|
|
return is_main_frame_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefFrameHostImpl::IsFocused() {
|
|
|
|
base::AutoLock lock_scope(state_lock_);
|
|
|
|
return is_focused_;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefFrameHostImpl::GetName() {
|
|
|
|
base::AutoLock lock_scope(state_lock_);
|
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 CefFrameHostImpl::GetIdentifier() {
|
|
|
|
return frame_id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefFrame> CefFrameHostImpl::GetParent() {
|
2013-05-31 17:34:53 +02:00
|
|
|
CefRefPtr<CefBrowserHostImpl> browser;
|
|
|
|
int64 parent_frame_id;
|
|
|
|
|
|
|
|
{
|
|
|
|
base::AutoLock lock_scope(state_lock_);
|
|
|
|
if (is_main_frame_ || parent_frame_id_ == kInvalidFrameId)
|
|
|
|
return NULL;
|
|
|
|
browser = browser_;
|
|
|
|
parent_frame_id = parent_frame_id_;
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
if (browser.get())
|
2013-05-31 17:34:53 +02:00
|
|
|
return browser->GetFrame(parent_frame_id);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefFrameHostImpl::GetURL() {
|
|
|
|
base::AutoLock lock_scope(state_lock_);
|
|
|
|
return url_;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefBrowser> CefFrameHostImpl::GetBrowser() {
|
|
|
|
base::AutoLock lock_scope(state_lock_);
|
|
|
|
return browser_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefFrameHostImpl::SetFocused(bool focused) {
|
|
|
|
base::AutoLock lock_scope(state_lock_);
|
|
|
|
is_focused_ = focused;
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:34:53 +02:00
|
|
|
void CefFrameHostImpl::SetAttributes(const CefString& url,
|
|
|
|
const CefString& name,
|
|
|
|
int64 parent_frame_id) {
|
2012-04-03 03:34:16 +02:00
|
|
|
base::AutoLock lock_scope(state_lock_);
|
2013-05-31 17:34:53 +02:00
|
|
|
if (!url.empty() && url != url_)
|
|
|
|
url_ = url;
|
|
|
|
if (!name.empty() && name != name_)
|
|
|
|
name_ = name;
|
|
|
|
if (parent_frame_id != kUnspecifiedFrameId)
|
|
|
|
parent_frame_id_ = parent_frame_id;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Context> CefFrameHostImpl::GetV8Context() {
|
|
|
|
NOTREACHED() << "GetV8Context cannot be called from the browser process";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-27 23:19:06 +02:00
|
|
|
void CefFrameHostImpl::VisitDOM(CefRefPtr<CefDOMVisitor> visitor) {
|
|
|
|
NOTREACHED() << "VisitDOM cannot be called from the browser process";
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void CefFrameHostImpl::SendJavaScript(const std::string& jsCode,
|
|
|
|
const std::string& scriptUrl,
|
|
|
|
int startLine) {
|
2012-10-18 00:45:49 +02:00
|
|
|
if (jsCode.empty())
|
|
|
|
return;
|
2016-10-17 20:14:44 +02:00
|
|
|
if (startLine <= 0) {
|
|
|
|
// A value of 0 is v8::Message::kNoLineNumberInfo in V8. There is code in
|
|
|
|
// V8 that will assert on that value (e.g. V8StackTraceImpl::Frame::Frame
|
|
|
|
// if a JS exception is thrown) so make sure |startLine| > 0.
|
|
|
|
startLine = 1;
|
|
|
|
}
|
2012-10-18 00:45:49 +02:00
|
|
|
|
2013-05-31 17:34:53 +02:00
|
|
|
CefRefPtr<CefBrowserHostImpl> browser;
|
|
|
|
int64 frame_id;
|
|
|
|
|
|
|
|
{
|
|
|
|
base::AutoLock lock_scope(state_lock_);
|
|
|
|
browser = browser_;
|
|
|
|
frame_id = (is_main_frame_ ? kMainFrameId : frame_id_);
|
2012-10-18 00:45:49 +02:00
|
|
|
}
|
2013-05-31 17:34:53 +02:00
|
|
|
|
2015-10-09 17:23:12 +02:00
|
|
|
if (browser.get() && frame_id != kInvalidFrameId)
|
2013-05-31 17:34:53 +02:00
|
|
|
browser->SendCode(frame_id, true, jsCode, scriptUrl, startLine, NULL);
|
2012-10-18 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
void CefFrameHostImpl::Detach() {
|
|
|
|
base::AutoLock lock_scope(state_lock_);
|
|
|
|
browser_ = NULL;
|
|
|
|
}
|
2013-05-31 17:34:53 +02:00
|
|
|
|
|
|
|
void CefFrameHostImpl::SendCommand(
|
|
|
|
const std::string& command,
|
|
|
|
CefRefPtr<CefResponseManager::Handler> responseHandler) {
|
|
|
|
CefRefPtr<CefBrowserHostImpl> browser;
|
|
|
|
int64 frame_id;
|
|
|
|
|
|
|
|
{
|
|
|
|
base::AutoLock lock_scope(state_lock_);
|
|
|
|
browser = browser_;
|
|
|
|
// Commands can only be sent to known frame ids.
|
|
|
|
frame_id = frame_id_;
|
|
|
|
}
|
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
if (browser.get() && frame_id != kInvalidFrameId)
|
2013-05-31 17:34:53 +02:00
|
|
|
browser->SendCommand(frame_id, command, responseHandler);
|
|
|
|
}
|