Bug 1131438 - SearchEngineManager doesn't scan the distro searchplugins locale folders or fallback to a distro locale r=margaret

This commit is contained in:
Mark Finkle 2015-02-22 16:59:33 -05:00
parent bc8438abf7
commit 8aefefbd4f

View File

@ -34,6 +34,8 @@ import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
public class SearchEngineManager implements SharedPreferences.OnSharedPreferenceChangeListener {
@ -42,6 +44,9 @@ public class SearchEngineManager implements SharedPreferences.OnSharedPreference
// Gecko pref that defines the name of the default search engine.
private static final String PREF_GECKO_DEFAULT_ENGINE = "browser.search.defaultenginename";
// Gecko pref that defines the name of the default searchplugin locale.
private static final String PREF_GECKO_DEFAULT_LOCALE = "distribution.searchplugins.defaultLocale";
// Key for shared preference that stores default engine name.
private static final String PREF_DEFAULT_ENGINE_KEY = "search.engines.defaultname";
@ -65,6 +70,10 @@ public class SearchEngineManager implements SharedPreferences.OnSharedPreference
// This should only be accessed from the background thread.
private String fallbackLocale;
// Cached version of default locale included in Distribution preferences.
// This should only be accessed from the background thread.
private String distributionLocale;
public static interface SearchEngineCallback {
public void execute(SearchEngine engine);
}
@ -239,7 +248,16 @@ public class SearchEngineManager implements SharedPreferences.OnSharedPreference
try {
final JSONObject all = new JSONObject(FileUtils.getFileContents(prefFile));
// First, check to see if there's a locale-specific override.
// First, look for a default locale specified by the distribution.
if (all.has("Preferences")) {
final JSONObject prefs = all.getJSONObject("Preferences");
if (prefs.has(PREF_GECKO_DEFAULT_LOCALE)) {
Log.d(LOG_TAG, "Found default searchplugin locale in distribution Preferences.");
distributionLocale = prefs.getString(PREF_GECKO_DEFAULT_LOCALE);
}
}
// Then, check to see if there's a locale-specific default engine override.
final String languageTag = Locales.getLanguageTag(Locale.getDefault());
final String overridesKey = "LocalizablePreferences." + languageTag;
if (all.has(overridesKey)) {
@ -250,7 +268,7 @@ public class SearchEngineManager implements SharedPreferences.OnSharedPreference
}
}
// Next, check to see if there's a non-override default pref.
// Next, check to see if there's a non-override default engine pref.
if (all.has("LocalizablePreferences")) {
final JSONObject localizablePrefs = all.getJSONObject("LocalizablePreferences");
if (localizablePrefs.has(PREF_GECKO_DEFAULT_ENGINE)) {
@ -443,12 +461,43 @@ public class SearchEngineManager implements SharedPreferences.OnSharedPreference
return null;
}
final File[] files = (new File(pluginsDir, "common")).listFiles();
if (files == null) {
// Collect an array of files to scan using the same approach as
// DirectoryService._appendDistroSearchDirs which states:
// Common engines are loaded for all locales. If there is no locale directory for
// the current locale, there is a pref: "distribution.searchplugins.defaultLocale",
// which specifies a default locale to use.
ArrayList<File> files = new ArrayList<>();
// Load files from the common folder first
final File[] commonFiles = (new File(pluginsDir, "common")).listFiles();
if (commonFiles != null) {
Collections.addAll(files, commonFiles);
}
// Next, check to see if there's a locale-specific override.
final File localeDir = new File(pluginsDir, "locale");
if (localeDir != null) {
final String languageTag = Locales.getLanguageTag(Locale.getDefault());
final File[] localeFiles = (new File(localeDir, languageTag)).listFiles();
if (localeFiles != null) {
Collections.addAll(files, localeFiles);
} else {
// We didn't append the locale dir - try the default one.
if (distributionLocale != null) {
final File[] defaultLocaleFiles = (new File(localeDir, distributionLocale)).listFiles();
if (defaultLocaleFiles != null) {
Collections.addAll(files, defaultLocaleFiles);
}
}
}
}
if (files.isEmpty()) {
Log.e(LOG_TAG, "Could not find search plugin files in distribution directory");
return null;
}
return createEngineFromFileList(files, name);
return createEngineFromFileList(files.toArray(new File[files.size()]), name);
}
/**