b2g/components/SmsProtocolHandler.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     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/. */
     5 /**
     6  * SmsProtocolHandle.js
     7  *
     8  * This file implements the URLs for SMS
     9  * https://www.rfc-editor.org/rfc/rfc5724.txt
    10  */
    12 "use strict";
    15 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
    17 Cu.import('resource://gre/modules/XPCOMUtils.jsm');
    18 Cu.import("resource:///modules/TelURIParser.jsm");
    20 XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
    21                                    "@mozilla.org/childprocessmessagemanager;1",
    22                                    "nsIMessageSender");
    24 function SmsProtocolHandler() {
    25 }
    27 SmsProtocolHandler.prototype = {
    29   scheme: "sms",
    30   defaultPort: -1,
    31   protocolFlags: Ci.nsIProtocolHandler.URI_NORELATIVE |
    32                  Ci.nsIProtocolHandler.URI_NOAUTH |
    33                  Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE |
    34                  Ci.nsIProtocolHandler.URI_DOES_NOT_RETURN_DATA,
    35   allowPort: function() false,
    37   newURI: function Proto_newURI(aSpec, aOriginCharset) {
    38     let uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
    39     uri.spec = aSpec;
    40     return uri;
    41   },
    43   newChannel: function Proto_newChannel(aURI) {
    44     let number = TelURIParser.parseURI('sms', aURI.spec);
    45     let body = "";
    46     let query = aURI.spec.split("?")[1];
    48     if (query) {
    49       let params = query.split("&");
    50       params.forEach(function(aParam) {
    51         let [name, value] = aParam.split("=");
    52         if (name === "body") {
    53           body = decodeURIComponent(value);
    54         }
    55       })
    56     }
    58     if (number || body) {
    59       cpmm.sendAsyncMessage("sms-handler", {
    60         number: number || "",
    61         type: "websms/sms",
    62         body: body });
    63     }
    65     throw Components.results.NS_ERROR_ILLEGAL_VALUE;
    66   },
    68   classID: Components.ID("{81ca20cb-0dad-4e32-8566-979c8998bd73}"),
    69   QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
    70 };
    72 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SmsProtocolHandler]);

mercurial