From 9f3cfa2dbd4259776216e8593a280722cbcab557 Mon Sep 17 00:00:00 2001 From: Ahmed Mohamed Ali Date: Thu, 19 Oct 2017 16:27:08 +0200 Subject: [PATCH] Fix crash that happens when the PDF viewer is refreshed. The root cause is the PdfViewerHandler instanceis destroyed but not removed from the list of observer in WebContentsZoomController --- atom/browser/ui/webui/pdf_viewer_handler.cc | 24 +++++++++++++++------ atom/browser/ui/webui/pdf_viewer_handler.h | 3 ++- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/atom/browser/ui/webui/pdf_viewer_handler.cc b/atom/browser/ui/webui/pdf_viewer_handler.cc index aee7ff05493e..cc51d2d92df9 100644 --- a/atom/browser/ui/webui/pdf_viewer_handler.cc +++ b/atom/browser/ui/webui/pdf_viewer_handler.cc @@ -58,7 +58,9 @@ void PopulateStreamInfo(base::DictionaryValue* stream_info, PdfViewerHandler::PdfViewerHandler(const std::string& src) : stream_(nullptr), original_url_(src) {} -PdfViewerHandler::~PdfViewerHandler() {} +PdfViewerHandler::~PdfViewerHandler() { + RemoveObserver(); +} void PdfViewerHandler::SetPdfResourceStream(content::StreamInfo* stream) { stream_ = stream; @@ -90,15 +92,11 @@ void PdfViewerHandler::RegisterMessages() { } void PdfViewerHandler::OnJavascriptAllowed() { - auto zoom_controller = WebContentsZoomController::FromWebContents( - web_ui()->GetWebContents()); - zoom_controller->AddObserver(this); + AddObserver(); } void PdfViewerHandler::OnJavascriptDisallowed() { - auto zoom_controller = WebContentsZoomController::FromWebContents( - web_ui()->GetWebContents()); - zoom_controller->RemoveObserver(this); + RemoveObserver(); } void PdfViewerHandler::Initialize(const base::ListValue* args) { @@ -214,4 +212,16 @@ void PdfViewerHandler::OnZoomLevelChanged(content::WebContents* web_contents, } } +void PdfViewerHandler::AddObserver() { + auto zoom_controller = + WebContentsZoomController::FromWebContents(web_ui()->GetWebContents()); + zoom_controller->AddObserver(this); +} + +void PdfViewerHandler::RemoveObserver() { + auto zoom_controller = + WebContentsZoomController::FromWebContents(web_ui()->GetWebContents()); + zoom_controller->RemoveObserver(this); +} + } // namespace atom diff --git a/atom/browser/ui/webui/pdf_viewer_handler.h b/atom/browser/ui/webui/pdf_viewer_handler.h index e6ae315e8764..2da19e684cdc 100644 --- a/atom/browser/ui/webui/pdf_viewer_handler.h +++ b/atom/browser/ui/webui/pdf_viewer_handler.h @@ -45,7 +45,8 @@ class PdfViewerHandler : public content::WebUIMessageHandler, void Reload(const base::ListValue* args); void OnZoomLevelChanged(content::WebContents* web_contents, double level, bool is_temporary); - + void AddObserver(); + void RemoveObserver(); std::unique_ptr initialize_callback_id_; content::StreamInfo* stream_; std::string original_url_;