OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/event_recorder.h" | 5 #include "base/event_recorder.h" |
6 | 6 |
7 #include <mmsystem.h> | 7 #include <mmsystem.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 return EventRecorder::current()->PlaybackWndProc(nCode, wParam, lParam); | 33 return EventRecorder::current()->PlaybackWndProc(nCode, wParam, lParam); |
34 } | 34 } |
35 | 35 |
36 EventRecorder::~EventRecorder() { | 36 EventRecorder::~EventRecorder() { |
37 // Try to assert early if the caller deletes the recorder | 37 // Try to assert early if the caller deletes the recorder |
38 // while it is still in use. | 38 // while it is still in use. |
39 DCHECK(!journal_hook_); | 39 DCHECK(!journal_hook_); |
40 DCHECK(!is_recording_ && !is_playing_); | 40 DCHECK(!is_recording_ && !is_playing_); |
41 } | 41 } |
42 | 42 |
43 bool EventRecorder::StartRecording(std::wstring& filename) { | 43 bool EventRecorder::StartRecording(const std::wstring& filename) { |
44 if (journal_hook_ != NULL) | 44 if (journal_hook_ != NULL) |
45 return false; | 45 return false; |
46 if (is_recording_ || is_playing_) | 46 if (is_recording_ || is_playing_) |
47 return false; | 47 return false; |
48 | 48 |
49 // Open the recording file. | 49 // Open the recording file. |
50 DCHECK(file_ == NULL); | 50 DCHECK(file_ == NULL); |
51 if (_wfopen_s(&file_, filename.c_str(), L"wb+") != 0) { | 51 if (_wfopen_s(&file_, filename.c_str(), L"wb+") != 0) { |
52 DLOG(ERROR) << "EventRecorder could not open log file"; | 52 DLOG(ERROR) << "EventRecorder could not open log file"; |
53 return false; | 53 return false; |
(...skipping 29 matching lines...) Expand all Loading... |
83 | 83 |
84 DCHECK(file_ != NULL); | 84 DCHECK(file_ != NULL); |
85 fclose(file_); | 85 fclose(file_); |
86 file_ = NULL; | 86 file_ = NULL; |
87 | 87 |
88 journal_hook_ = NULL; | 88 journal_hook_ = NULL; |
89 is_recording_ = false; | 89 is_recording_ = false; |
90 } | 90 } |
91 } | 91 } |
92 | 92 |
93 bool EventRecorder::StartPlayback(std::wstring& filename) { | 93 bool EventRecorder::StartPlayback(const std::wstring& filename) { |
94 if (journal_hook_ != NULL) | 94 if (journal_hook_ != NULL) |
95 return false; | 95 return false; |
96 if (is_recording_ || is_playing_) | 96 if (is_recording_ || is_playing_) |
97 return false; | 97 return false; |
98 | 98 |
99 // Open the recording file. | 99 // Open the recording file. |
100 DCHECK(file_ == NULL); | 100 DCHECK(file_ == NULL); |
101 if (_wfopen_s(&file_, filename.c_str(), L"rb") != 0) { | 101 if (_wfopen_s(&file_, filename.c_str(), L"rb") != 0) { |
102 DLOG(ERROR) << "EventRecorder Playback could not open log file"; | 102 DLOG(ERROR) << "EventRecorder Playback could not open log file"; |
103 return false; | 103 return false; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 ::timeEndPeriod(1); | 153 ::timeEndPeriod(1); |
154 | 154 |
155 journal_hook_ = NULL; | 155 journal_hook_ = NULL; |
156 is_playing_ = false; | 156 is_playing_ = false; |
157 } | 157 } |
158 } | 158 } |
159 | 159 |
160 // Windows callback hook for the recorder. | 160 // Windows callback hook for the recorder. |
161 LRESULT EventRecorder::RecordWndProc(int nCode, WPARAM wParam, LPARAM lParam) { | 161 LRESULT EventRecorder::RecordWndProc(int nCode, WPARAM wParam, LPARAM lParam) { |
162 static bool recording_enabled = true; | 162 static bool recording_enabled = true; |
163 EVENTMSG *msg_ptr = NULL; | 163 EVENTMSG* msg_ptr = NULL; |
164 | 164 |
165 // The API says we have to do this. | 165 // The API says we have to do this. |
166 // See http://msdn2.microsoft.com/en-us/library/ms644983(VS.85).aspx | 166 // See http://msdn2.microsoft.com/en-us/library/ms644983(VS.85).aspx |
167 if (nCode < 0) | 167 if (nCode < 0) |
168 return ::CallNextHookEx(journal_hook_, nCode, wParam, lParam); | 168 return ::CallNextHookEx(journal_hook_, nCode, wParam, lParam); |
169 | 169 |
170 // Check for the break key being pressed and stop recording. | 170 // Check for the break key being pressed and stop recording. |
171 if (::GetKeyState(VK_CANCEL) & 0x8000) { | 171 if (::GetKeyState(VK_CANCEL) & 0x8000) { |
172 StopRecording(); | 172 StopRecording(); |
173 return ::CallNextHookEx(journal_hook_, nCode, wParam, lParam); | 173 return ::CallNextHookEx(journal_hook_, nCode, wParam, lParam); |
174 } | 174 } |
175 | 175 |
176 // The Journal Recorder must stop recording events when system modal | 176 // The Journal Recorder must stop recording events when system modal |
177 // dialogs are present. (see msdn link above) | 177 // dialogs are present. (see msdn link above) |
178 switch(nCode) | 178 switch(nCode) { |
179 { | |
180 case HC_SYSMODALON: | 179 case HC_SYSMODALON: |
181 » recording_enabled = false; | 180 recording_enabled = false; |
182 break; | 181 break; |
183 case HC_SYSMODALOFF: | 182 case HC_SYSMODALOFF: |
184 » recording_enabled = true; | 183 recording_enabled = true; |
185 break; | 184 break; |
186 } | 185 } |
187 | 186 |
188 if (nCode == HC_ACTION && recording_enabled) { | 187 if (nCode == HC_ACTION && recording_enabled) { |
189 // Aha - we have an event to record. | 188 // Aha - we have an event to record. |
190 msg_ptr = reinterpret_cast<EVENTMSG*>(lParam); | 189 msg_ptr = reinterpret_cast<EVENTMSG*>(lParam); |
191 msg_ptr->time = timeGetTime(); | 190 msg_ptr->time = timeGetTime(); |
192 fwrite(msg_ptr, sizeof(EVENTMSG), 1, file_); | 191 fwrite(msg_ptr, sizeof(EVENTMSG), 1, file_); |
193 fflush(file_); | 192 fflush(file_); |
194 } | 193 } |
195 | 194 |
196 return CallNextHookEx(journal_hook_, nCode, wParam, lParam); | 195 return CallNextHookEx(journal_hook_, nCode, wParam, lParam); |
197 } | 196 } |
198 | 197 |
199 // Windows callback for the playback mode. | 198 // Windows callback for the playback mode. |
200 LRESULT EventRecorder::PlaybackWndProc(int nCode, WPARAM wParam, LPARAM lParam) | 199 LRESULT EventRecorder::PlaybackWndProc(int nCode, WPARAM wParam, |
201 { | 200 LPARAM lParam) { |
202 static bool playback_enabled = true; | 201 static bool playback_enabled = true; |
203 int delay = 0; | 202 int delay = 0; |
204 | 203 |
205 switch(nCode) | 204 switch(nCode) { |
206 { | |
207 // A system modal dialog box is being displayed. Stop playing back | 205 // A system modal dialog box is being displayed. Stop playing back |
208 // messages. | 206 // messages. |
209 case HC_SYSMODALON: | 207 case HC_SYSMODALON: |
210 » playback_enabled = false; | 208 playback_enabled = false; |
211 » break; | 209 break; |
212 | 210 |
213 // A system modal dialog box is destroyed. We can start playing back | 211 // A system modal dialog box is destroyed. We can start playing back |
214 // messages again. | 212 // messages again. |
215 case HC_SYSMODALOFF: | 213 case HC_SYSMODALOFF: |
216 » playback_enabled = true; | 214 playback_enabled = true; |
217 » break; | 215 break; |
218 | 216 |
219 // Prepare to copy the next mouse or keyboard event to playback. | 217 // Prepare to copy the next mouse or keyboard event to playback. |
220 case HC_SKIP: | 218 case HC_SKIP: |
221 » if (!playback_enabled) | 219 if (!playback_enabled) |
222 » break; | 220 break; |
223 | 221 |
224 // Read the next event from the record. | 222 // Read the next event from the record. |
225 if (fread(&playback_msg_, sizeof(EVENTMSG), 1, file_) != 1) | 223 if (fread(&playback_msg_, sizeof(EVENTMSG), 1, file_) != 1) |
226 this->StopPlayback(); | 224 this->StopPlayback(); |
227 » break; | 225 break; |
228 | 226 |
229 // Copy the mouse or keyboard event to the EVENTMSG structure in lParam. | 227 // Copy the mouse or keyboard event to the EVENTMSG structure in lParam. |
230 case HC_GETNEXT: | 228 case HC_GETNEXT: |
231 » if (!playback_enabled) | 229 if (!playback_enabled) |
232 break; | 230 break; |
233 | 231 |
234 memcpy(reinterpret_cast<void*>(lParam), &playback_msg_, sizeof(playback_ms
g_)); | 232 memcpy(reinterpret_cast<void*>(lParam), &playback_msg_, |
| 233 sizeof(playback_msg_)); |
235 | 234 |
236 // The return value is the amount of time (in milliseconds) to wait | 235 // The return value is the amount of time (in milliseconds) to wait |
237 // before playing back the next message in the playback queue. Each | 236 // before playing back the next message in the playback queue. Each |
238 // time this is called, we recalculate the delay relative to our current | 237 // time this is called, we recalculate the delay relative to our current |
239 // wall clock. | 238 // wall clock. |
240 delay = (playback_msg_.time - playback_first_msg_time_) - | 239 delay = (playback_msg_.time - playback_first_msg_time_) - |
241 (timeGetTime() - playback_start_time_); | 240 (timeGetTime() - playback_start_time_); |
242 if (delay < 0) | 241 if (delay < 0) |
243 delay = 0; | 242 delay = 0; |
244 return delay; | 243 return delay; |
245 | 244 |
246 // An application has called PeekMessage with wRemoveMsg set to PM_NOREMOVE | 245 // An application has called PeekMessage with wRemoveMsg set to PM_NOREMOVE |
247 // indicating that the message is not removed from the message queue after | 246 // indicating that the message is not removed from the message queue after |
248 // PeekMessage processing. | 247 // PeekMessage processing. |
249 case HC_NOREMOVE: | 248 case HC_NOREMOVE: |
250 break; | 249 break; |
251 } | 250 } |
252 | 251 |
253 return CallNextHookEx(journal_hook_, nCode, wParam, lParam); | 252 return CallNextHookEx(journal_hook_, nCode, wParam, lParam); |
254 } | 253 } |
255 | 254 |
256 } // namespace base | 255 } // namespace base |
257 | |
OLD | NEW |