refactor: ginify BrowserView (#23578)
This commit is contained in:
parent
66d65a6d35
commit
9bd0fc5348
12 changed files with 128 additions and 165 deletions
|
@ -738,11 +738,11 @@ void BaseWindow::AddBrowserView(v8::Local<v8::Value> value) {
|
|||
gin::Handle<BrowserView> browser_view;
|
||||
if (value->IsObject() &&
|
||||
gin::ConvertFromV8(isolate(), value, &browser_view)) {
|
||||
auto get_that_view = browser_views_.find(browser_view->weak_map_id());
|
||||
auto get_that_view = browser_views_.find(browser_view->ID());
|
||||
if (get_that_view == browser_views_.end()) {
|
||||
window_->AddBrowserView(browser_view->view());
|
||||
browser_view->web_contents()->SetOwnerWindow(window_.get());
|
||||
browser_views_[browser_view->weak_map_id()].Reset(isolate(), value);
|
||||
browser_views_[browser_view->ID()].Reset(isolate(), value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -751,7 +751,7 @@ void BaseWindow::RemoveBrowserView(v8::Local<v8::Value> value) {
|
|||
gin::Handle<BrowserView> browser_view;
|
||||
if (value->IsObject() &&
|
||||
gin::ConvertFromV8(isolate(), value, &browser_view)) {
|
||||
auto get_that_view = browser_views_.find(browser_view->weak_map_id());
|
||||
auto get_that_view = browser_views_.find(browser_view->ID());
|
||||
if (get_that_view != browser_views_.end()) {
|
||||
window_->RemoveBrowserView(browser_view->view());
|
||||
browser_view->web_contents()->SetOwnerWindow(nullptr);
|
||||
|
|
|
@ -52,12 +52,24 @@ struct Converter<electron::AutoResizeFlags> {
|
|||
|
||||
} // namespace gin
|
||||
|
||||
namespace {
|
||||
|
||||
int32_t GetNextId() {
|
||||
static int32_t next_id = 1;
|
||||
return next_id++;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace electron {
|
||||
|
||||
namespace api {
|
||||
|
||||
gin::WrapperInfo BrowserView::kWrapperInfo = {gin::kEmbedderNativeGin};
|
||||
|
||||
BrowserView::BrowserView(gin::Arguments* args,
|
||||
const gin_helper::Dictionary& options) {
|
||||
const gin_helper::Dictionary& options)
|
||||
: id_(GetNextId()) {
|
||||
v8::Isolate* isolate = args->isolate();
|
||||
gin_helper::Dictionary web_preferences =
|
||||
gin::Dictionary::CreateEmpty(isolate);
|
||||
|
@ -72,8 +84,6 @@ BrowserView::BrowserView(gin::Arguments* args,
|
|||
|
||||
view_.reset(
|
||||
NativeBrowserView::Create(api_web_contents_->managed_web_contents()));
|
||||
|
||||
InitWithArgs(args);
|
||||
}
|
||||
|
||||
BrowserView::~BrowserView() {
|
||||
|
@ -87,24 +97,24 @@ BrowserView::~BrowserView() {
|
|||
void BrowserView::WebContentsDestroyed() {
|
||||
api_web_contents_ = nullptr;
|
||||
web_contents_.Reset();
|
||||
Unpin();
|
||||
}
|
||||
|
||||
// static
|
||||
gin_helper::WrappableBase* BrowserView::New(gin_helper::ErrorThrower thrower,
|
||||
gin::Arguments* args) {
|
||||
gin::Handle<BrowserView> BrowserView::New(gin_helper::ErrorThrower thrower,
|
||||
gin::Arguments* args) {
|
||||
if (!Browser::Get()->is_ready()) {
|
||||
thrower.ThrowError("Cannot create BrowserView before app is ready");
|
||||
return nullptr;
|
||||
return gin::Handle<BrowserView>();
|
||||
}
|
||||
|
||||
gin::Dictionary options = gin::Dictionary::CreateEmpty(args->isolate());
|
||||
args->GetNext(&options);
|
||||
|
||||
return new BrowserView(args, options);
|
||||
}
|
||||
|
||||
int32_t BrowserView::ID() const {
|
||||
return weak_map_id();
|
||||
auto handle =
|
||||
gin::CreateHandle(args->isolate(), new BrowserView(args, options));
|
||||
handle->Pin(args->isolate());
|
||||
return handle;
|
||||
}
|
||||
|
||||
void BrowserView::SetAutoResize(AutoResizeFlags flags) {
|
||||
|
@ -123,26 +133,25 @@ void BrowserView::SetBackgroundColor(const std::string& color_name) {
|
|||
view_->SetBackgroundColor(ParseHexColor(color_name));
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> BrowserView::GetWebContents() {
|
||||
v8::Local<v8::Value> BrowserView::GetWebContents(v8::Isolate* isolate) {
|
||||
if (web_contents_.IsEmpty()) {
|
||||
return v8::Null(isolate());
|
||||
return v8::Null(isolate);
|
||||
}
|
||||
|
||||
return v8::Local<v8::Value>::New(isolate(), web_contents_);
|
||||
return v8::Local<v8::Value>::New(isolate, web_contents_);
|
||||
}
|
||||
|
||||
// static
|
||||
void BrowserView::BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Local<v8::FunctionTemplate> prototype) {
|
||||
prototype->SetClassName(gin::StringToV8(isolate, "BrowserView"));
|
||||
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
|
||||
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
||||
v8::Local<v8::ObjectTemplate> BrowserView::FillObjectTemplate(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::ObjectTemplate> templ) {
|
||||
return gin::ObjectTemplateBuilder(isolate, "BrowserView", templ)
|
||||
.SetMethod("setAutoResize", &BrowserView::SetAutoResize)
|
||||
.SetMethod("setBounds", &BrowserView::SetBounds)
|
||||
.SetMethod("getBounds", &BrowserView::GetBounds)
|
||||
.SetMethod("setBackgroundColor", &BrowserView::SetBackgroundColor)
|
||||
.SetProperty("webContents", &BrowserView::GetWebContents)
|
||||
.SetProperty("id", &BrowserView::ID);
|
||||
.Build();
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
@ -158,16 +167,9 @@ void Initialize(v8::Local<v8::Object> exports,
|
|||
v8::Local<v8::Context> context,
|
||||
void* priv) {
|
||||
v8::Isolate* isolate = context->GetIsolate();
|
||||
BrowserView::SetConstructor(isolate, base::BindRepeating(&BrowserView::New));
|
||||
|
||||
gin_helper::Dictionary browser_view(isolate,
|
||||
BrowserView::GetConstructor(isolate)
|
||||
->GetFunction(context)
|
||||
.ToLocalChecked());
|
||||
browser_view.SetMethod("fromId", &BrowserView::FromWeakMapID);
|
||||
browser_view.SetMethod("getAllViews", &BrowserView::GetAll);
|
||||
gin_helper::Dictionary dict(isolate, exports);
|
||||
dict.Set("BrowserView", browser_view);
|
||||
dict.Set("BrowserView", BrowserView::GetConstructor(context));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -10,9 +10,11 @@
|
|||
|
||||
#include "content/public/browser/web_contents_observer.h"
|
||||
#include "gin/handle.h"
|
||||
#include "gin/wrappable.h"
|
||||
#include "shell/browser/native_browser_view.h"
|
||||
#include "shell/common/gin_helper/constructible.h"
|
||||
#include "shell/common/gin_helper/error_thrower.h"
|
||||
#include "shell/common/gin_helper/trackable_object.h"
|
||||
#include "shell/common/gin_helper/pinnable.h"
|
||||
|
||||
namespace gfx {
|
||||
class Rect;
|
||||
|
@ -30,19 +32,25 @@ namespace api {
|
|||
|
||||
class WebContents;
|
||||
|
||||
class BrowserView : public gin_helper::TrackableObject<BrowserView>,
|
||||
class BrowserView : public gin::Wrappable<BrowserView>,
|
||||
public gin_helper::Constructible<BrowserView>,
|
||||
public gin_helper::Pinnable<BrowserView>,
|
||||
public content::WebContentsObserver {
|
||||
public:
|
||||
static gin_helper::WrappableBase* New(gin_helper::ErrorThrower thrower,
|
||||
gin::Arguments* args);
|
||||
// gin_helper::Constructible
|
||||
static gin::Handle<BrowserView> New(gin_helper::ErrorThrower thrower,
|
||||
gin::Arguments* args);
|
||||
static v8::Local<v8::ObjectTemplate> FillObjectTemplate(
|
||||
v8::Isolate*,
|
||||
v8::Local<v8::ObjectTemplate>);
|
||||
|
||||
static void BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Local<v8::FunctionTemplate> prototype);
|
||||
// gin::Wrappable
|
||||
static gin::WrapperInfo kWrapperInfo;
|
||||
|
||||
WebContents* web_contents() const { return api_web_contents_; }
|
||||
NativeBrowserView* view() const { return view_.get(); }
|
||||
|
||||
int32_t ID() const;
|
||||
int32_t ID() const { return id_; }
|
||||
|
||||
protected:
|
||||
BrowserView(gin::Arguments* args, const gin_helper::Dictionary& options);
|
||||
|
@ -56,13 +64,15 @@ class BrowserView : public gin_helper::TrackableObject<BrowserView>,
|
|||
void SetBounds(const gfx::Rect& bounds);
|
||||
gfx::Rect GetBounds();
|
||||
void SetBackgroundColor(const std::string& color_name);
|
||||
v8::Local<v8::Value> GetWebContents();
|
||||
v8::Local<v8::Value> GetWebContents(v8::Isolate*);
|
||||
|
||||
v8::Global<v8::Value> web_contents_;
|
||||
class WebContents* api_web_contents_ = nullptr;
|
||||
|
||||
std::unique_ptr<NativeBrowserView> view_;
|
||||
|
||||
int32_t id_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(BrowserView);
|
||||
};
|
||||
|
||||
|
|
|
@ -276,6 +276,10 @@ void BrowserWindow::OnCloseButtonClicked(bool* prevent_default) {
|
|||
}
|
||||
|
||||
void BrowserWindow::OnWindowClosed() {
|
||||
// We need to reset the browser views before we call Cleanup, because the
|
||||
// browser views are child views of the main web contents view, which will be
|
||||
// deleted by Cleanup.
|
||||
BaseWindow::ResetBrowserViews();
|
||||
Cleanup();
|
||||
// See BaseWindow::OnWindowClosed on why calling InvalidateWeakPtrs.
|
||||
weak_factory_.InvalidateWeakPtrs();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue