|
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. |
|
4 |
|
5 #ifndef BASE_EVENT_RECORDER_H_ |
|
6 #define BASE_EVENT_RECORDER_H_ |
|
7 |
|
8 #include <string> |
|
9 #if defined(OS_WIN) |
|
10 #include <windows.h> |
|
11 #endif |
|
12 #include "base/basictypes.h" |
|
13 |
|
14 class FilePath; |
|
15 |
|
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. |
|
44 bool StartRecording(const FilePath& filename); |
|
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. |
|
54 bool StartPlayback(const FilePath& filename); |
|
55 |
|
56 // Stops playback. |
|
57 void StopPlayback(); |
|
58 |
|
59 // Is the EventRecorder currently playing. |
|
60 bool is_playing() const { return is_playing_; } |
|
61 |
|
62 #if defined(OS_WIN) |
|
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); |
|
67 #endif |
|
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), |
|
76 #if defined(OS_WIN) |
|
77 journal_hook_(NULL), |
|
78 file_(NULL), |
|
79 #endif |
|
80 playback_first_msg_time_(0), |
|
81 playback_start_time_(0) { |
|
82 } |
|
83 ~EventRecorder(); |
|
84 |
|
85 static EventRecorder* current_; // Our singleton. |
|
86 |
|
87 bool is_recording_; |
|
88 bool is_playing_; |
|
89 #if defined(OS_WIN) |
|
90 HHOOK journal_hook_; |
|
91 FILE* file_; |
|
92 EVENTMSG playback_msg_; |
|
93 #endif |
|
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_ |