b2g/components/ContentHandler.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
michael@0 3
michael@0 4 "use strict";
michael@0 5
michael@0 6 const Cc = Components.classes;
michael@0 7 const Ci = Components.interfaces;
michael@0 8 const Cr = Components.results;
michael@0 9 const Cu = Components.utils;
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 12 Cu.import("resource://gre/modules/Services.jsm");
michael@0 13
michael@0 14 XPCOMUtils.defineLazyGetter(this, "cpmm", function() {
michael@0 15 return Cc["@mozilla.org/childprocessmessagemanager;1"]
michael@0 16 .getService(Ci.nsIMessageSender);
michael@0 17 });
michael@0 18
michael@0 19 function debug(aMsg) {
michael@0 20 //dump("--*-- ContentHandler: " + aMsg + "\n");
michael@0 21 }
michael@0 22
michael@0 23 const NS_ERROR_WONT_HANDLE_CONTENT = 0x805d0001;
michael@0 24
michael@0 25 let ActivityContentFactory = {
michael@0 26 createInstance: function createInstance(outer, iid) {
michael@0 27 if (outer != null) {
michael@0 28 throw Cr.NS_ERROR_NO_AGGREGATION;
michael@0 29 }
michael@0 30 return new ActivityContentHandler().QueryInterface(iid);
michael@0 31 },
michael@0 32
michael@0 33 QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory])
michael@0 34 }
michael@0 35
michael@0 36 function ActivityContentHandler() {
michael@0 37 }
michael@0 38
michael@0 39 ActivityContentHandler.prototype = {
michael@0 40 handleContent: function handleContent(aMimetype, aContext, aRequest) {
michael@0 41 if (!(aRequest instanceof Ci.nsIChannel))
michael@0 42 throw NS_ERROR_WONT_HANDLE_CONTENT;
michael@0 43
michael@0 44 let detail = {
michael@0 45 "type": aMimetype,
michael@0 46 "url": aRequest.URI.spec
michael@0 47 };
michael@0 48 cpmm.sendAsyncMessage("content-handler", detail);
michael@0 49
michael@0 50 aRequest.cancel(Cr.NS_BINDING_ABORTED);
michael@0 51 },
michael@0 52
michael@0 53 QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentHandler])
michael@0 54 }
michael@0 55
michael@0 56 function ContentHandler() {
michael@0 57 this.classIdMap = {};
michael@0 58 }
michael@0 59
michael@0 60 ContentHandler.prototype = {
michael@0 61 observe: function(aSubject, aTopic, aData) {
michael@0 62 if (aTopic == "app-startup") {
michael@0 63 // We only want to register these from content processes.
michael@0 64 let appInfo = Cc["@mozilla.org/xre/app-info;1"];
michael@0 65 if (appInfo.getService(Ci.nsIXULRuntime)
michael@0 66 .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
michael@0 67 return;
michael@0 68 }
michael@0 69 }
michael@0 70
michael@0 71 cpmm.addMessageListener("Activities:RegisterContentTypes", this);
michael@0 72 cpmm.addMessageListener("Activities:UnregisterContentTypes", this);
michael@0 73 cpmm.sendAsyncMessage("Activities:GetContentTypes", { });
michael@0 74 },
michael@0 75
michael@0 76 /**
michael@0 77 * Do the component registration for a content type.
michael@0 78 * We only need to register one component per content type, even if several
michael@0 79 * apps provide it, so we keep track of the number of providers for each
michael@0 80 * content type.
michael@0 81 */
michael@0 82 registerContentHandler: function registerContentHandler(aContentType) {
michael@0 83 debug("Registering " + aContentType);
michael@0 84
michael@0 85 // We already have a provider for this content type, just increase the
michael@0 86 // tracking count.
michael@0 87 if (this.classIdMap[aContentType]) {
michael@0 88 this.classIdMap[aContentType].count++;
michael@0 89 return;
michael@0 90 }
michael@0 91
michael@0 92 let contractID = "@mozilla.org/uriloader/content-handler;1?type=" +
michael@0 93 aContentType;
michael@0 94 let uuidGen = Cc["@mozilla.org/uuid-generator;1"]
michael@0 95 .getService(Ci.nsIUUIDGenerator);
michael@0 96 let id = Components.ID(uuidGen.generateUUID().toString());
michael@0 97 this.classIdMap[aContentType] = { count: 1, id: id };
michael@0 98 let cr = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
michael@0 99 cr.registerFactory(Components.ID(id), "Activity Content Handler", contractID,
michael@0 100 ActivityContentFactory);
michael@0 101 },
michael@0 102
michael@0 103 /**
michael@0 104 * Do the component unregistration for a content type.
michael@0 105 */
michael@0 106 unregisterContentHandler: function registerContentHandler(aContentType) {
michael@0 107 debug("Unregistering " + aContentType);
michael@0 108
michael@0 109 let record = this.classIdMap[aContentType];
michael@0 110 if (!record) {
michael@0 111 return;
michael@0 112 }
michael@0 113
michael@0 114 // Bail out if we still have providers left for this content type.
michael@0 115 if (--record.count > 0) {
michael@0 116 return;
michael@0 117 }
michael@0 118
michael@0 119 let contractID = "@mozilla.org/uriloader/content-handler;1?type=" +
michael@0 120 aContentType;
michael@0 121 let cr = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
michael@0 122 cr.unregisterFactory(record.id, ActivityContentFactory);
michael@0 123 delete this.classIdMap[aContentType]
michael@0 124 },
michael@0 125
michael@0 126 receiveMessage: function(aMessage) {
michael@0 127 let data = aMessage.data;
michael@0 128
michael@0 129 switch (aMessage.name) {
michael@0 130 case "Activities:RegisterContentTypes":
michael@0 131 data.contentTypes.forEach(this.registerContentHandler, this);
michael@0 132 break;
michael@0 133 case "Activities:UnregisterContentTypes":
michael@0 134 data.contentTypes.forEach(this.unregisterContentHandler, this);
michael@0 135 break;
michael@0 136 }
michael@0 137 },
michael@0 138
michael@0 139 classID: Components.ID("{d18d0216-d50c-11e1-ba54-efb18d0ef0ac}"),
michael@0 140 QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentHandler,
michael@0 141 Ci.nsIObserver,
michael@0 142 Ci.nsISupportsWeakReference])
michael@0 143 };
michael@0 144
michael@0 145 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentHandler]);

mercurial