From efa70131e2b88031470f8cc4ec5a8e166c17615b Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Mon, 20 Sep 2021 00:34:11 +0000 Subject: [PATCH] refactor: make `InitWithWebContents` and `InspectableWebContents` take a `unique_ptr` (#30920) * refactor: make InitWithWebContents take a unique_ptr Signed-off-by: Darshan Sen * refactor: make InspectableWebContents take a unique_ptr Signed-off-by: Darshan Sen --- .../browser/api/electron_api_web_contents.cc | 28 +++++++++---------- shell/browser/api/electron_api_web_contents.h | 2 +- shell/browser/ui/inspectable_web_contents.cc | 8 +++--- shell/browser/ui/inspectable_web_contents.h | 2 +- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 00bcf096ac42..9dc56374fde5 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -784,10 +784,7 @@ void WebContents::InitWithSessionAndOptions( gin::Handle session, const gin_helper::Dictionary& options) { Observe(owned_web_contents.get()); - // TODO(zcbenz): Make InitWithWebContents take unique_ptr. - // At the time of writing we are going through a refactoring and I don't want - // to make other people's work harder. - InitWithWebContents(owned_web_contents.release(), session->browser_context(), + InitWithWebContents(std::move(owned_web_contents), session->browser_context(), IsGuest()); inspectable_web_contents_->GetView()->SetDelegate(this); @@ -884,36 +881,39 @@ void WebContents::InitWithExtensionView(v8::Isolate* isolate, // Allow toggling DevTools for background pages Observe(web_contents); - InitWithWebContents(web_contents, GetBrowserContext(), IsGuest()); + InitWithWebContents(std::unique_ptr(web_contents), + GetBrowserContext(), IsGuest()); inspectable_web_contents_->GetView()->SetDelegate(this); } #endif -void WebContents::InitWithWebContents(content::WebContents* web_contents, - ElectronBrowserContext* browser_context, - bool is_guest) { +void WebContents::InitWithWebContents( + std::unique_ptr web_contents, + ElectronBrowserContext* browser_context, + bool is_guest) { browser_context_ = browser_context; web_contents->SetDelegate(this); #if BUILDFLAG(ENABLE_PRINTING) - PrintPreviewMessageHandler::CreateForWebContents(web_contents); - PrintViewManagerElectron::CreateForWebContents(web_contents); - printing::CreateCompositeClientIfNeeded(web_contents, + PrintPreviewMessageHandler::CreateForWebContents(web_contents.get()); + PrintViewManagerElectron::CreateForWebContents(web_contents.get()); + printing::CreateCompositeClientIfNeeded(web_contents.get(), browser_context->GetUserAgent()); #endif #if BUILDFLAG(ENABLE_PDF_VIEWER) pdf::PDFWebContentsHelper::CreateForWebContentsWithClient( - web_contents, std::make_unique()); + web_contents.get(), + std::make_unique()); #endif // Determine whether the WebContents is offscreen. - auto* web_preferences = WebContentsPreferences::From(web_contents); + auto* web_preferences = WebContentsPreferences::From(web_contents.get()); offscreen_ = web_preferences && web_preferences->IsOffscreen(); // Create InspectableWebContents. inspectable_web_contents_ = std::make_unique( - web_contents, browser_context->prefs(), is_guest); + std::move(web_contents), browser_context->prefs(), is_guest); inspectable_web_contents_->SetDelegate(this); } diff --git a/shell/browser/api/electron_api_web_contents.h b/shell/browser/api/electron_api_web_contents.h index c30f7fc44276..55ea0d3070da 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -432,7 +432,7 @@ class WebContents : public gin::Wrappable, // Creates a InspectableWebContents object and takes ownership of // |web_contents|. - void InitWithWebContents(content::WebContents* web_contents, + void InitWithWebContents(std::unique_ptr web_contents, ElectronBrowserContext* browser_context, bool is_guest); diff --git a/shell/browser/ui/inspectable_web_contents.cc b/shell/browser/ui/inspectable_web_contents.cc index 5c864eebdb3f..7b7ca173ad9d 100644 --- a/shell/browser/ui/inspectable_web_contents.cc +++ b/shell/browser/ui/inspectable_web_contents.cc @@ -339,11 +339,11 @@ void InspectableWebContents::RegisterPrefs(PrefRegistrySimple* registry) { } InspectableWebContents::InspectableWebContents( - content::WebContents* web_contents, + std::unique_ptr web_contents, PrefService* pref_service, bool is_guest) : pref_service_(pref_service), - web_contents_(web_contents), + web_contents_(std::move(web_contents)), is_guest_(is_guest), view_(CreateInspectableContentsView(this)) { const base::Value* bounds_dict = pref_service_->Get(kDevToolsBoundsPref); @@ -356,9 +356,9 @@ InspectableWebContents::InspectableWebContents( } if (!IsPointInScreen(devtools_bounds_.origin())) { gfx::Rect display; - if (!is_guest && web_contents->GetNativeView()) { + if (!is_guest && web_contents_->GetNativeView()) { display = display::Screen::GetScreen() - ->GetDisplayNearestView(web_contents->GetNativeView()) + ->GetDisplayNearestView(web_contents_->GetNativeView()) .bounds(); } else { display = display::Screen::GetScreen()->GetPrimaryDisplay().bounds(); diff --git a/shell/browser/ui/inspectable_web_contents.h b/shell/browser/ui/inspectable_web_contents.h index 8b233f2f6b5b..e908d4cb0dac 100644 --- a/shell/browser/ui/inspectable_web_contents.h +++ b/shell/browser/ui/inspectable_web_contents.h @@ -44,7 +44,7 @@ class InspectableWebContents static const List& GetAll(); static void RegisterPrefs(PrefRegistrySimple* pref_registry); - InspectableWebContents(content::WebContents* web_contents, + InspectableWebContents(std::unique_ptr web_contents, PrefService* pref_service, bool is_guest); ~InspectableWebContents() override;