license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 5 | #ifndef BASE_EVENT_RECORDER_H_ |
| 6 | #define BASE_EVENT_RECORDER_H_ |
| 7 | |
| 8 | #include <string> |
[email protected] | 238a20d | 2009-01-26 16:33:29 | [diff] [blame] | 9 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 10 | #include <windows.h> |
[email protected] | 238a20d | 2009-01-26 16:33:29 | [diff] [blame] | 11 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | #include "base/basictypes.h" |
| 13 | |
[email protected] | 4f09393 | 2009-05-01 04:31:22 | [diff] [blame] | 14 | class FilePath; |
| 15 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | namespace base { |
| 17 | |
| 18 | // A class for recording and playing back keyboard and mouse input events. |
| 19 | // |
| 20 | // Note - if you record events, and the playback with the windows in |
| 21 | // different sizes or positions, the playback will fail. When |
| 22 | // recording and playing, you should move the relevant windows |
| 23 | // to constant sizes and locations. |
| 24 | // TODO(mbelshe) For now this is a singleton. I believe that this class |
| 25 | // could be easily modified to: |
| 26 | // support two simultaneous recorders |
| 27 | // be playing back events while already recording events. |
| 28 | // Why? Imagine if the product had a "record a macro" feature. |
| 29 | // You might be recording globally, while recording or playing back |
| 30 | // a macro. I don't think two playbacks make sense. |
| 31 | class EventRecorder { |
| 32 | public: |
| 33 | // Get the singleton EventRecorder. |
| 34 | // We can only handle one recorder/player at a time. |
| 35 | static EventRecorder* current() { |
| 36 | if (!current_) |
| 37 | current_ = new EventRecorder(); |
| 38 | return current_; |
| 39 | } |
| 40 | |
| 41 | // Starts recording events. |
| 42 | // Will clobber the file if it already exists. |
| 43 | // Returns true on success, or false if an error occurred. |
[email protected] | 4f09393 | 2009-05-01 04:31:22 | [diff] [blame] | 44 | bool StartRecording(const FilePath& filename); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 45 | |
| 46 | // Stops recording. |
| 47 | void StopRecording(); |
| 48 | |
| 49 | // Is the EventRecorder currently recording. |
| 50 | bool is_recording() const { return is_recording_; } |
| 51 | |
| 52 | // Plays events previously recorded. |
| 53 | // Returns true on success, or false if an error occurred. |
[email protected] | 4f09393 | 2009-05-01 04:31:22 | [diff] [blame] | 54 | bool StartPlayback(const FilePath& filename); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 55 | |
| 56 | // Stops playback. |
| 57 | void StopPlayback(); |
| 58 | |
| 59 | // Is the EventRecorder currently playing. |
| 60 | bool is_playing() const { return is_playing_; } |
| 61 | |
[email protected] | 238a20d | 2009-01-26 16:33:29 | [diff] [blame] | 62 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 63 | // C-style callbacks for the EventRecorder. |
| 64 | // Used for internal purposes only. |
| 65 | LRESULT RecordWndProc(int nCode, WPARAM wParam, LPARAM lParam); |
| 66 | LRESULT PlaybackWndProc(int nCode, WPARAM wParam, LPARAM lParam); |
[email protected] | 238a20d | 2009-01-26 16:33:29 | [diff] [blame] | 67 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 68 | |
| 69 | private: |
| 70 | // Create a new EventRecorder. Events are saved to the file filename. |
| 71 | // If the file already exists, it will be deleted before recording |
| 72 | // starts. |
| 73 | explicit EventRecorder() |
| 74 | : is_recording_(false), |
| 75 | is_playing_(false), |
[email protected] | 238a20d | 2009-01-26 16:33:29 | [diff] [blame] | 76 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 77 | journal_hook_(NULL), |
[email protected] | c7f4b627 | 2008-09-30 20:50:51 | [diff] [blame] | 78 | file_(NULL), |
[email protected] | 238a20d | 2009-01-26 16:33:29 | [diff] [blame] | 79 | #endif |
[email protected] | c7f4b627 | 2008-09-30 20:50:51 | [diff] [blame] | 80 | playback_first_msg_time_(0), |
| 81 | playback_start_time_(0) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 82 | } |
| 83 | ~EventRecorder(); |
| 84 | |
| 85 | static EventRecorder* current_; // Our singleton. |
| 86 | |
| 87 | bool is_recording_; |
| 88 | bool is_playing_; |
[email protected] | 238a20d | 2009-01-26 16:33:29 | [diff] [blame] | 89 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 90 | HHOOK journal_hook_; |
| 91 | FILE* file_; |
| 92 | EVENTMSG playback_msg_; |
[email protected] | 238a20d | 2009-01-26 16:33:29 | [diff] [blame] | 93 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 94 | int playback_first_msg_time_; |
| 95 | int playback_start_time_; |
| 96 | |
| 97 | DISALLOW_EVIL_CONSTRUCTORS(EventRecorder); |
| 98 | }; |
| 99 | |
| 100 | } // namespace base |
| 101 | |
| 102 | #endif // BASE_EVENT_RECORDER_H_ |