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