From eda68cee3d889d3cfb3602e053b06e3fe2297116 Mon Sep 17 00:00:00 2001 From: Jim Chen Date: Wed, 10 Jun 2015 00:25:01 -0400 Subject: [PATCH] Bug 1166309 - Make GeckoJarReader load mozglue if necessary; r=mfinkle r=rnewman --- mobile/android/base/BrowserLocaleManager.java | 2 +- mobile/android/base/db/LocalBrowserDB.java | 2 +- mobile/android/base/favicons/Favicons.java | 3 ++- .../base/favicons/LoadFaviconTask.java | 2 +- mobile/android/base/gfx/BitmapUtils.java | 6 +++-- .../base/updater/UpdateServiceHelper.java | 2 +- mobile/android/base/util/GeckoJarReader.java | 22 +++++++++++-------- .../search/providers/SearchEngineManager.java | 9 ++++---- .../tests/browser/junit3/TestJarReader.java | 13 ++++++----- .../tests/browser/robocop/testJarReader.java | 13 ++++++----- 10 files changed, 44 insertions(+), 30 deletions(-) diff --git a/mobile/android/base/BrowserLocaleManager.java b/mobile/android/base/BrowserLocaleManager.java index 86087fdc78fe..c5433a02bdda 100644 --- a/mobile/android/base/BrowserLocaleManager.java +++ b/mobile/android/base/BrowserLocaleManager.java @@ -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; diff --git a/mobile/android/base/db/LocalBrowserDB.java b/mobile/android/base/db/LocalBrowserDB.java index 9a897b185e7e..a98217e9b627 100644 --- a/mobile/android/base/db/LocalBrowserDB.java +++ b/mobile/android/base/db/LocalBrowserDB.java @@ -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); } diff --git a/mobile/android/base/favicons/Favicons.java b/mobile/android/base/favicons/Favicons.java index afb2a86e72ae..43f4408fa75d 100644 --- a/mobile/android/base/favicons/Favicons.java +++ b/mobile/android/base/favicons/Favicons.java @@ -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!"); diff --git a/mobile/android/base/favicons/LoadFaviconTask.java b/mobile/android/base/favicons/LoadFaviconTask.java index f49797027016..77bf3e1fe3de 100644 --- a/mobile/android/base/favicons/LoadFaviconTask.java +++ b/mobile/android/base/favicons/LoadFaviconTask.java @@ -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); diff --git a/mobile/android/base/gfx/BitmapUtils.java b/mobile/android/base/gfx/BitmapUtils.java index 7c46e6e135dc..4d1d5bff40cf 100644 --- a/mobile/android/base/gfx/BitmapUtils.java +++ b/mobile/android/base/gfx/BitmapUtils.java @@ -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); diff --git a/mobile/android/base/updater/UpdateServiceHelper.java b/mobile/android/base/updater/UpdateServiceHelper.java index 7fc5ed4428f2..9a8f428a2d15 100644 --- a/mobile/android/base/updater/UpdateServiceHelper.java +++ b/mobile/android/base/updater/UpdateServiceHelper.java @@ -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(); } diff --git a/mobile/android/base/util/GeckoJarReader.java b/mobile/android/base/util/GeckoJarReader.java index b690e06a8a44..c2a3dce1a54d 100644 --- a/mobile/android/base/util/GeckoJarReader.java +++ b/mobile/android/base/util/GeckoJarReader.java @@ -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 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 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 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; diff --git a/mobile/android/search/java/org/mozilla/search/providers/SearchEngineManager.java b/mobile/android/search/java/org/mozilla/search/providers/SearchEngineManager.java index e8531e808d24..c951229f9544 100644 --- a/mobile/android/search/java/org/mozilla/search/providers/SearchEngineManager.java +++ b/mobile/android/search/java/org/mozilla/search/providers/SearchEngineManager.java @@ -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 { diff --git a/mobile/android/tests/browser/junit3/src/org/mozilla/tests/browser/junit3/TestJarReader.java b/mobile/android/tests/browser/junit3/src/org/mozilla/tests/browser/junit3/TestJarReader.java index fe0dfffb43fe..cc8b058a9878 100644 --- a/mobile/android/tests/browser/junit3/src/org/mozilla/tests/browser/junit3/TestJarReader.java +++ b/mobile/android/tests/browser/junit3/src/org/mozilla/tests/browser/junit3/TestJarReader.java @@ -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); } } diff --git a/mobile/android/tests/browser/robocop/testJarReader.java b/mobile/android/tests/browser/robocop/testJarReader.java index 82d82fbaa253..81109ef14c81 100644 --- a/mobile/android/tests/browser/robocop/testJarReader.java +++ b/mobile/android/tests/browser/robocop/testJarReader.java @@ -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