finnur@chromium.org | 1e0a8cf | 2011-02-14 18:24:36 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
| 5 | #ifndef BASE_EVENT_RECORDER_H_ |
| 6 | #define BASE_EVENT_RECORDER_H_ |
thakis@chromium.org | 01d1452 | 2010-07-27 08:08:24 +0900 | [diff] [blame] | 7 | #pragma once |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 8 | |
amanda@chromium.org | 5c44f99 | 2009-01-27 01:33:29 +0900 | [diff] [blame] | 9 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 10 | #include <windows.h> |
viettrungluu@chromium.org | 6bde7e8 | 2010-08-15 02:44:30 +0900 | [diff] [blame] | 11 | #include <stdio.h> |
amanda@chromium.org | 5c44f99 | 2009-01-27 01:33:29 +0900 | [diff] [blame] | 12 | #endif |
rvargas@google.com | c3b6518 | 2011-03-29 08:48:44 +0900 | [diff] [blame^] | 13 | |
| 14 | #include "base/base_api.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 15 | #include "base/basictypes.h" |
| 16 | |
thestig@chromium.org | 1c939cf | 2009-05-01 13:31:22 +0900 | [diff] [blame] | 17 | class FilePath; |
| 18 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 19 | namespace base { |
| 20 | |
| 21 | // A class for recording and playing back keyboard and mouse input events. |
| 22 | // |
| 23 | // Note - if you record events, and the playback with the windows in |
| 24 | // different sizes or positions, the playback will fail. When |
| 25 | // recording and playing, you should move the relevant windows |
| 26 | // to constant sizes and locations. |
| 27 | // TODO(mbelshe) For now this is a singleton. I believe that this class |
| 28 | // could be easily modified to: |
| 29 | // support two simultaneous recorders |
| 30 | // be playing back events while already recording events. |
| 31 | // Why? Imagine if the product had a "record a macro" feature. |
| 32 | // You might be recording globally, while recording or playing back |
| 33 | // a macro. I don't think two playbacks make sense. |
rvargas@google.com | c3b6518 | 2011-03-29 08:48:44 +0900 | [diff] [blame^] | 34 | class BASE_API EventRecorder { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 35 | public: |
| 36 | // Get the singleton EventRecorder. |
| 37 | // We can only handle one recorder/player at a time. |
| 38 | static EventRecorder* current() { |
| 39 | if (!current_) |
| 40 | current_ = new EventRecorder(); |
| 41 | return current_; |
| 42 | } |
| 43 | |
| 44 | // Starts recording events. |
| 45 | // Will clobber the file if it already exists. |
| 46 | // Returns true on success, or false if an error occurred. |
thestig@chromium.org | 1c939cf | 2009-05-01 13:31:22 +0900 | [diff] [blame] | 47 | bool StartRecording(const FilePath& filename); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 48 | |
| 49 | // Stops recording. |
| 50 | void StopRecording(); |
| 51 | |
| 52 | // Is the EventRecorder currently recording. |
| 53 | bool is_recording() const { return is_recording_; } |
| 54 | |
| 55 | // Plays events previously recorded. |
| 56 | // Returns true on success, or false if an error occurred. |
thestig@chromium.org | 1c939cf | 2009-05-01 13:31:22 +0900 | [diff] [blame] | 57 | bool StartPlayback(const FilePath& filename); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 58 | |
| 59 | // Stops playback. |
| 60 | void StopPlayback(); |
| 61 | |
| 62 | // Is the EventRecorder currently playing. |
| 63 | bool is_playing() const { return is_playing_; } |
| 64 | |
amanda@chromium.org | 5c44f99 | 2009-01-27 01:33:29 +0900 | [diff] [blame] | 65 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 66 | // C-style callbacks for the EventRecorder. |
| 67 | // Used for internal purposes only. |
| 68 | LRESULT RecordWndProc(int nCode, WPARAM wParam, LPARAM lParam); |
| 69 | LRESULT PlaybackWndProc(int nCode, WPARAM wParam, LPARAM lParam); |
amanda@chromium.org | 5c44f99 | 2009-01-27 01:33:29 +0900 | [diff] [blame] | 70 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 71 | |
| 72 | private: |
| 73 | // Create a new EventRecorder. Events are saved to the file filename. |
| 74 | // If the file already exists, it will be deleted before recording |
| 75 | // starts. |
| 76 | explicit EventRecorder() |
| 77 | : is_recording_(false), |
| 78 | is_playing_(false), |
amanda@chromium.org | 5c44f99 | 2009-01-27 01:33:29 +0900 | [diff] [blame] | 79 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 80 | journal_hook_(NULL), |
maruel@google.com | fc90337 | 2008-10-01 05:50:51 +0900 | [diff] [blame] | 81 | file_(NULL), |
amanda@chromium.org | 5c44f99 | 2009-01-27 01:33:29 +0900 | [diff] [blame] | 82 | #endif |
maruel@google.com | fc90337 | 2008-10-01 05:50:51 +0900 | [diff] [blame] | 83 | playback_first_msg_time_(0), |
| 84 | playback_start_time_(0) { |
finnur@chromium.org | 1e0a8cf | 2011-02-14 18:24:36 +0900 | [diff] [blame] | 85 | #if defined(OS_WIN) |
| 86 | memset(&playback_msg_, 0, sizeof(playback_msg_)); |
| 87 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 88 | } |
| 89 | ~EventRecorder(); |
| 90 | |
| 91 | static EventRecorder* current_; // Our singleton. |
| 92 | |
| 93 | bool is_recording_; |
| 94 | bool is_playing_; |
amanda@chromium.org | 5c44f99 | 2009-01-27 01:33:29 +0900 | [diff] [blame] | 95 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 96 | HHOOK journal_hook_; |
| 97 | FILE* file_; |
| 98 | EVENTMSG playback_msg_; |
amanda@chromium.org | 5c44f99 | 2009-01-27 01:33:29 +0900 | [diff] [blame] | 99 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 100 | int playback_first_msg_time_; |
| 101 | int playback_start_time_; |
| 102 | |
tfarina@chromium.org | 0aad842 | 2010-06-05 01:13:51 +0900 | [diff] [blame] | 103 | DISALLOW_COPY_AND_ASSIGN(EventRecorder); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | } // namespace base |
| 107 | |
| 108 | #endif // BASE_EVENT_RECORDER_H_ |