Register/unregister the protocol handlers automatically based on the preferences (can be tested in about:config).

Added handlers for the protocols that the phone understands.
This commit is contained in:
amla70
2010-08-01 18:33:04 +00:00
parent b829d718ea
commit 7f3f413fcb
7 changed files with 160 additions and 99 deletions

View File

@@ -14,5 +14,3 @@ locale sendtophone zh chrome/locale/zh/
overlay chrome://browser/content/browser.xul chrome://sendtophone/content/ff-overlay.xul
style chrome://global/content/customizeToolbar.xul chrome://sendtophone/skin/overlay.css
component {751de080-95d1-11df-981c-0800200c9a66} components/protocolHandler.js
contract @mozilla.org/network/protocol;1?name=market {751de080-95d1-11df-981c-0800200c9a66}

View File

@@ -23,8 +23,8 @@ sendtophone.init = function()
this.prefs.setBoolPref( "installedButton", true ) ;
}
document.getElementById("contentAreaContextMenu")
.addEventListener("popupshowing", function (e){ sendtophone.showFirefoxContextMenu(e); }, false);
document.getElementById("contentAreaContextMenu")
.addEventListener("popupshowing", function (e){ sendtophone.showFirefoxContextMenu(e); }, false);
}
sendtophone.installToolbarButton = function()

View File

@@ -17,6 +17,8 @@
// Core functions
Components.utils.import("resource://sendtophone/sendtophone.js");
// Protocol handlers
Components.utils.import("resource://sendtophone/protocolHandlers.js");
var sendtophone = {
@@ -32,7 +34,7 @@ var sendtophone = {
me.strings = document.getElementById("sendtophone-strings");
me.prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getService(Ci.nsIPrefService)
.getBranch("extensions.sendtophone.") ;
me.init();
@@ -67,34 +69,34 @@ var sendtophone = {
break;
}
if ((/^(https?|market|tel|sms|ftp):/i).test( info.url ))
if ((/^(https?|market|tel|sms|ftp):/i).test( url ))
{
var max_length = 1024;
if (selection.length > max_length)
selection = selection.substring(0, max_length);
sendtophoneCore.send(title, url, selection);
}
}
else
{
this.alert(this.strings.getString("InvalidScheme"));
}
}
},
},
// Shows a message in a modal alert
alert: function(text)
{
var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Components.interfaces.nsIPromptService);
promptService.alert(window, this.strings.getString("SendToPhoneTitle"),
text);
var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Ci.nsIPromptService);
promptService.alert(window, this.strings.getString("SendToPhoneTitle"),
text);
},
onToolbarButtonCommand: function(e) {
// just reuse the function above.
sendtophone.onMenuItemCommand(e, 'page');
},
onToolbarButtonCommand: function(e) {
// just reuse the function above.
sendtophone.onMenuItemCommand(e, 'page');
},
getInfo: function() {
var doc = gBrowser.contentDocument,

View File

@@ -1,74 +0,0 @@
// components used in this file
const NS_PREFSERVICE_CONTRACTID = "@mozilla.org/preferences-service;1";
const URI_CONTRACTID = "@mozilla.org/network/simple-uri;1";
const INPUTSTREAMCHANNEL_CONTRACTID = "@mozilla.org/network/input-stream-channel;1";
// interfaces used in this file
const nsIProtocolHandler = Components.interfaces.nsIProtocolHandler;
const nsIURI = Components.interfaces.nsIURI;
const nsIPrefService = Components.interfaces.nsIPrefService;
const nsIChannel = Components.interfaces.nsIChannel;
const nsIContentPolicy = Components.interfaces.nsIContentPolicy;
// some misc. constants
const PREF_BRANCH = "extensions.sendtophone.";
const alert = Components.classes['@mozilla.org/alerts-service;1']
.getService(Components.interfaces.nsIAlertsService)
.showAlertNotification;
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
function SendToPhoneComponent(scheme) {
this.scheme = scheme;
}
SendToPhoneComponent.prototype = {
classDescription: "XPCOM component to handle protocols in SendToPhone",
// this must match whatever is in chrome.manifest!
classID: Components.ID("{751de080-95d1-11df-981c-0800200c9a66}"),
contractID: "@mozilla.org/network/protocol;1?name=market",
QueryInterface: XPCOMUtils.generateQI([nsIProtocolHandler, nsIContentPolicy]),
// nsIProtocolHandler implementation:
// attribute defaults
protocolFlags : nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
newURI : function(aSpec, aCharset, aBaseURI)
{
var uri = Components.classes[URI_CONTRACTID].createInstance(nsIURI);
uri.spec = aSpec;
return uri;
},
newChannel : function(aURI)
{
var myURI = decodeURI(aURI.spec);
// Core functions
if (typeof sendtophoneCore == "undefined")
Components.utils.import("resource://sendtophone/sendtophone.js");
sendtophoneCore.send("Market link", myURI, "")
alert(null, "test", myURI);
// return a fake empty channel so current window doesn't change
return Components.classes[INPUTSTREAMCHANNEL_CONTRACTID].createInstance(nsIChannel);
}
};
// The following line is what XPCOM uses to create components. Each component prototype
// must have a .classID which is used to create it.
/**
* XPCOMUtils.generateNSGetFactory was introduced in Mozilla 2 (Firefox 4).
* XPCOMUtils.generateNSGetModule is for Mozilla 1.9.2 (Firefox 3.6).
*/
if (XPCOMUtils.generateNSGetFactory)
var NSGetFactory = XPCOMUtils.generateNSGetFactory([SendToPhoneComponent]);
else
var NSGetModule = XPCOMUtils.generateNSGetModule([SendToPhoneComponent]);

View File

@@ -3,7 +3,12 @@ pref("extensions.sendtophone.appUrl", "https://chrometophone.appspot.com");
pref("extensions.sendtophone.proxyUrl", "http://smallroomstudios.net/s2p.php?ml=");
pref("extensions.sendtophone.protocols", "market");
pref("extensions.sendtophone.protocols.market", true);
pref("extensions.sendtophone.protocols.sms", true);
pref("extensions.sendtophone.protocols.smsto", true);
pref("extensions.sendtophone.protocols.mms", true);
pref("extensions.sendtophone.protocols.mmsto", true);
pref("extensions.sendtophone.protocols.tel", true);
// https://developer.mozilla.org/en/Localizing_extension_descriptions
pref("extensions.sendtophone@martinezdelizarrondo.com.description", "chrome://sendtophone/locale/overlay.properties");

View File

@@ -0,0 +1,132 @@
/* This js module doesn't export anything, it's meant to handle the protocol registration/unregistration*/
var EXPORTED_SYMBOLS = [];
const Cc = Components.classes;
const Ci = Components.interfaces;
// Utility function to handle the preferences
// https://developer.mozilla.org/en/Code_snippets/Preferences
function PrefListener(branchName, func) {
var prefService = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService);
var branch = prefService.getBranch(branchName);
branch.QueryInterface(Components.interfaces.nsIPrefBranch2);
this.register = function() {
branch.addObserver("", this, false);
branch.getChildList("", { })
.forEach(function (name) { func(branch, name); });
};
this.unregister = function unregister() {
if (branch)
branch.removeObserver("", this);
};
this.observe = function(subject, topic, data) {
if (topic == "nsPref:changed")
func(branch, data);
};
}
// our XPCOM components to handle the protocols
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
function SendToPhone_ProtocolWrapper( properties )
{
var myHandler = function() {};
myHandler.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler]),
_xpcom_factory: {
singleton: null,
createInstance: function (aOuter, aIID) {
if (aOuter) throw Components.results.NS_ERROR_NO_AGGREGATION;
if (!this.singleton) this.singleton = new myHandler();
return this.singleton.QueryInterface(aIID);
}
},
// nsIProtocolHandler implementation:
// default attributes
protocolFlags : Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
defaultPort : -1,
newURI : function(aSpec, aCharset, aBaseURI)
{
var uri = Components.classes["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
uri.spec = aSpec;
return uri;
},
newChannel : function(aURI)
{
var myURI = decodeURI(aURI.spec);
// Core functions
if (typeof sendtophoneCore == "undefined")
Components.utils.import("resource://sendtophone/sendtophone.js");
sendtophoneCore.send(this.linkTitle, myURI, "")
// return a fake empty channel so current window doesn't change
return Components.classes[ "@mozilla.org/network/input-stream-channel;1" ].createInstance(Ci.nsIChannel);
},
scheme : properties.scheme,
classDescription : "SendToPhone handler for " + properties.scheme,
classID : Components.ID( properties.ID ),
contractID : "@mozilla.org/network/protocol;1?name=" + properties.scheme,
linkTitle : properties.scheme + " link" // fixme, needs translations...
}
return myHandler;
}
// This function takes care of register/unregister the protocol handlers as requested
// It's called when the preferences change.
function toggleProtocolHandler(protocol, register)
{
// Retrieve the object for that protocol
var marketHandler = myProtocols[protocol];
var manager = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
var proto = marketHandler.prototype;
if (register)
{
if (!marketHandler.registered)
manager.registerFactory(proto.classID,
proto.classDescription,
proto.contractID,
proto._xpcom_factory);
marketHandler.registered = true;
}
else
{
if (marketHandler.registered)
manager.unregisterFactory(proto.classID, proto._xpcom_factory);
marketHandler.registered = false;
}
}
// Each protocol handler
var myProtocols = {
market: SendToPhone_ProtocolWrapper( { scheme: "market", ID: "{751de080-95d1-11df-981c-0800200c9a66}" } ) ,
sms: SendToPhone_ProtocolWrapper( { scheme: "sms", ID: "{345de080-95d1-11df-981c-0800200c9a66}" } ) ,
smsto: SendToPhone_ProtocolWrapper( { scheme: "smsto", ID: "{854de080-95d1-11df-981c-0800200c9a66}" } ) ,
mms: SendToPhone_ProtocolWrapper( { scheme: "mms", ID: "{457de080-95d1-11df-981c-0800200c9a66}" } ) ,
mmsto: SendToPhone_ProtocolWrapper( { scheme: "mmsto", ID: "{331de080-95d1-11df-981c-0800200c9a66}" } ) ,
tel: SendToPhone_ProtocolWrapper( { scheme: "tel", ID: "{948de080-95d1-11df-981c-0800200c9a66}" } )
};
// Listen for changes in the preferences and register the protocols as needed.
var preferencesListener = new PrefListener("extensions.sendtophone.protocols.",
function(branch, name) {
toggleProtocolHandler(name, branch.getBoolPref(name));
});
preferencesListener.register();

View File

@@ -153,18 +153,16 @@ var sendtophoneCore = {
this.init();
// Send the protocols that aren't currently whitelisted through a proxy server
if ((/^(market|tel|sms|ftp):/i).test( url ))
{
var encodedUri = encodeURIComponent( url);
if (!(/^(https?):/i).test( url ))
{
var prefs = Cc["@mozilla.org/preferences-service;1"]
.getService(Ci.nsIPrefService)
.getBranch("extensions.sendtophone.") ;
// Rewrite the URI so it's send first to the proxy
var proxyUrl = prefs.getCharPref( "proxyUrl" ) ;
// rewrite the URI so it's send first to the proxy
url = proxyUrl + encodedUri;
if (proxyUrl)
url = proxyUrl + encodeURIComponent( url);
}
var data = 'title=' + encodeURIComponent(title) +