Bug 1497148 - Log warning for custom profile with null dir and avoid throwing exception. r=geckoview-reviewers,snorp, a=RyanVM
authorPetru Lingurar <petru.lingurar@softvision.ro>
Tue, 11 Dec 2018 17:56:11 +0000
changeset 508947 7fe309a250d8481dbe3f7238917bcc32e058486e
parent 508946 66fd4c2ab08185187eda60af2f32f82c3da325bd
child 508948 100268636829cc2ed0f5618d130335f86bad6311
push id1905
push userffxbld-merge
push dateMon, 21 Jan 2019 12:33:13 +0000
treeherdermozilla-release@c2fca1944d8c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgeckoview-reviewers, snorp, RyanVM
bugs1497148
milestone65.0
Bug 1497148 - Log warning for custom profile with null dir and avoid throwing exception. r=geckoview-reviewers,snorp, a=RyanVM Differential Revision: https://phabricator.services.mozilla.com/D13969
mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoProfile.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoProfile.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoProfile.java
@@ -149,17 +149,18 @@ public final class GeckoProfile {
         if (args != null && args.contains("-profile")) {
             final Pattern p = Pattern.compile("(?:-profile\\s*)(\\S*)(\\s*)");
             final Matcher m = p.matcher(args);
             if (m.find()) {
                 profilePath =  m.group(1);
             }
         }
 
-        if (profileName == null && profilePath == null) {
+        if (TextUtils.isEmpty(profileName) && profilePath == null) {
+            informIfCustomProfileIsUnavailable(profileName, false);
             // Get the default profile for the Activity.
             return getDefaultProfile(context);
         }
 
         return GeckoProfile.get(context, profileName, profilePath);
     }
 
     private static GeckoProfile getDefaultProfile(Context context) {
@@ -207,24 +208,30 @@ public final class GeckoProfile {
         }
 
         // Null name? | Null dir? | Returned profile
         // ------------------------------------------
         //     Yes    |    Yes    | Active profile or default profile.
         //     No     |    Yes    | Profile with specified name at default dir.
         //     Yes    |    No     | Custom (anonymous) profile with specified dir.
         //     No     |    No     | Profile with specified name at specified dir.
+        //
+        // Empty name?| Null dir? | Returned profile
+        // ------------------------------------------
+        //     Yes    |    Yes    | Active profile or default profile
 
-        if (profileName == null && profileDir == null) {
+        if (TextUtils.isEmpty(profileName) && profileDir == null) {
             // If no profile info was passed in, look for the active profile or a default profile.
             final GeckoProfile profile = GeckoThread.getActiveProfile();
             if (profile != null) {
+                informIfCustomProfileIsUnavailable(profileName, true);
                 return profile;
             }
 
+            informIfCustomProfileIsUnavailable(profileName, false);
             return GeckoProfile.initFromArgs(context, sIntentArgs);
         } else if (profileName == null) {
             // If only profile dir was passed in, use custom (anonymous) profile.
             profileName = CUSTOM_PROFILE;
         }
 
         // We require the profile dir to exist if specified, so create it here if needed.
         final boolean init = profileDir != null && profileDir.mkdirs();
@@ -268,16 +275,38 @@ public final class GeckoProfile {
         if (init) {
             // Initialize the profile directory if we had to create it.
             profile.enqueueInitialization(profileDir);
         }
 
         return profile;
     }
 
+    /**
+     * Custom profiles are an edge use case (must be passed in via Intent arguments)<br>
+     * Will inform users if the received arguments are invalid and the app fallbacks to use
+     * the currently active or the default Gecko profile.<br>
+     * Only to be called if other conditions than the profile name are already checked.
+     *
+     * @see <a href="http://google.com">Reasoning behind custom profiles</a>
+     *
+     * @param profileName intended profile name. Will be checked against {{@link #CUSTOM_PROFILE}}
+     *                    to decide if we should inform or not about using the fallback profile.
+     * @param activeOrDefaultProfileFallback true - will fallback to use the currently active Gecko profile
+     *                                       false - will fallback to use the default Gecko profile
+     */
+    private static void informIfCustomProfileIsUnavailable(
+            final String profileName, final boolean activeOrDefaultProfileFallback) {
+        if (CUSTOM_PROFILE.equals(profileName)) {
+            final String fallbackProfileName = activeOrDefaultProfileFallback ? "active" : "default";
+            Log.w(LOGTAG, String.format("Custom profile must have a directory specified! " +
+                    "Reverting to use the %s profile", fallbackProfileName));
+        }
+    }
+
     // Currently unused outside of testing.
     @RobocopTarget
     public static boolean removeProfile(final Context context, final GeckoProfile profile) {
         final boolean success = profile.remove();
 
         if (success) {
             // Clear all shared prefs for the given profile.
             GeckoSharedPrefs.forProfileName(context, profile.getName())
@@ -308,18 +337,16 @@ public final class GeckoProfile {
         } catch (final IOException e) {
             return false;
         }
     }
 
     private GeckoProfile(Context context, String profileName, File profileDir) throws NoMozillaDirectoryException {
         if (profileName == null) {
             throw new IllegalArgumentException("Unable to create GeckoProfile for empty profile name.");
-        } else if (CUSTOM_PROFILE.equals(profileName) && profileDir == null) {
-            throw new IllegalArgumentException("Custom profile must have a directory");
         }
 
         mName = profileName;
         mMozillaDir = GeckoProfileDirectories.getMozillaDirectory(context);
 
         mProfileDir = profileDir;
         if (profileDir != null && !profileDir.isDirectory()) {
             throw new IllegalArgumentException("Profile directory must exist if specified.");