forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromptService.js
353 lines (274 loc) · 12.6 KB
/
promptService.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Spoof the prompt service. Interesting thread on mozillazine:
// http://www.mail-archive.com/[email protected]/msg00193.html
goog.require('fxdriver.logging');
goog.require('fxdriver.modals');
goog.require('fxdriver.moz');
goog.require('goog.array');
var EXCLUDED_NAMES = [
'QueryInterface',
'alert',
'alertCheck',
'confirm',
'confirmCheck',
'confirmEx',
'getPrompt',
'prompt',
'promptPassword',
'promptUsernameAndPassword',
'select'
];
function addInterfaces(to, delegate, interfaces) {
goog.array.forEach(interfaces, function(iid) {
try {
var queried = delegate.QueryInterface(iid);
for (var i in queried) {
if (!goog.array.contains(EXCLUDED_NAMES, i.toString())) {
to[i] = queried[i];
}
}
} catch (ignored) {}
});
}
// Implementation of nsIPrompt
function ObservingAlert(parentWindow, delegate) {
this.parentWindow_ = parentWindow;
var interfaces = [CI.nsIPrompt, CI.nsIAuthPrompt, CI.nsIAuthPrompt2, CI.nsIWritablePropertyBag2];
addInterfaces(this, delegate, interfaces);
this.delegate_ = delegate;
this.QueryInterface = fxdriver.moz.queryInterface(this, interfaces);
}
ObservingAlert.prototype.alert = function(dialogTitle, text) {
fxdriver.modals.signalOpenModal(this.parentWindow_, text);
this.delegate_.alert(dialogTitle, text);
};
ObservingAlert.prototype.alertCheck = function(dialogTitle, text, checkMsg, checkValue) {
fxdriver.modals.signalOpenModal(this.parentWindow_, text);
this.delegate_.alertCheck(dialogTitle, text, checkMsg, checkValue);
};
ObservingAlert.prototype.confirm = function(dialogTitle, text) {
fxdriver.modals.signalOpenModal(this.parentWindow_, text);
return this.delegate_.confirm(dialogTitle, text);
};
ObservingAlert.prototype.confirmCheck = function(dialogTitle, text, checkMsg, checkValue) {
fxdriver.modals.signalOpenModal(this.parentWindow_, text);
return this.delegate_.confirmCheck(dialogTitle, text, checkMsg, checkValue);
};
ObservingAlert.prototype.confirmEx = function(dialogTitle, text,
buttonFlags, button0Title, button1Title, button2Title,
checkMsg, checkValue) {
fxdriver.modals.signalOpenModal(this.parentWindow_, text);
return this.delegate_.confirmEx(dialogTitle, text,
buttonFlags, button0Title, button1Title, button2Title,
checkMsg, checkValue);
};
ObservingAlert.prototype.prompt = function(dialogTitle, text, value, checkMsg, checkValue) {
fxdriver.modals.signalOpenModal(this.parentWindow_, text);
return this.delegate_.prompt(dialogTitle, text, value, checkMsg, checkValue);
};
ObservingAlert.prototype.promptPassword = function(dialogTitle, text, password, checkMsg, checkValue) {
fxdriver.modals.signalOpenModal(this.parentWindow_, text);
return this.delegate_.promptPassword(dialogTitle, text, password, checkMsg, checkValue);
};
ObservingAlert.prototype.promptUsernameAndPassword = function(dialogTitle, text, username, password, checkMsg, checkValue) {
fxdriver.modals.signalOpenModal(this.parentWindow_, text);
return this.delegate_.promptUsernameAndPassword(dialogTitle, text, username, password, checkMsg, checkValue);
};
ObservingAlert.prototype.select = function(dialogTitle, text, count, selectList, outSelection) {
fxdriver.modals.signalOpenModal(this.parentWindow_, text);
return this.delegate_.select(dialogTitle, text, count, selectList, outSelection);
};
// Spoof implementation
function DrivenPromptService() {
fxdriver.logging.info('Spoofing prompt service');
// @mozilla.org/prompter;1
var prompters = [
'{1c978d25-b37f-43a8-a2d6-0c7a239ead87}' // nsPrompter.js: Firefox 4 late betas onwards
];
// @mozilla.org/embedcomp/prompt-service;
var promptServices = [
'{7ad1b327-6dfa-46ec-9234-f2a620ea7e00}', // nsPrompter.js: Firefox 4 betas
'{A2112D6A-0E28-421f-B46A-25C0B308CBD0}' // nsPromptService.h: Firefox 3.x
];
var findImplementation = function(interfaceName, cids) {
for (var i = 0; i < cids.length; i++) {
var impl = Components.classesByID[cids[i]];
if (!impl) { continue; }
var service = impl.getService();
if (!service) { continue; }
try {
var toReturn = service.QueryInterface(interfaceName);
fxdriver.logging.info('Found implementation at: ' + cids[i]);
return toReturn;
} catch (ignored) {}
}
return null;
};
// Keep a reference to the original service. Check for the most recent version of Firefox first
var originalPromptService_ = findImplementation(CI.nsIPromptService2, promptServices);
var originalPrompter_ = findImplementation(CI.nsIPromptFactory, prompters);
if (!originalPromptService_) {
fxdriver.logging.info('Unable to locate original prompt service');
}
if (!originalPrompter_) {
fxdriver.logging.info('Unable to locate original prompter');
}
this.delegate_ = originalPrompter_ ? originalPrompter_ : originalPromptService_;
var interfaces = [CI.nsIPromptFactory, CI.nsIPromptService, CI.nsIPromptService2];
addInterfaces(this, this.delegate_, interfaces);
this.QueryInterface = fxdriver.moz.queryInterface(this,
[CI.nsIPromptFactory, CI.nsIPromptService, CI.nsIPromptService2]);
fxdriver.logging.info('Finished initializing spoofed prompt service');
}
// Constants from nsIPromtService.idl
DrivenPromptService.prototype = {
BUTTON_POS_0: 1,
BUTTON_POS_1: 256,
BUTTON_POS_2: 65536,
// Button Title Flags (used to set the labels of buttons in the prompt)
BUTTON_TITLE_OK: 1,
BUTTON_TITLE_CANCEL: 2,
BUTTON_TITLE_YES: 3,
BUTTON_TITLE_NO: 4,
BUTTON_TITLE_SAVE: 5,
BUTTON_TITLE_DONT_SAVE: 6,
BUTTON_TITLE_REVERT: 7,
BUTTON_TITLE_IS_STRING: 127,
// Button Default Flags (used to select which button is the default one)
BUTTON_POS_0_DEFAULT: 0,
BUTTON_POS_1_DEFAULT: 16777216,
BUTTON_POS_2_DEFAULT: 33554432,
// Causes the buttons to be initially disabled. They are enabled after a
// timeout expires. The implementation may interpret this loosely as the
// intent is to ensure that the user does not click through a security dialog
// too quickly. Strictly speaking, the implementation could choose to ignore
// this flag.
BUTTON_DELAY_ENABLE: 67108864,
// Selects the standard set of OK/Cancel buttons.
STD_OK_CANCEL_BUTTONS: (this.BUTTON_TITLE_OK * this.BUTTON_POS_0) + (this.BUTTON_TITLE_CANCEL
* this.BUTTON_POS_1),
// Selects the standard set of Yes/No buttons.
STD_YES_NO_BUTTONS: (this.BUTTON_TITLE_YES * this.BUTTON_POS_0) + (this.BUTTON_TITLE_NO
* this.BUTTON_POS_1)
};
DrivenPromptService.prototype.alert = function(aParent, aDialogTitle, aText) {
fxdriver.modals.signalOpenModal(aParent, aText);
var service = this.delegate_.QueryInterface(CI.nsIPromptService2);
return service.alert(aParent, aDialogTitle, aText);
};
DrivenPromptService.prototype.alertCheck =
function(aParent, aDialogTitle, aText, aCheckMsg, aCheckState) {
fxdriver.modals.signalOpenModal(aParent, aText);
var service = this.delegate_.QueryInterface(CI.nsIPromptService2);
return service.alertCheck(aParent, aDialogTitle, aText, aCheckMsg, aCheckState);
};
DrivenPromptService.prototype.confirm = function(aParent, aDialogTitle, aText) {
fxdriver.modals.signalOpenModal(aParent, aText);
var service = this.delegate_.QueryInterface(CI.nsIPromptService2);
return service.confirm(aParent, aDialogTitle, aText);
};
DrivenPromptService.prototype.confirmCheck =
function(aParent, aDialogTitle, aText, aCheckMsg, aCheckState) {
fxdriver.modals.signalOpenModal(aParent, aText);
var service = this.delegate_.QueryInterface(CI.nsIPromptService2);
return service.confirmCheck(aParent, aDialogTitle, aText, aCheckMsg, aCheckState);
};
DrivenPromptService.prototype.confirmEx =
function(aParent, aDialogTitle, aText, aButtonFlags, aButton0Title, aButton1Title, aButton2Title, aCheckMsg, aCheckState) {
fxdriver.modals.signalOpenModal(aParent, aText);
var service = this.delegate_.QueryInterface(CI.nsIPromptService2);
return service.confirmEx(aParent, aDialogTitle, aText, aButtonFlags, aButton0Title, aButton1Title, aButton2Title, aCheckMsg, aCheckState);
};
DrivenPromptService.prototype.prompt =
function(aParent, aDialogTitle, aText, aValue, aCheckMsg, aCheckState) {
fxdriver.modals.signalOpenModal(aParent, aText);
var service = this.delegate_.QueryInterface(CI.nsIPromptService2);
return service.prompt(aParent, aDialogTitle, aText, aValue, aCheckMsg, aCheckState);
};
DrivenPromptService.prototype.promptUsernameAndPassword =
function(aParent, aDialogTitle, aText, aUsername, aPassword, aCheckMsg, aCheckState) {
var service = this.delegate_.QueryInterface(CI.nsIPromptService2);
return service.promptUsernameAndPassword(aParent, aDialogTitle, aText, aUsername, aPassword, aCheckMsg, aCheckState);
};
DrivenPromptService.prototype.promptPassword =
function(aParent, aDialogTitle, aText, aPassword, aCheckMsg, aCheckState) {
var service = this.delegate_.QueryInterface(CI.nsIPromptService2);
return service.promptPassword(aParent, aDialogTitle, aText, aPassword, aCheckMsg, aCheckState);
};
DrivenPromptService.prototype.select =
function(aParent, aDialogTitle, aText, aCount, aSelectList, aOutSelection) {
var service = this.delegate_.QueryInterface(CI.nsIPromptService2);
return service.select(aParent, aDialogTitle, aText, aCount, aSelectList, aOutSelection);
};
// nsIPromptService2 functions
DrivenPromptService.prototype.promptAuth = function(aParent, aChannel, level, authInfo, checkboxLabel, checkValue) {
fxdriver.modals.signalOpenModal(aParent, '');
var service = this.delegate_.QueryInterface(CI.nsIPromptService2);
return service.promptAuth(aParent, aChannel, level, authInfo, checkboxLabel, checkValue);
};
DrivenPromptService.prototype.asyncPromptAuth = function(aParent, aChannel, aCallback, aContext, level, authInfo, checkboxLabel,checkValue) {
fxdriver.modals.signalOpenModal(aParent, '');
var service = this.delegate_.QueryInterface(CI.nsIPromptService2);
return service.asyncPromptAuth(aParent, aChannel, aCallback, aContext, level, authInfo, checkboxLabel, checkValue);
};
// nsIPromptFactory
DrivenPromptService.prototype.getPrompt = function(domWin, iid) {
var factory = this.delegate_.QueryInterface(CI.nsIPromptFactory);
var rawPrompt = factory.getPrompt(domWin, iid);
return new ObservingAlert(domWin, rawPrompt);
};
// nsIObserver
DrivenPromptService.prototype.observe = function(aSubject, aTopic, aData) {
return this.delegate_.observe(aSubject, aTopic, aData);
};
/** @const */ var PROMPT_SERVICE_CONTRACT_ID = '@mozilla.org/embedcomp/prompt-service;1';
/** @const */ var PROMPTER_CONTRACT_ID = '@mozilla.org/prompter;1';
// This is defined by us
/** @const */ var DRIVEN_PROMPT_SERVICE_CLASS_ID = Components.ID('{e26dbdcd-d3ba-4ded-88c3-6cb07ee3e9e0}');
var service = undefined;
var PromptServiceSpoofFactory = {
createInstance: function(aOuter, aIID) {
if (aOuter != null)
throw Components.results.NS_ERROR_NO_AGGREGATION;
if (service == undefined) {
service = new DrivenPromptService();
}
return service;
}
};
function PromptServiceSpoofModule() {
this.firstTime_ = true;
}
PromptServiceSpoofModule.prototype.registerSelf = function(aCompMgr, aFileSpec, aLocation, aType) {
if (this.firstTime_) {
this.firstTime_ = false;
throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
}
aCompMgr = aCompMgr.QueryInterface(CI.nsIComponentRegistrar);
aCompMgr.registerFactoryLocation(
DRIVEN_PROMPT_SERVICE_CLASS_ID, 'Driven prompt service', PROMPT_SERVICE_CONTRACT_ID, aFileSpec, aLocation, aType);
aCompMgr.registerFactoryLocation(
DRIVEN_PROMPT_SERVICE_CLASS_ID, 'Driven prompter service', PROMPTER_CONTRACT_ID, aFileSpec, aLocation, aType);
};
PromptServiceSpoofModule.prototype.unregisterSelf = function(aCompMgr, aLocation, aType) {
fxdriver.logging.info('Unregistering\n');
aCompMgr.QueryInterface(CI.nsIComponentRegistrar);
aCompMgr.unregisterFactoryLocation(DRIVEN_PROMPT_SERVICE_CLASS_ID, aLocation);
};
PromptServiceSpoofModule.prototype.getClassObject = function(aCompMgr, aCID, aIID) {
if (!aIID.equals(Components.interfaces.nsIFactory))
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
if (aCID.equals(DRIVEN_PROMPT_SERVICE_CLASS_ID))
return PromptServiceSpoofFactory;
throw Components.results.NS_ERROR_NO_INTERFACE;
};
PromptServiceSpoofModule.prototype.canUnload = function(aCompMgr) {
return true;
};
NSGetModule = function(comMgr, fileSpec) {
return new PromptServiceSpoofModule();
};
DrivenPromptService.prototype.classID = DRIVEN_PROMPT_SERVICE_CLASS_ID;
fxdriver.moz.load('resource://gre/modules/XPCOMUtils.jsm');
if (XPCOMUtils.generateNSGetFactory) {
/** @const */ NSGetFactory = XPCOMUtils.generateNSGetFactory([DrivenPromptService]);
}