Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: WebContents::From returns pointer #24605

Merged
merged 2 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions shell/browser/api/electron_api_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
7 changes: 2 additions & 5 deletions shell/browser/api/electron_api_desktop_capturer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 " +
Expand All @@ -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
Expand Down
25 changes: 13 additions & 12 deletions shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,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();
}
Expand Down Expand Up @@ -2864,24 +2864,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
Expand Down
7 changes: 4 additions & 3 deletions shell/browser/api/electron_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
2 changes: 1 addition & 1 deletion shell/browser/api/electron_api_web_contents_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions shell/browser/api/electron_api_web_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions shell/browser/electron_autofill_driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ void AutofillDriver::ShowAutofillPopup(
const std::vector<base::string16>& labels) {
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate);
auto* web_contents =
api::WebContents::From(isolate, content::WebContents::FromRenderFrameHost(
render_frame_host_))
.get();
auto* web_contents = api::WebContents::From(
content::WebContents::FromRenderFrameHost(render_frame_host_));
if (!web_contents || !web_contents->owner_window())
return;

Expand Down
12 changes: 6 additions & 6 deletions shell/browser/electron_javascript_dialog_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ constexpr int kUserWantsNoMoreDialogs = -1;

} // namespace

ElectronJavaScriptDialogManager::ElectronJavaScriptDialogManager(
api::WebContents* api_web_contents)
: api_web_contents_(api_web_contents) {}
ElectronJavaScriptDialogManager::ElectronJavaScriptDialogManager() {}
ElectronJavaScriptDialogManager::~ElectronJavaScriptDialogManager() = default;

void ElectronJavaScriptDialogManager::RunJavaScriptDialog(
Expand Down Expand Up @@ -115,9 +113,11 @@ void ElectronJavaScriptDialogManager::RunBeforeUnloadDialog(
content::RenderFrameHost* rfh,
bool is_reload,
DialogClosedCallback callback) {
bool default_prevented = api_web_contents_->Emit("will-prevent-unload");
std::move(callback).Run(default_prevented, base::string16());
return;
auto* api_web_contents = api::WebContents::From(web_contents);
if (api_web_contents) {
bool default_prevented = api_web_contents->Emit("will-prevent-unload");
std::move(callback).Run(default_prevented, base::string16());
}
}

void ElectronJavaScriptDialogManager::CancelDialogs(
Expand Down
3 changes: 1 addition & 2 deletions shell/browser/electron_javascript_dialog_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class WebContents;
class ElectronJavaScriptDialogManager
: public content::JavaScriptDialogManager {
public:
explicit ElectronJavaScriptDialogManager(api::WebContents* api_web_contents);
ElectronJavaScriptDialogManager();
~ElectronJavaScriptDialogManager() override;

// content::JavaScriptDialogManager implementations.
Expand All @@ -43,7 +43,6 @@ class ElectronJavaScriptDialogManager
int code,
bool checkbox_checked);

api::WebContents* api_web_contents_;
std::map<std::string, int> origin_counts_;
};

Expand Down
4 changes: 2 additions & 2 deletions shell/browser/electron_navigation_throttle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ ElectronNavigationThrottle::WillRedirectRequest() {

v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate);
auto api_contents = electron::api::WebContents::From(isolate, contents);
if (api_contents.IsEmpty()) {
api::WebContents* api_contents = api::WebContents::From(contents);
if (!api_contents) {
// No need to emit any event if the WebContents is not available in JS.
return PROCEED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ void StreamsPrivateAPI::SendExecuteMimeTypeHandlerEvent(
extensions::Extension::GetBaseURLFromExtensionId(extension_id).spec() +
handler->handler_url());
int tab_id = -1;
auto* api_contents = electron::api::WebContents::FromWrappedClass(
v8::Isolate::GetCurrent(), web_contents);
auto* api_contents = electron::api::WebContents::From(web_contents);
if (api_contents)
tab_id = api_contents->ID();
std::unique_ptr<extensions::StreamContainer> stream_container(
Expand Down
21 changes: 7 additions & 14 deletions shell/browser/extensions/api/tabs/tabs_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ bool ExecuteCodeInTabFunction::CanExecuteScriptOnPage(std::string* error) {
// If |tab_id| is specified, look for the tab. Otherwise default to selected
// tab in the current window.
CHECK_GE(execute_tab_id_, 0);
auto* contents = electron::api::WebContents::FromWeakMapID(
v8::Isolate::GetCurrent(), execute_tab_id_);
auto* contents = electron::api::WebContents::FromID(execute_tab_id_);
if (!contents) {
return false;
}
Expand Down Expand Up @@ -146,8 +145,7 @@ bool ExecuteCodeInTabFunction::CanExecuteScriptOnPage(std::string* error) {

ScriptExecutor* ExecuteCodeInTabFunction::GetScriptExecutor(
std::string* error) {
auto* contents = electron::api::WebContents::FromWeakMapID(
v8::Isolate::GetCurrent(), execute_tab_id_);
auto* contents = electron::api::WebContents::FromID(execute_tab_id_);
if (!contents)
return nullptr;
return contents->script_executor();
Expand All @@ -170,8 +168,7 @@ ExtensionFunction::ResponseAction TabsGetFunction::Run() {
EXTENSION_FUNCTION_VALIDATE(params.get());
int tab_id = params->tab_id;

auto* contents = electron::api::WebContents::FromWeakMapID(
v8::Isolate::GetCurrent(), tab_id);
auto* contents = electron::api::WebContents::FromID(tab_id);
if (!contents)
return RespondNow(Error("No such tab"));

Expand All @@ -193,8 +190,7 @@ ExtensionFunction::ResponseAction TabsSetZoomFunction::Run() {
EXTENSION_FUNCTION_VALIDATE(params);

int tab_id = params->tab_id ? *params->tab_id : -1;
auto* contents = electron::api::WebContents::FromWeakMapID(
v8::Isolate::GetCurrent(), tab_id);
auto* contents = electron::api::WebContents::FromID(tab_id);
if (!contents)
return RespondNow(Error("No such tab"));

Expand Down Expand Up @@ -222,8 +218,7 @@ ExtensionFunction::ResponseAction TabsGetZoomFunction::Run() {
EXTENSION_FUNCTION_VALIDATE(params);

int tab_id = params->tab_id ? *params->tab_id : -1;
auto* contents = electron::api::WebContents::FromWeakMapID(
v8::Isolate::GetCurrent(), tab_id);
auto* contents = electron::api::WebContents::FromID(tab_id);
if (!contents)
return RespondNow(Error("No such tab"));

Expand All @@ -239,8 +234,7 @@ ExtensionFunction::ResponseAction TabsGetZoomSettingsFunction::Run() {
EXTENSION_FUNCTION_VALIDATE(params);

int tab_id = params->tab_id ? *params->tab_id : -1;
auto* contents = electron::api::WebContents::FromWeakMapID(
v8::Isolate::GetCurrent(), tab_id);
auto* contents = electron::api::WebContents::FromID(tab_id);
if (!contents)
return RespondNow(Error("No such tab"));

Expand All @@ -265,8 +259,7 @@ ExtensionFunction::ResponseAction TabsSetZoomSettingsFunction::Run() {
EXTENSION_FUNCTION_VALIDATE(params);

int tab_id = params->tab_id ? *params->tab_id : -1;
auto* contents = electron::api::WebContents::FromWeakMapID(
v8::Isolate::GetCurrent(), tab_id);
auto* contents = electron::api::WebContents::FromID(tab_id);
if (!contents)
return RespondNow(Error("No such tab"));

Expand Down
6 changes: 2 additions & 4 deletions shell/browser/extensions/electron_messaging_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ ElectronMessagingDelegate::IsNativeMessagingHostAllowed(
std::unique_ptr<base::DictionaryValue>
ElectronMessagingDelegate::MaybeGetTabInfo(content::WebContents* web_contents) {
if (web_contents) {
auto* api_contents = electron::api::WebContents::FromWrappedClass(
v8::Isolate::GetCurrent(), web_contents);
auto* api_contents = electron::api::WebContents::From(web_contents);
if (api_contents) {
auto tab = std::make_unique<base::DictionaryValue>();
tab->SetWithoutPathExpansion(
Expand All @@ -63,8 +62,7 @@ ElectronMessagingDelegate::MaybeGetTabInfo(content::WebContents* web_contents) {
content::WebContents* ElectronMessagingDelegate::GetWebContentsByTabId(
content::BrowserContext* browser_context,
int tab_id) {
auto* contents = electron::api::WebContents::FromWeakMapID(
v8::Isolate::GetCurrent(), tab_id);
auto* contents = electron::api::WebContents::FromID(tab_id);
if (!contents) {
return nullptr;
}
Expand Down
4 changes: 2 additions & 2 deletions shell/browser/login_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ void LoginHandler::EmitEvent(
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate);

auto api_web_contents = api::WebContents::From(isolate, web_contents());
if (api_web_contents.IsEmpty()) {
api::WebContents* api_web_contents = api::WebContents::From(web_contents());
if (!api_web_contents) {
std::move(auth_required_callback_).Run(base::nullopt);
return;
}
Expand Down