Bug 1497148 - Log warning for custom profile with null dir and avoid throwing exception. r=geckoview-reviewers,snorp
authorPetru Lingurar <petru.lingurar@softvision.ro>
Tue, 11 Dec 2018 17:56:11 +0000
changeset 449953 c0b651621eb8c07dc454d25df9e3ebfa97de4c22
parent 449952 4db0d3e10cb162b850e90948015a7ed99e18b0e5
child 449954 d06fc29f4203056cc929b2d93207883cc0e4cfbe
push id35188
push userccoroiu@mozilla.com
push dateTue, 11 Dec 2018 21:11:28 +0000
treeherdermozilla-central@aee6c9dbb430 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgeckoview-reviewers, snorp
bugs1497148
milestone66.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 1497148 - Log warning for custom profile with null dir and avoid throwing exception. r=geckoview-reviewers,snorp 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.");