| 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/file_util.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/time.h" | 11 #include "base/time.h" |
| 11 | 12 |
| 12 // A note about time. | 13 // A note about time. |
| 13 // For perfect playback of events, you'd like a very accurate timer | 14 // For perfect playback of events, you'd like a very accurate timer |
| 14 // so that events are played back at exactly the same time that | 15 // so that events are played back at exactly the same time that |
| 15 // they were recorded. However, windows has a clock which is only | 16 // they were recorded. However, windows has a clock which is only |
| 16 // granular to ~15ms. We see more consistent event playback when | 17 // granular to ~15ms. We see more consistent event playback when |
| 17 // using a higher resolution timer. To do this, we use the | 18 // using a higher resolution timer. To do this, we use the |
| 18 // timeGetTime API instead of the default GetTickCount() API. | 19 // timeGetTime API instead of the default GetTickCount() API. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 41 } | 42 } |
| 42 | 43 |
| 43 bool EventRecorder::StartRecording(std::wstring& filename) { | 44 bool EventRecorder::StartRecording(std::wstring& filename) { |
| 44 if (journal_hook_ != NULL) | 45 if (journal_hook_ != NULL) |
| 45 return false; | 46 return false; |
| 46 if (is_recording_ || is_playing_) | 47 if (is_recording_ || is_playing_) |
| 47 return false; | 48 return false; |
| 48 | 49 |
| 49 // Open the recording file. | 50 // Open the recording file. |
| 50 DCHECK(file_ == NULL); | 51 DCHECK(file_ == NULL); |
| 51 if (_wfopen_s(&file_, filename.c_str(), L"wb+") != 0) { | 52 file_ = file_util::OpenFile(filename, "wb+"); |
| 53 if (!file_) { |
| 52 DLOG(ERROR) << "EventRecorder could not open log file"; | 54 DLOG(ERROR) << "EventRecorder could not open log file"; |
| 53 return false; | 55 return false; |
| 54 } | 56 } |
| 55 | 57 |
| 56 // Set the faster clock, if possible. | 58 // Set the faster clock, if possible. |
| 57 ::timeBeginPeriod(1); | 59 ::timeBeginPeriod(1); |
| 58 | 60 |
| 59 // Set the recording hook. JOURNALRECORD can only be used as a global hook. | 61 // Set the recording hook. JOURNALRECORD can only be used as a global hook. |
| 60 journal_hook_ = ::SetWindowsHookEx(WH_JOURNALRECORD, StaticRecordWndProc, | 62 journal_hook_ = ::SetWindowsHookEx(WH_JOURNALRECORD, StaticRecordWndProc, |
| 61 GetModuleHandle(NULL), 0); | 63 GetModuleHandle(NULL), 0); |
| 62 if (!journal_hook_) { | 64 if (!journal_hook_) { |
| 63 DLOG(ERROR) << "EventRecorder Record Hook failed"; | 65 DLOG(ERROR) << "EventRecorder Record Hook failed"; |
| 64 fclose(file_); | 66 file_util::CloseFile(file_); |
| 65 return false; | 67 return false; |
| 66 } | 68 } |
| 67 | 69 |
| 68 is_recording_ = true; | 70 is_recording_ = true; |
| 69 return true; | 71 return true; |
| 70 } | 72 } |
| 71 | 73 |
| 72 void EventRecorder::StopRecording() { | 74 void EventRecorder::StopRecording() { |
| 73 if (is_recording_) { | 75 if (is_recording_) { |
| 74 DCHECK(journal_hook_ != NULL); | 76 DCHECK(journal_hook_ != NULL); |
| 75 | 77 |
| 76 if (!::UnhookWindowsHookEx(journal_hook_)) { | 78 if (!::UnhookWindowsHookEx(journal_hook_)) { |
| 77 DLOG(ERROR) << "EventRecorder Unhook failed"; | 79 DLOG(ERROR) << "EventRecorder Unhook failed"; |
| 78 // Nothing else we can really do here. | 80 // Nothing else we can really do here. |
| 79 return; | 81 return; |
| 80 } | 82 } |
| 81 | 83 |
| 82 ::timeEndPeriod(1); | 84 ::timeEndPeriod(1); |
| 83 | 85 |
| 84 DCHECK(file_ != NULL); | 86 DCHECK(file_ != NULL); |
| 85 fclose(file_); | 87 file_util::CloseFile(file_); |
| 86 file_ = NULL; | 88 file_ = NULL; |
| 87 | 89 |
| 88 journal_hook_ = NULL; | 90 journal_hook_ = NULL; |
| 89 is_recording_ = false; | 91 is_recording_ = false; |
| 90 } | 92 } |
| 91 } | 93 } |
| 92 | 94 |
| 93 bool EventRecorder::StartPlayback(std::wstring& filename) { | 95 bool EventRecorder::StartPlayback(std::wstring& filename) { |
| 94 if (journal_hook_ != NULL) | 96 if (journal_hook_ != NULL) |
| 95 return false; | 97 return false; |
| 96 if (is_recording_ || is_playing_) | 98 if (is_recording_ || is_playing_) |
| 97 return false; | 99 return false; |
| 98 | 100 |
| 99 // Open the recording file. | 101 // Open the recording file. |
| 100 DCHECK(file_ == NULL); | 102 DCHECK(file_ == NULL); |
| 101 if (_wfopen_s(&file_, filename.c_str(), L"rb") != 0) { | 103 file_ = file_util::OpenFile(filename, "rb"); |
| 104 if (!file_) { |
| 102 DLOG(ERROR) << "EventRecorder Playback could not open log file"; | 105 DLOG(ERROR) << "EventRecorder Playback could not open log file"; |
| 103 return false; | 106 return false; |
| 104 } | 107 } |
| 105 // Read the first event from the record. | 108 // Read the first event from the record. |
| 106 if (fread(&playback_msg_, sizeof(EVENTMSG), 1, file_) != 1) { | 109 if (fread(&playback_msg_, sizeof(EVENTMSG), 1, file_) != 1) { |
| 107 DLOG(ERROR) << "EventRecorder Playback has no records!"; | 110 DLOG(ERROR) << "EventRecorder Playback has no records!"; |
| 108 fclose(file_); | 111 file_util::CloseFile(file_); |
| 109 return false; | 112 return false; |
| 110 } | 113 } |
| 111 | 114 |
| 112 // Set the faster clock, if possible. | 115 // Set the faster clock, if possible. |
| 113 ::timeBeginPeriod(1); | 116 ::timeBeginPeriod(1); |
| 114 | 117 |
| 115 // Playback time is tricky. When playing back, we read a series of events, | 118 // Playback time is tricky. When playing back, we read a series of events, |
| 116 // each with timeouts. Simply subtracting the delta between two timers will | 119 // each with timeouts. Simply subtracting the delta between two timers will |
| 117 // lead to fast playback (about 2x speed). The API has two events, one | 120 // lead to fast playback (about 2x speed). The API has two events, one |
| 118 // which advances to the next event (HC_SKIP), and another that requests the | 121 // which advances to the next event (HC_SKIP), and another that requests the |
| (...skipping 21 matching lines...) Expand all Loading... |
| 140 void EventRecorder::StopPlayback() { | 143 void EventRecorder::StopPlayback() { |
| 141 if (is_playing_) { | 144 if (is_playing_) { |
| 142 DCHECK(journal_hook_ != NULL); | 145 DCHECK(journal_hook_ != NULL); |
| 143 | 146 |
| 144 if (!::UnhookWindowsHookEx(journal_hook_)) { | 147 if (!::UnhookWindowsHookEx(journal_hook_)) { |
| 145 DLOG(ERROR) << "EventRecorder Unhook failed"; | 148 DLOG(ERROR) << "EventRecorder Unhook failed"; |
| 146 // Nothing else we can really do here. | 149 // Nothing else we can really do here. |
| 147 } | 150 } |
| 148 | 151 |
| 149 DCHECK(file_ != NULL); | 152 DCHECK(file_ != NULL); |
| 150 fclose(file_); | 153 file_util::CloseFile(file_); |
| 151 file_ = NULL; | 154 file_ = NULL; |
| 152 | 155 |
| 153 ::timeEndPeriod(1); | 156 ::timeEndPeriod(1); |
| 154 | 157 |
| 155 journal_hook_ = NULL; | 158 journal_hook_ = NULL; |
| 156 is_playing_ = false; | 159 is_playing_ = false; |
| 157 } | 160 } |
| 158 } | 161 } |
| 159 | 162 |
| 160 // Windows callback hook for the recorder. | 163 // Windows callback hook for the recorder. |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 // PeekMessage processing. | 251 // PeekMessage processing. |
| 249 case HC_NOREMOVE: | 252 case HC_NOREMOVE: |
| 250 break; | 253 break; |
| 251 } | 254 } |
| 252 | 255 |
| 253 return CallNextHookEx(journal_hook_, nCode, wParam, lParam); | 256 return CallNextHookEx(journal_hook_, nCode, wParam, lParam); |
| 254 } | 257 } |
| 255 | 258 |
| 256 } // namespace base | 259 } // namespace base |
| 257 | 260 |
| OLD | NEW |