2012-02-25 20:22:40 -08:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-05-22 10:23:53 -07:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-02-25 20:22:40 -08:00
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2012-08-02 10:40:57 -07:00
|
|
|
import org.mozilla.gecko.util.INIParser;
|
|
|
|
import org.mozilla.gecko.util.INISection;
|
|
|
|
|
2012-07-27 17:53:54 -07:00
|
|
|
import android.content.Context;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2012-02-25 20:22:40 -08:00
|
|
|
import java.io.File;
|
2013-05-21 09:03:11 -07:00
|
|
|
import java.io.FileOutputStream;
|
2012-02-25 20:22:40 -08:00
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.IOException;
|
2013-05-21 09:03:11 -07:00
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
import java.nio.charset.Charset;
|
2012-07-27 17:53:54 -07:00
|
|
|
import java.util.Enumeration;
|
2012-02-25 20:22:40 -08:00
|
|
|
import java.util.HashMap;
|
2012-06-18 10:03:03 -07:00
|
|
|
import java.util.Hashtable;
|
2012-02-25 20:22:40 -08:00
|
|
|
|
|
|
|
public final class GeckoProfile {
|
|
|
|
private static final String LOGTAG = "GeckoProfile";
|
|
|
|
|
|
|
|
private static HashMap<String, GeckoProfile> sProfileCache = new HashMap<String, GeckoProfile>();
|
2012-11-27 12:35:43 -08:00
|
|
|
private static String sDefaultProfileName = null;
|
2012-02-25 20:22:40 -08:00
|
|
|
|
|
|
|
private final Context mContext;
|
|
|
|
private final String mName;
|
|
|
|
private File mMozDir;
|
|
|
|
private File mDir;
|
|
|
|
|
2012-06-15 09:02:11 -07:00
|
|
|
static private INIParser getProfilesINI(Context context) {
|
|
|
|
File filesDir = context.getFilesDir();
|
|
|
|
File mozillaDir = new File(filesDir, "mozilla");
|
|
|
|
File profilesIni = new File(mozillaDir, "profiles.ini");
|
|
|
|
return new INIParser(profilesIni);
|
|
|
|
}
|
|
|
|
|
2012-02-25 20:22:40 -08:00
|
|
|
public static GeckoProfile get(Context context) {
|
2012-06-18 10:03:03 -07:00
|
|
|
if (context instanceof GeckoApp)
|
|
|
|
return get(context, ((GeckoApp)context).getDefaultProfileName());
|
|
|
|
|
2012-06-15 09:02:11 -07:00
|
|
|
return get(context, "");
|
2012-02-25 20:22:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static GeckoProfile get(Context context, String profileName) {
|
2012-11-27 12:35:43 -08:00
|
|
|
synchronized (sProfileCache) {
|
|
|
|
GeckoProfile profile = sProfileCache.get(profileName);
|
|
|
|
if (profile != null)
|
|
|
|
return profile;
|
|
|
|
}
|
2012-06-29 15:34:15 -07:00
|
|
|
return get(context, profileName, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static GeckoProfile get(Context context, String profileName, String profilePath) {
|
2012-02-25 20:22:40 -08:00
|
|
|
if (context == null) {
|
|
|
|
throw new IllegalArgumentException("context must be non-null");
|
|
|
|
}
|
2012-06-15 09:02:11 -07:00
|
|
|
|
|
|
|
// if no profile was passed in, look for the default profile listed in profiles.ini
|
|
|
|
// if that doesn't exist, look for a profile called 'default'
|
2012-06-29 15:34:15 -07:00
|
|
|
if (TextUtils.isEmpty(profileName) && TextUtils.isEmpty(profilePath)) {
|
2012-11-27 12:35:43 -08:00
|
|
|
profileName = GeckoProfile.findDefaultProfile(context);
|
|
|
|
if (profileName == null)
|
|
|
|
profileName = "default";
|
2012-02-25 20:22:40 -08:00
|
|
|
}
|
|
|
|
|
2012-06-15 09:02:11 -07:00
|
|
|
// actually try to look up the profile
|
2012-02-25 20:22:40 -08:00
|
|
|
synchronized (sProfileCache) {
|
|
|
|
GeckoProfile profile = sProfileCache.get(profileName);
|
|
|
|
if (profile == null) {
|
2012-06-29 15:34:15 -07:00
|
|
|
profile = new GeckoProfile(context, profileName, profilePath);
|
2012-02-25 20:22:40 -08:00
|
|
|
sProfileCache.put(profileName, profile);
|
2012-06-29 15:34:15 -07:00
|
|
|
} else {
|
|
|
|
profile.setDir(profilePath);
|
2012-02-25 20:22:40 -08:00
|
|
|
}
|
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:51:07 -07:00
|
|
|
public static File ensureMozillaDirectory(Context context) throws IOException {
|
|
|
|
synchronized (context) {
|
|
|
|
File filesDir = context.getFilesDir();
|
|
|
|
File mozDir = new File(filesDir, "mozilla");
|
|
|
|
if (! mozDir.exists()) {
|
|
|
|
if (! mozDir.mkdirs()) {
|
|
|
|
throw new IOException("Unable to create mozilla directory at " + mozDir.getAbsolutePath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mozDir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-18 10:03:03 -07:00
|
|
|
public static boolean removeProfile(Context context, String profileName) {
|
|
|
|
return new GeckoProfile(context, profileName).remove();
|
|
|
|
}
|
|
|
|
|
2012-02-25 20:22:40 -08:00
|
|
|
private GeckoProfile(Context context, String profileName) {
|
|
|
|
mContext = context;
|
|
|
|
mName = profileName;
|
|
|
|
}
|
|
|
|
|
2012-06-29 15:34:15 -07:00
|
|
|
private GeckoProfile(Context context, String profileName, String profilePath) {
|
|
|
|
mContext = context;
|
|
|
|
mName = profileName;
|
|
|
|
setDir(profilePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setDir(String profilePath) {
|
|
|
|
if (!TextUtils.isEmpty(profilePath)) {
|
|
|
|
File dir = new File(profilePath);
|
|
|
|
if (dir.exists() && dir.isDirectory()) {
|
|
|
|
mDir = dir;
|
|
|
|
} else {
|
2012-11-27 12:35:43 -08:00
|
|
|
Log.w(LOGTAG, "requested profile directory missing: " + profilePath);
|
2012-06-29 15:34:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-15 09:02:11 -07:00
|
|
|
public String getName() {
|
|
|
|
return mName;
|
|
|
|
}
|
|
|
|
|
2012-02-26 15:08:03 -08:00
|
|
|
public synchronized File getDir() {
|
2012-02-25 20:22:40 -08:00
|
|
|
if (mDir != null) {
|
|
|
|
return mDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2012-05-08 15:51:07 -07:00
|
|
|
// Check for old profiles that may need migration.
|
|
|
|
ProfileMigrator profileMigrator = new ProfileMigrator(mContext);
|
2012-09-28 19:52:11 -07:00
|
|
|
if (!GeckoApp.sIsUsingCustomProfile &&
|
|
|
|
!profileMigrator.isProfileMoved()) {
|
2012-05-08 15:51:07 -07:00
|
|
|
Log.i(LOGTAG, "New installation or update, checking for old profiles.");
|
|
|
|
profileMigrator.launchMoveProfile();
|
|
|
|
}
|
|
|
|
|
2012-06-15 09:02:11 -07:00
|
|
|
// now check if a profile with this name that already exists
|
2012-05-08 15:51:07 -07:00
|
|
|
File mozillaDir = ensureMozillaDirectory(mContext);
|
2012-02-25 20:22:40 -08:00
|
|
|
mDir = findProfileDir(mozillaDir);
|
|
|
|
if (mDir == null) {
|
2012-06-15 09:02:11 -07:00
|
|
|
// otherwise create it
|
2012-02-25 20:22:40 -08:00
|
|
|
mDir = createProfileDir(mozillaDir);
|
|
|
|
} else {
|
|
|
|
Log.d(LOGTAG, "Found profile dir: " + mDir.getAbsolutePath());
|
|
|
|
}
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.e(LOGTAG, "Error getting profile dir", ioe);
|
|
|
|
}
|
|
|
|
return mDir;
|
|
|
|
}
|
|
|
|
|
2012-06-26 11:56:03 -07:00
|
|
|
public File getFile(String aFile) {
|
|
|
|
File f = getDir();
|
|
|
|
if (f == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return new File(f, aFile);
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:51:07 -07:00
|
|
|
public File getFilesDir() {
|
|
|
|
return mContext.getFilesDir();
|
|
|
|
}
|
|
|
|
|
2012-10-29 16:34:29 -07:00
|
|
|
/**
|
|
|
|
* Moves the session file to the backup session file.
|
|
|
|
*
|
|
|
|
* sessionstore.js should hold the current session, and sessionstore.bak
|
|
|
|
* should hold the previous session (where it is used to read the "tabs
|
|
|
|
* from last time"). Normally, sessionstore.js is moved to sessionstore.bak
|
|
|
|
* on a clean quit, but this doesn't happen if Fennec crashed. Thus, this
|
|
|
|
* method should be called after a crash so sessionstore.bak correctly
|
|
|
|
* holds the previous session.
|
|
|
|
*/
|
|
|
|
public void moveSessionFile() {
|
|
|
|
File sessionFile = getFile("sessionstore.js");
|
|
|
|
if (sessionFile != null && sessionFile.exists()) {
|
|
|
|
File sessionFileBackup = getFile("sessionstore.bak");
|
|
|
|
sessionFile.renameTo(sessionFileBackup);
|
2012-02-25 20:22:40 -08:00
|
|
|
}
|
2012-10-29 16:34:29 -07:00
|
|
|
}
|
2012-02-25 20:22:40 -08:00
|
|
|
|
2012-10-29 16:34:29 -07:00
|
|
|
/**
|
|
|
|
* Get the string from a session file.
|
|
|
|
*
|
|
|
|
* The session can either be read from sessionstore.js or sessionstore.bak.
|
|
|
|
* In general, sessionstore.js holds the current session, and
|
|
|
|
* sessionstore.bak holds the previous session.
|
|
|
|
*
|
|
|
|
* @param readBackup if true, the session is read from sessionstore.bak;
|
|
|
|
* otherwise, the session is read from sessionstore.js
|
|
|
|
*
|
|
|
|
* @return the session string
|
|
|
|
*/
|
|
|
|
public String readSessionFile(boolean readBackup) {
|
|
|
|
File sessionFile = getFile(readBackup ? "sessionstore.bak" : "sessionstore.js");
|
2012-02-25 20:22:40 -08:00
|
|
|
|
|
|
|
try {
|
2012-10-29 16:34:29 -07:00
|
|
|
if (sessionFile != null && sessionFile.exists()) {
|
|
|
|
return readFile(sessionFile);
|
|
|
|
}
|
2012-02-25 20:22:40 -08:00
|
|
|
} catch (IOException ioe) {
|
2012-10-29 16:34:29 -07:00
|
|
|
Log.e(LOGTAG, "Unable to read session file", ioe);
|
2012-02-25 20:22:40 -08:00
|
|
|
}
|
2012-10-29 16:34:29 -07:00
|
|
|
return null;
|
2012-02-25 20:22:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public String readFile(String filename) throws IOException {
|
|
|
|
File dir = getDir();
|
|
|
|
if (dir == null) {
|
|
|
|
throw new IOException("No profile directory found");
|
|
|
|
}
|
|
|
|
File target = new File(dir, filename);
|
|
|
|
return readFile(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String readFile(File target) throws IOException {
|
|
|
|
FileReader fr = new FileReader(target);
|
|
|
|
try {
|
2013-07-02 05:12:17 -07:00
|
|
|
StringBuilder sb = new StringBuilder();
|
2012-02-25 20:22:40 -08:00
|
|
|
char[] buf = new char[8192];
|
|
|
|
int read = fr.read(buf);
|
|
|
|
while (read >= 0) {
|
|
|
|
sb.append(buf, 0, read);
|
|
|
|
read = fr.read(buf);
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
} finally {
|
|
|
|
fr.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-18 10:03:03 -07:00
|
|
|
private boolean remove() {
|
|
|
|
try {
|
|
|
|
File mozillaDir = ensureMozillaDirectory(mContext);
|
|
|
|
mDir = findProfileDir(mozillaDir);
|
|
|
|
if (mDir == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
INIParser parser = getProfilesINI(mContext);
|
|
|
|
|
|
|
|
Hashtable<String, INISection> sections = parser.getSections();
|
|
|
|
for (Enumeration<INISection> e = sections.elements(); e.hasMoreElements();) {
|
|
|
|
INISection section = e.nextElement();
|
|
|
|
String name = section.getStringProperty("Name");
|
|
|
|
|
|
|
|
if (name == null || !name.equals(mName))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (section.getName().startsWith("Profile")) {
|
|
|
|
// ok, we have stupid Profile#-named things. Rename backwards.
|
|
|
|
try {
|
|
|
|
int sectionNumber = Integer.parseInt(section.getName().substring("Profile".length()));
|
|
|
|
String curSection = "Profile" + sectionNumber;
|
|
|
|
String nextSection = "Profile" + (sectionNumber+1);
|
|
|
|
|
|
|
|
sections.remove(curSection);
|
|
|
|
|
|
|
|
while (sections.containsKey(nextSection)) {
|
|
|
|
parser.renameSection(nextSection, curSection);
|
|
|
|
sectionNumber++;
|
|
|
|
|
|
|
|
curSection = nextSection;
|
|
|
|
nextSection = "Profile" + (sectionNumber+1);
|
|
|
|
}
|
|
|
|
} catch (NumberFormatException nex) {
|
|
|
|
// uhm, malformed Profile thing; we can't do much.
|
|
|
|
Log.e(LOGTAG, "Malformed section name in profiles.ini: " + section.getName());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// this really shouldn't be the case, but handle it anyway
|
|
|
|
parser.removeSection(mName);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parser.write();
|
|
|
|
return true;
|
|
|
|
} catch (IOException ex) {
|
|
|
|
Log.w(LOGTAG, "Failed to remove profile " + mName + ":\n" + ex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-27 12:35:43 -08:00
|
|
|
public static String findDefaultProfile(Context context) {
|
|
|
|
// Have we read the default profile from the INI already?
|
|
|
|
// Changing the default profile requires a restart, so we don't
|
|
|
|
// need to worry about runtime changes.
|
|
|
|
if (sDefaultProfileName != null) {
|
|
|
|
return sDefaultProfileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open profiles.ini to find the correct path
|
|
|
|
INIParser parser = getProfilesINI(context);
|
|
|
|
|
|
|
|
for (Enumeration<INISection> e = parser.getSections().elements(); e.hasMoreElements();) {
|
|
|
|
INISection section = e.nextElement();
|
|
|
|
if (section.getIntProperty("Default") == 1) {
|
|
|
|
sDefaultProfileName = section.getStringProperty("Name");
|
|
|
|
return sDefaultProfileName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-02-25 20:22:40 -08:00
|
|
|
private File findProfileDir(File mozillaDir) {
|
2012-06-15 09:02:11 -07:00
|
|
|
// Open profiles.ini to find the correct path
|
|
|
|
INIParser parser = getProfilesINI(mContext);
|
|
|
|
|
|
|
|
for (Enumeration<INISection> e = parser.getSections().elements(); e.hasMoreElements();) {
|
|
|
|
INISection section = e.nextElement();
|
|
|
|
String name = section.getStringProperty("Name");
|
|
|
|
if (name != null && name.equals(mName)) {
|
|
|
|
if (section.getIntProperty("IsRelative") == 1) {
|
|
|
|
return new File(mozillaDir, section.getStringProperty("Path"));
|
|
|
|
}
|
|
|
|
return new File(section.getStringProperty("Path"));
|
2012-02-25 20:22:40 -08:00
|
|
|
}
|
|
|
|
}
|
2012-06-15 09:02:11 -07:00
|
|
|
|
2012-02-25 20:22:40 -08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String saltProfileName(String name) {
|
|
|
|
String allowedChars = "abcdefghijklmnopqrstuvwxyz0123456789";
|
2013-07-02 05:12:17 -07:00
|
|
|
StringBuilder salt = new StringBuilder(16);
|
2012-02-25 20:22:40 -08:00
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
salt.append(allowedChars.charAt((int)(Math.random() * allowedChars.length())));
|
|
|
|
}
|
|
|
|
salt.append('.');
|
|
|
|
salt.append(name);
|
|
|
|
return salt.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private File createProfileDir(File mozillaDir) throws IOException {
|
2012-06-15 09:02:11 -07:00
|
|
|
INIParser parser = getProfilesINI(mContext);
|
2012-02-25 20:22:40 -08:00
|
|
|
|
2012-06-15 09:02:11 -07:00
|
|
|
// Salt the name of our requested profile
|
2012-02-25 20:22:40 -08:00
|
|
|
String saltedName = saltProfileName(mName);
|
|
|
|
File profileDir = new File(mozillaDir, saltedName);
|
|
|
|
while (profileDir.exists()) {
|
|
|
|
saltedName = saltProfileName(mName);
|
|
|
|
profileDir = new File(mozillaDir, saltedName);
|
|
|
|
}
|
|
|
|
|
2012-06-15 09:02:11 -07:00
|
|
|
// Attempt to create the salted profile dir
|
2012-02-25 20:22:40 -08:00
|
|
|
if (! profileDir.mkdirs()) {
|
|
|
|
throw new IOException("Unable to create profile at " + profileDir.getAbsolutePath());
|
|
|
|
}
|
|
|
|
Log.d(LOGTAG, "Created new profile dir at " + profileDir.getAbsolutePath());
|
|
|
|
|
2012-06-15 09:02:11 -07:00
|
|
|
// Now update profiles.ini
|
|
|
|
// If this is the first time its created, we also add a General section
|
|
|
|
// look for the first profile number that isn't taken yet
|
|
|
|
int profileNum = 0;
|
|
|
|
while (parser.getSection("Profile" + profileNum) != null) {
|
|
|
|
profileNum++;
|
2012-06-07 10:28:09 -07:00
|
|
|
}
|
|
|
|
|
2012-06-15 09:02:11 -07:00
|
|
|
INISection profileSection = new INISection("Profile" + profileNum);
|
|
|
|
profileSection.setProperty("Name", mName);
|
|
|
|
profileSection.setProperty("IsRelative", 1);
|
|
|
|
profileSection.setProperty("Path", saltedName);
|
|
|
|
|
|
|
|
if (parser.getSection("General") == null) {
|
|
|
|
INISection generalSection = new INISection("General");
|
|
|
|
generalSection.setProperty("StartWithLastProfile", 1);
|
|
|
|
parser.addSection(generalSection);
|
|
|
|
|
|
|
|
// only set as default if this is the first profile we're creating
|
|
|
|
profileSection.setProperty("Default", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
parser.addSection(profileSection);
|
|
|
|
parser.write();
|
|
|
|
|
2013-05-21 09:03:11 -07:00
|
|
|
// Write out profile creation time, mirroring the logic in nsToolkitProfileService.
|
|
|
|
try {
|
|
|
|
FileOutputStream stream = new FileOutputStream(profileDir.getAbsolutePath() + File.separator + "times.json");
|
|
|
|
OutputStreamWriter writer = new OutputStreamWriter(stream, Charset.forName("UTF-8"));
|
|
|
|
try {
|
|
|
|
writer.append("{\"created\": " + System.currentTimeMillis() + "}\n");
|
|
|
|
} finally {
|
|
|
|
writer.close();
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Best-effort.
|
|
|
|
Log.w(LOGTAG, "Couldn't write times.json.", e);
|
|
|
|
}
|
|
|
|
|
2012-02-25 20:22:40 -08:00
|
|
|
return profileDir;
|
|
|
|
}
|
|
|
|
}
|