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