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;
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-06-16 16:07:43 -07:00
|
|
|
import org.mozilla.gecko.GeckoProfileDirectories.NoMozillaDirectoryException;
|
|
|
|
import org.mozilla.gecko.GeckoProfileDirectories.NoSuchProfileException;
|
2014-09-30 15:19:26 -07:00
|
|
|
import org.mozilla.gecko.db.LocalBrowserDB;
|
2014-06-16 16:07:43 -07:00
|
|
|
import org.mozilla.gecko.distribution.Distribution;
|
2014-06-20 07:41:05 -07:00
|
|
|
import org.mozilla.gecko.mozglue.RobocopTarget;
|
2014-06-16 16:07:43 -07:00
|
|
|
import org.mozilla.gecko.util.INIParser;
|
|
|
|
import org.mozilla.gecko.util.INISection;
|
|
|
|
|
2014-10-08 11:43:14 -07:00
|
|
|
import android.app.Activity;
|
2014-06-16 16:07:43 -07:00
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
2014-08-21 17:18:42 -07:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2014-06-16 16:07:43 -07:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2012-02-25 20:22:40 -08:00
|
|
|
public final class GeckoProfile {
|
|
|
|
private static final String LOGTAG = "GeckoProfile";
|
2014-03-03 15:27:42 -08:00
|
|
|
|
2013-08-02 11:04:42 -07:00
|
|
|
// Used to "lock" the guest profile, so that we'll always restart in it
|
|
|
|
private static final String LOCK_FILE_NAME = ".active_lock";
|
2014-01-14 21:45:59 -08:00
|
|
|
public static final String DEFAULT_PROFILE = "default";
|
2014-08-25 16:19:16 -07:00
|
|
|
public static final String GUEST_PROFILE = "guest";
|
2012-02-25 20:22:40 -08:00
|
|
|
|
|
|
|
private static HashMap<String, GeckoProfile> sProfileCache = new HashMap<String, GeckoProfile>();
|
2014-07-25 20:14:47 -07:00
|
|
|
private static String sDefaultProfileName;
|
2012-02-25 20:22:40 -08:00
|
|
|
|
2014-06-16 16:07:43 -07:00
|
|
|
// Caches the guest profile dir.
|
2014-07-25 20:14:47 -07:00
|
|
|
private static File sGuestDir;
|
|
|
|
private static GeckoProfile sGuestProfile;
|
2014-06-16 16:07:43 -07:00
|
|
|
|
2014-07-25 20:14:47 -07:00
|
|
|
public static boolean sIsUsingCustomProfile;
|
2014-06-16 16:07:43 -07:00
|
|
|
|
2014-03-03 15:27:42 -08:00
|
|
|
private final String mName;
|
|
|
|
private final File mMozillaDir;
|
2014-06-16 16:07:43 -07:00
|
|
|
private final boolean mIsWebAppProfile;
|
|
|
|
private final Context mApplicationContext;
|
|
|
|
|
2014-06-20 07:41:05 -07:00
|
|
|
/**
|
|
|
|
* Access to this member should be synchronized to avoid
|
|
|
|
* races during creation -- particularly between getDir and GeckoView#init.
|
|
|
|
*
|
|
|
|
* Not final because this is lazily computed.
|
|
|
|
*/
|
|
|
|
private File mProfileDir;
|
2012-02-25 20:22:40 -08:00
|
|
|
|
2014-06-16 16:07:43 -07:00
|
|
|
// Caches whether or not a profile is "locked".
|
|
|
|
// Only used by the guest profile to determine if it should be reused or
|
2014-06-20 07:41:05 -07:00
|
|
|
// deleted on startup.
|
|
|
|
// These are volatile for an incremental improvement in thread safety,
|
|
|
|
// but this is not a complete solution for concurrency.
|
|
|
|
private volatile LockState mLocked = LockState.UNDEFINED;
|
2014-07-25 20:14:47 -07:00
|
|
|
private volatile boolean mInGuestMode;
|
2014-06-16 16:07:43 -07:00
|
|
|
|
2013-08-02 11:04:42 -07:00
|
|
|
// Constants to cache whether or not a profile is "locked".
|
|
|
|
private enum LockState {
|
|
|
|
LOCKED,
|
|
|
|
UNLOCKED,
|
|
|
|
UNDEFINED
|
|
|
|
};
|
2014-03-03 15:27:42 -08:00
|
|
|
|
2012-02-25 20:22:40 -08:00
|
|
|
public static GeckoProfile get(Context context) {
|
2013-10-23 09:33:56 -07:00
|
|
|
boolean isGeckoApp = false;
|
|
|
|
try {
|
|
|
|
isGeckoApp = context instanceof GeckoApp;
|
|
|
|
} catch (NoClassDefFoundError ex) {}
|
|
|
|
|
|
|
|
if (isGeckoApp) {
|
2013-08-02 11:04:42 -07:00
|
|
|
// Check for a cached profile on this context already
|
|
|
|
// TODO: We should not be caching profile information on the Activity context
|
2014-03-03 15:27:42 -08:00
|
|
|
final GeckoApp geckoApp = (GeckoApp) context;
|
|
|
|
if (geckoApp.mProfile != null) {
|
|
|
|
return geckoApp.mProfile;
|
2013-08-02 11:04:42 -07:00
|
|
|
}
|
2013-09-19 10:40:48 -07:00
|
|
|
}
|
2013-08-02 11:04:42 -07:00
|
|
|
|
2014-10-08 11:43:14 -07:00
|
|
|
final String args;
|
|
|
|
if (context instanceof Activity) {
|
|
|
|
args = ((Activity) context).getIntent().getStringExtra("args");
|
|
|
|
} else {
|
|
|
|
args = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GuestSession.shouldUse(context, args)) {
|
|
|
|
GeckoProfile p = GeckoProfile.getOrCreateGuestProfile(context);
|
|
|
|
if (isGeckoApp) {
|
|
|
|
((GeckoApp) context).mProfile = p;
|
|
|
|
}
|
|
|
|
return p;
|
2013-09-19 10:40:48 -07:00
|
|
|
}
|
2013-08-02 11:04:42 -07:00
|
|
|
|
2013-10-23 09:33:56 -07:00
|
|
|
if (isGeckoApp) {
|
2014-03-03 15:27:42 -08:00
|
|
|
final GeckoApp geckoApp = (GeckoApp) context;
|
|
|
|
String defaultProfileName;
|
|
|
|
try {
|
|
|
|
defaultProfileName = geckoApp.getDefaultProfileName();
|
|
|
|
} catch (NoMozillaDirectoryException e) {
|
|
|
|
// If this failed, we're screwed. But there are so many callers that
|
|
|
|
// we'll just throw a RuntimeException.
|
|
|
|
Log.wtf(LOGTAG, "Unable to get default profile name.", e);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
// Otherwise, get the default profile for the Activity.
|
|
|
|
return get(context, defaultProfileName);
|
2013-07-18 11:43:34 -07:00
|
|
|
}
|
2012-06-18 10:03:03 -07:00
|
|
|
|
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;
|
|
|
|
}
|
2013-07-18 11:43:34 -07:00
|
|
|
return get(context, profileName, (File)null);
|
2012-06-29 15:34:15 -07:00
|
|
|
}
|
|
|
|
|
2014-06-20 07:41:05 -07:00
|
|
|
@RobocopTarget
|
2012-06-29 15:34:15 -07:00
|
|
|
public static GeckoProfile get(Context context, String profileName, String profilePath) {
|
2013-07-18 11:43:34 -07:00
|
|
|
File dir = null;
|
2014-03-26 10:47:15 -07:00
|
|
|
if (!TextUtils.isEmpty(profilePath)) {
|
2013-07-18 11:43:34 -07:00
|
|
|
dir = new File(profilePath);
|
2014-03-26 10:47:15 -07:00
|
|
|
if (!dir.exists() || !dir.isDirectory()) {
|
|
|
|
Log.w(LOGTAG, "requested profile directory missing: " + profilePath);
|
|
|
|
}
|
|
|
|
}
|
2013-07-18 11:43:34 -07:00
|
|
|
return get(context, profileName, dir);
|
|
|
|
}
|
|
|
|
|
2014-06-20 07:41:05 -07:00
|
|
|
@RobocopTarget
|
2013-07-18 11:43:34 -07:00
|
|
|
public static GeckoProfile get(Context context, String profileName, File profileDir) {
|
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'
|
2013-07-18 11:43:34 -07:00
|
|
|
if (TextUtils.isEmpty(profileName) && profileDir == null) {
|
2014-03-03 15:27:42 -08:00
|
|
|
try {
|
|
|
|
profileName = GeckoProfile.getDefaultProfileName(context);
|
|
|
|
} catch (NoMozillaDirectoryException e) {
|
|
|
|
// We're unable to do anything sane here.
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
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) {
|
2014-03-03 15:27:42 -08:00
|
|
|
try {
|
|
|
|
profile = new GeckoProfile(context, profileName);
|
|
|
|
} catch (NoMozillaDirectoryException e) {
|
|
|
|
// We're unable to do anything sane here.
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2013-11-15 22:53:53 -08:00
|
|
|
profile.setDir(profileDir);
|
2012-02-25 20:22:40 -08:00
|
|
|
sProfileCache.put(profileName, profile);
|
2012-06-29 15:34:15 -07:00
|
|
|
} else {
|
2013-07-18 11:43:34 -07:00
|
|
|
profile.setDir(profileDir);
|
2012-02-25 20:22:40 -08:00
|
|
|
}
|
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-18 10:03:03 -07:00
|
|
|
public static boolean removeProfile(Context context, String profileName) {
|
2014-06-16 16:07:43 -07:00
|
|
|
if (profileName == null) {
|
|
|
|
Log.w(LOGTAG, "Unable to remove profile: null profile name.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-25 16:19:16 -07:00
|
|
|
final GeckoProfile profile = get(context, profileName);
|
|
|
|
if (profile == null) {
|
|
|
|
return false;
|
2014-03-03 15:27:42 -08:00
|
|
|
}
|
2014-08-25 16:19:16 -07:00
|
|
|
final boolean success = profile.remove();
|
2014-04-01 07:02:14 -07:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
// Clear all shared prefs for the given profile.
|
|
|
|
GeckoSharedPrefs.forProfileName(context, profileName)
|
2014-09-02 23:00:42 -07:00
|
|
|
.edit().clear().apply();
|
2014-04-01 07:02:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
2012-06-18 10:03:03 -07:00
|
|
|
}
|
|
|
|
|
2014-10-08 11:43:14 -07:00
|
|
|
// Only public for access from tests.
|
2014-10-08 13:30:38 -07:00
|
|
|
@RobocopTarget
|
2013-07-18 11:43:34 -07:00
|
|
|
public static GeckoProfile createGuestProfile(Context context) {
|
|
|
|
try {
|
2013-08-02 11:04:42 -07:00
|
|
|
// We need to force the creation of a new guest profile if we want it outside of the normal profile path,
|
|
|
|
// otherwise GeckoProfile.getDir will try to be smart and build it for us in the normal profiles dir.
|
|
|
|
getGuestDir(context).mkdir();
|
|
|
|
GeckoProfile profile = getGuestProfile(context);
|
2014-09-10 16:32:14 -07:00
|
|
|
|
|
|
|
// If we're creating this guest session over the keyguard, don't lock it.
|
|
|
|
// This will force the guest session to exit if the user unlocks their phone
|
|
|
|
// and starts Fennec.
|
|
|
|
if (!GuestSession.isSecureKeyguardLocked(context)) {
|
|
|
|
profile.lock();
|
|
|
|
}
|
2014-07-03 19:37:27 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now do the things that createProfileDirectory normally does --
|
|
|
|
* right now that's kicking off DB init.
|
|
|
|
*/
|
2014-09-30 15:19:26 -07:00
|
|
|
profile.enqueueInitialization(profile.getDir());
|
2014-07-03 19:37:27 -07:00
|
|
|
|
2013-07-18 11:43:34 -07:00
|
|
|
return profile;
|
|
|
|
} catch (Exception ex) {
|
|
|
|
Log.e(LOGTAG, "Error creating guest profile", ex);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-08-02 11:04:42 -07:00
|
|
|
public static void leaveGuestSession(Context context) {
|
|
|
|
GeckoProfile profile = getGuestProfile(context);
|
|
|
|
if (profile != null) {
|
|
|
|
profile.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static File getGuestDir(Context context) {
|
2014-03-03 15:27:42 -08:00
|
|
|
if (sGuestDir == null) {
|
|
|
|
sGuestDir = context.getFileStreamPath("guest");
|
2013-08-02 11:04:42 -07:00
|
|
|
}
|
2014-03-03 15:27:42 -08:00
|
|
|
return sGuestDir;
|
2013-08-02 11:04:42 -07:00
|
|
|
}
|
|
|
|
|
2014-10-08 11:43:14 -07:00
|
|
|
/**
|
|
|
|
* Performs IO. Be careful of using this on the main thread.
|
|
|
|
*/
|
|
|
|
public static GeckoProfile getOrCreateGuestProfile(Context context) {
|
|
|
|
GeckoProfile p = getGuestProfile(context);
|
|
|
|
if (p == null) {
|
|
|
|
return createGuestProfile(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2014-09-10 16:32:14 -07:00
|
|
|
public static GeckoProfile getGuestProfile(Context context) {
|
2014-03-03 15:27:42 -08:00
|
|
|
if (sGuestProfile == null) {
|
2013-08-02 11:04:42 -07:00
|
|
|
File guestDir = getGuestDir(context);
|
2013-08-26 16:32:10 -07:00
|
|
|
if (guestDir.exists()) {
|
2014-04-01 07:02:14 -07:00
|
|
|
sGuestProfile = get(context, GUEST_PROFILE, guestDir);
|
2014-03-03 15:27:42 -08:00
|
|
|
sGuestProfile.mInGuestMode = true;
|
2013-08-26 16:32:10 -07:00
|
|
|
}
|
2013-08-02 11:04:42 -07:00
|
|
|
}
|
|
|
|
|
2014-03-03 15:27:42 -08:00
|
|
|
return sGuestProfile;
|
2013-08-02 11:04:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean maybeCleanupGuestProfile(final Context context) {
|
2013-08-26 16:32:10 -07:00
|
|
|
final GeckoProfile profile = getGuestProfile(context);
|
|
|
|
if (profile == null) {
|
2013-08-14 09:07:00 -07:00
|
|
|
return false;
|
2014-09-10 16:32:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!profile.locked()) {
|
2013-08-26 16:32:10 -07:00
|
|
|
profile.mInGuestMode = false;
|
2013-08-14 09:07:00 -07:00
|
|
|
|
2013-08-26 16:32:10 -07:00
|
|
|
// If the guest dir exists, but it's unlocked, delete it
|
2013-08-26 16:32:10 -07:00
|
|
|
removeGuestProfile(context);
|
|
|
|
|
2013-08-02 11:04:42 -07:00
|
|
|
return true;
|
2013-07-30 09:05:25 -07:00
|
|
|
}
|
2013-08-02 11:04:42 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void removeGuestProfile(Context context) {
|
2014-04-01 07:02:14 -07:00
|
|
|
boolean success = false;
|
2013-07-30 09:05:25 -07:00
|
|
|
try {
|
2013-08-02 11:04:42 -07:00
|
|
|
File guestDir = getGuestDir(context);
|
2013-07-30 09:05:25 -07:00
|
|
|
if (guestDir.exists()) {
|
2014-04-01 07:02:14 -07:00
|
|
|
success = delete(guestDir);
|
2013-07-30 09:05:25 -07:00
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
Log.e(LOGTAG, "Error removing guest profile", ex);
|
|
|
|
}
|
2014-04-01 07:02:14 -07:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
// Clear all shared prefs for the guest profile.
|
|
|
|
GeckoSharedPrefs.forProfileName(context, GUEST_PROFILE)
|
2014-09-02 23:00:42 -07:00
|
|
|
.edit().clear().apply();
|
2014-04-01 07:02:14 -07:00
|
|
|
}
|
2013-07-30 09:05:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean delete(File file) throws IOException {
|
|
|
|
// Try to do a quick initial delete
|
|
|
|
if (file.delete())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
// If the quick delete failed and this is a dir, recursively delete the contents of the dir
|
|
|
|
String files[] = file.list();
|
|
|
|
for (String temp : files) {
|
|
|
|
File fileDelete = new File(file, temp);
|
|
|
|
delete(fileDelete);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Even if this is a dir, it should now be empty and delete should work
|
|
|
|
return file.delete();
|
|
|
|
}
|
|
|
|
|
2014-03-03 15:27:42 -08:00
|
|
|
private GeckoProfile(Context context, String profileName) throws NoMozillaDirectoryException {
|
2014-06-16 16:07:43 -07:00
|
|
|
if (TextUtils.isEmpty(profileName)) {
|
|
|
|
throw new IllegalArgumentException("Unable to create GeckoProfile for empty profile name.");
|
|
|
|
}
|
|
|
|
|
|
|
|
mApplicationContext = context.getApplicationContext();
|
2014-03-03 15:27:42 -08:00
|
|
|
mName = profileName;
|
2014-06-16 16:07:43 -07:00
|
|
|
mIsWebAppProfile = profileName.startsWith("webapp");
|
2014-03-03 15:27:42 -08:00
|
|
|
mMozillaDir = GeckoProfileDirectories.getMozillaDirectory(context);
|
|
|
|
}
|
|
|
|
|
2013-08-02 11:04:42 -07:00
|
|
|
// Warning, Changing the lock file state from outside apis will cause this to become out of sync
|
|
|
|
public boolean locked() {
|
|
|
|
if (mLocked != LockState.UNDEFINED) {
|
|
|
|
return mLocked == LockState.LOCKED;
|
|
|
|
}
|
|
|
|
|
2014-06-20 07:41:05 -07:00
|
|
|
boolean profileExists;
|
|
|
|
synchronized (this) {
|
|
|
|
profileExists = mProfileDir != null && mProfileDir.exists();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't use getDir() as it will create a dir if none exists.
|
|
|
|
if (profileExists) {
|
2013-11-15 22:53:53 -08:00
|
|
|
File lockFile = new File(mProfileDir, LOCK_FILE_NAME);
|
2013-09-11 10:10:47 -07:00
|
|
|
boolean res = lockFile.exists();
|
|
|
|
mLocked = res ? LockState.LOCKED : LockState.UNLOCKED;
|
|
|
|
} else {
|
|
|
|
mLocked = LockState.UNLOCKED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mLocked == LockState.LOCKED;
|
2013-08-02 11:04:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean lock() {
|
|
|
|
try {
|
2013-09-11 10:10:47 -07:00
|
|
|
// If this dir doesn't exist getDir will create it for us
|
2014-06-20 07:41:05 -07:00
|
|
|
final File lockFile = new File(getDir(), LOCK_FILE_NAME);
|
2014-09-10 19:00:26 -07:00
|
|
|
final boolean result = lockFile.createNewFile();
|
2014-09-16 20:22:41 -07:00
|
|
|
if (lockFile.exists()) {
|
2013-08-02 11:04:42 -07:00
|
|
|
mLocked = LockState.LOCKED;
|
|
|
|
} else {
|
|
|
|
mLocked = LockState.UNLOCKED;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} catch(IOException ex) {
|
|
|
|
Log.e(LOGTAG, "Error locking profile", ex);
|
|
|
|
}
|
|
|
|
mLocked = LockState.UNLOCKED;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean unlock() {
|
2014-06-20 07:41:05 -07:00
|
|
|
final File profileDir;
|
|
|
|
synchronized (this) {
|
|
|
|
// Don't use getDir() as it will create a dir.
|
|
|
|
profileDir = mProfileDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (profileDir == null || !profileDir.exists()) {
|
2013-09-11 10:10:47 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-02 11:04:42 -07:00
|
|
|
try {
|
2014-06-20 07:41:05 -07:00
|
|
|
final File lockFile = new File(profileDir, LOCK_FILE_NAME);
|
2014-09-10 16:32:14 -07:00
|
|
|
if (!lockFile.exists()) {
|
|
|
|
mLocked = LockState.UNLOCKED;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-20 07:41:05 -07:00
|
|
|
final boolean result = delete(lockFile);
|
2013-08-02 11:04:42 -07:00
|
|
|
if (result) {
|
|
|
|
mLocked = LockState.UNLOCKED;
|
|
|
|
} else {
|
|
|
|
mLocked = LockState.LOCKED;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} catch(IOException ex) {
|
|
|
|
Log.e(LOGTAG, "Error unlocking profile", ex);
|
|
|
|
}
|
2014-09-10 16:32:14 -07:00
|
|
|
|
2013-08-02 11:04:42 -07:00
|
|
|
mLocked = LockState.LOCKED;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-18 11:43:34 -07:00
|
|
|
public boolean inGuestMode() {
|
|
|
|
return mInGuestMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setDir(File dir) {
|
|
|
|
if (dir != null && dir.exists() && dir.isDirectory()) {
|
2014-06-20 07:41:05 -07:00
|
|
|
synchronized (this) {
|
|
|
|
mProfileDir = dir;
|
|
|
|
}
|
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() {
|
2013-10-16 14:02:39 -07:00
|
|
|
forceCreate();
|
2013-11-15 22:53:53 -08:00
|
|
|
return mProfileDir;
|
2013-10-16 14:02:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized GeckoProfile forceCreate() {
|
2013-11-15 22:53:53 -08:00
|
|
|
if (mProfileDir != null) {
|
2013-10-16 14:02:39 -07:00
|
|
|
return this;
|
2012-02-25 20:22:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2013-07-24 15:50:26 -07:00
|
|
|
// Check if a profile with this name already exists.
|
2014-03-03 15:27:42 -08:00
|
|
|
try {
|
|
|
|
mProfileDir = findProfileDir();
|
2014-01-03 14:48:28 -08:00
|
|
|
Log.d(LOGTAG, "Found profile dir.");
|
2014-03-03 15:27:42 -08:00
|
|
|
} catch (NoSuchProfileException noSuchProfile) {
|
|
|
|
// If it doesn't exist, create it.
|
|
|
|
mProfileDir = createProfileDir();
|
2012-02-25 20:22:40 -08:00
|
|
|
}
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.e(LOGTAG, "Error getting profile dir", ioe);
|
|
|
|
}
|
2013-10-16 14:02:39 -07:00
|
|
|
return this;
|
2012-02-25 20:22:40 -08:00
|
|
|
}
|
|
|
|
|
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-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 {
|
2014-06-20 07:41:05 -07:00
|
|
|
synchronized (this) {
|
|
|
|
final File dir = getDir();
|
|
|
|
if (dir.exists()) {
|
|
|
|
delete(dir);
|
|
|
|
}
|
2013-08-02 11:04:42 -07:00
|
|
|
|
2014-06-20 07:41:05 -07:00
|
|
|
try {
|
|
|
|
mProfileDir = findProfileDir();
|
|
|
|
} catch (NoSuchProfileException noSuchProfile) {
|
|
|
|
// If the profile doesn't exist, there's nothing left for us to do.
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-02 11:04:42 -07:00
|
|
|
}
|
2013-08-28 09:10:07 -07:00
|
|
|
|
2014-03-03 15:27:42 -08:00
|
|
|
final INIParser parser = GeckoProfileDirectories.getProfilesINI(mMozillaDir);
|
|
|
|
final Hashtable<String, INISection> sections = parser.getSections();
|
2013-08-28 09:10:07 -07:00
|
|
|
for (Enumeration<INISection> e = sections.elements(); e.hasMoreElements();) {
|
2014-03-03 15:27:42 -08:00
|
|
|
final INISection section = e.nextElement();
|
2013-08-28 09:10:07 -07:00
|
|
|
String name = section.getStringProperty("Name");
|
|
|
|
|
2014-03-03 15:27:42 -08:00
|
|
|
if (name == null || !name.equals(mName)) {
|
2013-08-28 09:10:07 -07:00
|
|
|
continue;
|
2014-03-03 15:27:42 -08:00
|
|
|
}
|
2013-08-28 09:10:07 -07:00
|
|
|
|
|
|
|
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++;
|
2014-03-03 15:27:42 -08:00
|
|
|
|
2013-08-28 09:10:07 -07:00
|
|
|
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);
|
|
|
|
}
|
2014-05-21 08:12:51 -07:00
|
|
|
|
|
|
|
break;
|
2013-08-28 09:10:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
parser.write();
|
|
|
|
return true;
|
|
|
|
} catch (IOException ex) {
|
2014-01-03 14:48:28 -08:00
|
|
|
Log.w(LOGTAG, "Failed to remove profile.", ex);
|
2013-08-28 09:10:07 -07:00
|
|
|
return false;
|
2012-06-18 10:03:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-03 15:27:42 -08:00
|
|
|
/**
|
|
|
|
* @return the default profile name for this application, or
|
|
|
|
* {@link GeckoProfile#DEFAULT_PROFILE} if none could be found.
|
|
|
|
*
|
|
|
|
* @throws NoMozillaDirectoryException
|
|
|
|
* if the Mozilla directory did not exist and could not be
|
|
|
|
* created.
|
|
|
|
*/
|
|
|
|
public static String getDefaultProfileName(final Context context) throws NoMozillaDirectoryException {
|
2012-11-27 12:35:43 -08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-03-03 15:27:42 -08:00
|
|
|
final String profileName = GeckoProfileDirectories.findDefaultProfileName(context);
|
|
|
|
if (profileName == null) {
|
|
|
|
// Note that we don't persist this back to profiles.ini.
|
|
|
|
sDefaultProfileName = DEFAULT_PROFILE;
|
|
|
|
return DEFAULT_PROFILE;
|
2013-08-27 11:05:56 -07:00
|
|
|
}
|
2013-08-28 09:10:07 -07:00
|
|
|
|
2014-03-03 15:27:42 -08:00
|
|
|
sDefaultProfileName = profileName;
|
|
|
|
return sDefaultProfileName;
|
2012-02-25 20:22:40 -08:00
|
|
|
}
|
|
|
|
|
2014-03-03 15:27:42 -08:00
|
|
|
private File findProfileDir() throws NoSuchProfileException {
|
|
|
|
return GeckoProfileDirectories.findProfileDir(mMozillaDir, mName);
|
2012-02-25 20:22:40 -08:00
|
|
|
}
|
|
|
|
|
2014-03-03 15:27:42 -08:00
|
|
|
private File createProfileDir() throws IOException {
|
|
|
|
INIParser parser = GeckoProfileDirectories.getProfilesINI(mMozillaDir);
|
2013-08-28 09:10:07 -07:00
|
|
|
|
|
|
|
// Salt the name of our requested profile
|
2014-03-03 15:27:42 -08:00
|
|
|
String saltedName = GeckoProfileDirectories.saltProfileName(mName);
|
|
|
|
File profileDir = new File(mMozillaDir, saltedName);
|
2013-08-28 09:10:07 -07:00
|
|
|
while (profileDir.exists()) {
|
2014-03-03 15:27:42 -08:00
|
|
|
saltedName = GeckoProfileDirectories.saltProfileName(mName);
|
|
|
|
profileDir = new File(mMozillaDir, saltedName);
|
2013-08-28 09:10:07 -07:00
|
|
|
}
|
2012-02-25 20:22:40 -08:00
|
|
|
|
2013-08-28 09:10:07 -07:00
|
|
|
// Attempt to create the salted profile dir
|
2014-01-03 14:48:28 -08:00
|
|
|
if (!profileDir.mkdirs()) {
|
|
|
|
throw new IOException("Unable to create profile.");
|
2012-02-25 20:22:40 -08:00
|
|
|
}
|
2014-01-03 14:48:28 -08:00
|
|
|
Log.d(LOGTAG, "Created new profile dir.");
|
2012-02-25 20:22:40 -08:00
|
|
|
|
2013-08-28 09:10:07 -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;
|
2014-01-09 10:18:55 -08:00
|
|
|
boolean isDefaultSet = false;
|
|
|
|
INISection profileSection;
|
|
|
|
while ((profileSection = parser.getSection("Profile" + profileNum)) != null) {
|
2013-08-28 09:10:07 -07:00
|
|
|
profileNum++;
|
2014-01-09 10:18:55 -08:00
|
|
|
if (profileSection.getProperty("Default") != null) {
|
|
|
|
isDefaultSet = true;
|
|
|
|
}
|
2013-08-28 09:10:07 -07:00
|
|
|
}
|
|
|
|
|
2014-01-09 10:18:55 -08:00
|
|
|
profileSection = new INISection("Profile" + profileNum);
|
2013-08-28 09:10:07 -07:00
|
|
|
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);
|
2014-01-09 10:18:55 -08:00
|
|
|
}
|
2013-08-28 09:10:07 -07:00
|
|
|
|
2014-06-16 16:07:43 -07:00
|
|
|
if (!isDefaultSet && !mIsWebAppProfile) {
|
2014-01-09 10:18:55 -08:00
|
|
|
// only set as default if this is the first non-webapp
|
|
|
|
// profile we're creating
|
2013-08-28 09:10:07 -07:00
|
|
|
profileSection.setProperty("Default", 1);
|
2014-05-12 15:16:51 -07:00
|
|
|
|
|
|
|
// We have no intention of stopping this session. The FIRSTRUN session
|
|
|
|
// ends when the browsing session/activity has ended. All events
|
|
|
|
// during firstrun will be tagged as FIRSTRUN.
|
|
|
|
Telemetry.startUISession(TelemetryContract.Session.FIRSTRUN);
|
2013-08-28 09:10:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
parser.addSection(profileSection);
|
|
|
|
parser.write();
|
|
|
|
|
2014-06-16 16:07:43 -07:00
|
|
|
// Trigger init for non-webapp profiles.
|
|
|
|
if (!mIsWebAppProfile) {
|
2014-09-30 15:19:26 -07:00
|
|
|
enqueueInitialization(profileDir);
|
2014-06-16 16:07:43 -07:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-08-21 17:18:42 -07:00
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(mApplicationContext);
|
|
|
|
final Intent intent = new Intent(BrowserApp.ACTION_NEW_PROFILE);
|
|
|
|
lbm.sendBroadcast(intent);
|
|
|
|
|
2012-02-25 20:22:40 -08:00
|
|
|
return profileDir;
|
|
|
|
}
|
2014-06-16 16:07:43 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is called once, immediately before creation of the profile
|
|
|
|
* directory completes.
|
|
|
|
*
|
|
|
|
* It queues up work to be done in the background to prepare the profile,
|
|
|
|
* such as adding default bookmarks.
|
2014-06-20 07:41:05 -07:00
|
|
|
*
|
|
|
|
* This is public for use *from tests only*!
|
2014-06-16 16:07:43 -07:00
|
|
|
*/
|
2014-06-20 07:41:05 -07:00
|
|
|
@RobocopTarget
|
2014-09-30 15:19:26 -07:00
|
|
|
public void enqueueInitialization(final File profileDir) {
|
2014-06-20 07:41:05 -07:00
|
|
|
Log.i(LOGTAG, "Enqueuing profile init.");
|
2014-06-16 16:07:43 -07:00
|
|
|
final Context context = mApplicationContext;
|
|
|
|
|
|
|
|
// Add everything when we're done loading the distribution.
|
|
|
|
final Distribution distribution = Distribution.getInstance(context);
|
|
|
|
distribution.addOnDistributionReadyCallback(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Log.d(LOGTAG, "Running post-distribution task: bookmarks.");
|
|
|
|
|
|
|
|
final ContentResolver cr = context.getContentResolver();
|
|
|
|
|
2014-09-30 15:19:26 -07:00
|
|
|
// Because we are running in the background, we want to synchronize on the
|
|
|
|
// GeckoProfile instance so that we don't race with main thread operations
|
|
|
|
// such as locking/unlocking/removing the profile.
|
|
|
|
synchronized (GeckoProfile.this) {
|
|
|
|
// Skip initialization if the profile directory has been removed.
|
|
|
|
if (!profileDir.exists()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We pass the number of added bookmarks to ensure that the
|
|
|
|
// indices of the distribution and default bookmarks are
|
|
|
|
// contiguous. Because there are always at least as many
|
|
|
|
// bookmarks as there are favicons, we can also guarantee that
|
|
|
|
// the favicon IDs won't overlap.
|
|
|
|
final LocalBrowserDB db = new LocalBrowserDB(getName());
|
|
|
|
final int offset = db.addDistributionBookmarks(cr, distribution, 0);
|
|
|
|
db.addDefaultBookmarks(context, cr, offset);
|
|
|
|
}
|
2014-06-16 16:07:43 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-02-25 20:22:40 -08:00
|
|
|
}
|