comparison dwt/browser/PromptService2.d @ 278:93409d9838c5

Commit more browser/xpcom updates, including still uncoverted source.
author John Reimer<terminal.node@gmail.com>
date Thu, 31 Jul 2008 19:17:51 -0700
parents
children eec6ddb07873
comparison
equal deleted inserted replaced
277:687f261028b8 278:93409d9838c5
1 /*******************************************************************************
2 * Copyright (c) 2003, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 module dwt.browser.PromptService2;
12
13 import dwt.dwthelper.utils;
14
15 import dwt.DWT;
16 import dwt.internal.C;
17 import dwt.internal.Compatibility;
18 import dwt.internal.mozilla.XPCOM;
19 import dwt.internal.mozilla.XPCOMObject;
20 import dwt.internal.mozilla.nsEmbedString;
21 import dwt.internal.mozilla.nsIAuthInformation;
22 import dwt.internal.mozilla.nsIChannel;
23 import dwt.internal.mozilla.nsID;
24 import dwt.internal.mozilla.nsIDOMWindow;
25 import dwt.internal.mozilla.nsIEmbeddingSiteWindow;
26 import dwt.internal.mozilla.nsIMemory;
27 import dwt.internal.mozilla.nsIPromptService;
28 import dwt.internal.mozilla.nsIPromptService2;
29 import dwt.internal.mozilla.nsIServiceManager;
30 import dwt.internal.mozilla.nsISupports;
31 import dwt.internal.mozilla.nsIURI;
32 import dwt.internal.mozilla.nsIWebBrowserChrome;
33 import dwt.internal.mozilla.nsIWindowWatcher;
34 import dwt.widgets.MessageBox;
35 import dwt.widgets.Shell;
36
37 class PromptService2 {
38 XPCOMObject supports;
39 XPCOMObject promptService;
40 XPCOMObject promptService2;
41 int refCount = 0;
42
43 PromptService2 () {
44 createCOMInterfaces ();
45 }
46
47 int AddRef () {
48 refCount++;
49 return refCount;
50 }
51
52 void createCOMInterfaces () {
53 /* Create each of the interfaces that this object implements */
54 supports = new XPCOMObject (new int[] {2, 0, 0}) {
55 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
56 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
57 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
58 };
59
60 promptService = new XPCOMObject (new int[] {2, 0, 0, 3, 5, 4, 6, 10, 7, 8, 7, 7}) {
61 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
62 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
63 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
64 public int /*long*/ method3 (int /*long*/[] args) {return Alert (args[0], args[1], args[2]);}
65 public int /*long*/ method4 (int /*long*/[] args) {return AlertCheck (args[0], args[1], args[2], args[3], args[4]);}
66 public int /*long*/ method5 (int /*long*/[] args) {return Confirm (args[0], args[1], args[2], args[3]);}
67 public int /*long*/ method6 (int /*long*/[] args) {return ConfirmCheck (args[0], args[1], args[2], args[3], args[4], args[5]);}
68 public int /*long*/ method7 (int /*long*/[] args) {return ConfirmEx (args[0], args[1], args[2], (int)/*64*/args[3], args[4], args[5], args[6], args[7], args[8], args[9]);}
69 public int /*long*/ method8 (int /*long*/[] args) {return Prompt (args[0], args[1], args[2], args[3], args[4], args[5], args[6]);}
70 public int /*long*/ method9 (int /*long*/[] args) {return PromptUsernameAndPassword (args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);}
71 public int /*long*/ method10 (int /*long*/[] args) {return PromptPassword (args[0], args[1], args[2], args[3], args[4], args[5], args[6]);}
72 public int /*long*/ method11 (int /*long*/[] args) {return Select (args[0], args[1], args[2], (int)/*64*/args[3], args[4], args[5], args[6]);}
73 };
74
75 promptService2 = new XPCOMObject (new int[] {2, 0, 0, 3, 5, 4, 6, 10, 7, 8, 7, 7, 7, 9}) {
76 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
77 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
78 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
79 public int /*long*/ method3 (int /*long*/[] args) {return Alert (args[0], args[1], args[2]);}
80 public int /*long*/ method4 (int /*long*/[] args) {return AlertCheck (args[0], args[1], args[2], args[3], args[4]);}
81 public int /*long*/ method5 (int /*long*/[] args) {return Confirm (args[0], args[1], args[2], args[3]);}
82 public int /*long*/ method6 (int /*long*/[] args) {return ConfirmCheck (args[0], args[1], args[2], args[3], args[4], args[5]);}
83 public int /*long*/ method7 (int /*long*/[] args) {return ConfirmEx (args[0], args[1], args[2], (int)/*64*/args[3], args[4], args[5], args[6], args[7], args[8], args[9]);}
84 public int /*long*/ method8 (int /*long*/[] args) {return Prompt (args[0], args[1], args[2], args[3], args[4], args[5], args[6]);}
85 public int /*long*/ method9 (int /*long*/[] args) {return PromptUsernameAndPassword (args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);}
86 public int /*long*/ method10 (int /*long*/[] args) {return PromptPassword (args[0], args[1], args[2], args[3], args[4], args[5], args[6]);}
87 public int /*long*/ method11 (int /*long*/[] args) {return Select (args[0], args[1], args[2], (int)/*64*/args[3], args[4], args[5], args[6]);}
88 public int /*long*/ method12 (int /*long*/[] args) {return PromptAuth (args[0], args[1], (int)/*64*/args[2], args[3], args[4], args[5], args[6]);}
89 public int /*long*/ method13 (int /*long*/[] args) {return AsyncPromptAuth (args[0], args[1], args[2], args[3], (int)/*64*/args[4], args[5], args[6], args[7], args[8]);}
90 };
91 }
92
93 void disposeCOMInterfaces () {
94 if (supports !is null) {
95 supports.dispose ();
96 supports = null;
97 }
98 if (promptService !is null) {
99 promptService.dispose ();
100 promptService = null;
101 }
102 if (promptService2 !is null) {
103 promptService2.dispose ();
104 promptService2 = null;
105 }
106 }
107
108 int /*long*/ getAddress () {
109 return promptService2.getAddress ();
110 }
111
112 int QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
113 if (riid is 0 || ppvObject is 0) return XPCOM.NS_ERROR_NO_INTERFACE;
114 nsID guid = new nsID ();
115 XPCOM.memmove (guid, riid, nsID.sizeof);
116
117 if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
118 XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF);
119 AddRef ();
120 return XPCOM.NS_OK;
121 }
122 if (guid.Equals (nsIPromptService.NS_IPROMPTSERVICE_IID)) {
123 XPCOM.memmove (ppvObject, new int /*long*/[] {promptService.getAddress ()}, C.PTR_SIZEOF);
124 AddRef ();
125 return XPCOM.NS_OK;
126 }
127 if (guid.Equals (nsIPromptService2.NS_IPROMPTSERVICE2_IID)) {
128 XPCOM.memmove (ppvObject, new int /*long*/[] {promptService2.getAddress ()}, C.PTR_SIZEOF);
129 AddRef ();
130 return XPCOM.NS_OK;
131 }
132
133 XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
134 return XPCOM.NS_ERROR_NO_INTERFACE;
135 }
136
137 int Release () {
138 refCount--;
139 if (refCount is 0) disposeCOMInterfaces ();
140 return refCount;
141 }
142
143 Browser getBrowser (int /*long*/ aDOMWindow) {
144 if (aDOMWindow is 0) return null;
145
146 int /*long*/[] result = new int /*long*/[1];
147 int rc = XPCOM.NS_GetServiceManager (result);
148 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
149 if (result[0] is 0) Mozilla.error (XPCOM.NS_NOINTERFACE);
150
151 nsIServiceManager serviceManager = new nsIServiceManager (result[0]);
152 result[0] = 0;
153 byte[] aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_WINDOWWATCHER_CONTRACTID, true);
154 rc = serviceManager.GetServiceByContractID (aContractID, nsIWindowWatcher.NS_IWINDOWWATCHER_IID, result);
155 if (rc !is XPCOM.NS_OK) Mozilla.error(rc);
156 if (result[0] is 0) Mozilla.error (XPCOM.NS_NOINTERFACE);
157 serviceManager.Release ();
158
159 nsIWindowWatcher windowWatcher = new nsIWindowWatcher (result[0]);
160 result[0] = 0;
161 /* the chrome will only be answered for the top-level nsIDOMWindow */
162 nsIDOMWindow window = new nsIDOMWindow (aDOMWindow);
163 rc = window.GetTop (result);
164 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
165 if (result[0] is 0) Mozilla.error (XPCOM.NS_NOINTERFACE);
166 aDOMWindow = result[0];
167 result[0] = 0;
168 rc = windowWatcher.GetChromeForWindow (aDOMWindow, result);
169 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
170 if (result[0] is 0) Mozilla.error (XPCOM.NS_NOINTERFACE);
171 windowWatcher.Release ();
172
173 nsIWebBrowserChrome webBrowserChrome = new nsIWebBrowserChrome (result[0]);
174 result[0] = 0;
175 rc = webBrowserChrome.QueryInterface (nsIEmbeddingSiteWindow.NS_IEMBEDDINGSITEWINDOW_IID, result);
176 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
177 if (result[0] is 0) Mozilla.error (XPCOM.NS_NOINTERFACE);
178 webBrowserChrome.Release ();
179
180 nsIEmbeddingSiteWindow embeddingSiteWindow = new nsIEmbeddingSiteWindow (result[0]);
181 result[0] = 0;
182 rc = embeddingSiteWindow.GetSiteWindow (result);
183 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
184 if (result[0] is 0) Mozilla.error (XPCOM.NS_NOINTERFACE);
185 embeddingSiteWindow.Release ();
186
187 return Mozilla.findBrowser (result[0]);
188 }
189
190 String getLabel (int buttonFlag, int index, int /*long*/ buttonTitle) {
191 String label = null;
192 int flag = (buttonFlag & (0xff * index)) / index;
193 switch (flag) {
194 case nsIPromptService.BUTTON_TITLE_CANCEL : label = DWT.getMessage ("SWT_Cancel"); break; //$NON-NLS-1$
195 case nsIPromptService.BUTTON_TITLE_NO : label = DWT.getMessage ("SWT_No"); break; //$NON-NLS-1$
196 case nsIPromptService.BUTTON_TITLE_OK : label = DWT.getMessage ("SWT_OK"); break; //$NON-NLS-1$
197 case nsIPromptService.BUTTON_TITLE_SAVE : label = DWT.getMessage ("SWT_Save"); break; //$NON-NLS-1$
198 case nsIPromptService.BUTTON_TITLE_YES : label = DWT.getMessage ("SWT_Yes"); break; //$NON-NLS-1$
199 case nsIPromptService.BUTTON_TITLE_IS_STRING : {
200 int length = XPCOM.strlen_PRUnichar (buttonTitle);
201 char[] dest = new char[length];
202 XPCOM.memmove (dest, buttonTitle, length * 2);
203 label = new String (dest);
204 }
205 }
206 return label;
207 }
208
209 /* nsIPromptService */
210
211 int Alert (int /*long*/ aParent, int /*long*/ aDialogTitle, int /*long*/ aText) {
212 Browser browser = getBrowser (aParent);
213
214 int length = XPCOM.strlen_PRUnichar (aDialogTitle);
215 char[] dest = new char[length];
216 XPCOM.memmove (dest, aDialogTitle, length * 2);
217 String titleLabel = new String (dest);
218
219 length = XPCOM.strlen_PRUnichar (aText);
220 dest = new char[length];
221 XPCOM.memmove (dest, aText, length * 2);
222 String textLabel = new String (dest);
223
224 Shell shell = browser is null ? new Shell () : browser.getShell ();
225 MessageBox messageBox = new MessageBox (shell, DWT.OK | DWT.ICON_WARNING);
226 messageBox.setText (titleLabel);
227 messageBox.setMessage (textLabel);
228 messageBox.open ();
229 return XPCOM.NS_OK;
230 }
231
232 int AlertCheck (int /*long*/ aParent, int /*long*/ aDialogTitle, int /*long*/ aText, int /*long*/ aCheckMsg, int /*long*/ aCheckState) {
233 Browser browser = getBrowser (aParent);
234
235 int length = XPCOM.strlen_PRUnichar (aDialogTitle);
236 char[] dest = new char[length];
237 XPCOM.memmove (dest, aDialogTitle, length * 2);
238 String titleLabel = new String (dest);
239
240 length = XPCOM.strlen_PRUnichar (aText);
241 dest = new char[length];
242 XPCOM.memmove (dest, aText, length * 2);
243 String textLabel = new String (dest);
244
245 length = XPCOM.strlen_PRUnichar (aCheckMsg);
246 dest = new char[length];
247 XPCOM.memmove (dest, aCheckMsg, length * 2);
248 String checkLabel = new String (dest);
249
250 Shell shell = browser is null ? new Shell () : browser.getShell ();
251 PromptDialog dialog = new PromptDialog (shell);
252 int[] check = new int[1];
253 if (aCheckState !is 0) XPCOM.memmove (check, aCheckState, 4); /* PRBool */
254 dialog.alertCheck (titleLabel, textLabel, checkLabel, check);
255 if (aCheckState !is 0) XPCOM.memmove (aCheckState, check, 4); /* PRBool */
256 return XPCOM.NS_OK;
257 }
258
259 int AsyncPromptAuth(int /*long*/ aParent, int /*long*/ aChannel, int /*long*/ aCallback, int /*long*/ aContext, int level, int /*long*/ authInfo, int /*long*/ checkboxLabel, int /*long*/ checkValue, int /*long*/ _retval) {
260 return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
261 }
262
263 int Confirm (int /*long*/ aParent, int /*long*/ aDialogTitle, int /*long*/ aText, int /*long*/ _retval) {
264 Browser browser = getBrowser (aParent);
265
266 int length = XPCOM.strlen_PRUnichar (aDialogTitle);
267 char[] dest = new char[length];
268 XPCOM.memmove (dest, aDialogTitle, length * 2);
269 String titleLabel = new String (dest);
270
271 length = XPCOM.strlen_PRUnichar (aText);
272 dest = new char[length];
273 XPCOM.memmove (dest, aText, length * 2);
274 String textLabel = new String (dest);
275
276 Shell shell = browser is null ? new Shell () : browser.getShell ();
277 MessageBox messageBox = new MessageBox (shell, DWT.OK | DWT.CANCEL | DWT.ICON_QUESTION);
278 messageBox.setText (titleLabel);
279 messageBox.setMessage (textLabel);
280 int id = messageBox.open ();
281 int[] result = {id is DWT.OK ? 1 : 0};
282 XPCOM.memmove (_retval, result, 4);
283 return XPCOM.NS_OK;
284 }
285
286 int ConfirmCheck (int /*long*/ aParent, int /*long*/ aDialogTitle, int /*long*/ aText, int /*long*/ aCheckMsg, int /*long*/ aCheckState, int /*long*/ _retval) {
287 return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
288 }
289
290 int ConfirmEx (int /*long*/ aParent, int /*long*/ aDialogTitle, int /*long*/ aText, int aButtonFlags, int /*long*/ aButton0Title, int /*long*/ aButton1Title, int /*long*/ aButton2Title, int /*long*/ aCheckMsg, int /*long*/ aCheckState, int /*long*/ _retval) {
291 Browser browser = getBrowser (aParent);
292
293 int length = XPCOM.strlen_PRUnichar (aDialogTitle);
294 char[] dest = new char[length];
295 XPCOM.memmove (dest, aDialogTitle, length * 2);
296 String titleLabel = new String (dest);
297
298 length = XPCOM.strlen_PRUnichar (aText);
299 dest = new char[length];
300 XPCOM.memmove (dest, aText, length * 2);
301 String textLabel = new String (dest);
302
303 String checkLabel = null;
304 if (aCheckMsg !is 0) {
305 length = XPCOM.strlen_PRUnichar (aCheckMsg);
306 dest = new char[length];
307 XPCOM.memmove (dest, aCheckMsg, length * 2);
308 checkLabel = new String (dest);
309 }
310
311 String button0Label = getLabel (aButtonFlags, nsIPromptService.BUTTON_POS_0, aButton0Title);
312 String button1Label = getLabel (aButtonFlags, nsIPromptService.BUTTON_POS_1, aButton1Title);
313 String button2Label = getLabel (aButtonFlags, nsIPromptService.BUTTON_POS_2, aButton2Title);
314
315 int defaultIndex = 0;
316 if ((aButtonFlags & nsIPromptService.BUTTON_POS_1_DEFAULT) !is 0) {
317 defaultIndex = 1;
318 } else if ((aButtonFlags & nsIPromptService.BUTTON_POS_2_DEFAULT) !is 0) {
319 defaultIndex = 2;
320 }
321
322 Shell shell = browser is null ? new Shell () : browser.getShell ();
323 PromptDialog dialog = new PromptDialog (shell);
324 int[] check = new int[1], result = new int[1];
325 if (aCheckState !is 0) XPCOM.memmove (check, aCheckState, 4);
326 dialog.confirmEx (titleLabel, textLabel, checkLabel, button0Label, button1Label, button2Label, defaultIndex, check, result);
327 if (aCheckState !is 0) XPCOM.memmove (aCheckState, check, 4);
328 XPCOM.memmove (_retval, result, 4);
329 return XPCOM.NS_OK;
330 }
331
332 int Prompt (int /*long*/ aParent, int /*long*/ aDialogTitle, int /*long*/ aText, int /*long*/ aValue, int /*long*/ aCheckMsg, int /*long*/ aCheckState, int /*long*/ _retval) {
333 Browser browser = getBrowser (aParent);
334 String titleLabel = null, textLabel, checkLabel = null;
335 String[] valueLabel = new String[1];
336 char[] dest;
337 int length;
338 if (aDialogTitle !is 0) {
339 length = XPCOM.strlen_PRUnichar (aDialogTitle);
340 dest = new char[length];
341 XPCOM.memmove (dest, aDialogTitle, length * 2);
342 titleLabel = new String (dest);
343 }
344
345 length = XPCOM.strlen_PRUnichar (aText);
346 dest = new char[length];
347 XPCOM.memmove (dest, aText, length * 2);
348 textLabel = new String (dest);
349
350 int /*long*/[] valueAddr = new int /*long*/[1];
351 XPCOM.memmove (valueAddr, aValue, C.PTR_SIZEOF);
352 if (valueAddr[0] !is 0) {
353 length = XPCOM.strlen_PRUnichar (valueAddr[0]);
354 dest = new char[length];
355 XPCOM.memmove (dest, valueAddr[0], length * 2);
356 valueLabel[0] = new String (dest);
357 }
358
359 if (aCheckMsg !is 0) {
360 length = XPCOM.strlen_PRUnichar (aCheckMsg);
361 if (length > 0) {
362 dest = new char[length];
363 XPCOM.memmove (dest, aCheckMsg, length * 2);
364 checkLabel = new String (dest);
365 }
366 }
367
368 Shell shell = browser is null ? new Shell () : browser.getShell ();
369 PromptDialog dialog = new PromptDialog (shell);
370 int[] check = new int[1], result = new int[1];
371 if (aCheckState !is 0) XPCOM.memmove (check, aCheckState, 4);
372 dialog.prompt (titleLabel, textLabel, checkLabel, valueLabel, check, result);
373
374 XPCOM.memmove (_retval, result, 4);
375 if (result[0] is 1) {
376 /*
377 * User selected OK. User name and password are returned as PRUnichar values. Any default
378 * value that we override must be freed using the nsIMemory service.
379 */
380 int cnt, size;
381 int /*long*/ ptr;
382 char[] buffer;
383 int /*long*/[] result2 = new int /*long*/[1];
384 if (valueLabel[0] !is null) {
385 cnt = valueLabel[0].length ();
386 buffer = new char[cnt + 1];
387 valueLabel[0].getChars (0, cnt, buffer, 0);
388 size = buffer.length * 2;
389 ptr = C.malloc (size);
390 XPCOM.memmove (ptr, buffer, size);
391 XPCOM.memmove (aValue, new int /*long*/[] {ptr}, C.PTR_SIZEOF);
392
393 if (valueAddr[0] !is 0) {
394 int rc = XPCOM.NS_GetServiceManager (result2);
395 if (rc !is XPCOM.NS_OK) DWT.error (rc);
396 if (result2[0] is 0) DWT.error (XPCOM.NS_NOINTERFACE);
397
398 nsIServiceManager serviceManager = new nsIServiceManager (result2[0]);
399 result2[0] = 0;
400 byte[] aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_MEMORY_CONTRACTID, true);
401 rc = serviceManager.GetServiceByContractID (aContractID, nsIMemory.NS_IMEMORY_IID, result2);
402 if (rc !is XPCOM.NS_OK) DWT.error (rc);
403 if (result2[0] is 0) DWT.error (XPCOM.NS_NOINTERFACE);
404 serviceManager.Release ();
405
406 nsIMemory memory = new nsIMemory (result2[0]);
407 result2[0] = 0;
408 memory.Free (valueAddr[0]);
409 memory.Release ();
410 }
411 }
412 }
413 if (aCheckState !is 0) XPCOM.memmove (aCheckState, check, 4);
414 return XPCOM.NS_OK;
415 }
416
417 int PromptAuth(int /*long*/ aParent, int /*long*/ aChannel, int level, int /*long*/ authInfo, int /*long*/ checkboxLabel, int /*long*/ checkboxValue, int /*long*/ _retval) {
418 Browser browser = getBrowser (aParent);
419 String checkLabel = null;
420 int[] checkValue = new int[1];
421 String[] userLabel = new String[1], passLabel = new String[1];
422
423 String title = DWT.getMessage ("SWT_Authentication_Required"); //$NON-NLS-1$
424
425 if (checkboxLabel !is 0 && checkboxValue !is 0) {
426 int length = XPCOM.strlen_PRUnichar (checkboxLabel);
427 char[] dest = new char[length];
428 XPCOM.memmove (dest, checkboxLabel, length * 2);
429 checkLabel = new String (dest);
430 XPCOM.memmove (checkValue, checkboxValue, 4); /* PRBool */
431 }
432
433 /* get initial username and password values */
434
435 nsIAuthInformation auth = new nsIAuthInformation (authInfo);
436
437 int /*long*/ ptr = XPCOM.nsEmbedString_new ();
438 int rc = auth.GetUsername (ptr);
439 if (rc !is XPCOM.NS_OK) DWT.error (rc);
440 int length = XPCOM.nsEmbedString_Length (ptr);
441 int /*long*/ buffer = XPCOM.nsEmbedString_get (ptr);
442 char[] chars = new char[length];
443 XPCOM.memmove (chars, buffer, length * 2);
444 userLabel[0] = new String (chars);
445 XPCOM.nsEmbedString_delete (ptr);
446
447 ptr = XPCOM.nsEmbedString_new ();
448 rc = auth.GetPassword (ptr);
449 if (rc !is XPCOM.NS_OK) DWT.error (rc);
450 length = XPCOM.nsEmbedString_Length (ptr);
451 buffer = XPCOM.nsEmbedString_get (ptr);
452 chars = new char[length];
453 XPCOM.memmove (chars, buffer, length * 2);
454 passLabel[0] = new String (chars);
455 XPCOM.nsEmbedString_delete (ptr);
456
457 /* compute the message text */
458
459 ptr = XPCOM.nsEmbedString_new ();
460 rc = auth.GetRealm (ptr);
461 if (rc !is XPCOM.NS_OK) DWT.error (rc);
462 length = XPCOM.nsEmbedString_Length (ptr);
463 buffer = XPCOM.nsEmbedString_get (ptr);
464 chars = new char[length];
465 XPCOM.memmove (chars, buffer, length * 2);
466 String realm = new String (chars);
467 XPCOM.nsEmbedString_delete (ptr);
468
469 nsIChannel channel = new nsIChannel (aChannel);
470 int /*long*/[] uri = new int /*long*/[1];
471 rc = channel.GetURI (uri);
472 if (rc !is XPCOM.NS_OK) DWT.error (rc);
473 if (uri[0] is 0) Mozilla.error (XPCOM.NS_NOINTERFACE);
474
475 nsIURI nsURI = new nsIURI (uri[0]);
476 int /*long*/ aSpec = XPCOM.nsEmbedCString_new ();
477 rc = nsURI.GetHost (aSpec);
478 if (rc !is XPCOM.NS_OK) DWT.error (rc);
479 length = XPCOM.nsEmbedCString_Length (aSpec);
480 buffer = XPCOM.nsEmbedCString_get (aSpec);
481 byte[] bytes = new byte[length];
482 XPCOM.memmove (bytes, buffer, length);
483 XPCOM.nsEmbedCString_delete (aSpec);
484 String host = new String (bytes);
485 nsURI.Release ();
486
487 String message;
488 if (realm.length () > 0 && host.length () > 0) {
489 message = Compatibility.getMessage ("SWT_Enter_Username_and_Password", new String[] {realm, host}); //$NON-NLS-1$
490 } else {
491 message = ""; //$NON-NLS-1$
492 }
493
494 /* open the prompter */
495 Shell shell = browser is null ? new Shell () : browser.getShell ();
496 PromptDialog dialog = new PromptDialog (shell);
497 int[] result = new int[1];
498 dialog.promptUsernameAndPassword (title, message, checkLabel, userLabel, passLabel, checkValue, result);
499
500 XPCOM.memmove (_retval, result, 4); /* PRBool */
501 if (result[0] is 1) { /* User selected OK */
502 nsEmbedString string = new nsEmbedString (userLabel[0]);
503 rc = auth.SetUsername(string.getAddress ());
504 if (rc !is XPCOM.NS_OK) DWT.error (rc);
505 string.dispose ();
506
507 string = new nsEmbedString (passLabel[0]);
508 rc = auth.SetPassword(string.getAddress ());
509 if (rc !is XPCOM.NS_OK) DWT.error (rc);
510 string.dispose ();
511 }
512
513 if (checkboxValue !is 0) XPCOM.memmove (checkboxValue, checkValue, 4); /* PRBool */
514 return XPCOM.NS_OK;
515 }
516
517 int PromptUsernameAndPassword (int /*long*/ aParent, int /*long*/ aDialogTitle, int /*long*/ aText, int /*long*/ aUsername, int /*long*/ aPassword, int /*long*/ aCheckMsg, int /*long*/ aCheckState, int /*long*/ _retval) {
518 Browser browser = getBrowser (aParent);
519 String titleLabel, textLabel, checkLabel = null;
520 String[] userLabel = new String[1], passLabel = new String[1];
521 char[] dest;
522 int length;
523 if (aDialogTitle !is 0) {
524 length = XPCOM.strlen_PRUnichar (aDialogTitle);
525 dest = new char[length];
526 XPCOM.memmove (dest, aDialogTitle, length * 2);
527 titleLabel = new String (dest);
528 } else {
529 titleLabel = DWT.getMessage ("SWT_Authentication_Required"); //$NON-NLS-1$
530 }
531
532 length = XPCOM.strlen_PRUnichar (aText);
533 dest = new char[length];
534 XPCOM.memmove (dest, aText, length * 2);
535 textLabel = new String (dest);
536
537 int /*long*/[] userAddr = new int /*long*/[1];
538 XPCOM.memmove (userAddr, aUsername, C.PTR_SIZEOF);
539 if (userAddr[0] !is 0) {
540 length = XPCOM.strlen_PRUnichar (userAddr[0]);
541 dest = new char[length];
542 XPCOM.memmove (dest, userAddr[0], length * 2);
543 userLabel[0] = new String (dest);
544 }
545
546 int /*long*/[] passAddr = new int /*long*/[1];
547 XPCOM.memmove (passAddr, aPassword, C.PTR_SIZEOF);
548 if (passAddr[0] !is 0) {
549 length = XPCOM.strlen_PRUnichar (passAddr[0]);
550 dest = new char[length];
551 XPCOM.memmove (dest, passAddr[0], length * 2);
552 passLabel[0] = new String (dest);
553 }
554
555 if (aCheckMsg !is 0) {
556 length = XPCOM.strlen_PRUnichar (aCheckMsg);
557 if (length > 0) {
558 dest = new char[length];
559 XPCOM.memmove (dest, aCheckMsg, length * 2);
560 checkLabel = new String (dest);
561 }
562 }
563
564 Shell shell = browser is null ? new Shell () : browser.getShell ();
565 PromptDialog dialog = new PromptDialog (shell);
566 int[] check = new int[1], result = new int[1];
567 if (aCheckState !is 0) XPCOM.memmove (check, aCheckState, 4); /* PRBool */
568 dialog.promptUsernameAndPassword (titleLabel, textLabel, checkLabel, userLabel, passLabel, check, result);
569
570 XPCOM.memmove (_retval, result, 4); /* PRBool */
571 if (result[0] is 1) {
572 /*
573 * User selected OK. User name and password are returned as PRUnichar values. Any default
574 * value that we override must be freed using the nsIMemory service.
575 */
576 int cnt, size;
577 int /*long*/ ptr;
578 char[] buffer;
579 int /*long*/[] result2 = new int /*long*/[1];
580 if (userLabel[0] !is null) {
581 cnt = userLabel[0].length ();
582 buffer = new char[cnt + 1];
583 userLabel[0].getChars (0, cnt, buffer, 0);
584 size = buffer.length * 2;
585 ptr = C.malloc (size);
586 XPCOM.memmove (ptr, buffer, size);
587 XPCOM.memmove (aUsername, new int /*long*/[] {ptr}, C.PTR_SIZEOF);
588
589 if (userAddr[0] !is 0) {
590 int rc = XPCOM.NS_GetServiceManager (result2);
591 if (rc !is XPCOM.NS_OK) DWT.error (rc);
592 if (result2[0] is 0) DWT.error (XPCOM.NS_NOINTERFACE);
593
594 nsIServiceManager serviceManager = new nsIServiceManager (result2[0]);
595 result2[0] = 0;
596 byte[] aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_MEMORY_CONTRACTID, true);
597 rc = serviceManager.GetServiceByContractID (aContractID, nsIMemory.NS_IMEMORY_IID, result2);
598 if (rc !is XPCOM.NS_OK) DWT.error (rc);
599 if (result[0] is 0) DWT.error (XPCOM.NS_NOINTERFACE);
600 serviceManager.Release ();
601
602 nsIMemory memory = new nsIMemory (result2[0]);
603 result2[0] = 0;
604 memory.Free (userAddr[0]);
605 memory.Release ();
606 }
607 }
608 if (passLabel[0] !is null) {
609 cnt = passLabel[0].length ();
610 buffer = new char[cnt + 1];
611 passLabel[0].getChars (0, cnt, buffer, 0);
612 size = buffer.length * 2;
613 ptr = C.malloc (size);
614 XPCOM.memmove (ptr, buffer, size);
615 XPCOM.memmove (aPassword, new int /*long*/[] {ptr}, C.PTR_SIZEOF);
616
617 if (passAddr[0] !is 0) {
618 int rc = XPCOM.NS_GetServiceManager (result2);
619 if (rc !is XPCOM.NS_OK) DWT.error (rc);
620 if (result2[0] is 0) DWT.error (XPCOM.NS_NOINTERFACE);
621
622 nsIServiceManager serviceManager = new nsIServiceManager (result2[0]);
623 result2[0] = 0;
624 byte[] aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_MEMORY_CONTRACTID, true);
625 rc = serviceManager.GetServiceByContractID (aContractID, nsIMemory.NS_IMEMORY_IID, result2);
626 if (rc !is XPCOM.NS_OK) DWT.error (rc);
627 if (result2[0] is 0) DWT.error (XPCOM.NS_NOINTERFACE);
628 serviceManager.Release ();
629
630 nsIMemory memory = new nsIMemory (result2[0]);
631 result2[0] = 0;
632 memory.Free (passAddr[0]);
633 memory.Release ();
634 }
635 }
636 }
637 if (aCheckState !is 0) XPCOM.memmove (aCheckState, check, 4); /* PRBool */
638 return XPCOM.NS_OK;
639 }
640
641 int PromptPassword (int /*long*/ aParent, int /*long*/ aDialogTitle, int /*long*/ aText, int /*long*/ aPassword, int /*long*/ aCheckMsg, int /*long*/ aCheckState, int /*long*/ _retval) {
642 return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
643 }
644
645 int Select (int /*long*/ aParent, int /*long*/ aDialogTitle, int /*long*/ aText, int aCount, int /*long*/ aSelectList, int /*long*/ aOutSelection, int /*long*/ _retval) {
646 return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
647 }
648
649 }