Bug 1035131 - Factor out methods to encode/decode user-entered:// URLs (r=mfinkle)
authorLucas Rocha <lucasr@mozilla.com>
Mon, 07 Jul 2014 15:59:52 +0100
changeset 192621 b459090bc1e3f4c3a75384f9f3f9dbf34657d297
parent 192620 c9eb74c5f3fd8e9b3422e54a80b69c27b309cc75
child 192622 f26a0f525fb457c09ccd0c4e52a7be82372391fe
push id27091
push userkwierso@gmail.com
push dateMon, 07 Jul 2014 21:11:34 +0000
treeherdermozilla-central@f14b47c39013 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs1035131
milestone33.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 1035131 - Factor out methods to encode/decode user-entered:// URLs (r=mfinkle)
mobile/android/base/home/HomeFragment.java
mobile/android/base/home/PinSiteDialog.java
mobile/android/base/home/TopSitesGridView.java
mobile/android/base/home/TopSitesPanel.java
mobile/android/base/util/StringUtils.java
--- a/mobile/android/base/home/HomeFragment.java
+++ b/mobile/android/base/home/HomeFragment.java
@@ -191,17 +191,17 @@ abstract class HomeFragment extends Frag
             }
 
             Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL, TelemetryContract.Method.CONTEXT_MENU);
 
             final String url = (info.isInReadingList() ? ReaderModeUtils.getAboutReaderForUrl(info.url) : info.url);
 
             // Some pinned site items have "user-entered" urls. URLs entered in the PinSiteDialog are wrapped in
             // a special URI until we can get a valid URL. If the url is a user-entered url, decode the URL before loading it.
-            final Tab newTab = Tabs.getInstance().loadUrl(decodeUserEnteredUrl(url), flags);
+            final Tab newTab = Tabs.getInstance().loadUrl(StringUtils.decodeUserEnteredUrl(url), flags);
             final int newTabId = newTab.getId(); // We don't want to hold a reference to the Tab.
 
             final String message = isPrivate ?
                     getResources().getString(R.string.new_private_tab_opened) :
                     getResources().getString(R.string.new_tab_opened);
             final String buttonMessage = getResources().getString(R.string.switch_button_message);
             final GeckoApp geckoApp = (GeckoApp) context;
             geckoApp.getButtonToast().show(false,
@@ -270,32 +270,16 @@ abstract class HomeFragment extends Frag
         mCanLoadHint = canLoadHint;
         loadIfVisible();
     }
 
     boolean getCanLoadHint() {
         return mCanLoadHint;
     }
 
-    /**
-     * Given a url with a user-entered scheme, extract the
-     * scheme-specific component. For e.g, given "user-entered://www.google.com",
-     * this method returns "//www.google.com". If the passed url
-     * does not have a user-entered scheme, the same url will be returned.
-     *
-     * @param  url to be decoded
-     * @return url component entered by user
-     */
-    public static String decodeUserEnteredUrl(String url) {
-        Uri uri = Uri.parse(url);
-        if ("user-entered".equals(uri.getScheme())) {
-            return uri.getSchemeSpecificPart();
-        }
-        return url;
-    }
 
     protected abstract void load();
 
     protected boolean canLoad() {
         return (mCanLoadHint && isVisible() && getUserVisibleHint());
     }
 
     protected void loadIfVisible() {
--- a/mobile/android/base/home/PinSiteDialog.java
+++ b/mobile/android/base/home/PinSiteDialog.java
@@ -5,16 +5,17 @@
 
 package org.mozilla.gecko.home;
 
 import java.util.EnumSet;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.db.BrowserDB.FilterFlags;
 import org.mozilla.gecko.db.BrowserDB.URLColumns;
+import org.mozilla.gecko.util.StringUtils;
 
 import android.content.Context;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.support.v4.app.DialogFragment;
 import android.support.v4.app.LoaderManager;
 import android.support.v4.app.LoaderManager.LoaderCallbacks;
 import android.support.v4.content.Loader;
@@ -109,17 +110,17 @@ class PinSiteDialog extends DialogFragme
                 if (keyCode != KeyEvent.KEYCODE_ENTER || mOnSiteSelectedListener == null) {
                     return false;
                 }
 
                 // If the user manually entered a search term or URL, wrap the value in
                 // a special URI until we can get a valid URL for this bookmark.
                 final String text = mSearch.getText().toString().trim();
                 if (!TextUtils.isEmpty(text)) {
-                    final String url = TopSitesPanel.encodeUserEnteredUrl(text);
+                    final String url = StringUtils.encodeUserEnteredUrl(text);
                     mOnSiteSelectedListener.onSiteSelected(url, text);
                     dismiss();
                 }
 
                 return true;
             }
         });
 
--- a/mobile/android/base/home/TopSitesGridView.java
+++ b/mobile/android/base/home/TopSitesGridView.java
@@ -9,16 +9,17 @@ import java.util.EnumSet;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.ThumbnailHelper;
 import org.mozilla.gecko.db.BrowserContract.TopSites;
 import org.mozilla.gecko.db.TopSitesCursorWrapper;
 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
+import org.mozilla.gecko.util.StringUtils;
 
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.database.Cursor;
 import android.graphics.Rect;
 import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.view.ContextMenu.ContextMenuInfo;
@@ -101,17 +102,17 @@ public class TopSitesGridView extends Gr
         super.onAttachedToWindow();
 
         setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                 TopSitesGridItemView item = (TopSitesGridItemView) view;
 
                 // Decode "user-entered" URLs before loading them.
-                String url = HomeFragment.decodeUserEnteredUrl(item.getUrl());
+                String url = StringUtils.decodeUserEnteredUrl(item.getUrl());
                 int type = item.getType();
 
                 // If the url is empty, the user can pin a site.
                 // If not, navigate to the page given by the url.
                 if (type != TopSites.TYPE_BLANK) {
                     if (mUrlOpenListener != null) {
                         final TelemetryContract.Method method;
                         if (type == TopSites.TYPE_SUGGESTED) {
--- a/mobile/android/base/home/TopSitesPanel.java
+++ b/mobile/android/base/home/TopSitesPanel.java
@@ -351,17 +351,18 @@ public class TopSitesPanel extends HomeF
 
             Telemetry.sendUIEvent(TelemetryContract.Event.UNPIN);
 
             return true;
         }
 
         if (itemId == R.id.top_sites_edit) {
             // Decode "user-entered" URLs before showing them.
-            mEditPinnedSiteListener.onEditPinnedSite(info.position, decodeUserEnteredUrl(info.url));
+            mEditPinnedSiteListener.onEditPinnedSite(info.position,
+                                                     StringUtils.decodeUserEnteredUrl(info.url));
 
             Telemetry.sendUIEvent(TelemetryContract.Event.EDIT);
             return true;
         }
 
         return false;
     }
 
@@ -373,20 +374,16 @@ public class TopSitesPanel extends HomeF
         // visited, we want to load it as quickly as possible. Heavy load on
         // the Gecko thread can slow down the time it takes for thumbnails to
         // appear, especially during startup (bug 897162). By minimizing the
         // Gecko thread priority, we ensure that the UI appears quickly. The
         // priority is reset to normal once thumbnails are loaded.
         ThreadUtils.reduceGeckoPriority(PRIORITY_RESET_TIMEOUT);
     }
 
-    static String encodeUserEnteredUrl(String url) {
-        return Uri.fromParts("user-entered", url, null).toString();
-    }
-
     /**
      * Listener for editing pinned sites.
      */
     private class EditPinnedSiteListener implements OnEditPinnedSiteListener,
                                                     OnSiteSelectedListener {
         // Tag for the PinSiteDialog fragment.
         private static final String TAG_PIN_SITE = "pin_site";
 
--- a/mobile/android/base/util/StringUtils.java
+++ b/mobile/android/base/util/StringUtils.java
@@ -155,9 +155,30 @@ public class StringUtils {
         return url.substring(FILTER_URL_PREFIX.length());
     }
 
     public static boolean isShareableUrl(final String url) {
         final String scheme = Uri.parse(url).getScheme();
         return !("about".equals(scheme) || "chrome".equals(scheme) ||
                 "file".equals(scheme) || "resource".equals(scheme));
     }
+
+    /**
+     * Given a url with a user-entered scheme, extract the
+     * scheme-specific component. For e.g, given "user-entered://www.google.com",
+     * this method returns "//www.google.com". If the passed url
+     * does not have a user-entered scheme, the same url will be returned.
+     *
+     * @param  url to be decoded
+     * @return url component entered by user
+     */
+    public static String decodeUserEnteredUrl(String url) {
+        Uri uri = Uri.parse(url);
+        if ("user-entered".equals(uri.getScheme())) {
+            return uri.getSchemeSpecificPart();
+        }
+        return url;
+    }
+
+    public static String encodeUserEnteredUrl(String url) {
+        return Uri.fromParts("user-entered", url, null).toString();
+    }
 }