|
1 /* |
|
2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Recognizer for the bowser supported MIME types |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "BrowserRec.h" |
|
19 |
|
20 #include "ImplementationProxy.h" |
|
21 |
|
22 // |
|
23 // CBrowserRecognizer |
|
24 // |
|
25 CBrowserRecognizer::CBrowserRecognizer() |
|
26 :CApaDataRecognizerType( KUidMimeBrowserRecognizer, CApaDataRecognizerType::EHigh ) |
|
27 { |
|
28 iCountDataTypes = KSupportedMimetypes; |
|
29 } |
|
30 |
|
31 TUint CBrowserRecognizer::PreferredBufSize() |
|
32 { |
|
33 return 0x80; |
|
34 } |
|
35 |
|
36 TDataType CBrowserRecognizer::SupportedDataTypeL( TInt aIndex ) const |
|
37 { |
|
38 __ASSERT_DEBUG( aIndex>=0 && aIndex < KSupportedMimetypes, User::Invariant() ); |
|
39 switch ( aIndex ) |
|
40 { |
|
41 case 0: |
|
42 return TDataType( KHTMLMimeType ); |
|
43 case 1: |
|
44 return TDataType( KXHTMLMimeType1 ); |
|
45 case 2: |
|
46 return TDataType( KXHTMLMimeType2 ); |
|
47 case 3: |
|
48 return TDataType( KXHTMLMimeType2 ); |
|
49 case 4: |
|
50 return TDataType( KCssMimeType ); |
|
51 case 5: |
|
52 return TDataType( KJavaSCMimeType); |
|
53 case 6: |
|
54 return TDataType( KJavaSCMimeType); |
|
55 case 7: |
|
56 return TDataType( KJavaEcmaMimeType); |
|
57 case 8: |
|
58 return TDataType( KOPMLMimeType); |
|
59 #ifndef BRDO_WML_DISABLED_FF |
|
60 case 9: |
|
61 return TDataType( KWMLCMimeType ); |
|
62 case 10: |
|
63 return TDataType( KWMLMimeType ); |
|
64 case 11: |
|
65 return TDataType( KWBXMLMimeType ); |
|
66 case 12: |
|
67 return TDataType( KWMLSCMimeType ); |
|
68 #endif |
|
69 default: |
|
70 return TDataType( KHTMLMimeType ); |
|
71 } |
|
72 } |
|
73 |
|
74 void CBrowserRecognizer::DoRecognizeL( const TDesC& aName, const TDesC8& aBuffer ) |
|
75 { |
|
76 iConfidence = ENotRecognized; |
|
77 if ( aBuffer.Length() < 3 ) |
|
78 return; |
|
79 |
|
80 // First try the name extension |
|
81 if ( aName.Length() >= 3 ) |
|
82 { |
|
83 TInt dotPos = aName.LocateReverse( '.' ); |
|
84 if ( dotPos != KErrNotFound ) |
|
85 { |
|
86 TInt extLength = aName.Length() - dotPos; |
|
87 HBufC* ext = aName.Right( extLength ).AllocL(); |
|
88 CleanupStack::PushL( ext ); |
|
89 |
|
90 #ifndef BRDO_WML_DISABLED_FF |
|
91 // application/vnd.wap.wmlc or application/vnd.wap.wbxml |
|
92 if ( ext->CompareF( KDotWMLC ) == 0 || |
|
93 ext->CompareF( KDotWBXML ) == 0 ) |
|
94 { |
|
95 iDataType = TDataType( KWMLCMimeType ); |
|
96 if ( CheckWbxmlVersion( aBuffer[0] ) && |
|
97 CheckWbxmlPubId( aBuffer[1] ) ) |
|
98 { |
|
99 iConfidence = ECertain; |
|
100 } |
|
101 else |
|
102 { |
|
103 iConfidence = EPossible; |
|
104 } |
|
105 } |
|
106 // application/vnd.wap.wmlscriptc |
|
107 else if ( ext->CompareF(KDotWMLSC) == 0 ) |
|
108 { |
|
109 iDataType = TDataType( KWMLSCMimeType ); |
|
110 iConfidence = ECertain; |
|
111 } |
|
112 else if ( ext->CompareF(KDotJavaScript) == 0 ) |
|
113 { |
|
114 iDataType = TDataType( KJavaSCMimeType ); |
|
115 iConfidence = ECertain; |
|
116 } |
|
117 #else |
|
118 if ( ext->CompareF(KDotJavaScript) == 0 ) |
|
119 { |
|
120 iDataType = TDataType( KJavaSCMimeType ); |
|
121 iConfidence = ECertain; |
|
122 } |
|
123 #endif // BRDO_WML_DISABLED_FF |
|
124 else if ( ext->CompareF(KDotCss) == 0 ) |
|
125 { |
|
126 iDataType = TDataType( KCssMimeType ); |
|
127 iConfidence = ECertain; |
|
128 } |
|
129 |
|
130 // text/html |
|
131 else if ( ext->CompareF(KDotHTML) == 0 || |
|
132 ext->CompareF(KDotHTM) == 0 ) |
|
133 { |
|
134 iDataType = TDataType( KHTMLMimeType ); |
|
135 iConfidence = ECertain; |
|
136 } |
|
137 // application/vnd.wap.html+xml and application/xhtml+xml |
|
138 else if ( ext->CompareF(KDotXHTML) == 0 || |
|
139 ext->CompareF(KDotXHTML2) == 0 ) |
|
140 { |
|
141 iDataType = TDataType( KHTMLMimeType ); |
|
142 iConfidence = ECertain; |
|
143 } |
|
144 // OPML file |
|
145 else if ( ext->CompareF(KDotOPML) == 0 ) |
|
146 { |
|
147 iDataType = TDataType( KOPMLMimeType ); |
|
148 iConfidence = ECertain; |
|
149 } |
|
150 CleanupStack::PopAndDestroy(); // ext |
|
151 } |
|
152 } |
|
153 |
|
154 // No recognized file name extensions, we have to look into the buffer |
|
155 if ( iConfidence == ENotRecognized ) |
|
156 { |
|
157 #ifndef BRDO_WML_DISABLED_FF |
|
158 if ( CheckForWMLC( aBuffer ) ) |
|
159 { |
|
160 iDataType = TDataType( KWMLCMimeType ); |
|
161 iConfidence = ECertain; |
|
162 } |
|
163 else if ( CheckForOPML( aBuffer ) ) |
|
164 { |
|
165 iDataType = TDataType( KOPMLMimeType ); |
|
166 iConfidence = ECertain; |
|
167 } |
|
168 else if ( CheckForWML( aBuffer ) ) |
|
169 { |
|
170 iDataType = TDataType( KWMLMimeType ); |
|
171 iConfidence = ECertain; |
|
172 } |
|
173 #else |
|
174 if ( CheckForOPML( aBuffer ) ) |
|
175 { |
|
176 iDataType = TDataType( KOPMLMimeType ); |
|
177 iConfidence = ECertain; |
|
178 } |
|
179 #endif // BRDO_WML_DISABLED_FF |
|
180 else if ( CheckForHtml( aBuffer ) ) |
|
181 { |
|
182 if ( CheckForXhtml( aBuffer ) ) |
|
183 { |
|
184 iDataType = TDataType( KXHTMLMimeType2 ); |
|
185 } |
|
186 else |
|
187 { |
|
188 iDataType = TDataType( KHTMLMimeType ); |
|
189 } |
|
190 iConfidence = ECertain; |
|
191 } |
|
192 } |
|
193 } |
|
194 |
|
195 |
|
196 // We support only wml version 1.1, 1.2 and 1.3 |
|
197 TBool CBrowserRecognizer::CheckWbxmlVersion( TUint8 byte ) |
|
198 { |
|
199 switch( byte ) |
|
200 { |
|
201 case 0x01: // wml version 1.1 |
|
202 case 0x02: // wml version 1.2 |
|
203 case 0x03: // wml version 1.3 |
|
204 return ETrue; |
|
205 default: |
|
206 return EFalse; |
|
207 } |
|
208 } |
|
209 |
|
210 // We support only wml version 1.1, 1.2 and 1.3 |
|
211 TBool CBrowserRecognizer::CheckWbxmlPubId( TUint8 byte ) |
|
212 { |
|
213 switch( byte ) |
|
214 { |
|
215 case 0x00: // String table follows. |
|
216 case 0x04: // "-//WAPFORUM//DTD WML 1.1//EN (WML 1.1) |
|
217 case 0x09: // "-//WAPFORUM//DTD WML 1.2//EN (WML 1.2) |
|
218 case 0x0A: // "-//WAPFORUM//DTD WML 1.2//EN (WML 1.3) |
|
219 return ETrue; |
|
220 default: |
|
221 return EFalse; |
|
222 } |
|
223 } |
|
224 |
|
225 // |
|
226 // Check that the content has certain bytes is correct order |
|
227 // according to the wmlc spec. |
|
228 // |
|
229 TBool CBrowserRecognizer::CheckForWMLC( const TDesC8& aBuffer ) |
|
230 { |
|
231 TInt IndexOfxmlVer = 0; |
|
232 TInt IndexOfpubId = 1; |
|
233 TInt IndexOfstrTblLen = 3; |
|
234 TInt IndexOfstrTblStart = 4; |
|
235 TInt root = -1; |
|
236 |
|
237 if ( aBuffer.Length() <= IndexOfstrTblStart ) |
|
238 return EFalse; |
|
239 |
|
240 if ( !CheckWbxmlVersion( aBuffer[IndexOfxmlVer] ) || |
|
241 !CheckWbxmlPubId( aBuffer[IndexOfpubId] ) ) |
|
242 return EFalse; |
|
243 |
|
244 // Skip chararcter set |
|
245 |
|
246 // if the string table length is less than zero or more that the |
|
247 // length of the buffer we cannot find a root element. |
|
248 if ( aBuffer[IndexOfstrTblLen] > aBuffer.Length() - IndexOfstrTblLen ) |
|
249 return EFalse; |
|
250 |
|
251 root = aBuffer[IndexOfstrTblStart + aBuffer[IndexOfstrTblLen]]; |
|
252 |
|
253 // Check if the root element is <wml> |
|
254 if ( root == 0x7F ) |
|
255 return ETrue; |
|
256 |
|
257 return EFalse; |
|
258 } |
|
259 |
|
260 // |
|
261 // From RFC 3236 "5. Recognizing XHTML files" |
|
262 // |
|
263 TBool CBrowserRecognizer::CheckForHtml( const TDesC8& aBuffer ) |
|
264 { |
|
265 _LIT8( html, "<html" ); |
|
266 _LIT8( script, "<script" ); |
|
267 if ( aBuffer.FindF( html ) != KErrNotFound ) |
|
268 return ETrue; |
|
269 if ( aBuffer.FindF( script ) != KErrNotFound ) |
|
270 return ETrue; |
|
271 return EFalse; |
|
272 } |
|
273 |
|
274 // |
|
275 // From RFC 3236 "5. Recognizing XHTML files" |
|
276 // |
|
277 TBool CBrowserRecognizer::CheckForXhtml( const TDesC8& aBuffer ) |
|
278 { |
|
279 _LIT8( dtd, "//DTD XHTML" ); |
|
280 if ( aBuffer.FindF( dtd ) != KErrNotFound ) |
|
281 return ETrue; |
|
282 return EFalse; |
|
283 } |
|
284 |
|
285 TBool CBrowserRecognizer::CheckForWML( const TDesC8& aBuffer ) |
|
286 { |
|
287 _LIT8( dtd, "-//WAPFORUM//DTD WML" ); |
|
288 if ( aBuffer.FindF( dtd ) != KErrNotFound ) |
|
289 return ETrue; |
|
290 return EFalse; |
|
291 } |
|
292 |
|
293 TBool CBrowserRecognizer::CheckForOPML( const TDesC8& aBuffer ) |
|
294 { |
|
295 _LIT8( dtd, "<opml" ); |
|
296 if( aBuffer.FindF( dtd ) != KErrNotFound ) |
|
297 return ETrue; |
|
298 return EFalse; |
|
299 } |
|
300 |
|
301 |
|
302 CApaDataRecognizerType* CBrowserRecognizer::CreateRecognizerL() |
|
303 { |
|
304 return new (ELeave) CBrowserRecognizer(); |
|
305 } |
|
306 |
|
307 const TImplementationProxy ImplementationTable[] = |
|
308 { |
|
309 // { { KBrowserRecognizerImplUIDValue}, CBrowserRecognizer::CreateRecognizerL} |
|
310 |
|
311 IMPLEMENTATION_PROXY_ENTRY(KBrowserRecognizerImplUIDValue, CBrowserRecognizer::CreateRecognizerL) |
|
312 }; |
|
313 |
|
314 |
|
315 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount) |
|
316 { |
|
317 aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy); |
|
318 return ImplementationTable; |
|
319 } |