sbSongbirdProtocol.js
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 const Cc = Components.classes;
28 const Ci = Components.interfaces;
29 const Cr = Components.results;
30 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
31 
32 function sbSongbirdDispatch() {}
33 sbSongbirdDispatch.prototype = {
34  classDescription: "Nightingale ngale:// protocol command dispatch",
35  classID: Components.ID("2c15f30e-815f-4414-8d18-277d943a2f68"),
36  contractID: "@mozilla.org/network/protocol;1?name=ngale",
37 
38  newChannel: function(aUri) {
39  if (!(aUri.scheme == "ngale")) {
40  throw Components.Exception("URI " + aUri.spec + " not supported",
41  Cr.NS_ERROR_UNEXPECTED);
42  }
43  aUri.QueryInterface(Ci.nsIURL);
44 
45  // the URI is automatically changed by standard-url to look like this:
46  // ngale://open/?url=http://foo.com
47  // thus "host" will contain the command (if it exists)
48  if (aUri.host) {
49  switch(aUri.host) {
50  case "open":
51  var params = this._parseQueryString(aUri.query);
52  var ios = Cc["@mozilla.org/network/io-service;1"]
53  .getService(Ci.nsIIOService);
54 
55  // Errors can occur if the URL fails to parse, so in that case
56  // we quietly report to the error console.
57  try {
58  var channel = ios.newChannel(params["url"], null, null);
59  // setting the spec here makes the address bar
60  // look nice and correct.
61  aUri.spec = params["url"];
62  if (aUri.scheme == "chrome") {
63  Components.utils.reportError(
64  "sbSongbirdProtocol::newChannel: " +
65  "chrome URLs are not permitted as an argument to open.");
66  return new BogusChannel(aUri, "application/dummy-mime-type");
67  }
68  return channel;
69  }
70  catch (e) {
71  Components.utils.reportError(
72  "sbSongbirdProtocol::newChannel: " +
73  "failed to make a new channel/url for: " + aUri.query +
74  "\nThe syntax is: ngale:open?url=<encodedURL>");
75  return new BogusChannel(aUri, "application/dummy-mime-type");
76  }
77 
78  break; // pedantry
79  case "nocommand":
80  var origURISpec = aUri.spec.replace(/nocommand/, "");
81  Components.utils.reportError(
82  "sbSongbirdProtocol::newChannel: no command: " + origURISpec);
83  return new BogusChannel(aUri, "application/dummy-mime-type");
84  break;
85  default:
86  Components.utils.reportError(
87  "sbSongbirdProtocol::newChannel: bad command: " + aUri.host);
88  return new BogusChannel(aUri, "application/dummy-mime-type");
89  break;
90  }
91  } else {
92  Components.utils.reportError(
93  "sbSongbirdProtocol::newChannel: no command: " + aUri.spec);
94  return new BogusChannel(aUri, "application/dummy-mime-type");
95  }
96  },
97 
98  newURI: function (spec, charSet, baseURI) {
99  var url = Cc["@mozilla.org/network/standard-url;1"]
100  .createInstance(Ci.nsIURL);
101  url.spec = spec;
102  if (!url.host) {
103  // Returning a URL from newURI with no host causes XULRunner to crash.
104  // mozbug 478478
105  Components.utils.reportError("sbSongbirdProtocol::newURI: no command provided.\n"+
106  "Syntax is in the form: ngale:<command>?p1=a&p2=b");
107 
108  // Set up the host so that a valid URI can be returned that indicates that
109  // no command was provided.
110  url.host = "nocommand";
111  }
112  return url;
113  },
114 
115  getURIFlags: function(uri) {
116  return Ci.nsIAboutModule.ALLOW_SCRIPT
117  },
118 
119  // modified from filtersPage.js
120  _parseQueryString: function(query) {
121  var queryString = query.split("&");
122  var queryObject = {};
123  var key, value;
124  for each (var pair in queryString) {
125  [key, value] = pair.split("=");
126  queryObject[key] = unescape(value);
127  }
128  return queryObject;
129  },
130 
131  QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler])
132 };
133 
134 function NSGetModule(compMgr, fileSpec) {
135  return XPCOMUtils.generateModule([sbSongbirdDispatch]);
136 }
137 
138 /* We need to be able to throw NS_ERROR_NO_CONTENT to placate the channel
139  consumer so that it knows it doesn't have to do any work.
140  This BogusChannel is based on the Chatzilla BogusChannel in
141  chatzilla-service.js */
142 function BogusChannel(URI, contentType)
143 {
144  this.URI = URI;
145  this.originalURI = URI;
146  this.contentType = contentType;
147 }
148 
149 BogusChannel.prototype.QueryInterface =
150 function bc_QueryInterface(iid)
151 {
152  if (!iid.equals(nsIChannel) && !iid.equals(nsIRequest) &&
153  !iid.equals(nsISupports))
154  throw Components.results.NS_ERROR_NO_INTERFACE;
155 
156  return this;
157 }
158 
159 BogusChannel.prototype.loadAttributes = null;
160 BogusChannel.prototype.contentLength = 0;
161 BogusChannel.prototype.owner = null;
162 BogusChannel.prototype.loadGroup = null;
163 BogusChannel.prototype.notificationCallbacks = null;
164 BogusChannel.prototype.securityInfo = null;
165 
166 BogusChannel.prototype.open =
167 BogusChannel.prototype.asyncOpen =
168 function bc_open(observer, ctxt)
169 {
170  throw Components.results.NS_ERROR_NO_CONTENT;
171 }
172 
173 BogusChannel.prototype.asyncRead =
174 function bc_asyncRead(listener, ctxt)
175 {
176  throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
177 }
178 
179 /* nsIRequest */
180 BogusChannel.prototype.isPending =
181 function bc_isPending()
182 {
183  return true;
184 }
185 
186 BogusChannel.prototype.status = Components.results.NS_OK;
187 
188 BogusChannel.prototype.cancel =
189 function bc_cancel(status)
190 {
191  this.status = status;
192 }
193 
194 BogusChannel.prototype.suspend =
195 BogusChannel.prototype.resume =
196 function bc_suspres()
197 {
198  throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
199 }
200 
const Cc
sbDeviceFirmwareAutoCheckForUpdate prototype contractID
function NSGetModule(compMgr, fileSpec)
sbDeviceFirmwareAutoCheckForUpdate prototype classDescription
const Ci
BogusChannel prototype QueryInterface
const nsIChannel
function BogusChannel(URI, contentType)
return null
Definition: FeedWriter.js:1143
function newURI(aURLString)
var uri
Definition: FeedWriter.js:1135
function url(spec)
countRef value
Definition: FeedWriter.js:1423
sbDeviceFirmwareAutoCheckForUpdate prototype classID
var ios
Definition: head_feeds.js:5
let observer
function sbSongbirdDispatch()
const Cr