Fix 3373012, null pointer in WebView.loadUrl

UrlHandler can spawn a background RLZTask which will, upon completion,
load the url. This change checks with the controller to see that the
tab is still open before loading the url.

Change-Id: If756c1bc1f79b54878c7d03b5bc5f86b8ab07245
diff --git a/src/com/android/browser/UrlHandler.java b/src/com/android/browser/UrlHandler.java
index 9e41990..f1d1c4c 100644
--- a/src/com/android/browser/UrlHandler.java
+++ b/src/com/android/browser/UrlHandler.java
@@ -119,8 +119,7 @@
         return false;
     }
 
-    boolean startActivityForUrl(String url)
-    {
+    boolean startActivityForUrl(String url) {
       Intent intent;
       // perform generic parsing of the URI to turn it into an Intent.
       try {
@@ -173,8 +172,7 @@
 
     // In case a physical keyboard is attached, handle clicks with the menu key
     // depressed by opening in a new tab
-    boolean handleMenuClick(Tab tab, String url)
-    {
+    boolean handleMenuClick(Tab tab, String url) {
         if (mController.isMenuDown()) {
             mController.openTab(tab, url, false);
             mActivity.closeOptionsMenu();
@@ -184,6 +182,8 @@
         return false;
     }
 
+    // TODO: Move this class into Tab, where it can be properly stopped upon
+    // closure of the tab
     private class RLZTask extends AsyncTask<Void, Void, String> {
         private Tab mTab;
         private Uri mSiteUri;
@@ -215,10 +215,13 @@
         }
 
         protected void onPostExecute(String result) {
-            // If the Activity Manager is not invoked, load the URL directly
-            if (!startActivityForUrl(result)) {
-                if (!handleMenuClick(mTab, result)) {
-                    mController.loadUrl(mWebView, result);
+            // Make sure the Tab was not closed while handling the task
+            if (mController.getTabControl().getTabIndex(mTab) != -1) {
+                // If the Activity Manager is not invoked, load the URL directly
+                if (!startActivityForUrl(result)) {
+                    if (!handleMenuClick(mTab, result)) {
+                            mController.loadUrl(mWebView, result);
+                    }
                 }
             }
         }