1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/webapps/MacNativeApp.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,315 @@ 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 file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +const USER_LIB_DIR = OS.Constants.Path.macUserLibDir; 1.9 +const LOCAL_APP_DIR = OS.Constants.Path.macLocalApplicationsDir; 1.10 + 1.11 +/** 1.12 + * Constructor for the Mac native app shell 1.13 + * 1.14 + * @param aApp {Object} the app object provided to the install function 1.15 + * @param aManifest {Object} the manifest data provided by the web app 1.16 + * @param aCategories {Array} array of app categories 1.17 + * @param aRegistryDir {String} (optional) path to the registry 1.18 + */ 1.19 +function NativeApp(aApp, aManifest, aCategories, aRegistryDir) { 1.20 + CommonNativeApp.call(this, aApp, aManifest, aCategories, aRegistryDir); 1.21 + 1.22 + // The ${ProfileDir} is: sanitized app name + "-" + manifest url hash 1.23 + this.appProfileDir = OS.Path.join(USER_LIB_DIR, "Application Support", 1.24 + this.uniqueName); 1.25 + this.configJson = "webapp.json"; 1.26 + 1.27 + this.contentsDir = "Contents"; 1.28 + this.macOSDir = OS.Path.join(this.contentsDir, "MacOS"); 1.29 + this.resourcesDir = OS.Path.join(this.contentsDir, "Resources"); 1.30 + this.iconFile = OS.Path.join(this.resourcesDir, "appicon.icns"); 1.31 + this.zipFile = OS.Path.join(this.resourcesDir, "application.zip"); 1.32 +} 1.33 + 1.34 +NativeApp.prototype = { 1.35 + __proto__: CommonNativeApp.prototype, 1.36 + /* 1.37 + * The _rootInstallDir property is the path of the directory where we install 1.38 + * apps. In production code, it's "/Applications". In tests, it's 1.39 + * "~/Applications" because on build machines we don't have enough privileges 1.40 + * to write to the global "/Applications" directory. 1.41 + */ 1.42 + _rootInstallDir: LOCAL_APP_DIR, 1.43 + 1.44 + /** 1.45 + * Creates a native installation of the web app in the OS 1.46 + * 1.47 + * @param aManifest {Object} the manifest data provided by the web app 1.48 + * @param aZipPath {String} path to the zip file for packaged apps (undefined 1.49 + * for hosted apps) 1.50 + */ 1.51 + install: Task.async(function*(aManifest, aZipPath) { 1.52 + if (this._dryRun) { 1.53 + return; 1.54 + } 1.55 + 1.56 + // If the application is already installed, this is a reinstallation. 1.57 + if (WebappOSUtils.getInstallPath(this.app)) { 1.58 + return yield this.prepareUpdate(aManifest, aZipPath); 1.59 + } 1.60 + 1.61 + this._setData(aManifest); 1.62 + 1.63 + let localAppDir = getFile(this._rootInstallDir); 1.64 + if (!localAppDir.isWritable()) { 1.65 + throw("Not enough privileges to install apps"); 1.66 + } 1.67 + 1.68 + let destinationName = yield getAvailableFileName([ this._rootInstallDir ], 1.69 + this.appNameAsFilename, 1.70 + ".app"); 1.71 + 1.72 + let installDir = OS.Path.join(this._rootInstallDir, destinationName); 1.73 + 1.74 + let dir = getFile(TMP_DIR, this.appNameAsFilename + ".app"); 1.75 + dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, PERMS_DIRECTORY); 1.76 + let tmpDir = dir.path; 1.77 + 1.78 + try { 1.79 + yield this._createDirectoryStructure(tmpDir); 1.80 + this._copyPrebuiltFiles(tmpDir); 1.81 + yield this._createConfigFiles(tmpDir); 1.82 + 1.83 + if (aZipPath) { 1.84 + yield OS.File.move(aZipPath, OS.Path.join(tmpDir, this.zipFile)); 1.85 + } 1.86 + 1.87 + yield this._getIcon(tmpDir); 1.88 + } catch (ex) { 1.89 + yield OS.File.removeDir(tmpDir, { ignoreAbsent: true }); 1.90 + throw ex; 1.91 + } 1.92 + 1.93 + this._removeInstallation(true, installDir); 1.94 + 1.95 + try { 1.96 + // Move the temp installation directory to the /Applications directory 1.97 + yield this._applyTempInstallation(tmpDir, installDir); 1.98 + } catch (ex) { 1.99 + this._removeInstallation(false, installDir); 1.100 + yield OS.File.removeDir(tmpDir, { ignoreAbsent: true }); 1.101 + throw ex; 1.102 + } 1.103 + }), 1.104 + 1.105 + /** 1.106 + * Creates an update in a temporary directory to be applied later. 1.107 + * 1.108 + * @param aManifest {Object} the manifest data provided by the web app 1.109 + * @param aZipPath {String} path to the zip file for packaged apps (undefined 1.110 + * for hosted apps) 1.111 + */ 1.112 + prepareUpdate: Task.async(function*(aManifest, aZipPath) { 1.113 + if (this._dryRun) { 1.114 + return; 1.115 + } 1.116 + 1.117 + this._setData(aManifest); 1.118 + 1.119 + let [ oldUniqueName, installDir ] = WebappOSUtils.getLaunchTarget(this.app); 1.120 + if (!installDir) { 1.121 + throw ERR_NOT_INSTALLED; 1.122 + } 1.123 + 1.124 + if (this.uniqueName != oldUniqueName) { 1.125 + // Bug 919799: If the app is still in the registry, migrate its data to 1.126 + // the new format. 1.127 + throw ERR_UPDATES_UNSUPPORTED_OLD_NAMING_SCHEME; 1.128 + } 1.129 + 1.130 + let updateDir = OS.Path.join(installDir, "update"); 1.131 + yield OS.File.removeDir(updateDir, { ignoreAbsent: true }); 1.132 + yield OS.File.makeDir(updateDir); 1.133 + 1.134 + try { 1.135 + yield this._createDirectoryStructure(updateDir); 1.136 + this._copyPrebuiltFiles(updateDir); 1.137 + yield this._createConfigFiles(updateDir); 1.138 + 1.139 + if (aZipPath) { 1.140 + yield OS.File.move(aZipPath, OS.Path.join(updateDir, this.zipFile)); 1.141 + } 1.142 + 1.143 + yield this._getIcon(updateDir); 1.144 + } catch (ex) { 1.145 + yield OS.File.removeDir(updateDir, { ignoreAbsent: true }); 1.146 + throw ex; 1.147 + } 1.148 + }), 1.149 + 1.150 + /** 1.151 + * Applies an update. 1.152 + */ 1.153 + applyUpdate: Task.async(function*() { 1.154 + if (this._dryRun) { 1.155 + return; 1.156 + } 1.157 + 1.158 + let installDir = WebappOSUtils.getInstallPath(this.app); 1.159 + let updateDir = OS.Path.join(installDir, "update"); 1.160 + 1.161 + let backupDir = yield this._backupInstallation(installDir); 1.162 + 1.163 + try { 1.164 + // Move the update directory to the /Applications directory 1.165 + yield this._applyTempInstallation(updateDir, installDir); 1.166 + } catch (ex) { 1.167 + yield this._restoreInstallation(backupDir, installDir); 1.168 + throw ex; 1.169 + } finally { 1.170 + yield OS.File.removeDir(backupDir, { ignoreAbsent: true }); 1.171 + yield OS.File.removeDir(updateDir, { ignoreAbsent: true }); 1.172 + } 1.173 + }), 1.174 + 1.175 + _applyTempInstallation: Task.async(function*(aTmpDir, aInstallDir) { 1.176 + yield OS.File.move(OS.Path.join(aTmpDir, this.configJson), 1.177 + OS.Path.join(this.appProfileDir, this.configJson)); 1.178 + 1.179 + yield moveDirectory(aTmpDir, aInstallDir); 1.180 + }), 1.181 + 1.182 + _removeInstallation: function(keepProfile, aInstallDir) { 1.183 + let filesToRemove = [ aInstallDir ]; 1.184 + 1.185 + if (!keepProfile) { 1.186 + filesToRemove.push(this.appProfileDir); 1.187 + } 1.188 + 1.189 + removeFiles(filesToRemove); 1.190 + }, 1.191 + 1.192 + _backupInstallation: Task.async(function*(aInstallDir) { 1.193 + let backupDir = OS.Path.join(aInstallDir, "backup"); 1.194 + yield OS.File.removeDir(backupDir, { ignoreAbsent: true }); 1.195 + yield OS.File.makeDir(backupDir); 1.196 + 1.197 + yield moveDirectory(OS.Path.join(aInstallDir, this.contentsDir), 1.198 + backupDir); 1.199 + yield OS.File.move(OS.Path.join(this.appProfileDir, this.configJson), 1.200 + OS.Path.join(backupDir, this.configJson)); 1.201 + 1.202 + return backupDir; 1.203 + }), 1.204 + 1.205 + _restoreInstallation: Task.async(function*(aBackupDir, aInstallDir) { 1.206 + yield OS.File.move(OS.Path.join(aBackupDir, this.configJson), 1.207 + OS.Path.join(this.appProfileDir, this.configJson)); 1.208 + yield moveDirectory(aBackupDir, 1.209 + OS.Path.join(aInstallDir, this.contentsDir)); 1.210 + }), 1.211 + 1.212 + _createDirectoryStructure: Task.async(function*(aDir) { 1.213 + yield OS.File.makeDir(this.appProfileDir, 1.214 + { unixMode: PERMS_DIRECTORY, ignoreExisting: true }); 1.215 + 1.216 + yield OS.File.makeDir(OS.Path.join(aDir, this.contentsDir), 1.217 + { unixMode: PERMS_DIRECTORY, ignoreExisting: true }); 1.218 + 1.219 + yield OS.File.makeDir(OS.Path.join(aDir, this.macOSDir), 1.220 + { unixMode: PERMS_DIRECTORY, ignoreExisting: true }); 1.221 + 1.222 + yield OS.File.makeDir(OS.Path.join(aDir, this.resourcesDir), 1.223 + { unixMode: PERMS_DIRECTORY, ignoreExisting: true }); 1.224 + }), 1.225 + 1.226 + _copyPrebuiltFiles: function(aDir) { 1.227 + let destDir = getFile(aDir, this.macOSDir); 1.228 + let stub = getFile(this.runtimeFolder, "webapprt-stub"); 1.229 + stub.copyTo(destDir, "webapprt"); 1.230 + }, 1.231 + 1.232 + _createConfigFiles: function(aDir) { 1.233 + // ${ProfileDir}/webapp.json 1.234 + yield writeToFile(OS.Path.join(aDir, this.configJson), 1.235 + JSON.stringify(this.webappJson)); 1.236 + 1.237 + // ${InstallDir}/Contents/MacOS/webapp.ini 1.238 + let applicationINI = getFile(aDir, this.macOSDir, "webapp.ini"); 1.239 + 1.240 + let writer = Cc["@mozilla.org/xpcom/ini-processor-factory;1"]. 1.241 + getService(Ci.nsIINIParserFactory). 1.242 + createINIParser(applicationINI). 1.243 + QueryInterface(Ci.nsIINIParserWriter); 1.244 + writer.setString("Webapp", "Name", this.appName); 1.245 + writer.setString("Webapp", "Profile", this.uniqueName); 1.246 + writer.writeFile(); 1.247 + applicationINI.permissions = PERMS_FILE; 1.248 + 1.249 + // ${InstallDir}/Contents/Info.plist 1.250 + let infoPListContent = '<?xml version="1.0" encoding="UTF-8"?>\n\ 1.251 +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n\ 1.252 +<plist version="1.0">\n\ 1.253 + <dict>\n\ 1.254 + <key>CFBundleDevelopmentRegion</key>\n\ 1.255 + <string>English</string>\n\ 1.256 + <key>CFBundleDisplayName</key>\n\ 1.257 + <string>' + escapeXML(this.appName) + '</string>\n\ 1.258 + <key>CFBundleExecutable</key>\n\ 1.259 + <string>webapprt</string>\n\ 1.260 + <key>CFBundleIconFile</key>\n\ 1.261 + <string>appicon</string>\n\ 1.262 + <key>CFBundleIdentifier</key>\n\ 1.263 + <string>' + escapeXML(this.uniqueName) + '</string>\n\ 1.264 + <key>CFBundleInfoDictionaryVersion</key>\n\ 1.265 + <string>6.0</string>\n\ 1.266 + <key>CFBundleName</key>\n\ 1.267 + <string>' + escapeXML(this.appName) + '</string>\n\ 1.268 + <key>CFBundlePackageType</key>\n\ 1.269 + <string>APPL</string>\n\ 1.270 + <key>CFBundleVersion</key>\n\ 1.271 + <string>0</string>\n\ 1.272 + <key>NSHighResolutionCapable</key>\n\ 1.273 + <true/>\n\ 1.274 + <key>NSPrincipalClass</key>\n\ 1.275 + <string>GeckoNSApplication</string>\n\ 1.276 + <key>FirefoxBinary</key>\n\ 1.277 +#expand <string>__MOZ_MACBUNDLE_ID__</string>\n\ 1.278 + </dict>\n\ 1.279 +</plist>'; 1.280 + 1.281 + yield writeToFile(OS.Path.join(aDir, this.contentsDir, "Info.plist"), 1.282 + infoPListContent); 1.283 + }, 1.284 + 1.285 + /** 1.286 + * Process the icon from the imageStream as retrieved from 1.287 + * the URL by getIconForApp(). This will bundle the icon to the 1.288 + * app package at Contents/Resources/appicon.icns. 1.289 + * 1.290 + * @param aMimeType the icon mimetype 1.291 + * @param aImageStream the stream for the image data 1.292 + * @param aDir the directory where the icon should be stored 1.293 + */ 1.294 + _processIcon: function(aMimeType, aIcon, aDir) { 1.295 + let deferred = Promise.defer(); 1.296 + 1.297 + function conversionDone(aSubject, aTopic) { 1.298 + if (aTopic == "process-finished") { 1.299 + deferred.resolve(); 1.300 + } else { 1.301 + deferred.reject("Failure converting icon, exit code: " + aSubject.exitValue); 1.302 + } 1.303 + } 1.304 + 1.305 + let process = Cc["@mozilla.org/process/util;1"]. 1.306 + createInstance(Ci.nsIProcess); 1.307 + let sipsFile = getFile("/usr/bin/sips"); 1.308 + 1.309 + process.init(sipsFile); 1.310 + process.runAsync(["-s", "format", "icns", 1.311 + aIcon.path, 1.312 + "--out", OS.Path.join(aDir, this.iconFile), 1.313 + "-z", "128", "128"], 1.314 + 9, conversionDone); 1.315 + 1.316 + return deferred.promise; 1.317 + } 1.318 +}