dom/wappush/src/gonk/SlPduHelper.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/wappush/src/gonk/SlPduHelper.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,164 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
    1.11 +
    1.12 +let WSP = {};
    1.13 +Cu.import("resource://gre/modules/WspPduHelper.jsm", WSP);
    1.14 +let WBXML = {};
    1.15 +Cu.import("resource://gre/modules/WbxmlPduHelper.jsm", WBXML);
    1.16 +
    1.17 +// set to true to see debug messages
    1.18 +let DEBUG = WBXML.DEBUG_ALL | false;
    1.19 +
    1.20 +/**
    1.21 + * Public identifier for SL
    1.22 + *
    1.23 + * @see http://technical.openmobilealliance.org/tech/omna/omna-wbxml-public-docid.aspx
    1.24 + */
    1.25 +const PUBLIC_IDENTIFIER_SL    = "-//WAPFORUM//DTD SL 1.0//EN";
    1.26 +
    1.27 +this.PduHelper = {
    1.28 +
    1.29 +  /**
    1.30 +   * @param data
    1.31 +   *        A wrapped object containing raw PDU data.
    1.32 +   * @param contentType
    1.33 +   *        Content type of incoming SL message, should be "text/vnd.wap.sl" or
    1.34 +   *        "application/vnd.wap.slc".
    1.35 +   *
    1.36 +   * @return A message object containing attribute content and contentType.
    1.37 +   *         |content| will contain string of decoded SL message if successfully
    1.38 +   *         decoded, or raw data if failed.
    1.39 +   *         |contentType| will be string representing corresponding type of
    1.40 +   *         content.
    1.41 +   */
    1.42 +  parse: function parse_sl(data, contentType) {
    1.43 +    // We only need content and contentType
    1.44 +    let msg = {
    1.45 +      contentType: contentType
    1.46 +    };
    1.47 +
    1.48 +    /**
    1.49 +     * Message is compressed by WBXML, decode into string.
    1.50 +     *
    1.51 +     * @see WAP-192-WBXML-20010725-A
    1.52 +     */
    1.53 +    if (contentType === "application/vnd.wap.slc") {
    1.54 +      let appToken = {
    1.55 +        publicId: PUBLIC_IDENTIFIER_SL,
    1.56 +        tagTokenList: SL_TAG_FIELDS,
    1.57 +        attrTokenList: SL_ATTRIBUTE_FIELDS,
    1.58 +        valueTokenList: SL_VALUE_FIELDS,
    1.59 +        globalTokenOverride: null
    1.60 +      }
    1.61 +
    1.62 +      try {
    1.63 +        let parseResult = WBXML.PduHelper.parse(data, appToken);
    1.64 +        msg.content = parseResult.content;
    1.65 +        msg.contentType = "text/vnd.wap.sl";
    1.66 +      } catch (e) {
    1.67 +        // Provide raw data if we failed to parse.
    1.68 +        msg.content = data.array;
    1.69 +      }
    1.70 +
    1.71 +      return msg;
    1.72 +    }
    1.73 +
    1.74 +    /**
    1.75 +     * Message is plain text, transform raw to string.
    1.76 +     */
    1.77 +    try {
    1.78 +      let stringData = WSP.Octet.decodeMultiple(data, data.array.length);
    1.79 +      msg.content = WSP.PduHelper.decodeStringContent(stringData, "UTF-8");
    1.80 +    } catch (e) {
    1.81 +      // Provide raw data if we failed to parse.
    1.82 +      msg.content = data.array;
    1.83 +    }
    1.84 +    return msg;
    1.85 +
    1.86 +  }
    1.87 +};
    1.88 +
    1.89 +/**
    1.90 + * Tag tokens
    1.91 + *
    1.92 + * @see WAP-168-SERVICELOAD-20010731-A, clause 9.3.1
    1.93 + */
    1.94 +const SL_TAG_FIELDS = (function () {
    1.95 +  let names = {};
    1.96 +  function add(name, codepage, number) {
    1.97 +    let entry = {
    1.98 +      name: name,
    1.99 +      number: number,
   1.100 +    };
   1.101 +    if (!names[codepage]) {
   1.102 +      names[codepage] = {};
   1.103 +    }
   1.104 +    names[codepage][number] = entry;
   1.105 +  }
   1.106 +
   1.107 +  add("sl",       0,  0x05);
   1.108 +
   1.109 +  return names;
   1.110 +})();
   1.111 +
   1.112 +/**
   1.113 + * Attribute Tokens
   1.114 + *
   1.115 + * @see WAP-168-SERVICELOAD-20010731-A, clause 9.3.2
   1.116 + */
   1.117 +const SL_ATTRIBUTE_FIELDS = (function () {
   1.118 +  let names = {};
   1.119 +  function add(name, value, codepage, number) {
   1.120 +    let entry = {
   1.121 +      name: name,
   1.122 +      value: value,
   1.123 +      number: number,
   1.124 +    };
   1.125 +    if (!names[codepage]) {
   1.126 +      names[codepage] = {};
   1.127 +    }
   1.128 +    names[codepage][number] = entry;
   1.129 +  }
   1.130 +
   1.131 +  add("action",       "execute-low",    0,  0x05);
   1.132 +  add("action",       "execute-high",   0,  0x06);
   1.133 +  add("action",       "cache",          0,  0x07);
   1.134 +  add("href",         "",               0,  0x08);
   1.135 +  add("href",         "http://",        0,  0x09);
   1.136 +  add("href",         "http://www.",    0,  0x0A);
   1.137 +  add("href",         "https://",       0,  0x0B);
   1.138 +  add("href",         "https://www.",   0,  0x0C);
   1.139 +
   1.140 +  return names;
   1.141 +})();
   1.142 +
   1.143 +const SL_VALUE_FIELDS = (function () {
   1.144 +  let names = {};
   1.145 +  function add(value, codepage, number) {
   1.146 +    let entry = {
   1.147 +      value: value,
   1.148 +      number: number,
   1.149 +    };
   1.150 +    if (!names[codepage]) {
   1.151 +      names[codepage] = {};
   1.152 +    }
   1.153 +    names[codepage][number] = entry;
   1.154 +  }
   1.155 +
   1.156 +  add(".com/",      0,  0x85);
   1.157 +  add(".edu/",      0,  0x86);
   1.158 +  add(".net/",      0,  0x87);
   1.159 +  add(".org/",      0,  0x88);
   1.160 +
   1.161 +  return names;
   1.162 +})();
   1.163 +
   1.164 +this.EXPORTED_SYMBOLS = [
   1.165 +  // Parser
   1.166 +  "PduHelper",
   1.167 +];

mercurial