Reland: Refactor code that defers extension background page loading

src/extensions depends on chrome::NOTIFICATION_PROFILE_CREATED to support
deferred loading of extension background pages when the profile isn't ready
yet. This is a layering violation.

* Remove Chrome concepts like "browser window ready" and "profile created"
from ProcessManager. Introduce ProcessManagerDelegate with a general concept
of deferring background page loading.
* Consolidate all the tricky Chrome-specific background page loading rules into
ChromeProcessManagerDelegate. This keeps all the rules in one place. Annotate
each block of special case code with the bug that inspired it.
* Extend unit test coverage for ProcessManager.

This will make it easier to eliminate chrome::NOTIFICATION_PROFILE_DESTROYED
in ProcessManager in a later CL.

(Original CL https://codereview.chromium.org/381283002 broke valgrind bots
because it was initializing left-over BrowserContextKeyedServices from tests
running earlier in the same process.)

BUG=392658
TEST=unit_tests ProcessManagerTest, browser_tests ProcessManagerBrowserTest, manual

Review URL: https://codereview.chromium.org/408523005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284593 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/extensions/chrome_extensions_browser_client.cc b/chrome/browser/extensions/chrome_extensions_browser_client.cc
index c66d0236..db2ae3b 100644
--- a/chrome/browser/extensions/chrome_extensions_browser_client.cc
+++ b/chrome/browser/extensions/chrome_extensions_browser_client.cc
@@ -21,7 +21,6 @@
 #include "chrome/browser/external_protocol/external_protocol_handler.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/profiles/profile_manager.h"
-#include "chrome/browser/ui/browser_finder.h"
 #include "chrome/common/chrome_paths.h"
 #include "chrome/common/chrome_switches.h"
 #include "chrome/common/chrome_version_info.h"
@@ -42,12 +41,14 @@
 #include "chrome/browser/extensions/activity_log/activity_log.h"
 #include "chrome/browser/extensions/api/chrome_extensions_api_client.h"
 #include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
+#include "chrome/browser/extensions/chrome_process_manager_delegate.h"
 #endif
 
 namespace extensions {
 
 ChromeExtensionsBrowserClient::ChromeExtensionsBrowserClient() {
 #if defined(ENABLE_EXTENSIONS)
+  process_manager_delegate_.reset(new ChromeProcessManagerDelegate);
   api_client_.reset(new ChromeExtensionsAPIClient);
 #endif
   // Only set if it hasn't already been set (e.g. by a test).
@@ -158,35 +159,15 @@
 #endif
 }
 
-bool ChromeExtensionsBrowserClient::DeferLoadingBackgroundHosts(
-    content::BrowserContext* context) const {
-  Profile* profile = static_cast<Profile*>(context);
-
-  // The profile may not be valid yet if it is still being initialized.
-  // In that case, defer loading, since it depends on an initialized profile.
-  // http://crbug.com/222473
-  if (!g_browser_process->profile_manager()->IsValidProfile(profile))
-    return true;
-
-#if defined(OS_ANDROID)
-  return false;
+ProcessManagerDelegate*
+ChromeExtensionsBrowserClient::GetProcessManagerDelegate() const {
+#if defined(ENABLE_EXTENSIONS)
+  return process_manager_delegate_.get();
 #else
-  // There are no browser windows open and the browser process was
-  // started to show the app launcher.
-  return chrome::GetTotalBrowserCountForProfile(profile) == 0 &&
-         CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowAppList);
+  return NULL;
 #endif
 }
 
-bool ChromeExtensionsBrowserClient::IsBackgroundPageAllowed(
-    content::BrowserContext* context) const {
-  // Returns true if current session is Guest mode session and current
-  // browser context is *not* off-the-record. Such context is artificial and
-  // background page shouldn't be created in it.
-  return !static_cast<Profile*>(context)->IsGuestSession() ||
-         context->IsOffTheRecord();
-}
-
 scoped_ptr<ExtensionHostDelegate>
 ChromeExtensionsBrowserClient::CreateExtensionHostDelegate() {
   return scoped_ptr<ExtensionHostDelegate>(new ChromeExtensionHostDelegate);