b2g/components/SmsProtocolHandler.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/b2g/components/SmsProtocolHandler.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,72 @@
     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 +/**
     1.9 + * SmsProtocolHandle.js
    1.10 + *
    1.11 + * This file implements the URLs for SMS
    1.12 + * https://www.rfc-editor.org/rfc/rfc5724.txt
    1.13 + */
    1.14 +
    1.15 +"use strict";
    1.16 +
    1.17 +
    1.18 +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
    1.19 +
    1.20 +Cu.import('resource://gre/modules/XPCOMUtils.jsm');
    1.21 +Cu.import("resource:///modules/TelURIParser.jsm");
    1.22 +
    1.23 +XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
    1.24 +                                   "@mozilla.org/childprocessmessagemanager;1",
    1.25 +                                   "nsIMessageSender");
    1.26 +
    1.27 +function SmsProtocolHandler() {
    1.28 +}
    1.29 +
    1.30 +SmsProtocolHandler.prototype = {
    1.31 +
    1.32 +  scheme: "sms",
    1.33 +  defaultPort: -1,
    1.34 +  protocolFlags: Ci.nsIProtocolHandler.URI_NORELATIVE |
    1.35 +                 Ci.nsIProtocolHandler.URI_NOAUTH |
    1.36 +                 Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE |
    1.37 +                 Ci.nsIProtocolHandler.URI_DOES_NOT_RETURN_DATA,
    1.38 +  allowPort: function() false,
    1.39 +
    1.40 +  newURI: function Proto_newURI(aSpec, aOriginCharset) {
    1.41 +    let uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
    1.42 +    uri.spec = aSpec;
    1.43 +    return uri;
    1.44 +  },
    1.45 +
    1.46 +  newChannel: function Proto_newChannel(aURI) {
    1.47 +    let number = TelURIParser.parseURI('sms', aURI.spec);
    1.48 +    let body = "";
    1.49 +    let query = aURI.spec.split("?")[1];
    1.50 +
    1.51 +    if (query) {
    1.52 +      let params = query.split("&");
    1.53 +      params.forEach(function(aParam) {
    1.54 +        let [name, value] = aParam.split("=");
    1.55 +        if (name === "body") {
    1.56 +          body = decodeURIComponent(value);
    1.57 +        }
    1.58 +      })
    1.59 +    }
    1.60 +
    1.61 +    if (number || body) {
    1.62 +      cpmm.sendAsyncMessage("sms-handler", {
    1.63 +        number: number || "",
    1.64 +        type: "websms/sms",
    1.65 +        body: body });
    1.66 +    }
    1.67 +
    1.68 +    throw Components.results.NS_ERROR_ILLEGAL_VALUE;
    1.69 +  },
    1.70 +
    1.71 +  classID: Components.ID("{81ca20cb-0dad-4e32-8566-979c8998bd73}"),
    1.72 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
    1.73 +};
    1.74 +
    1.75 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SmsProtocolHandler]);

mercurial