summaryrefslogtreecommitdiffstats
path: root/dom/plugins/test/mochitest/dialog_watcher.js
blob: 8a8c829974c006635e542836d964d6dab5fc2cc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const EVENT_OBJECT_SHOW = 0x8002;
const EVENT_OBJECT_HIDE = 0x8003;
const WINEVENT_OUTOFCONTEXT = 0;
const WINEVENT_SKIPOWNPROCESS = 2;
const QS_ALLINPUT = 0x04FF;
const INFINITE = 0xFFFFFFFF;
const WAIT_OBJECT_0 = 0;
const WAIT_TIMEOUT = 258;
const PM_NOREMOVE = 0;

function DialogWatcher(titleText, onDialogStart, onDialogEnd) {
  this.titleText = titleText;
  this.onDialogStart = onDialogStart;
  this.onDialogEnd = onDialogEnd;
}

DialogWatcher.prototype.init = function() {
  this.hwnd = undefined;
  if (!this.user32) {
    this.user32 = ctypes.open("user32.dll");
  }
  if (!this.findWindow) {
    this.findWindow = user32.declare("FindWindowW",
                                     ctypes.winapi_abi,
                                     ctypes.uintptr_t,
                                     ctypes.char16_t.ptr,
                                     ctypes.char16_t.ptr);
  }
  if (!this.winEventProcType) {
    this.winEventProcType = ctypes.FunctionType(ctypes.stdcall_abi,
                                                ctypes.void_t,
                                                [ctypes.uintptr_t,
                                                ctypes.uint32_t,
                                                ctypes.uintptr_t,
                                                ctypes.long,
                                                ctypes.long,
                                                ctypes.uint32_t,
                                                ctypes.uint32_t]).ptr;
  }
  if (!this.setWinEventHook) {
    this.setWinEventHook = user32.declare("SetWinEventHook",
                                          ctypes.winapi_abi,
                                          ctypes.uintptr_t,
                                          ctypes.uint32_t,
                                          ctypes.uint32_t,
                                          ctypes.uintptr_t,
                                          this.winEventProcType,
                                          ctypes.uint32_t,
                                          ctypes.uint32_t,
                                          ctypes.uint32_t);
  }
  if (!this.unhookWinEvent) {
    this.unhookWinEvent = user32.declare("UnhookWinEvent",
                                         ctypes.winapi_abi,
                                         ctypes.int,
                                         ctypes.uintptr_t);
  }
  if (!this.pointType) {
    this.pointType = ctypes.StructType("tagPOINT",
                                       [ { "x": ctypes.long },
                                         { "y": ctypes.long } ] );
  }
  if (!this.msgType) {
    this.msgType = ctypes.StructType("tagMSG",
                                     [ { "hwnd": ctypes.uintptr_t },
                                       { "message": ctypes.uint32_t },
                                       { "wParam": ctypes.uintptr_t },
                                       { "lParam": ctypes.intptr_t },
                                       { "time": ctypes.uint32_t },
                                       { "pt": this.pointType } ] );
  }
  if (!this.peekMessage) {
    this.peekMessage = user32.declare("PeekMessageW",
                                      ctypes.winapi_abi,
                                      ctypes.int,
                                      this.msgType.ptr,
                                      ctypes.uintptr_t,
                                      ctypes.uint32_t,
                                      ctypes.uint32_t,
                                      ctypes.uint32_t);
  }
  if (!this.msgWaitForMultipleObjects) {
    this.msgWaitForMultipleObjects = user32.declare("MsgWaitForMultipleObjects",
                                                    ctypes.winapi_abi,
                                                    ctypes.uint32_t,
                                                    ctypes.uint32_t,
                                                    ctypes.uintptr_t.ptr,
                                                    ctypes.int,
                                                    ctypes.uint32_t,
                                                    ctypes.uint32_t);
  }
  if (!this.getWindowTextW) {
    this.getWindowTextW = user32.declare("GetWindowTextW",
                                         ctypes.winapi_abi,
                                         ctypes.int,
                                         ctypes.uintptr_t,
                                         ctypes.char16_t.ptr,
                                         ctypes.int);
  }
  if (!this.messageBox) {
    // Handy for debugging this code
    this.messageBox = user32.declare("MessageBoxW",
                                     ctypes.winapi_abi,
                                     ctypes.int,
                                     ctypes.uintptr_t,
                                     ctypes.char16_t.ptr,
                                     ctypes.char16_t.ptr,
                                     ctypes.uint32_t);
  }
};

DialogWatcher.prototype.getWindowText = function(hwnd) {
  var bufType = ctypes.ArrayType(ctypes.char16_t);
  var buffer = new bufType(256);

  if (this.getWindowTextW(hwnd, buffer, buffer.length)) {
    return buffer.readString();
  }
};

DialogWatcher.prototype.processWindowEvents = function(timeout) {
  var onWinEvent = function(self, hook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime) {
    var nhwnd = Number(hwnd)
    if (event == EVENT_OBJECT_SHOW) {
      if (nhwnd == self.hwnd) {
        // We've already picked up this event via FindWindow
        return;
      }
      var windowText = self.getWindowText(hwnd);
      if (windowText == self.titleText && self.onDialogStart) {
        self.hwnd = nhwnd;
        self.onDialogStart(nhwnd);
      }
    } else if (event == EVENT_OBJECT_HIDE && nhwnd == self.hwnd && self.onDialogEnd) {
      self.onDialogEnd();
      self.hwnd = null;
    }
  };
  var self = this;
  var callback = this.winEventProcType(function(hook, event, hwnd, idObject,
                                                idChild, dwEventThread,
                                                dwmsEventTime) {
      onWinEvent(self, hook, event, hwnd, idObject, idChild, dwEventThread,
                 dwmsEventTime);
    } );
  var hook = this.setWinEventHook(EVENT_OBJECT_SHOW, EVENT_OBJECT_HIDE,
                                  0, callback, 0, 0,
                                  WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
  if (!hook) {
    return;
  }
  // Check if the window is already showing
  var hwnd = this.findWindow(null, this.titleText);
  if (hwnd && hwnd > 0) {
    this.hwnd = Number(hwnd);
    if (this.onDialogStart) {
      this.onDialogStart(this.hwnd);
    }
  }

  if (!timeout) {
    timeout = INFINITE;
  }

  var waitStatus = WAIT_OBJECT_0;
  var expectingStart = this.onDialogStart && this.hwnd === undefined;
  var startWaitTime = Date.now();
  while (this.hwnd === undefined || this.onDialogEnd && this.hwnd) {
    waitStatus = this.msgWaitForMultipleObjects(0, null, 0, expectingStart ?
                                                            INFINITE : timeout, 0);
    if (waitStatus == WAIT_OBJECT_0) {
      var msg = new this.msgType;
      this.peekMessage(msg.address(), 0, 0, 0, PM_NOREMOVE);
    }
    if (waitStatus == WAIT_TIMEOUT || (Date.now() - startWaitTime) >= timeout) {
      break;
    }
  }

  this.unhookWinEvent(hook);
  // Returns true if the hook was successful, something was found, and we never timed out
  return this.hwnd !== undefined && waitStatus == WAIT_OBJECT_0;
};