Bug 1105316 - Look in Gecko chrome registry for fallback locale in search activity. r=rnewman

--HG--
extra : rebase_source : 42e908cf3c6d08cb8a108d6f7b11b169ef3ab0dd
This commit is contained in:
Margaret Leibovic 2014-12-09 12:45:13 -08:00
Родитель f9583f8348
Коммит 0fbd54f296
2 изменённых файлов: 59 добавлений и 13 удалений

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

@ -110,6 +110,7 @@ public final class GeckoJarReader {
// Some JNI code throws IllegalArgumentException on a bad file name;
// swallow the error and return null. We could also see legitimate
// IOExceptions here.
Log.e(LOGTAG, "Exception getting input stream from jar URL: " + url, ex);
return null;
}
}

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

@ -31,6 +31,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@ -49,6 +50,10 @@ public class SearchEngineManager implements SharedPreferences.OnSharedPreference
private SearchEngineCallback changeCallback;
private SearchEngine engine;
// Cached version of default locale included in Gecko chrome manifest.
// This should only be accessed from the background thread.
private String fallbackLocale;
public static interface SearchEngineCallback {
public void execute(SearchEngine engine);
}
@ -309,11 +314,9 @@ public class SearchEngineManager implements SharedPreferences.OnSharedPreference
*/
private SearchEngine createEngineFromLocale(String name) {
final InputStream in = getInputStreamFromSearchPluginsJar("list.txt");
InputStreamReader isr = null;
final BufferedReader br = getBufferedReader(in);
try {
isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
String identifier;
while ((identifier = br.readLine()) != null) {
final InputStream pluginIn = getInputStreamFromSearchPluginsJar(identifier + ".xml");
@ -325,15 +328,8 @@ public class SearchEngineManager implements SharedPreferences.OnSharedPreference
} catch (IOException e) {
Log.e(LOG_TAG, "Error creating shipped search engine from name: " + name, e);
} finally {
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
// Ignore.
}
}
try {
in.close();
br.close();
} catch (IOException e) {
// Ignore.
}
@ -439,11 +435,47 @@ public class SearchEngineManager implements SharedPreferences.OnSharedPreference
}
}
// Finally, fall back to en-US.
url = getSearchPluginsJarURL("en-US", fileName);
// Finally, fall back to default locale defined in chrome registry.
url = getSearchPluginsJarURL(getFallbackLocale(), fileName);
return GeckoJarReader.getStream(url);
}
/**
* Finds a fallback locale in the Gecko chrome registry. If a locale is declared
* here, we should be guaranteed to find a searchplugins directory for it.
*
* This method should only be accessed from the background thread.
*/
private String getFallbackLocale() {
if (fallbackLocale != null) {
return fallbackLocale;
}
final InputStream in = GeckoJarReader.getStream(getJarURL("!/chrome/chrome.manifest"));
final BufferedReader br = getBufferedReader(in);
try {
String line;
while ((line = br.readLine()) != null) {
// We're looking for a line like "locale global en-US en-US/locale/en-US/global/"
// https://developer.mozilla.org/en/docs/Chrome_Registration#locale
if (line.startsWith("locale global ")) {
fallbackLocale = line.split(" ", 4)[2];
break;
}
}
} catch (IOException e) {
Log.e(LOG_TAG, "Error reading fallback locale from chrome registry", e);
} finally {
try {
br.close();
} catch (IOException e) {
// Ignore.
}
}
return fallbackLocale;
}
/**
* Gets the jar URL for a file in the searchplugins directory.
*
@ -453,6 +485,19 @@ public class SearchEngineManager implements SharedPreferences.OnSharedPreference
*/
private String getSearchPluginsJarURL(String locale, String fileName) {
final String path = "!/chrome/" + locale + "/locale/" + locale + "/browser/searchplugins/" + fileName;
return getJarURL(path);
}
private String getJarURL(String path) {
return "jar:jar:file://" + context.getPackageResourcePath() + "!/" + AppConstants.OMNIJAR_NAME + path;
}
private BufferedReader getBufferedReader(InputStream in) {
try {
return new BufferedReader(new InputStreamReader(in, "UTF-8"));
} catch (UnsupportedEncodingException e) {
// Cannot happen.
return null;
}
}
}