1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/webapps/WebappOSUtils.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,468 @@ 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 +const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu, Constructor: CC } = Components; 1.9 + 1.10 +Cu.import("resource://gre/modules/Services.jsm"); 1.11 +Cu.import("resource://gre/modules/FileUtils.jsm"); 1.12 +Cu.import("resource://gre/modules/osfile.jsm"); 1.13 +Cu.import("resource://gre/modules/Promise.jsm"); 1.14 + 1.15 +this.EXPORTED_SYMBOLS = ["WebappOSUtils"]; 1.16 + 1.17 +// Returns the MD5 hash of a string. 1.18 +function computeHash(aString) { 1.19 + let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]. 1.20 + createInstance(Ci.nsIScriptableUnicodeConverter); 1.21 + converter.charset = "UTF-8"; 1.22 + let result = {}; 1.23 + // Data is an array of bytes. 1.24 + let data = converter.convertToByteArray(aString, result); 1.25 + 1.26 + let hasher = Cc["@mozilla.org/security/hash;1"]. 1.27 + createInstance(Ci.nsICryptoHash); 1.28 + hasher.init(hasher.MD5); 1.29 + hasher.update(data, data.length); 1.30 + // We're passing false to get the binary hash and not base64. 1.31 + let hash = hasher.finish(false); 1.32 + 1.33 + function toHexString(charCode) { 1.34 + return ("0" + charCode.toString(16)).slice(-2); 1.35 + } 1.36 + 1.37 + // Convert the binary hash data to a hex string. 1.38 + return [toHexString(hash.charCodeAt(i)) for (i in hash)].join(""); 1.39 +} 1.40 + 1.41 +this.WebappOSUtils = { 1.42 + getUniqueName: function(aApp) { 1.43 + return this.sanitizeStringForFilename(aApp.name).toLowerCase() + "-" + 1.44 + computeHash(aApp.manifestURL); 1.45 + }, 1.46 + 1.47 +#ifdef XP_WIN 1.48 + /** 1.49 + * Returns the registry key associated to the given app and a boolean that 1.50 + * specifies whether we're using the old naming scheme or the new one. 1.51 + */ 1.52 + getAppRegKey: function(aApp) { 1.53 + let regKey = Cc["@mozilla.org/windows-registry-key;1"]. 1.54 + createInstance(Ci.nsIWindowsRegKey); 1.55 + 1.56 + try { 1.57 + regKey.open(Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, 1.58 + "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + 1.59 + this.getUniqueName(aApp), Ci.nsIWindowsRegKey.ACCESS_READ); 1.60 + 1.61 + return { value: regKey, 1.62 + namingSchemeVersion: 2}; 1.63 + } catch (ex) {} 1.64 + 1.65 + // Fall back to the old installation naming scheme 1.66 + try { 1.67 + regKey.open(Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, 1.68 + "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + 1.69 + aApp.origin, Ci.nsIWindowsRegKey.ACCESS_READ); 1.70 + 1.71 + return { value: regKey, 1.72 + namingSchemeVersion: 1 }; 1.73 + } catch (ex) {} 1.74 + 1.75 + return null; 1.76 + }, 1.77 +#endif 1.78 + 1.79 + /** 1.80 + * Returns the executable of the given app, identifying it by its unique name, 1.81 + * which is in either the new format or the old format. 1.82 + * On Mac OS X, it returns the identifier of the app. 1.83 + * 1.84 + * The new format ensures a readable and unique name for an app by combining 1.85 + * its name with a hash of its manifest URL. The old format uses its origin, 1.86 + * which is only unique until we support multiple apps per origin. 1.87 + */ 1.88 + getLaunchTarget: function(aApp) { 1.89 +#ifdef XP_WIN 1.90 + let appRegKey = this.getAppRegKey(aApp); 1.91 + 1.92 + if (!appRegKey) { 1.93 + return null; 1.94 + } 1.95 + 1.96 + let appFilename, installLocation; 1.97 + try { 1.98 + appFilename = appRegKey.value.readStringValue("AppFilename"); 1.99 + installLocation = appRegKey.value.readStringValue("InstallLocation"); 1.100 + } catch (ex) { 1.101 + return null; 1.102 + } finally { 1.103 + appRegKey.value.close(); 1.104 + } 1.105 + 1.106 + installLocation = installLocation.substring(1, installLocation.length - 1); 1.107 + 1.108 + if (appRegKey.namingSchemeVersion == 1 && 1.109 + !this.isOldInstallPathValid(aApp, installLocation)) { 1.110 + return null; 1.111 + } 1.112 + 1.113 + let initWithPath = CC("@mozilla.org/file/local;1", 1.114 + "nsILocalFile", "initWithPath"); 1.115 + let launchTarget = initWithPath(installLocation); 1.116 + launchTarget.append(appFilename + ".exe"); 1.117 + 1.118 + return launchTarget; 1.119 +#elifdef XP_MACOSX 1.120 + let uniqueName = this.getUniqueName(aApp); 1.121 + 1.122 + let mwaUtils = Cc["@mozilla.org/widget/mac-web-app-utils;1"]. 1.123 + createInstance(Ci.nsIMacWebAppUtils); 1.124 + 1.125 + try { 1.126 + let path; 1.127 + if (path = mwaUtils.pathForAppWithIdentifier(uniqueName)) { 1.128 + return [ uniqueName, path ]; 1.129 + } 1.130 + } catch(ex) {} 1.131 + 1.132 + // Fall back to the old installation naming scheme 1.133 + try { 1.134 + let path; 1.135 + if ((path = mwaUtils.pathForAppWithIdentifier(aApp.origin)) && 1.136 + this.isOldInstallPathValid(aApp, path)) { 1.137 + return [ aApp.origin, path ]; 1.138 + } 1.139 + } catch(ex) {} 1.140 + 1.141 + return [ null, null ]; 1.142 +#elifdef XP_UNIX 1.143 + let uniqueName = this.getUniqueName(aApp); 1.144 + 1.145 + let exeFile = Services.dirsvc.get("Home", Ci.nsIFile); 1.146 + exeFile.append("." + uniqueName); 1.147 + exeFile.append("webapprt-stub"); 1.148 + 1.149 + // Fall back to the old installation naming scheme 1.150 + if (!exeFile.exists()) { 1.151 + exeFile = Services.dirsvc.get("Home", Ci.nsIFile); 1.152 + 1.153 + let origin = Services.io.newURI(aApp.origin, null, null); 1.154 + let installDir = "." + origin.scheme + ";" + 1.155 + origin.host + 1.156 + (origin.port != -1 ? ";" + origin.port : ""); 1.157 + 1.158 + exeFile.append(installDir); 1.159 + exeFile.append("webapprt-stub"); 1.160 + 1.161 + if (!exeFile.exists() || 1.162 + !this.isOldInstallPathValid(aApp, exeFile.parent.path)) { 1.163 + return null; 1.164 + } 1.165 + } 1.166 + 1.167 + return exeFile; 1.168 +#endif 1.169 + }, 1.170 + 1.171 + getInstallPath: function(aApp) { 1.172 +#ifdef MOZ_B2G 1.173 + // All b2g builds 1.174 + return aApp.basePath + "/" + aApp.id; 1.175 + 1.176 +#elifdef MOZ_FENNEC 1.177 + // All fennec 1.178 + return aApp.basePath + "/" + aApp.id; 1.179 + 1.180 +#elifdef MOZ_PHOENIX 1.181 + // Firefox 1.182 + 1.183 +#ifdef XP_WIN 1.184 + let execFile = this.getLaunchTarget(aApp); 1.185 + if (!execFile) { 1.186 + return null; 1.187 + } 1.188 + 1.189 + return execFile.parent.path; 1.190 +#elifdef XP_MACOSX 1.191 + let [ bundleID, path ] = this.getLaunchTarget(aApp); 1.192 + return path; 1.193 +#elifdef XP_UNIX 1.194 + let execFile = this.getLaunchTarget(aApp); 1.195 + if (!execFile) { 1.196 + return null; 1.197 + } 1.198 + 1.199 + return execFile.parent.path; 1.200 +#endif 1.201 + 1.202 +#elifdef MOZ_WEBAPP_RUNTIME 1.203 + // Webapp runtime 1.204 + 1.205 +#ifdef XP_WIN 1.206 + let execFile = this.getLaunchTarget(aApp); 1.207 + if (!execFile) { 1.208 + return null; 1.209 + } 1.210 + 1.211 + return execFile.parent.path; 1.212 +#elifdef XP_MACOSX 1.213 + let [ bundleID, path ] = this.getLaunchTarget(aApp); 1.214 + return path; 1.215 +#elifdef XP_UNIX 1.216 + let execFile = this.getLaunchTarget(aApp); 1.217 + if (!execFile) { 1.218 + return null; 1.219 + } 1.220 + 1.221 + return execFile.parent.path; 1.222 +#endif 1.223 + 1.224 +#endif 1.225 + // Anything unsupported, like Metro 1.226 + throw new Error("Unsupported apps platform"); 1.227 + }, 1.228 + 1.229 + getPackagePath: function(aApp) { 1.230 + let packagePath = this.getInstallPath(aApp); 1.231 + 1.232 + // Only for Firefox on Mac OS X 1.233 +#ifndef MOZ_B2G 1.234 +#ifdef XP_MACOSX 1.235 + packagePath = OS.Path.join(packagePath, "Contents", "Resources"); 1.236 +#endif 1.237 +#endif 1.238 + 1.239 + return packagePath; 1.240 + }, 1.241 + 1.242 + launch: function(aApp) { 1.243 + let uniqueName = this.getUniqueName(aApp); 1.244 + 1.245 +#ifdef XP_WIN 1.246 + let launchTarget = this.getLaunchTarget(aApp); 1.247 + if (!launchTarget) { 1.248 + return false; 1.249 + } 1.250 + 1.251 + try { 1.252 + let process = Cc["@mozilla.org/process/util;1"]. 1.253 + createInstance(Ci.nsIProcess); 1.254 + 1.255 + process.init(launchTarget); 1.256 + process.runwAsync([], 0); 1.257 + } catch (e) { 1.258 + return false; 1.259 + } 1.260 + 1.261 + return true; 1.262 +#elifdef XP_MACOSX 1.263 + let [ launchIdentifier, path ] = this.getLaunchTarget(aApp); 1.264 + if (!launchIdentifier) { 1.265 + return false; 1.266 + } 1.267 + 1.268 + let mwaUtils = Cc["@mozilla.org/widget/mac-web-app-utils;1"]. 1.269 + createInstance(Ci.nsIMacWebAppUtils); 1.270 + 1.271 + try { 1.272 + mwaUtils.launchAppWithIdentifier(launchIdentifier); 1.273 + } catch(e) { 1.274 + return false; 1.275 + } 1.276 + 1.277 + return true; 1.278 +#elifdef XP_UNIX 1.279 + let exeFile = this.getLaunchTarget(aApp); 1.280 + if (!exeFile) { 1.281 + return false; 1.282 + } 1.283 + 1.284 + try { 1.285 + let process = Cc["@mozilla.org/process/util;1"] 1.286 + .createInstance(Ci.nsIProcess); 1.287 + 1.288 + process.init(exeFile); 1.289 + process.runAsync([], 0); 1.290 + } catch (e) { 1.291 + return false; 1.292 + } 1.293 + 1.294 + return true; 1.295 +#endif 1.296 + }, 1.297 + 1.298 + uninstall: function(aApp) { 1.299 +#ifdef XP_WIN 1.300 + let appRegKey = this.getAppRegKey(aApp); 1.301 + 1.302 + if (!appRegKey) { 1.303 + return Promise.reject("App registry key not found"); 1.304 + } 1.305 + 1.306 + let deferred = Promise.defer(); 1.307 + 1.308 + try { 1.309 + let uninstallerPath = appRegKey.value.readStringValue("UninstallString"); 1.310 + uninstallerPath = uninstallerPath.substring(1, uninstallerPath.length - 1); 1.311 + 1.312 + let uninstaller = Cc["@mozilla.org/file/local;1"]. 1.313 + createInstance(Ci.nsIFile); 1.314 + uninstaller.initWithPath(uninstallerPath); 1.315 + 1.316 + let process = Cc["@mozilla.org/process/util;1"]. 1.317 + createInstance(Ci.nsIProcess); 1.318 + process.init(uninstaller); 1.319 + process.runwAsync(["/S"], 1, (aSubject, aTopic) => { 1.320 + if (aTopic == "process-finished") { 1.321 + deferred.resolve(true); 1.322 + } else { 1.323 + deferred.reject("Uninstaller failed with exit code: " + aSubject.exitValue); 1.324 + } 1.325 + }); 1.326 + } catch (e) { 1.327 + deferred.reject(e); 1.328 + } finally { 1.329 + appRegKey.value.close(); 1.330 + } 1.331 + 1.332 + return deferred.promise; 1.333 +#elifdef XP_MACOSX 1.334 + let [ , path ] = this.getLaunchTarget(aApp); 1.335 + if (!path) { 1.336 + return Promise.reject("App not found"); 1.337 + } 1.338 + 1.339 + let deferred = Promise.defer(); 1.340 + 1.341 + let mwaUtils = Cc["@mozilla.org/widget/mac-web-app-utils;1"]. 1.342 + createInstance(Ci.nsIMacWebAppUtils); 1.343 + 1.344 + mwaUtils.trashApp(path, (aResult) => { 1.345 + if (aResult == Cr.NS_OK) { 1.346 + deferred.resolve(true); 1.347 + } else { 1.348 + deferred.resolve("Error moving the app to the Trash: " + aResult); 1.349 + } 1.350 + }); 1.351 + 1.352 + return deferred.promise; 1.353 +#elifdef XP_UNIX 1.354 + let exeFile = this.getLaunchTarget(aApp); 1.355 + if (!exeFile) { 1.356 + return Promise.reject("App executable file not found"); 1.357 + } 1.358 + 1.359 + let deferred = Promise.defer(); 1.360 + 1.361 + try { 1.362 + let process = Cc["@mozilla.org/process/util;1"] 1.363 + .createInstance(Ci.nsIProcess); 1.364 + 1.365 + process.init(exeFile); 1.366 + process.runAsync(["-remove"], 1, (aSubject, aTopic) => { 1.367 + if (aTopic == "process-finished") { 1.368 + deferred.resolve(true); 1.369 + } else { 1.370 + deferred.reject("Uninstaller failed with exit code: " + aSubject.exitValue); 1.371 + } 1.372 + }); 1.373 + } catch (e) { 1.374 + deferred.reject(e); 1.375 + } 1.376 + 1.377 + return deferred.promise; 1.378 +#endif 1.379 + }, 1.380 + 1.381 + /** 1.382 + * Returns true if the given install path (in the old naming scheme) actually 1.383 + * belongs to the given application. 1.384 + */ 1.385 + isOldInstallPathValid: function(aApp, aInstallPath) { 1.386 + // Applications with an origin that starts with "app" are packaged apps and 1.387 + // packaged apps have never been installed using the old naming scheme. 1.388 + // After bug 910465, we'll have a better way to check if an app is 1.389 + // packaged. 1.390 + if (aApp.origin.startsWith("app")) { 1.391 + return false; 1.392 + } 1.393 + 1.394 + // Bug 915480: We could check the app name from the manifest to 1.395 + // better verify the installation path. 1.396 + return true; 1.397 + }, 1.398 + 1.399 + /** 1.400 + * Checks if the given app is locally installed. 1.401 + */ 1.402 + isLaunchable: function(aApp) { 1.403 + let uniqueName = this.getUniqueName(aApp); 1.404 + 1.405 +#ifdef XP_WIN 1.406 + if (!this.getLaunchTarget(aApp)) { 1.407 + return false; 1.408 + } 1.409 + 1.410 + return true; 1.411 +#elifdef XP_MACOSX 1.412 + if (!this.getInstallPath(aApp)) { 1.413 + return false; 1.414 + } 1.415 + 1.416 + return true; 1.417 +#elifdef XP_UNIX 1.418 + let env = Cc["@mozilla.org/process/environment;1"] 1.419 + .getService(Ci.nsIEnvironment); 1.420 + 1.421 + let xdg_data_home_env; 1.422 + try { 1.423 + xdg_data_home_env = env.get("XDG_DATA_HOME"); 1.424 + } catch(ex) {} 1.425 + 1.426 + let desktopINI; 1.427 + if (xdg_data_home_env) { 1.428 + desktopINI = new FileUtils.File(xdg_data_home_env); 1.429 + } else { 1.430 + desktopINI = FileUtils.getFile("Home", [".local", "share"]); 1.431 + } 1.432 + desktopINI.append("applications"); 1.433 + desktopINI.append("owa-" + uniqueName + ".desktop"); 1.434 + 1.435 + // Fall back to the old installation naming scheme 1.436 + if (!desktopINI.exists()) { 1.437 + if (xdg_data_home_env) { 1.438 + desktopINI = new FileUtils.File(xdg_data_home_env); 1.439 + } else { 1.440 + desktopINI = FileUtils.getFile("Home", [".local", "share"]); 1.441 + } 1.442 + 1.443 + let origin = Services.io.newURI(aApp.origin, null, null); 1.444 + let oldUniqueName = origin.scheme + ";" + 1.445 + origin.host + 1.446 + (origin.port != -1 ? ";" + origin.port : ""); 1.447 + 1.448 + desktopINI.append("owa-" + oldUniqueName + ".desktop"); 1.449 + 1.450 + if (!desktopINI.exists()) { 1.451 + return false; 1.452 + } 1.453 + 1.454 + let installDir = Services.dirsvc.get("Home", Ci.nsIFile); 1.455 + installDir.append("." + origin.scheme + ";" + origin.host + 1.456 + (origin.port != -1 ? ";" + origin.port : "")); 1.457 + 1.458 + return isOldInstallPathValid(aApp, installDir.path); 1.459 + } 1.460 + 1.461 + return true; 1.462 +#endif 1.463 + }, 1.464 + 1.465 + /** 1.466 + * Sanitize the filename (accepts only a-z, 0-9, - and _) 1.467 + */ 1.468 + sanitizeStringForFilename: function(aPossiblyBadFilenameString) { 1.469 + return aPossiblyBadFilenameString.replace(/[^a-z0-9_\-]/gi, ""); 1.470 + } 1.471 +}