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
This commit is contained in:
Ahmed Mohamed Ali 2017-10-19 16:27:08 +02:00
parent 44f91e12e1
commit 9f3cfa2dbd
2 changed files with 19 additions and 8 deletions

View file

@ -58,7 +58,9 @@ void PopulateStreamInfo(base::DictionaryValue* stream_info,
PdfViewerHandler::PdfViewerHandler(const std::string& src) PdfViewerHandler::PdfViewerHandler(const std::string& src)
: stream_(nullptr), original_url_(src) {} : stream_(nullptr), original_url_(src) {}
PdfViewerHandler::~PdfViewerHandler() {} PdfViewerHandler::~PdfViewerHandler() {
RemoveObserver();
}
void PdfViewerHandler::SetPdfResourceStream(content::StreamInfo* stream) { void PdfViewerHandler::SetPdfResourceStream(content::StreamInfo* stream) {
stream_ = stream; stream_ = stream;
@ -90,15 +92,11 @@ void PdfViewerHandler::RegisterMessages() {
} }
void PdfViewerHandler::OnJavascriptAllowed() { void PdfViewerHandler::OnJavascriptAllowed() {
auto zoom_controller = WebContentsZoomController::FromWebContents( AddObserver();
web_ui()->GetWebContents());
zoom_controller->AddObserver(this);
} }
void PdfViewerHandler::OnJavascriptDisallowed() { void PdfViewerHandler::OnJavascriptDisallowed() {
auto zoom_controller = WebContentsZoomController::FromWebContents( RemoveObserver();
web_ui()->GetWebContents());
zoom_controller->RemoveObserver(this);
} }
void PdfViewerHandler::Initialize(const base::ListValue* args) { 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 } // namespace atom

View file

@ -45,7 +45,8 @@ class PdfViewerHandler : public content::WebUIMessageHandler,
void Reload(const base::ListValue* args); void Reload(const base::ListValue* args);
void OnZoomLevelChanged(content::WebContents* web_contents, double level, void OnZoomLevelChanged(content::WebContents* web_contents, double level,
bool is_temporary); bool is_temporary);
void AddObserver();
void RemoveObserver();
std::unique_ptr<base::Value> initialize_callback_id_; std::unique_ptr<base::Value> initialize_callback_id_;
content::StreamInfo* stream_; content::StreamInfo* stream_;
std::string original_url_; std::string original_url_;