summaryrefslogtreecommitdiffstats
path: root/chrome/browser/hang_monitor/hung_window_detector.h
blob: 4de7bebd38b7b8d01a19f69f6116007c724b8c36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef CHROME_BROWSER_HUNG_WINDOW_DETECTOR_H__
#define CHROME_BROWSER_HUNG_WINDOW_DETECTOR_H__

#include "base/lock.h"
#include "chrome/common/worker_thread_ticker.h"

// This class provides the following functionality:
// Given a top-level window handle, it enumerates all descendant windows
// of that window and, on finding a window that belongs to a different
// thread from that of the top-level window, it tests to see if that window
// is responding to messages. It does this test by first calling the
// IsHungAppWindow API and, additionally (since the IsHungAppWindow API
// does not deal correctly with suspended threads), send a dummy message
// (WM_NULL) to the window and verifies that the call does not timeout.
// This class is typically used in conjunction with the WorkerThreadTicker
// class so that the checking can happen on a periodic basis.
// If a hung window is detected it calls back the specified implementation of
// the HungWindowNotification interface. Currently this class only supports
// a single callback but it can be extended later to support multiple
// callbacks.
class HungWindowDetector : public WorkerThreadTicker::Callback {
 public:
  // This property specifies the message timeout for a child window.
  static const wchar_t kHungChildWindowTimeout[];
  // This is the notification callback interface that is used to notify
  // callers about a non-responsive window
  class HungWindowNotification  {
   public:
    enum ActionOnHungWindow {
      HUNG_WINDOW_IGNORE,
      HUNG_WINDOW_TERMINATE_THREAD,
      HUNG_WINDOW_TERMINATE_PROCESS,
    };

    // This callback method is invoked when a hung window is detected.
    // A return value of false indicates that we should stop enumerating the
    // child windows of the browser window to check if they are hung.
    virtual bool OnHungWindowDetected(HWND hung_window, HWND top_level_window,
                                      ActionOnHungWindow* action) = 0;
  };

  // The notification object is not owned by this class. It is assumed that
  // this pointer will be valid throughout the lifetime of this class.
  // Ownership of this pointer is not transferred to this class.
  // Note that the Initialize method needs to be called to initiate monitoring
  // of hung windows.
  HungWindowDetector(HungWindowNotification* notification);
  ~HungWindowDetector();

  // This method initialized the monitoring of hung windows. All descendant
  // windows of the passed-in top-level window which belong to a thread
  // different from that of the top-level window are monitored. The
  // message_response_timeout parameter indicates how long this class must
  // wait for a window to respond to a sent message before it is considered
  // to be non-responsive.
  // Initialize can be called multiple times to change the actual window to
  // be monitored as well as the message response timeout
  bool Initialize(HWND top_level_window,
                  int message_response_timeout);

  // Implementation of the WorkerThreadTicker::Callback interface
  virtual void OnTick();

 private:
  // Helper function that checks whether the specified child window is hung.
  // If so, it invokes the HungWindowNotification interface implementation
  bool CheckChildWindow(HWND child_window);

  static BOOL CALLBACK ChildWndEnumProc(HWND child_window, LPARAM param);

  // Pointer to the HungWindowNotification callback interface. This class does
  // not RefCount this pointer and it is assumed that the pointer will be valid
  // throughout the lifetime of this class.
  HungWindowNotification* notification_;
  HWND top_level_window_;

  // How long do we wait before we consider a window hung (in ms)
  int message_response_timeout_;
  Lock hang_detection_lock_;
  // Indicates if this object is currently enumerating hung windows
  bool enumerating_;

  DISALLOW_EVIL_CONSTRUCTORS(HungWindowDetector);
};


#endif  // CHROME_BROWSER_HUNG_WINDOW_DETECTOR_H__