Refactor the code in atom_api_web_contents
This commit is contained in:
parent
921aaf9aa3
commit
9d8e510a55
4 changed files with 95 additions and 98 deletions
|
@ -17,6 +17,7 @@
|
|||
#include "atom/browser/lib/bluetooth_chooser.h"
|
||||
#include "atom/browser/native_window.h"
|
||||
#include "atom/browser/net/atom_network_delegate.h"
|
||||
#include "atom/browser/osr/osr_output_device.h"
|
||||
#include "atom/browser/osr/osr_web_contents_view.h"
|
||||
#include "atom/browser/osr/osr_render_widget_host_view.h"
|
||||
#include "atom/browser/ui/drag_util.h"
|
||||
|
@ -313,16 +314,11 @@ WebContents::WebContents(v8::Isolate* isolate,
|
|||
options.Get("transparent", &transparent);
|
||||
|
||||
content::WebContents::CreateParams params(session->browser_context());
|
||||
|
||||
auto view = new OffScreenWebContentsView(transparent);
|
||||
params.view = view;
|
||||
params.delegate_view = view;
|
||||
params.view = new OffScreenWebContentsView(transparent);
|
||||
params.delegate_view = params.view;
|
||||
|
||||
web_contents = content::WebContents::Create(params);
|
||||
view->SetWebContents(web_contents);
|
||||
|
||||
paint_callback_ = base::Bind(&WebContents::OnPaint, base::Unretained(this),
|
||||
isolate);
|
||||
} else {
|
||||
content::WebContents::CreateParams params(session->browser_context());
|
||||
web_contents = content::WebContents::Create(params);
|
||||
|
@ -383,7 +379,7 @@ bool WebContents::AddMessageToConsole(content::WebContents* source,
|
|||
const base::string16& message,
|
||||
int32_t line_no,
|
||||
const base::string16& source_id) {
|
||||
if ((type_ == BROWSER_WINDOW || type_ == OFF_SCREEN)) {
|
||||
if (type_ == BROWSER_WINDOW || type_ == OFF_SCREEN) {
|
||||
return false;
|
||||
} else {
|
||||
Emit("console-message", level, message, line_no, source_id);
|
||||
|
@ -394,7 +390,7 @@ bool WebContents::AddMessageToConsole(content::WebContents* source,
|
|||
void WebContents::OnCreateWindow(const GURL& target_url,
|
||||
const std::string& frame_name,
|
||||
WindowOpenDisposition disposition) {
|
||||
if ((type_ == BROWSER_WINDOW || type_ == OFF_SCREEN))
|
||||
if (type_ == BROWSER_WINDOW || type_ == OFF_SCREEN)
|
||||
Emit("-new-window", target_url, frame_name, disposition);
|
||||
else
|
||||
Emit("new-window", target_url, frame_name, disposition);
|
||||
|
@ -404,7 +400,7 @@ content::WebContents* WebContents::OpenURLFromTab(
|
|||
content::WebContents* source,
|
||||
const content::OpenURLParams& params) {
|
||||
if (params.disposition != CURRENT_TAB) {
|
||||
if ((type_ == BROWSER_WINDOW || type_ == OFF_SCREEN))
|
||||
if (type_ == BROWSER_WINDOW || type_ == OFF_SCREEN)
|
||||
Emit("-new-window", params.url, "", params.disposition);
|
||||
else
|
||||
Emit("new-window", params.url, "", params.disposition);
|
||||
|
@ -421,7 +417,7 @@ content::WebContents* WebContents::OpenURLFromTab(
|
|||
void WebContents::BeforeUnloadFired(content::WebContents* tab,
|
||||
bool proceed,
|
||||
bool* proceed_to_fire_unload) {
|
||||
if ((type_ == BROWSER_WINDOW || type_ == OFF_SCREEN))
|
||||
if (type_ == BROWSER_WINDOW || type_ == OFF_SCREEN)
|
||||
*proceed_to_fire_unload = proceed;
|
||||
else
|
||||
*proceed_to_fire_unload = true;
|
||||
|
@ -613,9 +609,9 @@ void WebContents::DocumentLoadedInFrame(
|
|||
content::RenderFrameHost* render_frame_host) {
|
||||
if (!render_frame_host->GetParent()) {
|
||||
if (IsOffScreen()) {
|
||||
const auto rwhv = web_contents()->GetRenderWidgetHostView();
|
||||
auto osr_rwhv = static_cast<OffScreenRenderWidgetHostView *>(rwhv);
|
||||
osr_rwhv->SetPaintCallback(&paint_callback_);
|
||||
const auto* rwhv = web_contents()->GetRenderWidgetHostView();
|
||||
static_cast<OffScreenRenderWidgetHostView*>(rwhv)->SetPaintCallback(
|
||||
base::Bind(&WebContents::OnPaint, base::Unretained(this), isolate));
|
||||
}
|
||||
|
||||
Emit("dom-ready");
|
||||
|
@ -813,13 +809,6 @@ WebContents::Type WebContents::GetType() const {
|
|||
return type_;
|
||||
}
|
||||
|
||||
#if !defined(OS_MACOSX)
|
||||
bool WebContents::IsFocused() const {
|
||||
auto view = web_contents()->GetRenderWidgetHostView();
|
||||
return view && view->HasFocus();
|
||||
}
|
||||
#endif
|
||||
|
||||
bool WebContents::Equal(const WebContents* web_contents) const {
|
||||
return GetID() == web_contents->GetID();
|
||||
}
|
||||
|
@ -1183,6 +1172,13 @@ void WebContents::Focus() {
|
|||
web_contents()->Focus();
|
||||
}
|
||||
|
||||
#if !defined(OS_MACOSX)
|
||||
bool WebContents::IsFocused() const {
|
||||
auto view = web_contents()->GetRenderWidgetHostView();
|
||||
return view && view->HasFocus();
|
||||
}
|
||||
#endif
|
||||
|
||||
void WebContents::TabTraverse(bool reverse) {
|
||||
web_contents()->FocusThroughTabTraversal(reverse);
|
||||
}
|
||||
|
@ -1351,6 +1347,67 @@ bool WebContents::IsOffScreen() const {
|
|||
return type_ == OFF_SCREEN;
|
||||
}
|
||||
|
||||
void WebContents::OnPaint(v8::Isolate* isolate, const gfx::Rect& dirty_rect,
|
||||
const gfx::Size& bitmap_size, void* bitmap_pixels) {
|
||||
v8::MaybeLocal<v8::Object> buffer = node::Buffer::New(
|
||||
isolate, reinterpret_cast<char*>(bitmap_pixels), sizeof(bitmap_pixels));
|
||||
if (!buffer.IsEmpty())
|
||||
Emit("paint", damage_rect, buffer.ToLocalChecked(), bitmap_size);
|
||||
}
|
||||
|
||||
void WebContents::StartPainting() {
|
||||
if (!IsOffScreen())
|
||||
return;
|
||||
|
||||
const auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
|
||||
web_contents()->GetRenderWidgetHostView());
|
||||
if (osr_rwhv) {
|
||||
osr_rwhv->SetPainting(true);
|
||||
osr_rwhv->Show();
|
||||
}
|
||||
}
|
||||
|
||||
void WebContents::StopPainting() {
|
||||
if (!IsOffScreen())
|
||||
return;
|
||||
|
||||
const auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
|
||||
web_contents()->GetRenderWidgetHostView());
|
||||
if (osr_rwhv) {
|
||||
osr_rwhv->SetPainting(false);
|
||||
osr_rwhv->Hide();
|
||||
}
|
||||
}
|
||||
|
||||
bool WebContents::IsPainting() const {
|
||||
if (!IsOffScreen())
|
||||
return false;
|
||||
|
||||
const auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
|
||||
web_contents()->GetRenderWidgetHostView());
|
||||
return osr_rwhv && osr_rwhv->IsPainting();
|
||||
}
|
||||
|
||||
void WebContents::SetFrameRate(int frame_rate) {
|
||||
if (!IsOffScreen())
|
||||
return;
|
||||
|
||||
const auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
|
||||
web_contents()->GetRenderWidgetHostView());
|
||||
if (osr_rwhv)
|
||||
osr_rwhv->SetFrameRate(frame_rate);
|
||||
}
|
||||
|
||||
int WebContents::GetFrameRate() const {
|
||||
if (!IsOffScreen())
|
||||
return 0;
|
||||
|
||||
const auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
|
||||
web_contents()->GetRenderWidgetHostView());
|
||||
return osr_rwhv ? osr_rwhv->GetFrameRate() : 0;
|
||||
}
|
||||
|
||||
|
||||
v8::Local<v8::Value> WebContents::GetWebPreferences(v8::Isolate* isolate) {
|
||||
WebContentsPreferences* web_preferences =
|
||||
WebContentsPreferences::FromWebContents(web_contents());
|
||||
|
@ -1393,64 +1450,6 @@ v8::Local<v8::Value> WebContents::Debugger(v8::Isolate* isolate) {
|
|||
return v8::Local<v8::Value>::New(isolate, debugger_);
|
||||
}
|
||||
|
||||
void WebContents::OnPaint(
|
||||
v8::Isolate* isolate,
|
||||
const gfx::Rect& damage_rect,
|
||||
int bitmap_width,
|
||||
int bitmap_height,
|
||||
void* bitmap_pixels) {
|
||||
v8::MaybeLocal<v8::Object> buffer = node::Buffer::New(isolate,
|
||||
reinterpret_cast<char *>(bitmap_pixels), sizeof(bitmap_pixels));
|
||||
|
||||
const gfx::Size bitmap_size = gfx::Size(bitmap_width, bitmap_height);
|
||||
Emit("paint", damage_rect, buffer.ToLocalChecked(), bitmap_size);
|
||||
}
|
||||
|
||||
void WebContents::StartPainting() {
|
||||
if (IsOffScreen()) {
|
||||
const auto osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
|
||||
web_contents()->GetRenderWidgetHostView());
|
||||
osr_rwhv->SetPainting(true);
|
||||
osr_rwhv->Show();
|
||||
}
|
||||
}
|
||||
|
||||
void WebContents::StopPainting() {
|
||||
if (IsOffScreen()) {
|
||||
const auto osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
|
||||
web_contents()->GetRenderWidgetHostView());
|
||||
osr_rwhv->SetPainting(false);
|
||||
osr_rwhv->Hide();
|
||||
}
|
||||
}
|
||||
|
||||
bool WebContents::IsPainting() const {
|
||||
if (IsOffScreen()) {
|
||||
const auto osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
|
||||
web_contents()->GetRenderWidgetHostView());
|
||||
return osr_rwhv->IsPainting();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void WebContents::SetFrameRate(int frame_rate) {
|
||||
if (IsOffScreen()) {
|
||||
const auto osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
|
||||
web_contents()->GetRenderWidgetHostView());
|
||||
osr_rwhv->SetFrameRate(frame_rate);
|
||||
}
|
||||
}
|
||||
|
||||
int WebContents::GetFrameRate() const {
|
||||
if (IsOffScreen()) {
|
||||
const auto osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(
|
||||
web_contents()->GetRenderWidgetHostView());
|
||||
return osr_rwhv->GetFrameRate();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// static
|
||||
void WebContents::BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Local<v8::FunctionTemplate> prototype) {
|
||||
|
@ -1501,6 +1500,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
|
|||
.SetMethod("findInPage", &WebContents::FindInPage)
|
||||
.SetMethod("stopFindInPage", &WebContents::StopFindInPage)
|
||||
.SetMethod("focus", &WebContents::Focus)
|
||||
.SetMethod("isFocused", &WebContents::IsFocused)
|
||||
.SetMethod("tabTraverse", &WebContents::TabTraverse)
|
||||
.SetMethod("_send", &WebContents::SendIPCMessage)
|
||||
.SetMethod("sendInputEvent", &WebContents::SendInputEvent)
|
||||
|
@ -1510,6 +1510,12 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
|
|||
.SetMethod("startDrag", &WebContents::StartDrag)
|
||||
.SetMethod("setSize", &WebContents::SetSize)
|
||||
.SetMethod("isGuest", &WebContents::IsGuest)
|
||||
.SetMethod("isOffscreen", &WebContents::IsOffScreen)
|
||||
.SetMethod("startPainting", &WebContents::StartPainting)
|
||||
.SetMethod("stopPainting", &WebContents::StopPainting)
|
||||
.SetMethod("isPainting", &WebContents::IsPainting)
|
||||
.SetMethod("setFrameRate", &WebContents::SetFrameRate)
|
||||
.SetMethod("getFrameRate", &WebContents::GetFrameRate)
|
||||
.SetMethod("getType", &WebContents::GetType)
|
||||
.SetMethod("getWebPreferences", &WebContents::GetWebPreferences)
|
||||
.SetMethod("getOwnerBrowserWindow", &WebContents::GetOwnerBrowserWindow)
|
||||
|
@ -1525,13 +1531,6 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
|
|||
&WebContents::ShowDefinitionForSelection)
|
||||
.SetMethod("copyImageAt", &WebContents::CopyImageAt)
|
||||
.SetMethod("capturePage", &WebContents::CapturePage)
|
||||
.SetMethod("isFocused", &WebContents::IsFocused)
|
||||
.SetMethod("isOffscreen", &WebContents::IsOffScreen)
|
||||
.SetMethod("startPainting", &WebContents::StartPainting)
|
||||
.SetMethod("stopPainting", &WebContents::StopPainting)
|
||||
.SetMethod("isPainting", &WebContents::IsPainting)
|
||||
.SetMethod("setFrameRate", &WebContents::SetFrameRate)
|
||||
.SetMethod("getFrameRate", &WebContents::GetFrameRate)
|
||||
.SetProperty("id", &WebContents::ID)
|
||||
.SetProperty("session", &WebContents::Session)
|
||||
.SetProperty("hostWebContents", &WebContents::HostWebContents)
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
#include "atom/browser/api/save_page_handler.h"
|
||||
#include "atom/browser/api/trackable_object.h"
|
||||
#include "atom/browser/common_web_contents_delegate.h"
|
||||
#include "atom/browser/osr/osr_output_device.h"
|
||||
#include "content/public/browser/web_contents_observer.h"
|
||||
#include "content/public/common/favicon_url.h"
|
||||
#include "content/common/cursors/webcursor.h"
|
||||
|
@ -69,7 +68,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
|||
|
||||
int GetID() const;
|
||||
Type GetType() const;
|
||||
bool IsFocused() const;
|
||||
bool Equal(const WebContents* web_contents) const;
|
||||
void LoadURL(const GURL& url, const mate::Dictionary& options);
|
||||
void DownloadURL(const GURL& url);
|
||||
|
@ -132,6 +130,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
|||
|
||||
// Focus.
|
||||
void Focus();
|
||||
bool IsFocused() const;
|
||||
void TabTraverse(bool reverse);
|
||||
|
||||
// Send messages to browser.
|
||||
|
@ -158,8 +157,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
|||
bool IsGuest() const;
|
||||
|
||||
// Methods for offscreen rendering
|
||||
void OnPaint(v8::Isolate*, const gfx::Rect&, int, int, void*);
|
||||
bool IsOffScreen() const;
|
||||
void OnPaint(v8::Isolate* isolate, const gfx::Rect& dirty_rect,
|
||||
const gfx::Size& bitmap_size, void* bitmap_pixels);
|
||||
void StartPainting();
|
||||
void StopPainting();
|
||||
bool IsPainting() const;
|
||||
|
@ -290,8 +290,6 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
|||
private:
|
||||
AtomBrowserContext* GetBrowserContext() const;
|
||||
|
||||
atom::OnPaintCallback paint_callback_;
|
||||
|
||||
uint32_t GetNextRequestId() {
|
||||
return ++request_id_;
|
||||
}
|
||||
|
|
|
@ -301,7 +301,7 @@ class AtomCopyFrameGenerator {
|
|||
class AtomBeginFrameTimer : public cc::DelayBasedTimeSourceClient {
|
||||
public:
|
||||
AtomBeginFrameTimer(int frame_rate_threshold_ms,
|
||||
const base::Closure& callback)
|
||||
const base::Closure& callback)
|
||||
: callback_(callback) {
|
||||
time_source_ = cc::DelayBasedTimeSource::Create(
|
||||
base::TimeDelta::FromMilliseconds(frame_rate_threshold_ms),
|
||||
|
@ -341,7 +341,6 @@ OffScreenRenderWidgetHostView::OffScreenRenderWidgetHostView(
|
|||
render_widget_host_(content::RenderWidgetHostImpl::From(host)),
|
||||
native_window_(native_window),
|
||||
software_output_device_(NULL),
|
||||
callback_(nullptr),
|
||||
frame_rate_(60),
|
||||
frame_rate_threshold_ms_(0),
|
||||
transparent_(transparent),
|
||||
|
@ -450,7 +449,7 @@ bool OffScreenRenderWidgetHostView::OnMessageReceived(
|
|||
}
|
||||
|
||||
void OffScreenRenderWidgetHostView::SetPaintCallback(
|
||||
const OnPaintCallback* callback) {
|
||||
const OnPaintCallback& callback) {
|
||||
callback_ = callback;
|
||||
}
|
||||
|
||||
|
@ -851,8 +850,9 @@ void OffScreenRenderWidgetHostView::OnPaint(
|
|||
void* bitmap_pixels) {
|
||||
TRACE_EVENT0("electron", "OffScreenRenderWidgetHostView::OnPaint");
|
||||
|
||||
if (callback_ != nullptr) {
|
||||
callback_->Run(damage_rect, bitmap_width, bitmap_height, bitmap_pixels);
|
||||
if (!callback_.is_null()) {
|
||||
callback_.Run(damage_rect, gfx::Size(bitmap_width, bitmap_height),
|
||||
bitmap_pixels);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -195,7 +195,7 @@ class OffScreenRenderWidgetHostView:
|
|||
void CreatePlatformWidget();
|
||||
void DestroyPlatformWidget();
|
||||
|
||||
void SetPaintCallback(const atom::OnPaintCallback*);
|
||||
void SetPaintCallback(const OnPaintCallback& callback);
|
||||
|
||||
void OnPaint(const gfx::Rect& damage_rect,
|
||||
int bitmap_width,
|
||||
|
@ -219,7 +219,7 @@ private:
|
|||
|
||||
OffScreenOutputDevice* software_output_device_;
|
||||
|
||||
const atom::OnPaintCallback* callback_;
|
||||
const OnPaintCallback callback_;
|
||||
|
||||
int frame_rate_;
|
||||
int frame_rate_threshold_ms_;
|
||||
|
|
Loading…
Reference in a new issue