Bug 809942 - ToolkitProfile service can't change default profile r=wesj
authorMark Finkle <mfinkle@mozilla.com>
Tue, 27 Nov 2012 15:35:43 -0500
changeset 114275 b5b4caaea44f44d45e9dd1de59f721a230e72012
parent 114274 d0a5bc47e590dafaa9884755e4ccd5e801576d39
child 114276 987aea26a43a02bb471298ee20ce45ae1310a802
push id23913
push useremorley@mozilla.com
push dateWed, 28 Nov 2012 17:11:31 +0000
treeherdermozilla-central@17c267a881cf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswesj
bugs809942
milestone20.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 809942 - ToolkitProfile service can't change default profile r=wesj
mobile/android/base/App.java.in
mobile/android/base/GeckoProfile.java
--- a/mobile/android/base/App.java.in
+++ b/mobile/android/base/App.java.in
@@ -6,31 +6,33 @@
 #filter substitution
 package @ANDROID_PACKAGE_NAME@;
 
 import android.view.MenuItem;
 
 import org.mozilla.gecko.BrowserApp;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoEvent;
+import org.mozilla.gecko.GeckoProfile;
 import org.mozilla.gecko.R;
 
 public class App extends BrowserApp {
     public int getLayout() { return R.layout.gecko_app; }
 
     public String getPackageName() {
         return "@ANDROID_PACKAGE_NAME@";
     }
 
     public String getContentProcessName() {
         return "@MOZ_CHILD_PROCESS_NAME@";
     }
 
     protected String getDefaultProfileName() {
-      return "default";
+        String profile = GeckoProfile.findDefaultProfile(this);
+        return (profile != null ? profile : "default");
     }
 
     public String getDefaultUAString() {
         String deviceType = "Mobile";
         if (GeckoAppShell.isTablet())
             deviceType = "Tablet";
         return "Mozilla/5.0 (Android; " + deviceType + "; rv:@MOZ_APP_VERSION@) Gecko/@MOZ_APP_VERSION@ Firefox/@MOZ_APP_VERSION@";
     }
--- a/mobile/android/base/GeckoProfile.java
+++ b/mobile/android/base/GeckoProfile.java
@@ -23,16 +23,17 @@ import java.io.IOException;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Hashtable;
 
 public final class GeckoProfile {
     private static final String LOGTAG = "GeckoProfile";
 
     private static HashMap<String, GeckoProfile> sProfileCache = new HashMap<String, GeckoProfile>();
+    private static String sDefaultProfileName = null;
 
     private final Context mContext;
     private final String mName;
     private File mMozDir;
     private File mDir;
 
     // this short timeout is a temporary fix until bug 735399 is implemented
     private static final long SESSION_TIMEOUT = 30 * 1000; // 30 seconds
@@ -47,43 +48,35 @@ public final class GeckoProfile {
     public static GeckoProfile get(Context context) {
         if (context instanceof GeckoApp)
             return get(context, ((GeckoApp)context).getDefaultProfileName());
 
         return get(context, "");
     }
 
     public static GeckoProfile get(Context context, String profileName) {
+        synchronized (sProfileCache) {
+            GeckoProfile profile = sProfileCache.get(profileName);
+            if (profile != null)
+                return profile;
+        }
         return get(context, profileName, null);
     }
 
     public static GeckoProfile get(Context context, String profileName, String profilePath) {
         if (context == null) {
             throw new IllegalArgumentException("context must be non-null");
         }
 
         // 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'
         if (TextUtils.isEmpty(profileName) && TextUtils.isEmpty(profilePath)) {
-            profileName = "default";
-
-            INIParser parser = getProfilesINI(context);
-
-            String profile = "";
-            boolean foundDefault = false;
-            for (Enumeration<INISection> e = parser.getSections().elements(); e.hasMoreElements();) {
-                INISection section = e.nextElement();
-                if (section.getIntProperty("Default") == 1) {
-                    profile = section.getStringProperty("Name");
-                    foundDefault = true;
-                }
-            }
-
-            if (foundDefault)
-                profileName = profile;
+            profileName = GeckoProfile.findDefaultProfile(context);
+            if (profileName == null)
+                profileName = "default";
         }
 
         // actually try to look up the profile
         synchronized (sProfileCache) {
             GeckoProfile profile = sProfileCache.get(profileName);
             if (profile == null) {
                 profile = new GeckoProfile(context, profileName, profilePath);
                 sProfileCache.put(profileName, profile);
@@ -121,22 +114,19 @@ public final class GeckoProfile {
         mName = profileName;
         setDir(profilePath);
     }
 
     private void setDir(String profilePath) {
         if (!TextUtils.isEmpty(profilePath)) {
             File dir = new File(profilePath);
             if (dir.exists() && dir.isDirectory()) {
-                if (mDir != null) {
-                    Log.i(LOGTAG, "profile dir changed from "+mDir+" to "+dir);
-                }
                 mDir = dir;
             } else {
-                Log.w(LOGTAG, "requested profile directory missing: "+profilePath);
+                Log.w(LOGTAG, "requested profile directory missing: " + profilePath);
             }
         }
     }
 
     public String getName() {
         return mName;
     }
 
@@ -318,16 +308,38 @@ public final class GeckoProfile {
             parser.write();
             return true;
         } catch (IOException ex) {
             Log.w(LOGTAG, "Failed to remove profile " + mName + ":\n" + ex);
             return false;
         }
     }
 
+    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;
+    }
+
     private File findProfileDir(File mozillaDir) {
         // 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)) {