|
/** |
|
* Vimperator Plugin |
|
* Chrome特権を有するプロトコルをつくり、XUL等を開けるようにするプラグイン |
|
* @license MPL 1.1/GPL 2.0/LGPL 2.1 |
|
* |
|
* vimp-plugin://.... => $runtimePath/chrome/.... |
|
* |
|
* window.openDialog("vimp-plugin://test.xul", "", "chrome,all"); |
|
* |
|
*/ |
|
|
|
// ProtocolSheme |
|
var scheme = "vimp-plugin"; |
|
|
|
var rootFile = (function() { |
|
var dirs = io.getRuntimeDirectories("chrome"); |
|
var rootDir; |
|
if (dirs.length === 0) { |
|
rootDir = io.File(File.getPathsFromPathList(options.runtimepath)[0]); |
|
rootDir.append("chrome"); |
|
rootDir.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("0755", 8)); |
|
} else { |
|
rootDir = dirs[0]; |
|
} |
|
|
|
return rootDir; |
|
})(); |
|
|
|
|
|
var UUID = "{ab87dede-be05-462e-981d-22712d89e9df}"; |
|
var VimpPluginProtocolHandler; |
|
|
|
installProtocolHandler(rootFile); |
|
|
|
function installProtocolHandler (aRootFile) { |
|
|
|
var rootSpec = window.Services.io.newFileURI(aRootFile).spec; |
|
|
|
VimpPluginProtocolHandler = { |
|
scheme: scheme, |
|
defaultPort: -1, |
|
protocolFlags: Ci.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD | |
|
Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE | |
|
Ci.nsIProtocolHandler.URI_NORELATIVE | |
|
Ci.nsIProtocolHandler.URI_NOAUTH, |
|
get system_principal() { |
|
delete this.system_principal; |
|
return this.system_principal = Cc["@mozilla.org/scriptsecuritymanager;1"]. |
|
getService(Ci.nsIScriptSecurityManager). |
|
getSystemPrincipal(); |
|
}, |
|
newURI: function VPPH_newURI(aSpec, aOriginCharset, aBaseUri) { |
|
let uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI); |
|
uri.spec = aSpec; |
|
return uri; |
|
}, |
|
newChannel: function VPPH_newChannel(aURI) { |
|
let url = rootSpec + aURI.path.substr(2); |
|
let ch = window.Services.io.newChannel(url, null, null); |
|
ch.originalURI = aURI; |
|
ch.owner = this.system_principal; |
|
return ch; |
|
}, |
|
allowPort: function VPPH_allowPort(aPort, aScheme) { |
|
return false; |
|
}, |
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler]), |
|
classID: Components.ID(UUID), |
|
factory: { |
|
createInstance: function PC_createInstance(aOuter, aIID) { |
|
if (aOuter) { |
|
throw Components.results.NS_ERROR_NO_AGGREGATION; |
|
} |
|
return VimpPluginProtocolHandler.QueryInterface(aIID); |
|
}, |
|
lockFactory: function VPPH_lockFactory(aLock) { |
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED; |
|
}, |
|
QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIFactory]) |
|
}, |
|
}; |
|
|
|
try { |
|
var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); |
|
registrar.registerFactory(Components.ID(UUID), |
|
"VimpPluginProtocolHandler", |
|
"@mozilla.org/network/protocol;1?name=" + scheme, |
|
VimpPluginProtocolHandler.factory); |
|
} catch(e) { |
|
Cu.reportError(e); |
|
} |
|
} |
|
|
|
function uninstallProtocolHandler () { |
|
if ("@mozilla.org/network/protocol;1?name=" + scheme in Cc) { |
|
var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); |
|
registrar.unregisterFactory(Components.ID(UUID), |
|
VimpPluginProtocolHandler.factory); |
|
} |
|
} |
|
|
|
function onUnload () { |
|
uninstallProtocolHandler(); |
|
} |