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: package org.mozilla.gecko; michael@0: michael@0: import java.io.File; michael@0: import java.util.Enumeration; michael@0: import java.util.HashMap; michael@0: import java.util.Map; michael@0: michael@0: import org.mozilla.gecko.util.INIParser; michael@0: import org.mozilla.gecko.util.INISection; michael@0: michael@0: import android.content.Context; michael@0: michael@0: /** michael@0: * GeckoProfileDirectories manages access to mappings from profile michael@0: * names to salted profile directory paths, as well as the default profile name. michael@0: * michael@0: * This class will eventually come to encapsulate the remaining logic embedded michael@0: * in profiles.ini; for now it's a read-only wrapper. michael@0: */ michael@0: public class GeckoProfileDirectories { michael@0: @SuppressWarnings("serial") michael@0: public static class NoMozillaDirectoryException extends Exception { michael@0: public NoMozillaDirectoryException(Throwable cause) { michael@0: super(cause); michael@0: } michael@0: michael@0: public NoMozillaDirectoryException(String reason) { michael@0: super(reason); michael@0: } michael@0: michael@0: public NoMozillaDirectoryException(String reason, Throwable cause) { michael@0: super(reason, cause); michael@0: } michael@0: } michael@0: michael@0: @SuppressWarnings("serial") michael@0: public static class NoSuchProfileException extends Exception { michael@0: public NoSuchProfileException(String detailMessage, Throwable cause) { michael@0: super(detailMessage, cause); michael@0: } michael@0: michael@0: public NoSuchProfileException(String detailMessage) { michael@0: super(detailMessage); michael@0: } michael@0: } michael@0: michael@0: private interface INISectionPredicate { michael@0: public boolean matches(INISection section); michael@0: } michael@0: michael@0: private static final String MOZILLA_DIR_NAME = "mozilla"; michael@0: michael@0: /** michael@0: * Returns true if the supplied profile entry represents the default profile. michael@0: */ michael@0: private static INISectionPredicate sectionIsDefault = new INISectionPredicate() { michael@0: @Override michael@0: public boolean matches(INISection section) { michael@0: return section.getIntProperty("Default") == 1; michael@0: } michael@0: }; michael@0: michael@0: /** michael@0: * Returns true if the supplied profile entry has a 'Name' field. michael@0: */ michael@0: private static INISectionPredicate sectionHasName = new INISectionPredicate() { michael@0: @Override michael@0: public boolean matches(INISection section) { michael@0: final String name = section.getStringProperty("Name"); michael@0: return name != null; michael@0: } michael@0: }; michael@0: michael@0: /** michael@0: * Package-scoped because GeckoProfile needs to dig into this in order to do writes. michael@0: * This will be fixed in Bug 975212. michael@0: */ michael@0: static INIParser getProfilesINI(File mozillaDir) { michael@0: return new INIParser(new File(mozillaDir, "profiles.ini")); michael@0: } michael@0: michael@0: /** michael@0: * Utility method to compute a salted profile name: eight random alphanumeric michael@0: * characters, followed by a period, followed by the profile name. michael@0: */ michael@0: public static String saltProfileName(final String name) { michael@0: if (name == null) { michael@0: throw new IllegalArgumentException("Cannot salt null profile name."); michael@0: } michael@0: michael@0: final String allowedChars = "abcdefghijklmnopqrstuvwxyz0123456789"; michael@0: final int scale = allowedChars.length(); michael@0: final int saltSize = 8; michael@0: michael@0: final StringBuilder saltBuilder = new StringBuilder(saltSize + 1 + name.length()); michael@0: for (int i = 0; i < saltSize; i++) { michael@0: saltBuilder.append(allowedChars.charAt((int)(Math.random() * scale))); michael@0: } michael@0: saltBuilder.append('.'); michael@0: saltBuilder.append(name); michael@0: return saltBuilder.toString(); michael@0: } michael@0: michael@0: /** michael@0: * Return the Mozilla directory within the files directory of the provided michael@0: * context. This should always be the same within a running application. michael@0: * michael@0: * This method is package-scoped so that new {@link GeckoProfile} instances can michael@0: * contextualize themselves. michael@0: * michael@0: * @return a new File object for the Mozilla directory. michael@0: * @throws NoMozillaDirectoryException michael@0: * if the directory did not exist and could not be created. michael@0: */ michael@0: static File getMozillaDirectory(Context context) throws NoMozillaDirectoryException { michael@0: final File mozillaDir = new File(context.getFilesDir(), MOZILLA_DIR_NAME); michael@0: if (mozillaDir.exists() || mozillaDir.mkdirs()) { michael@0: return mozillaDir; michael@0: } michael@0: michael@0: // Although this leaks a path to the system log, the path is michael@0: // predictable (unlike a profile directory), so this is fine. michael@0: throw new NoMozillaDirectoryException("Unable to create mozilla directory at " + mozillaDir.getAbsolutePath()); michael@0: } michael@0: michael@0: /** michael@0: * Discover the default profile name by examining profiles.ini. michael@0: * michael@0: * Package-scoped because {@link GeckoProfile} needs access to it. michael@0: * michael@0: * @return null if there is no "Default" entry in profiles.ini, or the profile michael@0: * name if there is. michael@0: * @throws NoMozillaDirectoryException michael@0: * if the Mozilla directory did not exist and could not be created. michael@0: */ michael@0: static String findDefaultProfileName(final Context context) throws NoMozillaDirectoryException { michael@0: final INIParser parser = GeckoProfileDirectories.getProfilesINI(getMozillaDirectory(context)); michael@0: michael@0: for (Enumeration e = parser.getSections().elements(); e.hasMoreElements();) { michael@0: final INISection section = e.nextElement(); michael@0: if (section.getIntProperty("Default") == 1) { michael@0: return section.getStringProperty("Name"); michael@0: } michael@0: } michael@0: michael@0: return null; michael@0: } michael@0: michael@0: static Map getDefaultProfile(final File mozillaDir) { michael@0: return getMatchingProfiles(mozillaDir, sectionIsDefault, true); michael@0: } michael@0: michael@0: static Map getProfilesNamed(final File mozillaDir, final String name) { michael@0: final INISectionPredicate predicate = new INISectionPredicate() { michael@0: @Override michael@0: public boolean matches(final INISection section) { michael@0: return name.equals(section.getStringProperty("Name")); michael@0: } michael@0: }; michael@0: return getMatchingProfiles(mozillaDir, predicate, true); michael@0: } michael@0: michael@0: /** michael@0: * Calls {@link GeckoProfileDirectories#getMatchingProfiles(File, INISectionPredicate, boolean)} michael@0: * with a filter to ensure that all profiles are named. michael@0: */ michael@0: static Map getAllProfiles(final File mozillaDir) { michael@0: return getMatchingProfiles(mozillaDir, sectionHasName, false); michael@0: } michael@0: michael@0: /** michael@0: * Return a mapping from the names of all matching profiles (that is, michael@0: * profiles appearing in profiles.ini that match the supplied predicate) to michael@0: * their absolute paths on disk. michael@0: * michael@0: * @param mozillaDir michael@0: * a directory containing profiles.ini. michael@0: * @param predicate michael@0: * a predicate to use when evaluating whether to include a michael@0: * particular INI section. michael@0: * @param stopOnSuccess michael@0: * if true, this method will return with the first result that michael@0: * matches the predicate; if false, all matching results are michael@0: * included. michael@0: * @return a {@link Map} from name to path. michael@0: */ michael@0: public static Map getMatchingProfiles(final File mozillaDir, INISectionPredicate predicate, boolean stopOnSuccess) { michael@0: final HashMap result = new HashMap(); michael@0: final INIParser parser = GeckoProfileDirectories.getProfilesINI(mozillaDir); michael@0: michael@0: for (Enumeration e = parser.getSections().elements(); e.hasMoreElements();) { michael@0: final INISection section = e.nextElement(); michael@0: if (predicate == null || predicate.matches(section)) { michael@0: final String name = section.getStringProperty("Name"); michael@0: final String pathString = section.getStringProperty("Path"); michael@0: final boolean isRelative = section.getIntProperty("IsRelative") == 1; michael@0: final File path = isRelative ? new File(mozillaDir, pathString) : new File(pathString); michael@0: result.put(name, path.getAbsolutePath()); michael@0: michael@0: if (stopOnSuccess) { michael@0: return result; michael@0: } michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: public static File findProfileDir(final File mozillaDir, final String profileName) throws NoSuchProfileException { michael@0: // Open profiles.ini to find the correct path. michael@0: final INIParser parser = GeckoProfileDirectories.getProfilesINI(mozillaDir); michael@0: michael@0: for (Enumeration e = parser.getSections().elements(); e.hasMoreElements();) { michael@0: final INISection section = e.nextElement(); michael@0: final String name = section.getStringProperty("Name"); michael@0: if (name != null && name.equals(profileName)) { michael@0: if (section.getIntProperty("IsRelative") == 1) { michael@0: return new File(mozillaDir, section.getStringProperty("Path")); michael@0: } michael@0: return new File(section.getStringProperty("Path")); michael@0: } michael@0: } michael@0: michael@0: throw new NoSuchProfileException("No profile " + profileName); michael@0: } michael@0: }