refactor: WebContents::From returns pointer (#24605)

This commit is contained in:
Jeremy Rose 2020-07-16 16:16:05 -07:00 committed by GitHub
parent 14bbc07f1e
commit 45551f6bf2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 48 additions and 63 deletions

View file

@ -712,9 +712,9 @@ bool App::CanCreateWindow(
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(opener);
if (web_contents) {
auto api_web_contents = WebContents::From(isolate(), web_contents);
WebContents* api_web_contents = WebContents::From(web_contents);
// No need to emit any event if the WebContents is not available in JS.
if (!api_web_contents.IsEmpty()) {
if (api_web_contents) {
api_web_contents->OnCreateWindow(target_url, referrer, frame_name,
disposition, raw_features, body);
}

View file

@ -215,8 +215,7 @@ std::string DesktopCapturer::GetMediaSourceIdForWebContents(
int32_t request_web_contents_id,
int32_t web_contents_id) {
std::string id;
auto* web_contents = gin_helper::TrackableObject<WebContents>::FromWeakMapID(
isolate, web_contents_id);
auto* web_contents = WebContents::FromID(web_contents_id);
if (!web_contents) {
thrower.ThrowError("Failed to find WebContents with id " +
@ -232,9 +231,7 @@ std::string DesktopCapturer::GetMediaSourceIdForWebContents(
content::WebContentsMediaCaptureId(main_frame->GetProcess()->GetID(),
main_frame->GetRoutingID()));
auto* request_web_contents =
gin_helper::TrackableObject<WebContents>::FromWeakMapID(
isolate, request_web_contents_id);
auto* request_web_contents = WebContents::FromID(request_web_contents_id);
if (request_web_contents) {
// comment copied from
// chrome/browser/extensions/api/desktop_capture/desktop_capture_base.cc

View file

@ -947,7 +947,7 @@ std::unique_ptr<content::BluetoothChooser> WebContents::RunBluetoothChooser(
content::JavaScriptDialogManager* WebContents::GetJavaScriptDialogManager(
content::WebContents* source) {
if (!dialog_manager_)
dialog_manager_ = std::make_unique<ElectronJavaScriptDialogManager>(this);
dialog_manager_ = std::make_unique<ElectronJavaScriptDialogManager>();
return dialog_manager_.get();
}
@ -2878,24 +2878,25 @@ gin::Handle<WebContents> WebContents::CreateAndTake(
}
// static
gin::Handle<WebContents> WebContents::From(v8::Isolate* isolate,
content::WebContents* web_contents) {
WebContents* WebContents::From(content::WebContents* web_contents) {
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
auto* existing = TrackableObject::FromWrappedClass(isolate, web_contents);
if (existing)
return gin::CreateHandle(isolate, static_cast<WebContents*>(existing));
else
return gin::Handle<WebContents>();
return static_cast<WebContents*>(existing);
}
// static
gin::Handle<WebContents> WebContents::FromOrCreate(
v8::Isolate* isolate,
content::WebContents* web_contents) {
auto existing = From(isolate, web_contents);
if (!existing.IsEmpty())
return existing;
else
return gin::CreateHandle(isolate, new WebContents(isolate, web_contents));
WebContents* api_web_contents = From(web_contents);
if (!api_web_contents)
api_web_contents = new WebContents(isolate, web_contents);
return gin::CreateHandle(isolate, api_web_contents);
}
// static
WebContents* WebContents::FromID(int32_t id) {
return FromWeakMapID(JavascriptEnvironment::GetIsolate(), id);
}
} // namespace api

View file

@ -154,9 +154,10 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
std::unique_ptr<content::WebContents> web_contents,
Type type);
// Get the V8 wrapper of |web_content|, return empty handle if not wrapped.
static gin::Handle<WebContents> From(v8::Isolate* isolate,
content::WebContents* web_content);
// Get the api::WebContents associated with |web_contents|. Returns nullptr
// if there is no associated wrapper.
static WebContents* From(content::WebContents* web_contents);
static WebContents* FromID(int32_t id);
// Get the V8 wrapper of the |web_contents|, or create one if not existed.
//

View file

@ -31,7 +31,7 @@ WebContentsView::WebContentsView(v8::Isolate* isolate,
#else
: View(web_contents->managed_web_contents()->GetView()->GetView()),
#endif
web_contents_(isolate, web_contents->GetWrapper()),
web_contents_(isolate, web_contents.ToV8()),
api_web_contents_(web_contents.get()) {
#if !defined(OS_MACOSX)
// On macOS the View is a newly-created |DelayedNativeViewHost| and it is our

View file

@ -143,10 +143,9 @@ void ToDictionary(gin::Dictionary* details, extensions::WebRequestInfo* info) {
auto* web_contents = content::WebContents::FromRenderFrameHost(
content::RenderFrameHost::FromID(info->render_process_id,
info->frame_id));
int32_t id = api::WebContents::GetIDFromWrappedClass(web_contents);
// id must be greater than zero.
if (id > 0)
details->Set("webContentsId", id);
auto* api_web_contents = WebContents::From(web_contents);
if (api_web_contents)
details->Set("webContentsId", api_web_contents->ID());
}
void ToDictionary(gin::Dictionary* details,