2018-05-07 07:04:33 +00:00
|
|
|
// Copyright (c) 2018 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/api/electron_api_web_contents_view.h"
|
2018-05-07 07:04:33 +00:00
|
|
|
|
2020-04-09 07:01:16 +00:00
|
|
|
#include "base/no_destructor.h"
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/api/electron_api_web_contents.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/browser.h"
|
|
|
|
#include "shell/browser/ui/inspectable_web_contents_view.h"
|
2020-04-09 07:01:16 +00:00
|
|
|
#include "shell/browser/web_contents_preferences.h"
|
|
|
|
#include "shell/common/gin_converters/value_converter.h"
|
2019-12-05 09:46:34 +00:00
|
|
|
#include "shell/common/gin_helper/constructor.h"
|
2019-10-25 13:03:28 +00:00
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2020-04-09 07:01:16 +00:00
|
|
|
#include "shell/common/gin_helper/object_template_builder.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
2018-05-08 03:31:47 +00:00
|
|
|
|
2020-08-12 18:33:58 +00:00
|
|
|
#if defined(OS_MAC)
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/ui/cocoa/delayed_native_view_host.h"
|
2018-05-08 03:31:47 +00:00
|
|
|
#endif
|
2018-05-07 07:04:33 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2018-05-07 07:04:33 +00:00
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2018-05-16 10:29:44 +00:00
|
|
|
WebContentsView::WebContentsView(v8::Isolate* isolate,
|
2020-04-09 07:01:16 +00:00
|
|
|
gin::Handle<WebContents> web_contents)
|
2020-08-12 18:33:58 +00:00
|
|
|
#if defined(OS_MAC)
|
2020-04-09 07:01:16 +00:00
|
|
|
: View(new DelayedNativeViewHost(
|
|
|
|
web_contents->managed_web_contents()->GetView()->GetNativeView())),
|
2018-05-07 07:04:33 +00:00
|
|
|
#else
|
2020-04-09 07:01:16 +00:00
|
|
|
: View(web_contents->managed_web_contents()->GetView()->GetView()),
|
2018-05-07 07:04:33 +00:00
|
|
|
#endif
|
2020-07-16 23:16:05 +00:00
|
|
|
web_contents_(isolate, web_contents.ToV8()),
|
2018-05-16 10:29:44 +00:00
|
|
|
api_web_contents_(web_contents.get()) {
|
2020-08-12 18:33:58 +00:00
|
|
|
#if !defined(OS_MAC)
|
2020-03-20 06:41:41 +00:00
|
|
|
// On macOS the View is a newly-created |DelayedNativeViewHost| and it is our
|
|
|
|
// responsibility to delete it. On other platforms the View is created and
|
|
|
|
// managed by InspectableWebContents.
|
2018-05-07 07:04:33 +00:00
|
|
|
set_delete_view(false);
|
|
|
|
#endif
|
2018-05-21 01:50:26 +00:00
|
|
|
Observe(web_contents->web_contents());
|
2018-05-16 10:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WebContentsView::~WebContentsView() {
|
2018-11-08 15:57:28 +00:00
|
|
|
if (api_web_contents_) { // destroy() is called
|
2020-03-13 17:33:37 +00:00
|
|
|
// Destroy WebContents asynchronously, as we might be in GC currently and
|
|
|
|
// WebContents emits an event in its destructor.
|
|
|
|
base::PostTask(FROM_HERE, {content::BrowserThread::UI},
|
|
|
|
base::BindOnce(
|
|
|
|
[](base::WeakPtr<WebContents> contents) {
|
|
|
|
if (contents)
|
|
|
|
contents->DestroyWebContents(
|
|
|
|
!Browser::Get()->is_shutting_down());
|
|
|
|
},
|
|
|
|
api_web_contents_->GetWeakPtr()));
|
2018-11-08 15:57:28 +00:00
|
|
|
}
|
2018-05-07 07:04:33 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 07:01:16 +00:00
|
|
|
gin::Handle<WebContents> WebContentsView::GetWebContents(v8::Isolate* isolate) {
|
|
|
|
return gin::CreateHandle(isolate, api_web_contents_);
|
|
|
|
}
|
|
|
|
|
2018-05-21 01:50:26 +00:00
|
|
|
void WebContentsView::WebContentsDestroyed() {
|
2018-05-18 07:31:43 +00:00
|
|
|
api_web_contents_ = nullptr;
|
|
|
|
web_contents_.Reset();
|
2018-05-16 10:29:44 +00:00
|
|
|
}
|
2018-05-07 07:04:33 +00:00
|
|
|
|
2020-04-09 07:01:16 +00:00
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
|
2018-05-07 07:04:33 +00:00
|
|
|
// static
|
2019-12-05 09:46:34 +00:00
|
|
|
gin_helper::WrappableBase* WebContentsView::New(
|
2019-10-25 13:03:28 +00:00
|
|
|
gin_helper::Arguments* args,
|
2020-04-09 07:01:16 +00:00
|
|
|
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);
|
2018-05-17 08:01:56 +00:00
|
|
|
}
|
|
|
|
// Constructor call.
|
2020-04-09 07:01:16 +00:00
|
|
|
auto* view = new WebContentsView(args->isolate(), web_contents);
|
2019-10-15 01:15:23 +00:00
|
|
|
view->InitWithArgs(args);
|
2018-05-08 05:47:26 +00:00
|
|
|
return view;
|
2018-05-07 07:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void WebContentsView::BuildPrototype(
|
|
|
|
v8::Isolate* isolate,
|
2020-04-09 07:01:16 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> prototype) {
|
|
|
|
prototype->SetClassName(gin::StringToV8(isolate, "WebContentsView"));
|
|
|
|
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
|
|
|
.SetProperty("webContents", &WebContentsView::GetWebContents);
|
|
|
|
}
|
2018-05-07 07:04:33 +00:00
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|
2018-05-07 07:04:33 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
using electron::api::WebContentsView;
|
2018-05-07 07:04:33 +00:00
|
|
|
|
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2019-10-25 13:03:28 +00:00
|
|
|
gin_helper::Dictionary dict(isolate, exports);
|
2020-04-09 07:01:16 +00:00
|
|
|
dict.Set("WebContentsView", WebContentsView::GetConstructor(isolate));
|
2018-05-07 07:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-02-14 11:25:39 +00:00
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_browser_web_contents_view, Initialize)
|