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
|
|
|
|
2018-05-17 08:01:56 +00:00
|
|
|
#include "content/public/browser/web_contents_user_data.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"
|
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"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
2018-05-08 03:31:47 +00:00
|
|
|
|
|
|
|
#if defined(OS_MACOSX)
|
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
|
|
|
|
2018-05-17 08:01:56 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Used to indicate whether a WebContents already has a view.
|
|
|
|
class WebContentsViewRelay
|
|
|
|
: public content::WebContentsUserData<WebContentsViewRelay> {
|
|
|
|
public:
|
2019-09-16 22:12:00 +00:00
|
|
|
~WebContentsViewRelay() override = default;
|
2018-05-17 08:01:56 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
explicit WebContentsViewRelay(content::WebContents* contents) {}
|
|
|
|
friend class content::WebContentsUserData<WebContentsViewRelay>;
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
electron::api::WebContentsView* view_ = nullptr;
|
2018-05-17 08:01:56 +00:00
|
|
|
|
2019-01-21 16:56:54 +00:00
|
|
|
WEB_CONTENTS_USER_DATA_KEY_DECL();
|
|
|
|
|
2018-05-17 08:01:56 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(WebContentsViewRelay);
|
|
|
|
};
|
|
|
|
|
2019-01-21 16:56:54 +00:00
|
|
|
WEB_CONTENTS_USER_DATA_KEY_IMPL(WebContentsViewRelay)
|
|
|
|
|
2018-05-17 08:01:56 +00:00
|
|
|
} // namespace
|
|
|
|
|
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,
|
2019-10-25 13:03:28 +00:00
|
|
|
gin::Handle<WebContents> web_contents,
|
2018-10-19 13:50:30 +00:00
|
|
|
InspectableWebContents* iwc)
|
2018-05-07 07:04:33 +00:00
|
|
|
#if defined(OS_MACOSX)
|
2018-05-16 10:29:44 +00:00
|
|
|
: View(new DelayedNativeViewHost(iwc->GetView()->GetNativeView())),
|
2018-05-07 07:04:33 +00:00
|
|
|
#else
|
2018-05-16 10:29:44 +00:00
|
|
|
: View(iwc->GetView()->GetView()),
|
2018-05-07 07:04:33 +00:00
|
|
|
#endif
|
2018-05-16 10:29:44 +00:00
|
|
|
web_contents_(isolate, web_contents->GetWrapper()),
|
|
|
|
api_web_contents_(web_contents.get()) {
|
2018-05-07 07:04:33 +00:00
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
// On macOS a View is created to wrap the NSView, and its lifetime is managed
|
|
|
|
// by us.
|
2018-05-08 03:31:47 +00:00
|
|
|
view()->set_owned_by_client();
|
2018-05-07 07:04:33 +00:00
|
|
|
#else
|
|
|
|
// On other platforms the View is managed by InspectableWebContents.
|
|
|
|
set_delete_view(false);
|
|
|
|
#endif
|
2018-05-17 08:01:56 +00:00
|
|
|
WebContentsViewRelay::CreateForWebContents(web_contents->web_contents());
|
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
|
|
|
|
// Destroy WebContents asynchronously unless app is shutting down,
|
|
|
|
// because destroy() might be called inside WebContents's event handler.
|
|
|
|
api_web_contents_->DestroyWebContents(!Browser::Get()->is_shutting_down());
|
|
|
|
}
|
2018-05-07 07:04:33 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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,
|
|
|
|
gin::Handle<WebContents> web_contents) {
|
2018-05-17 08:01:56 +00:00
|
|
|
// Currently we only support InspectableWebContents, e.g. the WebContents
|
|
|
|
// created by users directly. To support devToolsWebContents we need to create
|
|
|
|
// a wrapper view.
|
2018-05-07 07:04:33 +00:00
|
|
|
if (!web_contents->managed_web_contents()) {
|
2019-10-25 13:03:28 +00:00
|
|
|
args->ThrowError("The WebContents must be created by user");
|
2018-05-07 07:04:33 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-05-17 08:01:56 +00:00
|
|
|
// Check if the WebContents has already been added to a view.
|
|
|
|
if (WebContentsViewRelay::FromWebContents(web_contents->web_contents())) {
|
2019-10-25 13:03:28 +00:00
|
|
|
args->ThrowError("The WebContents has already been added to a View");
|
2018-05-17 08:01:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// Constructor call.
|
2018-05-16 10:29:44 +00:00
|
|
|
auto* view = new WebContentsView(args->isolate(), web_contents,
|
2018-05-08 05:47:26 +00:00
|
|
|
web_contents->managed_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,
|
|
|
|
v8::Local<v8::FunctionTemplate> prototype) {}
|
|
|
|
|
|
|
|
} // 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);
|
2019-04-30 00:40:39 +00:00
|
|
|
dict.Set("WebContentsView",
|
2019-12-05 09:46:34 +00:00
|
|
|
gin_helper::CreateConstructor<WebContentsView>(
|
2019-04-30 00:40:39 +00:00
|
|
|
isolate, base::BindRepeating(&WebContentsView::New)));
|
2018-05-07 07:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-03-08 18:29:52 +00:00
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(atom_browser_web_contents_view, Initialize)
|