Example #1
0
  private File createProfileDir(File mozillaDir) throws IOException {
    INIParser parser = getProfilesINI(mContext);

    // Salt the name of our requested profile
    String saltedName = saltProfileName(mName);
    File profileDir = new File(mozillaDir, saltedName);
    while (profileDir.exists()) {
      saltedName = saltProfileName(mName);
      profileDir = new File(mozillaDir, saltedName);
    }

    // Attempt to create the salted profile dir
    if (!profileDir.mkdirs()) {
      throw new IOException("Unable to create profile.");
    }
    Log.d(LOGTAG, "Created new profile dir.");

    // 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++;
    }

    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();

    // 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);
    }

    return profileDir;
  }
Example #2
0
  private boolean remove() {
    try {
      File dir = getDir();
      if (dir.exists()) delete(dir);

      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.", ex);
      return false;
    }
  }
  private File createProfileDir() throws IOException {
    INIParser parser = GeckoProfileDirectories.getProfilesINI(mMozillaDir);

    // Salt the name of our requested profile
    String saltedName = GeckoProfileDirectories.saltProfileName(mName);
    File profileDir = new File(mMozillaDir, saltedName);
    while (profileDir.exists()) {
      saltedName = GeckoProfileDirectories.saltProfileName(mName);
      profileDir = new File(mMozillaDir, saltedName);
    }

    // Attempt to create the salted profile dir
    if (!profileDir.mkdirs()) {
      throw new IOException("Unable to create profile.");
    }
    Log.d(LOGTAG, "Created new profile dir.");

    // 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;
    boolean isDefaultSet = false;
    INISection profileSection;
    while ((profileSection = parser.getSection("Profile" + profileNum)) != null) {
      profileNum++;
      if (profileSection.getProperty("Default") != null) {
        isDefaultSet = true;
      }
    }

    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);
    }

    if (!isDefaultSet && !mIsWebAppProfile) {
      // only set as default if this is the first non-webapp
      // profile we're creating
      profileSection.setProperty("Default", 1);

      // 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);
    }

    parser.addSection(profileSection);
    parser.write();

    // Trigger init for non-webapp profiles.
    if (!mIsWebAppProfile) {
      enqueueInitialization();
    }

    // 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);
    }

    // Initialize pref flag for displaying the start pane for a new non-webapp profile.
    if (!mIsWebAppProfile) {
      final SharedPreferences prefs = GeckoSharedPrefs.forProfile(mApplicationContext);
      prefs.edit().putBoolean(BrowserApp.PREF_STARTPANE_ENABLED, true).apply();
    }

    return profileDir;
  }
  private boolean remove() {
    try {
      synchronized (this) {
        final File dir = getDir();
        if (dir.exists()) {
          delete(dir);
        }

        try {
          mProfileDir = findProfileDir();
        } catch (NoSuchProfileException noSuchProfile) {
          // If the profile doesn't exist, there's nothing left for us to do.
          return false;
        }
      }

      final INIParser parser = GeckoProfileDirectories.getProfilesINI(mMozillaDir);
      final Hashtable<String, INISection> sections = parser.getSections();
      for (Enumeration<INISection> e = sections.elements(); e.hasMoreElements(); ) {
        final 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);
        }

        break;
      }

      parser.write();
      return true;
    } catch (IOException ex) {
      Log.w(LOGTAG, "Failed to remove profile.", ex);
      return false;
    }
  }