michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "cpmm", function() { michael@0: return Cc["@mozilla.org/childprocessmessagemanager;1"] michael@0: .getService(Ci.nsIMessageSender); michael@0: }); michael@0: michael@0: function YoutubeProtocolHandler() { michael@0: } michael@0: michael@0: YoutubeProtocolHandler.prototype = { michael@0: classID: Components.ID("{c3f1b945-7e71-49c8-95c7-5ae9cc9e2bad}"), michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler]), michael@0: michael@0: scheme: "vnd.youtube", michael@0: defaultPort: -1, michael@0: protocolFlags: Ci.nsIProtocolHandler.URI_NORELATIVE | michael@0: Ci.nsIProtocolHandler.URI_NOAUTH | michael@0: Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE, michael@0: michael@0: // Sample URL: michael@0: // vnd.youtube:iNuKL2Gy_QM?vndapp=youtube_mobile&vndclient=mv-google&vndel=watch&vnddnc=1 michael@0: // Note that there is no hostname, so we use URLTYPE_NO_AUTHORITY michael@0: newURI: function yt_phNewURI(aSpec, aOriginCharset, aBaseURI) { michael@0: let uri = Cc["@mozilla.org/network/standard-url;1"] michael@0: .createInstance(Ci.nsIStandardURL); michael@0: uri.init(Ci.nsIStandardURL.URLTYPE_NO_AUTHORITY, this.defaultPort, michael@0: aSpec, aOriginCharset, aBaseURI); michael@0: return uri.QueryInterface(Ci.nsIURI); michael@0: }, michael@0: michael@0: newChannel: function yt_phNewChannel(aURI) { michael@0: /* michael@0: * This isn't a real protocol handler. Instead of creating a channel michael@0: * we just send a message and throw an exception. This 'content-handler' michael@0: * message is handled in b2g/chrome/content/shell.js where it starts michael@0: * an activity request that will open the Video app. The video app michael@0: * includes code to handle this fake 'video/youtube' mime type michael@0: */ michael@0: cpmm.sendAsyncMessage("content-handler", { michael@0: type: 'video/youtube', // A fake MIME type for the activity handler michael@0: url: aURI.spec // The path component of this URL is the video id michael@0: }); michael@0: michael@0: throw Components.results.NS_ERROR_ILLEGAL_VALUE; michael@0: }, michael@0: michael@0: allowPort: function yt_phAllowPort(aPort, aScheme) { michael@0: return false; michael@0: } michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([YoutubeProtocolHandler]);