OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 #include <windows.h> | |
6 | |
7 #include "chrome/browser/hang_monitor/hung_plugin_action.h" | |
8 | |
9 #include "base/metrics/histogram.h" | |
10 #include "base/version.h" | |
11 #include "base/win/win_util.h" | |
12 #include "chrome/browser/ui/simple_message_box.h" | |
13 #include "chrome/common/logging_chrome.h" | |
14 #include "chrome/grit/generated_resources.h" | |
15 #include "content/public/browser/plugin_service.h" | |
16 #include "content/public/common/webplugininfo.h" | |
17 #include "ui/base/l10n/l10n_util.h" | |
18 #include "ui/gfx/win/hwnd_util.h" | |
19 | |
20 HungPluginAction::HungPluginAction() : current_hung_plugin_window_(NULL) { | |
21 } | |
22 | |
23 HungPluginAction::~HungPluginAction() { | |
24 } | |
25 | |
26 bool HungPluginAction::OnHungWindowDetected(HWND hung_window, | |
27 HWND top_level_window, | |
28 ActionOnHungWindow* action) { | |
29 if (NULL == action) { | |
30 return false; | |
31 } | |
32 if (!IsWindow(hung_window)) { | |
33 return false; | |
34 } | |
35 | |
36 bool continue_hang_detection = true; | |
37 | |
38 DWORD hung_window_process_id = 0; | |
39 DWORD top_level_window_process_id = 0; | |
40 GetWindowThreadProcessId(hung_window, &hung_window_process_id); | |
41 GetWindowThreadProcessId(top_level_window, &top_level_window_process_id); | |
42 | |
43 *action = HungWindowNotification::HUNG_WINDOW_IGNORE; | |
44 if (top_level_window_process_id != hung_window_process_id) { | |
45 base::string16 plugin_name = | |
46 l10n_util::GetStringUTF16(IDS_UNKNOWN_PLUGIN_NAME); | |
47 | |
48 if (logging::DialogsAreSuppressed()) { | |
49 NOTREACHED() << "Terminated a hung plugin process."; | |
50 *action = HungWindowNotification::HUNG_WINDOW_TERMINATE_PROCESS; | |
51 } else { | |
52 const base::string16 title = l10n_util::GetStringUTF16( | |
53 IDS_BROWSER_HANGMONITOR_TITLE); | |
54 const base::string16 message = l10n_util::GetStringFUTF16( | |
55 IDS_BROWSER_HANGMONITOR, plugin_name); | |
56 // Before displaying the message box, invoke SendMessageCallback on the | |
57 // hung window. If the callback ever hits, the window is not hung anymore | |
58 // and we can dismiss the message box. | |
59 SendMessageCallback(hung_window, | |
60 WM_NULL, | |
61 0, | |
62 0, | |
63 HungWindowResponseCallback, | |
64 reinterpret_cast<ULONG_PTR>(this)); | |
65 current_hung_plugin_window_ = hung_window; | |
66 if (chrome::ShowQuestionMessageBox(NULL, title, message) == | |
67 chrome::MESSAGE_BOX_RESULT_YES) { | |
68 *action = HungWindowNotification::HUNG_WINDOW_TERMINATE_PROCESS; | |
69 } else { | |
70 // If the user choses to ignore the hung window warning, the | |
71 // message timeout for this window should be doubled. We only | |
72 // double the timeout property on the window if the property | |
73 // exists. The property is deleted if the window becomes | |
74 // responsive. | |
75 continue_hang_detection = false; | |
76 int child_window_message_timeout = base::win::HandleToUint32( | |
77 GetProp(hung_window, HungWindowDetector::kHungChildWindowTimeout)); | |
78 if (child_window_message_timeout) { | |
79 child_window_message_timeout *= 2; | |
80 #pragma warning(disable:4312) | |
81 SetProp(hung_window, HungWindowDetector::kHungChildWindowTimeout, | |
82 reinterpret_cast<HANDLE>(child_window_message_timeout)); | |
83 #pragma warning(default:4312) | |
84 } | |
85 } | |
86 current_hung_plugin_window_ = NULL; | |
87 } | |
88 } | |
89 if (HungWindowNotification::HUNG_WINDOW_TERMINATE_PROCESS == *action) { | |
90 // Enable the top-level window just in case the plugin had been | |
91 // displaying a modal box that had disabled the top-level window | |
92 EnableWindow(top_level_window, TRUE); | |
93 } | |
94 return continue_hang_detection; | |
95 } | |
96 | |
97 void HungPluginAction::OnWindowResponsive(HWND window) { | |
98 if (window == current_hung_plugin_window_) { | |
99 // The message timeout for this window should fallback to the default | |
100 // timeout as this window is now responsive. | |
101 RemoveProp(window, HungWindowDetector::kHungChildWindowTimeout); | |
102 // The monitored plugin recovered. Let's dismiss the message box. | |
103 EnumThreadWindows(GetCurrentThreadId(), | |
104 reinterpret_cast<WNDENUMPROC>(DismissMessageBox), | |
105 NULL); | |
106 } | |
107 } | |
108 | |
109 // static | |
110 BOOL CALLBACK HungPluginAction::DismissMessageBox(HWND window, LPARAM ignore) { | |
111 base::string16 class_name = gfx::GetClassName(window); | |
112 // #32770 is the dialog window class which is the window class of | |
113 // the message box being displayed. | |
114 if (class_name == L"#32770") { | |
115 EndDialog(window, IDNO); | |
116 return FALSE; | |
117 } | |
118 return TRUE; | |
119 } | |
120 | |
121 // static | |
122 void CALLBACK HungPluginAction::HungWindowResponseCallback(HWND target_window, | |
123 UINT message, | |
124 ULONG_PTR data, | |
125 LRESULT result) { | |
126 HungPluginAction* instance = reinterpret_cast<HungPluginAction*>(data); | |
127 DCHECK(NULL != instance); | |
128 if (NULL != instance) { | |
129 instance->OnWindowResponsive(target_window); | |
130 } | |
131 } | |
OLD | NEW |