Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko; |
michael@0 | 7 | |
michael@0 | 8 | import org.mozilla.gecko.GeckoProfileDirectories.NoMozillaDirectoryException; |
michael@0 | 9 | import org.mozilla.gecko.GeckoProfileDirectories.NoSuchProfileException; |
michael@0 | 10 | import org.mozilla.gecko.Telemetry; |
michael@0 | 11 | import org.mozilla.gecko.TelemetryContract; |
michael@0 | 12 | import org.mozilla.gecko.util.INIParser; |
michael@0 | 13 | import org.mozilla.gecko.util.INISection; |
michael@0 | 14 | |
michael@0 | 15 | import android.content.Context; |
michael@0 | 16 | import android.text.TextUtils; |
michael@0 | 17 | import android.util.Log; |
michael@0 | 18 | |
michael@0 | 19 | import java.io.File; |
michael@0 | 20 | import java.io.FileOutputStream; |
michael@0 | 21 | import java.io.FileReader; |
michael@0 | 22 | import java.io.IOException; |
michael@0 | 23 | import java.io.OutputStreamWriter; |
michael@0 | 24 | import java.nio.charset.Charset; |
michael@0 | 25 | import java.util.Enumeration; |
michael@0 | 26 | import java.util.HashMap; |
michael@0 | 27 | import java.util.Hashtable; |
michael@0 | 28 | |
michael@0 | 29 | public final class GeckoProfile { |
michael@0 | 30 | private static final String LOGTAG = "GeckoProfile"; |
michael@0 | 31 | |
michael@0 | 32 | // Used to "lock" the guest profile, so that we'll always restart in it |
michael@0 | 33 | private static final String LOCK_FILE_NAME = ".active_lock"; |
michael@0 | 34 | public static final String DEFAULT_PROFILE = "default"; |
michael@0 | 35 | private static final String GUEST_PROFILE = "guest"; |
michael@0 | 36 | |
michael@0 | 37 | private static HashMap<String, GeckoProfile> sProfileCache = new HashMap<String, GeckoProfile>(); |
michael@0 | 38 | private static String sDefaultProfileName = null; |
michael@0 | 39 | |
michael@0 | 40 | public static boolean sIsUsingCustomProfile = false; |
michael@0 | 41 | private final String mName; |
michael@0 | 42 | private final File mMozillaDir; |
michael@0 | 43 | private File mProfileDir; // Not final because this is lazily computed. |
michael@0 | 44 | |
michael@0 | 45 | // Constants to cache whether or not a profile is "locked". |
michael@0 | 46 | private enum LockState { |
michael@0 | 47 | LOCKED, |
michael@0 | 48 | UNLOCKED, |
michael@0 | 49 | UNDEFINED |
michael@0 | 50 | }; |
michael@0 | 51 | |
michael@0 | 52 | // Caches whether or not a profile is "locked". Only used by the guest profile to determine if it should |
michael@0 | 53 | // be reused or deleted on startup |
michael@0 | 54 | private LockState mLocked = LockState.UNDEFINED; |
michael@0 | 55 | |
michael@0 | 56 | // Caches the guest profile dir. |
michael@0 | 57 | private static File sGuestDir = null; |
michael@0 | 58 | private static GeckoProfile sGuestProfile = null; |
michael@0 | 59 | |
michael@0 | 60 | private boolean mInGuestMode = false; |
michael@0 | 61 | |
michael@0 | 62 | |
michael@0 | 63 | public static GeckoProfile get(Context context) { |
michael@0 | 64 | boolean isGeckoApp = false; |
michael@0 | 65 | try { |
michael@0 | 66 | isGeckoApp = context instanceof GeckoApp; |
michael@0 | 67 | } catch (NoClassDefFoundError ex) {} |
michael@0 | 68 | |
michael@0 | 69 | if (isGeckoApp) { |
michael@0 | 70 | // Check for a cached profile on this context already |
michael@0 | 71 | // TODO: We should not be caching profile information on the Activity context |
michael@0 | 72 | final GeckoApp geckoApp = (GeckoApp) context; |
michael@0 | 73 | if (geckoApp.mProfile != null) { |
michael@0 | 74 | return geckoApp.mProfile; |
michael@0 | 75 | } |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | // If the guest profile exists and is locked, return it |
michael@0 | 79 | GeckoProfile guest = GeckoProfile.getGuestProfile(context); |
michael@0 | 80 | if (guest != null && guest.locked()) { |
michael@0 | 81 | return guest; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | if (isGeckoApp) { |
michael@0 | 85 | final GeckoApp geckoApp = (GeckoApp) context; |
michael@0 | 86 | String defaultProfileName; |
michael@0 | 87 | try { |
michael@0 | 88 | defaultProfileName = geckoApp.getDefaultProfileName(); |
michael@0 | 89 | } catch (NoMozillaDirectoryException e) { |
michael@0 | 90 | // If this failed, we're screwed. But there are so many callers that |
michael@0 | 91 | // we'll just throw a RuntimeException. |
michael@0 | 92 | Log.wtf(LOGTAG, "Unable to get default profile name.", e); |
michael@0 | 93 | throw new RuntimeException(e); |
michael@0 | 94 | } |
michael@0 | 95 | // Otherwise, get the default profile for the Activity. |
michael@0 | 96 | return get(context, defaultProfileName); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | return get(context, ""); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | public static GeckoProfile get(Context context, String profileName) { |
michael@0 | 103 | synchronized (sProfileCache) { |
michael@0 | 104 | GeckoProfile profile = sProfileCache.get(profileName); |
michael@0 | 105 | if (profile != null) |
michael@0 | 106 | return profile; |
michael@0 | 107 | } |
michael@0 | 108 | return get(context, profileName, (File)null); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | public static GeckoProfile get(Context context, String profileName, String profilePath) { |
michael@0 | 112 | File dir = null; |
michael@0 | 113 | if (!TextUtils.isEmpty(profilePath)) { |
michael@0 | 114 | dir = new File(profilePath); |
michael@0 | 115 | if (!dir.exists() || !dir.isDirectory()) { |
michael@0 | 116 | Log.w(LOGTAG, "requested profile directory missing: " + profilePath); |
michael@0 | 117 | } |
michael@0 | 118 | } |
michael@0 | 119 | return get(context, profileName, dir); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | public static GeckoProfile get(Context context, String profileName, File profileDir) { |
michael@0 | 123 | if (context == null) { |
michael@0 | 124 | throw new IllegalArgumentException("context must be non-null"); |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | // if no profile was passed in, look for the default profile listed in profiles.ini |
michael@0 | 128 | // if that doesn't exist, look for a profile called 'default' |
michael@0 | 129 | if (TextUtils.isEmpty(profileName) && profileDir == null) { |
michael@0 | 130 | try { |
michael@0 | 131 | profileName = GeckoProfile.getDefaultProfileName(context); |
michael@0 | 132 | } catch (NoMozillaDirectoryException e) { |
michael@0 | 133 | // We're unable to do anything sane here. |
michael@0 | 134 | throw new RuntimeException(e); |
michael@0 | 135 | } |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | // actually try to look up the profile |
michael@0 | 139 | synchronized (sProfileCache) { |
michael@0 | 140 | GeckoProfile profile = sProfileCache.get(profileName); |
michael@0 | 141 | if (profile == null) { |
michael@0 | 142 | try { |
michael@0 | 143 | profile = new GeckoProfile(context, profileName); |
michael@0 | 144 | } catch (NoMozillaDirectoryException e) { |
michael@0 | 145 | // We're unable to do anything sane here. |
michael@0 | 146 | throw new RuntimeException(e); |
michael@0 | 147 | } |
michael@0 | 148 | profile.setDir(profileDir); |
michael@0 | 149 | sProfileCache.put(profileName, profile); |
michael@0 | 150 | } else { |
michael@0 | 151 | profile.setDir(profileDir); |
michael@0 | 152 | } |
michael@0 | 153 | return profile; |
michael@0 | 154 | } |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | public static boolean removeProfile(Context context, String profileName) { |
michael@0 | 158 | final boolean success; |
michael@0 | 159 | try { |
michael@0 | 160 | success = new GeckoProfile(context, profileName).remove(); |
michael@0 | 161 | } catch (NoMozillaDirectoryException e) { |
michael@0 | 162 | Log.w(LOGTAG, "Unable to remove profile: no Mozilla directory.", e); |
michael@0 | 163 | return true; |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | if (success) { |
michael@0 | 167 | // Clear all shared prefs for the given profile. |
michael@0 | 168 | GeckoSharedPrefs.forProfileName(context, profileName) |
michael@0 | 169 | .edit().clear().commit(); |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | return success; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | public static GeckoProfile createGuestProfile(Context context) { |
michael@0 | 176 | try { |
michael@0 | 177 | removeGuestProfile(context); |
michael@0 | 178 | // We need to force the creation of a new guest profile if we want it outside of the normal profile path, |
michael@0 | 179 | // otherwise GeckoProfile.getDir will try to be smart and build it for us in the normal profiles dir. |
michael@0 | 180 | getGuestDir(context).mkdir(); |
michael@0 | 181 | GeckoProfile profile = getGuestProfile(context); |
michael@0 | 182 | profile.lock(); |
michael@0 | 183 | return profile; |
michael@0 | 184 | } catch (Exception ex) { |
michael@0 | 185 | Log.e(LOGTAG, "Error creating guest profile", ex); |
michael@0 | 186 | } |
michael@0 | 187 | return null; |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | public static void leaveGuestSession(Context context) { |
michael@0 | 191 | GeckoProfile profile = getGuestProfile(context); |
michael@0 | 192 | if (profile != null) { |
michael@0 | 193 | profile.unlock(); |
michael@0 | 194 | } |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | private static File getGuestDir(Context context) { |
michael@0 | 198 | if (sGuestDir == null) { |
michael@0 | 199 | sGuestDir = context.getFileStreamPath("guest"); |
michael@0 | 200 | } |
michael@0 | 201 | return sGuestDir; |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | private static GeckoProfile getGuestProfile(Context context) { |
michael@0 | 205 | if (sGuestProfile == null) { |
michael@0 | 206 | File guestDir = getGuestDir(context); |
michael@0 | 207 | if (guestDir.exists()) { |
michael@0 | 208 | sGuestProfile = get(context, GUEST_PROFILE, guestDir); |
michael@0 | 209 | sGuestProfile.mInGuestMode = true; |
michael@0 | 210 | } |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | return sGuestProfile; |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | public static boolean maybeCleanupGuestProfile(final Context context) { |
michael@0 | 217 | final GeckoProfile profile = getGuestProfile(context); |
michael@0 | 218 | |
michael@0 | 219 | if (profile == null) { |
michael@0 | 220 | return false; |
michael@0 | 221 | } else if (!profile.locked()) { |
michael@0 | 222 | profile.mInGuestMode = false; |
michael@0 | 223 | |
michael@0 | 224 | // If the guest dir exists, but it's unlocked, delete it |
michael@0 | 225 | removeGuestProfile(context); |
michael@0 | 226 | |
michael@0 | 227 | return true; |
michael@0 | 228 | } |
michael@0 | 229 | return false; |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | private static void removeGuestProfile(Context context) { |
michael@0 | 233 | boolean success = false; |
michael@0 | 234 | try { |
michael@0 | 235 | File guestDir = getGuestDir(context); |
michael@0 | 236 | if (guestDir.exists()) { |
michael@0 | 237 | success = delete(guestDir); |
michael@0 | 238 | } |
michael@0 | 239 | } catch (Exception ex) { |
michael@0 | 240 | Log.e(LOGTAG, "Error removing guest profile", ex); |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | if (success) { |
michael@0 | 244 | // Clear all shared prefs for the guest profile. |
michael@0 | 245 | GeckoSharedPrefs.forProfileName(context, GUEST_PROFILE) |
michael@0 | 246 | .edit().clear().commit(); |
michael@0 | 247 | } |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | public static boolean delete(File file) throws IOException { |
michael@0 | 251 | // Try to do a quick initial delete |
michael@0 | 252 | if (file.delete()) |
michael@0 | 253 | return true; |
michael@0 | 254 | |
michael@0 | 255 | if (file.isDirectory()) { |
michael@0 | 256 | // If the quick delete failed and this is a dir, recursively delete the contents of the dir |
michael@0 | 257 | String files[] = file.list(); |
michael@0 | 258 | for (String temp : files) { |
michael@0 | 259 | File fileDelete = new File(file, temp); |
michael@0 | 260 | delete(fileDelete); |
michael@0 | 261 | } |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | // Even if this is a dir, it should now be empty and delete should work |
michael@0 | 265 | return file.delete(); |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | private GeckoProfile(Context context, String profileName) throws NoMozillaDirectoryException { |
michael@0 | 269 | mName = profileName; |
michael@0 | 270 | mMozillaDir = GeckoProfileDirectories.getMozillaDirectory(context); |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | // Warning, Changing the lock file state from outside apis will cause this to become out of sync |
michael@0 | 274 | public boolean locked() { |
michael@0 | 275 | if (mLocked != LockState.UNDEFINED) { |
michael@0 | 276 | return mLocked == LockState.LOCKED; |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | // Don't use getDir() as it will create a dir if none exists |
michael@0 | 280 | if (mProfileDir != null && mProfileDir.exists()) { |
michael@0 | 281 | File lockFile = new File(mProfileDir, LOCK_FILE_NAME); |
michael@0 | 282 | boolean res = lockFile.exists(); |
michael@0 | 283 | mLocked = res ? LockState.LOCKED : LockState.UNLOCKED; |
michael@0 | 284 | } else { |
michael@0 | 285 | mLocked = LockState.UNLOCKED; |
michael@0 | 286 | } |
michael@0 | 287 | |
michael@0 | 288 | return mLocked == LockState.LOCKED; |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | public boolean lock() { |
michael@0 | 292 | try { |
michael@0 | 293 | // If this dir doesn't exist getDir will create it for us |
michael@0 | 294 | File lockFile = new File(getDir(), LOCK_FILE_NAME); |
michael@0 | 295 | boolean result = lockFile.createNewFile(); |
michael@0 | 296 | if (result) { |
michael@0 | 297 | mLocked = LockState.LOCKED; |
michael@0 | 298 | } else { |
michael@0 | 299 | mLocked = LockState.UNLOCKED; |
michael@0 | 300 | } |
michael@0 | 301 | return result; |
michael@0 | 302 | } catch(IOException ex) { |
michael@0 | 303 | Log.e(LOGTAG, "Error locking profile", ex); |
michael@0 | 304 | } |
michael@0 | 305 | mLocked = LockState.UNLOCKED; |
michael@0 | 306 | return false; |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | public boolean unlock() { |
michael@0 | 310 | // Don't use getDir() as it will create a dir |
michael@0 | 311 | if (mProfileDir == null || !mProfileDir.exists()) { |
michael@0 | 312 | return true; |
michael@0 | 313 | } |
michael@0 | 314 | |
michael@0 | 315 | try { |
michael@0 | 316 | File lockFile = new File(mProfileDir, LOCK_FILE_NAME); |
michael@0 | 317 | boolean result = delete(lockFile); |
michael@0 | 318 | if (result) { |
michael@0 | 319 | mLocked = LockState.UNLOCKED; |
michael@0 | 320 | } else { |
michael@0 | 321 | mLocked = LockState.LOCKED; |
michael@0 | 322 | } |
michael@0 | 323 | return result; |
michael@0 | 324 | } catch(IOException ex) { |
michael@0 | 325 | Log.e(LOGTAG, "Error unlocking profile", ex); |
michael@0 | 326 | } |
michael@0 | 327 | mLocked = LockState.LOCKED; |
michael@0 | 328 | return false; |
michael@0 | 329 | } |
michael@0 | 330 | |
michael@0 | 331 | public boolean inGuestMode() { |
michael@0 | 332 | return mInGuestMode; |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | private void setDir(File dir) { |
michael@0 | 336 | if (dir != null && dir.exists() && dir.isDirectory()) { |
michael@0 | 337 | mProfileDir = dir; |
michael@0 | 338 | } |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | public String getName() { |
michael@0 | 342 | return mName; |
michael@0 | 343 | } |
michael@0 | 344 | |
michael@0 | 345 | public synchronized File getDir() { |
michael@0 | 346 | forceCreate(); |
michael@0 | 347 | return mProfileDir; |
michael@0 | 348 | } |
michael@0 | 349 | |
michael@0 | 350 | public synchronized GeckoProfile forceCreate() { |
michael@0 | 351 | if (mProfileDir != null) { |
michael@0 | 352 | return this; |
michael@0 | 353 | } |
michael@0 | 354 | |
michael@0 | 355 | try { |
michael@0 | 356 | // Check if a profile with this name already exists. |
michael@0 | 357 | try { |
michael@0 | 358 | mProfileDir = findProfileDir(); |
michael@0 | 359 | Log.d(LOGTAG, "Found profile dir."); |
michael@0 | 360 | } catch (NoSuchProfileException noSuchProfile) { |
michael@0 | 361 | // If it doesn't exist, create it. |
michael@0 | 362 | mProfileDir = createProfileDir(); |
michael@0 | 363 | } |
michael@0 | 364 | } catch (IOException ioe) { |
michael@0 | 365 | Log.e(LOGTAG, "Error getting profile dir", ioe); |
michael@0 | 366 | } |
michael@0 | 367 | return this; |
michael@0 | 368 | } |
michael@0 | 369 | |
michael@0 | 370 | public File getFile(String aFile) { |
michael@0 | 371 | File f = getDir(); |
michael@0 | 372 | if (f == null) |
michael@0 | 373 | return null; |
michael@0 | 374 | |
michael@0 | 375 | return new File(f, aFile); |
michael@0 | 376 | } |
michael@0 | 377 | |
michael@0 | 378 | /** |
michael@0 | 379 | * Moves the session file to the backup session file. |
michael@0 | 380 | * |
michael@0 | 381 | * sessionstore.js should hold the current session, and sessionstore.bak |
michael@0 | 382 | * should hold the previous session (where it is used to read the "tabs |
michael@0 | 383 | * from last time"). Normally, sessionstore.js is moved to sessionstore.bak |
michael@0 | 384 | * on a clean quit, but this doesn't happen if Fennec crashed. Thus, this |
michael@0 | 385 | * method should be called after a crash so sessionstore.bak correctly |
michael@0 | 386 | * holds the previous session. |
michael@0 | 387 | */ |
michael@0 | 388 | public void moveSessionFile() { |
michael@0 | 389 | File sessionFile = getFile("sessionstore.js"); |
michael@0 | 390 | if (sessionFile != null && sessionFile.exists()) { |
michael@0 | 391 | File sessionFileBackup = getFile("sessionstore.bak"); |
michael@0 | 392 | sessionFile.renameTo(sessionFileBackup); |
michael@0 | 393 | } |
michael@0 | 394 | } |
michael@0 | 395 | |
michael@0 | 396 | /** |
michael@0 | 397 | * Get the string from a session file. |
michael@0 | 398 | * |
michael@0 | 399 | * The session can either be read from sessionstore.js or sessionstore.bak. |
michael@0 | 400 | * In general, sessionstore.js holds the current session, and |
michael@0 | 401 | * sessionstore.bak holds the previous session. |
michael@0 | 402 | * |
michael@0 | 403 | * @param readBackup if true, the session is read from sessionstore.bak; |
michael@0 | 404 | * otherwise, the session is read from sessionstore.js |
michael@0 | 405 | * |
michael@0 | 406 | * @return the session string |
michael@0 | 407 | */ |
michael@0 | 408 | public String readSessionFile(boolean readBackup) { |
michael@0 | 409 | File sessionFile = getFile(readBackup ? "sessionstore.bak" : "sessionstore.js"); |
michael@0 | 410 | |
michael@0 | 411 | try { |
michael@0 | 412 | if (sessionFile != null && sessionFile.exists()) { |
michael@0 | 413 | return readFile(sessionFile); |
michael@0 | 414 | } |
michael@0 | 415 | } catch (IOException ioe) { |
michael@0 | 416 | Log.e(LOGTAG, "Unable to read session file", ioe); |
michael@0 | 417 | } |
michael@0 | 418 | return null; |
michael@0 | 419 | } |
michael@0 | 420 | |
michael@0 | 421 | public String readFile(String filename) throws IOException { |
michael@0 | 422 | File dir = getDir(); |
michael@0 | 423 | if (dir == null) { |
michael@0 | 424 | throw new IOException("No profile directory found"); |
michael@0 | 425 | } |
michael@0 | 426 | File target = new File(dir, filename); |
michael@0 | 427 | return readFile(target); |
michael@0 | 428 | } |
michael@0 | 429 | |
michael@0 | 430 | private String readFile(File target) throws IOException { |
michael@0 | 431 | FileReader fr = new FileReader(target); |
michael@0 | 432 | try { |
michael@0 | 433 | StringBuilder sb = new StringBuilder(); |
michael@0 | 434 | char[] buf = new char[8192]; |
michael@0 | 435 | int read = fr.read(buf); |
michael@0 | 436 | while (read >= 0) { |
michael@0 | 437 | sb.append(buf, 0, read); |
michael@0 | 438 | read = fr.read(buf); |
michael@0 | 439 | } |
michael@0 | 440 | return sb.toString(); |
michael@0 | 441 | } finally { |
michael@0 | 442 | fr.close(); |
michael@0 | 443 | } |
michael@0 | 444 | } |
michael@0 | 445 | |
michael@0 | 446 | private boolean remove() { |
michael@0 | 447 | try { |
michael@0 | 448 | final File dir = getDir(); |
michael@0 | 449 | if (dir.exists()) { |
michael@0 | 450 | delete(dir); |
michael@0 | 451 | } |
michael@0 | 452 | |
michael@0 | 453 | try { |
michael@0 | 454 | mProfileDir = findProfileDir(); |
michael@0 | 455 | } catch (NoSuchProfileException noSuchProfile) { |
michael@0 | 456 | // If the profile doesn't exist, there's nothing left for us to do. |
michael@0 | 457 | return false; |
michael@0 | 458 | } |
michael@0 | 459 | |
michael@0 | 460 | final INIParser parser = GeckoProfileDirectories.getProfilesINI(mMozillaDir); |
michael@0 | 461 | final Hashtable<String, INISection> sections = parser.getSections(); |
michael@0 | 462 | for (Enumeration<INISection> e = sections.elements(); e.hasMoreElements();) { |
michael@0 | 463 | final INISection section = e.nextElement(); |
michael@0 | 464 | String name = section.getStringProperty("Name"); |
michael@0 | 465 | |
michael@0 | 466 | if (name == null || !name.equals(mName)) { |
michael@0 | 467 | continue; |
michael@0 | 468 | } |
michael@0 | 469 | |
michael@0 | 470 | if (section.getName().startsWith("Profile")) { |
michael@0 | 471 | // ok, we have stupid Profile#-named things. Rename backwards. |
michael@0 | 472 | try { |
michael@0 | 473 | int sectionNumber = Integer.parseInt(section.getName().substring("Profile".length())); |
michael@0 | 474 | String curSection = "Profile" + sectionNumber; |
michael@0 | 475 | String nextSection = "Profile" + (sectionNumber+1); |
michael@0 | 476 | |
michael@0 | 477 | sections.remove(curSection); |
michael@0 | 478 | |
michael@0 | 479 | while (sections.containsKey(nextSection)) { |
michael@0 | 480 | parser.renameSection(nextSection, curSection); |
michael@0 | 481 | sectionNumber++; |
michael@0 | 482 | |
michael@0 | 483 | curSection = nextSection; |
michael@0 | 484 | nextSection = "Profile" + (sectionNumber+1); |
michael@0 | 485 | } |
michael@0 | 486 | } catch (NumberFormatException nex) { |
michael@0 | 487 | // uhm, malformed Profile thing; we can't do much. |
michael@0 | 488 | Log.e(LOGTAG, "Malformed section name in profiles.ini: " + section.getName()); |
michael@0 | 489 | return false; |
michael@0 | 490 | } |
michael@0 | 491 | } else { |
michael@0 | 492 | // this really shouldn't be the case, but handle it anyway |
michael@0 | 493 | parser.removeSection(mName); |
michael@0 | 494 | } |
michael@0 | 495 | |
michael@0 | 496 | break; |
michael@0 | 497 | } |
michael@0 | 498 | |
michael@0 | 499 | parser.write(); |
michael@0 | 500 | return true; |
michael@0 | 501 | } catch (IOException ex) { |
michael@0 | 502 | Log.w(LOGTAG, "Failed to remove profile.", ex); |
michael@0 | 503 | return false; |
michael@0 | 504 | } |
michael@0 | 505 | } |
michael@0 | 506 | |
michael@0 | 507 | /** |
michael@0 | 508 | * @return the default profile name for this application, or |
michael@0 | 509 | * {@link GeckoProfile#DEFAULT_PROFILE} if none could be found. |
michael@0 | 510 | * |
michael@0 | 511 | * @throws NoMozillaDirectoryException |
michael@0 | 512 | * if the Mozilla directory did not exist and could not be |
michael@0 | 513 | * created. |
michael@0 | 514 | */ |
michael@0 | 515 | public static String getDefaultProfileName(final Context context) throws NoMozillaDirectoryException { |
michael@0 | 516 | // Have we read the default profile from the INI already? |
michael@0 | 517 | // Changing the default profile requires a restart, so we don't |
michael@0 | 518 | // need to worry about runtime changes. |
michael@0 | 519 | if (sDefaultProfileName != null) { |
michael@0 | 520 | return sDefaultProfileName; |
michael@0 | 521 | } |
michael@0 | 522 | |
michael@0 | 523 | final String profileName = GeckoProfileDirectories.findDefaultProfileName(context); |
michael@0 | 524 | if (profileName == null) { |
michael@0 | 525 | // Note that we don't persist this back to profiles.ini. |
michael@0 | 526 | sDefaultProfileName = DEFAULT_PROFILE; |
michael@0 | 527 | return DEFAULT_PROFILE; |
michael@0 | 528 | } |
michael@0 | 529 | |
michael@0 | 530 | sDefaultProfileName = profileName; |
michael@0 | 531 | return sDefaultProfileName; |
michael@0 | 532 | } |
michael@0 | 533 | |
michael@0 | 534 | private File findProfileDir() throws NoSuchProfileException { |
michael@0 | 535 | return GeckoProfileDirectories.findProfileDir(mMozillaDir, mName); |
michael@0 | 536 | } |
michael@0 | 537 | |
michael@0 | 538 | private File createProfileDir() throws IOException { |
michael@0 | 539 | INIParser parser = GeckoProfileDirectories.getProfilesINI(mMozillaDir); |
michael@0 | 540 | |
michael@0 | 541 | // Salt the name of our requested profile |
michael@0 | 542 | String saltedName = GeckoProfileDirectories.saltProfileName(mName); |
michael@0 | 543 | File profileDir = new File(mMozillaDir, saltedName); |
michael@0 | 544 | while (profileDir.exists()) { |
michael@0 | 545 | saltedName = GeckoProfileDirectories.saltProfileName(mName); |
michael@0 | 546 | profileDir = new File(mMozillaDir, saltedName); |
michael@0 | 547 | } |
michael@0 | 548 | |
michael@0 | 549 | // Attempt to create the salted profile dir |
michael@0 | 550 | if (!profileDir.mkdirs()) { |
michael@0 | 551 | throw new IOException("Unable to create profile."); |
michael@0 | 552 | } |
michael@0 | 553 | Log.d(LOGTAG, "Created new profile dir."); |
michael@0 | 554 | |
michael@0 | 555 | // Now update profiles.ini |
michael@0 | 556 | // If this is the first time its created, we also add a General section |
michael@0 | 557 | // look for the first profile number that isn't taken yet |
michael@0 | 558 | int profileNum = 0; |
michael@0 | 559 | boolean isDefaultSet = false; |
michael@0 | 560 | INISection profileSection; |
michael@0 | 561 | while ((profileSection = parser.getSection("Profile" + profileNum)) != null) { |
michael@0 | 562 | profileNum++; |
michael@0 | 563 | if (profileSection.getProperty("Default") != null) { |
michael@0 | 564 | isDefaultSet = true; |
michael@0 | 565 | } |
michael@0 | 566 | } |
michael@0 | 567 | |
michael@0 | 568 | profileSection = new INISection("Profile" + profileNum); |
michael@0 | 569 | profileSection.setProperty("Name", mName); |
michael@0 | 570 | profileSection.setProperty("IsRelative", 1); |
michael@0 | 571 | profileSection.setProperty("Path", saltedName); |
michael@0 | 572 | |
michael@0 | 573 | if (parser.getSection("General") == null) { |
michael@0 | 574 | INISection generalSection = new INISection("General"); |
michael@0 | 575 | generalSection.setProperty("StartWithLastProfile", 1); |
michael@0 | 576 | parser.addSection(generalSection); |
michael@0 | 577 | } |
michael@0 | 578 | |
michael@0 | 579 | if (!isDefaultSet && !mName.startsWith("webapp")) { |
michael@0 | 580 | // only set as default if this is the first non-webapp |
michael@0 | 581 | // profile we're creating |
michael@0 | 582 | profileSection.setProperty("Default", 1); |
michael@0 | 583 | |
michael@0 | 584 | // We have no intention of stopping this session. The FIRSTRUN session |
michael@0 | 585 | // ends when the browsing session/activity has ended. All events |
michael@0 | 586 | // during firstrun will be tagged as FIRSTRUN. |
michael@0 | 587 | Telemetry.startUISession(TelemetryContract.Session.FIRSTRUN); |
michael@0 | 588 | } |
michael@0 | 589 | |
michael@0 | 590 | parser.addSection(profileSection); |
michael@0 | 591 | parser.write(); |
michael@0 | 592 | |
michael@0 | 593 | // Write out profile creation time, mirroring the logic in nsToolkitProfileService. |
michael@0 | 594 | try { |
michael@0 | 595 | FileOutputStream stream = new FileOutputStream(profileDir.getAbsolutePath() + File.separator + "times.json"); |
michael@0 | 596 | OutputStreamWriter writer = new OutputStreamWriter(stream, Charset.forName("UTF-8")); |
michael@0 | 597 | try { |
michael@0 | 598 | writer.append("{\"created\": " + System.currentTimeMillis() + "}\n"); |
michael@0 | 599 | } finally { |
michael@0 | 600 | writer.close(); |
michael@0 | 601 | } |
michael@0 | 602 | } catch (Exception e) { |
michael@0 | 603 | // Best-effort. |
michael@0 | 604 | Log.w(LOGTAG, "Couldn't write times.json.", e); |
michael@0 | 605 | } |
michael@0 | 606 | |
michael@0 | 607 | return profileDir; |
michael@0 | 608 | } |
michael@0 | 609 | } |