From 5586e917b8ab58d017380982f43702efdd7bf52a Mon Sep 17 00:00:00 2001 From: tedchoc Date: Thu, 21 May 2015 19:39:09 -0700 Subject: [PATCH] Fix NPE from policy manager de-registration on Android. The ChromeActivity#onDestroy can happen before initializeProcess is called, so this makes add and remove keep track of listeners and handle out of order better. BUG=490894 Review URL: https://codereview.chromium.org/1157493002 Cr-Commit-Position: refs/heads/master@{#331052} --- .../chrome/browser/ChromeMobileApplication.java | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/chrome/android/java_staging/src/org/chromium/chrome/browser/ChromeMobileApplication.java b/chrome/android/java_staging/src/org/chromium/chrome/browser/ChromeMobileApplication.java index 2fe4333b8b91..9a26ff57ed27 100644 --- a/chrome/android/java_staging/src/org/chromium/chrome/browser/ChromeMobileApplication.java +++ b/chrome/android/java_staging/src/org/chromium/chrome/browser/ChromeMobileApplication.java @@ -67,6 +67,8 @@ import org.chromium.content.browser.DownloadController; import org.chromium.printing.PrintingController; import org.chromium.ui.UiUtils; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; /** @@ -149,6 +151,8 @@ public class ChromeMobileApplication extends ChromiumApplication { private ChromeLifetimeController mChromeLifetimeController; private PrintingController mPrintingController; private PolicyManager mPolicyManager; + private List mPendingPolicyChangeListeners = + new ArrayList(); /** * This is called once per ChromeMobileApplication instance, which get created per process @@ -329,7 +333,12 @@ public class ChromeMobileApplication extends ChromiumApplication { ApplicationStatus.registerApplicationStateListener(createApplicationStateListener()); AppBannerManager.setAppDetailsDelegate(createAppDetailsDelegate()); mChromeLifetimeController = new ChromeLifetimeController(this); + mPolicyManager = new PolicyManager(); + for (int i = 0; i < mPendingPolicyChangeListeners.size(); i++) { + mPolicyManager.addPolicyChangeListener(mPendingPolicyChangeListeners.get(i)); + } + mPendingPolicyChangeListeners.clear(); registerPolicyProviders(mPolicyManager); PrefServiceBridge.getInstance().migratePreferences(this); @@ -403,6 +412,7 @@ public class ChromeMobileApplication extends ChromiumApplication { PartnerBrowserCustomizations.destroy(); ShareHelper.clearSharedScreenshots(this); mPolicyManager.destroy(); + mPendingPolicyChangeListeners.clear(); mPolicyManager = null; } } @@ -528,11 +538,25 @@ public class ChromeMobileApplication extends ChromiumApplication { manager.registerProvider(new AppRestrictionsProvider(getApplicationContext())); } + /** + * Add a listener to be notified upon policy changes. + */ public void addPolicyChangeListener(PolicyChangeListener listener) { + if (mPolicyManager == null) { + mPendingPolicyChangeListeners.add(listener); + return; + } mPolicyManager.addPolicyChangeListener(listener); } + /** + * Remove a listener to be notified upon policy changes. + */ public void removePolicyChangeListener(PolicyChangeListener listener) { + if (mPolicyManager == null) { + mPendingPolicyChangeListeners.remove(listener); + return; + } mPolicyManager.removePolicyChangeListener(listener); } -- 2.11.4.GIT