OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome_frame/test/win_event_receiver.h" | 5 #include "chrome_frame/test/win_event_receiver.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/weak_ptr.h" |
8 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
9 #include "base/win/object_watcher.h" | 11 #include "base/win/object_watcher.h" |
10 #include "base/string_util.h" | 12 #include "base/string_util.h" |
11 | |
12 #include "chrome_frame/function_stub.h" | 13 #include "chrome_frame/function_stub.h" |
13 | 14 |
14 // WinEventReceiver methods | 15 // WinEventReceiver methods |
15 WinEventReceiver::WinEventReceiver() | 16 WinEventReceiver::WinEventReceiver() |
16 : listener_(NULL), | 17 : listener_(NULL), |
17 hook_(NULL), | 18 hook_(NULL), |
18 hook_stub_(NULL) { | 19 hook_stub_(NULL) { |
19 } | 20 } |
20 | 21 |
21 WinEventReceiver::~WinEventReceiver() { | 22 WinEventReceiver::~WinEventReceiver() { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 virtual ~ProcessExitObserver(); | 87 virtual ~ProcessExitObserver(); |
87 | 88 |
88 // base::ObjectWatcher::Delegate implementation | 89 // base::ObjectWatcher::Delegate implementation |
89 virtual void OnObjectSignaled(HANDLE process_handle); | 90 virtual void OnObjectSignaled(HANDLE process_handle); |
90 | 91 |
91 private: | 92 private: |
92 WindowWatchdog* window_watchdog_; | 93 WindowWatchdog* window_watchdog_; |
93 HANDLE process_handle_; | 94 HANDLE process_handle_; |
94 HWND hwnd_; | 95 HWND hwnd_; |
95 | 96 |
96 ScopedRunnableMethodFactory<ProcessExitObserver> method_task_factory_; | 97 base::WeakPtrFactory<ProcessExitObserver> weak_factory_; |
97 base::win::ObjectWatcher object_watcher_; | 98 base::win::ObjectWatcher object_watcher_; |
98 | 99 |
99 DISALLOW_COPY_AND_ASSIGN(ProcessExitObserver); | 100 DISALLOW_COPY_AND_ASSIGN(ProcessExitObserver); |
100 }; | 101 }; |
101 | 102 |
102 WindowWatchdog::ProcessExitObserver::ProcessExitObserver( | 103 WindowWatchdog::ProcessExitObserver::ProcessExitObserver( |
103 WindowWatchdog* window_watchdog, HWND hwnd) | 104 WindowWatchdog* window_watchdog, HWND hwnd) |
104 : window_watchdog_(window_watchdog), | 105 : window_watchdog_(window_watchdog), |
105 process_handle_(NULL), | 106 process_handle_(NULL), |
106 hwnd_(hwnd), | 107 hwnd_(hwnd), |
107 ALLOW_THIS_IN_INITIALIZER_LIST(method_task_factory_(this)) { | 108 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
108 DWORD pid = 0; | 109 DWORD pid = 0; |
109 ::GetWindowThreadProcessId(hwnd, &pid); | 110 ::GetWindowThreadProcessId(hwnd, &pid); |
110 if (pid != 0) { | 111 if (pid != 0) { |
111 process_handle_ = ::OpenProcess(SYNCHRONIZE, FALSE, pid); | 112 process_handle_ = ::OpenProcess(SYNCHRONIZE, FALSE, pid); |
112 } | 113 } |
113 | 114 |
114 if (process_handle_ != NULL) { | 115 if (process_handle_ != NULL) { |
115 object_watcher_.StartWatching(process_handle_, this); | 116 object_watcher_.StartWatching(process_handle_, this); |
116 } else { | 117 } else { |
117 // Process is gone, so the window must be gone too. Notify our observer! | 118 // Process is gone, so the window must be gone too. Notify our observer! |
118 MessageLoop::current()->PostTask( | 119 MessageLoop::current()->PostTask( |
119 FROM_HERE, | 120 FROM_HERE, base::Bind(&ProcessExitObserver::OnObjectSignaled, |
120 method_task_factory_.NewRunnableMethod( | 121 weak_factory_.GetWeakPtr(), HANDLE(NULL))); |
121 &ProcessExitObserver::OnObjectSignaled, HANDLE(NULL))); | |
122 } | 122 } |
123 } | 123 } |
124 | 124 |
125 WindowWatchdog::ProcessExitObserver::~ProcessExitObserver() { | 125 WindowWatchdog::ProcessExitObserver::~ProcessExitObserver() { |
126 if (process_handle_ != NULL) { | 126 if (process_handle_ != NULL) { |
127 ::CloseHandle(process_handle_); | 127 ::CloseHandle(process_handle_); |
128 } | 128 } |
129 } | 129 } |
130 | 130 |
131 void WindowWatchdog::ProcessExitObserver::OnObjectSignaled( | 131 void WindowWatchdog::ProcessExitObserver::OnObjectSignaled( |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 HandleOnOpen(hwnd); | 266 HandleOnOpen(hwnd); |
267 } else { | 267 } else { |
268 DCHECK(event == EVENT_OBJECT_DESTROY || event == EVENT_OBJECT_HIDE); | 268 DCHECK(event == EVENT_OBJECT_DESTROY || event == EVENT_OBJECT_HIDE); |
269 HandleOnClose(hwnd); | 269 HandleOnClose(hwnd); |
270 } | 270 } |
271 } | 271 } |
272 | 272 |
273 void WindowWatchdog::OnHwndProcessExited(HWND hwnd) { | 273 void WindowWatchdog::OnHwndProcessExited(HWND hwnd) { |
274 HandleOnClose(hwnd); | 274 HandleOnClose(hwnd); |
275 } | 275 } |
OLD | NEW |