2018-05-07 16:04:33 +09: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.
|
|
|
|
|
2019-06-19 13:46:59 -07:00
|
|
|
#include "shell/browser/api/electron_api_web_contents_view.h"
|
2018-05-07 16:04:33 +09:00
|
|
|
|
2020-04-09 16:01:16 +09:00
|
|
|
#include "base/no_destructor.h"
|
2023-12-13 13:01:03 -08:00
|
|
|
#include "gin/data_object_builder.h"
|
2019-06-19 13:46:59 -07:00
|
|
|
#include "shell/browser/api/electron_api_web_contents.h"
|
|
|
|
#include "shell/browser/browser.h"
|
2023-12-13 13:01:03 -08:00
|
|
|
#include "shell/browser/native_window.h"
|
2024-07-29 12:42:57 -05:00
|
|
|
#include "shell/browser/ui/inspectable_web_contents.h"
|
2019-06-19 13:46:59 -07:00
|
|
|
#include "shell/browser/ui/inspectable_web_contents_view.h"
|
2020-04-09 16:01:16 +09:00
|
|
|
#include "shell/browser/web_contents_preferences.h"
|
2023-12-13 13:01:03 -08:00
|
|
|
#include "shell/common/gin_converters/gfx_converter.h"
|
2020-04-09 16:01:16 +09:00
|
|
|
#include "shell/common/gin_converters/value_converter.h"
|
2019-12-05 18:46:34 +09:00
|
|
|
#include "shell/common/gin_helper/constructor.h"
|
2019-10-25 22:03:28 +09:00
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2020-04-09 16:01:16 +09:00
|
|
|
#include "shell/common/gin_helper/object_template_builder.h"
|
2019-06-19 13:46:59 -07:00
|
|
|
#include "shell/common/node_includes.h"
|
2023-12-13 13:01:03 -08:00
|
|
|
#include "shell/common/options_switches.h"
|
2022-11-07 10:15:57 -08:00
|
|
|
#include "third_party/skia/include/core/SkRegion.h"
|
|
|
|
#include "ui/base/hit_test.h"
|
2024-07-16 20:16:25 -04:00
|
|
|
#include "ui/gfx/geometry/rounded_corners_f.h"
|
|
|
|
#include "ui/views/controls/webview/webview.h"
|
2023-12-13 13:01:03 -08:00
|
|
|
#include "ui/views/layout/flex_layout_types.h"
|
|
|
|
#include "ui/views/view_class_properties.h"
|
|
|
|
#include "ui/views/widget/widget.h"
|
2018-05-08 12:31:47 +09:00
|
|
|
|
2022-06-29 12:55:47 -07:00
|
|
|
namespace electron::api {
|
2018-05-07 16:04:33 +09:00
|
|
|
|
2018-05-16 19:29:44 +09:00
|
|
|
WebContentsView::WebContentsView(v8::Isolate* isolate,
|
2020-04-09 16:01:16 +09:00
|
|
|
gin::Handle<WebContents> web_contents)
|
refactor: remove InspectableWebContentsViewMac in favor of the Views version (#45238)
* refactor: remove InspectableWebContentsViewMac in favor of the Views version
* cherry-pick: refactor: remove InspectableWebContentsViewMac in favor of the Views version (#41326)
commit e67ab9a93dadccecff30de50ab4555191c30b6c4
Confilcts not resolved, except removal of the files removed
by the original commit.
* resolved conflicts and build issues after cherry-pick
* cherry-picked: fix: add method allowing to disable headless mode in native widget
https://github.com/electron/electron/pull/42996
fixing
https://github.com/electron/electron/issues/42995
* fix: displaying select popup in window created as fullscreen window
`constrainFrameRect:toScreen:` is not being call for windows created
with `fullscreen: true` therefore `headless` mode was not being removed
and `RenderWidgetHostNSViewBridge::DisplayPopupMenu` ignored displaying
popup.
Issue could be fixed by placing additional removal of `headless` mode
in the `toggleFullScreen:`, but `orderWindow:relativeTo:` is called
both for a regular and a fullscreen window, therefore there will be
a single place fixing both cases.
Because `electron::NativeWindowMac` lifetime may be shorter than
`ElectronNSWindow` on which macOS may execute `orderWindow:relativeTo:`
we need to clear `shell_` when `NativeWindow` is being closed.
Fixes #43010.
* fix: Content visibility when using `vibrancy`
We need to put `NSVisualEffectView` before `ViewsCompositorSuperview`
otherwise when using `vibrancy` in `BrowserWindow` `NSVisualEffectView`
will hide content displayed by the compositor.
Fixes #43003
Fixes #42336
In fact main issues reported in these tickets were not present after
cherry-picking original refactor switching to `views::WebView`, so
text could be selected and click event was properly generated. However
both issues testcases were using `vibrancy` and actual content was
invisible, because it was covered by the `NSVisualEffectView`.
* fix: EXCEPTION_ACCESS_VIOLATION crash on BrowserWindow.destroy()
Restored postponed deletion of the `NativeWindow`.
Restoration caused `DCHECK(new_parent_ui_layer->GetCompositor());` failure
in `BrowserCompositorMac::SetParentUiLayer` after the spec test:
`chrome extensions chrome.webRequest does not take precedence over Electron webRequest - http`
with stack:
```
7 Electron Framework 0x000000011fe07830 content::BrowserCompositorMac::SetParentUiLayer(ui::Layer*) + 628
8 Electron Framework 0x000000011fe0c154 content::RenderWidgetHostViewMac::SetParentUiLayer(ui::Layer*) + 220
9 Electron Framework 0x000000011fe226a8 content::WebContentsViewMac::CreateViewForWidget(content::RenderWidgetHost*) + 600
10 Electron Framework 0x000000011fd37e4c content::WebContentsImpl::CreateRenderWidgetHostViewForRenderManager(content::RenderViewHost*) + 164
11 Electron Framework 0x000000011fb32278 content::RenderFrameHostManager::CreateSpeculativeRenderFrame(content::SiteInstanceImpl*, bool, scoped_refptr<content::BrowsingContextState> const&) + 816
12 Electron Framework 0x000000011fb2ab8c content::RenderFrameHostManager::CreateSpeculativeRenderFrameHost(content::SiteInstanceImpl*, content::SiteInstanceImpl*, bool) + 1308
13 Electron Framework 0x000000011fb28598 content::RenderFrameHostManager::GetFrameHostForNavigation(content::NavigationRequest*, content::BrowsingContextGroupSwap*, std::__Cr::basic_string<char, std::__Cr::char_traits<char>, std::__Cr::allocator<char>>*) + 1796
14 Electron Framework 0x000000011fa78660 content::NavigationRequest::SelectFrameHostForOnRequestFailedInternal(bool, bool, std::__Cr::optional<std::__Cr::basic_string<char, std::__Cr::char_traits<char>, std::__Cr::allocator<char>>> const&) + 280
15 Electron Framework 0x000000011fa6a994 content::NavigationRequest::OnRequestFailedInternal(network::URLLoaderCompletionStatus const&, bool, std::__Cr::optional<std::__Cr::basic_string<char, std::__Cr::char_traits<char>, std::__Cr::allocator<char>>> const&, bo
+ 1008
16 Electron Framework 0x000000011fa7772c content::NavigationRequest::OnRequestFailed(network::URLLoaderCompletionStatus const&) + 72
17 Electron Framework 0x000000011f8554ac content::NavigationURLLoaderImpl::NotifyRequestFailed(network::URLLoaderCompletionStatus const&) + 248
```
This was probably the reason of removing `NativeWindow` immediately
in order to cleanup `views_host_` in `WebContentsViewMac` to prevent
using layer without compositor in `WebContentsViewMac::CreateViewForWidget`.
`[ElectronNSWindowDelegate windowWillClose:]` is deleting window host
and the compositor used by the `NativeWindow` therefore detach `NativeWindow`
contents from parent. This will clear `views_host_` and prevent failing
mentioned `DCHECK`.
Fixes #42975
* chore: Applied review suggestions
Co-authored-by: Michał Pichliński <michal.pichlinski@here.io>
* refactor: directly cleanup shell
Co-authored-by: Samuel Maddock <smaddock@slack-corp.com>
---------
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Michał Pichliński <michal.pichlinski@here.io>
Co-authored-by: Samuel Maddock <smaddock@slack-corp.com>
2025-01-23 11:54:15 +01:00
|
|
|
: View(web_contents->inspectable_web_contents()->GetView()),
|
2020-07-16 16:16:05 -07:00
|
|
|
web_contents_(isolate, web_contents.ToV8()),
|
2018-05-16 19:29:44 +09:00
|
|
|
api_web_contents_(web_contents.get()) {
|
2018-05-07 16:04:33 +09:00
|
|
|
set_delete_view(false);
|
2023-12-13 13:01:03 -08:00
|
|
|
view()->SetProperty(
|
|
|
|
views::kFlexBehaviorKey,
|
|
|
|
views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToMinimum,
|
|
|
|
views::MaximumFlexSizeRule::kUnbounded));
|
2018-05-21 10:50:26 +09:00
|
|
|
Observe(web_contents->web_contents());
|
2018-05-16 19:29:44 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
WebContentsView::~WebContentsView() {
|
2021-03-07 21:14:12 +09:00
|
|
|
if (api_web_contents_) // destroy() called without closing WebContents
|
|
|
|
api_web_contents_->Destroy();
|
2018-05-07 16:04:33 +09:00
|
|
|
}
|
|
|
|
|
2020-04-09 16:01:16 +09:00
|
|
|
gin::Handle<WebContents> WebContentsView::GetWebContents(v8::Isolate* isolate) {
|
2023-12-13 13:01:03 -08:00
|
|
|
if (api_web_contents_)
|
|
|
|
return gin::CreateHandle(isolate, api_web_contents_.get());
|
|
|
|
else
|
2024-11-29 11:44:33 -06:00
|
|
|
return {};
|
2023-12-13 13:01:03 -08:00
|
|
|
}
|
|
|
|
|
2024-01-10 23:23:35 +01:00
|
|
|
void WebContentsView::SetBackgroundColor(std::optional<WrappedSkColor> color) {
|
2023-12-13 13:01:03 -08:00
|
|
|
View::SetBackgroundColor(color);
|
|
|
|
if (api_web_contents_) {
|
|
|
|
api_web_contents_->SetBackgroundColor(color);
|
|
|
|
// Also update the web preferences object otherwise the view will be reset
|
|
|
|
// on the next load URL call
|
|
|
|
auto* web_preferences =
|
|
|
|
WebContentsPreferences::From(api_web_contents_->web_contents());
|
|
|
|
if (web_preferences) {
|
|
|
|
web_preferences->SetBackgroundColor(color);
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 16:01:16 +09:00
|
|
|
}
|
|
|
|
|
2024-07-16 20:16:25 -04:00
|
|
|
void WebContentsView::SetBorderRadius(int radius) {
|
|
|
|
View::SetBorderRadius(radius);
|
|
|
|
ApplyBorderRadius();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebContentsView::ApplyBorderRadius() {
|
|
|
|
if (border_radius().has_value() && api_web_contents_ && view()->GetWidget()) {
|
2024-07-27 09:44:22 -07:00
|
|
|
auto* view = api_web_contents_->inspectable_web_contents()->GetView();
|
|
|
|
view->SetCornerRadii(gfx::RoundedCornersF(border_radius().value()));
|
2024-07-16 20:16:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-07 10:15:57 -08:00
|
|
|
int WebContentsView::NonClientHitTest(const gfx::Point& point) {
|
2024-01-30 11:53:19 +01:00
|
|
|
if (api_web_contents_) {
|
|
|
|
gfx::Point local_point(point);
|
|
|
|
views::View::ConvertPointFromWidget(view(), &local_point);
|
|
|
|
SkRegion* region = api_web_contents_->draggable_region();
|
|
|
|
if (region && region->contains(local_point.x(), local_point.y()))
|
|
|
|
return HTCAPTION;
|
|
|
|
}
|
|
|
|
|
2022-11-07 10:15:57 -08:00
|
|
|
return HTNOWHERE;
|
|
|
|
}
|
|
|
|
|
2018-05-21 10:50:26 +09:00
|
|
|
void WebContentsView::WebContentsDestroyed() {
|
2018-05-18 16:31:43 +09:00
|
|
|
api_web_contents_ = nullptr;
|
|
|
|
web_contents_.Reset();
|
2018-05-16 19:29:44 +09:00
|
|
|
}
|
2018-05-07 16:04:33 +09:00
|
|
|
|
2023-12-13 13:01:03 -08:00
|
|
|
void WebContentsView::OnViewAddedToWidget(views::View* observed_view) {
|
|
|
|
DCHECK_EQ(observed_view, view());
|
|
|
|
views::Widget* widget = view()->GetWidget();
|
2024-11-04 12:27:49 -06:00
|
|
|
auto* native_window =
|
|
|
|
static_cast<NativeWindow*>(widget->GetNativeWindowProperty(
|
|
|
|
electron::kElectronNativeWindowKey.c_str()));
|
2023-12-13 13:01:03 -08:00
|
|
|
if (!native_window)
|
|
|
|
return;
|
2024-04-29 19:09:29 -04:00
|
|
|
// We don't need to call SetOwnerWindow(nullptr) in OnViewRemovedFromWidget
|
|
|
|
// because that's handled in the WebContents dtor called prior.
|
|
|
|
api_web_contents_->SetOwnerWindow(native_window);
|
2023-12-13 13:01:03 -08:00
|
|
|
native_window->AddDraggableRegionProvider(this);
|
2024-07-16 20:16:25 -04:00
|
|
|
ApplyBorderRadius();
|
2023-12-13 13:01:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebContentsView::OnViewRemovedFromWidget(views::View* observed_view) {
|
|
|
|
DCHECK_EQ(observed_view, view());
|
|
|
|
views::Widget* widget = view()->GetWidget();
|
|
|
|
auto* native_window = static_cast<NativeWindow*>(
|
2024-11-04 12:27:49 -06:00
|
|
|
widget->GetNativeWindowProperty(kElectronNativeWindowKey.c_str()));
|
2023-12-13 13:01:03 -08:00
|
|
|
if (!native_window)
|
|
|
|
return;
|
|
|
|
native_window->RemoveDraggableRegionProvider(this);
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:01:16 +09:00
|
|
|
// static
|
|
|
|
gin::Handle<WebContentsView> WebContentsView::Create(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const gin_helper::Dictionary& web_preferences) {
|
|
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
2023-12-13 13:01:03 -08:00
|
|
|
v8::Local<v8::Value> arg = gin::DataObjectBuilder(isolate)
|
|
|
|
.Set("webPreferences", web_preferences)
|
|
|
|
.Build();
|
|
|
|
v8::Local<v8::Object> web_contents_view_obj;
|
|
|
|
if (GetConstructor(isolate)
|
|
|
|
->NewInstance(context, 1, &arg)
|
|
|
|
.ToLocal(&web_contents_view_obj)) {
|
2020-04-09 16:01:16 +09:00
|
|
|
gin::Handle<WebContentsView> web_contents_view;
|
2023-12-13 13:01:03 -08:00
|
|
|
if (gin::ConvertFromV8(isolate, web_contents_view_obj, &web_contents_view))
|
2020-04-09 16:01:16 +09:00
|
|
|
return web_contents_view;
|
|
|
|
}
|
2024-11-29 11:44:33 -06:00
|
|
|
return {};
|
2020-04-09 16:01:16 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 16:04:33 +09:00
|
|
|
// static
|
2023-12-13 13:01:03 -08:00
|
|
|
gin_helper::WrappableBase* WebContentsView::New(gin_helper::Arguments* args) {
|
|
|
|
gin_helper::Dictionary web_preferences;
|
2024-05-30 21:45:35 +02:00
|
|
|
v8::Local<v8::Value> existing_web_contents_value;
|
2023-12-13 13:01:03 -08:00
|
|
|
{
|
|
|
|
v8::Local<v8::Value> options_value;
|
|
|
|
if (args->GetNext(&options_value)) {
|
|
|
|
gin_helper::Dictionary options;
|
|
|
|
if (!gin::ConvertFromV8(args->isolate(), options_value, &options)) {
|
|
|
|
args->ThrowError("options must be an object");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
v8::Local<v8::Value> web_preferences_value;
|
|
|
|
if (options.Get("webPreferences", &web_preferences_value)) {
|
|
|
|
if (!gin::ConvertFromV8(args->isolate(), web_preferences_value,
|
|
|
|
&web_preferences)) {
|
|
|
|
args->ThrowError("options.webPreferences must be an object");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2024-05-30 21:45:35 +02:00
|
|
|
|
|
|
|
if (options.Get("webContents", &existing_web_contents_value)) {
|
|
|
|
gin::Handle<WebContents> existing_web_contents;
|
|
|
|
if (!gin::ConvertFromV8(args->isolate(), existing_web_contents_value,
|
|
|
|
&existing_web_contents)) {
|
|
|
|
args->ThrowError("options.webContents must be a WebContents");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (existing_web_contents->owner_window() != nullptr) {
|
|
|
|
args->ThrowError(
|
|
|
|
"options.webContents is already attached to a window");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2023-12-13 13:01:03 -08:00
|
|
|
}
|
|
|
|
}
|
2024-05-30 21:45:35 +02:00
|
|
|
|
2023-12-13 13:01:03 -08:00
|
|
|
if (web_preferences.IsEmpty())
|
|
|
|
web_preferences = gin_helper::Dictionary::CreateEmpty(args->isolate());
|
|
|
|
if (!web_preferences.Has(options::kShow))
|
|
|
|
web_preferences.Set(options::kShow, false);
|
2024-05-30 21:45:35 +02:00
|
|
|
|
|
|
|
if (!existing_web_contents_value.IsEmpty()) {
|
|
|
|
web_preferences.SetHidden("webContents", existing_web_contents_value);
|
|
|
|
}
|
|
|
|
|
2020-12-16 00:52:43 +01:00
|
|
|
auto web_contents =
|
|
|
|
WebContents::CreateFromWebPreferences(args->isolate(), web_preferences);
|
|
|
|
|
2018-05-17 17:01:56 +09:00
|
|
|
// Constructor call.
|
2020-04-09 16:01:16 +09:00
|
|
|
auto* view = new WebContentsView(args->isolate(), web_contents);
|
2019-10-15 10:15:23 +09:00
|
|
|
view->InitWithArgs(args);
|
2018-05-08 14:47:26 +09:00
|
|
|
return view;
|
2018-05-07 16:04:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void WebContentsView::BuildPrototype(
|
|
|
|
v8::Isolate* isolate,
|
2020-04-09 16:01:16 +09:00
|
|
|
v8::Local<v8::FunctionTemplate> prototype) {
|
|
|
|
prototype->SetClassName(gin::StringToV8(isolate, "WebContentsView"));
|
|
|
|
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
2023-12-13 13:01:03 -08:00
|
|
|
.SetMethod("setBackgroundColor", &WebContentsView::SetBackgroundColor)
|
2024-07-16 20:16:25 -04:00
|
|
|
.SetMethod("setBorderRadius", &WebContentsView::SetBorderRadius)
|
2020-04-09 16:01:16 +09:00
|
|
|
.SetProperty("webContents", &WebContentsView::GetWebContents);
|
|
|
|
}
|
2018-05-07 16:04:33 +09:00
|
|
|
|
2022-06-29 12:55:47 -07:00
|
|
|
} // namespace electron::api
|
2018-05-07 16:04:33 +09:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using electron::api::WebContentsView;
|
|
|
|
|
|
|
|
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 22:03:28 +09:00
|
|
|
gin_helper::Dictionary dict(isolate, exports);
|
2020-04-09 16:01:16 +09:00
|
|
|
dict.Set("WebContentsView", WebContentsView::GetConstructor(isolate));
|
2018-05-07 16:04:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2023-02-09 02:31:38 +01:00
|
|
|
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_web_contents_view,
|
|
|
|
Initialize)
|