Bug 1107925 - Don't launch fennec on search redirects. r=bnicholson

--HG--
extra : rebase_source : 0e99b01c7b2d1fbd78e1dff646d9137684ed3af8
extra : amend_source : a4d5ca535158dd1bc0197d92738d8296d55c53c7
This commit is contained in:
Margaret Leibovic 2015-01-08 12:48:23 -08:00
Родитель 2756ce4d4b
Коммит 83f80afb6b
2 изменённых файлов: 26 добавлений и 13 удалений

Просмотреть файл

@ -4,7 +4,9 @@
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;
@ -40,6 +42,8 @@ public class PostSearchFragment extends Fragment {
private WebView webview;
private View errorView;
private String resultsPageHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@ -72,6 +76,7 @@ public class PostSearchFragment extends Fragment {
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);
}
@ -95,14 +100,22 @@ public class PostSearchFragment extends Fragment {
@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);
@ -132,7 +145,7 @@ public class PostSearchFragment extends Fragment {
}
return false;
}
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
@ -166,6 +179,14 @@ public class PostSearchFragment extends Fragment {
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);
}
}
}
}

Просмотреть файл

@ -219,14 +219,6 @@ public class SearchEngine {
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.
*