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* web_contents =
content::WebContents::FromRenderFrameHost(opener); content::WebContents::FromRenderFrameHost(opener);
if (web_contents) { 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. // 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, api_web_contents->OnCreateWindow(target_url, referrer, frame_name,
disposition, raw_features, body); disposition, raw_features, body);
} }

View file

@ -215,8 +215,7 @@ std::string DesktopCapturer::GetMediaSourceIdForWebContents(
int32_t request_web_contents_id, int32_t request_web_contents_id,
int32_t web_contents_id) { int32_t web_contents_id) {
std::string id; std::string id;
auto* web_contents = gin_helper::TrackableObject<WebContents>::FromWeakMapID( auto* web_contents = WebContents::FromID(web_contents_id);
isolate, web_contents_id);
if (!web_contents) { if (!web_contents) {
thrower.ThrowError("Failed to find WebContents with id " + thrower.ThrowError("Failed to find WebContents with id " +
@ -232,9 +231,7 @@ std::string DesktopCapturer::GetMediaSourceIdForWebContents(
content::WebContentsMediaCaptureId(main_frame->GetProcess()->GetID(), content::WebContentsMediaCaptureId(main_frame->GetProcess()->GetID(),
main_frame->GetRoutingID())); main_frame->GetRoutingID()));
auto* request_web_contents = auto* request_web_contents = WebContents::FromID(request_web_contents_id);
gin_helper::TrackableObject<WebContents>::FromWeakMapID(
isolate, request_web_contents_id);
if (request_web_contents) { if (request_web_contents) {
// comment copied from // comment copied from
// chrome/browser/extensions/api/desktop_capture/desktop_capture_base.cc // 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::JavaScriptDialogManager* WebContents::GetJavaScriptDialogManager(
content::WebContents* source) { content::WebContents* source) {
if (!dialog_manager_) if (!dialog_manager_)
dialog_manager_ = std::make_unique<ElectronJavaScriptDialogManager>(this); dialog_manager_ = std::make_unique<ElectronJavaScriptDialogManager>();
return dialog_manager_.get(); return dialog_manager_.get();
} }
@ -2878,24 +2878,25 @@ gin::Handle<WebContents> WebContents::CreateAndTake(
} }
// static // static
gin::Handle<WebContents> WebContents::From(v8::Isolate* isolate, WebContents* WebContents::From(content::WebContents* web_contents) {
content::WebContents* web_contents) { v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
auto* existing = TrackableObject::FromWrappedClass(isolate, web_contents); auto* existing = TrackableObject::FromWrappedClass(isolate, web_contents);
if (existing) return static_cast<WebContents*>(existing);
return gin::CreateHandle(isolate, static_cast<WebContents*>(existing));
else
return gin::Handle<WebContents>();
} }
// static // static
gin::Handle<WebContents> WebContents::FromOrCreate( gin::Handle<WebContents> WebContents::FromOrCreate(
v8::Isolate* isolate, v8::Isolate* isolate,
content::WebContents* web_contents) { content::WebContents* web_contents) {
auto existing = From(isolate, web_contents); WebContents* api_web_contents = From(web_contents);
if (!existing.IsEmpty()) if (!api_web_contents)
return existing; api_web_contents = new WebContents(isolate, web_contents);
else return gin::CreateHandle(isolate, api_web_contents);
return gin::CreateHandle(isolate, new WebContents(isolate, web_contents)); }
// static
WebContents* WebContents::FromID(int32_t id) {
return FromWeakMapID(JavascriptEnvironment::GetIsolate(), id);
} }
} // namespace api } // namespace api

View file

@ -154,9 +154,10 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
std::unique_ptr<content::WebContents> web_contents, std::unique_ptr<content::WebContents> web_contents,
Type type); Type type);
// Get the V8 wrapper of |web_content|, return empty handle if not wrapped. // Get the api::WebContents associated with |web_contents|. Returns nullptr
static gin::Handle<WebContents> From(v8::Isolate* isolate, // if there is no associated wrapper.
content::WebContents* web_content); 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. // 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 #else
: View(web_contents->managed_web_contents()->GetView()->GetView()), : View(web_contents->managed_web_contents()->GetView()->GetView()),
#endif #endif
web_contents_(isolate, web_contents->GetWrapper()), web_contents_(isolate, web_contents.ToV8()),
api_web_contents_(web_contents.get()) { api_web_contents_(web_contents.get()) {
#if !defined(OS_MACOSX) #if !defined(OS_MACOSX)
// On macOS the View is a newly-created |DelayedNativeViewHost| and it is our // 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( auto* web_contents = content::WebContents::FromRenderFrameHost(
content::RenderFrameHost::FromID(info->render_process_id, content::RenderFrameHost::FromID(info->render_process_id,
info->frame_id)); info->frame_id));
int32_t id = api::WebContents::GetIDFromWrappedClass(web_contents); auto* api_web_contents = WebContents::From(web_contents);
// id must be greater than zero. if (api_web_contents)
if (id > 0) details->Set("webContentsId", api_web_contents->ID());
details->Set("webContentsId", id);
} }
void ToDictionary(gin::Dictionary* details, void ToDictionary(gin::Dictionary* details,

View file

@ -31,10 +31,8 @@ void AutofillDriver::ShowAutofillPopup(
const std::vector<base::string16>& labels) { const std::vector<base::string16>& labels) {
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate); v8::HandleScope scope(isolate);
auto* web_contents = auto* web_contents = api::WebContents::From(
api::WebContents::From(isolate, content::WebContents::FromRenderFrameHost( content::WebContents::FromRenderFrameHost(render_frame_host_));
render_frame_host_))
.get();
if (!web_contents || !web_contents->owner_window()) if (!web_contents || !web_contents->owner_window())
return; return;

View file

@ -27,9 +27,7 @@ constexpr int kUserWantsNoMoreDialogs = -1;
} // namespace } // namespace
ElectronJavaScriptDialogManager::ElectronJavaScriptDialogManager( ElectronJavaScriptDialogManager::ElectronJavaScriptDialogManager() {}
api::WebContents* api_web_contents)
: api_web_contents_(api_web_contents) {}
ElectronJavaScriptDialogManager::~ElectronJavaScriptDialogManager() = default; ElectronJavaScriptDialogManager::~ElectronJavaScriptDialogManager() = default;
void ElectronJavaScriptDialogManager::RunJavaScriptDialog( void ElectronJavaScriptDialogManager::RunJavaScriptDialog(
@ -115,9 +113,11 @@ void ElectronJavaScriptDialogManager::RunBeforeUnloadDialog(
content::RenderFrameHost* rfh, content::RenderFrameHost* rfh,
bool is_reload, bool is_reload,
DialogClosedCallback callback) { DialogClosedCallback callback) {
bool default_prevented = api_web_contents_->Emit("will-prevent-unload"); auto* api_web_contents = api::WebContents::From(web_contents);
std::move(callback).Run(default_prevented, base::string16()); if (api_web_contents) {
return; bool default_prevented = api_web_contents->Emit("will-prevent-unload");
std::move(callback).Run(default_prevented, base::string16());
}
} }
void ElectronJavaScriptDialogManager::CancelDialogs( void ElectronJavaScriptDialogManager::CancelDialogs(

View file

@ -19,7 +19,7 @@ class WebContents;
class ElectronJavaScriptDialogManager class ElectronJavaScriptDialogManager
: public content::JavaScriptDialogManager { : public content::JavaScriptDialogManager {
public: public:
explicit ElectronJavaScriptDialogManager(api::WebContents* api_web_contents); ElectronJavaScriptDialogManager();
~ElectronJavaScriptDialogManager() override; ~ElectronJavaScriptDialogManager() override;
// content::JavaScriptDialogManager implementations. // content::JavaScriptDialogManager implementations.
@ -43,7 +43,6 @@ class ElectronJavaScriptDialogManager
int code, int code,
bool checkbox_checked); bool checkbox_checked);
api::WebContents* api_web_contents_;
std::map<std::string, int> origin_counts_; std::map<std::string, int> origin_counts_;
}; };

View file

@ -31,8 +31,8 @@ ElectronNavigationThrottle::WillRedirectRequest() {
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate); v8::HandleScope scope(isolate);
auto api_contents = electron::api::WebContents::From(isolate, contents); api::WebContents* api_contents = api::WebContents::From(contents);
if (api_contents.IsEmpty()) { if (!api_contents) {
// No need to emit any event if the WebContents is not available in JS. // No need to emit any event if the WebContents is not available in JS.
return PROCEED; return PROCEED;
} }

View file

@ -58,8 +58,7 @@ void StreamsPrivateAPI::SendExecuteMimeTypeHandlerEvent(
extensions::Extension::GetBaseURLFromExtensionId(extension_id).spec() + extensions::Extension::GetBaseURLFromExtensionId(extension_id).spec() +
handler->handler_url()); handler->handler_url());
int tab_id = -1; int tab_id = -1;
auto* api_contents = electron::api::WebContents::FromWrappedClass( auto* api_contents = electron::api::WebContents::From(web_contents);
v8::Isolate::GetCurrent(), web_contents);
if (api_contents) if (api_contents)
tab_id = api_contents->ID(); tab_id = api_contents->ID();
std::unique_ptr<extensions::StreamContainer> stream_container( std::unique_ptr<extensions::StreamContainer> stream_container(

View file

@ -92,8 +92,7 @@ bool ExecuteCodeInTabFunction::CanExecuteScriptOnPage(std::string* error) {
// If |tab_id| is specified, look for the tab. Otherwise default to selected // If |tab_id| is specified, look for the tab. Otherwise default to selected
// tab in the current window. // tab in the current window.
CHECK_GE(execute_tab_id_, 0); CHECK_GE(execute_tab_id_, 0);
auto* contents = electron::api::WebContents::FromWeakMapID( auto* contents = electron::api::WebContents::FromID(execute_tab_id_);
v8::Isolate::GetCurrent(), execute_tab_id_);
if (!contents) { if (!contents) {
return false; return false;
} }
@ -146,8 +145,7 @@ bool ExecuteCodeInTabFunction::CanExecuteScriptOnPage(std::string* error) {
ScriptExecutor* ExecuteCodeInTabFunction::GetScriptExecutor( ScriptExecutor* ExecuteCodeInTabFunction::GetScriptExecutor(
std::string* error) { std::string* error) {
auto* contents = electron::api::WebContents::FromWeakMapID( auto* contents = electron::api::WebContents::FromID(execute_tab_id_);
v8::Isolate::GetCurrent(), execute_tab_id_);
if (!contents) if (!contents)
return nullptr; return nullptr;
return contents->script_executor(); return contents->script_executor();
@ -170,8 +168,7 @@ ExtensionFunction::ResponseAction TabsGetFunction::Run() {
EXTENSION_FUNCTION_VALIDATE(params.get()); EXTENSION_FUNCTION_VALIDATE(params.get());
int tab_id = params->tab_id; int tab_id = params->tab_id;
auto* contents = electron::api::WebContents::FromWeakMapID( auto* contents = electron::api::WebContents::FromID(tab_id);
v8::Isolate::GetCurrent(), tab_id);
if (!contents) if (!contents)
return RespondNow(Error("No such tab")); return RespondNow(Error("No such tab"));
@ -193,8 +190,7 @@ ExtensionFunction::ResponseAction TabsSetZoomFunction::Run() {
EXTENSION_FUNCTION_VALIDATE(params); EXTENSION_FUNCTION_VALIDATE(params);
int tab_id = params->tab_id ? *params->tab_id : -1; int tab_id = params->tab_id ? *params->tab_id : -1;
auto* contents = electron::api::WebContents::FromWeakMapID( auto* contents = electron::api::WebContents::FromID(tab_id);
v8::Isolate::GetCurrent(), tab_id);
if (!contents) if (!contents)
return RespondNow(Error("No such tab")); return RespondNow(Error("No such tab"));
@ -222,8 +218,7 @@ ExtensionFunction::ResponseAction TabsGetZoomFunction::Run() {
EXTENSION_FUNCTION_VALIDATE(params); EXTENSION_FUNCTION_VALIDATE(params);
int tab_id = params->tab_id ? *params->tab_id : -1; int tab_id = params->tab_id ? *params->tab_id : -1;
auto* contents = electron::api::WebContents::FromWeakMapID( auto* contents = electron::api::WebContents::FromID(tab_id);
v8::Isolate::GetCurrent(), tab_id);
if (!contents) if (!contents)
return RespondNow(Error("No such tab")); return RespondNow(Error("No such tab"));
@ -239,8 +234,7 @@ ExtensionFunction::ResponseAction TabsGetZoomSettingsFunction::Run() {
EXTENSION_FUNCTION_VALIDATE(params); EXTENSION_FUNCTION_VALIDATE(params);
int tab_id = params->tab_id ? *params->tab_id : -1; int tab_id = params->tab_id ? *params->tab_id : -1;
auto* contents = electron::api::WebContents::FromWeakMapID( auto* contents = electron::api::WebContents::FromID(tab_id);
v8::Isolate::GetCurrent(), tab_id);
if (!contents) if (!contents)
return RespondNow(Error("No such tab")); return RespondNow(Error("No such tab"));
@ -265,8 +259,7 @@ ExtensionFunction::ResponseAction TabsSetZoomSettingsFunction::Run() {
EXTENSION_FUNCTION_VALIDATE(params); EXTENSION_FUNCTION_VALIDATE(params);
int tab_id = params->tab_id ? *params->tab_id : -1; int tab_id = params->tab_id ? *params->tab_id : -1;
auto* contents = electron::api::WebContents::FromWeakMapID( auto* contents = electron::api::WebContents::FromID(tab_id);
v8::Isolate::GetCurrent(), tab_id);
if (!contents) if (!contents)
return RespondNow(Error("No such tab")); return RespondNow(Error("No such tab"));

View file

@ -41,8 +41,7 @@ ElectronMessagingDelegate::IsNativeMessagingHostAllowed(
std::unique_ptr<base::DictionaryValue> std::unique_ptr<base::DictionaryValue>
ElectronMessagingDelegate::MaybeGetTabInfo(content::WebContents* web_contents) { ElectronMessagingDelegate::MaybeGetTabInfo(content::WebContents* web_contents) {
if (web_contents) { if (web_contents) {
auto* api_contents = electron::api::WebContents::FromWrappedClass( auto* api_contents = electron::api::WebContents::From(web_contents);
v8::Isolate::GetCurrent(), web_contents);
if (api_contents) { if (api_contents) {
auto tab = std::make_unique<base::DictionaryValue>(); auto tab = std::make_unique<base::DictionaryValue>();
tab->SetWithoutPathExpansion( tab->SetWithoutPathExpansion(
@ -63,8 +62,7 @@ ElectronMessagingDelegate::MaybeGetTabInfo(content::WebContents* web_contents) {
content::WebContents* ElectronMessagingDelegate::GetWebContentsByTabId( content::WebContents* ElectronMessagingDelegate::GetWebContentsByTabId(
content::BrowserContext* browser_context, content::BrowserContext* browser_context,
int tab_id) { int tab_id) {
auto* contents = electron::api::WebContents::FromWeakMapID( auto* contents = electron::api::WebContents::FromID(tab_id);
v8::Isolate::GetCurrent(), tab_id);
if (!contents) { if (!contents) {
return nullptr; return nullptr;
} }

View file

@ -54,8 +54,8 @@ void LoginHandler::EmitEvent(
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate); v8::HandleScope scope(isolate);
auto api_web_contents = api::WebContents::From(isolate, web_contents()); api::WebContents* api_web_contents = api::WebContents::From(web_contents());
if (api_web_contents.IsEmpty()) { if (!api_web_contents) {
std::move(auth_required_callback_).Run(base::nullopt); std::move(auth_required_callback_).Run(base::nullopt);
return; return;
} }