chore: make WebContentsView take webPreferences as parameter (#22997)

* chore: add WebContentsView.webContents helper

* chore: no need to handle webContents option

* chore: Create WebContentsView in C++

* chore: make WebContentsView accept web_preferences

* fix: nativeWindowOpen still passes WebContents to BrowserWindow

* chore: no more need of WebContentsViewRelay

* test: WebContentsView now takes options

* fix: avoid creating 2 constructors
This commit is contained in:
Cheng Zhao 2020-04-09 16:01:16 +09:00 committed by GitHub
parent de893360f7
commit ca947307db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 108 additions and 92 deletions

View file

@ -4,52 +4,32 @@
#include "shell/browser/api/electron_api_web_contents_view.h"
#include "content/public/browser/web_contents_user_data.h"
#include "base/no_destructor.h"
#include "shell/browser/api/electron_api_web_contents.h"
#include "shell/browser/browser.h"
#include "shell/browser/ui/inspectable_web_contents_view.h"
#include "shell/browser/web_contents_preferences.h"
#include "shell/common/gin_converters/value_converter.h"
#include "shell/common/gin_helper/constructor.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/node_includes.h"
#if defined(OS_MACOSX)
#include "shell/browser/ui/cocoa/delayed_native_view_host.h"
#endif
namespace {
// Used to indicate whether a WebContents already has a view.
class WebContentsViewRelay
: public content::WebContentsUserData<WebContentsViewRelay> {
public:
~WebContentsViewRelay() override = default;
private:
explicit WebContentsViewRelay(content::WebContents* contents) {}
friend class content::WebContentsUserData<WebContentsViewRelay>;
electron::api::WebContentsView* view_ = nullptr;
WEB_CONTENTS_USER_DATA_KEY_DECL();
DISALLOW_COPY_AND_ASSIGN(WebContentsViewRelay);
};
WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsViewRelay)
} // namespace
namespace electron {
namespace api {
WebContentsView::WebContentsView(v8::Isolate* isolate,
gin::Handle<WebContents> web_contents,
InspectableWebContents* iwc)
gin::Handle<WebContents> web_contents)
#if defined(OS_MACOSX)
: View(new DelayedNativeViewHost(iwc->GetView()->GetNativeView())),
: View(new DelayedNativeViewHost(
web_contents->managed_web_contents()->GetView()->GetNativeView())),
#else
: View(iwc->GetView()->GetView()),
: View(web_contents->managed_web_contents()->GetView()->GetView()),
#endif
web_contents_(isolate, web_contents->GetWrapper()),
api_web_contents_(web_contents.get()) {
@ -59,7 +39,6 @@ WebContentsView::WebContentsView(v8::Isolate* isolate,
// managed by InspectableWebContents.
set_delete_view(false);
#endif
WebContentsViewRelay::CreateForWebContents(web_contents->web_contents());
Observe(web_contents->web_contents());
}
@ -78,30 +57,66 @@ WebContentsView::~WebContentsView() {
}
}
gin::Handle<WebContents> WebContentsView::GetWebContents(v8::Isolate* isolate) {
return gin::CreateHandle(isolate, api_web_contents_);
}
void WebContentsView::WebContentsDestroyed() {
api_web_contents_ = nullptr;
web_contents_.Reset();
}
// static
gin::Handle<WebContentsView> WebContentsView::Create(
v8::Isolate* isolate,
const gin_helper::Dictionary& web_preferences) {
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Value> arg = gin::ConvertToV8(isolate, web_preferences);
v8::Local<v8::Object> obj;
if (GetConstructor(isolate)->NewInstance(context, 1, &arg).ToLocal(&obj)) {
gin::Handle<WebContentsView> web_contents_view;
if (gin::ConvertFromV8(isolate, obj, &web_contents_view))
return web_contents_view;
}
return gin::Handle<WebContentsView>();
}
// static
v8::Local<v8::Function> WebContentsView::GetConstructor(v8::Isolate* isolate) {
static base::NoDestructor<v8::Global<v8::Function>> constructor;
if (constructor.get()->IsEmpty()) {
constructor->Reset(
isolate, gin_helper::CreateConstructor<WebContentsView>(
isolate, base::BindRepeating(&WebContentsView::New)));
}
return v8::Local<v8::Function>::New(isolate, *constructor.get());
}
// static
gin_helper::WrappableBase* WebContentsView::New(
gin_helper::Arguments* args,
gin::Handle<WebContents> web_contents) {
// Currently we only support InspectableWebContents, e.g. the WebContents
// created by users directly. To support devToolsWebContents we need to create
// a wrapper view.
if (!web_contents->managed_web_contents()) {
args->ThrowError("The WebContents must be created by user");
return nullptr;
}
// Check if the WebContents has already been added to a view.
if (WebContentsViewRelay::FromWebContents(web_contents->web_contents())) {
args->ThrowError("The WebContents has already been added to a View");
return nullptr;
const gin_helper::Dictionary& web_preferences) {
// Check if BrowserWindow has passend |webContents| option to us.
gin::Handle<WebContents> web_contents;
if (web_preferences.GetHidden("webContents", &web_contents) &&
!web_contents.IsEmpty()) {
// Set webPreferences from options if using an existing webContents.
// These preferences will be used when the webContent launches new
// render processes.
auto* existing_preferences =
WebContentsPreferences::From(web_contents->web_contents());
base::DictionaryValue web_preferences_dict;
if (gin::ConvertFromV8(args->isolate(), web_preferences.GetHandle(),
&web_preferences_dict)) {
existing_preferences->Clear();
existing_preferences->Merge(web_preferences_dict);
}
} else {
// Create one if not.
web_contents = WebContents::Create(args->isolate(), web_preferences);
}
// Constructor call.
auto* view = new WebContentsView(args->isolate(), web_contents,
web_contents->managed_web_contents());
auto* view = new WebContentsView(args->isolate(), web_contents);
view->InitWithArgs(args);
return view;
}
@ -109,7 +124,11 @@ gin_helper::WrappableBase* WebContentsView::New(
// static
void WebContentsView::BuildPrototype(
v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {}
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(gin::StringToV8(isolate, "WebContentsView"));
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetProperty("webContents", &WebContentsView::GetWebContents);
}
} // namespace api
@ -125,9 +144,7 @@ void Initialize(v8::Local<v8::Object> exports,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
gin_helper::Dictionary dict(isolate, exports);
dict.Set("WebContentsView",
gin_helper::CreateConstructor<WebContentsView>(
isolate, base::BindRepeating(&WebContentsView::New)));
dict.Set("WebContentsView", WebContentsView::GetConstructor(isolate));
}
} // namespace