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: const PFS_NS = "http://www.mozilla.org/2004/pfs-rdf#"; michael@0: michael@0: function nsRDFItemUpdater(aClientOS, aChromeLocale) { michael@0: this._rdfService = Components.classes["@mozilla.org/rdf/rdf-service;1"] michael@0: .getService(Components.interfaces.nsIRDFService); michael@0: this._os = Components.classes["@mozilla.org/observer-service;1"] michael@0: .getService(Components.interfaces.nsIObserverService); michael@0: michael@0: var app = Components.classes["@mozilla.org/xre/app-info;1"] michael@0: .getService(Components.interfaces.nsIXULAppInfo); michael@0: this.appID = app.ID; michael@0: this.buildID = app.platformBuildID; michael@0: this.appRelease = app.version; michael@0: michael@0: this.clientOS = aClientOS; michael@0: this.chromeLocale = aChromeLocale; michael@0: michael@0: var prefBranch = Components.classes["@mozilla.org/preferences-service;1"] michael@0: .getService(Components.interfaces.nsIPrefBranch); michael@0: this.dsURI = prefBranch.getCharPref("pfs.datasource.url"); michael@0: } michael@0: michael@0: nsRDFItemUpdater.prototype = { michael@0: checkForPlugin: function (aPluginRequestItem) { michael@0: var dsURI = this.dsURI; michael@0: // escape the mimetype as mimetypes can contain '+', which will break pfs. michael@0: dsURI = dsURI.replace(/%PLUGIN_MIMETYPE%/g, encodeURIComponent(aPluginRequestItem.mimetype)); michael@0: dsURI = dsURI.replace(/%APP_ID%/g, this.appID); michael@0: dsURI = dsURI.replace(/%APP_VERSION%/g, this.buildID); michael@0: dsURI = dsURI.replace(/%APP_RELEASE%/g, this.appRelease); michael@0: dsURI = dsURI.replace(/%CLIENT_OS%/g, this.clientOS); michael@0: dsURI = dsURI.replace(/%CHROME_LOCALE%/g, this.chromeLocale); michael@0: michael@0: var ds = this._rdfService.GetDataSource(dsURI); michael@0: var rds = ds.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource) michael@0: if (rds.loaded) michael@0: this.onDatasourceLoaded(ds, aPluginRequestItem); michael@0: else { michael@0: var sink = ds.QueryInterface(Components.interfaces.nsIRDFXMLSink); michael@0: sink.addXMLSinkObserver(new nsPluginXMLRDFDSObserver(this, aPluginRequestItem)); michael@0: } michael@0: }, michael@0: michael@0: onDatasourceLoaded: function pfs_onDatasourceLoaded (aDatasource, aPluginRequestItem) { michael@0: var container = Components.classes["@mozilla.org/rdf/container;1"] michael@0: .createInstance(Components.interfaces.nsIRDFContainer); michael@0: var resultRes = this._rdfService.GetResource("urn:mozilla:plugin-results:" + aPluginRequestItem.mimetype); michael@0: var pluginList = aDatasource.GetTarget(resultRes, this._rdfService.GetResource(PFS_NS+"plugins"), true); michael@0: michael@0: var pluginInfo = null; michael@0: michael@0: try { michael@0: container.Init(aDatasource, pluginList); michael@0: michael@0: var children = container.GetElements(); michael@0: var target; michael@0: michael@0: // get the first item michael@0: var child = children.getNext(); michael@0: if (child instanceof Components.interfaces.nsIRDFResource) { michael@0: var name = this._rdfService.GetResource("http://www.mozilla.org/2004/pfs-rdf#updates"); michael@0: target = aDatasource.GetTarget(child, name, true); michael@0: } michael@0: michael@0: try { michael@0: container.Init(aDatasource, target); michael@0: target = null; michael@0: children = container.GetElements(); michael@0: michael@0: var child = children.getNext(); michael@0: if (child instanceof Components.interfaces.nsIRDFResource) { michael@0: target = child; michael@0: } michael@0: michael@0: var rdfs = this._rdfService; michael@0: michael@0: function getPFSValueFromRDF(aValue) { michael@0: var rv = null; michael@0: michael@0: var myTarget = aDatasource.GetTarget(target, rdfs.GetResource(PFS_NS + aValue), true); michael@0: if (myTarget) michael@0: rv = myTarget.QueryInterface(Components.interfaces.nsIRDFLiteral).Value; michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: pluginInfo = { michael@0: name: getPFSValueFromRDF("name"), michael@0: pid: getPFSValueFromRDF("guid"), michael@0: version: getPFSValueFromRDF("version"), michael@0: IconUrl: getPFSValueFromRDF("IconUrl"), michael@0: InstallerLocation: getPFSValueFromRDF("InstallerLocation"), michael@0: InstallerHash: getPFSValueFromRDF("InstallerHash"), michael@0: XPILocation: getPFSValueFromRDF("XPILocation"), michael@0: XPIHash: getPFSValueFromRDF("XPIHash"), michael@0: InstallerShowsUI: getPFSValueFromRDF("InstallerShowsUI"), michael@0: manualInstallationURL: getPFSValueFromRDF("manualInstallationURL"), michael@0: requestedMimetype: getPFSValueFromRDF("requestedMimetype"), michael@0: licenseURL: getPFSValueFromRDF("licenseURL"), michael@0: needsRestart: getPFSValueFromRDF("needsRestart") michael@0: }; michael@0: } michael@0: catch (ex) { michael@0: Components.utils.reportError(ex); michael@0: } michael@0: } michael@0: catch (ex) { michael@0: Components.utils.reportError(ex); michael@0: } michael@0: michael@0: gPluginInstaller.pluginInfoReceived(aPluginRequestItem, pluginInfo); michael@0: }, michael@0: michael@0: onDatasourceError: function pfs_onDatasourceError (aPluginRequestItem, aError) { michael@0: this._os.notifyObservers(aPluginRequestItem, "error", aError); michael@0: Components.utils.reportError(aError); michael@0: gPluginInstaller.pluginInfoReceived(aPluginRequestItem, null); michael@0: } michael@0: }; michael@0: michael@0: function nsPluginXMLRDFDSObserver(aUpdater, aPluginRequestItem) { michael@0: this._updater = aUpdater; michael@0: this._item = aPluginRequestItem; michael@0: } michael@0: michael@0: nsPluginXMLRDFDSObserver.prototype = michael@0: { michael@0: _updater : null, michael@0: _item : null, michael@0: michael@0: // nsIRDFXMLSinkObserver michael@0: onBeginLoad: function(aSink) {}, michael@0: onInterrupt: function(aSink) {}, michael@0: onResume: function(aSink) {}, michael@0: onEndLoad: function(aSink) { michael@0: aSink.removeXMLSinkObserver(this); michael@0: michael@0: var ds = aSink.QueryInterface(Components.interfaces.nsIRDFDataSource); michael@0: this._updater.onDatasourceLoaded(ds, this._item); michael@0: }, michael@0: michael@0: onError: function(aSink, aStatus, aErrorMsg) { michael@0: aSink.removeXMLSinkObserver(this); michael@0: this._updater.onDatasourceError(this._item, aStatus.toString()); michael@0: } michael@0: };