1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/b2g/components/YoutubeProtocolHandler.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,64 @@ 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 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +const Cc = Components.classes; 1.11 +const Ci = Components.interfaces; 1.12 +const Cu = Components.utils; 1.13 + 1.14 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.15 +Cu.import("resource://gre/modules/Services.jsm"); 1.16 + 1.17 +XPCOMUtils.defineLazyGetter(this, "cpmm", function() { 1.18 + return Cc["@mozilla.org/childprocessmessagemanager;1"] 1.19 + .getService(Ci.nsIMessageSender); 1.20 +}); 1.21 + 1.22 +function YoutubeProtocolHandler() { 1.23 +} 1.24 + 1.25 +YoutubeProtocolHandler.prototype = { 1.26 + classID: Components.ID("{c3f1b945-7e71-49c8-95c7-5ae9cc9e2bad}"), 1.27 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler]), 1.28 + 1.29 + scheme: "vnd.youtube", 1.30 + defaultPort: -1, 1.31 + protocolFlags: Ci.nsIProtocolHandler.URI_NORELATIVE | 1.32 + Ci.nsIProtocolHandler.URI_NOAUTH | 1.33 + Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE, 1.34 + 1.35 + // Sample URL: 1.36 + // vnd.youtube:iNuKL2Gy_QM?vndapp=youtube_mobile&vndclient=mv-google&vndel=watch&vnddnc=1 1.37 + // Note that there is no hostname, so we use URLTYPE_NO_AUTHORITY 1.38 + newURI: function yt_phNewURI(aSpec, aOriginCharset, aBaseURI) { 1.39 + let uri = Cc["@mozilla.org/network/standard-url;1"] 1.40 + .createInstance(Ci.nsIStandardURL); 1.41 + uri.init(Ci.nsIStandardURL.URLTYPE_NO_AUTHORITY, this.defaultPort, 1.42 + aSpec, aOriginCharset, aBaseURI); 1.43 + return uri.QueryInterface(Ci.nsIURI); 1.44 + }, 1.45 + 1.46 + newChannel: function yt_phNewChannel(aURI) { 1.47 + /* 1.48 + * This isn't a real protocol handler. Instead of creating a channel 1.49 + * we just send a message and throw an exception. This 'content-handler' 1.50 + * message is handled in b2g/chrome/content/shell.js where it starts 1.51 + * an activity request that will open the Video app. The video app 1.52 + * includes code to handle this fake 'video/youtube' mime type 1.53 + */ 1.54 + cpmm.sendAsyncMessage("content-handler", { 1.55 + type: 'video/youtube', // A fake MIME type for the activity handler 1.56 + url: aURI.spec // The path component of this URL is the video id 1.57 + }); 1.58 + 1.59 + throw Components.results.NS_ERROR_ILLEGAL_VALUE; 1.60 + }, 1.61 + 1.62 + allowPort: function yt_phAllowPort(aPort, aScheme) { 1.63 + return false; 1.64 + } 1.65 +}; 1.66 + 1.67 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([YoutubeProtocolHandler]);