From bfe8928ab14b3152f7e705c04dcc78939721c8b3 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 4 Dec 2013 17:10:59 +0800 Subject: [PATCH] Do not use flags to mark whether the window is unresponsive. It's error prone. --- browser/native_window.cc | 36 ++++++++++++++++++------------------ browser/native_window.h | 8 ++++---- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/browser/native_window.cc b/browser/native_window.cc index c656edbf6667..0db2cfd99403 100644 --- a/browser/native_window.cc +++ b/browser/native_window.cc @@ -44,7 +44,6 @@ NativeWindow::NativeWindow(content::WebContents* web_contents, : content::WebContentsObserver(web_contents), has_frame_(true), is_closed_(false), - not_responding_(false), weak_factory_(this), inspectable_web_contents_( brightray::InspectableWebContents::Create(web_contents)) { @@ -226,7 +225,16 @@ void NativeWindow::CloseWebContents() { // not closed in 500ms, in this way we can quickly show the unresponsive // dialog when the window is busy executing some script withouth waiting for // the unresponsive timeout. - RendererUnresponsive(web_contents); + if (window_unresposive_closure_.IsCancelled()) { + window_unresposive_closure_.Reset( + base::Bind(&NativeWindow::RendererUnresponsive, + weak_factory_.GetWeakPtr(), + web_contents)); + base::MessageLoop::current()->PostDelayedTask( + FROM_HERE, + window_unresposive_closure_.callback(), + base::TimeDelta::FromMilliseconds(500)); + } if (web_contents->NeedToFireBeforeUnload()) web_contents->GetRenderViewHost()->FirePageBeforeUnload(false); @@ -286,7 +294,7 @@ void NativeWindow::BeforeUnloadFired(content::WebContents* tab, WindowList::WindowCloseCancelled(this); // When the "beforeunload" callback is fired the window is certainly live. - not_responding_ = false; + window_unresposive_closure_.Cancel(); } void NativeWindow::RequestToLockMouse(content::WebContents* web_contents, @@ -331,7 +339,7 @@ void NativeWindow::CloseContents(content::WebContents* source) { NotifyWindowClosed(); // Do not sent "unresponsive" event after window is closed. - not_responding_ = false; + window_unresposive_closure_.Cancel(); } bool NativeWindow::IsPopupOrPanel(const content::WebContents* source) const { @@ -340,16 +348,15 @@ bool NativeWindow::IsPopupOrPanel(const content::WebContents* source) const { } void NativeWindow::RendererUnresponsive(content::WebContents* source) { - not_responding_ = true; - base::MessageLoop::current()->PostDelayedTask( - FROM_HERE, - base::Bind(&NativeWindow::RendererUnresponsiveDelayed, - weak_factory_.GetWeakPtr()), - base::TimeDelta::FromMilliseconds(500)); + LOG(ERROR) << "NativeWindow::RendererUnresponsive"; + window_unresposive_closure_.Cancel(); + FOR_EACH_OBSERVER(NativeWindowObserver, + observers_, + OnRendererUnresponsive()); } void NativeWindow::RendererResponsive(content::WebContents* source) { - not_responding_ = false; + window_unresposive_closure_.Cancel(); FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnRendererResponsive()); } @@ -371,13 +378,6 @@ void NativeWindow::RenderViewGone(base::TerminationStatus status) { FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnRendererCrashed()); } -void NativeWindow::RendererUnresponsiveDelayed() { - if (not_responding_) - FOR_EACH_OBSERVER(NativeWindowObserver, - observers_, - OnRendererUnresponsive()); -} - void NativeWindow::Observe(int type, const content::NotificationSource& source, const content::NotificationDetails& details) { diff --git a/browser/native_window.h b/browser/native_window.h index a52448afedf5..d3ea4312c2fe 100644 --- a/browser/native_window.h +++ b/browser/native_window.h @@ -6,6 +6,7 @@ #define ATOM_BROWSER_NATIVE_WINDOW_H_ #include "base/basictypes.h" +#include "base/cancelable_callback.h" #include "base/compiler_specific.h" #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" @@ -192,8 +193,6 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, gfx::Image icon_; private: - void RendererUnresponsiveDelayed(); - // Called when CapturePage has done. void OnCapturePageDone(const CapturePageCallback& callback, bool succeed, @@ -215,8 +214,9 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, // The windows has been closed. bool is_closed_; - // The window is not responding. - bool not_responding_; + // Closure that would be called when window is unresponsive when closing, + // it should be cancelled when we can prove that the window is responsive. + base::CancelableClosure window_unresposive_closure_; base::WeakPtrFactory weak_factory_;