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