Bug 1107925 - Don't launch fennec on search redirects. r=bnicholson, a=sledru
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Thu, 08 Jan 2015 12:48:23 -0800
changeset 242844 6796cf5b59b1
parent 242843 b71146fc0e37
child 242845 06bb4d89e2bf
push id4320
push userryanvm@gmail.com
push date2015-01-14 14:48 +0000
treeherdermozilla-beta@06bb4d89e2bf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbnicholson, sledru
bugs1107925
milestone36.0
Bug 1107925 - Don't launch fennec on search redirects. r=bnicholson, a=sledru
mobile/android/search/java/org/mozilla/search/PostSearchFragment.java
mobile/android/search/java/org/mozilla/search/providers/SearchEngine.java
--- a/mobile/android/search/java/org/mozilla/search/PostSearchFragment.java
+++ b/mobile/android/search/java/org/mozilla/search/PostSearchFragment.java
@@ -1,15 +1,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.search;
 
+import java.net.MalformedURLException;
 import java.net.URISyntaxException;
+import java.net.URL;
 
 import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.search.providers.SearchEngine;
 
 import android.content.Intent;
@@ -35,16 +37,18 @@ public class PostSearchFragment extends 
     private static final String LOG_TAG = "PostSearchFragment";
 
     private SearchEngine engine;
 
     private ProgressBar progressBar;
     private WebView webview;
     private View errorView;
 
+    private String resultsPageHost;
+
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
         View mainView = inflater.inflate(R.layout.search_fragment_post_search, container, false);
 
         progressBar = (ProgressBar) mainView.findViewById(R.id.progress_bar);
 
         webview = (WebView) mainView.findViewById(R.id.webview);
@@ -67,16 +71,17 @@ public class PostSearchFragment extends 
     }
 
     public void startSearch(SearchEngine engine, String query) {
         this.engine = engine;
 
         final String url = engine.resultsUriForQuery(query);
         // Only load urls if the url is different than the webview's current url.
         if (!TextUtils.equals(webview.getUrl(), url)) {
+            resultsPageHost = null;
             webview.loadUrl(Constants.ABOUT_BLANK);
             webview.loadUrl(url);
         }
     }
 
     /**
      * A custom WebViewClient that intercepts every page load. This allows
      * us to decide whether to load the url here, or send it to Android
@@ -90,24 +95,32 @@ public class PostSearchFragment extends 
         @Override
         public void onPageStarted(WebView view, final String url, Bitmap favicon) {
             // Reset the error state.
             networkError = false;
         }
 
         @Override
         public boolean shouldOverrideUrlLoading(WebView view, String url) {
-            // Ignore about:blank URL loads.
-            if (TextUtils.equals(url, Constants.ABOUT_BLANK)) {
+            // Ignore about:blank URL loads and the first results page we try to load.
+            if (TextUtils.equals(url, Constants.ABOUT_BLANK) || resultsPageHost == null) {
                 return false;
             }
 
-            // If the URL is a results page, don't override the URL load, but
+            String host = null;
+            try {
+                host = new URL(url).getHost();
+            } catch (MalformedURLException e) {
+                Log.e(LOG_TAG, "Error getting host from URL loading in webview", e);
+            }
+
+            // If the host name is the same as the results page, don't override the URL load, but
             // do update the query in the search bar if possible.
-            if (engine.isSearchResultsPage(url)) {
+            if (TextUtils.equals(resultsPageHost, host)) {
+                // This won't work for results pages that redirect (e.g. Google in different country)
                 final String query = engine.queryForResultsUrl(url);
                 if (!TextUtils.isEmpty(query)) {
                     ((AcceptsSearchQuery) getActivity()).onQueryChange(query);
                 }
                 return false;
             }
 
             try {
@@ -127,17 +140,17 @@ public class PostSearchFragment extends 
 
                 startActivity(i);
                 return true;
             } catch (URISyntaxException e) {
                 Log.e(LOG_TAG, "Error parsing intent URI", e);
             }
 
             return false;
-}
+        }
 
         @Override
         public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
             Log.e(LOG_TAG, "Error loading search results: " + description);
 
             networkError = true;
 
             if (errorView == null) {
@@ -161,16 +174,24 @@ public class PostSearchFragment extends 
 
         @Override
         public void onPageFinished(WebView view, String url) {
             // Make sure the error view is hidden if the network error was fixed.
             if (errorView != null) {
                 errorView.setVisibility(networkError ? View.VISIBLE : View.GONE);
                 webview.setVisibility(networkError ? View.GONE : View.VISIBLE);
             }
+
+            if (!TextUtils.equals(url, Constants.ABOUT_BLANK) && resultsPageHost == null) {
+                try {
+                    resultsPageHost = new URL(url).getHost();
+                } catch (MalformedURLException e) {
+                    Log.e(LOG_TAG, "Error getting host from results page URL", e);
+                }
+            }
         }
     }
 
     /**
      * A custom WebChromeClient that allows us to inject CSS into
      * the head of the HTML and to monitor pageload progress.
      *
      * We use the WebChromeClient because it provides a hook to the titleReceived
--- a/mobile/android/search/java/org/mozilla/search/providers/SearchEngine.java
+++ b/mobile/android/search/java/org/mozilla/search/providers/SearchEngine.java
@@ -215,24 +215,16 @@ public class SearchEngine {
         return shortName;
     }
 
     public String getIconURL() {
         return iconURL;
     }
 
     /**
-     * Determine whether a particular url belongs to this search engine. If not,
-     * the url will be sent to Fennec.
-     */
-    public boolean isSearchResultsPage(String url) {
-        return getResultsUri().getAuthority().equalsIgnoreCase(Uri.parse(url).getAuthority());
-    }
-
-    /**
      * Finds the search query encoded in a given results URL.
      *
      * @param url Current results URL.
      * @return The search query, or an empty string if a query couldn't be found.
      */
     public String queryForResultsUrl(String url) {
         final Uri resultsUri = getResultsUri();
         final Set<String> names = StringUtils.getQueryParameterNames(resultsUri);