blob: 29ed350a6a3c2daf03a1120f4b1830cfba58dd6c [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commitd7cae122008-07-26 21:49:384
5#ifndef BASE_EVENT_RECORDER_H_
6#define BASE_EVENT_RECORDER_H_
7
8#include <string>
[email protected]238a20d2009-01-26 16:33:299#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3810#include <windows.h>
[email protected]238a20d2009-01-26 16:33:2911#endif
initial.commitd7cae122008-07-26 21:49:3812#include "base/basictypes.h"
13
[email protected]4f093932009-05-01 04:31:2214class FilePath;
15
initial.commitd7cae122008-07-26 21:49:3816namespace 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.
31class 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]4f093932009-05-01 04:31:2244 bool StartRecording(const FilePath& filename);
initial.commitd7cae122008-07-26 21:49:3845
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]4f093932009-05-01 04:31:2254 bool StartPlayback(const FilePath& filename);
initial.commitd7cae122008-07-26 21:49:3855
56 // Stops playback.
57 void StopPlayback();
58
59 // Is the EventRecorder currently playing.
60 bool is_playing() const { return is_playing_; }
61
[email protected]238a20d2009-01-26 16:33:2962#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3863 // 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]238a20d2009-01-26 16:33:2967#endif
initial.commitd7cae122008-07-26 21:49:3868
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]238a20d2009-01-26 16:33:2976#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3877 journal_hook_(NULL),
[email protected]c7f4b6272008-09-30 20:50:5178 file_(NULL),
[email protected]238a20d2009-01-26 16:33:2979#endif
[email protected]c7f4b6272008-09-30 20:50:5180 playback_first_msg_time_(0),
81 playback_start_time_(0) {
initial.commitd7cae122008-07-26 21:49:3882 }
83 ~EventRecorder();
84
85 static EventRecorder* current_; // Our singleton.
86
87 bool is_recording_;
88 bool is_playing_;
[email protected]238a20d2009-01-26 16:33:2989#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3890 HHOOK journal_hook_;
91 FILE* file_;
92 EVENTMSG playback_msg_;
[email protected]238a20d2009-01-26 16:33:2993#endif
initial.commitd7cae122008-07-26 21:49:3894 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_