Bug 1166309 - Make GeckoJarReader load mozglue if necessary; r=mfinkle r=rnewman

This commit is contained in:
Jim Chen 2015-06-10 00:25:01 -04:00
parent e44c4f499f
commit 7a9fcd017d
10 changed files with 44 additions and 30 deletions

View File

@ -400,7 +400,7 @@ public class BrowserLocaleManager implements LocaleManager {
final String resPath = "res/multilocale.json";
final String jarURL = GeckoJarReader.getJarURL(context, resPath);
final String contents = GeckoJarReader.getText(jarURL);
final String contents = GeckoJarReader.getText(context, jarURL);
if (contents == null) {
// GeckoJarReader logs and swallows exceptions.
return null;

View File

@ -483,7 +483,7 @@ public class LocalBrowserDB implements BrowserDB {
}
final String bitmapPath = GeckoJarReader.getJarURL(context, context.getString(faviconId));
final InputStream iStream = GeckoJarReader.getStream(bitmapPath);
final InputStream iStream = GeckoJarReader.getStream(context, bitmapPath);
return IOUtils.readFully(iStream, DEFAULT_FAVICON_BUFFER_SIZE);
}

View File

@ -494,7 +494,8 @@ public class Favicons {
}
private static Bitmap loadBrandingBitmap(Context context, String name) {
Bitmap b = GeckoJarReader.getBitmap(context.getResources(),
Bitmap b = GeckoJarReader.getBitmap(context,
context.getResources(),
getBrandingBitmapPath(context, name));
if (b == null) {
throw new IllegalStateException("Bitmap " + name + " missing from JAR!");

View File

@ -196,7 +196,7 @@ public class LoadFaviconTask {
if (uri.startsWith("jar:jar:")) {
Log.d(LOGTAG, "Fetching favicon from JAR.");
try {
return GeckoJarReader.getBitmap(context.getResources(), uri);
return GeckoJarReader.getBitmap(context, context.getResources(), uri);
} catch (Exception e) {
// Just about anything could happen here.
Log.w(LOGTAG, "Error fetching favicon from JAR.", e);

View File

@ -86,12 +86,14 @@ public final class BitmapUtils {
public Drawable doInBackground() {
try {
if (data.startsWith("jar:jar")) {
return GeckoJarReader.getBitmapDrawable(context.getResources(), data);
return GeckoJarReader.getBitmapDrawable(
context, context.getResources(), data);
}
// Don't attempt to validate the JAR signature when loading an add-on icon
if (data.startsWith("jar:file")) {
return GeckoJarReader.getBitmapDrawable(context.getResources(), Uri.decode(data));
return GeckoJarReader.getBitmapDrawable(
context, context.getResources(), Uri.decode(data));
}
final URL url = new URL(data);

View File

@ -103,7 +103,7 @@ public class UpdateServiceHelper {
ApplicationInfo info = pm.getApplicationInfo(AppConstants.ANDROID_PACKAGE_NAME, 0);
String updateLocaleUrl = "jar:jar:file://" + info.sourceDir + "!/" + AppConstants.OMNIJAR_NAME + "!/update.locale";
final String jarLocale = GeckoJarReader.getText(updateLocaleUrl);
final String jarLocale = GeckoJarReader.getText(context, updateLocaleUrl);
if (jarLocale != null) {
locale = jarLocale.trim();
}

View File

@ -6,6 +6,7 @@ package org.mozilla.gecko.util;
import android.content.Context;
import org.mozilla.gecko.AppConstants;
import org.mozilla.gecko.mozglue.GeckoLoader;
import org.mozilla.gecko.mozglue.NativeZip;
import android.content.res.Resources;
@ -31,12 +32,13 @@ public final class GeckoJarReader {
private GeckoJarReader() {}
public static Bitmap getBitmap(Resources resources, String url) {
BitmapDrawable drawable = getBitmapDrawable(resources, url);
public static Bitmap getBitmap(Context context, Resources resources, String url) {
BitmapDrawable drawable = getBitmapDrawable(context, resources, url);
return (drawable != null) ? drawable.getBitmap() : null;
}
public static BitmapDrawable getBitmapDrawable(Resources resources, String url) {
public static BitmapDrawable getBitmapDrawable(Context context, Resources resources,
String url) {
Stack<String> jarUrls = parseUrl(url);
InputStream inputStream = null;
BitmapDrawable bitmap = null;
@ -44,7 +46,7 @@ public final class GeckoJarReader {
NativeZip zip = null;
try {
// Load the initial jar file as a zip
zip = getZipFile(jarUrls.pop());
zip = getZipFile(context, jarUrls.pop());
inputStream = getStream(zip, jarUrls, url);
if (inputStream != null) {
bitmap = new BitmapDrawable(resources, inputStream);
@ -67,14 +69,14 @@ public final class GeckoJarReader {
return bitmap;
}
public static String getText(String url) {
public static String getText(Context context, String url) {
Stack<String> jarUrls = parseUrl(url);
NativeZip zip = null;
BufferedReader reader = null;
String text = null;
try {
zip = getZipFile(jarUrls.pop());
zip = getZipFile(context, jarUrls.pop());
InputStream input = getStream(zip, jarUrls, url);
if (input != null) {
reader = new BufferedReader(new InputStreamReader(input));
@ -98,16 +100,18 @@ public final class GeckoJarReader {
return text;
}
private static NativeZip getZipFile(String url) throws IOException, URISyntaxException {
private static NativeZip getZipFile(Context context, String url)
throws IOException, URISyntaxException {
URI fileUrl = new URI(url);
GeckoLoader.loadMozGlue(context);
return new NativeZip(fileUrl.getPath());
}
@RobocopTarget
public static InputStream getStream(String url) {
public static InputStream getStream(Context context, String url) {
Stack<String> jarUrls = parseUrl(url);
try {
NativeZip zip = getZipFile(jarUrls.pop());
NativeZip zip = getZipFile(context, jarUrls.pop());
return getStream(zip, jarUrls, url);
} catch (Exception ex) {
// Some JNI code throws IllegalArgumentException on a bad file name;

View File

@ -619,7 +619,7 @@ public class SearchEngineManager implements SharedPreferences.OnSharedPreference
final String languageTag = Locales.getLanguageTag(locale);
String url = getSearchPluginsJarURL(context, languageTag, fileName);
InputStream in = GeckoJarReader.getStream(url);
InputStream in = GeckoJarReader.getStream(context, url);
if (in != null) {
return in;
}
@ -628,7 +628,7 @@ public class SearchEngineManager implements SharedPreferences.OnSharedPreference
final String language = Locales.getLanguage(locale);
if (!languageTag.equals(language)) {
url = getSearchPluginsJarURL(context, language, fileName);
in = GeckoJarReader.getStream(url);
in = GeckoJarReader.getStream(context, url);
if (in != null) {
return in;
}
@ -636,7 +636,7 @@ public class SearchEngineManager implements SharedPreferences.OnSharedPreference
// Finally, fall back to default locale defined in chrome registry.
url = getSearchPluginsJarURL(context, getFallbackLocale(), fileName);
return GeckoJarReader.getStream(url);
return GeckoJarReader.getStream(context, url);
}
/**
@ -650,7 +650,8 @@ public class SearchEngineManager implements SharedPreferences.OnSharedPreference
return fallbackLocale;
}
final InputStream in = GeckoJarReader.getStream(GeckoJarReader.getJarURL(context, "chrome/chrome.manifest"));
final InputStream in = GeckoJarReader.getStream(
context, GeckoJarReader.getJarURL(context, "chrome/chrome.manifest"));
final BufferedReader br = getBufferedReader(in);
try {

View File

@ -9,38 +9,41 @@ import android.test.InstrumentationTestCase;
import org.mozilla.gecko.AppConstants;
import org.mozilla.gecko.util.GeckoJarReader;
import android.content.Context;
/**
* A basic jar reader test. Tests reading a png from fennec's apk, as well as
* loading some invalid jar urls.
*/
public class TestJarReader extends InstrumentationTestCase {
public void testJarReader() {
final Context context = getInstrumentation().getTargetContext().getApplicationContext();
String appPath = getInstrumentation().getTargetContext().getPackageResourcePath();
assertNotNull(appPath);
// Test reading a file from a jar url that looks correct.
String url = "jar:file://" + appPath + "!/" + AppConstants.OMNIJAR_NAME;
InputStream stream = GeckoJarReader.getStream("jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
InputStream stream = GeckoJarReader.getStream(context, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
assertNotNull(stream);
// Test looking for an non-existent file in a jar.
url = "jar:file://" + appPath + "!/" + AppConstants.OMNIJAR_NAME;
stream = GeckoJarReader.getStream("jar:" + url + "!/chrome/chrome/content/branding/nonexistent_file.png");
stream = GeckoJarReader.getStream(context, "jar:" + url + "!/chrome/chrome/content/branding/nonexistent_file.png");
assertNull(stream);
// Test looking for a file that doesn't exist in the APK.
url = "jar:file://" + appPath + "!/" + "BAD" + AppConstants.OMNIJAR_NAME;
stream = GeckoJarReader.getStream("jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
stream = GeckoJarReader.getStream(context, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
assertNull(stream);
// Test looking for an jar with an invalid url.
url = "jar:file://" + appPath + "!" + "!/" + AppConstants.OMNIJAR_NAME;
stream = GeckoJarReader.getStream("jar:" + url + "!/chrome/chrome/content/branding/nonexistent_file.png");
stream = GeckoJarReader.getStream(context, "jar:" + url + "!/chrome/chrome/content/branding/nonexistent_file.png");
assertNull(stream);
// Test looking for a file that doesn't exist on disk.
url = "jar:file://" + appPath + "BAD" + "!/" + AppConstants.OMNIJAR_NAME;
stream = GeckoJarReader.getStream("jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
stream = GeckoJarReader.getStream(context, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
assertNull(stream);
}
}

View File

@ -9,6 +9,8 @@ import java.io.InputStream;
import org.mozilla.gecko.AppConstants;
import org.mozilla.gecko.util.GeckoJarReader;
import android.content.Context;
/**
* A basic jar reader test. Tests reading a png from fennec's apk, as well
* as loading some invalid jar urls.
@ -22,32 +24,33 @@ public class testJarReader extends BaseTest {
}
public void testJarReader() {
final Context context = getInstrumentation().getTargetContext().getApplicationContext();
String appPath = getActivity().getApplication().getPackageResourcePath();
mAsserter.isnot(appPath, null, "getPackageResourcePath is non-null");
// Test reading a file from a jar url that looks correct.
String url = "jar:file://" + appPath + "!/" + AppConstants.OMNIJAR_NAME;
InputStream stream = GeckoJarReader.getStream("jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
InputStream stream = GeckoJarReader.getStream(context, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
mAsserter.isnot(stream, null, "JarReader returned non-null for valid file in valid jar");
// Test looking for an non-existent file in a jar.
url = "jar:file://" + appPath + "!/" + AppConstants.OMNIJAR_NAME;
stream = GeckoJarReader.getStream("jar:" + url + "!/chrome/chrome/content/branding/nonexistent_file.png");
stream = GeckoJarReader.getStream(context, "jar:" + url + "!/chrome/chrome/content/branding/nonexistent_file.png");
mAsserter.is(stream, null, "JarReader returned null for non-existent file in valid jar");
// Test looking for a file that doesn't exist in the APK.
url = "jar:file://" + appPath + "!/" + "BAD" + AppConstants.OMNIJAR_NAME;
stream = GeckoJarReader.getStream("jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
stream = GeckoJarReader.getStream(context, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
mAsserter.is(stream, null, "JarReader returned null for valid file in invalid jar file");
// Test looking for an jar with an invalid url.
url = "jar:file://" + appPath + "!" + "!/" + AppConstants.OMNIJAR_NAME;
stream = GeckoJarReader.getStream("jar:" + url + "!/chrome/chrome/content/branding/nonexistent_file.png");
stream = GeckoJarReader.getStream(context, "jar:" + url + "!/chrome/chrome/content/branding/nonexistent_file.png");
mAsserter.is(stream, null, "JarReader returned null for bad jar url");
// Test looking for a file that doesn't exist on disk.
url = "jar:file://" + appPath + "BAD" + "!/" + AppConstants.OMNIJAR_NAME;
stream = GeckoJarReader.getStream("jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
stream = GeckoJarReader.getStream(context, "jar:" + url + "!/chrome/chrome/content/branding/favicon32.png");
mAsserter.is(stream, null, "JarReader returned null for a non-existent APK");
// This test completes very quickly. If it completes too soon, the