Rework ordering in ExtensionsService::UninstallExtension()

BUG=39147
TEST=Existing tests in extensions_service_unittest.cc

Review URL: http://codereview.chromium.org/1278003
Patch from Mattias Nissler.

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42752 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/extensions/extensions_service.cc b/chrome/browser/extensions/extensions_service.cc
index 6807f6a..9c24464 100644
--- a/chrome/browser/extensions/extensions_service.cc
+++ b/chrome/browser/extensions/extensions_service.cc
@@ -325,26 +325,32 @@
 
   // Callers should not send us nonexistant extensions.
   DCHECK(extension);
-  GURL extension_url(extension->url());
 
-  extension_prefs_->OnExtensionUninstalled(extension, external_uninstall);
+  // Get hold of information we need after unloading, since the extension
+  // pointer will be invalid then.
+  GURL extension_url(extension->url());
+  Extension::Location location(extension->location());
+
+  // Also copy the extension identifier since the reference might have been
+  // obtained via Extension::id().
+  std::string extension_id_copy(extension_id);
+
+  // Unload before doing more cleanup to ensure that nothing is hanging on to
+  // any of these resources.
+  UnloadExtension(extension_id);
+
+  extension_prefs_->OnExtensionUninstalled(extension_id_copy, location,
+                                           external_uninstall);
 
   // Tell the backend to start deleting installed extensions on the file thread.
-  if (Extension::LOAD != extension->location()) {
+  if (Extension::LOAD != location) {
     ChromeThread::PostTask(
       ChromeThread::FILE, FROM_HERE,
       NewRunnableFunction(
-          &extension_file_util::UninstallExtension, extension_id,
+          &extension_file_util::UninstallExtension, extension_id_copy,
           install_directory_));
   }
 
-  ExtensionDOMUI::UnregisterChromeURLOverrides(profile_,
-      extension->GetChromeURLOverrides());
-
-  // TODO(mnissler, erikkay) Check whether we should really unload the extension
-  // first, so we we're sure it's not running while we clean up.
-  UnloadExtension(extension_id);
-
   ClearExtensionData(extension_url);
 }