Home | History | Annotate | Download | only in hang_monitor
      1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_HANG_MONITOR_HUNG_WINDOW_DETECTOR_H__
      6 #define CHROME_BROWSER_HANG_MONITOR_HUNG_WINDOW_DETECTOR_H__
      7 
      8 #include "base/synchronization/lock.h"
      9 #include "chrome/common/worker_thread_ticker.h"
     10 
     11 // This class provides the following functionality:
     12 // Given a top-level window handle, it enumerates all descendant windows
     13 // of that window and, on finding a window that belongs to a different
     14 // thread from that of the top-level window, it tests to see if that window
     15 // is responding to messages. It does this test by first calling the
     16 // IsHungAppWindow API and, additionally (since the IsHungAppWindow API
     17 // does not deal correctly with suspended threads), send a dummy message
     18 // (WM_NULL) to the window and verifies that the call does not timeout.
     19 // This class is typically used in conjunction with the WorkerThreadTicker
     20 // class so that the checking can happen on a periodic basis.
     21 // If a hung window is detected it calls back the specified implementation of
     22 // the HungWindowNotification interface. Currently this class only supports
     23 // a single callback but it can be extended later to support multiple
     24 // callbacks.
     25 class HungWindowDetector : public WorkerThreadTicker::Callback {
     26  public:
     27   // This property specifies the message timeout for a child window.
     28   static const wchar_t kHungChildWindowTimeout[];
     29   // This is the notification callback interface that is used to notify
     30   // callers about a non-responsive window
     31   class HungWindowNotification  {
     32    public:
     33     enum ActionOnHungWindow {
     34       HUNG_WINDOW_IGNORE,
     35       HUNG_WINDOW_TERMINATE_PROCESS,
     36     };
     37 
     38     // This callback method is invoked when a hung window is detected.
     39     // A return value of false indicates that we should stop enumerating the
     40     // child windows of the browser window to check if they are hung.
     41     virtual bool OnHungWindowDetected(HWND hung_window, HWND top_level_window,
     42                                       ActionOnHungWindow* action) = 0;
     43   };
     44 
     45   // The notification object is not owned by this class. It is assumed that
     46   // this pointer will be valid throughout the lifetime of this class.
     47   // Ownership of this pointer is not transferred to this class.
     48   // Note that the Initialize method needs to be called to initiate monitoring
     49   // of hung windows.
     50   explicit HungWindowDetector(HungWindowNotification* notification);
     51   ~HungWindowDetector();
     52 
     53   // This method initialized the monitoring of hung windows. All descendant
     54   // windows of the passed-in top-level window which belong to a thread
     55   // different from that of the top-level window are monitored. The
     56   // message_response_timeout parameter indicates how long this class must
     57   // wait for a window to respond to a sent message before it is considered
     58   // to be non-responsive.
     59   // Initialize can be called multiple times to change the actual window to
     60   // be monitored as well as the message response timeout
     61   bool Initialize(HWND top_level_window,
     62                   int message_response_timeout);
     63 
     64   // Implementation of the WorkerThreadTicker::Callback interface
     65   virtual void OnTick();
     66 
     67  private:
     68   // Helper function that checks whether the specified child window is hung.
     69   // If so, it invokes the HungWindowNotification interface implementation
     70   bool CheckChildWindow(HWND child_window);
     71 
     72   static BOOL CALLBACK ChildWndEnumProc(HWND child_window, LPARAM param);
     73 
     74   // Pointer to the HungWindowNotification callback interface. This class does
     75   // not RefCount this pointer and it is assumed that the pointer will be valid
     76   // throughout the lifetime of this class.
     77   HungWindowNotification* notification_;
     78   HWND top_level_window_;
     79 
     80   // How long do we wait before we consider a window hung (in ms)
     81   int message_response_timeout_;
     82   base::Lock hang_detection_lock_;
     83   // Indicates if this object is currently enumerating hung windows
     84   bool enumerating_;
     85 
     86   DISALLOW_COPY_AND_ASSIGN(HungWindowDetector);
     87 };
     88 
     89 
     90 #endif  // CHROME_BROWSER_HANG_MONITOR_HUNG_WINDOW_DETECTOR_H__
     91