comparison dwt/browser/InputStream.d @ 286:44258e0b6687

More fixes for xpcom
author John Reimer<terminal.node@gmail.com>
date Tue, 05 Aug 2008 10:11:58 -0700
parents 93409d9838c5
children eec6ddb07873
comparison
equal deleted inserted replaced
280:e72345914350 286:44258e0b6687
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * John Reimer <terminal.node@gmail.com>
12 *******************************************************************************/ 10 *******************************************************************************/
13
14 module dwt.browser.InputStream; 11 module dwt.browser.InputStream;
15 12
16 import dwt.dwthelper.utils; 13 import dwt.dwthelper.utils;
17 14
18 import dwt.internal.mozilla.nsError; 15 import dwt.internal.C;
16 import dwt.internal.mozilla.XPCOM;
17 import dwt.internal.mozilla.XPCOMObject;
19 import dwt.internal.mozilla.nsID; 18 import dwt.internal.mozilla.nsID;
20 import dwt.internal.mozilla.nsIInputStream; 19 import dwt.internal.mozilla.nsIInputStream;
21 import dwt.internal.mozilla.nsISupports; 20 import dwt.internal.mozilla.nsISupports;
22 21
23 import Math = tango.math.Math; 22 class InputStream {
23 XPCOMObject inputStream;
24 int refCount = 0;
24 25
25 /****************************************************************************** 26 byte[] buffer;
27 int index = 0;
28
29 InputStream (byte[] buffer) {
30 this.buffer = buffer;
31 index = 0;
32 createCOMInterfaces ();
33 }
26 34
27 COMMENTS: SWT implements this class as a replacement for the XPCOM 35 int AddRef () {
28 implementation; it may be possible to use the XPCOM one instead 36 refCount++;
29 (which also may be safer for D), but for now we'll follow SWT's example 37 return refCount;
30 and use this reimplementation of InputStream. It should be trivial to 38 }
31 move over to strict XPCOM implementation should the need arise. It appears
32 that the Java SWT source uses many workarounds in order to use the XPCOM
33 interface. For D, I remove much of the Java layered callback
34 approach since it is no longer necessary. -JJR
35 39
36 ******************************************************************************/ 40 void createCOMInterfaces () {
41 /* Create each of the interfaces that this object implements */
42 inputStream = new XPCOMObject (new int[] {2, 0, 0, 0, 1, 3, 4, 1}) {
43 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
44 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
45 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
46 public int /*long*/ method3 (int /*long*/[] args) {return Close ();}
47 public int /*long*/ method4 (int /*long*/[] args) {return Available (args[0]);}
48 public int /*long*/ method5 (int /*long*/[] args) {return Read (args[0], (int)/*64*/args[1], args[2]);}
49 public int /*long*/ method6 (int /*long*/[] args) {return ReadSegments (args[0], args[1], (int)/*64*/args[2], args[3]);}
50 public int /*long*/ method7 (int /*long*/[] args) {return IsNonBlocking (args[0]);}
51 };
52 }
37 53
38 class InputStream : nsIInputStream 54 void disposeCOMInterfaces () {
39 { 55 if (inputStream !is null) {
40 int _refCount = 0; 56 inputStream.dispose ();
41 int _index = 0; 57 inputStream = null;
42 byte[] _buffer; 58 }
59 }
43 60
44 /************************************************************************** 61 int /*long*/ getAddress () {
62 return inputStream.getAddress ();
63 }
45 64
46 **************************************************************************/ 65 int QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
66 if (riid is 0 || ppvObject is 0) return XPCOM.NS_ERROR_NO_INTERFACE;
67 nsID guid = new nsID ();
68 XPCOM.memmove (guid, riid, nsID.sizeof);
69
70 if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
71 XPCOM.memmove (ppvObject, new int /*long*/[] {inputStream.getAddress ()}, C.PTR_SIZEOF);
72 AddRef ();
73 return XPCOM.NS_OK;
74 }
75 if (guid.Equals (nsIInputStream.NS_IINPUTSTREAM_IID)) {
76 XPCOM.memmove (ppvObject, new int /*long*/[] {inputStream.getAddress ()}, C.PTR_SIZEOF);
77 AddRef ();
78 return XPCOM.NS_OK;
79 }
80 XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
81 return XPCOM.NS_ERROR_NO_INTERFACE;
82 }
83
84 int Release () {
85 refCount--;
86 if (refCount is 0) disposeCOMInterfaces ();
87 return refCount;
88 }
89
90 /* nsIInputStream implementation */
47 91
48 this (byte[] buffer) 92 int Close () {
49 { 93 buffer = null;
50 this._buffer = buffer; 94 index = 0;
51 index = 0; 95 return XPCOM.NS_OK;
96 }
97
98 int Available (int /*long*/ _retval) {
99 int available = buffer is null ? 0 : buffer.length - index;
100 XPCOM.memmove (_retval, new int[] {available}, 4);
101 return XPCOM.NS_OK;
102 }
103
104 int Read(int /*long*/ aBuf, int aCount, int /*long*/ _retval) {
105 int max = Math.min (aCount, buffer is null ? 0 : buffer.length - index);
106 if (max > 0) {
107 byte[] src = new byte[max];
108 System.arraycopy (buffer, index, src, 0, max);
109 XPCOM.memmove (aBuf, src, max);
110 index += max;
52 } 111 }
112 XPCOM.memmove(_retval, new int[] {max}, 4);
113 return XPCOM.NS_OK;
114 }
53 115
54 /************************************************************************** 116 int ReadSegments (int /*long*/ aWriter, int /*long*/ aClosure, int aCount, int /*long*/ _retval) {
117 int max = Math.min (aCount, buffer is null ? 0 : buffer.length - index);
118 int cnt = max;
119 while (cnt > 0) {
120 int[] aWriteCount = new int[1];
121 int /*long*/ rc = XPCOM.Call (aWriter, getAddress (), aClosure, buffer, index, cnt, aWriteCount);
122 if (rc !is XPCOM.NS_OK) break;
123 index += aWriteCount[0];
124 cnt -= aWriteCount[0];
125 }
126 XPCOM.memmove (_retval, new int[] {max - cnt}, 4);
127 return XPCOM.NS_OK;
128 }
55 129
56 **************************************************************************/ 130 int IsNonBlocking (int /*long*/ _retval) {
57 131 /* blocking */
58 nsrefcnt AddRef () 132 XPCOM.memmove (_retval, new int[] {0}, 4);
59 { 133 return XPCOM.NS_OK;
60 _refCount++; 134 }
61 return _refCount;
62 }
63
64 /**************************************************************************
65
66 **************************************************************************/
67
68 nsresult QueryInterface ( inout nsID riid, void** ppvObject)
69 {
70 if (riid is null || ppvObject is null)
71 return NS_ERROR_NO_INTERFACE;
72
73 nsID guid = riid;
74
75 if (guid == nsISupports.NS_ISUPPORTS_IID)
76 {
77 *ppvObject = cast(void*)cast(nsISupports) this;
78 this.AddRef ();
79 return NS_OK;
80 }
81
82 if (guid == nsIInputStream.NS_IINPUTSTREAM_IID)
83 {
84 *ppvObject = cast(void*)cast(nsIInputStream) this;
85 this.AddRef ();
86 return NS_OK;
87 }
88
89 *ppvObject = null;
90 return NS_ERROR_NO_INTERFACE;
91 }
92
93 /**************************************************************************
94
95 **************************************************************************/
96
97 nsrefcnt Release ()
98 {
99 _refCount--;
100 if (_refCount is 0)
101 return 0
102 // delete this;
103 return _refCount;
104 }
105
106 /**************************************************************************
107
108 **************************************************************************/
109
110 nsresult Close ()
111 {
112 _buffer = null;
113 _index = 0;
114 return NS_OK;
115 }
116
117 /**************************************************************************
118
119 **************************************************************************/
120
121 nsresult Available ( PRUint32* retval)
122 {
123 PRUint32 available = buffer is null ? 0 : buffer.length - _index;
124 *retval = available;
125 return NS_OK;
126 }
127
128 /**************************************************************************
129
130 **************************************************************************/
131
132 nsresult Read(byte* aBuf, PRUint32 aCount, PRUint32* retval)
133 {
134 int max = Math.min (aCount, _buffer is null ? 0 : _buffer.length - _index);
135
136 if (max > 0)
137 {
138 aBuf[0..max] = _buffer[_index..$];
139 _index += max;
140 }
141
142 *retval = max;
143 return NS_OK;
144 }
145
146 /**************************************************************************
147
148 **************************************************************************/
149
150 nsresult ReadSegments (nsWriteSegmentFun aWriter, void* aClosure, PRUint32 aCount, PRUint32* retval)
151 {
152 int max = Math.min (aCount, buffer is null ? 0 : buffer.length - _index);
153 PRUint32 cnt = max;
154
155 while (cnt > 0)
156 {
157 PRUint32 aWriteCount;
158 nsresult rc = aWriter ( cast(nsIInputStream)this, aClosure, buffer.ptr, _index, cnt, &aWriteCount);
159 if (rc !is NS_OK) break;
160 _index += aWriteCount;
161 cnt -= aWriteCount;
162 }
163
164 *retval = (max - cnt);
165 return NS_OK;
166 }
167
168 /**************************************************************************
169
170 **************************************************************************/
171
172 nsresult IsNonBlocking ( PRUint32* retval)
173 {
174 *retval = 0;
175 return NS_OK;
176 }
177 } 135 }