From 4190aab1c970780126f1d9db7a91579065cd6870 Mon Sep 17 00:00:00 2001 From: Kristian Monsen Date: Thu, 16 Dec 2010 18:39:21 +0000 Subject: [PATCH] Remove WebView leak We were leaking the first WebView created since we kept a reference in a static variable. Using the new way of sending messages in a static method to avoid this. Change-Id: Ibb665e32c22c16c17176cd69bf8f7e83fd94894f --- core/java/android/webkit/WebView.java | 65 ++++++++++++----------- core/java/android/webkit/WebViewCore.java | 34 ++++++------ 2 files changed, 49 insertions(+), 50 deletions(-) diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java index 05bb19d97bc9e..885badab408a8 100644 --- a/core/java/android/webkit/WebView.java +++ b/core/java/android/webkit/WebView.java @@ -991,10 +991,10 @@ public class WebView extends AbsoluteLayout } /* - * The intent receiver that monitors for changes to relevant packages (e.g., - * sGoogleApps) and notifies WebViewCore of their existence. + * A variable to track if there is a receiver added for ACTION_PACKAGE_ADDED + * or ACTION_PACKAGE_REMOVED. */ - private static BroadcastReceiver sPackageInstallationReceiver = null; + private static boolean sPackageInstallationReceiverAdded = false; /* * A set of Google packages we monitor for the @@ -1007,6 +1007,32 @@ public class WebView extends AbsoluteLayout sGoogleApps.add("com.google.android.youtube"); } + private static class PackageListener extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + final String action = intent.getAction(); + final String packageName = intent.getData().getSchemeSpecificPart(); + final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); + if (Intent.ACTION_PACKAGE_REMOVED.equals(action) && replacing) { + // if it is replacing, refreshPlugins() when adding + return; + } + + if (sGoogleApps.contains(packageName)) { + if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { + WebViewCore.sendStaticMessage(EventHub.ADD_PACKAGE_NAME, packageName); + } else { + WebViewCore.sendStaticMessage(EventHub.REMOVE_PACKAGE_NAME, packageName); + } + } + + PluginManager pm = PluginManager.getInstance(context); + if (pm.containsPluginPermissionAndSignatures(packageName)) { + pm.refreshPlugins(Intent.ACTION_PACKAGE_ADDED.equals(action)); + } + } + } + private void setupPackageListener(Context context) { /* @@ -1018,41 +1044,16 @@ public class WebView extends AbsoluteLayout // if the receiver already exists then we do not need to register it // again - if (sPackageInstallationReceiver != null) { + if (sPackageInstallationReceiverAdded) { return; } IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); filter.addAction(Intent.ACTION_PACKAGE_REMOVED); filter.addDataScheme("package"); - sPackageInstallationReceiver = new BroadcastReceiver() { - - @Override - public void onReceive(Context context, Intent intent) { - final String action = intent.getAction(); - final String packageName = intent.getData().getSchemeSpecificPart(); - final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); - if (Intent.ACTION_PACKAGE_REMOVED.equals(action) && replacing) { - // if it is replacing, refreshPlugins() when adding - return; - } - - if (sGoogleApps.contains(packageName) && mWebViewCore != null) { - if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { - mWebViewCore.sendMessage(EventHub.ADD_PACKAGE_NAME, packageName); - } else { - mWebViewCore.sendMessage(EventHub.REMOVE_PACKAGE_NAME, packageName); - } - } - - PluginManager pm = PluginManager.getInstance(context); - if (pm.containsPluginPermissionAndSignatures(packageName)) { - pm.refreshPlugins(Intent.ACTION_PACKAGE_ADDED.equals(action)); - } - } - }; - - context.getApplicationContext().registerReceiver(sPackageInstallationReceiver, filter); + BroadcastReceiver packageListener = new PackageListener(); + context.getApplicationContext().registerReceiver(packageListener, filter); + sPackageInstallationReceiverAdded = true; } // check if any of the monitored apps are already installed diff --git a/core/java/android/webkit/WebViewCore.java b/core/java/android/webkit/WebViewCore.java index fdd0710375252..f45fad92194cf 100644 --- a/core/java/android/webkit/WebViewCore.java +++ b/core/java/android/webkit/WebViewCore.java @@ -668,6 +668,22 @@ final class WebViewCore { Process.setThreadPriority( Process.THREAD_PRIORITY_DEFAULT); break; + + case EventHub.ADD_PACKAGE_NAME: + if (BrowserFrame.sJavaBridge == null) { + throw new IllegalStateException( + "No WebView has been created in this process!"); + } + BrowserFrame.sJavaBridge.addPackageName((String) msg.obj); + break; + + case EventHub.REMOVE_PACKAGE_NAME: + if (BrowserFrame.sJavaBridge == null) { + throw new IllegalStateException( + "No WebView has been created in this process!"); + } + BrowserFrame.sJavaBridge.removePackageName((String) msg.obj); + break; } } }; @@ -1488,24 +1504,6 @@ final class WebViewCore { (Set) msg.obj); break; - case ADD_PACKAGE_NAME: - if (BrowserFrame.sJavaBridge == null) { - throw new IllegalStateException("No WebView " + - "has been created in this process!"); - } - BrowserFrame.sJavaBridge.addPackageName( - (String) msg.obj); - break; - - case REMOVE_PACKAGE_NAME: - if (BrowserFrame.sJavaBridge == null) { - throw new IllegalStateException("No WebView " + - "has been created in this process!"); - } - BrowserFrame.sJavaBridge.removePackageName( - (String) msg.obj); - break; - case GET_TOUCH_HIGHLIGHT_RECTS: TouchHighlightData d = (TouchHighlightData) msg.obj; ArrayList rects = nativeGetTouchHighlightRects