Delay the unresponsive message for a second, fixes #42.
authorCheng Zhao <zcbenz@gmail.com>
Tue, 23 Jul 2013 07:29:56 +0000 (15:29 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 23 Jul 2013 07:29:56 +0000 (15:29 +0800)
It could happen that a window became responsive immediately after the
unresponsive message is sent (for example, the window was blocked by
showing a save as dialog), by delaying sending the unresponsive message
for a second, we can give the window a chance to whether it's really
unresponsive or not.

browser/native_window.cc
browser/native_window.h

index b7bed17..391e647 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <string>
 
+#include "base/message_loop.h"
 #include "base/utf_string_conversions.h"
 #include "base/values.h"
 #include "brightray/browser/inspectable_web_contents.h"
@@ -39,6 +40,7 @@ NativeWindow::NativeWindow(content::WebContents* web_contents,
                            base::DictionaryValue* options)
     : content::WebContentsObserver(web_contents),
       is_closed_(false),
+      not_responding_(false),
       inspectable_web_contents_(
           brightray::InspectableWebContents::Create(web_contents)) {
   web_contents->SetDelegate(this);
@@ -275,10 +277,16 @@ bool NativeWindow::IsPopupOrPanel(const content::WebContents* source) const {
 }
 
 void NativeWindow::RendererUnresponsive(content::WebContents* source) {
-  FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnRendererUnresponsive());
+  not_responding_ = true;
+  base::MessageLoop::current()->PostDelayedTask(
+      FROM_HERE,
+      base::Bind(&NativeWindow::RendererUnresponsiveDelayed,
+                 base::Unretained(this)),
+      base::TimeDelta::FromSeconds(1));
 }
 
 void NativeWindow::RendererResponsive(content::WebContents* source) {
+  not_responding_ = false;
   FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnRendererResponsive());
 }
 
@@ -297,6 +305,13 @@ 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) {
index c6a6c84..762920c 100644 (file)
@@ -163,6 +163,8 @@ class NativeWindow : public content::WebContentsDelegate,
                        const content::NotificationDetails& details) OVERRIDE;
 
  private:
+  void RendererUnresponsiveDelayed();
+
   void OnRendererMessage(const std::string& channel,
                          const base::ListValue& args);
 
@@ -176,8 +178,12 @@ class NativeWindow : public content::WebContentsDelegate,
   // Observers of this window.
   ObserverList<NativeWindowObserver> observers_;
 
+  // The windows has been closed.
   bool is_closed_;
 
+  // The window is not responding.
+  bool not_responding_;
+
   scoped_ptr<AtomJavaScriptDialogManager> dialog_manager_;
   scoped_ptr<brightray::InspectableWebContents> inspectable_web_contents_;