dom/wappush/src/gonk/SlPduHelper.jsm

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:470ba4010e26
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 "use strict";
6
7 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
8
9 let WSP = {};
10 Cu.import("resource://gre/modules/WspPduHelper.jsm", WSP);
11 let WBXML = {};
12 Cu.import("resource://gre/modules/WbxmlPduHelper.jsm", WBXML);
13
14 // set to true to see debug messages
15 let DEBUG = WBXML.DEBUG_ALL | false;
16
17 /**
18 * Public identifier for SL
19 *
20 * @see http://technical.openmobilealliance.org/tech/omna/omna-wbxml-public-docid.aspx
21 */
22 const PUBLIC_IDENTIFIER_SL = "-//WAPFORUM//DTD SL 1.0//EN";
23
24 this.PduHelper = {
25
26 /**
27 * @param data
28 * A wrapped object containing raw PDU data.
29 * @param contentType
30 * Content type of incoming SL message, should be "text/vnd.wap.sl" or
31 * "application/vnd.wap.slc".
32 *
33 * @return A message object containing attribute content and contentType.
34 * |content| will contain string of decoded SL message if successfully
35 * decoded, or raw data if failed.
36 * |contentType| will be string representing corresponding type of
37 * content.
38 */
39 parse: function parse_sl(data, contentType) {
40 // We only need content and contentType
41 let msg = {
42 contentType: contentType
43 };
44
45 /**
46 * Message is compressed by WBXML, decode into string.
47 *
48 * @see WAP-192-WBXML-20010725-A
49 */
50 if (contentType === "application/vnd.wap.slc") {
51 let appToken = {
52 publicId: PUBLIC_IDENTIFIER_SL,
53 tagTokenList: SL_TAG_FIELDS,
54 attrTokenList: SL_ATTRIBUTE_FIELDS,
55 valueTokenList: SL_VALUE_FIELDS,
56 globalTokenOverride: null
57 }
58
59 try {
60 let parseResult = WBXML.PduHelper.parse(data, appToken);
61 msg.content = parseResult.content;
62 msg.contentType = "text/vnd.wap.sl";
63 } catch (e) {
64 // Provide raw data if we failed to parse.
65 msg.content = data.array;
66 }
67
68 return msg;
69 }
70
71 /**
72 * Message is plain text, transform raw to string.
73 */
74 try {
75 let stringData = WSP.Octet.decodeMultiple(data, data.array.length);
76 msg.content = WSP.PduHelper.decodeStringContent(stringData, "UTF-8");
77 } catch (e) {
78 // Provide raw data if we failed to parse.
79 msg.content = data.array;
80 }
81 return msg;
82
83 }
84 };
85
86 /**
87 * Tag tokens
88 *
89 * @see WAP-168-SERVICELOAD-20010731-A, clause 9.3.1
90 */
91 const SL_TAG_FIELDS = (function () {
92 let names = {};
93 function add(name, codepage, number) {
94 let entry = {
95 name: name,
96 number: number,
97 };
98 if (!names[codepage]) {
99 names[codepage] = {};
100 }
101 names[codepage][number] = entry;
102 }
103
104 add("sl", 0, 0x05);
105
106 return names;
107 })();
108
109 /**
110 * Attribute Tokens
111 *
112 * @see WAP-168-SERVICELOAD-20010731-A, clause 9.3.2
113 */
114 const SL_ATTRIBUTE_FIELDS = (function () {
115 let names = {};
116 function add(name, value, codepage, number) {
117 let entry = {
118 name: name,
119 value: value,
120 number: number,
121 };
122 if (!names[codepage]) {
123 names[codepage] = {};
124 }
125 names[codepage][number] = entry;
126 }
127
128 add("action", "execute-low", 0, 0x05);
129 add("action", "execute-high", 0, 0x06);
130 add("action", "cache", 0, 0x07);
131 add("href", "", 0, 0x08);
132 add("href", "http://", 0, 0x09);
133 add("href", "http://www.", 0, 0x0A);
134 add("href", "https://", 0, 0x0B);
135 add("href", "https://www.", 0, 0x0C);
136
137 return names;
138 })();
139
140 const SL_VALUE_FIELDS = (function () {
141 let names = {};
142 function add(value, codepage, number) {
143 let entry = {
144 value: value,
145 number: number,
146 };
147 if (!names[codepage]) {
148 names[codepage] = {};
149 }
150 names[codepage][number] = entry;
151 }
152
153 add(".com/", 0, 0x85);
154 add(".edu/", 0, 0x86);
155 add(".net/", 0, 0x87);
156 add(".org/", 0, 0x88);
157
158 return names;
159 })();
160
161 this.EXPORTED_SYMBOLS = [
162 // Parser
163 "PduHelper",
164 ];

mercurial